Commit | Line | Data |
---|---|---|
b8f253e8 | 1 | /* |
74155b62 KB |
2 | * Copyright (c) 1988, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
196b72db | 4 | * |
ad787160 C |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
b8f253e8 KM |
32 | */ |
33 | ||
2ce81398 | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
ad787160 | 35 | static char sccsid[] = "@(#)getpwent.c 8.1 (Berkeley) 6/4/93"; |
196b72db | 36 | #endif /* LIBC_SCCS and not lint */ |
b8f253e8 | 37 | |
babc0585 KB |
38 | #include <sys/param.h> |
39 | #include <fcntl.h> | |
5b622830 | 40 | #include <db.h> |
11cb27e6 | 41 | #include <syslog.h> |
a4b07b23 | 42 | #include <pwd.h> |
babc0585 | 43 | #include <utmp.h> |
89b9ff0b KB |
44 | #include <errno.h> |
45 | #include <unistd.h> | |
46 | #include <stdlib.h> | |
47 | #include <string.h> | |
babc0585 KB |
48 | #include <limits.h> |
49 | ||
50 | static struct passwd _pw_passwd; /* password structure */ | |
5b622830 | 51 | static DB *_pw_db; /* password database */ |
babc0585 KB |
52 | static int _pw_keynum; /* key counter */ |
53 | static int _pw_stayopen; /* keep fd's open */ | |
89b9ff0b | 54 | static int __hashpw(), __initdb(); |
a4b07b23 | 55 | |
96efcec2 KB |
56 | struct passwd * |
57 | getpwent() | |
58 | { | |
5b622830 | 59 | DBT key; |
babc0585 | 60 | char bf[sizeof(_pw_keynum) + 1]; |
96efcec2 | 61 | |
babc0585 | 62 | if (!_pw_db && !__initdb()) |
96efcec2 | 63 | return((struct passwd *)NULL); |
babc0585 KB |
64 | |
65 | ++_pw_keynum; | |
66 | bf[0] = _PW_KEYBYNUM; | |
67 | bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum)); | |
5b622830 KB |
68 | key.data = (u_char *)bf; |
69 | key.size = sizeof(_pw_keynum) + 1; | |
70 | return(__hashpw(&key) ? &_pw_passwd : (struct passwd *)NULL); | |
96efcec2 KB |
71 | } |
72 | ||
73 | struct passwd * | |
babc0585 | 74 | getpwnam(name) |
89b9ff0b | 75 | const char *name; |
96efcec2 | 76 | { |
5b622830 | 77 | DBT key; |
babc0585 KB |
78 | int len, rval; |
79 | char bf[UT_NAMESIZE + 1]; | |
96efcec2 | 80 | |
babc0585 | 81 | if (!_pw_db && !__initdb()) |
96efcec2 | 82 | return((struct passwd *)NULL); |
96efcec2 | 83 | |
babc0585 KB |
84 | bf[0] = _PW_KEYBYNAME; |
85 | len = strlen(name); | |
86 | bcopy(name, bf + 1, MIN(len, UT_NAMESIZE)); | |
5b622830 KB |
87 | key.data = (u_char *)bf; |
88 | key.size = len + 1; | |
89 | rval = __hashpw(&key); | |
babc0585 | 90 | |
1f5dac11 | 91 | if (!_pw_stayopen) { |
5b622830 KB |
92 | (void)(_pw_db->close)(_pw_db); |
93 | _pw_db = (DB *)NULL; | |
babc0585 | 94 | } |
96efcec2 KB |
95 | return(rval ? &_pw_passwd : (struct passwd *)NULL); |
96 | } | |
97 | ||
98 | struct passwd * | |
89b9ff0b KB |
99 | #ifdef __STDC__ |
100 | getpwuid(uid_t uid) | |
101 | #else | |
96efcec2 KB |
102 | getpwuid(uid) |
103 | int uid; | |
89b9ff0b | 104 | #endif |
96efcec2 | 105 | { |
5b622830 | 106 | DBT key; |
1f5dac11 | 107 | int keyuid, rval; |
afa8f724 | 108 | char bf[sizeof(keyuid) + 1]; |
96efcec2 | 109 | |
babc0585 | 110 | if (!_pw_db && !__initdb()) |
96efcec2 | 111 | return((struct passwd *)NULL); |
96efcec2 | 112 | |
babc0585 | 113 | bf[0] = _PW_KEYBYUID; |
1f5dac11 KB |
114 | keyuid = uid; |
115 | bcopy(&keyuid, bf + 1, sizeof(keyuid)); | |
5b622830 KB |
116 | key.data = (u_char *)bf; |
117 | key.size = sizeof(keyuid) + 1; | |
118 | rval = __hashpw(&key); | |
33306bfc | 119 | |
1f5dac11 | 120 | if (!_pw_stayopen) { |
5b622830 KB |
121 | (void)(_pw_db->close)(_pw_db); |
122 | _pw_db = (DB *)NULL; | |
94aa68ce | 123 | } |
babc0585 | 124 | return(rval ? &_pw_passwd : (struct passwd *)NULL); |
96efcec2 KB |
125 | } |
126 | ||
89b9ff0b | 127 | int |
96efcec2 KB |
128 | setpassent(stayopen) |
129 | int stayopen; | |
130 | { | |
babc0585 | 131 | _pw_keynum = 0; |
96efcec2 KB |
132 | _pw_stayopen = stayopen; |
133 | return(1); | |
134 | } | |
135 | ||
89b9ff0b | 136 | int |
babc0585 KB |
137 | setpwent() |
138 | { | |
139 | _pw_keynum = 0; | |
140 | _pw_stayopen = 0; | |
141 | return(1); | |
142 | } | |
143 | ||
96efcec2 KB |
144 | void |
145 | endpwent() | |
146 | { | |
babc0585 | 147 | _pw_keynum = 0; |
96efcec2 | 148 | if (_pw_db) { |
5b622830 KB |
149 | (void)(_pw_db->close)(_pw_db); |
150 | _pw_db = (DB *)NULL; | |
94aa68ce | 151 | } |
96efcec2 KB |
152 | } |
153 | ||
154 | static | |
babc0585 | 155 | __initdb() |
a4b07b23 | 156 | { |
babc0585 KB |
157 | static int warned; |
158 | char *p; | |
352c409f | 159 | |
1f5dac11 | 160 | p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB; |
46430cc9 | 161 | _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL); |
5b622830 | 162 | if (_pw_db) |
96efcec2 | 163 | return(1); |
8d33df65 KB |
164 | if (!warned) |
165 | syslog(LOG_ERR, "%s: %m", p); | |
babc0585 | 166 | return(0); |
a4b07b23 BJ |
167 | } |
168 | ||
196b72db | 169 | static |
babc0585 | 170 | __hashpw(key) |
5b622830 | 171 | DBT *key; |
a4b07b23 | 172 | { |
96efcec2 | 173 | register char *p, *t; |
babc0585 KB |
174 | static u_int max; |
175 | static char *line; | |
5b622830 | 176 | DBT data; |
196b72db | 177 | |
5b622830 | 178 | if ((_pw_db->get)(_pw_db, key, &data, 0)) |
196b72db | 179 | return(0); |
5b622830 KB |
180 | p = (char *)data.data; |
181 | if (data.size > max && !(line = realloc(line, max += 1024))) | |
196b72db | 182 | return(0); |
babc0585 | 183 | |
96efcec2 KB |
184 | t = line; |
185 | #define EXPAND(e) e = t; while (*t++ = *p++); | |
186 | EXPAND(_pw_passwd.pw_name); | |
187 | EXPAND(_pw_passwd.pw_passwd); | |
188 | bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int)); | |
189 | p += sizeof(int); | |
190 | bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int)); | |
191 | p += sizeof(int); | |
192 | bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t)); | |
193 | p += sizeof(time_t); | |
194 | EXPAND(_pw_passwd.pw_class); | |
195 | EXPAND(_pw_passwd.pw_gecos); | |
196 | EXPAND(_pw_passwd.pw_dir); | |
197 | EXPAND(_pw_passwd.pw_shell); | |
198 | bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t)); | |
199 | p += sizeof(time_t); | |
196b72db | 200 | return(1); |
a4b07b23 | 201 | } |