xref: /freebsd/lib/libutil/pw_util.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)pw_util.c	8.3 (Berkeley) 4/2/94";
36 #endif /* not lint */
37 
38 /*
39  * This file is used by all the "password" programs; vipw(8), chpass(1),
40  * and passwd(1).
41  */
42 
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/resource.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "pw_util.h"
61 
62 extern char *tempname;
63 
64 void
65 pw_init()
66 {
67 	struct rlimit rlim;
68 
69 	/* Unlimited resource limits. */
70 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
71 	(void)setrlimit(RLIMIT_CPU, &rlim);
72 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
73 	(void)setrlimit(RLIMIT_STACK, &rlim);
74 	(void)setrlimit(RLIMIT_DATA, &rlim);
75 	(void)setrlimit(RLIMIT_RSS, &rlim);
76 
77 	/* Don't drop core (not really necessary, but GP's). */
78 	rlim.rlim_cur = rlim.rlim_max = 0;
79 	(void)setrlimit(RLIMIT_CORE, &rlim);
80 
81 	/* Turn off signals. */
82 	(void)signal(SIGALRM, SIG_IGN);
83 	(void)signal(SIGHUP, SIG_IGN);
84 	(void)signal(SIGINT, SIG_IGN);
85 	(void)signal(SIGPIPE, SIG_IGN);
86 	(void)signal(SIGQUIT, SIG_IGN);
87 	(void)signal(SIGTERM, SIG_IGN);
88 	(void)signal(SIGTSTP, SIG_IGN);
89 	(void)signal(SIGTTOU, SIG_IGN);
90 
91 	/* Create with exact permissions. */
92 	(void)umask(0);
93 }
94 
95 static int lockfd;
96 
97 int
98 pw_lock()
99 {
100 	/*
101 	 * If the master password file doesn't exist, the system is hosed.
102 	 * Might as well try to build one.  Set the close-on-exec bit so
103 	 * that users can't get at the encrypted passwords while editing.
104 	 * Open should allow flock'ing the file; see 4.4BSD.	XXX
105 	 */
106 	lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
107 	if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
108 		err(1, "%s", _PATH_MASTERPASSWD);
109 	if (flock(lockfd, LOCK_EX|LOCK_NB))
110 		errx(1, "the password db file is busy");
111 	return (lockfd);
112 }
113 
114 int
115 pw_tmp()
116 {
117 	static char path[MAXPATHLEN] = _PATH_MASTERPASSWD;
118 	int fd;
119 	char *p;
120 
121 	if (p = strrchr(path, '/'))
122 		++p;
123 	else
124 		p = path;
125 	strcpy(p, "pw.XXXXXX");
126 	if ((fd = mkstemp(path)) == -1)
127 		err(1, "%s", path);
128 	tempname = path;
129 	return (fd);
130 }
131 
132 int
133 pw_mkdb()
134 {
135 	int pstat;
136 	pid_t pid;
137 
138 	warnx("rebuilding the database...");
139 	(void)fflush(stderr);
140 	if (!(pid = vfork())) {
141 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL);
142 		pw_error(_PATH_PWD_MKDB, 1, 1);
143 	}
144 	pid = waitpid(pid, &pstat, 0);
145 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
146 		return (0);
147 	warnx("done");
148 	return (1);
149 }
150 
151 void
152 pw_edit(notsetuid)
153 	int notsetuid;
154 {
155 	int pstat;
156 	pid_t pid;
157 	char *p, *editor;
158 
159 	if (!(editor = getenv("EDITOR")))
160 		editor = _PATH_VI;
161 	if (p = strrchr(editor, '/'))
162 		++p;
163 	else
164 		p = editor;
165 
166 	if (!(pid = vfork())) {
167 		if (notsetuid) {
168 			(void)setgid(getgid());
169 			(void)setuid(getuid());
170 		}
171 		execlp(editor, p, tempname, NULL);
172 		_exit(1);
173 	}
174 	pid = waitpid(pid, (int *)&pstat, 0);
175 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
176 		pw_error(editor, 1, 1);
177 }
178 
179 void
180 pw_prompt()
181 {
182 	int c;
183 
184 	(void)printf("re-edit the password file? [y]: ");
185 	(void)fflush(stdout);
186 	c = getchar();
187 	if (c != EOF && c != '\n')
188 		while (getchar() != '\n');
189 	if (c == 'n')
190 		pw_error(NULL, 0, 0);
191 }
192 
193 void
194 pw_error(name, err, eval)
195 	char *name;
196 	int err, eval;
197 {
198 	if (err)
199 		warn(name);
200 
201 	warnx("%s: unchanged", _PATH_MASTERPASSWD);
202 	(void)unlink(tempname);
203 	exit(eval);
204 }
205