Home | History | Annotate | Line # | Download | only in libutil
passwd.c revision 1.15.2.2
      1  1.15.2.2        he /*	$NetBSD: passwd.c,v 1.15.2.2 2000/10/04 14:11:02 he Exp $	*/
      2       1.8  christos 
      3       1.1       jtc /*
      4       1.1       jtc  * Copyright (c) 1987, 1993, 1994, 1995
      5       1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      6       1.1       jtc  *
      7       1.1       jtc  * Redistribution and use in source and binary forms, with or without
      8       1.1       jtc  * modification, are permitted provided that the following conditions
      9       1.1       jtc  * are met:
     10       1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     11       1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     12       1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       jtc  *    documentation and/or other materials provided with the distribution.
     15       1.1       jtc  * 3. All advertising materials mentioning features or use of this software
     16       1.1       jtc  *    must display the following acknowledgement:
     17       1.1       jtc  *	This product includes software developed by the University of
     18       1.1       jtc  *	California, Berkeley and its contributors.
     19       1.1       jtc  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       jtc  *    may be used to endorse or promote products derived from this software
     21       1.1       jtc  *    without specific prior written permission.
     22       1.1       jtc  *
     23       1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       jtc  * SUCH DAMAGE.
     34       1.1       jtc  */
     35       1.1       jtc 
     36       1.8  christos #include <sys/cdefs.h>
     37       1.1       jtc #if defined(LIBC_SCCS) && !defined(lint)
     38  1.15.2.2        he __RCSID("$NetBSD: passwd.c,v 1.15.2.2 2000/10/04 14:11:02 he Exp $");
     39       1.1       jtc #endif /* LIBC_SCCS and not lint */
     40       1.1       jtc 
     41       1.1       jtc #include <sys/types.h>
     42       1.1       jtc #include <sys/stat.h>
     43       1.1       jtc #include <sys/time.h>
     44       1.1       jtc #include <sys/resource.h>
     45       1.1       jtc #include <sys/wait.h>
     46       1.1       jtc 
     47       1.3   thorpej #include <ctype.h>
     48       1.5   mycroft #include <err.h>
     49      1.12     lukem #include <errno.h>
     50       1.1       jtc #include <fcntl.h>
     51      1.12     lukem #include <limits.h>
     52      1.12     lukem #include <paths.h>
     53      1.12     lukem #include <pwd.h>
     54      1.12     lukem #include <signal.h>
     55      1.12     lukem #include <stdio.h>
     56       1.1       jtc #include <stdlib.h>
     57       1.1       jtc #include <string.h>
     58      1.12     lukem #include <unistd.h>
     59       1.1       jtc #include <util.h>
     60       1.1       jtc 
     61       1.1       jtc static void	pw_cont __P((int sig));
     62      1.10      phil static int	pw_equal __P((char *buf, struct passwd *old_pw));
     63       1.1       jtc 
     64       1.1       jtc int
     65       1.1       jtc pw_lock(retries)
     66       1.1       jtc 	int retries;
     67       1.1       jtc {
     68       1.1       jtc 	int i, fd;
     69       1.1       jtc 	mode_t old_mode;
     70      1.14  christos 	int oerrno;
     71       1.1       jtc 
     72       1.1       jtc 	/* Acquire the lock file. */
     73       1.1       jtc 	old_mode = umask(0);
     74       1.1       jtc 	fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
     75       1.1       jtc 	for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
     76       1.1       jtc 		sleep(1);
     77       1.1       jtc 		fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
     78       1.1       jtc 			  0600);
     79       1.1       jtc 	}
     80      1.14  christos 	oerrno = errno;
     81      1.14  christos 	(void)umask(old_mode);
     82      1.14  christos 	errno = oerrno;
     83       1.1       jtc 	return(fd);
     84       1.1       jtc }
     85       1.1       jtc 
     86       1.1       jtc int
     87       1.1       jtc pw_mkdb()
     88       1.1       jtc {
     89       1.1       jtc 	int pstat;
     90       1.1       jtc 	pid_t pid;
     91       1.1       jtc 
     92       1.1       jtc 	pid = vfork();
     93  1.15.2.1        he 	if (pid == -1)
     94  1.15.2.1        he 		return (-1);
     95  1.15.2.1        he 
     96       1.1       jtc 	if (pid == 0) {
     97       1.1       jtc 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
     98       1.1       jtc 		      _PATH_MASTERPASSWD_LOCK, NULL);
     99      1.11   thorpej 		_exit(1);
    100       1.1       jtc 	}
    101       1.1       jtc 	pid = waitpid(pid, &pstat, 0);
    102       1.2   ghudson 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
    103       1.1       jtc 		return(-1);
    104       1.1       jtc 	return(0);
    105       1.1       jtc }
    106       1.1       jtc 
    107       1.1       jtc int
    108       1.1       jtc pw_abort()
    109       1.1       jtc {
    110       1.1       jtc 	return(unlink(_PATH_MASTERPASSWD_LOCK));
    111       1.1       jtc }
    112       1.1       jtc 
    113       1.1       jtc /* Everything below this point is intended for the convenience of programs
    114       1.1       jtc  * which allow a user to interactively edit the passwd file.  Errors in the
    115       1.1       jtc  * routines below will cause the process to abort. */
    116       1.1       jtc 
    117       1.1       jtc static pid_t editpid = -1;
    118       1.1       jtc 
    119       1.1       jtc static void
    120       1.1       jtc pw_cont(sig)
    121       1.1       jtc 	int sig;
    122       1.1       jtc {
    123       1.1       jtc 
    124       1.1       jtc 	if (editpid != -1)
    125       1.1       jtc 		kill(editpid, sig);
    126       1.1       jtc }
    127       1.1       jtc 
    128       1.1       jtc void
    129       1.1       jtc pw_init()
    130       1.1       jtc {
    131       1.1       jtc 	struct rlimit rlim;
    132       1.1       jtc 
    133       1.1       jtc 	/* Unlimited resource limits. */
    134       1.1       jtc 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
    135       1.1       jtc 	(void)setrlimit(RLIMIT_CPU, &rlim);
    136       1.1       jtc 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
    137       1.1       jtc 	(void)setrlimit(RLIMIT_STACK, &rlim);
    138       1.1       jtc 	(void)setrlimit(RLIMIT_DATA, &rlim);
    139       1.1       jtc 	(void)setrlimit(RLIMIT_RSS, &rlim);
    140       1.1       jtc 
    141       1.1       jtc 	/* Don't drop core (not really necessary, but GP's). */
    142       1.1       jtc 	rlim.rlim_cur = rlim.rlim_max = 0;
    143       1.1       jtc 	(void)setrlimit(RLIMIT_CORE, &rlim);
    144       1.1       jtc 
    145       1.1       jtc 	/* Turn off signals. */
    146       1.1       jtc 	(void)signal(SIGALRM, SIG_IGN);
    147       1.1       jtc 	(void)signal(SIGHUP, SIG_IGN);
    148       1.1       jtc 	(void)signal(SIGINT, SIG_IGN);
    149       1.1       jtc 	(void)signal(SIGPIPE, SIG_IGN);
    150       1.1       jtc 	(void)signal(SIGQUIT, SIG_IGN);
    151       1.1       jtc 	(void)signal(SIGTERM, SIG_IGN);
    152       1.1       jtc 	(void)signal(SIGCONT, pw_cont);
    153       1.1       jtc }
    154       1.1       jtc 
    155       1.1       jtc void
    156       1.1       jtc pw_edit(notsetuid, filename)
    157       1.1       jtc 	int notsetuid;
    158       1.1       jtc 	const char *filename;
    159       1.1       jtc {
    160  1.15.2.1        he 	int pstat;
    161       1.1       jtc 	char *p, *editor;
    162  1.15.2.1        he 	char *argp[] = { "sh", "-c", NULL, NULL };
    163  1.15.2.1        he 
    164       1.8  christos #ifdef __GNUC__
    165       1.8  christos 	(void) &editor;
    166       1.8  christos #endif
    167       1.1       jtc 
    168       1.3   thorpej 	if (filename == NULL)
    169       1.1       jtc 		filename = _PATH_MASTERPASSWD_LOCK;
    170       1.3   thorpej 
    171  1.15.2.1        he 	if ((editor = getenv("EDITOR")) == NULL)
    172  1.15.2.1        he 		editor = _PATH_VI;
    173       1.3   thorpej 
    174  1.15.2.1        he 	p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
    175  1.15.2.1        he 	if (p == NULL)
    176  1.15.2.1        he 		return;
    177  1.15.2.1        he 
    178  1.15.2.1        he 	sprintf(p, "%s %s", editor, filename);
    179  1.15.2.1        he 	argp[2] = p;
    180  1.15.2.1        he 
    181  1.15.2.1        he 	switch(editpid = vfork()) {
    182  1.15.2.1        he 	case -1:
    183  1.15.2.1        he 		free(p);
    184  1.15.2.1        he 		return;
    185  1.15.2.1        he 	case 0:
    186       1.1       jtc 		if (notsetuid) {
    187       1.1       jtc 			setgid(getgid());
    188       1.1       jtc 			setuid(getuid());
    189       1.1       jtc 		}
    190  1.15.2.1        he 		execvp(_PATH_BSHELL, argp);
    191       1.1       jtc 		_exit(1);
    192       1.1       jtc 	}
    193  1.15.2.1        he 
    194  1.15.2.1        he 	free(p);
    195  1.15.2.1        he 
    196       1.1       jtc 	for (;;) {
    197       1.1       jtc 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
    198       1.1       jtc 		if (editpid == -1)
    199       1.1       jtc 			pw_error(editor, 1, 1);
    200       1.1       jtc 		else if (WIFSTOPPED(pstat))
    201       1.1       jtc 			raise(WSTOPSIG(pstat));
    202       1.1       jtc 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
    203       1.1       jtc 			break;
    204       1.1       jtc 		else
    205       1.1       jtc 			pw_error(editor, 1, 1);
    206       1.1       jtc 	}
    207       1.1       jtc 	editpid = -1;
    208       1.1       jtc }
    209       1.1       jtc 
    210       1.1       jtc void
    211       1.1       jtc pw_prompt()
    212       1.1       jtc {
    213       1.1       jtc 	int c;
    214       1.1       jtc 
    215       1.1       jtc 	(void)printf("re-edit the password file? [y]: ");
    216       1.1       jtc 	(void)fflush(stdout);
    217       1.1       jtc 	c = getchar();
    218       1.1       jtc 	if (c != EOF && c != '\n')
    219       1.1       jtc 		while (getchar() != '\n');
    220       1.1       jtc 	if (c == 'n')
    221       1.1       jtc 		pw_error(NULL, 0, 0);
    222       1.1       jtc }
    223       1.1       jtc 
    224      1.10      phil /* for use in pw_copy(). Compare a pw entry to a pw struct. */
    225      1.10      phil static int
    226      1.10      phil pw_equal (buf, pw)
    227      1.10      phil 	char *buf;
    228      1.10      phil 	struct passwd *pw;
    229      1.10      phil {
    230      1.10      phil 	struct passwd buf_pw;
    231      1.10      phil 	int len = strlen (buf);
    232      1.10      phil 	if (buf[len-1] == '\n')
    233      1.10      phil 		buf[len-1] = '\0';
    234      1.10      phil 	if (!pw_scan(buf, &buf_pw, NULL))
    235      1.10      phil 		return 0;
    236      1.10      phil 	return !strcmp(pw->pw_name, buf_pw.pw_name)
    237      1.10      phil 		&& pw->pw_uid == buf_pw.pw_uid
    238      1.10      phil 		&& pw->pw_gid == buf_pw.pw_gid
    239      1.10      phil 		&& !strcmp(pw->pw_class, buf_pw.pw_class)
    240      1.10      phil 		&& (long)pw->pw_change == (long)buf_pw.pw_change
    241      1.10      phil 		&& (long)pw->pw_expire == (long)buf_pw.pw_expire
    242      1.10      phil 		&& !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
    243      1.10      phil 		&& !strcmp(pw->pw_dir, buf_pw.pw_dir)
    244      1.10      phil 		&& !strcmp(pw->pw_shell, buf_pw.pw_shell);
    245      1.10      phil }
    246      1.10      phil 
    247       1.1       jtc void
    248      1.10      phil pw_copy(ffd, tfd, pw, old_pw)
    249       1.1       jtc 	int ffd, tfd;
    250      1.10      phil 	struct passwd *pw, *old_pw;
    251       1.1       jtc {
    252       1.1       jtc 	FILE *from, *to;
    253       1.1       jtc 	int done;
    254       1.1       jtc 	char *p, buf[8192];
    255       1.1       jtc 
    256       1.1       jtc 	if (!(from = fdopen(ffd, "r")))
    257       1.1       jtc 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    258       1.1       jtc 	if (!(to = fdopen(tfd, "w")))
    259       1.1       jtc 		pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
    260       1.1       jtc 
    261       1.1       jtc 	for (done = 0; fgets(buf, sizeof(buf), from);) {
    262       1.1       jtc 		if (!strchr(buf, '\n')) {
    263       1.1       jtc 			warnx("%s: line too long", _PATH_MASTERPASSWD);
    264       1.1       jtc 			pw_error(NULL, 0, 1);
    265       1.1       jtc 		}
    266       1.1       jtc 		if (done) {
    267       1.1       jtc 			(void)fprintf(to, "%s", buf);
    268       1.1       jtc 			if (ferror(to))
    269       1.1       jtc 				goto err;
    270       1.1       jtc 			continue;
    271       1.1       jtc 		}
    272       1.1       jtc 		if (!(p = strchr(buf, ':'))) {
    273       1.1       jtc 			warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
    274       1.1       jtc 			pw_error(NULL, 0, 1);
    275       1.1       jtc 		}
    276       1.1       jtc 		*p = '\0';
    277       1.1       jtc 		if (strcmp(buf, pw->pw_name)) {
    278       1.1       jtc 			*p = ':';
    279       1.1       jtc 			(void)fprintf(to, "%s", buf);
    280       1.1       jtc 			if (ferror(to))
    281       1.1       jtc 				goto err;
    282       1.1       jtc 			continue;
    283       1.1       jtc 		}
    284      1.10      phil 		*p = ':';
    285      1.10      phil 		if (old_pw && !pw_equal(buf, old_pw)) {
    286      1.10      phil 			warnx("%s: entry inconsistent",
    287      1.10      phil 			      _PATH_MASTERPASSWD);
    288      1.10      phil 			pw_error(NULL, 0, 1);
    289      1.10      phil 		}
    290       1.1       jtc 		(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    291       1.1       jtc 		    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    292       1.9  christos 		    pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
    293       1.9  christos 		    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
    294       1.1       jtc 		done = 1;
    295       1.1       jtc 		if (ferror(to))
    296       1.1       jtc 			goto err;
    297       1.1       jtc 	}
    298      1.10      phil 	/* Only append a new entry if real uid is root! */
    299      1.13   thorpej 	if (!done) {
    300      1.10      phil 		if (getuid() == 0)
    301      1.10      phil 			(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    302      1.10      phil 			    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    303      1.10      phil 			    pw->pw_class, (long)pw->pw_change,
    304      1.10      phil 			    (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
    305      1.10      phil 			    pw->pw_shell);
    306      1.10      phil 		else
    307      1.10      phil 			warnx("%s: changes not made, no such entry",
    308      1.13   thorpej 			    _PATH_MASTERPASSWD);
    309      1.13   thorpej 	}
    310       1.1       jtc 
    311       1.1       jtc 	if (ferror(to))
    312       1.1       jtc err:		pw_error(NULL, 1, 1);
    313       1.1       jtc 	(void)fclose(to);
    314       1.1       jtc }
    315       1.1       jtc 
    316       1.1       jtc void
    317       1.1       jtc pw_error(name, err, eval)
    318       1.1       jtc 	const char *name;
    319       1.1       jtc 	int err, eval;
    320       1.1       jtc {
    321       1.1       jtc 	if (err)
    322  1.15.2.2        he 		warn("%s", name);
    323       1.1       jtc 
    324       1.1       jtc 	warnx("%s: unchanged", _PATH_MASTERPASSWD);
    325       1.1       jtc 	pw_abort();
    326       1.1       jtc 	exit(eval);
    327       1.1       jtc }
    328