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