home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3349 / dpmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  2.8 KB  |  161 lines

  1. /*
  2.  * Copyright 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #ifdef    BSD
  17. #include <strings.h>
  18. #else
  19. #include <string.h>
  20. #endif
  21. #include "dialup.h"
  22.  
  23. #ifndef    lint
  24. static    char    sccsid[] = "@(#)dpmain.c    3.2    12:30:37    12/12/90";
  25. #endif
  26.  
  27. #define    DTMP    "/etc/d_passwd.tmp"
  28.  
  29. /*
  30.  * Prompts and messages go here.
  31.  */
  32.  
  33. #define    PASS1    "Shell password:"
  34. #define    PASS2    "re-enter Shell password:"
  35. #define    NOMATCH    "%s: Passwords do not match, try again.\n"
  36. #define    NOFOUND    "%s: Shell %s not found.\n"
  37.  
  38. int    aflg;
  39. int    dflg;
  40. char    *Prog;
  41.  
  42. extern    char    *pw_encrypt();
  43. extern    char    *getpass();
  44.  
  45. usage ()
  46. {
  47.     fprintf (stderr, "Usage: %s -a|c|d shell\n", Prog);
  48.     exit (1);
  49. }
  50.  
  51. main (argc, argv)
  52. int    argc;
  53. char    **argv;
  54. {
  55.     struct    dialup    *dial;
  56.     struct    dialup    dent;
  57.     struct    stat    sb;
  58.     FILE    *fp;
  59.     char    *shell;
  60.     char    *cp;
  61.     char    pass[BUFSIZ];
  62.     int    fd;
  63.     int    found = 0;
  64.     int    opt;
  65.     extern    int    optind;
  66.     extern    char    *optarg;
  67.  
  68.     if (Prog = strrchr (argv[0], '/'))
  69.         Prog++;
  70.     else
  71.         Prog = argv[0];
  72.  
  73.     while ((opt = getopt (argc, argv, "a:d:")) != EOF) {
  74.         switch (opt) {
  75.             case 'a':
  76.                 aflg++;
  77.                 shell = optarg;
  78.                 break;
  79.             case 'd':
  80.                 dflg++;
  81.                 shell = optarg;
  82.                 break;
  83.             default:
  84.                 usage ();
  85.         }
  86.     }
  87.     if (aflg + dflg != 1)
  88.         usage ();
  89.  
  90.     if (aflg) {
  91.         dent.du_shell = shell;
  92.         dent.du_passwd = "";
  93.  
  94.         if (! (cp = getpass (PASS1))) {
  95.             unlink (DTMP);
  96.             exit (1);
  97.         }
  98.         strcpy (pass, cp);
  99.         if (! (cp = getpass (PASS2))) {
  100.             unlink (DTMP);
  101.             exit (1);
  102.         }
  103.         if (strcmp (pass, cp)) {
  104.             fprintf (stderr, NOMATCH, Prog);
  105.             unlink (DTMP);
  106.             exit (1);
  107.         }
  108.         dent.du_passwd = pw_encrypt (pass, (char *) 0);
  109.     }
  110.     if ((fd = open (DTMP, O_CREAT|O_EXCL|O_RDWR, 0600)) < 0) {
  111.         sprintf (pass, "%s: can't create %s", Prog, DTMP);
  112.         perror (pass);
  113.         exit (1);
  114.     }
  115.     if (! (fp = fdopen (fd, "r+"))) {
  116.         sprintf (pass, "%s: can't open %s", Prog, DTMP);
  117.         perror (pass);
  118.         exit (1);
  119.     }
  120.     while (dial = getduent ()) {
  121.         if (strcmp (dial->du_shell, shell) == 0) {
  122.             found = 1;
  123.             break;
  124.         }
  125.         if (putduent (dial, fp))
  126.             goto failure;
  127.     }
  128.     if (dflg && ! found) {
  129.         fprintf (stderr, NOMATCH, Prog, shell);
  130.         exit (1);
  131.     }
  132.     if (aflg)
  133.         if (putduent (&dent, fp))
  134.             goto failure;
  135.  
  136.     while (dial = getduent ())
  137.         if (putduent (dial, fp))
  138.             goto failure;
  139.  
  140.     if (fflush (fp))
  141.         goto failure;
  142.  
  143.     if (! stat (DIALPWD, &sb)) {
  144.         chown (DTMP, sb.st_uid, sb.st_gid);
  145.         chmod (DTMP, sb.st_mode);
  146.         unlink (DIALPWD);
  147.     } else {
  148.         chown (DTMP, 0, 0);
  149.         chmod (DTMP, 0400);
  150.     }
  151.     link (DTMP, DIALPWD);
  152.     unlink (DTMP);
  153.  
  154.     sync ();
  155.     exit (0);
  156.  
  157. failure:
  158.     unlink (DTMP);
  159.     exit (1);
  160. }
  161.