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