home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1989, 1990, John F. Haugh II
- * All rights reserved.
- *
- * Use, duplication, and disclosure prohibited without
- * the express written permission of the author.
- */
-
- /*
- * pwconv - convert and update shadow password files
- *
- * Pwconv copies the old password file information to a new shadow
- * password file, merging entries from an optional existing shadow
- * file.
- *
- * The new password file is left in npasswd, the new shadow file is
- * left in nshadow. Existing shadow entries are copied as is.
- * New entries are created with passwords which expire in MAXDAYS days,
- * with a last changed date of today, unless password aging
- * information was already present. Likewise, the minimum number of
- * days before which the password may be changed is controlled by
- * MINDAYS. Entries with blank passwordsare not copied to the shadow
- * file at all.
- */
-
- #include <sys/types.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <pwd.h>
- #ifndef BSD
- #include <string.h>
- #else
- #define strchr index
- #define strrchr rindex
- #include <strings.h>
- #endif
- #include "config.h"
- #include "shadow.h"
-
- #ifndef lint
- static char _sccsid[] = "@(#)pwconv.c 2.6 08:01:45 11/9/90";
- #endif
-
- char buf[BUFSIZ];
-
- long time ();
- long a64l ();
-
- int main ()
- {
- long today;
- struct passwd *pw;
- struct passwd *sgetpwent ();
- FILE *pwd;
- FILE *npwd;
- FILE *shadow;
- struct spwd *spwd;
- struct spwd tspwd;
- int fd;
- char *cp;
-
- if (! (pwd = fopen (PWDFILE, "r"))) {
- perror (PWDFILE);
- exit (1);
- }
- unlink ("npasswd");
- if ((fd = open ("npasswd", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
- ! (npwd = fdopen (fd, "w"))) {
- perror ("npasswd");
- exit (1);
- }
- unlink ("nshadow");
- if ((fd = open ("nshadow", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 ||
- ! (shadow = fdopen (fd, "w"))) {
- perror ("nshadow");
- (void) unlink ("npasswd");
- (void) unlink ("nshadow");
- exit (1);
- }
-
- (void) time (&today);
- today /= (24L * 60L * 60L);
-
- while (fgets (buf, BUFSIZ, pwd) == buf) {
- if (cp = strrchr (buf, '\n'))
- *cp = '\0';
-
- if (buf[0] == '#') { /* comment line */
- (void) fprintf (npwd, "%s\n", buf);
- continue;
- }
- if (! (pw = sgetpwent (buf))) { /* copy bad lines verbatim */
- (void) fprintf (npwd, "%s\n", buf);
- continue;
- }
- if (pw->pw_passwd[0] == '\0') { /* no password, skip */
- (void) fprintf (npwd, "%s\n", buf);
- continue;
- }
- setspent (); /* rewind old shadow file */
-
- if (spwd = getspnam (pw->pw_name)) {
- if (putspent (spwd, shadow)) { /* copy old entry */
- perror ("nshadow");
- goto error;
- }
- } else { /* need a new entry. */
- tspwd.sp_namp = pw->pw_name;
- tspwd.sp_pwdp = pw->pw_passwd;
- pw->pw_passwd = "x";
-
- if (pw->pw_age) { /* copy old password age stuff */
- tspwd.sp_min = c64i (pw->pw_age[1]);
- tspwd.sp_max = c64i (pw->pw_age[0]);
- if (strlen (pw->pw_age) == 4)
- tspwd.sp_lstchg = a64l (&pw->pw_age[2]);
- else
- tspwd.sp_lstchg = 0L;
-
- /*
- * Convert weeks to days
- */
-
- tspwd.sp_min *= 7;
- tspwd.sp_max *= 7;
- tspwd.sp_lstchg *= 7;
- } else { /* fake up new password age stuff */
- tspwd.sp_max = MAXDAYS;
- tspwd.sp_min = MINDAYS;
- tspwd.sp_lstchg = today;
- }
- if (putspent (&tspwd, shadow)) { /* output entry */
- perror ("nshadow");
- goto error;
- }
- }
- (void) fprintf (npwd, "%s:%s:%d:%d:%s:%s:",
- pw->pw_name, pw->pw_passwd,
- pw->pw_uid, pw->pw_gid,
- pw->pw_gecos, pw->pw_dir);
-
- if (fprintf (npwd, "%s\n",
- pw->pw_shell ? pw->pw_shell:"") == EOF) {
- perror ("npasswd");
- goto error;
- }
- }
- endspent ();
-
- if (ferror (npwd) || ferror (shadow)) {
- perror ("pwconv");
- error:
- (void) unlink ("npasswd");
- (void) unlink ("nshadow");
- exit (1);
- }
- (void) fclose (pwd);
- (void) fclose (npwd);
- (void) fclose (shadow);
-
- exit (0);
- }
-