4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / lib / libc / gen / getpwent.c
CommitLineData
b8f253e8 1/*
74155b62
KB
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
196b72db 4 *
269a7923 5 * %sccs.include.redist.c%
b8f253e8
KM
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
74155b62 9static char sccsid[] = "@(#)getpwent.c 8.1 (Berkeley) %G%";
196b72db 10#endif /* LIBC_SCCS and not lint */
b8f253e8 11
babc0585
KB
12#include <sys/param.h>
13#include <fcntl.h>
5b622830 14#include <db.h>
11cb27e6 15#include <syslog.h>
a4b07b23 16#include <pwd.h>
babc0585 17#include <utmp.h>
89b9ff0b
KB
18#include <errno.h>
19#include <unistd.h>
20#include <stdlib.h>
21#include <string.h>
babc0585
KB
22#include <limits.h>
23
24static struct passwd _pw_passwd; /* password structure */
5b622830 25static DB *_pw_db; /* password database */
babc0585
KB
26static int _pw_keynum; /* key counter */
27static int _pw_stayopen; /* keep fd's open */
89b9ff0b 28static int __hashpw(), __initdb();
a4b07b23 29
96efcec2
KB
30struct passwd *
31getpwent()
32{
5b622830 33 DBT key;
babc0585 34 char bf[sizeof(_pw_keynum) + 1];
96efcec2 35
babc0585 36 if (!_pw_db && !__initdb())
96efcec2 37 return((struct passwd *)NULL);
babc0585
KB
38
39 ++_pw_keynum;
40 bf[0] = _PW_KEYBYNUM;
41 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
5b622830
KB
42 key.data = (u_char *)bf;
43 key.size = sizeof(_pw_keynum) + 1;
44 return(__hashpw(&key) ? &_pw_passwd : (struct passwd *)NULL);
96efcec2
KB
45}
46
47struct passwd *
babc0585 48getpwnam(name)
89b9ff0b 49 const char *name;
96efcec2 50{
5b622830 51 DBT key;
babc0585
KB
52 int len, rval;
53 char bf[UT_NAMESIZE + 1];
96efcec2 54
babc0585 55 if (!_pw_db && !__initdb())
96efcec2 56 return((struct passwd *)NULL);
96efcec2 57
babc0585
KB
58 bf[0] = _PW_KEYBYNAME;
59 len = strlen(name);
60 bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
5b622830
KB
61 key.data = (u_char *)bf;
62 key.size = len + 1;
63 rval = __hashpw(&key);
babc0585 64
1f5dac11 65 if (!_pw_stayopen) {
5b622830
KB
66 (void)(_pw_db->close)(_pw_db);
67 _pw_db = (DB *)NULL;
babc0585 68 }
96efcec2
KB
69 return(rval ? &_pw_passwd : (struct passwd *)NULL);
70}
71
72struct passwd *
89b9ff0b
KB
73#ifdef __STDC__
74getpwuid(uid_t uid)
75#else
96efcec2
KB
76getpwuid(uid)
77 int uid;
89b9ff0b 78#endif
96efcec2 79{
5b622830 80 DBT key;
1f5dac11 81 int keyuid, rval;
afa8f724 82 char bf[sizeof(keyuid) + 1];
96efcec2 83
babc0585 84 if (!_pw_db && !__initdb())
96efcec2 85 return((struct passwd *)NULL);
96efcec2 86
babc0585 87 bf[0] = _PW_KEYBYUID;
1f5dac11
KB
88 keyuid = uid;
89 bcopy(&keyuid, bf + 1, sizeof(keyuid));
5b622830
KB
90 key.data = (u_char *)bf;
91 key.size = sizeof(keyuid) + 1;
92 rval = __hashpw(&key);
33306bfc 93
1f5dac11 94 if (!_pw_stayopen) {
5b622830
KB
95 (void)(_pw_db->close)(_pw_db);
96 _pw_db = (DB *)NULL;
94aa68ce 97 }
babc0585 98 return(rval ? &_pw_passwd : (struct passwd *)NULL);
96efcec2
KB
99}
100
89b9ff0b 101int
96efcec2
KB
102setpassent(stayopen)
103 int stayopen;
104{
babc0585 105 _pw_keynum = 0;
96efcec2
KB
106 _pw_stayopen = stayopen;
107 return(1);
108}
109
89b9ff0b 110int
babc0585
KB
111setpwent()
112{
113 _pw_keynum = 0;
114 _pw_stayopen = 0;
115 return(1);
116}
117
96efcec2
KB
118void
119endpwent()
120{
babc0585 121 _pw_keynum = 0;
96efcec2 122 if (_pw_db) {
5b622830
KB
123 (void)(_pw_db->close)(_pw_db);
124 _pw_db = (DB *)NULL;
94aa68ce 125 }
96efcec2
KB
126}
127
128static
babc0585 129__initdb()
a4b07b23 130{
babc0585
KB
131 static int warned;
132 char *p;
352c409f 133
1f5dac11 134 p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
46430cc9 135 _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
5b622830 136 if (_pw_db)
96efcec2 137 return(1);
8d33df65
KB
138 if (!warned)
139 syslog(LOG_ERR, "%s: %m", p);
babc0585 140 return(0);
a4b07b23
BJ
141}
142
196b72db 143static
babc0585 144__hashpw(key)
5b622830 145 DBT *key;
a4b07b23 146{
96efcec2 147 register char *p, *t;
babc0585
KB
148 static u_int max;
149 static char *line;
5b622830 150 DBT data;
196b72db 151
5b622830 152 if ((_pw_db->get)(_pw_db, key, &data, 0))
196b72db 153 return(0);
5b622830
KB
154 p = (char *)data.data;
155 if (data.size > max && !(line = realloc(line, max += 1024)))
196b72db 156 return(0);
babc0585 157
96efcec2
KB
158 t = line;
159#define EXPAND(e) e = t; while (*t++ = *p++);
160 EXPAND(_pw_passwd.pw_name);
161 EXPAND(_pw_passwd.pw_passwd);
162 bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
163 p += sizeof(int);
164 bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
165 p += sizeof(int);
166 bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
167 p += sizeof(time_t);
168 EXPAND(_pw_passwd.pw_class);
169 EXPAND(_pw_passwd.pw_gecos);
170 EXPAND(_pw_passwd.pw_dir);
171 EXPAND(_pw_passwd.pw_shell);
172 bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
173 p += sizeof(time_t);
196b72db 174 return(1);
a4b07b23 175}