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

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