home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / pwunconv.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  3KB  |  125 lines

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