home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2290 / pwunconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  3.0 KB  |  120 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.  * pwunconv - restore old password file from shadow password file.
  11.  *
  12.  *    Pwunconv copies the password file information from the shadow
  13.  *    password file, merging entries from an optional existing shadow
  14.  *    file.
  15.  *
  16.  *    The new password file is left in npasswd.  There is no new
  17.  *    shadow file.  Password aging information is translated where
  18.  *    possible.
  19.  */
  20.  
  21. #include <sys/types.h>
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include <pwd.h>
  25. #include "config.h"
  26. #include "shadow.h"
  27.  
  28. #ifndef    lint
  29. static    char    _sccsid[] = "@(#)pwunconv.c    2.2    19:24:08    7/29/90";
  30. #endif
  31.  
  32. char    buf[BUFSIZ];
  33. char    *l64a ();
  34.  
  35. int    main ()
  36. {
  37.     struct    passwd    *pw;
  38.     struct    passwd    *sgetpwent ();
  39.     FILE    *pwd;
  40.     FILE    *npwd;
  41.     struct    spwd    *spwd;
  42.     int    fd;
  43.  
  44.     if (! (pwd = fopen (PWDFILE, "r"))) {
  45.         perror (PWDFILE);
  46.         return (1);
  47.     }
  48.     unlink ("npasswd");
  49.     if ((fd = open ("npasswd", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
  50.             ! (npwd = fdopen (fd, "w"))) {
  51.         perror ("npasswd");
  52.         return (1);
  53.     }
  54.     while (fgets (buf, BUFSIZ, pwd) == buf) {
  55.         buf[strlen (buf) - 1] = '\0'; /* remove '\n' character */
  56.  
  57.         if (buf[0] == '#') {    /* comment line */
  58.             (void) fprintf (npwd, "%s\n", buf);
  59.             continue;
  60.         }
  61.         if (! (pw = sgetpwent (buf))) { /* copy bad lines verbatim */
  62.             (void) fprintf (npwd, "%s\n", buf);
  63.             continue;
  64.         }
  65.         setspent ();        /* rewind shadow file */
  66.  
  67.         if (! (spwd = getspnam (pw->pw_name))) {
  68.             (void) fprintf (npwd, "%s\n", buf);
  69.             continue;
  70.         }
  71.         pw->pw_passwd = spwd->sp_pwdp;
  72.  
  73.     /*
  74.      * Password aging works differently in the two different systems.
  75.      * With shadow password files you apparently must have some aging
  76.      * information.  The maxweeks or minweeks may not map exactly.
  77.      * In pwconv we set max == 10000, which is about 30 years.  Here
  78.      * we have to undo that kludge.  So, if maxdays == 10000, no aging
  79.      * information is put into the new file.  Otherwise, the days are
  80.      * converted to weeks and so on.
  81.      */
  82.  
  83.         if (spwd->sp_max > (63*7) && spwd->sp_max < 10000)
  84.             spwd->sp_max = (63*7); /* 10000 is infinity this week */
  85.  
  86.         if (spwd->sp_min >= 0 && spwd->sp_min <= 63*7 &&
  87.                 spwd->sp_max >= 0 && spwd->sp_max <= 63*7) {
  88.             spwd->sp_max /= 7;    /* turn it into weeks */
  89.             spwd->sp_min /= 7;
  90.             spwd->sp_lstchg /= 7;
  91.             pw->pw_age = l64a ((long) spwd->sp_lstchg * (64L*64L) +
  92.                           spwd->sp_min * (64L) +
  93.                           spwd->sp_max);
  94.         } else
  95.             pw->pw_age = (char *) 0;
  96.  
  97.         if (pw->pw_age)
  98.             (void) fprintf (npwd, "%s:%s,%s:%d:%d:%s:%s:%s\n",
  99.                 pw->pw_name, pw->pw_passwd ? pw->pw_passwd:"",
  100.                 pw->pw_age, pw->pw_uid, pw->pw_gid,
  101.                 pw->pw_gecos, pw->pw_dir,
  102.                 pw->pw_shell ? pw->pw_shell:"");
  103.         else
  104.             (void) fprintf (npwd, "%s:%s:%d:%d:%s:%s:%s\n",
  105.                 pw->pw_name, pw->pw_passwd ? pw->pw_passwd:"",
  106.                 pw->pw_uid, pw->pw_gid,
  107.                 pw->pw_gecos, pw->pw_dir,
  108.                 pw->pw_shell ? pw->pw_shell:"");
  109.     }
  110.     endspent ();
  111.  
  112.     if (ferror (npwd)) {
  113.         perror ("pwunconv");
  114.         (void) unlink ("npasswd");
  115.     }
  116.     (void) fclose (npwd);
  117.     (void) fclose (pwd);
  118.     return (0);
  119. }
  120.