Annotation of src/usr.bin/chpass/pw_copy.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: pw_copy.c,v 1.3 1995/03/26 04:55:30 glass Exp $ */
! 2:
! 3: /*-
! 4: * Copyright (c) 1990, 1993, 1994
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions
! 9: * are met:
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. Redistributions in binary form must reproduce the above copyright
! 13: * notice, this list of conditions and the following disclaimer in the
! 14: * documentation and/or other materials provided with the distribution.
! 15: * 3. All advertising materials mentioning features or use of this software
! 16: * must display the following acknowledgement:
! 17: * This product includes software developed by the University of
! 18: * California, Berkeley and its contributors.
! 19: * 4. Neither the name of the University nor the names of its contributors
! 20: * may be used to endorse or promote products derived from this software
! 21: * without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
! 35:
! 36: #ifndef lint
! 37: #if 0
! 38: static char sccsid[] = "@(#)pw_copy.c 8.4 (Berkeley) 4/2/94";
! 39: #else
! 40: static char rcsid[] = "$NetBSD: pw_copy.c,v 1.3 1995/03/26 04:55:30 glass Exp $";
! 41: #endif
! 42: #endif /* not lint */
! 43:
! 44: /*
! 45: * This module is used to copy the master password file, replacing a single
! 46: * record, by chpass(1) and passwd(1).
! 47: */
! 48:
! 49: #include <err.h>
! 50: #include <pwd.h>
! 51: #include <stdio.h>
! 52: #include <string.h>
! 53:
! 54: #include <pw_util.h>
! 55: #include "pw_copy.h"
! 56:
! 57: extern char *tempname;
! 58:
! 59: void
! 60: pw_copy(ffd, tfd, pw)
! 61: int ffd, tfd;
! 62: struct passwd *pw;
! 63: {
! 64: FILE *from, *to;
! 65: int done;
! 66: char *p, buf[8192];
! 67:
! 68: if (!(from = fdopen(ffd, "r")))
! 69: pw_error(_PATH_MASTERPASSWD, 1, 1);
! 70: if (!(to = fdopen(tfd, "w")))
! 71: pw_error(tempname, 1, 1);
! 72:
! 73: for (done = 0; fgets(buf, sizeof(buf), from);) {
! 74: if (!strchr(buf, '\n')) {
! 75: warnx("%s: line too long", _PATH_MASTERPASSWD);
! 76: pw_error(NULL, 0, 1);
! 77: }
! 78: if (done) {
! 79: (void)fprintf(to, "%s", buf);
! 80: if (ferror(to))
! 81: goto err;
! 82: continue;
! 83: }
! 84: if (!(p = strchr(buf, ':'))) {
! 85: warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
! 86: pw_error(NULL, 0, 1);
! 87: }
! 88: *p = '\0';
! 89: if (strcmp(buf, pw->pw_name)) {
! 90: *p = ':';
! 91: (void)fprintf(to, "%s", buf);
! 92: if (ferror(to))
! 93: goto err;
! 94: continue;
! 95: }
! 96: (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
! 97: pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
! 98: pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
! 99: pw->pw_dir, pw->pw_shell);
! 100: done = 1;
! 101: if (ferror(to))
! 102: goto err;
! 103: }
! 104: if (!done)
! 105: (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
! 106: pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
! 107: pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
! 108: pw->pw_dir, pw->pw_shell);
! 109:
! 110: if (ferror(to))
! 111: err: pw_error(NULL, 1, 1);
! 112: (void)fclose(to);
! 113: }