Home | History | Annotate | Line # | Download | only in libutil
passwd.c revision 1.27
      1  1.27      tron /*	$NetBSD: passwd.c,v 1.27 2000/12/06 13:37:53 tron 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.27      tron __RCSID("$NetBSD: passwd.c,v 1.27 2000/12/06 13:37:53 tron 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.27      tron #include <sys/param.h>
     43   1.1       jtc #include <sys/stat.h>
     44   1.1       jtc #include <sys/time.h>
     45   1.1       jtc #include <sys/resource.h>
     46   1.1       jtc #include <sys/wait.h>
     47   1.1       jtc 
     48  1.16     lukem #include <assert.h>
     49   1.3   thorpej #include <ctype.h>
     50   1.5   mycroft #include <err.h>
     51  1.12     lukem #include <errno.h>
     52   1.1       jtc #include <fcntl.h>
     53  1.12     lukem #include <limits.h>
     54  1.12     lukem #include <paths.h>
     55  1.12     lukem #include <pwd.h>
     56  1.12     lukem #include <signal.h>
     57  1.12     lukem #include <stdio.h>
     58   1.1       jtc #include <stdlib.h>
     59   1.1       jtc #include <string.h>
     60  1.12     lukem #include <unistd.h>
     61   1.1       jtc #include <util.h>
     62   1.1       jtc 
     63  1.27      tron static const char      *pw_filename(const char *filename);
     64  1.27      tron static void		pw_cont(int sig);
     65  1.27      tron static int		pw_equal(char *buf, struct passwd *old_pw);
     66  1.27      tron static const char      *pw_default(const char *option);
     67  1.27      tron static int		read_line(FILE *fp, char *line, int max);
     68  1.27      tron static void		trim_whitespace(char *line);
     69  1.27      tron 
     70  1.27      tron static	char	pw_prefix[MAXPATHLEN];
     71  1.27      tron 
     72  1.27      tron const char *
     73  1.27      tron pw_getprefix(void)
     74  1.27      tron {
     75  1.27      tron 
     76  1.27      tron 	return(pw_prefix);
     77  1.27      tron }
     78  1.27      tron 
     79  1.27      tron int
     80  1.27      tron pw_setprefix(const char *new_prefix)
     81  1.27      tron {
     82  1.27      tron 	size_t length;
     83  1.27      tron 
     84  1.27      tron 	length = strlen(new_prefix);
     85  1.27      tron 	if (length < sizeof(pw_prefix)) {
     86  1.27      tron 		(void)strcpy(pw_prefix, new_prefix);
     87  1.27      tron 		while (length > 0 && pw_prefix[length - 1] == '/')
     88  1.27      tron 			pw_prefix[--length] = '\0';
     89  1.27      tron 		return(0);
     90  1.27      tron 	}
     91  1.27      tron 	errno = ENAMETOOLONG;
     92  1.27      tron 	return(-1);
     93  1.27      tron }
     94  1.27      tron 
     95  1.27      tron static const char *
     96  1.27      tron pw_filename(const char *filename)
     97  1.27      tron {
     98  1.27      tron 	static char newfilename[MAXPATHLEN];
     99  1.27      tron 
    100  1.27      tron 	if (pw_prefix[0] == '\0')
    101  1.27      tron 		return filename;
    102  1.27      tron 
    103  1.27      tron 	if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
    104  1.27      tron 		return strcat(strcpy(newfilename, pw_prefix), filename);
    105  1.27      tron 
    106  1.27      tron 	errno = ENAMETOOLONG;
    107  1.27      tron 	return(NULL);
    108  1.27      tron }
    109   1.1       jtc 
    110   1.1       jtc int
    111  1.20        ad pw_lock(int retries)
    112   1.1       jtc {
    113  1.27      tron 	const char *filename;
    114   1.1       jtc 	int i, fd;
    115   1.1       jtc 	mode_t old_mode;
    116  1.14  christos 	int oerrno;
    117   1.1       jtc 
    118   1.1       jtc 	/* Acquire the lock file. */
    119  1.27      tron 	filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
    120  1.27      tron 	if (filename == NULL)
    121  1.27      tron 		return(-1);
    122   1.1       jtc 	old_mode = umask(0);
    123  1.27      tron 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
    124   1.1       jtc 	for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
    125   1.1       jtc 		sleep(1);
    126  1.27      tron 		fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
    127   1.1       jtc 			  0600);
    128   1.1       jtc 	}
    129  1.14  christos 	oerrno = errno;
    130  1.14  christos 	(void)umask(old_mode);
    131  1.14  christos 	errno = oerrno;
    132   1.1       jtc 	return(fd);
    133   1.1       jtc }
    134   1.1       jtc 
    135   1.1       jtc int
    136  1.20        ad pw_mkdb(void)
    137   1.1       jtc {
    138   1.1       jtc 	int pstat;
    139   1.1       jtc 	pid_t pid;
    140   1.1       jtc 
    141   1.1       jtc 	pid = vfork();
    142  1.19       mjl 	if (pid == -1)
    143  1.19       mjl 		return (-1);
    144  1.19       mjl 
    145   1.1       jtc 	if (pid == 0) {
    146  1.27      tron 		execl(_PATH_PWD_MKDB, "pwd_mkdb", "-d", pw_prefix,
    147  1.27      tron 		      "-p", _PATH_MASTERPASSWD_LOCK, NULL);
    148  1.11   thorpej 		_exit(1);
    149   1.1       jtc 	}
    150   1.1       jtc 	pid = waitpid(pid, &pstat, 0);
    151   1.2   ghudson 	if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
    152   1.1       jtc 		return(-1);
    153   1.1       jtc 	return(0);
    154   1.1       jtc }
    155   1.1       jtc 
    156   1.1       jtc int
    157  1.20        ad pw_abort(void)
    158   1.1       jtc {
    159  1.27      tron 	const char *filename;
    160  1.20        ad 
    161  1.27      tron 	filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
    162  1.27      tron 	return((filename == NULL) ? -1 : unlink(filename));
    163   1.1       jtc }
    164   1.1       jtc 
    165   1.1       jtc /* Everything below this point is intended for the convenience of programs
    166   1.1       jtc  * which allow a user to interactively edit the passwd file.  Errors in the
    167   1.1       jtc  * routines below will cause the process to abort. */
    168   1.1       jtc 
    169   1.1       jtc static pid_t editpid = -1;
    170   1.1       jtc 
    171   1.1       jtc static void
    172  1.20        ad pw_cont(int sig)
    173   1.1       jtc {
    174   1.1       jtc 
    175   1.1       jtc 	if (editpid != -1)
    176   1.1       jtc 		kill(editpid, sig);
    177   1.1       jtc }
    178   1.1       jtc 
    179   1.1       jtc void
    180  1.20        ad pw_init(void)
    181   1.1       jtc {
    182   1.1       jtc 	struct rlimit rlim;
    183   1.1       jtc 
    184   1.1       jtc 	/* Unlimited resource limits. */
    185   1.1       jtc 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
    186   1.1       jtc 	(void)setrlimit(RLIMIT_CPU, &rlim);
    187   1.1       jtc 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
    188   1.1       jtc 	(void)setrlimit(RLIMIT_STACK, &rlim);
    189   1.1       jtc 	(void)setrlimit(RLIMIT_DATA, &rlim);
    190   1.1       jtc 	(void)setrlimit(RLIMIT_RSS, &rlim);
    191   1.1       jtc 
    192   1.1       jtc 	/* Don't drop core (not really necessary, but GP's). */
    193   1.1       jtc 	rlim.rlim_cur = rlim.rlim_max = 0;
    194   1.1       jtc 	(void)setrlimit(RLIMIT_CORE, &rlim);
    195   1.1       jtc 
    196   1.1       jtc 	/* Turn off signals. */
    197   1.1       jtc 	(void)signal(SIGALRM, SIG_IGN);
    198   1.1       jtc 	(void)signal(SIGHUP, SIG_IGN);
    199   1.1       jtc 	(void)signal(SIGINT, SIG_IGN);
    200   1.1       jtc 	(void)signal(SIGPIPE, SIG_IGN);
    201   1.1       jtc 	(void)signal(SIGQUIT, SIG_IGN);
    202   1.1       jtc 	(void)signal(SIGTERM, SIG_IGN);
    203   1.1       jtc 	(void)signal(SIGCONT, pw_cont);
    204   1.1       jtc }
    205   1.1       jtc 
    206   1.1       jtc void
    207  1.20        ad pw_edit(int notsetuid, const char *filename)
    208   1.1       jtc {
    209  1.19       mjl 	int pstat;
    210   1.1       jtc 	char *p, *editor;
    211  1.19       mjl 	char *argp[] = { "sh", "-c", NULL, NULL };
    212  1.19       mjl 
    213   1.8  christos #ifdef __GNUC__
    214   1.8  christos 	(void) &editor;
    215   1.8  christos #endif
    216   1.1       jtc 
    217   1.3   thorpej 	if (filename == NULL)
    218   1.1       jtc 		filename = _PATH_MASTERPASSWD_LOCK;
    219  1.19       mjl 
    220  1.27      tron 	filename = pw_filename(filename);
    221  1.27      tron 	if (filename == NULL)
    222  1.27      tron 		return;
    223  1.27      tron 
    224   1.3   thorpej 	if ((editor = getenv("EDITOR")) == NULL)
    225  1.19       mjl 		editor = _PATH_VI;
    226   1.3   thorpej 
    227  1.19       mjl 	p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
    228  1.19       mjl 	if (p == NULL)
    229  1.19       mjl 		return;
    230  1.19       mjl 
    231  1.19       mjl 	sprintf(p, "%s %s", editor, filename);
    232  1.19       mjl 	argp[2] = p;
    233  1.19       mjl 
    234  1.19       mjl 	switch(editpid = vfork()) {
    235  1.19       mjl 	case -1:
    236  1.19       mjl 		free(p);
    237  1.19       mjl 		return;
    238  1.19       mjl 	case 0:
    239   1.1       jtc 		if (notsetuid) {
    240   1.1       jtc 			setgid(getgid());
    241   1.1       jtc 			setuid(getuid());
    242   1.1       jtc 		}
    243  1.19       mjl 		execvp(_PATH_BSHELL, argp);
    244   1.1       jtc 		_exit(1);
    245   1.1       jtc 	}
    246  1.19       mjl 
    247  1.19       mjl 	free(p);
    248  1.19       mjl 
    249   1.1       jtc 	for (;;) {
    250   1.1       jtc 		editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
    251   1.1       jtc 		if (editpid == -1)
    252   1.1       jtc 			pw_error(editor, 1, 1);
    253   1.1       jtc 		else if (WIFSTOPPED(pstat))
    254   1.1       jtc 			raise(WSTOPSIG(pstat));
    255   1.1       jtc 		else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
    256   1.1       jtc 			break;
    257   1.1       jtc 		else
    258   1.1       jtc 			pw_error(editor, 1, 1);
    259   1.1       jtc 	}
    260   1.1       jtc 	editpid = -1;
    261   1.1       jtc }
    262   1.1       jtc 
    263   1.1       jtc void
    264  1.20        ad pw_prompt(void)
    265   1.1       jtc {
    266   1.1       jtc 	int c;
    267   1.1       jtc 
    268   1.1       jtc 	(void)printf("re-edit the password file? [y]: ");
    269   1.1       jtc 	(void)fflush(stdout);
    270   1.1       jtc 	c = getchar();
    271   1.1       jtc 	if (c != EOF && c != '\n')
    272   1.1       jtc 		while (getchar() != '\n');
    273   1.1       jtc 	if (c == 'n')
    274   1.1       jtc 		pw_error(NULL, 0, 0);
    275   1.1       jtc }
    276   1.1       jtc 
    277  1.10      phil /* for use in pw_copy(). Compare a pw entry to a pw struct. */
    278  1.10      phil static int
    279  1.20        ad pw_equal(char *buf, struct passwd *pw)
    280  1.10      phil {
    281  1.10      phil 	struct passwd buf_pw;
    282  1.16     lukem 	int len;
    283  1.16     lukem 
    284  1.16     lukem 	_DIAGASSERT(buf != NULL);
    285  1.16     lukem 	_DIAGASSERT(pw != NULL);
    286  1.16     lukem 
    287  1.16     lukem 	len = strlen (buf);
    288  1.10      phil 	if (buf[len-1] == '\n')
    289  1.10      phil 		buf[len-1] = '\0';
    290  1.10      phil 	if (!pw_scan(buf, &buf_pw, NULL))
    291  1.10      phil 		return 0;
    292  1.10      phil 	return !strcmp(pw->pw_name, buf_pw.pw_name)
    293  1.10      phil 		&& pw->pw_uid == buf_pw.pw_uid
    294  1.10      phil 		&& pw->pw_gid == buf_pw.pw_gid
    295  1.10      phil 		&& !strcmp(pw->pw_class, buf_pw.pw_class)
    296  1.10      phil 		&& (long)pw->pw_change == (long)buf_pw.pw_change
    297  1.10      phil 		&& (long)pw->pw_expire == (long)buf_pw.pw_expire
    298  1.10      phil 		&& !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
    299  1.10      phil 		&& !strcmp(pw->pw_dir, buf_pw.pw_dir)
    300  1.10      phil 		&& !strcmp(pw->pw_shell, buf_pw.pw_shell);
    301  1.10      phil }
    302  1.10      phil 
    303   1.1       jtc void
    304  1.20        ad pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
    305   1.1       jtc {
    306  1.27      tron 	const char *filename;
    307  1.27      tron 	char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
    308   1.1       jtc 	FILE *from, *to;
    309   1.1       jtc 	int done;
    310   1.1       jtc 
    311  1.16     lukem 	_DIAGASSERT(pw != NULL);
    312  1.16     lukem 	/* old_pw may be NULL */
    313  1.16     lukem 
    314  1.27      tron 	if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL)
    315  1.27      tron 		pw_error(pw_prefix, 1,1);
    316  1.27      tron 	(void)strcpy(mpwd, filename);
    317  1.27      tron 	if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL)
    318  1.27      tron 		pw_error(pw_prefix, 1,1);
    319  1.27      tron 	(void)strcpy(mpwdl, filename);
    320  1.27      tron 
    321   1.1       jtc 	if (!(from = fdopen(ffd, "r")))
    322  1.27      tron 		pw_error(mpwd, 1, 1);
    323   1.1       jtc 	if (!(to = fdopen(tfd, "w")))
    324  1.27      tron 		pw_error(mpwdl, 1, 1);
    325   1.1       jtc 
    326   1.1       jtc 	for (done = 0; fgets(buf, sizeof(buf), from);) {
    327   1.1       jtc 		if (!strchr(buf, '\n')) {
    328  1.27      tron 			warnx("%s: line too long", mpwd);
    329   1.1       jtc 			pw_error(NULL, 0, 1);
    330   1.1       jtc 		}
    331   1.1       jtc 		if (done) {
    332   1.1       jtc 			(void)fprintf(to, "%s", buf);
    333   1.1       jtc 			if (ferror(to))
    334   1.1       jtc 				goto err;
    335   1.1       jtc 			continue;
    336   1.1       jtc 		}
    337   1.1       jtc 		if (!(p = strchr(buf, ':'))) {
    338  1.27      tron 			warnx("%s: corrupted entry", mpwd);
    339   1.1       jtc 			pw_error(NULL, 0, 1);
    340   1.1       jtc 		}
    341   1.1       jtc 		*p = '\0';
    342   1.1       jtc 		if (strcmp(buf, pw->pw_name)) {
    343   1.1       jtc 			*p = ':';
    344   1.1       jtc 			(void)fprintf(to, "%s", buf);
    345   1.1       jtc 			if (ferror(to))
    346   1.1       jtc 				goto err;
    347   1.1       jtc 			continue;
    348   1.1       jtc 		}
    349  1.10      phil 		*p = ':';
    350  1.10      phil 		if (old_pw && !pw_equal(buf, old_pw)) {
    351  1.27      tron 			warnx("%s: entry inconsistent", mpwd);
    352  1.10      phil 			pw_error(NULL, 0, 1);
    353  1.10      phil 		}
    354   1.1       jtc 		(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    355   1.1       jtc 		    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    356   1.9  christos 		    pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
    357   1.9  christos 		    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
    358   1.1       jtc 		done = 1;
    359   1.1       jtc 		if (ferror(to))
    360   1.1       jtc 			goto err;
    361   1.1       jtc 	}
    362  1.10      phil 	/* Only append a new entry if real uid is root! */
    363  1.13   thorpej 	if (!done) {
    364  1.10      phil 		if (getuid() == 0)
    365  1.10      phil 			(void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
    366  1.10      phil 			    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
    367  1.10      phil 			    pw->pw_class, (long)pw->pw_change,
    368  1.10      phil 			    (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
    369  1.10      phil 			    pw->pw_shell);
    370  1.10      phil 		else
    371  1.27      tron 			warnx("%s: changes not made, no such entry", mpwd);
    372  1.13   thorpej 	}
    373   1.1       jtc 
    374   1.1       jtc 	if (ferror(to))
    375   1.1       jtc err:		pw_error(NULL, 1, 1);
    376   1.1       jtc 	(void)fclose(to);
    377   1.1       jtc }
    378   1.1       jtc 
    379   1.1       jtc void
    380  1.20        ad pw_error(const char *name, int err, int eval)
    381   1.1       jtc {
    382  1.16     lukem 
    383  1.26    itojun 	if (err) {
    384  1.26    itojun 		if (name)
    385  1.26    itojun 			warn("%s", name);
    386  1.26    itojun 		else
    387  1.26    itojun 			warn(NULL);
    388  1.26    itojun 	}
    389   1.1       jtc 
    390  1.27      tron 	warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
    391   1.1       jtc 	pw_abort();
    392   1.1       jtc 	exit(eval);
    393  1.20        ad }
    394  1.20        ad 
    395  1.20        ad /* Removes head and/or tail spaces. */
    396  1.20        ad static void
    397  1.20        ad trim_whitespace(char *line)
    398  1.20        ad {
    399  1.20        ad 	char *p;
    400  1.20        ad 
    401  1.20        ad 	/* Remove leading spaces */
    402  1.20        ad 	p = line;
    403  1.24     itohy 	while (isspace((unsigned char) *p))
    404  1.20        ad 		p++;
    405  1.20        ad 	memmove(line, p, strlen(p) + 1);
    406  1.20        ad 
    407  1.20        ad 	/* Remove trailing spaces */
    408  1.20        ad 	p = line + strlen(line) - 1;
    409  1.24     itohy 	while (isspace((unsigned char) *p))
    410  1.20        ad 		p--;
    411  1.20        ad 	*(p + 1) = '\0';
    412  1.20        ad }
    413  1.20        ad 
    414  1.20        ad 
    415  1.20        ad /* Get one line, remove spaces from front and tail */
    416  1.20        ad static int
    417  1.20        ad read_line(FILE *fp, char *line, int max)
    418  1.20        ad {
    419  1.20        ad 	char   *p;
    420  1.20        ad 
    421  1.20        ad 	/* Read one line of config */
    422  1.20        ad 	if (fgets(line, max, fp) == NULL)
    423  1.20        ad 		return (0);
    424  1.20        ad 
    425  1.20        ad 	if ((p = strchr(line, '\n')) == NULL) {
    426  1.20        ad 		warnx("line too long");
    427  1.20        ad 		return (0);
    428  1.20        ad 	}
    429  1.20        ad 	*p = '\0';
    430  1.20        ad 
    431  1.20        ad 	/* Remove comments */
    432  1.20        ad 	if ((p = strchr(line, '#')) != NULL)
    433  1.20        ad 		*p = '\0';
    434  1.20        ad 
    435  1.20        ad 	trim_whitespace(line);
    436  1.20        ad 	return (1);
    437  1.20        ad }
    438  1.20        ad 
    439  1.20        ad static const char *
    440  1.20        ad pw_default(const char *option)
    441  1.20        ad {
    442  1.20        ad 	static const char *options[][2] = {
    443  1.20        ad 		{ "localcipher",	"old" },
    444  1.20        ad 		{ "ypcipher",		"old" },
    445  1.20        ad 	};
    446  1.20        ad 	int i;
    447  1.20        ad 
    448  1.20        ad 	for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
    449  1.20        ad 		if (strcmp(options[i][0], option) == 0)
    450  1.20        ad 			return (options[i][1]);
    451  1.20        ad 
    452  1.20        ad 	return (NULL);
    453  1.20        ad }
    454  1.20        ad 
    455  1.20        ad /*
    456  1.20        ad  * Retrieve password information from the /etc/passwd.conf file, at the
    457  1.20        ad  * moment this is only for choosing the cipher to use.  It could easily be
    458  1.20        ad  * used for other authentication methods as well.
    459  1.20        ad  */
    460  1.20        ad void
    461  1.20        ad pw_getconf(char *data, size_t max, const char *key, const char *option)
    462  1.20        ad {
    463  1.20        ad 	FILE *fp;
    464  1.20        ad 	char line[LINE_MAX], *p, *p2;
    465  1.20        ad 	static char result[LINE_MAX];
    466  1.20        ad 	int got, found;
    467  1.20        ad 	const char *cp;
    468  1.20        ad 
    469  1.20        ad 	got = 0;
    470  1.20        ad 	found = 0;
    471  1.20        ad 	result[0] = '\0';
    472  1.20        ad 
    473  1.20        ad 	if ((fp = fopen(_PATH_PASSWDCONF, "r")) == NULL) {
    474  1.20        ad 		if ((cp = pw_default(option)) != NULL)
    475  1.23        ad 			strlcpy(data, cp, max);
    476  1.20        ad 		else
    477  1.20        ad 			data[0] = '\0';
    478  1.20        ad 		return;
    479  1.20        ad 	}
    480  1.20        ad 
    481  1.20        ad 	while (!found && (got || read_line(fp, line, LINE_MAX))) {
    482  1.20        ad 		got = 0;
    483  1.20        ad 
    484  1.20        ad 		if (strncmp(key, line, strlen(key)) != 0 ||
    485  1.20        ad 		    line[strlen(key)] != ':')
    486  1.20        ad 			continue;
    487  1.20        ad 
    488  1.20        ad 		/* Now we found our specified key */
    489  1.20        ad 		while (read_line(fp, line, LINE_MAX)) {
    490  1.20        ad 			/* Leaving key field */
    491  1.21        ad 			if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
    492  1.20        ad 				got = 1;
    493  1.20        ad 				break;
    494  1.20        ad 			}
    495  1.20        ad 			p2 = line;
    496  1.20        ad 			if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
    497  1.20        ad 				continue;
    498  1.20        ad 			trim_whitespace(p);
    499  1.20        ad 
    500  1.20        ad 			if (!strncmp(p, option, strlen(option))) {
    501  1.20        ad 				trim_whitespace(p2);
    502  1.20        ad 				strcpy(result, p2);
    503  1.20        ad 				found = 1;
    504  1.20        ad 				break;
    505  1.20        ad 			}
    506  1.20        ad 		}
    507  1.20        ad 	}
    508  1.20        ad 	fclose(fp);
    509  1.20        ad 
    510  1.20        ad 	/*
    511  1.20        ad 	 * If we got no result and were looking for a default
    512  1.20        ad 	 * value, try hard coded defaults.
    513  1.20        ad 	 */
    514  1.20        ad 
    515  1.20        ad 	if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
    516  1.20        ad 	    (cp = pw_default(option)) != NULL)
    517  1.23        ad 		strlcpy(data, cp, max);
    518  1.20        ad 	else
    519  1.20        ad 		strlcpy(data, result, max);
    520   1.1       jtc }
    521