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