home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / pwconv.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  3KB  |  137 lines

  1. /*
  2.  * pwconv - convert and update shadow password files
  3.  *
  4.  *    Pwconv copies the old password file information to a new shadow
  5.  *    password file, merging entries from an optional existing shadow
  6.  *    file.
  7.  *
  8.  *    The new password file is left in npasswd, the new shadow file is
  9.  *    left in nshadow.  Existing shadow entries are copied as is.
  10.  *    New entries are created with passwords which expire in 10000 days,
  11.  *    with a last changed date of today, unless password aging
  12.  *    information was already present.  Entries with blank passwords
  13.  *    are not copied to the shadow file at all.
  14.  */
  15.  
  16. #include <sys/types.h>
  17. #include <stdio.h>
  18. #include <fcntl.h>
  19. #include <pwd.h>
  20. #include "config.h"
  21. #include "lastlog.h"
  22. #include "shadow.h"
  23.  
  24. char    buf[BUFSIZ];
  25.  
  26. long    time ();
  27. long    a64l ();
  28.  
  29. int    main ()
  30. {
  31.     long    today;
  32.     struct    passwd    *pw;
  33.     struct    passwd    *sgetpwent ();
  34.     FILE    *pwd;
  35.     FILE    *npwd;
  36.     FILE    *shadow;
  37.     struct    spwd    *spwd;
  38.     struct    spwd    tspwd;
  39.     int    fd;
  40.  
  41.     if (! (pwd = fopen (PWDFILE, "r"))) {
  42.         perror (PWDFILE);
  43.         return (1);
  44.     }
  45.     unlink ("npasswd");
  46.     if ((fd = open ("npasswd", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
  47.             ! (npwd = fdopen (fd, "w"))) {
  48.         perror ("npasswd");
  49.         return (1);
  50.     }
  51.     unlink  ("nshadow");
  52.     if ((fd = open ("nshadow", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
  53.             ! (shadow = fdopen (fd, "w"))) {
  54.         perror ("nshadow");
  55.         return (1);
  56.     }
  57.  
  58.     (void) time (&today);
  59.     today /= (24L * 60L * 60L);
  60.  
  61.     while (fgets (buf, BUFSIZ, pwd) == buf) {
  62.         buf[strlen (buf) - 1] = '\0'; /* remove '\n' character */
  63.  
  64.         if (buf[0] == '#') {    /* comment line */
  65.             (void) fprintf (npwd, "%s\n", buf);
  66.             continue;
  67.         }
  68.         if (! (pw = sgetpwent (buf))) { /* copy bad lines verbatim */
  69.             (void) fprintf (npwd, "%s\n", buf);
  70.             continue;
  71.         }
  72.         if (pw->pw_passwd == (char *) 0) { /* no password, skip */
  73.             (void) fprintf (npwd, "%s\n", buf);
  74.             continue;
  75.         }
  76.         setspent ();        /* rewind old shadow file */
  77.  
  78.         if (spwd = getspnam (pw->pw_name)) {
  79.             if (! putspent (spwd, shadow)) { /* copy old entry */
  80.                 perror ("nshadow");
  81.                 return (1);
  82.             }
  83.         } else {        /* need a new entry. */
  84.             tspwd.sp_namp = pw->pw_name;
  85.             tspwd.sp_pwdp = pw->pw_passwd;
  86.             pw->pw_passwd = "x";
  87.  
  88.             if (pw->pw_age) { /* copy old password age stuff */
  89.                 tspwd.sp_min = c64i (pw->pw_age[1]);
  90.                 tspwd.sp_max = c64i (pw->pw_age[0]);
  91.                 if (strlen (pw->pw_age) == 4)
  92.                     tspwd.sp_lstchg = a64l (&pw->pw_age[2]);
  93.                 else
  94.                     tspwd.sp_lstchg = 0L;
  95.  
  96.                 /*
  97.                  * Convert weeks to days
  98.                  */
  99.  
  100.                 tspwd.sp_min *= 7;
  101.                 tspwd.sp_max *= 7;
  102.                 tspwd.sp_lstchg *= 7;
  103.             } else {    /* fake up new password age stuff */
  104.                 tspwd.sp_max = 10000;
  105.                 tspwd.sp_min = 0;
  106.                 tspwd.sp_lstchg = today;
  107.             }
  108.             if (! putspent (&tspwd, shadow)) { /* output entry */
  109.                 perror ("nshadow");
  110.                 return (1);
  111.             }
  112.         }
  113.         (void) fprintf (npwd, "%s:%s:%d:%d:%s:%s:",
  114.                 pw->pw_name, pw->pw_passwd ? pw->pw_passwd:"",
  115.                 pw->pw_uid, pw->pw_gid,
  116.                 pw->pw_gecos, pw->pw_dir);
  117.  
  118.         if (fprintf (npwd, "%s\n",
  119.                 pw->pw_shell ? pw->pw_shell:"") == EOF) {
  120.             perror ("npasswd");
  121.             return (1);
  122.         }
  123.     }
  124.     endspent ();
  125.  
  126.     if (ferror (npwd) || ferror (shadow)) {
  127.         perror ("pwconv");
  128.         (void) unlink ("npasswd");
  129.         (void) unlink ("nshadow");
  130.     }
  131.     (void) fclose (pwd);
  132.     (void) fclose (npwd);
  133.     (void) fclose (shadow);
  134.  
  135.     return (0);
  136. }
  137.