1 /* $OpenBSD: vipw.c,v 1.18 2015/10/16 22:54:15 deraadt 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 if (pledge("stdio rpath wpath cpath fattr proc exec", NULL) == -1) 68 err(1, "pledge"); 69 70 pw_init(); 71 tfd = pw_lock(0); 72 if (tfd < 0) 73 errx(1, "the passwd file is busy or you cannot lock."); 74 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0); 75 if (pfd < 0) 76 pw_error(_PATH_MASTERPASSWD, 1, 1); 77 copyfile(pfd, tfd, &begin); 78 (void)close(tfd); 79 80 for (;;) { 81 pw_edit(0, NULL); 82 if (stat(_PATH_MASTERPASSWD_LOCK, &end)) 83 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1); 84 if (timespeccmp(&begin.st_mtimespec, &end.st_mtimespec, ==) && 85 begin.st_size == end.st_size) { 86 warnx("no changes made"); 87 pw_error((char *)NULL, 0, 0); 88 } 89 if (pw_mkdb(NULL, 0) == 0) 90 break; 91 pw_prompt(); 92 } 93 exit(0); 94 } 95 96 void 97 copyfile(int from, int to, struct stat *sb) 98 { 99 int nr, nw, off; 100 char buf[8*1024]; 101 struct timespec ts[2]; 102 103 if (fstat(from, sb) == -1) 104 pw_error(_PATH_MASTERPASSWD, 1, 1); 105 while ((nr = read(from, buf, sizeof(buf))) > 0) 106 for (off = 0; nr > 0; nr -= nw, off += nw) 107 if ((nw = write(to, buf + off, nr)) < 0) 108 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1); 109 if (nr < 0) 110 pw_error(_PATH_MASTERPASSWD, 1, 1); 111 112 ts[0] = sb->st_atim; 113 ts[1] = sb->st_mtim; 114 (void)futimens(to, ts); 115 } 116 117 void 118 usage(void) 119 { 120 extern char *__progname; 121 122 (void)fprintf(stderr, "usage: %s\n", __progname); 123 exit(1); 124 } 125