home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2287 / pwconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.7 KB  |  163 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. /*
  10.  * pwconv - convert and update shadow password files
  11.  *
  12.  *    Pwconv copies the old password file information to a new shadow
  13.  *    password file, merging entries from an optional existing shadow
  14.  *    file.
  15.  *
  16.  *    The new password file is left in npasswd, the new shadow file is
  17.  *    left in nshadow.  Existing shadow entries are copied as is.
  18.  *    New entries are created with passwords which expire in MAXDAYS days,
  19.  *    with a last changed date of today, unless password aging
  20.  *    information was already present.  Likewise, the minimum number of
  21.  *    days before which the password may be changed is controlled by
  22.  *    MINDAYS.  Entries with blank passwordsare not copied to the shadow
  23.  *    file at all.
  24.  */
  25.  
  26. #include <sys/types.h>
  27. #include <stdio.h>
  28. #include <fcntl.h>
  29. #include <pwd.h>
  30. #ifndef    BSD
  31. #include <string.h>
  32. #else
  33. #define    strchr    index
  34. #define    strrchr    rindex
  35. #include <strings.h>
  36. #endif
  37. #include "config.h"
  38. #include "shadow.h"
  39.  
  40. #ifndef    lint
  41. static    char    _sccsid[] = "@(#)pwconv.c    2.6    08:01:45    11/9/90";
  42. #endif
  43.  
  44. char    buf[BUFSIZ];
  45.  
  46. long    time ();
  47. long    a64l ();
  48.  
  49. int    main ()
  50. {
  51.     long    today;
  52.     struct    passwd    *pw;
  53.     struct    passwd    *sgetpwent ();
  54.     FILE    *pwd;
  55.     FILE    *npwd;
  56.     FILE    *shadow;
  57.     struct    spwd    *spwd;
  58.     struct    spwd    tspwd;
  59.     int    fd;
  60.     char    *cp;
  61.  
  62.     if (! (pwd = fopen (PWDFILE, "r"))) {
  63.         perror (PWDFILE);
  64.         exit (1);
  65.     }
  66.     unlink ("npasswd");
  67.     if ((fd = open ("npasswd", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
  68.             ! (npwd = fdopen (fd, "w"))) {
  69.         perror ("npasswd");
  70.         exit (1);
  71.     }
  72.     unlink  ("nshadow");
  73.     if ((fd = open ("nshadow", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
  74.             ! (shadow = fdopen (fd, "w"))) {
  75.         perror ("nshadow");
  76.         (void) unlink ("npasswd");
  77.         (void) unlink ("nshadow");
  78.         exit (1);
  79.     }
  80.  
  81.     (void) time (&today);
  82.     today /= (24L * 60L * 60L);
  83.  
  84.     while (fgets (buf, BUFSIZ, pwd) == buf) {
  85.         if (cp = strrchr (buf, '\n'))
  86.             *cp = '\0';
  87.  
  88.         if (buf[0] == '#') {    /* comment line */
  89.             (void) fprintf (npwd, "%s\n", buf);
  90.             continue;
  91.         }
  92.         if (! (pw = sgetpwent (buf))) { /* copy bad lines verbatim */
  93.             (void) fprintf (npwd, "%s\n", buf);
  94.             continue;
  95.         }
  96.         if (pw->pw_passwd[0] == '\0') { /* no password, skip */
  97.             (void) fprintf (npwd, "%s\n", buf);
  98.             continue;
  99.         }
  100.         setspent ();        /* rewind old shadow file */
  101.  
  102.         if (spwd = getspnam (pw->pw_name)) {
  103.             if (putspent (spwd, shadow)) { /* copy old entry */
  104.                 perror ("nshadow");
  105.                 goto error;
  106.             }
  107.         } else {        /* need a new entry. */
  108.             tspwd.sp_namp = pw->pw_name;
  109.             tspwd.sp_pwdp = pw->pw_passwd;
  110.             pw->pw_passwd = "x";
  111.  
  112.             if (pw->pw_age) { /* copy old password age stuff */
  113.                 tspwd.sp_min = c64i (pw->pw_age[1]);
  114.                 tspwd.sp_max = c64i (pw->pw_age[0]);
  115.                 if (strlen (pw->pw_age) == 4)
  116.                     tspwd.sp_lstchg = a64l (&pw->pw_age[2]);
  117.                 else
  118.                     tspwd.sp_lstchg = 0L;
  119.  
  120.                 /*
  121.                  * Convert weeks to days
  122.                  */
  123.  
  124.                 tspwd.sp_min *= 7;
  125.                 tspwd.sp_max *= 7;
  126.                 tspwd.sp_lstchg *= 7;
  127.             } else {    /* fake up new password age stuff */
  128.                 tspwd.sp_max = MAXDAYS;
  129.                 tspwd.sp_min = MINDAYS;
  130.                 tspwd.sp_lstchg = today;
  131.             }
  132.             if (putspent (&tspwd, shadow)) { /* output entry */
  133.                 perror ("nshadow");
  134.                 goto error;
  135.             }
  136.         }
  137.         (void) fprintf (npwd, "%s:%s:%d:%d:%s:%s:",
  138.                 pw->pw_name, pw->pw_passwd,
  139.                 pw->pw_uid, pw->pw_gid,
  140.                 pw->pw_gecos, pw->pw_dir);
  141.  
  142.         if (fprintf (npwd, "%s\n",
  143.                 pw->pw_shell ? pw->pw_shell:"") == EOF) {
  144.             perror ("npasswd");
  145.             goto error;
  146.         }
  147.     }
  148.     endspent ();
  149.  
  150.     if (ferror (npwd) || ferror (shadow)) {
  151.         perror ("pwconv");
  152. error:
  153.         (void) unlink ("npasswd");
  154.         (void) unlink ("nshadow");
  155.         exit (1);
  156.     }
  157.     (void) fclose (pwd);
  158.     (void) fclose (npwd);
  159.     (void) fclose (shadow);
  160.  
  161.     exit (0);
  162. }
  163.