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