home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tnlogin.zip / passwd.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  3KB  |  172 lines

  1. /* passwd.c - "passwd" program for /etc/passwd
  2.  *
  3.  * Author:  Kai Uwe Rommel <rommel@ars.muc.de>
  4.  * Created: Wed Mar 16 1994
  5.  */
  6.  
  7. static char *rcsid =
  8. "$Id: passwd.c,v 1.1 1994/09/16 08:32:48 rommel Exp $";
  9. static char *rcsrev = "$Revision: 1.1 $";
  10.  
  11. /*
  12.  * $Log: passwd.c,v $
  13.  * Revision 1.1  1994/09/16 08:32:48  rommel
  14.  * Initial revision
  15.  * 
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <pwd.h>
  22.  
  23. #define SALT "pc"
  24.  
  25. extern char *crypt(char *, char*);
  26.  
  27. extern int getopt(int, char **, char*);
  28. extern int optind;
  29. extern char *optarg;
  30.  
  31. extern char *getpass(char *);
  32. char *getline(const char *prompt);
  33.  
  34. int usage(void)
  35. {
  36.   printf("\nUsage: passwd [-ad] <user>\n"
  37.      "\n       -a  add new user"
  38.      "\n       -d  delete user\n");
  39.   exit(1);
  40. }
  41.  
  42. int main(int argc, char **argv)
  43. {
  44.   struct passwd *pw, new;
  45.   int opt, add = 0, delete = 0;
  46.   char user[64], password[64], crypted[64];
  47.   char *cp;
  48.  
  49.   while ( (opt = getopt(argc, argv, "ad?")) != EOF )
  50.     switch ( opt )
  51.     {
  52.     case 'a':
  53.       add = 1;
  54.       break;
  55.     case 'd':
  56.       delete = 1;
  57.       break;
  58.     default:
  59.       usage();
  60.     }
  61.  
  62.   if (add && delete)
  63.     usage();
  64.  
  65.   if (optind == argc)
  66.   {
  67.     if ((cp = getenv("USER")) == NULL)
  68.       if ((cp = getenv("LOGNAME")) == NULL)
  69.     usage();
  70.  
  71.     strcpy(user, cp);
  72.   }
  73.   else
  74.     strcpy(user, argv[optind]);
  75.  
  76.   printf("User: %s\n", user);
  77.  
  78.   if ((pw = getpwnam(user)) != NULL)
  79.   {
  80.     if (add)
  81.     {
  82.       printf("User already exists.\n");
  83.       return 1;
  84.     }
  85.   }
  86.   else
  87.   {
  88.     if (!add)
  89.     {
  90.       printf("No such user.\n");
  91.       return 1;
  92.     }
  93.     else
  94.     {
  95.       new.pw_name = user;
  96.       new.pw_passwd = "*";
  97.  
  98.       do
  99.       {
  100.     cp = getline("User id: ");
  101.     new.pw_uid = atoi(cp);
  102.     if ((pw = getpwuid(new.pw_uid)) != NULL)
  103.       puts("User id already in use, please choose a different one.");
  104.       }
  105.       while (pw != NULL);
  106.  
  107.       cp = getline("Group id: ");
  108.       new.pw_gid = atoi(cp);
  109.  
  110.       new.pw_gecos = strdup(getline("Full name: "));
  111.       new.pw_dir   = strdup(getline("Home directory: "));
  112.       new.pw_shell = strdup(getline("Command line shell: "));
  113.  
  114.       pw = &new;
  115.     }
  116.   }
  117.  
  118.   if (strcmp(pw->pw_passwd, "*"))
  119.   {
  120.     strcpy(password, getpass("Current password: "));
  121.     if (strcmp(pw->pw_passwd, crypt(password, pw->pw_passwd)))
  122.     {
  123.       printf("Incorrect password.\n");
  124.       return 1;
  125.     }
  126.   }
  127.   
  128.   if (delete)
  129.   {
  130.     pw->pw_uid = -1;
  131.  
  132.     if (setpwent(pw))
  133.     {
  134.       printf("Update of passwd file failed.\n");
  135.       return 1;
  136.     }
  137.  
  138.     return 0;
  139.   }
  140.  
  141.   strcpy(password, getpass("New password: "));
  142.   strcpy(crypted, getpass("Re-type new password: "));
  143.  
  144.   if (strcmp(password, crypted))
  145.   {
  146.     printf("New passwords don't match.\n");
  147.     return 1;
  148.   }
  149.   
  150.   strcpy(crypted, crypt(password, SALT));
  151.  
  152.   if (strcmp(pw->pw_passwd, crypted) == 0)
  153.   {
  154.     printf("Password unchanged.\n");
  155.     return 1;
  156.   }
  157.  
  158.   pw->pw_passwd = crypted;
  159.   
  160.   if (setpwent(pw))
  161.   {
  162.     printf("Update of passwd file failed.\n");
  163.     return 1;
  164.   }
  165.  
  166.   printf("Done.\n");
  167.  
  168.   return 0;
  169. }
  170.  
  171. /* end of passwd.c */
  172.