GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/vipw/vipw.c Lines: 0 44 0.0 %
Date: 2017-11-07 Branches: 0 32 0.0 %

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