/[muddleftpd]/muddleftpd/src/mudpasswd.c
ViewVC logotype

Diff of /muddleftpd/src/mudpasswd.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by ganneff, Thu Sep 26 07:55:42 2002 UTC revision 1.1.6.1 by ganneff, Mon Oct 21 19:52:58 2002 UTC
# Line 1  Line 1 
1    
2  /* Copyright (C) 1999 Beau Kuiper  /* Copyright (C) 1999 Beau Kuiper
3     this is a quick hack for mudpasswd, servicable 1 nighter     this is a quick hack for mudpasswd, servicable 1 nighter
4     this has had some more work on it now, it is safer to use and can't     this has had some more work on it now, it is safer to use and can't
# Line 44  Line 45 
45    
46  extern void showversion(char *desc);  extern void showversion(char *desc);
47    
48  char *strsplit(char *in, char *cmd, char *data)  char *strsplit(char *in,
49                               char *cmd,
50                               char *data)
51  {  {
52          int pos = 0;          int pos = 0;
53          int pos2 = 0;          int pos2 = 0;
54            
55          if (in[strlen(in) - 1] == 10)          if (in[strlen(in) - 1] == 10)
56                  in[strlen(in) - 1] = 0;                  in[strlen(in) - 1] = 0;
57                    
58          while((in[pos] <= 32) && (in[pos] != 0))          while ((in[pos] <= 32) && (in[pos] != 0))
59                  pos++;                  pos++;
60          if ((in[pos] == '#') || (in[pos] == 0))          if ((in[pos] == '#') || (in[pos] == 0))
61                  return(NULL);                  return (NULL);
62            
63          while((in[pos] != 32) && (in[pos] != 0))          while ((in[pos] != 32) && (in[pos] != 0))
64          {          {
65                  cmd[pos2] = in[pos];                  cmd[pos2] = in[pos];
66                  pos2++;                  pos2++;
67                  pos++;                  pos++;
68          }          }
69            
70          if (in[pos] == 0)          if (in[pos] == 0)
71                  return(NULL);                  return (NULL);
72            
73          cmd[pos2] = 0;          cmd[pos2] = 0;
74            
75          while((in[pos] <= 32) && (in[pos] != 0))          while ((in[pos] <= 32) && (in[pos] != 0))
76                  pos++;                  pos++;
77          if (in[pos] == 0)          if (in[pos] == 0)
78                  return(NULL);                  return (NULL);
79    
80          pos2 = 0;          pos2 = 0;
81          while((in[pos] != 32) && (in[pos] != 0) && (in[pos] != '\n'))          while ((in[pos] != 32) && (in[pos] != 0) && (in[pos] != '\n'))
82          {          {
83                  if ((in[pos] == '\\') && (in[pos+1] != 0))                  if ((in[pos] == '\\') && (in[pos + 1] != 0))
84                          pos++;                          pos++;
85                    
86                  data[pos2] = in[pos];                  data[pos2] = in[pos];
87                  pos2++;                  pos2++;
88                  pos++;                  pos++;
89          }          }
90            
91          data[pos2] = 0;          data[pos2] = 0;
92          return(data);          return (data);
93  }  }
94    
95  char getsaltchar(void)  char getsaltchar(void)
# Line 104  char getsaltchar(void) Line 107  char getsaltchar(void)
107                  outchr = '.';                  outchr = '.';
108          else          else
109                  outchr = '/';                  outchr = '/';
110            
111          return(outchr);          return (outchr);
112  }  }
113    
114  char *cryptedpass(char *clearpass)  char *cryptedpass(char *clearpass)
115  {  {
116          char salt[5];          char salt[5];
117            
118          srandom((unsigned int)time(NULL));          srandom((unsigned int) time(NULL));
119          salt[0] = getsaltchar();          salt[0] = getsaltchar();
120          salt[1] = getsaltchar();          salt[1] = getsaltchar();
121          salt[2] = 0;          salt[2] = 0;
122            
123          return(crypt(clearpass, salt));          return (crypt(clearpass, salt));
124  }  }
125    
126  void usage(void)  void usage(void)
# Line 135  void usage(void) Line 138  void usage(void)
138          printf("        -h              Displays this usage message.\n");          printf("        -h              Displays this usage message.\n");
139          printf("        -V              Displays version information.\n\n");          printf("        -V              Displays version information.\n\n");
140    
141          printf("You must specify -p and  only one of -a -e or -d when running mudpasswd\n\n");          printf
142                    ("You must specify -p and  only one of -a -e or -d when running mudpasswd\n\n");
143          exit(0);          exit(0);
144  }  }
145    
146  int searchuser(char *passfile, char *username)  int searchuser(char *passfile,
147                               char *username)
148  {  {
149          FILE *passwdfile;          FILE *passwdfile;
150          char instring[MAXSTRLEN], username2[MAXSTRLEN], data[MAXSTRLEN];          char instring[MAXSTRLEN], username2[MAXSTRLEN], data[MAXSTRLEN];
151            
152          passwdfile = fopen(passfile, "r");          passwdfile = fopen(passfile, "r");
153          if (!passwdfile)          if (!passwdfile)
154                  return(FALSE);                  return (FALSE);
155            
156          while (fgets(instring, MAXSTRLEN, passwdfile) != NULL)          while (fgets(instring, MAXSTRLEN, passwdfile) != NULL)
157          {          {
158                  if (strsplit(instring, username2, data) != NULL)                  if (strsplit(instring, username2, data) != NULL)
159                          if (strcmp(username2, username) == 0)                          if (strcmp(username2, username) == 0)
160                          {                          {
161                                  fclose(passwdfile);                                  fclose(passwdfile);
162                                  return(TRUE);                                  return (TRUE);
163                          }                          }
164          }          }
165            
166          fclose(passwdfile);          fclose(passwdfile);
167          return(FALSE);            return (FALSE);
168  }  }
169            
170  void adduser(char *passfile, char *username, char *rootdir, char *homedir,  void adduser(char *passfile,
171               char *password)                           char *username,
172                             char *rootdir,
173                             char *homedir,
174                             char *password)
175  {  {
176          FILE *passwdfile;          FILE *passwdfile;
177          int pos;          int pos;
178            
179          if (searchuser(passfile, username))          if (searchuser(passfile, username))
180                  ERRORMSGFATAL("Username already exists - Operation Failed!");                  ERRORMSGFATAL("Username already exists - Operation Failed!");
181    
182          passwdfile = fopen(passfile, "a+");          passwdfile = fopen(passfile, "a+");
183          if (passwdfile == NULL)          if (passwdfile == NULL)
184                  ERRORMSGFATAL("Cannot open password file!");                  ERRORMSGFATAL("Cannot open password file!");
185                    
186          pos = ftell(passwdfile);          pos = ftell(passwdfile);
187            
188          if (pos > 0)          if (pos > 0)
189          {          {
190                  char chkchr;                  char chkchr;
191                    
192                  fseek(passwdfile, -1, SEEK_CUR);                  fseek(passwdfile, -1, SEEK_CUR);
193                  chkchr = fgetc(passwdfile);                  chkchr = fgetc(passwdfile);
194                  if (chkchr != '\n')                  if (chkchr != '\n')
# Line 189  void adduser(char *passfile, char *usern Line 197  void adduser(char *passfile, char *usern
197                          fprintf(passwdfile, "\n");                          fprintf(passwdfile, "\n");
198                  }                  }
199          }          }
200            
201          if (!passwdfile)          if (!passwdfile)
202                  ERRORMSGFATAL("Couldn't open password file for writing!");                  ERRORMSGFATAL("Couldn't open password file for writing!");
203            
204          fprintf(passwdfile, "%s %s:%s:%s\n", username, cryptedpass(password),          fprintf(passwdfile, "%s %s:%s:%s\n", username, cryptedpass(password),
205                  homedir, rootdir);                          homedir, rootdir);
206          fclose(passwdfile);          fclose(passwdfile);
207  }  }
208    
209  void deleteuser(char *passfile, char *username)  void deleteuser(char *passfile,
210                                    char *username)
211  {  {
212          FILE *passwdfilein, *passwdfileout;          FILE *passwdfilein, *passwdfileout;
213          char instring[MAXSTRLEN], username2[MAXSTRLEN], data[MAXSTRLEN];          char instring[MAXSTRLEN], username2[MAXSTRLEN], data[MAXSTRLEN];
214          char newpassfile[MAXSTRLEN];          char newpassfile[MAXSTRLEN];
215            
216          if (!searchuser(passfile, username))          if (!searchuser(passfile, username))
217                  ERRORMSGFATAL("Username doesn't exists - Operation Failed!");                  ERRORMSGFATAL("Username doesn't exists - Operation Failed!");
218    
# Line 215  void deleteuser(char *passfile, char *us Line 224  void deleteuser(char *passfile, char *us
224                  ERRORMSGFATAL("Could not open password file!");                  ERRORMSGFATAL("Could not open password file!");
225    
226          passwdfileout = fopen(newpassfile, "w");          passwdfileout = fopen(newpassfile, "w");
227            
228          if (!passwdfileout)          if (!passwdfileout)
229                  ERRORMSGFATAL("Could not open replacement password file!");                  ERRORMSGFATAL("Could not open replacement password file!");
230            
231          while (fgets(instring, MAXSTRLEN, passwdfilein) != NULL)          while (fgets(instring, MAXSTRLEN, passwdfilein) != NULL)
232          {          {
233                  int n = TRUE;                  int n = TRUE;
234                    
235                  if (strsplit(instring, username2, data) != NULL)                  if (strsplit(instring, username2, data) != NULL)
236                          if (strcmp(username2, username) == 0)                          if (strcmp(username2, username) == 0)
237                                  n = FALSE;                                  n = FALSE;
238                  if (n)                  if (n)
239                          fprintf(passwdfileout, "%s\n", instring);                                fprintf(passwdfileout, "%s\n", instring);
240          }          }
241            
242          fclose(passwdfilein);          fclose(passwdfilein);
243          fclose(passwdfileout);          fclose(passwdfileout);
244          if (rename(newpassfile, passfile) == -1)          if (rename(newpassfile, passfile) == -1)
# Line 239  void deleteuser(char *passfile, char *us Line 248  void deleteuser(char *passfile, char *us
248          }          }
249  }  }
250    
251  void edituserstr(char *userstr, int attrmask, char *password, char *rootdir,  void edituserstr(char *userstr,
252                  char *homedir, char *username, char *data)                                   int attrmask,
253                                     char *password,
254                                     char *rootdir,
255                                     char *homedir,
256                                     char *username,
257                                     char *data)
258  {  {
259          char passwordold[MAXSTRLEN], homedirold[MAXSTRLEN], rootdirold[MAXSTRLEN];          char passwordold[MAXSTRLEN], homedirold[MAXSTRLEN], rootdirold[MAXSTRLEN];
260          char *pos;          char *pos;
261            
262          strcpy(passwordold, "");          strcpy(passwordold, "");
263          strcpy(homedirold, "");          strcpy(homedirold, "");
264          strcpy(rootdirold, "");          strcpy(rootdirold, "");
265            
266          /* get old settings */          /*
267             * get old settings
268             */
269          if ((pos = strchr(data, ':')) != NULL)          if ((pos = strchr(data, ':')) != NULL)
270          {          {
271                  strncpy(passwordold, data, 4096);                  strncpy(passwordold, data, 4096);
# Line 267  void edituserstr(char *userstr, int attr Line 283  void edituserstr(char *userstr, int attr
283                  }                  }
284          if (pos != NULL)          if (pos != NULL)
285                  strncpy(rootdirold, pos, 4096);                  strncpy(rootdirold, pos, 4096);
286            
287          if (attrmask & ROOTMSK)          if (attrmask & ROOTMSK)
288                  strncpy(rootdirold, rootdir, 4096);                  strncpy(rootdirold, rootdir, 4096);
289          if (attrmask & HOMEMSK)          if (attrmask & HOMEMSK)
290                  strncpy(homedirold, homedir, 4096);                  strncpy(homedirold, homedir, 4096);
291          if (attrmask & PASSMSK)          if (attrmask & PASSMSK)
292                  strncpy(passwordold, cryptedpass(password), 4096);                  strncpy(passwordold, cryptedpass(password), 4096);
293            
294          snprintf(userstr, MAXSTRLEN, "%s %s:%s:%s", username, passwordold, homedirold,          snprintf(userstr, MAXSTRLEN, "%s %s:%s:%s", username, passwordold,
295                  rootdirold);                           homedirold, rootdirold);
296          userstr[MAXSTRLEN-1] = 0;          userstr[MAXSTRLEN - 1] = 0;
297  }  }
298    
299  void edituser(char *passfile, char *username, char *rootdir, char *homedir,  void edituser(char *passfile,
300               char *password, int attrmask)                            char *username,
301                              char *rootdir,
302                              char *homedir,
303                              char *password,
304                              int attrmask)
305  {  {
306          FILE *passwdfilein, *passwdfileout;          FILE *passwdfilein, *passwdfileout;
307          char instring[MAXSTRLEN], username2[MAXSTRLEN], data[MAXSTRLEN];          char instring[MAXSTRLEN], username2[MAXSTRLEN], data[MAXSTRLEN];
308          char newpassfile[MAXSTRLEN];          char newpassfile[MAXSTRLEN];
309            
310          if (!searchuser(passfile, username))          if (!searchuser(passfile, username))
311                  ERRORMSGFATAL("Username doesn't exists - Operation Failed!");                  ERRORMSGFATAL("Username doesn't exists - Operation Failed!");
312    
# Line 296  void edituser(char *passfile, char *user Line 316  void edituser(char *passfile, char *user
316          passwdfilein = fopen(passfile, "r");          passwdfilein = fopen(passfile, "r");
317          if (!passwdfilein)          if (!passwdfilein)
318                  ERRORMSGFATAL("Could not open password file!");                  ERRORMSGFATAL("Could not open password file!");
319                    
320          passwdfileout = fopen(newpassfile, "w");          passwdfileout = fopen(newpassfile, "w");
321            
322          if (!passwdfileout)          if (!passwdfileout)
323                  ERRORMSGFATAL("Could not open replacement password file!");                  ERRORMSGFATAL("Could not open replacement password file!");
324            
325          while (fgets(instring, MAXSTRLEN, passwdfilein) != NULL)          while (fgets(instring, MAXSTRLEN, passwdfilein) != NULL)
326          {          {
327                  int n = TRUE;                  int n = TRUE;
328                    
329                  if (strsplit(instring, username2, data) != NULL)                  if (strsplit(instring, username2, data) != NULL)
330                          if (strcmp(username2, username) == 0)                          if (strcmp(username2, username) == 0)
331                                  edituserstr(instring, attrmask, password,                                  edituserstr(instring, attrmask, password,
332                                              rootdir, homedir, username, data);                                                          rootdir, homedir, username, data);
333                  if (n)                  if (n)
334                          fprintf(passwdfileout, "%s\n", instring);                                fprintf(passwdfileout, "%s\n", instring);
335          }          }
336            
337          fclose(passwdfilein);          fclose(passwdfilein);
338          fclose(passwdfileout);          fclose(passwdfileout);
339          if (rename(newpassfile, passfile) == -1)          if (rename(newpassfile, passfile) == -1)
# Line 323  void edituser(char *passfile, char *user Line 343  void edituser(char *passfile, char *user
343          }          }
344  }  }
345    
346  int main(int argc, char **argv)  int main(int argc,
347                     char **argv)
348  {  {
349          int ch;          int ch;
350          char *passfile = NULL;          char *passfile = NULL;
# Line 336  int main(int argc, char **argv) Line 357  int main(int argc, char **argv)
357          int edit_user = FALSE;          int edit_user = FALSE;
358          int edit_mask = FALSE;          int edit_mask = FALSE;
359          extern char *optarg;          extern char *optarg;
360            
361          /* stolen from smbpasswd - checking for setuid root! */          /*
362                     * stolen from smbpasswd - checking for setuid root!
363          /* Check the effective uid - make sure we are not setuid */           */
364          if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0))  
365            /*
366             * Check the effective uid - make sure we are not setuid
367             */
368            if ((geteuid() == (uid_t) 0) && (getuid() != (uid_t) 0))
369                  ERRORMSGFATAL("mudpasswd must *NOT* be setuid root.\n");                  ERRORMSGFATAL("mudpasswd must *NOT* be setuid root.\n");
370                                                      
371          while ((ch = getopt(argc, argv, "p:a:d:e:P:R:H:WhV")) != EOF)          while ((ch = getopt(argc, argv, "p:a:d:e:P:R:H:WhV")) != EOF)
372          {          {
373                  switch(ch)                  switch (ch)
374                  {                  {
375                  case 'V':                  case 'V':
376                          showversion("mudpasswd");                          showversion("mudpasswd");
# Line 389  int main(int argc, char **argv) Line 414  int main(int argc, char **argv)
414    
415          if (!passfile)          if (!passfile)
416                  usage();                  usage();
417            
418          if (add_user)          if (add_user)
419          {          {
420                  if (edit_user || delete_user)                  if (edit_user || delete_user)
421                          usage();                          usage();
422                  if (!homedir)                  if (!homedir)
423                  {                  {
424                          homedir = (char *)malloc(MAXSTRLEN);                          homedir = (char *) malloc(MAXSTRLEN);
425                          printf("Enter Home Directory For New User: ");                          printf("Enter Home Directory For New User: ");
426                          fgets(homedir, MAXSTRLEN, stdin);                          fgets(homedir, MAXSTRLEN, stdin);
427                          homedir[strlen(homedir) - 1] = 0;                          homedir[strlen(homedir) - 1] = 0;
428                  }                  }
429                  if (!rootdir)                  if (!rootdir)
430                  {                  {
431                          rootdir = (char *)malloc(MAXSTRLEN);                          rootdir = (char *) malloc(MAXSTRLEN);
432                          printf("Enter Root Directory For New User: ");                          printf("Enter Root Directory For New User: ");
433                          fgets(rootdir, MAXSTRLEN, stdin);                          fgets(rootdir, MAXSTRLEN, stdin);
434                          rootdir[strlen(rootdir) - 1] = 0;                          rootdir[strlen(rootdir) - 1] = 0;
# Line 415  int main(int argc, char **argv) Line 440  int main(int argc, char **argv)
440    
441                  adduser(passfile, username, rootdir, homedir, password);                  adduser(passfile, username, rootdir, homedir, password);
442                  exit(0);                  exit(0);
443          }          }
444            
445          if (delete_user)          if (delete_user)
446          {          {
447                  if (edit_user)                  if (edit_user)
448                          usage();                          usage();
449                    
450                  deleteuser(passfile, username);                  deleteuser(passfile, username);
451                  exit(0);                  exit(0);
452          }          }
453    
454          if (edit_user)          if (edit_user)
455          {          {
456                  if (!edit_mask)                  if (!edit_mask)
457                          usage();                          usage();
458                    
459                  if (!password && (edit_mask & PASSMSK))                  if (!password && (edit_mask & PASSMSK))
460                  {                  {
461                          password = getpass("Enter Password For New User: ");                          password = getpass("Enter Password For New User: ");
462                  }                  }
463                    
464                  edituser(passfile, username, rootdir, homedir, password, edit_mask);                  edituser(passfile, username, rootdir, homedir, password, edit_mask);
465                  exit(0);                  exit(0);
466          }          }
467                            
468          usage();                  usage();
469          return(0);          return (0);
470  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.6.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26