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*46376Sbostic static char sccsid[] = "@(#)getpwent.c 5.15 (Berkeley) 02/12/91"; 1036215Sbostic #endif /* LIBC_SCCS and not lint */ 1122095Smckusick 12*46376Sbostic #include <sys/param.h> 13*46376Sbostic #include <fcntl.h> 142019Swnj #include <pwd.h> 1516416Sralph #include <ndbm.h> 16*46376Sbostic #include <unistd.h> 17*46376Sbostic #include <syslog.h> 18*46376Sbostic #include <utmp.h> 19*46376Sbostic #include <limits.h> 202019Swnj 21*46376Sbostic static struct passwd _pw_passwd; /* password structure */ 22*46376Sbostic static DBM *_pw_db; /* password database */ 23*46376Sbostic static int _pw_keynum; /* key counter */ 24*46376Sbostic static int _pw_stayopen; /* keep fd's open */ 25*46376Sbostic static int _pw_euid; 26*46376Sbostic static __hashpw(), __initdb(); 272019Swnj 2836855Sbostic struct passwd * 2936855Sbostic getpwent() 3036855Sbostic { 31*46376Sbostic datum key; 32*46376Sbostic int rval; 33*46376Sbostic char bf[sizeof(_pw_keynum) + 1]; 3436855Sbostic 35*46376Sbostic if (!_pw_db && !__initdb()) 3636855Sbostic return((struct passwd *)NULL); 37*46376Sbostic 38*46376Sbostic ++_pw_keynum; 39*46376Sbostic bf[0] = _PW_KEYBYNUM; 40*46376Sbostic bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum)); 41*46376Sbostic key.dptr = bf; 42*46376Sbostic key.dsize = sizeof(_pw_keynum) + 1; 43*46376Sbostic rval = __hashpw(key); 44*46376Sbostic 45*46376Sbostic /* Can't leave secure database open. */ 46*46376Sbostic if (!_pw_euid) { 47*46376Sbostic (void)dbm_close(_pw_db); 48*46376Sbostic _pw_db = (DBM *)NULL; 49*46376Sbostic } 50*46376Sbostic return(rval ? &_pw_passwd : (struct passwd *)NULL); 5136855Sbostic } 5236855Sbostic 5336855Sbostic struct passwd * 54*46376Sbostic getpwnam(name) 55*46376Sbostic char *name; 5636855Sbostic { 57*46376Sbostic datum key; 58*46376Sbostic int len, rval; 59*46376Sbostic char bf[UT_NAMESIZE + 1]; 6036855Sbostic 61*46376Sbostic if (!_pw_db && !__initdb()) 6236855Sbostic return((struct passwd *)NULL); 6336855Sbostic 64*46376Sbostic bf[0] = _PW_KEYBYNAME; 65*46376Sbostic len = strlen(name); 66*46376Sbostic bcopy(name, bf + 1, MIN(len, UT_NAMESIZE)); 67*46376Sbostic key.dptr = bf; 68*46376Sbostic key.dsize = len + 1; 69*46376Sbostic rval = __hashpw(key); 70*46376Sbostic 71*46376Sbostic /* Can't leave secure database open. */ 72*46376Sbostic if (!_pw_stayopen || !_pw_euid) { 73*46376Sbostic (void)dbm_close(_pw_db); 74*46376Sbostic _pw_db = (DBM *)NULL; 75*46376Sbostic } 7636855Sbostic return(rval ? &_pw_passwd : (struct passwd *)NULL); 7736855Sbostic } 7836855Sbostic 7936855Sbostic struct passwd * 8036855Sbostic getpwuid(uid) 8136855Sbostic int uid; 8236855Sbostic { 83*46376Sbostic datum key; 8436855Sbostic int rval; 85*46376Sbostic char bf[sizeof(uid) + 1]; 8636855Sbostic 87*46376Sbostic if (!_pw_db && !__initdb()) 8836855Sbostic return((struct passwd *)NULL); 8936855Sbostic 90*46376Sbostic bf[0] = _PW_KEYBYUID; 91*46376Sbostic bcopy(&uid, bf + 1, sizeof(uid)); 92*46376Sbostic key.dptr = bf; 93*46376Sbostic key.dsize = sizeof(uid) + 1; 94*46376Sbostic rval = __hashpw(key); 9536855Sbostic 96*46376Sbostic /* Can't leave secure database open. */ 97*46376Sbostic if (!_pw_stayopen || !_pw_euid) { 98*46376Sbostic (void)dbm_close(_pw_db); 99*46376Sbostic _pw_db = (DBM *)NULL; 10036855Sbostic } 101*46376Sbostic return(rval ? &_pw_passwd : (struct passwd *)NULL); 10236855Sbostic } 10336855Sbostic 10436855Sbostic setpassent(stayopen) 10536855Sbostic int stayopen; 10636855Sbostic { 107*46376Sbostic _pw_keynum = 0; 10836855Sbostic _pw_stayopen = stayopen; 10936855Sbostic return(1); 11036855Sbostic } 11136855Sbostic 112*46376Sbostic setpwent() 113*46376Sbostic { 114*46376Sbostic _pw_keynum = 0; 115*46376Sbostic _pw_stayopen = 0; 116*46376Sbostic return(1); 117*46376Sbostic } 118*46376Sbostic 11936855Sbostic void 12036855Sbostic endpwent() 12136855Sbostic { 122*46376Sbostic _pw_keynum = 0; 12336855Sbostic if (_pw_db) { 124*46376Sbostic (void)dbm_close(_pw_db); 12536855Sbostic _pw_db = (DBM *)NULL; 12641572Sedward } 12736855Sbostic } 12836855Sbostic 12936855Sbostic static 130*46376Sbostic __initdb() 13136855Sbostic { 132*46376Sbostic static int warned; 133*46376Sbostic char *p; 13436215Sbostic 135*46376Sbostic p = (_pw_euid = geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB; 136*46376Sbostic if (_pw_db = dbm_open(p, O_RDONLY, 0)) 13736855Sbostic return(1); 138*46376Sbostic if (!warned) { 139*46376Sbostic openlog("getpwent", LOG_CONS|LOG_PERROR); 140*46376Sbostic syslog(LOG_ALERT, "%s: %m", p); 141*46376Sbostic closelog(); 142*46376Sbostic warned = 1; 14336215Sbostic } 144*46376Sbostic return(0); 1452019Swnj } 1462019Swnj 14736215Sbostic static 148*46376Sbostic __hashpw(key) 14936215Sbostic datum key; 1502019Swnj { 15136855Sbostic register char *p, *t; 152*46376Sbostic static u_int max; 153*46376Sbostic static char *line; 154*46376Sbostic datum dp; 15536215Sbostic 156*46376Sbostic dp = dbm_fetch(_pw_db, key); 157*46376Sbostic if (!(p = dp.dptr)) 15836215Sbostic return(0); 159*46376Sbostic if (dp.dsize > max && !(line = (char *)realloc(line, max += 1024))) 16036215Sbostic return(0); 161*46376Sbostic 16236855Sbostic t = line; 16336855Sbostic #define EXPAND(e) e = t; while (*t++ = *p++); 16436855Sbostic EXPAND(_pw_passwd.pw_name); 16536855Sbostic EXPAND(_pw_passwd.pw_passwd); 16636855Sbostic bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int)); 16736855Sbostic p += sizeof(int); 16836855Sbostic bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int)); 16936855Sbostic p += sizeof(int); 17036855Sbostic bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t)); 17136855Sbostic p += sizeof(time_t); 17236855Sbostic EXPAND(_pw_passwd.pw_class); 17336855Sbostic EXPAND(_pw_passwd.pw_gecos); 17436855Sbostic EXPAND(_pw_passwd.pw_dir); 17536855Sbostic EXPAND(_pw_passwd.pw_shell); 17636855Sbostic bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t)); 17736855Sbostic p += sizeof(time_t); 17836215Sbostic return(1); 1792019Swnj } 180