Home | History | Annotate | Line # | Download | only in libutil
passwd.c revision 1.21
      1 /*	$NetBSD: passwd.c,v 1.21 2000/07/06 13:09:47 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994, 1995
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 __RCSID("$NetBSD: passwd.c,v 1.21 2000/07/06 13:09:47 ad Exp $");
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <sys/time.h>
     44 #include <sys/resource.h>
     45 #include <sys/wait.h>
     46 
     47 #include <assert.h>
     48 #include <ctype.h>
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <limits.h>
     53 #include <paths.h>
     54 #include <pwd.h>
     55 #include <signal.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 #include <util.h>
     61 
     62 static void	pw_cont(int sig);
     63 static int	pw_equal(char *buf, struct passwd *old_pw);
     64 
     65 int
     66 pw_lock(int retries)
     67 {
     68 	int i, fd;
     69 	mode_t old_mode;
     70 	int oerrno;
     71 
     72 	/* Acquire the lock file. */
     73 	old_mode = umask(0);
     74 	fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
     75 	for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
     76 		sleep(1);
     77 		fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
     78 			  0600);
     79 	}
     80 	oerrno = errno;
     81 	(void)umask(old_mode);
     82 	errno = oerrno;
     83 	return(fd);
     84 }
     85 
     86 int
     87 pw_mkdb(void)
     88 {
     89 	int pstat;
     90 	pid_t pid;
     91 
     92 	pid = vfork();
     93 	if (pid == -1)
     94 		return (-1);
     95 
     96 	if (pid == 0) {
     97 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
     98 		      _PATH_MASTERPASSWD_LOCK, NULL);
     99 		_exit(1);
    100 	}
    101 	pid = waitpid(pid, &pstat, 0);
    102 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
    103 		return(-1);
    104 	return(0);
    105 }
    106 
    107 int
    108 pw_abort(void)
    109 {
    110 
    111 	return(unlink(_PATH_MASTERPASSWD_LOCK));
    112 }
    113 
    114 /* Everything below this point is intended for the convenience of programs
    115  * which allow a user to interactively edit the passwd file.  Errors in the
    116  * routines below will cause the process to abort. */
    117 
    118 static pid_t editpid = -1;
    119 
    120 static void
    121 pw_cont(int sig)
    122 {
    123 
    124 	if (editpid != -1)
    125 		kill(editpid, sig);
    126 }
    127 
    128 void
    129 pw_init(void)
    130 {
    131 	struct rlimit rlim;
    132 
    133 	/* Unlimited resource limits. */
    134 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
    135 	(void)setrlimit(RLIMIT_CPU, &rlim);
    136 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
    137 	(void)setrlimit(RLIMIT_STACK, &rlim);
    138 	(void)setrlimit(RLIMIT_DATA, &rlim);
    139 	(void)setrlimit(RLIMIT_RSS, &rlim);
    140 
    141 	/* Don't drop core (not really necessary, but GP's). */
    142 	rlim.rlim_cur = rlim.rlim_max = 0;
    143 	(void)setrlimit(RLIMIT_CORE, &rlim);
    144 
    145 	/* Turn off signals. */
    146 	(void)signal(SIGALRM, SIG_IGN);
    147 	(void)signal(SIGHUP, SIG_IGN);
    148 	(void)signal(SIGINT, SIG_IGN);
    149 	(void)signal(SIGPIPE, SIG_IGN);
    150 	(void)signal(SIGQUIT, SIG_IGN);
    151 	(void)signal(SIGTERM, SIG_IGN);
    152 	(void)signal(SIGCONT, pw_cont);
    153 }
    154 
    155 void
    156 pw_edit(int notsetuid, const char *filename)
    157 {
    158 	int pstat;
    159 	char *p, *editor;
    160 	char *argp[] = { "sh", "-c", NULL, NULL };
    161 
    162 #ifdef __GNUC__
    163 	(void) &editor;
    164 #endif
    165 
    166 	if (filename == NULL)
    167 		filename = _PATH_MASTERPASSWD_LOCK;
    168 
    169 	if ((editor = getenv("EDITOR")) == NULL)
    170 		editor = _PATH_VI;
    171 
    172 	p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
    173 	if (p == NULL)
    174 		return;
    175 
    176 	sprintf(p, "%s %s", editor, filename);
    177 	argp[2] = p;
    178 
    179 	switch(editpid = vfork()) {
    180 	case -1:
    181 		free(p);
    182 		return;
    183 	case 0:
    184 		if (notsetuid) {
    185 			setgid(getgid());
    186 			setuid(getuid());
    187 		}
    188 		execvp(_PATH_BSHELL, argp);
    189 		_exit(1);
    190 	}
    191 
    192 	free(p);
    193 
    194 	for (;;) {
    195 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
    196 		if (editpid == -1)
    197 			pw_error(editor, 1, 1);
    198 		else if (WIFSTOPPED(pstat))
    199 			raise(WSTOPSIG(pstat));
    200 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
    201 			break;
    202 		else
    203 			pw_error(editor, 1, 1);
    204 	}
    205 	editpid = -1;
    206 }
    207 
    208 void
    209 pw_prompt(void)
    210 {
    211 	int c;
    212 
    213 	(void)printf("re-edit the password file? [y]: ");
    214 	(void)fflush(stdout);
    215 	c = getchar();
    216 	if (c != EOF && c != '\n')
    217 		while (getchar() != '\n');
    218 	if (c == 'n')
    219 		pw_error(NULL, 0, 0);
    220 }
    221 
    222 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
    223 static int
    224 pw_equal(char *buf, struct passwd *pw)
    225 {
    226 	struct passwd buf_pw;
    227 	int len;
    228 
    229 	_DIAGASSERT(buf != NULL);
    230 	_DIAGASSERT(pw != NULL);
    231 
    232 	len = strlen (buf);
    233 	if (buf[len-1] == '\n')
    234 		buf[len-1] = '\0';
    235 	if (!pw_scan(buf, &buf_pw, NULL))
    236 		return 0;
    237 	return !strcmp(pw->pw_name, buf_pw.pw_name)
    238 		&& pw->pw_uid == buf_pw.pw_uid
    239 		&& pw->pw_gid == buf_pw.pw_gid
    240 		&& !strcmp(pw->pw_class, buf_pw.pw_class)
    241 		&& (long)pw->pw_change == (long)buf_pw.pw_change
    242 		&& (long)pw->pw_expire == (long)buf_pw.pw_expire
    243 		&& !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
    244 		&& !strcmp(pw->pw_dir, buf_pw.pw_dir)
    245 		&& !strcmp(pw->pw_shell, buf_pw.pw_shell);
    246 }
    247 
    248 void
    249 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
    250 {
    251 	FILE *from, *to;
    252 	int done;
    253 	char *p, buf[8192];
    254 
    255 	_DIAGASSERT(pw != NULL);
    256 	/* old_pw may be NULL */
    257 
    258 	if (!(from = fdopen(ffd, "r")))
    259 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    260 	if (!(to = fdopen(tfd, "w")))
    261 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    262 
    263 	for (done = 0; fgets(buf, sizeof(buf), from);) {
    264 		if (!strchr(buf, '\n')) {
    265 			warnx("%s: line too long", _PATH_MASTERPASSWD);
    266 			pw_error(NULL, 0, 1);
    267 		}
    268 		if (done) {
    269 			(void)fprintf(to, "%s", buf);
    270 			if (ferror(to))
    271 				goto err;
    272 			continue;
    273 		}
    274 		if (!(p = strchr(buf, ':'))) {
    275 			warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
    276 			pw_error(NULL, 0, 1);
    277 		}
    278 		*p = '\0';
    279 		if (strcmp(buf, pw->pw_name)) {
    280 			*p = ':';
    281 			(void)fprintf(to, "%s", buf);
    282 			if (ferror(to))
    283 				goto err;
    284 			continue;
    285 		}
    286 		*p = ':';
    287 		if (old_pw && !pw_equal(buf, old_pw)) {
    288 			warnx("%s: entry inconsistent",
    289 			      _PATH_MASTERPASSWD);
    290 			pw_error(NULL, 0, 1);
    291 		}
    292 		(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    293 		    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    294 		    pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
    295 		    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
    296 		done = 1;
    297 		if (ferror(to))
    298 			goto err;
    299 	}
    300 	/* Only append a new entry if real uid is root! */
    301 	if (!done) {
    302 		if (getuid() == 0)
    303 			(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    304 			    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    305 			    pw->pw_class, (long)pw->pw_change,
    306 			    (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
    307 			    pw->pw_shell);
    308 		else
    309 			warnx("%s: changes not made, no such entry",
    310 			    _PATH_MASTERPASSWD);
    311 	}
    312 
    313 	if (ferror(to))
    314 err:		pw_error(NULL, 1, 1);
    315 	(void)fclose(to);
    316 }
    317 
    318 void
    319 pw_error(const char *name, int err, int eval)
    320 {
    321 
    322 	if (err)
    323 		warn(name);
    324 
    325 	warnx("%s: unchanged", _PATH_MASTERPASSWD);
    326 	pw_abort();
    327 	exit(eval);
    328 }
    329 
    330 /* Removes head and/or tail spaces. */
    331 static void
    332 trim_whitespace(char *line)
    333 {
    334 	char *p;
    335 
    336 	/* Remove leading spaces */
    337 	p = line;
    338 	while (isspace(*p))
    339 		p++;
    340 	memmove(line, p, strlen(p) + 1);
    341 
    342 	/* Remove trailing spaces */
    343 	p = line + strlen(line) - 1;
    344 	while (isspace(*p))
    345 		p--;
    346 	*(p + 1) = '\0';
    347 }
    348 
    349 
    350 /* Get one line, remove spaces from front and tail */
    351 static int
    352 read_line(FILE *fp, char *line, int max)
    353 {
    354 	char   *p;
    355 
    356 	/* Read one line of config */
    357 	if (fgets(line, max, fp) == NULL)
    358 		return (0);
    359 
    360 	if ((p = strchr(line, '\n')) == NULL) {
    361 		warnx("line too long");
    362 		return (0);
    363 	}
    364 	*p = '\0';
    365 
    366 	/* Remove comments */
    367 	if ((p = strchr(line, '#')) != NULL)
    368 		*p = '\0';
    369 
    370 	trim_whitespace(line);
    371 	return (1);
    372 }
    373 
    374 static const char *
    375 pw_default(const char *option)
    376 {
    377 	static const char *options[][2] = {
    378 		{ "localcipher",	"old" },
    379 		{ "ypcipher",		"old" },
    380 	};
    381 	int i;
    382 
    383 	for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
    384 		if (strcmp(options[i][0], option) == 0)
    385 			return (options[i][1]);
    386 
    387 	return (NULL);
    388 }
    389 
    390 /*
    391  * Retrieve password information from the /etc/passwd.conf file, at the
    392  * moment this is only for choosing the cipher to use.  It could easily be
    393  * used for other authentication methods as well.
    394  */
    395 void
    396 pw_getconf(char *data, size_t max, const char *key, const char *option)
    397 {
    398 	FILE *fp;
    399 	char line[LINE_MAX], *p, *p2;
    400 	static char result[LINE_MAX];
    401 	int got, found;
    402 	const char *cp;
    403 
    404 	got = 0;
    405 	found = 0;
    406 	result[0] = '\0';
    407 
    408 	if ((fp = fopen(_PATH_PASSWDCONF, "r")) == NULL) {
    409 		if ((cp = pw_default(option)) != NULL)
    410 			strlcpy(data, p, max);
    411 		else
    412 			data[0] = '\0';
    413 		return;
    414 	}
    415 
    416 	while (!found && (got || read_line(fp, line, LINE_MAX))) {
    417 		got = 0;
    418 
    419 		if (strncmp(key, line, strlen(key)) != 0 ||
    420 		    line[strlen(key)] != ':')
    421 			continue;
    422 
    423 		/* Now we found our specified key */
    424 		while (read_line(fp, line, LINE_MAX)) {
    425 			/* Leaving key field */
    426 			if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
    427 				got = 1;
    428 				break;
    429 			}
    430 			p2 = line;
    431 			if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
    432 				continue;
    433 			trim_whitespace(p);
    434 
    435 			if (!strncmp(p, option, strlen(option))) {
    436 				trim_whitespace(p2);
    437 				strcpy(result, p2);
    438 				found = 1;
    439 				break;
    440 			}
    441 		}
    442 	}
    443 	fclose(fp);
    444 
    445 	/*
    446 	 * If we got no result and were looking for a default
    447 	 * value, try hard coded defaults.
    448 	 */
    449 
    450 	if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
    451 	    (cp = pw_default(option)) != NULL)
    452 		strlcpy(data, p, max);
    453 	else
    454 		strlcpy(data, result, max);
    455 }
    456