xref: /netbsd-src/lib/libutil/passwd.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*
2  * Copyright (c) 1987, 1993, 1994, 1995
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 #if defined(LIBC_SCCS) && !defined(lint)
35 static char rcsid[] = "$NetBSD: passwd.c,v 1.4 1997/04/23 18:51:17 mycroft Exp $";
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/wait.h>
43 
44 #include <ctype.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <pwd.h>
51 #include <errno.h>
52 #include <paths.h>
53 #include <signal.h>
54 #include <limits.h>
55 #include <util.h>
56 
57 static void	pw_cont __P((int sig));
58 
59 int
60 pw_lock(retries)
61 	int retries;
62 {
63 	int i, fd;
64 	mode_t old_mode;
65 
66 	/* Acquire the lock file. */
67 	old_mode = umask(0);
68 	fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
69 	for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
70 		sleep(1);
71 		fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
72 			  0600);
73 	}
74 	umask(old_mode);
75 	return(fd);
76 }
77 
78 int
79 pw_mkdb()
80 {
81 	int pstat;
82 	pid_t pid;
83 
84 	pid = vfork();
85 	if (pid == 0) {
86 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
87 		      _PATH_MASTERPASSWD_LOCK, NULL);
88 		exit(1);
89 	}
90 	pid = waitpid(pid, &pstat, 0);
91 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
92 		return(-1);
93 	return(0);
94 }
95 
96 int
97 pw_abort()
98 {
99 	return(unlink(_PATH_MASTERPASSWD_LOCK));
100 }
101 
102 /* Everything below this point is intended for the convenience of programs
103  * which allow a user to interactively edit the passwd file.  Errors in the
104  * routines below will cause the process to abort. */
105 
106 static pid_t editpid = -1;
107 
108 static void
109 pw_cont(sig)
110 	int sig;
111 {
112 
113 	if (editpid != -1)
114 		kill(editpid, sig);
115 }
116 
117 void
118 pw_init()
119 {
120 	struct rlimit rlim;
121 
122 	/* Unlimited resource limits. */
123 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
124 	(void)setrlimit(RLIMIT_CPU, &rlim);
125 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
126 	(void)setrlimit(RLIMIT_STACK, &rlim);
127 	(void)setrlimit(RLIMIT_DATA, &rlim);
128 	(void)setrlimit(RLIMIT_RSS, &rlim);
129 
130 	/* Don't drop core (not really necessary, but GP's). */
131 	rlim.rlim_cur = rlim.rlim_max = 0;
132 	(void)setrlimit(RLIMIT_CORE, &rlim);
133 
134 	/* Turn off signals. */
135 	(void)signal(SIGALRM, SIG_IGN);
136 	(void)signal(SIGHUP, SIG_IGN);
137 	(void)signal(SIGINT, SIG_IGN);
138 	(void)signal(SIGPIPE, SIG_IGN);
139 	(void)signal(SIGQUIT, SIG_IGN);
140 	(void)signal(SIGTERM, SIG_IGN);
141 	(void)signal(SIGCONT, pw_cont);
142 }
143 
144 void
145 pw_edit(notsetuid, filename)
146 	int notsetuid;
147 	const char *filename;
148 {
149 	int i, j, xargc, pstat;
150 	char *p, *editor;
151 	char **xargv;
152 
153 	if (filename == NULL)
154 		filename = _PATH_MASTERPASSWD_LOCK;
155 	if ((editor = getenv("EDITOR")) == NULL)
156 		editor = strdup(_PATH_VI);
157 	else
158 		editor = strdup(editor);
159 	if (p = strrchr(editor, '/'))
160 		++p;
161 	else
162 		p = editor;
163 
164 	/* Scan editor string, count spaces, allocate arg vector. */
165 	for (i = 0, xargc = 0; p[i] != '\0'; i++) {
166 		if (isspace(p[i])) {
167 			while (isspace(p[i++]))
168 				/* skip white space */ ;
169 			if (p[i] == '\0')
170 				break;
171 			xargc++;
172 		}
173 	}
174 
175 	/* argv[0] + <xargc args> + filename + NULL */
176 	xargv = (char **)malloc(sizeof(char *) * (xargc + 3));
177 	if (xargv == NULL)
178 		pw_error("malloc failed", 1, 1);
179 
180 	i = 0;
181 	xargv[i++] = p;
182 	for (; *p != '\0'; p++) {
183 		if (isspace(*p)) {
184 			while(isspace(*p))
185 				*p++ = '\0';	/* blast whitespace */
186 			if (*p == '\0')
187 				break;
188 			xargv[i++] = p;
189 		}
190 	}
191 
192 	xargv[i++] = (char *)filename;
193 	xargv[i] = NULL;
194 
195 	if (!(editpid = vfork())) {
196 		if (notsetuid) {
197 			setgid(getgid());
198 			setuid(getuid());
199 		}
200 		execvp(editor, xargv);
201 		_exit(1);
202 	}
203 	for (;;) {
204 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
205 		if (editpid == -1)
206 			pw_error(editor, 1, 1);
207 		else if (WIFSTOPPED(pstat))
208 			raise(WSTOPSIG(pstat));
209 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
210 			break;
211 		else
212 			pw_error(editor, 1, 1);
213 	}
214 	editpid = -1;
215 	free(editor);
216 	free(xargv);
217 }
218 
219 void
220 pw_prompt()
221 {
222 	int c;
223 
224 	(void)printf("re-edit the password file? [y]: ");
225 	(void)fflush(stdout);
226 	c = getchar();
227 	if (c != EOF && c != '\n')
228 		while (getchar() != '\n');
229 	if (c == 'n')
230 		pw_error(NULL, 0, 0);
231 }
232 
233 void
234 pw_copy(ffd, tfd, pw)
235 	int ffd, tfd;
236 	struct passwd *pw;
237 {
238 	FILE *from, *to;
239 	int done;
240 	char *p, buf[8192];
241 
242 	if (!(from = fdopen(ffd, "r")))
243 		pw_error(_PATH_MASTERPASSWD, 1, 1);
244 	if (!(to = fdopen(tfd, "w")))
245 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
246 
247 	for (done = 0; fgets(buf, sizeof(buf), from);) {
248 		if (!strchr(buf, '\n')) {
249 			warnx("%s: line too long", _PATH_MASTERPASSWD);
250 			pw_error(NULL, 0, 1);
251 		}
252 		if (done) {
253 			(void)fprintf(to, "%s", buf);
254 			if (ferror(to))
255 				goto err;
256 			continue;
257 		}
258 		if (!(p = strchr(buf, ':'))) {
259 			warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
260 			pw_error(NULL, 0, 1);
261 		}
262 		*p = '\0';
263 		if (strcmp(buf, pw->pw_name)) {
264 			*p = ':';
265 			(void)fprintf(to, "%s", buf);
266 			if (ferror(to))
267 				goto err;
268 			continue;
269 		}
270 		(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
271 		    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
272 		    pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
273 		    pw->pw_dir, pw->pw_shell);
274 		done = 1;
275 		if (ferror(to))
276 			goto err;
277 	}
278 	if (!done)
279 		(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
280 		    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
281 		    pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
282 		    pw->pw_dir, pw->pw_shell);
283 
284 	if (ferror(to))
285 err:		pw_error(NULL, 1, 1);
286 	(void)fclose(to);
287 }
288 
289 int
290 pw_scan(bp, pw, flags)
291 	char *bp;
292 	struct passwd *pw;
293 	int *flags;
294 {
295 	long id;
296 	int root;
297 	char *p, *sh;
298 
299 	if (flags != (int *)NULL)
300 		*flags = 0;
301 
302 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
303 		goto fmt;
304 	root = !strcmp(pw->pw_name, "root");
305 
306 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
307 		goto fmt;
308 
309 	if (!(p = strsep(&bp, ":")))			/* uid */
310 		goto fmt;
311 	id = atol(p);
312 	if (root && id) {
313 		warnx("root uid should be 0");
314 		return (0);
315 	}
316 	pw->pw_uid = id;
317 	if ((*p == '\0') && (flags != (int *)NULL))
318 		*flags |= _PASSWORD_NOUID;
319 
320 	if (!(p = strsep(&bp, ":")))			/* gid */
321 		goto fmt;
322 	id = atol(p);
323 	pw->pw_gid = id;
324 	if ((*p == '\0') && (flags != (int *)NULL))
325 		*flags |= _PASSWORD_NOGID;
326 
327 	pw->pw_class = strsep(&bp, ":");		/* class */
328 	if (!(p = strsep(&bp, ":")))			/* change */
329 		goto fmt;
330 	pw->pw_change = atol(p);
331 	if ((*p == '\0') && (flags != (int *)NULL))
332 		*flags |= _PASSWORD_NOCHG;
333 	if (!(p = strsep(&bp, ":")))			/* expire */
334 		goto fmt;
335 	pw->pw_expire = atol(p);
336 	if ((*p == '\0') && (flags != (int *)NULL))
337 		*flags |= _PASSWORD_NOEXP;
338 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
339 	pw->pw_dir = strsep(&bp, ":");			/* directory */
340 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
341 		goto fmt;
342 
343 	p = pw->pw_shell;
344 	if (root && *p)					/* empty == /bin/sh */
345 		for (setusershell();;) {
346 			if (!(sh = getusershell())) {
347 				warnx("warning, unknown root shell");
348 				break;
349 			}
350 			if (!strcmp(p, sh))
351 				break;
352 		}
353 
354 	if (p = strsep(&bp, ":")) {			/* too many */
355 fmt:		warnx("corrupted entry");
356 		return (0);
357 	}
358 
359 	return (1);
360 }
361 
362 void
363 pw_error(name, err, eval)
364 	const char *name;
365 	int err, eval;
366 {
367 	if (err)
368 		warn(name);
369 
370 	warnx("%s: unchanged", _PATH_MASTERPASSWD);
371 	pw_abort();
372 	exit(eval);
373 }
374 
375