1 /* $OpenBSD: vipw.c,v 1.17 2014/05/07 21:20:06 schwarze Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/time.h> 33 #include <sys/stat.h> 34 35 #include <err.h> 36 #include <pwd.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <fcntl.h> 42 #include <util.h> 43 44 void copyfile(int, int, struct stat *); 45 void usage(void); 46 47 int 48 main(int argc, char *argv[]) 49 { 50 int pfd, tfd; 51 struct stat begin, end; 52 int ch; 53 54 while ((ch = getopt(argc, argv, "")) != -1) { 55 switch (ch) { 56 case '?': 57 default: 58 usage(); 59 } 60 } 61 argc -= optind; 62 argv += optind; 63 64 if (argc != 0) 65 usage(); 66 67 pw_init(); 68 tfd = pw_lock(0); 69 if (tfd < 0) 70 errx(1, "the passwd file is busy or you cannot lock."); 71 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0); 72 if (pfd < 0) 73 pw_error(_PATH_MASTERPASSWD, 1, 1); 74 copyfile(pfd, tfd, &begin); 75 (void)close(tfd); 76 77 for (;;) { 78 pw_edit(0, NULL); 79 if (stat(_PATH_MASTERPASSWD_LOCK, &end)) 80 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1); 81 if (timespeccmp(&begin.st_mtimespec, &end.st_mtimespec, ==) && 82 begin.st_size == end.st_size) { 83 warnx("no changes made"); 84 pw_error((char *)NULL, 0, 0); 85 } 86 if (pw_mkdb(NULL, 0) == 0) 87 break; 88 pw_prompt(); 89 } 90 exit(0); 91 } 92 93 void 94 copyfile(int from, int to, struct stat *sb) 95 { 96 int nr, nw, off; 97 char buf[8*1024]; 98 struct timespec ts[2]; 99 100 if (fstat(from, sb) == -1) 101 pw_error(_PATH_MASTERPASSWD, 1, 1); 102 while ((nr = read(from, buf, sizeof(buf))) > 0) 103 for (off = 0; nr > 0; nr -= nw, off += nw) 104 if ((nw = write(to, buf + off, nr)) < 0) 105 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1); 106 if (nr < 0) 107 pw_error(_PATH_MASTERPASSWD, 1, 1); 108 109 ts[0] = sb->st_atim; 110 ts[1] = sb->st_mtim; 111 (void)futimens(to, ts); 112 } 113 114 void 115 usage(void) 116 { 117 extern char *__progname; 118 119 (void)fprintf(stderr, "usage: %s\n", __progname); 120 exit(1); 121 } 122