home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / pwd / mcpasswd.c < prev    next >
C/C++ Source or Header  |  1999-01-04  |  3KB  |  177 lines

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