xref: /csrg-svn/lib/libc/gen/getpwent.c (revision 46660)
122095Smckusick /*
236215Sbostic  * Copyright (c) 1988 The Regents of the University of California.
336215Sbostic  * All rights reserved.
436215Sbostic  *
542624Sbostic  * %sccs.include.redist.c%
622095Smckusick  */
722095Smckusick 
826560Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*46660Sbostic static char sccsid[] = "@(#)getpwent.c	5.18 (Berkeley) 02/25/91";
1036215Sbostic #endif /* LIBC_SCCS and not lint */
1122095Smckusick 
1246376Sbostic #include <sys/param.h>
1346376Sbostic #include <fcntl.h>
142019Swnj #include <pwd.h>
1516416Sralph #include <ndbm.h>
1646615Sbostic #include <utmp.h>
1746615Sbostic #include <errno.h>
1846376Sbostic #include <unistd.h>
1946615Sbostic #include <stdlib.h>
2046615Sbostic #include <string.h>
2146376Sbostic #include <limits.h>
222019Swnj 
2346376Sbostic static struct passwd _pw_passwd;	/* password structure */
2446376Sbostic static DBM *_pw_db;			/* password database */
2546376Sbostic static int _pw_keynum;			/* key counter */
2646376Sbostic static int _pw_stayopen;		/* keep fd's open */
2746615Sbostic static int __hashpw(), __initdb();
282019Swnj 
2936855Sbostic struct passwd *
3036855Sbostic getpwent()
3136855Sbostic {
3246376Sbostic 	datum key;
3346376Sbostic 	char bf[sizeof(_pw_keynum) + 1];
3436855Sbostic 
3546376Sbostic 	if (!_pw_db && !__initdb())
3636855Sbostic 		return((struct passwd *)NULL);
3746376Sbostic 
3846376Sbostic 	++_pw_keynum;
3946376Sbostic 	bf[0] = _PW_KEYBYNUM;
4046376Sbostic 	bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
4146376Sbostic 	key.dptr = bf;
4246376Sbostic 	key.dsize = sizeof(_pw_keynum) + 1;
4346659Sbostic 	return(__hashpw(key) ? &_pw_passwd : (struct passwd *)NULL);
4436855Sbostic }
4536855Sbostic 
4636855Sbostic struct passwd *
4746376Sbostic getpwnam(name)
4846615Sbostic 	const char *name;
4936855Sbostic {
5046376Sbostic 	datum key;
5146376Sbostic 	int len, rval;
5246376Sbostic 	char bf[UT_NAMESIZE + 1];
5336855Sbostic 
5446376Sbostic 	if (!_pw_db && !__initdb())
5536855Sbostic 		return((struct passwd *)NULL);
5636855Sbostic 
5746376Sbostic 	bf[0] = _PW_KEYBYNAME;
5846376Sbostic 	len = strlen(name);
5946376Sbostic 	bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
6046376Sbostic 	key.dptr = bf;
6146376Sbostic 	key.dsize = len + 1;
6246376Sbostic 	rval = __hashpw(key);
6346376Sbostic 
6446659Sbostic 	if (!_pw_stayopen) {
6546376Sbostic 		(void)dbm_close(_pw_db);
6646376Sbostic 		_pw_db = (DBM *)NULL;
6746376Sbostic 	}
6836855Sbostic 	return(rval ? &_pw_passwd : (struct passwd *)NULL);
6936855Sbostic }
7036855Sbostic 
7136855Sbostic struct passwd *
7246615Sbostic #ifdef __STDC__
7346615Sbostic getpwuid(uid_t uid)
7446615Sbostic #else
7536855Sbostic getpwuid(uid)
7636855Sbostic 	int uid;
7746615Sbostic #endif
7836855Sbostic {
7946376Sbostic 	datum key;
8046659Sbostic 	int keyuid, rval;
81*46660Sbostic 	char bf[sizeof(keyuid) + 1];
8236855Sbostic 
8346376Sbostic 	if (!_pw_db && !__initdb())
8436855Sbostic 		return((struct passwd *)NULL);
8536855Sbostic 
8646376Sbostic 	bf[0] = _PW_KEYBYUID;
8746659Sbostic 	keyuid = uid;
8846659Sbostic 	bcopy(&keyuid, bf + 1, sizeof(keyuid));
8946376Sbostic 	key.dptr = bf;
9046659Sbostic 	key.dsize = sizeof(keyuid) + 1;
9146376Sbostic 	rval = __hashpw(key);
9236855Sbostic 
9346659Sbostic 	if (!_pw_stayopen) {
9446376Sbostic 		(void)dbm_close(_pw_db);
9546376Sbostic 		_pw_db = (DBM *)NULL;
9636855Sbostic 	}
9746376Sbostic 	return(rval ? &_pw_passwd : (struct passwd *)NULL);
9836855Sbostic }
9936855Sbostic 
10046615Sbostic int
10136855Sbostic setpassent(stayopen)
10236855Sbostic 	int stayopen;
10336855Sbostic {
10446376Sbostic 	_pw_keynum = 0;
10536855Sbostic 	_pw_stayopen = stayopen;
10636855Sbostic 	return(1);
10736855Sbostic }
10836855Sbostic 
10946615Sbostic int
11046376Sbostic setpwent()
11146376Sbostic {
11246376Sbostic 	_pw_keynum = 0;
11346376Sbostic 	_pw_stayopen = 0;
11446376Sbostic 	return(1);
11546376Sbostic }
11646376Sbostic 
11736855Sbostic void
11836855Sbostic endpwent()
11936855Sbostic {
12046376Sbostic 	_pw_keynum = 0;
12136855Sbostic 	if (_pw_db) {
12246376Sbostic 		(void)dbm_close(_pw_db);
12336855Sbostic 		_pw_db = (DBM *)NULL;
12441572Sedward 	}
12536855Sbostic }
12636855Sbostic 
12736855Sbostic static
12846376Sbostic __initdb()
12936855Sbostic {
13046376Sbostic 	static int warned;
13146376Sbostic 	char *p;
13236215Sbostic 
13346659Sbostic 	p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
13446376Sbostic 	if (_pw_db = dbm_open(p, O_RDONLY, 0))
13536855Sbostic 		return(1);
13646376Sbostic 	if (!warned) {
13746615Sbostic 		(void)write(STDERR_FILENO, "getpwent: ", 10);
13846615Sbostic 		(void)write(STDERR_FILENO, p, strlen(p));
13946615Sbostic 		(void)write(STDERR_FILENO, ": ", 2);
14046615Sbostic 		p = strerror(errno);
14146615Sbostic 		(void)write(STDERR_FILENO, p, strlen(p));
14246615Sbostic 		(void)write(STDERR_FILENO, "\n", 1);
14346376Sbostic 		warned = 1;
14436215Sbostic 	}
14546376Sbostic 	return(0);
1462019Swnj }
1472019Swnj 
14836215Sbostic static
14946376Sbostic __hashpw(key)
15036215Sbostic 	datum key;
1512019Swnj {
15236855Sbostic 	register char *p, *t;
15346376Sbostic 	static u_int max;
15446376Sbostic 	static char *line;
15546376Sbostic 	datum dp;
15636215Sbostic 
15746376Sbostic 	dp = dbm_fetch(_pw_db, key);
15846376Sbostic 	if (!(p = dp.dptr))
15936215Sbostic 		return(0);
16046376Sbostic 	if (dp.dsize > max && !(line = (char *)realloc(line, max += 1024)))
16136215Sbostic 		return(0);
16246376Sbostic 
16336855Sbostic 	t = line;
16436855Sbostic #define	EXPAND(e)	e = t; while (*t++ = *p++);
16536855Sbostic 	EXPAND(_pw_passwd.pw_name);
16636855Sbostic 	EXPAND(_pw_passwd.pw_passwd);
16736855Sbostic 	bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
16836855Sbostic 	p += sizeof(int);
16936855Sbostic 	bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
17036855Sbostic 	p += sizeof(int);
17136855Sbostic 	bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
17236855Sbostic 	p += sizeof(time_t);
17336855Sbostic 	EXPAND(_pw_passwd.pw_class);
17436855Sbostic 	EXPAND(_pw_passwd.pw_gecos);
17536855Sbostic 	EXPAND(_pw_passwd.pw_dir);
17636855Sbostic 	EXPAND(_pw_passwd.pw_shell);
17736855Sbostic 	bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
17836855Sbostic 	p += sizeof(time_t);
17936215Sbostic 	return(1);
1802019Swnj }
181