xref: /freebsd/lib/libutil/pw_util.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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 #if 0
36 static const char sccsid[] = "@(#)pw_util.c	8.3 (Berkeley) 4/2/94";
37 #endif
38 static const char rcsid[] =
39 	"$Id$";
40 #endif /* not lint */
41 
42 /*
43  * This file is used by all the "password" programs; vipw(8), chpass(1),
44  * and passwd(1).
45  */
46 
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52 
53 #include <err.h>
54 #include <fcntl.h>
55 #include <paths.h>
56 #include <pwd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 #include "pw_util.h"
64 
65 extern char *tempname;
66 static pid_t editpid = -1;
67 static int lockfd;
68 
69 void
70 pw_cont(sig)
71 	int sig;
72 {
73 
74 	if (editpid != -1)
75 		kill(editpid, sig);
76 }
77 
78 void
79 pw_init()
80 {
81 	struct rlimit rlim;
82 
83 	/* Unlimited resource limits. */
84 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
85 	(void)setrlimit(RLIMIT_CPU, &rlim);
86 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
87 	(void)setrlimit(RLIMIT_STACK, &rlim);
88 	(void)setrlimit(RLIMIT_DATA, &rlim);
89 	(void)setrlimit(RLIMIT_RSS, &rlim);
90 
91 	/* Don't drop core (not really necessary, but GP's). */
92 	rlim.rlim_cur = rlim.rlim_max = 0;
93 	(void)setrlimit(RLIMIT_CORE, &rlim);
94 
95 	/* Turn off signals. */
96 	(void)signal(SIGALRM, SIG_IGN);
97 	(void)signal(SIGHUP, SIG_IGN);
98 	(void)signal(SIGINT, SIG_IGN);
99 	(void)signal(SIGPIPE, SIG_IGN);
100 	(void)signal(SIGQUIT, SIG_IGN);
101 	(void)signal(SIGTERM, SIG_IGN);
102 	(void)signal(SIGCONT, pw_cont);
103 
104 	/* Create with exact permissions. */
105 	(void)umask(0);
106 }
107 
108 int
109 pw_lock()
110 {
111 	/*
112 	 * If the master password file doesn't exist, the system is hosed.
113 	 * Might as well try to build one.  Set the close-on-exec bit so
114 	 * that users can't get at the encrypted passwords while editing.
115 	 * Open should allow flock'ing the file; see 4.4BSD.	XXX
116 	 */
117 	lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
118 	if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1)
119 		err(1, "%s", _PATH_MASTERPASSWD);
120 	if (flock(lockfd, LOCK_EX|LOCK_NB))
121 		errx(1, "the password db file is busy");
122 	return (lockfd);
123 }
124 
125 int
126 pw_tmp()
127 {
128 	static char path[MAXPATHLEN] = _PATH_MASTERPASSWD;
129 	int fd;
130 	char *p;
131 
132 	if ((p = strrchr(path, '/')))
133 		++p;
134 	else
135 		p = path;
136 	strcpy(p, "pw.XXXXXX");
137 	if ((fd = mkstemp(path)) == -1)
138 		err(1, "%s", path);
139 	tempname = path;
140 	return (fd);
141 }
142 
143 int
144 pw_mkdb(username)
145 char *username;
146 {
147 	int pstat;
148 	pid_t pid;
149 
150 	(void)fflush(stderr);
151 	if (!(pid = vfork())) {
152 		if(!username) {
153 			warnx("rebuilding the database...");
154 			execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL);
155 		} else {
156 			warnx("updating the database...");
157 			execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-u",
158 					username, tempname, NULL);
159 		}
160 		pw_error(_PATH_PWD_MKDB, 1, 1);
161 	}
162 	pid = waitpid(pid, &pstat, 0);
163 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
164 		return (0);
165 	warnx("done");
166 	return (1);
167 }
168 
169 void
170 pw_edit(notsetuid)
171 	int notsetuid;
172 {
173 	int pstat;
174 	char *p, *editor;
175 
176 	if (!(editor = getenv("EDITOR")))
177 		editor = _PATH_VI;
178 	if ((p = strrchr(editor, '/')))
179 		++p;
180 	else
181 		p = editor;
182 
183 	if (!(editpid = vfork())) {
184 		if (notsetuid) {
185 			(void)setgid(getgid());
186 			(void)setuid(getuid());
187 		}
188 		execlp(editor, p, tempname, NULL);
189 		_exit(1);
190 	}
191 	for (;;) {
192 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
193 		if (editpid == -1)
194 			pw_error(editor, 1, 1);
195 		else if (WIFSTOPPED(pstat))
196 			raise(WSTOPSIG(pstat));
197 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
198 			break;
199 		else
200 			pw_error(editor, 1, 1);
201 	}
202 	editpid = -1;
203 }
204 
205 void
206 pw_prompt()
207 {
208 	int c, first;
209 
210 	(void)printf("re-edit the password file? [y]: ");
211 	(void)fflush(stdout);
212 	first = c = getchar();
213 	while (c != '\n' && c != EOF)
214 		c = getchar();
215 	if (first == 'n')
216 		pw_error(NULL, 0, 0);
217 }
218 
219 void
220 pw_error(name, err, eval)
221 	char *name;
222 	int err, eval;
223 {
224 #ifdef YP
225 	extern int _use_yp;
226 #endif /* YP */
227 	if (err)
228 		warn(name);
229 #ifdef YP
230 	if (_use_yp)
231 		warnx("NIS information unchanged");
232 	else
233 #endif /* YP */
234 	warnx("%s: unchanged", _PATH_MASTERPASSWD);
235 	(void)unlink(tempname);
236 	exit(eval);
237 }
238