1/*	$OpenBSD: vipw.c,v 1.27 2023/08/11 04:45:05 guenther 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 <fcntl.h>
37#include <paths.h>
38#include <pwd.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <util.h>
43
44void		copyfile(int, int, struct stat *);
45__dead void	usage(void);
46
47int
48main(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		default:
57			usage();
58		}
59	}
60	argc -= optind;
61	argv += optind;
62
63	if (argc != 0)
64		usage();
65
66	if (unveil(_PATH_MASTERPASSWD_LOCK, "rwc") == -1)
67		err(1, "unveil %s", _PATH_MASTERPASSWD_LOCK);
68	if (unveil(_PATH_MASTERPASSWD, "r") == -1)
69		err(1, "unveil %s",  _PATH_MASTERPASSWD);
70	if (unveil(_PATH_BSHELL, "x") == -1)
71		err(1, "unveil %s", _PATH_BSHELL);
72	if (unveil(_PATH_PWD_MKDB, "x") == -1)
73		err(1, "unveil %s", _PATH_PWD_MKDB);
74	if (pledge("stdio rpath wpath cpath fattr proc exec", NULL) == -1)
75		err(1, "pledge");
76
77	pw_init();
78	tfd = pw_lock(0);
79	if (tfd == -1)
80		errx(1, "the passwd file is busy or you cannot lock.");
81	pfd = open(_PATH_MASTERPASSWD, O_RDONLY);
82	if (pfd == -1)
83		pw_error(_PATH_MASTERPASSWD, 1, 1);
84	copyfile(pfd, tfd, &begin);
85	(void)close(tfd);
86
87	for (;;) {
88		pw_edit(0, NULL);
89		if (stat(_PATH_MASTERPASSWD_LOCK, &end))
90			pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
91		if (timespeccmp(&begin.st_mtim, &end.st_mtim, ==) &&
92		    begin.st_size == end.st_size) {
93			warnx("no changes made");
94			pw_error((char *)NULL, 0, 0);
95		}
96		if (pw_mkdb(NULL, 0) == 0)
97			break;
98		pw_prompt();
99	}
100
101	return 0;
102}
103
104void
105copyfile(int from, int to, struct stat *sb)
106{
107	int nr, nw, off;
108	char buf[8*1024];
109	struct timespec ts[2];
110
111	if (fstat(from, sb) == -1)
112		pw_error(_PATH_MASTERPASSWD, 1, 1);
113	while ((nr = read(from, buf, sizeof(buf))) > 0)
114		for (off = 0; nr > 0; nr -= nw, off += nw)
115			if ((nw = write(to, buf + off, nr)) == -1)
116				pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
117	if (nr == -1)
118		pw_error(_PATH_MASTERPASSWD, 1, 1);
119
120	ts[0] = sb->st_atim;
121	ts[1] = sb->st_mtim;
122	(void)futimens(to, ts);
123}
124
125__dead void
126usage(void)
127{
128	extern char *__progname;
129
130	(void)fprintf(stderr, "usage: %s\n", __progname);
131	exit(1);
132}
133