home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / LOGIN.ZIP / USER.C < prev    next >
C/C++ Source or Header  |  1991-02-03  |  8KB  |  322 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <direct.h>
  4. #include <dos.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8.  
  9. #include "login.h"
  10.  
  11. struct usr    *getusr();
  12. char    *getfld();
  13. char        *motd, *passwd, *logfile;       /* for default overrides */
  14. int         root = ROOT;
  15.  
  16. #define ADD     'a'
  17. #define CHANGE    'c'
  18. #define DELETE    'd'
  19. #define LIST    'l'
  20.  
  21. void
  22. main(argc, argv)
  23. int     argc;
  24. char    **argv;
  25. {
  26.     struct    usr     *usr;
  27.     extern    char *optarg;
  28.     extern    int optind;
  29.     int     type = '0', errflg = 0, c;
  30.  
  31.     /* l=list a=add d=delete c=change */
  32.     while ((c = getopt(argc, argv, "acdl")) != EOF) {
  33.         switch (tolower(c)) {
  34.         case ADD:
  35.         case CHANGE:
  36.         case DELETE:
  37.         case LIST:
  38.             type = c;
  39.             break;
  40.         default:                /* error */
  41.             errflg++;
  42.             break;
  43.         }
  44.     }
  45.  
  46.     if (errflg) {
  47.         fprintf(stderr, USAGE);
  48.         exit(9);
  49.     }
  50.  
  51.     /* override the PASSWD file path */
  52.     if ((passwd = getenv("PASSWD")) == NULL)
  53.         passwd = PASSWD;
  54.  
  55.     /* override the LOGFILE file path */
  56.     if ((logfile = getenv("LOGFILE")) == NULL)
  57.         logfile = LOGFILE;
  58.  
  59.     /* override the MOTD file path */
  60.     if ((motd = getenv("MOTD")) == NULL)
  61.         motd = MOTD;
  62.  
  63.     /* override the ROOT user id */
  64.     if (getenv("ROOT"))
  65.         root = atoi(getenv("ROOT"));
  66.  
  67.     switch (type) {
  68.     case ADD:
  69.         adduser();
  70.         break;
  71. #ifdef FINISHED
  72.     case CHANGE:
  73.         chguser();
  74.         break;
  75.     case DELETE:
  76.         deluser();
  77.         break;
  78.     case LIST:
  79.         lstuser();
  80.         break;
  81. #endif
  82.     }
  83. }
  84.  
  85. /*#  name:id number:drive:home directory:command or "shell"  #*/
  86. adduser()
  87. {
  88.     struct    usr u;
  89.     char    linbuf[256];
  90.     int     run = 1;
  91.     unsigned drive;
  92.  
  93.     while (run) {
  94.         while (1) {
  95.             fprintf(stderr, "User Name or (q)uit: ");
  96.             memset(linbuf, '\0', sizeof (linbuf));
  97.             u.name[0] = '\0';
  98.             gets(linbuf);
  99.             if (toupper(linbuf[0]) == 'Q' && strlen(linbuf) == 1) {
  100.                 run = 0;
  101.                 break;
  102.             }
  103.             if (strlen(linbuf) > 15) {
  104.                 fprintf(stderr, "user name must be less than 15 characters\n");
  105.                 continue;
  106.             } else {
  107.                 strcpy(u.name, linbuf);
  108.                 break;
  109.             }
  110.             if (*u.name && getusr(passwd, u.name))
  111.                 fprintf(stderr, "user %s already assigned\n", u.name);
  112.             else
  113.                 break;
  114.         }
  115.         if (!run)
  116.             break;
  117.  
  118.         while (1) {
  119.             fprintf(stderr, "User ID Number (0-999) or (q)uit: ");
  120.             memset(linbuf, '\0', sizeof (linbuf));
  121.             gets(linbuf);
  122.             if (toupper(linbuf[0]) == 'Q') {
  123.                 run = 0;
  124.                 break;
  125.             }
  126.             u.uid = atoi(linbuf);
  127.             if (u.uid > 999 || u.uid < 0) {
  128.                 fprintf(stderr, "user ID must be 0-999\n");
  129.                 continue;
  130.             }
  131.  
  132.             if (chkuid(passwd, u.uid))
  133.                 fprintf(stderr, "user ID %d already assigned\n", u.uid);
  134.             else
  135.                 break;
  136.         }
  137.         if (!run)
  138.             break;
  139.  
  140.         _dos_getdrive(&drive);                  /* use current drive as default */
  141.  
  142.         do {
  143.             fprintf(stderr, "Enter Drive (A-Z) or (0) to quit (default is \'%c\'): ", drive+'@');
  144.             gets(linbuf);
  145.             if (linbuf[0] == '0') {
  146.                 run = 0;
  147.                 break;
  148.             } else if (!strlen(linbuf))
  149.                 linbuf[0] = drive + '@';
  150.         } while ((linbuf[0] = toupper(linbuf[0])) < 'A' || linbuf[0] > 'Z');
  151.  
  152.         if (!run)
  153.             break;
  154.  
  155.         u.drive = linbuf[0] - '@';
  156.  
  157.  
  158.         fprintf(stderr, "Enter User password: ");
  159.         gets(linbuf);
  160.         if (strlen(linbuf))
  161.             strcpy(u.passwd, linbuf);
  162.         else
  163.             memset(u.passwd, '\0', sizeof (u.passwd));
  164.  
  165.         fprintf(stderr, "Enter HOME directory: ");
  166.         gets(linbuf);
  167.         if (!strlen(linbuf)) {                        /* no HOME specified */
  168.             if (getcwd(u.home, 80) == NULL)         /* current directory */
  169.                 error(4);
  170.         } else
  171.             strcpy(u.home, linbuf);
  172.  
  173.         fprintf(stderr, "Enter COMMAND or \"shell\" (shell is default): ");
  174.         gets(linbuf);
  175.         strcpy(u.shell, *linbuf ? linbuf : "shell");
  176.         if (ask(&u))
  177.             if (install(&u))
  178.                 fprintf(stderr, "install failed\n");
  179.     }
  180. }
  181.  
  182. /* write entry to password file */
  183. int
  184. install(struct usr *u)
  185. {
  186.     FILE    *fp;
  187.     char    *p;
  188.  
  189.     /* open password file */
  190.     if ((fp = fopen(passwd, "a+")) == NULL) {
  191.         error(3);
  192.         return (1);
  193.     }
  194.     p = encrypt(u->passwd);
  195.     fprintf(fp, "%s:%d:%s:%c:%s:%s\n", u->name, u->uid,
  196.         *u->passwd ? p : "", u->drive+'@',
  197.         u->home, u->shell);
  198.  
  199.     if (p)
  200.         free(p);
  201.  
  202.     fclose(fp);
  203.     return (0);
  204. }
  205.  
  206.  
  207. /* display user info & ask for verification */
  208. ask(struct usr * u)
  209. {
  210.     char    linbuf[256];
  211.     fprintf(stderr, "\n");
  212.     fprintf(stderr, "name.......%s\n", u->name);        /* 0 */
  213.     fprintf(stderr, "user id....%d\n", u->uid);         /* 1 */
  214.     fprintf(stderr, "password...%s\n", u->passwd);      /* 2 */
  215.     fprintf(stderr, "drive......%c\n", u->drive + '@'); /* 3 */
  216.     fprintf(stderr, "home.......%s\n", u->home);        /* 4 */
  217.     fprintf(stderr, "shell......%s\n", u->shell);       /* 5 */
  218.     while (1) {
  219.         fprintf(stderr, "Is This OK (Y/N)? ");
  220.         gets(linbuf);
  221.         switch (linbuf[0]) {
  222.         case 'Y':
  223.         case 'y':
  224.             return(1);
  225.         case 'N':
  226.         case 'n':
  227.             return(0);
  228.         }
  229.     }
  230. }
  231.  
  232. int
  233. chkuid(char *passwd, int u)
  234. {
  235.     FILE    *fp;
  236.     int     found = 0, uuid;
  237.     char    linbuf[256];
  238.  
  239.     /* open password file */
  240.     if ((fp = fopen(passwd, "r")) == NULL)
  241.         return (0);
  242.  
  243.     /* read lines from the PASSWD file */
  244.     while (!found && fgets(linbuf, 256, fp)) {
  245.         if (*linbuf == '#')                         /* '#' denotes comment */
  246.             continue;
  247.  
  248.         uuid = atoi(getfld(linbuf, 1, ':'));       /* user ID */
  249.         found = (uuid == u);
  250.     }
  251.     return (found);
  252. }
  253.  
  254. struct usr *
  255. getusr(char *l, char *name)
  256. {
  257.     FILE    *fp;
  258.     static    struct usr u;
  259.     char    linbuf[256];
  260.     char    *comspec, *p, shell[80];
  261.     int     found = 0;
  262.  
  263.     /* open password file */
  264.     if ((fp = fopen(passwd, "r")) == NULL) {
  265.         error(3);
  266.         return ((struct usr *)NULL);
  267.     }
  268.  
  269.     /* read lines from the PASSWD file */
  270.     while (!found && fgets(linbuf, 256, fp)) {
  271.         memset(&u, '\0', sizeof (struct usr));      /* clean up structure */
  272.  
  273.         if (*linbuf == '#')                         /* '#' denotes comment */
  274.             continue;
  275.  
  276.         if ((p = strchr(linbuf, '\n')) != NULL)     /* zap new-lines */
  277.             *p = '\0';
  278.  
  279.         strcpy(u.name,   getfld(linbuf, 0, ':'));   /* user name */
  280.         if (!strcmp(u.name, name))                       /* name matches */
  281.             found = 1;
  282.         else
  283.             continue;
  284.  
  285.         u.uid =     atoi(getfld(linbuf, 1, ':'));   /* user ID */
  286.         strcpy(u.passwd, getfld(linbuf, 2, ':'));   /* user password */
  287.  
  288.         p = getfld(linbuf, 3, ':');                 /* disk drive */
  289.         u.drive = *p == '\0' ? 0 : *p - '@';        /* set to 0 if none */
  290.         strcpy(u.home,   getfld(linbuf, 4, ':'));   /* home directory */
  291.         strcpy(shell,     getfld(linbuf, 5, ':'));   /* command/shell */
  292.  
  293.         if (*u.home == '\0')                        /* no HOME specified */
  294.             if (getcwd(u.home, 80) == NULL)         /* current directory */
  295.                 error(4);
  296.  
  297.     }
  298.  
  299.     /* if name was found */
  300.     if (found) {
  301.         if (u.drive == 0)                            /* no drive specified */
  302.             _dos_getdrive(&u.drive);                /* use current drive */
  303.  
  304.         if ((comspec = (char *) getenv("COMSPEC")) == NULL) {
  305.             comspec = "X:\command.com";
  306.             *comspec = (char )u.drive + '@';    /* use current drive */
  307.         }
  308.         /* if "shell" specified, use COMSPEC to find "command.com" */
  309.         if (!strncmp(strupr(shell), "SHELL", 5))
  310.             strcpy(u.shell, comspec);
  311.         else if (p = strchr(shell, '.'))
  312.             if (!strcmp(strupr(p), ".BAT"))     /* execute batch file */
  313.                 sprintf(u.shell, "%s /C %s", comspec, shell);
  314.             else
  315.                 strcpy(u.shell, shell);         /* execute whatever */
  316.     } else
  317.         found = 0;
  318.     fclose(fp);
  319.     return (found ? &u : (struct usr *) NULL);
  320. }
  321.  
  322.