Home | History | Annotate | Line # | Download | only in libutil
passwd.c revision 1.35
      1 /*	$NetBSD: passwd.c,v 1.35 2003/08/07 16:44:59 agc 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. 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/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 __RCSID("$NetBSD: passwd.c,v 1.35 2003/08/07 16:44:59 agc Exp $");
     35 #endif /* LIBC_SCCS and not lint */
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/stat.h>
     40 #include <sys/time.h>
     41 #include <sys/resource.h>
     42 #include <sys/wait.h>
     43 
     44 #include <assert.h>
     45 #include <ctype.h>
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <fcntl.h>
     49 #include <limits.h>
     50 #include <paths.h>
     51 #include <pwd.h>
     52 #include <signal.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 #include <util.h>
     58 
     59 static const char      *pw_filename(const char *filename);
     60 static void		pw_cont(int sig);
     61 static int		pw_equal(char *buf, struct passwd *old_pw);
     62 static const char      *pw_default(const char *option);
     63 static int		read_line(FILE *fp, char *line, int max);
     64 static void		trim_whitespace(char *line);
     65 
     66 static	char	pw_prefix[MAXPATHLEN];
     67 
     68 const char *
     69 pw_getprefix(void)
     70 {
     71 
     72 	return(pw_prefix);
     73 }
     74 
     75 int
     76 pw_setprefix(const char *new_prefix)
     77 {
     78 	size_t length;
     79 
     80 	_DIAGASSERT(new_prefix != NULL);
     81 
     82 	length = strlen(new_prefix);
     83 	if (length < sizeof(pw_prefix)) {
     84 		(void)strcpy(pw_prefix, new_prefix);
     85 		while (length > 0 && pw_prefix[length - 1] == '/')
     86 			pw_prefix[--length] = '\0';
     87 		return(0);
     88 	}
     89 	errno = ENAMETOOLONG;
     90 	return(-1);
     91 }
     92 
     93 static const char *
     94 pw_filename(const char *filename)
     95 {
     96 	static char newfilename[MAXPATHLEN];
     97 
     98 	_DIAGASSERT(filename != NULL);
     99 
    100 	if (pw_prefix[0] == '\0')
    101 		return filename;
    102 
    103 	if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
    104 		return strcat(strcpy(newfilename, pw_prefix), filename);
    105 
    106 	errno = ENAMETOOLONG;
    107 	return(NULL);
    108 }
    109 
    110 int
    111 pw_lock(int retries)
    112 {
    113 	const char *filename;
    114 	int i, fd;
    115 	mode_t old_mode;
    116 	int oerrno;
    117 
    118 	/* Acquire the lock file. */
    119 	filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
    120 	if (filename == NULL)
    121 		return(-1);
    122 	old_mode = umask(0);
    123 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
    124 	for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
    125 		sleep(1);
    126 		fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
    127 			  0600);
    128 	}
    129 	oerrno = errno;
    130 	(void)umask(old_mode);
    131 	errno = oerrno;
    132 	return(fd);
    133 }
    134 
    135 int
    136 pw_mkdb(username, secureonly)
    137 	const char *username;
    138 	int secureonly;
    139 {
    140 	const char *args[9];
    141 	int pstat, i;
    142 	pid_t pid;
    143 
    144 	pid = vfork();
    145 	if (pid == -1)
    146 		return (-1);
    147 
    148 	if (pid == 0) {
    149 		args[0] = "pwd_mkdb";
    150 		args[1] = "-d";
    151 		args[2] = pw_prefix;
    152 		args[3] = "-p";
    153 		i = 4;
    154 
    155 		if (secureonly)
    156 			args[i++] = "-s";
    157 		if (username != NULL) {
    158 			args[i++] = "-u";
    159 			args[i++] = username;
    160 		}
    161 
    162 		args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK);
    163 		args[i] = NULL;
    164 		execv(_PATH_PWD_MKDB, (char * const *)args);
    165 		_exit(1);
    166 	}
    167 	pid = waitpid(pid, &pstat, 0);
    168 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
    169 		return(-1);
    170 	return(0);
    171 }
    172 
    173 int
    174 pw_abort(void)
    175 {
    176 	const char *filename;
    177 
    178 	filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
    179 	return((filename == NULL) ? -1 : unlink(filename));
    180 }
    181 
    182 /* Everything below this point is intended for the convenience of programs
    183  * which allow a user to interactively edit the passwd file.  Errors in the
    184  * routines below will cause the process to abort. */
    185 
    186 static pid_t editpid = -1;
    187 
    188 static void
    189 pw_cont(int sig)
    190 {
    191 
    192 	if (editpid != -1)
    193 		kill(editpid, sig);
    194 }
    195 
    196 void
    197 pw_init(void)
    198 {
    199 	struct rlimit rlim;
    200 
    201 	/* Unlimited resource limits. */
    202 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
    203 	(void)setrlimit(RLIMIT_CPU, &rlim);
    204 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
    205 	(void)setrlimit(RLIMIT_STACK, &rlim);
    206 	(void)setrlimit(RLIMIT_DATA, &rlim);
    207 	(void)setrlimit(RLIMIT_RSS, &rlim);
    208 
    209 	/* Don't drop core (not really necessary, but GP's). */
    210 	rlim.rlim_cur = rlim.rlim_max = 0;
    211 	(void)setrlimit(RLIMIT_CORE, &rlim);
    212 
    213 	/* Turn off signals. */
    214 	(void)signal(SIGALRM, SIG_IGN);
    215 	(void)signal(SIGHUP, SIG_IGN);
    216 	(void)signal(SIGINT, SIG_IGN);
    217 	(void)signal(SIGPIPE, SIG_IGN);
    218 	(void)signal(SIGQUIT, SIG_IGN);
    219 	(void)signal(SIGTERM, SIG_IGN);
    220 	(void)signal(SIGCONT, pw_cont);
    221 }
    222 
    223 void
    224 pw_edit(int notsetuid, const char *filename)
    225 {
    226 	int pstat;
    227 	char *p, *editor;
    228 	char *argp[] = { "sh", "-c", NULL, NULL };
    229 
    230 #ifdef __GNUC__
    231 	(void) &editor;
    232 #endif
    233 
    234 	if (filename == NULL)
    235 		filename = _PATH_MASTERPASSWD_LOCK;
    236 
    237 	filename = pw_filename(filename);
    238 	if (filename == NULL)
    239 		return;
    240 
    241 	if ((editor = getenv("EDITOR")) == NULL)
    242 		editor = _PATH_VI;
    243 
    244 	p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
    245 	if (p == NULL)
    246 		return;
    247 
    248 	sprintf(p, "%s %s", editor, filename);
    249 	argp[2] = p;
    250 
    251 	switch(editpid = vfork()) {
    252 	case -1:
    253 		free(p);
    254 		return;
    255 	case 0:
    256 		if (notsetuid) {
    257 			setgid(getgid());
    258 			setuid(getuid());
    259 		}
    260 		execvp(_PATH_BSHELL, argp);
    261 		_exit(1);
    262 	}
    263 
    264 	free(p);
    265 
    266 	for (;;) {
    267 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
    268 		if (editpid == -1)
    269 			pw_error(editor, 1, 1);
    270 		else if (WIFSTOPPED(pstat))
    271 			raise(WSTOPSIG(pstat));
    272 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
    273 			break;
    274 		else
    275 			pw_error(editor, 1, 1);
    276 	}
    277 	editpid = -1;
    278 }
    279 
    280 void
    281 pw_prompt(void)
    282 {
    283 	int c;
    284 
    285 	(void)printf("re-edit the password file? [y]: ");
    286 	(void)fflush(stdout);
    287 	c = getchar();
    288 	if (c != EOF && c != '\n')
    289 		while (getchar() != '\n');
    290 	if (c == 'n')
    291 		pw_error(NULL, 0, 0);
    292 }
    293 
    294 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
    295 static int
    296 pw_equal(char *buf, struct passwd *pw)
    297 {
    298 	struct passwd buf_pw;
    299 	int len;
    300 
    301 	_DIAGASSERT(buf != NULL);
    302 	_DIAGASSERT(pw != NULL);
    303 
    304 	len = strlen (buf);
    305 	if (buf[len-1] == '\n')
    306 		buf[len-1] = '\0';
    307 	if (!pw_scan(buf, &buf_pw, NULL))
    308 		return 0;
    309 	return !strcmp(pw->pw_name, buf_pw.pw_name)
    310 		&& pw->pw_uid == buf_pw.pw_uid
    311 		&& pw->pw_gid == buf_pw.pw_gid
    312 		&& !strcmp(pw->pw_class, buf_pw.pw_class)
    313 		&& (long)pw->pw_change == (long)buf_pw.pw_change
    314 		&& (long)pw->pw_expire == (long)buf_pw.pw_expire
    315 		&& !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
    316 		&& !strcmp(pw->pw_dir, buf_pw.pw_dir)
    317 		&& !strcmp(pw->pw_shell, buf_pw.pw_shell);
    318 }
    319 
    320 void
    321 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
    322 {
    323 	const char *filename;
    324 	char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
    325 	FILE *from, *to;
    326 	int done;
    327 
    328 	_DIAGASSERT(pw != NULL);
    329 	/* old_pw may be NULL */
    330 
    331 	if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL)
    332 		pw_error(pw_prefix, 1,1);
    333 	(void)strcpy(mpwd, filename);
    334 	if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL)
    335 		pw_error(pw_prefix, 1,1);
    336 	(void)strcpy(mpwdl, filename);
    337 
    338 	if (!(from = fdopen(ffd, "r")))
    339 		pw_error(mpwd, 1, 1);
    340 	if (!(to = fdopen(tfd, "w")))
    341 		pw_error(mpwdl, 1, 1);
    342 
    343 	for (done = 0; fgets(buf, sizeof(buf), from);) {
    344 		if (!strchr(buf, '\n')) {
    345 			warnx("%s: line too long", mpwd);
    346 			pw_error(NULL, 0, 1);
    347 		}
    348 		if (done) {
    349 			(void)fprintf(to, "%s", buf);
    350 			if (ferror(to))
    351 				goto err;
    352 			continue;
    353 		}
    354 		if (!(p = strchr(buf, ':'))) {
    355 			warnx("%s: corrupted entry", mpwd);
    356 			pw_error(NULL, 0, 1);
    357 		}
    358 		*p = '\0';
    359 		if (strcmp(buf, pw->pw_name)) {
    360 			*p = ':';
    361 			(void)fprintf(to, "%s", buf);
    362 			if (ferror(to))
    363 				goto err;
    364 			continue;
    365 		}
    366 		*p = ':';
    367 		if (old_pw && !pw_equal(buf, old_pw)) {
    368 			warnx("%s: entry inconsistent", mpwd);
    369 			pw_error(NULL, 0, 1);
    370 		}
    371 		(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    372 		    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    373 		    pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
    374 		    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
    375 		done = 1;
    376 		if (ferror(to))
    377 			goto err;
    378 	}
    379 	/* Only append a new entry if real uid is root! */
    380 	if (!done) {
    381 		if (getuid() == 0)
    382 			(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    383 			    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    384 			    pw->pw_class, (long)pw->pw_change,
    385 			    (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
    386 			    pw->pw_shell);
    387 		else
    388 			warnx("%s: changes not made, no such entry", mpwd);
    389 	}
    390 
    391 	if (ferror(to))
    392 err:		pw_error(NULL, 1, 1);
    393 	(void)fclose(to);
    394 }
    395 
    396 void
    397 pw_error(const char *name, int error, int eval)
    398 {
    399 
    400 	if (error) {
    401 		if (name)
    402 			warn("%s", name);
    403 		else
    404 			warn(NULL);
    405 	}
    406 
    407 	warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
    408 	pw_abort();
    409 	exit(eval);
    410 }
    411 
    412 /* Removes head and/or tail spaces. */
    413 static void
    414 trim_whitespace(char *line)
    415 {
    416 	char *p;
    417 
    418 	_DIAGASSERT(line != NULL);
    419 
    420 	/* Remove leading spaces */
    421 	p = line;
    422 	while (isspace((unsigned char) *p))
    423 		p++;
    424 	memmove(line, p, strlen(p) + 1);
    425 
    426 	/* Remove trailing spaces */
    427 	p = line + strlen(line) - 1;
    428 	while (isspace((unsigned char) *p))
    429 		p--;
    430 	*(p + 1) = '\0';
    431 }
    432 
    433 
    434 /* Get one line, remove spaces from front and tail */
    435 static int
    436 read_line(FILE *fp, char *line, int max)
    437 {
    438 	char   *p;
    439 
    440 	_DIAGASSERT(fp != NULL);
    441 	_DIAGASSERT(line != NULL);
    442 
    443 	/* Read one line of config */
    444 	if (fgets(line, max, fp) == NULL)
    445 		return (0);
    446 
    447 	if ((p = strchr(line, '\n')) == NULL) {
    448 		warnx("line too long");
    449 		return (0);
    450 	}
    451 	*p = '\0';
    452 
    453 	/* Remove comments */
    454 	if ((p = strchr(line, '#')) != NULL)
    455 		*p = '\0';
    456 
    457 	trim_whitespace(line);
    458 	return (1);
    459 }
    460 
    461 static const char *
    462 pw_default(const char *option)
    463 {
    464 	static const char *options[][2] = {
    465 		{ "localcipher",	"old" },
    466 		{ "ypcipher",		"old" },
    467 	};
    468 	int i;
    469 
    470 	_DIAGASSERT(option != NULL);
    471 	for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
    472 		if (strcmp(options[i][0], option) == 0)
    473 			return (options[i][1]);
    474 
    475 	return (NULL);
    476 }
    477 
    478 /*
    479  * Retrieve password information from the /etc/passwd.conf file, at the
    480  * moment this is only for choosing the cipher to use.  It could easily be
    481  * used for other authentication methods as well.
    482  */
    483 void
    484 pw_getconf(char *data, size_t max, const char *key, const char *option)
    485 {
    486 	FILE *fp;
    487 	char line[LINE_MAX], *p, *p2;
    488 	static char result[LINE_MAX];
    489 	int got, found;
    490 	const char *cp;
    491 
    492 	_DIAGASSERT(data != NULL);
    493 	_DIAGASSERT(key != NULL);
    494 	_DIAGASSERT(option != NULL);
    495 
    496 	got = 0;
    497 	found = 0;
    498 	result[0] = '\0';
    499 
    500 	if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) {
    501 		if ((cp = pw_default(option)) != NULL)
    502 			strlcpy(data, cp, max);
    503 		else
    504 			data[0] = '\0';
    505 		return;
    506 	}
    507 
    508 	while (!found && (got || read_line(fp, line, LINE_MAX))) {
    509 		got = 0;
    510 
    511 		if (strncmp(key, line, strlen(key)) != 0 ||
    512 		    line[strlen(key)] != ':')
    513 			continue;
    514 
    515 		/* Now we found our specified key */
    516 		while (read_line(fp, line, LINE_MAX)) {
    517 			/* Leaving key field */
    518 			if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
    519 				got = 1;
    520 				break;
    521 			}
    522 			p2 = line;
    523 			if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
    524 				continue;
    525 			trim_whitespace(p);
    526 
    527 			if (!strncmp(p, option, strlen(option))) {
    528 				trim_whitespace(p2);
    529 				strcpy(result, p2);
    530 				found = 1;
    531 				break;
    532 			}
    533 		}
    534 	}
    535 	fclose(fp);
    536 
    537 	/*
    538 	 * If we got no result and were looking for a default
    539 	 * value, try hard coded defaults.
    540 	 */
    541 
    542 	if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
    543 	    (cp = pw_default(option)) != NULL)
    544 		strlcpy(data, cp, max);
    545 	else
    546 		strlcpy(data, result, max);
    547 }
    548