Home | History | Annotate | Line # | Download | only in libutil
passwd.c revision 1.46.2.2
      1  1.46.2.2  christos /*	$NetBSD: passwd.c,v 1.46.2.2 2008/12/28 01:14:32 christos Exp $	*/
      2  1.46.2.2  christos 
      3  1.46.2.2  christos /*
      4  1.46.2.2  christos  * Copyright (c) 1987, 1993, 1994, 1995
      5  1.46.2.2  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.46.2.2  christos  *
      7  1.46.2.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.46.2.2  christos  * modification, are permitted provided that the following conditions
      9  1.46.2.2  christos  * are met:
     10  1.46.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.46.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.46.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.46.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.46.2.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.46.2.2  christos  * 3. Neither the name of the University nor the names of its contributors
     16  1.46.2.2  christos  *    may be used to endorse or promote products derived from this software
     17  1.46.2.2  christos  *    without specific prior written permission.
     18  1.46.2.2  christos  *
     19  1.46.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.46.2.2  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.46.2.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.46.2.2  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.46.2.2  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.46.2.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.46.2.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.46.2.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.46.2.2  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.46.2.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.46.2.2  christos  * SUCH DAMAGE.
     30  1.46.2.2  christos  */
     31  1.46.2.2  christos 
     32  1.46.2.2  christos #include <sys/cdefs.h>
     33  1.46.2.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.46.2.2  christos __RCSID("$NetBSD: passwd.c,v 1.46.2.2 2008/12/28 01:14:32 christos Exp $");
     35  1.46.2.2  christos #endif /* LIBC_SCCS and not lint */
     36  1.46.2.2  christos 
     37  1.46.2.2  christos #include <sys/types.h>
     38  1.46.2.2  christos #include <sys/param.h>
     39  1.46.2.2  christos #include <sys/stat.h>
     40  1.46.2.2  christos #include <sys/time.h>
     41  1.46.2.2  christos #include <sys/resource.h>
     42  1.46.2.2  christos #include <sys/wait.h>
     43  1.46.2.2  christos 
     44  1.46.2.2  christos #include <assert.h>
     45  1.46.2.2  christos #include <ctype.h>
     46  1.46.2.2  christos #include <err.h>
     47  1.46.2.2  christos #include <errno.h>
     48  1.46.2.2  christos #include <fcntl.h>
     49  1.46.2.2  christos #include <limits.h>
     50  1.46.2.2  christos #include <paths.h>
     51  1.46.2.2  christos #include <pwd.h>
     52  1.46.2.2  christos #include <grp.h>
     53  1.46.2.2  christos #include <signal.h>
     54  1.46.2.2  christos #include <stdio.h>
     55  1.46.2.2  christos #include <stdlib.h>
     56  1.46.2.2  christos #include <string.h>
     57  1.46.2.2  christos #include <unistd.h>
     58  1.46.2.2  christos #include <util.h>
     59  1.46.2.2  christos 
     60  1.46.2.2  christos static const char      *pw_filename(const char *filename);
     61  1.46.2.2  christos static void		pw_cont(int sig);
     62  1.46.2.2  christos static const char *	pw_equal(char *buf, struct passwd *old_pw);
     63  1.46.2.2  christos static const char      *pw_default(const char *option);
     64  1.46.2.2  christos static int		read_line(FILE *fp, char *line, int max);
     65  1.46.2.2  christos static void		trim_whitespace(char *line);
     66  1.46.2.2  christos 
     67  1.46.2.2  christos static	char	pw_prefix[MAXPATHLEN];
     68  1.46.2.2  christos 
     69  1.46.2.2  christos const char *
     70  1.46.2.2  christos pw_getprefix(void)
     71  1.46.2.2  christos {
     72  1.46.2.2  christos 
     73  1.46.2.2  christos 	return(pw_prefix);
     74  1.46.2.2  christos }
     75  1.46.2.2  christos 
     76  1.46.2.2  christos int
     77  1.46.2.2  christos pw_setprefix(const char *new_prefix)
     78  1.46.2.2  christos {
     79  1.46.2.2  christos 	size_t length;
     80  1.46.2.2  christos 
     81  1.46.2.2  christos 	_DIAGASSERT(new_prefix != NULL);
     82  1.46.2.2  christos 
     83  1.46.2.2  christos 	length = strlen(new_prefix);
     84  1.46.2.2  christos 	if (length < sizeof(pw_prefix)) {
     85  1.46.2.2  christos 		(void)strcpy(pw_prefix, new_prefix);
     86  1.46.2.2  christos 		while (length > 0 && pw_prefix[length - 1] == '/')
     87  1.46.2.2  christos 			pw_prefix[--length] = '\0';
     88  1.46.2.2  christos 		return(0);
     89  1.46.2.2  christos 	}
     90  1.46.2.2  christos 	errno = ENAMETOOLONG;
     91  1.46.2.2  christos 	return(-1);
     92  1.46.2.2  christos }
     93  1.46.2.2  christos 
     94  1.46.2.2  christos static const char *
     95  1.46.2.2  christos pw_filename(const char *filename)
     96  1.46.2.2  christos {
     97  1.46.2.2  christos 	static char newfilename[MAXPATHLEN];
     98  1.46.2.2  christos 
     99  1.46.2.2  christos 	_DIAGASSERT(filename != NULL);
    100  1.46.2.2  christos 
    101  1.46.2.2  christos 	if (pw_prefix[0] == '\0')
    102  1.46.2.2  christos 		return filename;
    103  1.46.2.2  christos 
    104  1.46.2.2  christos 	if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
    105  1.46.2.2  christos 		return strcat(strcpy(newfilename, pw_prefix), filename);
    106  1.46.2.2  christos 
    107  1.46.2.2  christos 	errno = ENAMETOOLONG;
    108  1.46.2.2  christos 	return(NULL);
    109  1.46.2.2  christos }
    110  1.46.2.2  christos 
    111  1.46.2.2  christos int
    112  1.46.2.2  christos pw_lock(int retries)
    113  1.46.2.2  christos {
    114  1.46.2.2  christos 	const char *filename;
    115  1.46.2.2  christos 	int i, fd;
    116  1.46.2.2  christos 	mode_t old_mode;
    117  1.46.2.2  christos 	int oerrno;
    118  1.46.2.2  christos 
    119  1.46.2.2  christos 	/* Acquire the lock file. */
    120  1.46.2.2  christos 	filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
    121  1.46.2.2  christos 	if (filename == NULL)
    122  1.46.2.2  christos 		return(-1);
    123  1.46.2.2  christos 	old_mode = umask(0);
    124  1.46.2.2  christos 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
    125  1.46.2.2  christos 	for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
    126  1.46.2.2  christos 		sleep(1);
    127  1.46.2.2  christos 		fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
    128  1.46.2.2  christos 			  0600);
    129  1.46.2.2  christos 	}
    130  1.46.2.2  christos 	oerrno = errno;
    131  1.46.2.2  christos 	(void)umask(old_mode);
    132  1.46.2.2  christos 	errno = oerrno;
    133  1.46.2.2  christos 	return(fd);
    134  1.46.2.2  christos }
    135  1.46.2.2  christos 
    136  1.46.2.2  christos int
    137  1.46.2.2  christos pw_mkdb(username, secureonly)
    138  1.46.2.2  christos 	const char *username;
    139  1.46.2.2  christos 	int secureonly;
    140  1.46.2.2  christos {
    141  1.46.2.2  christos 	const char *args[9];
    142  1.46.2.2  christos 	int pstat, i;
    143  1.46.2.2  christos 	pid_t pid;
    144  1.46.2.2  christos 
    145  1.46.2.2  christos 	pid = vfork();
    146  1.46.2.2  christos 	if (pid == -1)
    147  1.46.2.2  christos 		return (-1);
    148  1.46.2.2  christos 
    149  1.46.2.2  christos 	if (pid == 0) {
    150  1.46.2.2  christos 		args[0] = "pwd_mkdb";
    151  1.46.2.2  christos 		args[1] = "-d";
    152  1.46.2.2  christos 		args[2] = pw_prefix;
    153  1.46.2.2  christos 		args[3] = "-p";
    154  1.46.2.2  christos 		i = 4;
    155  1.46.2.2  christos 
    156  1.46.2.2  christos 		if (secureonly)
    157  1.46.2.2  christos 			args[i++] = "-s";
    158  1.46.2.2  christos 		if (username != NULL) {
    159  1.46.2.2  christos 			args[i++] = "-u";
    160  1.46.2.2  christos 			args[i++] = username;
    161  1.46.2.2  christos 		}
    162  1.46.2.2  christos 
    163  1.46.2.2  christos 		args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK);
    164  1.46.2.2  christos 		args[i] = NULL;
    165  1.46.2.2  christos 		execv(_PATH_PWD_MKDB, (char * const *)__UNCONST(args));
    166  1.46.2.2  christos 		_exit(1);
    167  1.46.2.2  christos 	}
    168  1.46.2.2  christos 	pid = waitpid(pid, &pstat, 0);
    169  1.46.2.2  christos 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
    170  1.46.2.2  christos 		return(-1);
    171  1.46.2.2  christos 	return(0);
    172  1.46.2.2  christos }
    173  1.46.2.2  christos 
    174  1.46.2.2  christos int
    175  1.46.2.2  christos pw_abort(void)
    176  1.46.2.2  christos {
    177  1.46.2.2  christos 	const char *filename;
    178  1.46.2.2  christos 
    179  1.46.2.2  christos 	filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
    180  1.46.2.2  christos 	return((filename == NULL) ? -1 : unlink(filename));
    181  1.46.2.2  christos }
    182  1.46.2.2  christos 
    183  1.46.2.2  christos /* Everything below this point is intended for the convenience of programs
    184  1.46.2.2  christos  * which allow a user to interactively edit the passwd file.  Errors in the
    185  1.46.2.2  christos  * routines below will cause the process to abort. */
    186  1.46.2.2  christos 
    187  1.46.2.2  christos static pid_t editpid = -1;
    188  1.46.2.2  christos 
    189  1.46.2.2  christos static void
    190  1.46.2.2  christos pw_cont(int sig)
    191  1.46.2.2  christos {
    192  1.46.2.2  christos 
    193  1.46.2.2  christos 	if (editpid != -1)
    194  1.46.2.2  christos 		kill(editpid, sig);
    195  1.46.2.2  christos }
    196  1.46.2.2  christos 
    197  1.46.2.2  christos void
    198  1.46.2.2  christos pw_init(void)
    199  1.46.2.2  christos {
    200  1.46.2.2  christos 	struct rlimit rlim;
    201  1.46.2.2  christos 
    202  1.46.2.2  christos 	/* Unlimited resource limits. */
    203  1.46.2.2  christos 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
    204  1.46.2.2  christos 	(void)setrlimit(RLIMIT_CPU, &rlim);
    205  1.46.2.2  christos 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
    206  1.46.2.2  christos 	(void)setrlimit(RLIMIT_STACK, &rlim);
    207  1.46.2.2  christos 	(void)setrlimit(RLIMIT_DATA, &rlim);
    208  1.46.2.2  christos 	(void)setrlimit(RLIMIT_RSS, &rlim);
    209  1.46.2.2  christos 
    210  1.46.2.2  christos 	/* Don't drop core (not really necessary, but GP's). */
    211  1.46.2.2  christos 	rlim.rlim_cur = rlim.rlim_max = 0;
    212  1.46.2.2  christos 	(void)setrlimit(RLIMIT_CORE, &rlim);
    213  1.46.2.2  christos 
    214  1.46.2.2  christos 	/* Turn off signals. */
    215  1.46.2.2  christos 	(void)signal(SIGALRM, SIG_IGN);
    216  1.46.2.2  christos 	(void)signal(SIGHUP, SIG_IGN);
    217  1.46.2.2  christos 	(void)signal(SIGINT, SIG_IGN);
    218  1.46.2.2  christos 	(void)signal(SIGPIPE, SIG_IGN);
    219  1.46.2.2  christos 	(void)signal(SIGQUIT, SIG_IGN);
    220  1.46.2.2  christos 	(void)signal(SIGTERM, SIG_IGN);
    221  1.46.2.2  christos 	(void)signal(SIGCONT, pw_cont);
    222  1.46.2.2  christos }
    223  1.46.2.2  christos 
    224  1.46.2.2  christos void
    225  1.46.2.2  christos pw_edit(int notsetuid, const char *filename)
    226  1.46.2.2  christos {
    227  1.46.2.2  christos 	int pstat;
    228  1.46.2.2  christos 	char *p;
    229  1.46.2.2  christos 	const char * volatile editor;
    230  1.46.2.2  christos 	const char *argp[] = { "sh", "-c", NULL, NULL };
    231  1.46.2.2  christos 
    232  1.46.2.2  christos 	if (filename == NULL)
    233  1.46.2.2  christos 		filename = _PATH_MASTERPASSWD_LOCK;
    234  1.46.2.2  christos 
    235  1.46.2.2  christos 	filename = pw_filename(filename);
    236  1.46.2.2  christos 	if (filename == NULL)
    237  1.46.2.2  christos 		return;
    238  1.46.2.2  christos 
    239  1.46.2.2  christos 	if ((editor = getenv("EDITOR")) == NULL)
    240  1.46.2.2  christos 		editor = _PATH_VI;
    241  1.46.2.2  christos 
    242  1.46.2.2  christos 	p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
    243  1.46.2.2  christos 	if (p == NULL)
    244  1.46.2.2  christos 		return;
    245  1.46.2.2  christos 
    246  1.46.2.2  christos 	sprintf(p, "%s %s", editor, filename);
    247  1.46.2.2  christos 	argp[2] = p;
    248  1.46.2.2  christos 
    249  1.46.2.2  christos 	switch(editpid = vfork()) {
    250  1.46.2.2  christos 	case -1:
    251  1.46.2.2  christos 		free(p);
    252  1.46.2.2  christos 		return;
    253  1.46.2.2  christos 	case 0:
    254  1.46.2.2  christos 		if (notsetuid) {
    255  1.46.2.2  christos 			setgid(getgid());
    256  1.46.2.2  christos 			setuid(getuid());
    257  1.46.2.2  christos 		}
    258  1.46.2.2  christos 		execvp(_PATH_BSHELL, (char *const *)__UNCONST(argp));
    259  1.46.2.2  christos 		_exit(1);
    260  1.46.2.2  christos 	}
    261  1.46.2.2  christos 
    262  1.46.2.2  christos 	free(p);
    263  1.46.2.2  christos 
    264  1.46.2.2  christos 	for (;;) {
    265  1.46.2.2  christos 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
    266  1.46.2.2  christos 		if (editpid == -1)
    267  1.46.2.2  christos 			pw_error(editor, 1, 1);
    268  1.46.2.2  christos 		else if (WIFSTOPPED(pstat))
    269  1.46.2.2  christos 			raise(WSTOPSIG(pstat));
    270  1.46.2.2  christos 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
    271  1.46.2.2  christos 			break;
    272  1.46.2.2  christos 		else
    273  1.46.2.2  christos 			pw_error(editor, 1, 1);
    274  1.46.2.2  christos 	}
    275  1.46.2.2  christos 	editpid = -1;
    276  1.46.2.2  christos }
    277  1.46.2.2  christos 
    278  1.46.2.2  christos void
    279  1.46.2.2  christos pw_prompt(void)
    280  1.46.2.2  christos {
    281  1.46.2.2  christos 	int c;
    282  1.46.2.2  christos 
    283  1.46.2.2  christos 	(void)printf("re-edit the password file? [y]: ");
    284  1.46.2.2  christos 	(void)fflush(stdout);
    285  1.46.2.2  christos 	c = getchar();
    286  1.46.2.2  christos 	if (c != EOF && c != '\n')
    287  1.46.2.2  christos 		while (getchar() != '\n');
    288  1.46.2.2  christos 	if (c == 'n')
    289  1.46.2.2  christos 		pw_error(NULL, 0, 0);
    290  1.46.2.2  christos }
    291  1.46.2.2  christos 
    292  1.46.2.2  christos /* for use in pw_copy(). Compare a pw entry to a pw struct. */
    293  1.46.2.2  christos /* returns a character string labelling the miscompared field or 0 */
    294  1.46.2.2  christos static const char *
    295  1.46.2.2  christos pw_equal(char *buf, struct passwd *pw)
    296  1.46.2.2  christos {
    297  1.46.2.2  christos 	struct passwd buf_pw;
    298  1.46.2.2  christos 	size_t len;
    299  1.46.2.2  christos 
    300  1.46.2.2  christos 	_DIAGASSERT(buf != NULL);
    301  1.46.2.2  christos 	_DIAGASSERT(pw != NULL);
    302  1.46.2.2  christos 
    303  1.46.2.2  christos 	len = strlen (buf);
    304  1.46.2.2  christos 	if (buf[len-1] == '\n')
    305  1.46.2.2  christos 		buf[len-1] = '\0';
    306  1.46.2.2  christos 	if (!pw_scan(buf, &buf_pw, NULL))
    307  1.46.2.2  christos 		return "corrupt line";
    308  1.46.2.2  christos 	if (strcmp(pw->pw_name, buf_pw.pw_name) != 0)
    309  1.46.2.2  christos 		return "name";
    310  1.46.2.2  christos 	if (pw->pw_uid != buf_pw.pw_uid)
    311  1.46.2.2  christos 		return "uid";
    312  1.46.2.2  christos 	if (pw->pw_gid != buf_pw.pw_gid)
    313  1.46.2.2  christos 		return "gid";
    314  1.46.2.2  christos 	if (strcmp( pw->pw_class, buf_pw.pw_class) != 0)
    315  1.46.2.2  christos 		return "class";
    316  1.46.2.2  christos 	if (pw->pw_change != buf_pw.pw_change)
    317  1.46.2.2  christos 		return "change";
    318  1.46.2.2  christos 	if (pw->pw_expire != buf_pw.pw_expire)
    319  1.46.2.2  christos 		return "expire";
    320  1.46.2.2  christos 	if (strcmp( pw->pw_gecos, buf_pw.pw_gecos) != 0)
    321  1.46.2.2  christos 		return "gecos";
    322  1.46.2.2  christos 	if (strcmp( pw->pw_dir, buf_pw.pw_dir) != 0)
    323  1.46.2.2  christos 		return "dir";
    324  1.46.2.2  christos 	if (strcmp( pw->pw_shell, buf_pw.pw_shell) != 0)
    325  1.46.2.2  christos 		return "shell";
    326  1.46.2.2  christos 	return (char *)0;
    327  1.46.2.2  christos }
    328  1.46.2.2  christos 
    329  1.46.2.2  christos void
    330  1.46.2.2  christos pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
    331  1.46.2.2  christos {
    332  1.46.2.2  christos 	char errbuf[200];
    333  1.46.2.2  christos 	int rv;
    334  1.46.2.2  christos 
    335  1.46.2.2  christos 	rv = pw_copyx(ffd, tfd, pw, old_pw, errbuf, sizeof(errbuf));
    336  1.46.2.2  christos 	if (rv == 0) {
    337  1.46.2.2  christos 		warnx("%s", errbuf);
    338  1.46.2.2  christos 		pw_error(NULL, 0, 1);
    339  1.46.2.2  christos 	}
    340  1.46.2.2  christos }
    341  1.46.2.2  christos 
    342  1.46.2.2  christos static void
    343  1.46.2.2  christos pw_print(FILE *to, const struct passwd *pw)
    344  1.46.2.2  christos {
    345  1.46.2.2  christos 	(void)fprintf(to, "%s:%s:%d:%d:%s:%lld:%lld:%s:%s:%s\n",
    346  1.46.2.2  christos 	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    347  1.46.2.2  christos 	    pw->pw_class, (long long)pw->pw_change,
    348  1.46.2.2  christos 	    (long long)pw->pw_expire,
    349  1.46.2.2  christos 	    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
    350  1.46.2.2  christos }
    351  1.46.2.2  christos 
    352  1.46.2.2  christos int
    353  1.46.2.2  christos pw_copyx(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw,
    354  1.46.2.2  christos     char *errbuf, size_t errbufsz)
    355  1.46.2.2  christos {
    356  1.46.2.2  christos 	const char *filename;
    357  1.46.2.2  christos 	char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
    358  1.46.2.2  christos 	FILE *from, *to;
    359  1.46.2.2  christos 	int done;
    360  1.46.2.2  christos 
    361  1.46.2.2  christos 	_DIAGASSERT(pw != NULL);
    362  1.46.2.2  christos 	_DIAGASSERT(errbuf != NULL);
    363  1.46.2.2  christos 	/* old_pw may be NULL */
    364  1.46.2.2  christos 
    365  1.46.2.2  christos 	if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL) {
    366  1.46.2.2  christos 		snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
    367  1.46.2.2  christos 		    strerror(errno));
    368  1.46.2.2  christos 		return (0);
    369  1.46.2.2  christos 	}
    370  1.46.2.2  christos 	(void)strcpy(mpwd, filename);
    371  1.46.2.2  christos 	if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL) {
    372  1.46.2.2  christos 		snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
    373  1.46.2.2  christos 		    strerror(errno));
    374  1.46.2.2  christos 		return (0);
    375  1.46.2.2  christos 	}
    376  1.46.2.2  christos 	(void)strcpy(mpwdl, filename);
    377  1.46.2.2  christos 
    378  1.46.2.2  christos 	if (!(from = fdopen(ffd, "r"))) {
    379  1.46.2.2  christos 		snprintf(errbuf, errbufsz, "%s: %s", mpwd, strerror(errno));
    380  1.46.2.2  christos 		return (0);
    381  1.46.2.2  christos 	}
    382  1.46.2.2  christos 	if (!(to = fdopen(tfd, "w"))) {
    383  1.46.2.2  christos 		snprintf(errbuf, errbufsz, "%s: %s", mpwdl, strerror(errno));
    384  1.46.2.2  christos 		(void)fclose(from);
    385  1.46.2.2  christos 		return (0);
    386  1.46.2.2  christos 	}
    387  1.46.2.2  christos 
    388  1.46.2.2  christos 	for (done = 0; fgets(buf, (int)sizeof(buf), from);) {
    389  1.46.2.2  christos 		const char *neq;
    390  1.46.2.2  christos 		if (!strchr(buf, '\n')) {
    391  1.46.2.2  christos 			snprintf(errbuf, errbufsz, "%s: line too long", mpwd);
    392  1.46.2.2  christos 			(void)fclose(from);
    393  1.46.2.2  christos 			(void)fclose(to);
    394  1.46.2.2  christos 			return (0);
    395  1.46.2.2  christos 		}
    396  1.46.2.2  christos 		if (done) {
    397  1.46.2.2  christos 			(void)fprintf(to, "%s", buf);
    398  1.46.2.2  christos 			if (ferror(to)) {
    399  1.46.2.2  christos 				snprintf(errbuf, errbufsz, "%s",
    400  1.46.2.2  christos 				    strerror(errno));
    401  1.46.2.2  christos 				(void)fclose(from);
    402  1.46.2.2  christos 				(void)fclose(to);
    403  1.46.2.2  christos 				return (0);
    404  1.46.2.2  christos 			}
    405  1.46.2.2  christos 			continue;
    406  1.46.2.2  christos 		}
    407  1.46.2.2  christos 		if (!(p = strchr(buf, ':'))) {
    408  1.46.2.2  christos 			snprintf(errbuf, errbufsz, "%s: corrupted entry", mpwd);
    409  1.46.2.2  christos 			(void)fclose(from);
    410  1.46.2.2  christos 			(void)fclose(to);
    411  1.46.2.2  christos 			return (0);
    412  1.46.2.2  christos 		}
    413  1.46.2.2  christos 		*p = '\0';
    414  1.46.2.2  christos 		if (strcmp(buf, pw->pw_name)) {
    415  1.46.2.2  christos 			*p = ':';
    416  1.46.2.2  christos 			(void)fprintf(to, "%s", buf);
    417  1.46.2.2  christos 			if (ferror(to)) {
    418  1.46.2.2  christos 				snprintf(errbuf, errbufsz, "%s",
    419  1.46.2.2  christos 				    strerror(errno));
    420  1.46.2.2  christos 				(void)fclose(from);
    421  1.46.2.2  christos 				(void)fclose(to);
    422  1.46.2.2  christos 				return (0);
    423  1.46.2.2  christos 			}
    424  1.46.2.2  christos 			continue;
    425  1.46.2.2  christos 		}
    426  1.46.2.2  christos 		*p = ':';
    427  1.46.2.2  christos 		if (old_pw && (neq = pw_equal(buf, old_pw)) != NULL) {
    428  1.46.2.2  christos 			if (strcmp(neq, "corrupt line") == 0)
    429  1.46.2.2  christos 				(void)snprintf(errbuf, errbufsz,
    430  1.46.2.2  christos 				    "%s: entry %s corrupted", mpwd,
    431  1.46.2.2  christos 				    pw->pw_name);
    432  1.46.2.2  christos 			else
    433  1.46.2.2  christos 				(void)snprintf(errbuf, errbufsz,
    434  1.46.2.2  christos 				    "%s: entry %s inconsistent %s",
    435  1.46.2.2  christos 				    mpwd, pw->pw_name, neq);
    436  1.46.2.2  christos 			(void)fclose(from);
    437  1.46.2.2  christos 			(void)fclose(to);
    438  1.46.2.2  christos 			return (0);
    439  1.46.2.2  christos 		}
    440  1.46.2.2  christos 		pw_print(to, pw);
    441  1.46.2.2  christos 		done = 1;
    442  1.46.2.2  christos 		if (ferror(to)) {
    443  1.46.2.2  christos 			snprintf(errbuf, errbufsz, "%s", strerror(errno));
    444  1.46.2.2  christos 			(void)fclose(from);
    445  1.46.2.2  christos 			(void)fclose(to);
    446  1.46.2.2  christos 			return (0);
    447  1.46.2.2  christos 		}
    448  1.46.2.2  christos 	}
    449  1.46.2.2  christos 	/* Only append a new entry if real uid is root! */
    450  1.46.2.2  christos 	if (!done) {
    451  1.46.2.2  christos 		if (getuid() == 0) {
    452  1.46.2.2  christos 			pw_print(to, pw);
    453  1.46.2.2  christos 			done = 1;
    454  1.46.2.2  christos 		} else {
    455  1.46.2.2  christos 			snprintf(errbuf, errbufsz,
    456  1.46.2.2  christos 			    "%s: changes not made, no such entry", mpwd);
    457  1.46.2.2  christos 		}
    458  1.46.2.2  christos 	}
    459  1.46.2.2  christos 
    460  1.46.2.2  christos 	if (ferror(to)) {
    461  1.46.2.2  christos 		snprintf(errbuf, errbufsz, "%s", strerror(errno));
    462  1.46.2.2  christos 		(void)fclose(from);
    463  1.46.2.2  christos 		(void)fclose(to);
    464  1.46.2.2  christos 		return (0);
    465  1.46.2.2  christos 	}
    466  1.46.2.2  christos 	(void)fclose(from);
    467  1.46.2.2  christos 	(void)fclose(to);
    468  1.46.2.2  christos 
    469  1.46.2.2  christos 	return (done);
    470  1.46.2.2  christos }
    471  1.46.2.2  christos 
    472  1.46.2.2  christos void
    473  1.46.2.2  christos pw_error(const char *name, int error, int eval)
    474  1.46.2.2  christos {
    475  1.46.2.2  christos 
    476  1.46.2.2  christos 	if (error) {
    477  1.46.2.2  christos 		if (name)
    478  1.46.2.2  christos 			warn("%s", name);
    479  1.46.2.2  christos 		else
    480  1.46.2.2  christos 			warn(NULL);
    481  1.46.2.2  christos 	}
    482  1.46.2.2  christos 
    483  1.46.2.2  christos 	warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
    484  1.46.2.2  christos 	pw_abort();
    485  1.46.2.2  christos 	exit(eval);
    486  1.46.2.2  christos }
    487  1.46.2.2  christos 
    488  1.46.2.2  christos /* Removes head and/or tail spaces. */
    489  1.46.2.2  christos static void
    490  1.46.2.2  christos trim_whitespace(char *line)
    491  1.46.2.2  christos {
    492  1.46.2.2  christos 	char *p;
    493  1.46.2.2  christos 
    494  1.46.2.2  christos 	_DIAGASSERT(line != NULL);
    495  1.46.2.2  christos 
    496  1.46.2.2  christos 	/* Remove leading spaces */
    497  1.46.2.2  christos 	p = line;
    498  1.46.2.2  christos 	while (isspace((unsigned char) *p))
    499  1.46.2.2  christos 		p++;
    500  1.46.2.2  christos 	memmove(line, p, strlen(p) + 1);
    501  1.46.2.2  christos 
    502  1.46.2.2  christos 	/* Remove trailing spaces */
    503  1.46.2.2  christos 	p = line + strlen(line) - 1;
    504  1.46.2.2  christos 	while (isspace((unsigned char) *p))
    505  1.46.2.2  christos 		p--;
    506  1.46.2.2  christos 	*(p + 1) = '\0';
    507  1.46.2.2  christos }
    508  1.46.2.2  christos 
    509  1.46.2.2  christos 
    510  1.46.2.2  christos /* Get one line, remove spaces from front and tail */
    511  1.46.2.2  christos static int
    512  1.46.2.2  christos read_line(FILE *fp, char *line, int max)
    513  1.46.2.2  christos {
    514  1.46.2.2  christos 	char   *p;
    515  1.46.2.2  christos 
    516  1.46.2.2  christos 	_DIAGASSERT(fp != NULL);
    517  1.46.2.2  christos 	_DIAGASSERT(line != NULL);
    518  1.46.2.2  christos 
    519  1.46.2.2  christos 	/* Read one line of config */
    520  1.46.2.2  christos 	if (fgets(line, max, fp) == NULL)
    521  1.46.2.2  christos 		return (0);
    522  1.46.2.2  christos 
    523  1.46.2.2  christos 	if ((p = strchr(line, '\n')) == NULL) {
    524  1.46.2.2  christos 		warnx("line too long");
    525  1.46.2.2  christos 		return (0);
    526  1.46.2.2  christos 	}
    527  1.46.2.2  christos 	*p = '\0';
    528  1.46.2.2  christos 
    529  1.46.2.2  christos 	/* Remove comments */
    530  1.46.2.2  christos 	if ((p = strchr(line, '#')) != NULL)
    531  1.46.2.2  christos 		*p = '\0';
    532  1.46.2.2  christos 
    533  1.46.2.2  christos 	trim_whitespace(line);
    534  1.46.2.2  christos 	return (1);
    535  1.46.2.2  christos }
    536  1.46.2.2  christos 
    537  1.46.2.2  christos static const char *
    538  1.46.2.2  christos pw_default(const char *option)
    539  1.46.2.2  christos {
    540  1.46.2.2  christos 	static const char *options[][2] = {
    541  1.46.2.2  christos 		{ "localcipher",	"old" },
    542  1.46.2.2  christos 		{ "ypcipher",		"old" },
    543  1.46.2.2  christos 	};
    544  1.46.2.2  christos 	int i;
    545  1.46.2.2  christos 
    546  1.46.2.2  christos 	_DIAGASSERT(option != NULL);
    547  1.46.2.2  christos 	for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
    548  1.46.2.2  christos 		if (strcmp(options[i][0], option) == 0)
    549  1.46.2.2  christos 			return (options[i][1]);
    550  1.46.2.2  christos 
    551  1.46.2.2  christos 	return (NULL);
    552  1.46.2.2  christos }
    553  1.46.2.2  christos 
    554  1.46.2.2  christos /*
    555  1.46.2.2  christos  * Retrieve password information from the /etc/passwd.conf file, at the
    556  1.46.2.2  christos  * moment this is only for choosing the cipher to use.  It could easily be
    557  1.46.2.2  christos  * used for other authentication methods as well.
    558  1.46.2.2  christos  */
    559  1.46.2.2  christos void
    560  1.46.2.2  christos pw_getconf(char *data, size_t max, const char *key, const char *option)
    561  1.46.2.2  christos {
    562  1.46.2.2  christos 	FILE *fp;
    563  1.46.2.2  christos 	char line[LINE_MAX], *p, *p2;
    564  1.46.2.2  christos 	static char result[LINE_MAX];
    565  1.46.2.2  christos 	int got, found;
    566  1.46.2.2  christos 	const char *cp;
    567  1.46.2.2  christos 
    568  1.46.2.2  christos 	_DIAGASSERT(data != NULL);
    569  1.46.2.2  christos 	_DIAGASSERT(key != NULL);
    570  1.46.2.2  christos 	_DIAGASSERT(option != NULL);
    571  1.46.2.2  christos 
    572  1.46.2.2  christos 	got = 0;
    573  1.46.2.2  christos 	found = 0;
    574  1.46.2.2  christos 	result[0] = '\0';
    575  1.46.2.2  christos 
    576  1.46.2.2  christos 	if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) {
    577  1.46.2.2  christos 		if ((cp = pw_default(option)) != NULL)
    578  1.46.2.2  christos 			strlcpy(data, cp, max);
    579  1.46.2.2  christos 		else
    580  1.46.2.2  christos 			data[0] = '\0';
    581  1.46.2.2  christos 		return;
    582  1.46.2.2  christos 	}
    583  1.46.2.2  christos 
    584  1.46.2.2  christos 	while (!found && (got || read_line(fp, line, LINE_MAX))) {
    585  1.46.2.2  christos 		got = 0;
    586  1.46.2.2  christos 
    587  1.46.2.2  christos 		if (strncmp(key, line, strlen(key)) != 0 ||
    588  1.46.2.2  christos 		    line[strlen(key)] != ':')
    589  1.46.2.2  christos 			continue;
    590  1.46.2.2  christos 
    591  1.46.2.2  christos 		/* Now we found our specified key */
    592  1.46.2.2  christos 		while (read_line(fp, line, LINE_MAX)) {
    593  1.46.2.2  christos 			/* Leaving key field */
    594  1.46.2.2  christos 			if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
    595  1.46.2.2  christos 				got = 1;
    596  1.46.2.2  christos 				break;
    597  1.46.2.2  christos 			}
    598  1.46.2.2  christos 			p2 = line;
    599  1.46.2.2  christos 			if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
    600  1.46.2.2  christos 				continue;
    601  1.46.2.2  christos 			trim_whitespace(p);
    602  1.46.2.2  christos 
    603  1.46.2.2  christos 			if (!strncmp(p, option, strlen(option))) {
    604  1.46.2.2  christos 				trim_whitespace(p2);
    605  1.46.2.2  christos 				strcpy(result, p2);
    606  1.46.2.2  christos 				found = 1;
    607  1.46.2.2  christos 				break;
    608  1.46.2.2  christos 			}
    609  1.46.2.2  christos 		}
    610  1.46.2.2  christos 	}
    611  1.46.2.2  christos 	fclose(fp);
    612  1.46.2.2  christos 
    613  1.46.2.2  christos 	if (!found)
    614  1.46.2.2  christos 		errno = ENOENT;
    615  1.46.2.2  christos 	if (!got)
    616  1.46.2.2  christos 		errno = ENOTDIR;
    617  1.46.2.2  christos 
    618  1.46.2.2  christos 	/*
    619  1.46.2.2  christos 	 * If we got no result and were looking for a default
    620  1.46.2.2  christos 	 * value, try hard coded defaults.
    621  1.46.2.2  christos 	 */
    622  1.46.2.2  christos 
    623  1.46.2.2  christos 	if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
    624  1.46.2.2  christos 	    (cp = pw_default(option)) != NULL)
    625  1.46.2.2  christos 		strlcpy(data, cp, max);
    626  1.46.2.2  christos 	else
    627  1.46.2.2  christos 		strlcpy(data, result, max);
    628  1.46.2.2  christos }
    629  1.46.2.2  christos 
    630  1.46.2.2  christos void
    631  1.46.2.2  christos pw_getpwconf(char *data, size_t max, const struct passwd *pwd,
    632  1.46.2.2  christos     const char *option)
    633  1.46.2.2  christos {
    634  1.46.2.2  christos 	char grpkey[LINE_MAX];
    635  1.46.2.2  christos 	struct group grs, *grp;
    636  1.46.2.2  christos 	char grbuf[1024];
    637  1.46.2.2  christos 
    638  1.46.2.2  christos 	pw_getconf(data, max, pwd->pw_name, option);
    639  1.46.2.2  christos 
    640  1.46.2.2  christos 	/* Try to find an entry for the group */
    641  1.46.2.2  christos 	if (*data == '\0') {
    642  1.46.2.2  christos 		(void)getgrgid_r(pwd->pw_gid, &grs, grbuf, sizeof(grbuf), &grp);
    643  1.46.2.2  christos 		if (grp != NULL) {
    644  1.46.2.2  christos 			(void)snprintf(grpkey, sizeof(grpkey), ":%s",
    645  1.46.2.2  christos 			    grp->gr_name);
    646  1.46.2.2  christos 			pw_getconf(data, max, grpkey, option);
    647  1.46.2.2  christos 		}
    648  1.46.2.2  christos 		if (*data == '\0')
    649  1.46.2.2  christos 		        pw_getconf(data, max, "default", option);
    650  1.46.2.2  christos 	}
    651  1.46.2.2  christos }
    652