getpwent.c revision 1.1.1.3 1 /*
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)getpwent.c 8.2 (Berkeley) 4/27/95";
36 #endif /* LIBC_SCCS and not lint */
37
38 #include <sys/param.h>
39 #include <fcntl.h>
40 #include <db.h>
41 #include <syslog.h>
42 #include <pwd.h>
43 #include <utmp.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <limits.h>
49
50 /*
51 * The lookup techniques and data extraction code here must be kept
52 * in sync with that in `pwd_mkdb'.
53 */
54
55 static struct passwd _pw_passwd; /* password structure */
56 static DB *_pw_db; /* password database */
57 static int _pw_keynum; /* key counter */
58 static int _pw_stayopen; /* keep fd's open */
59 static int __hashpw(), __initdb();
60
61 struct passwd *
62 getpwent()
63 {
64 DBT key;
65 char bf[sizeof(_pw_keynum) + 1];
66
67 if (!_pw_db && !__initdb())
68 return((struct passwd *)NULL);
69
70 ++_pw_keynum;
71 bf[0] = _PW_KEYBYNUM;
72 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
73 key.data = (u_char *)bf;
74 key.size = sizeof(_pw_keynum) + 1;
75 return(__hashpw(&key) ? &_pw_passwd : (struct passwd *)NULL);
76 }
77
78 struct passwd *
79 getpwnam(name)
80 const char *name;
81 {
82 DBT key;
83 int len, rval;
84 char bf[UT_NAMESIZE + 1];
85
86 if (!_pw_db && !__initdb())
87 return((struct passwd *)NULL);
88
89 bf[0] = _PW_KEYBYNAME;
90 len = strlen(name);
91 bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
92 key.data = (u_char *)bf;
93 key.size = len + 1;
94 rval = __hashpw(&key);
95
96 if (!_pw_stayopen) {
97 (void)(_pw_db->close)(_pw_db);
98 _pw_db = (DB *)NULL;
99 }
100 return(rval ? &_pw_passwd : (struct passwd *)NULL);
101 }
102
103 struct passwd *
104 getpwuid(uid)
105 uid_t uid;
106 {
107 DBT key;
108 int keyuid, rval;
109 char bf[sizeof(keyuid) + 1];
110
111 if (!_pw_db && !__initdb())
112 return((struct passwd *)NULL);
113
114 bf[0] = _PW_KEYBYUID;
115 keyuid = uid;
116 bcopy(&keyuid, bf + 1, sizeof(keyuid));
117 key.data = (u_char *)bf;
118 key.size = sizeof(keyuid) + 1;
119 rval = __hashpw(&key);
120
121 if (!_pw_stayopen) {
122 (void)(_pw_db->close)(_pw_db);
123 _pw_db = (DB *)NULL;
124 }
125 return(rval ? &_pw_passwd : (struct passwd *)NULL);
126 }
127
128 int
129 setpassent(stayopen)
130 int stayopen;
131 {
132 _pw_keynum = 0;
133 _pw_stayopen = stayopen;
134 return(1);
135 }
136
137 int
138 setpwent()
139 {
140 _pw_keynum = 0;
141 _pw_stayopen = 0;
142 return(1);
143 }
144
145 void
146 endpwent()
147 {
148 _pw_keynum = 0;
149 if (_pw_db) {
150 (void)(_pw_db->close)(_pw_db);
151 _pw_db = (DB *)NULL;
152 }
153 }
154
155 static
156 __initdb()
157 {
158 static int warned;
159 char *p;
160
161 p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
162 _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
163 if (_pw_db)
164 return(1);
165 if (!warned)
166 syslog(LOG_ERR, "%s: %m", p);
167 return(0);
168 }
169
170 static
171 __hashpw(key)
172 DBT *key;
173 {
174 register char *p, *t;
175 static u_int max;
176 static char *line;
177 DBT data;
178
179 if ((_pw_db->get)(_pw_db, key, &data, 0))
180 return(0);
181 p = (char *)data.data;
182 if (data.size > max && !(line = realloc(line, max += 1024)))
183 return(0);
184
185 /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
186 t = line;
187 #define EXPAND(e) e = t; while (*t++ = *p++);
188 #define SCALAR(v) memmove(&(v), p, sizeof v); p += sizeof v
189 EXPAND(_pw_passwd.pw_name);
190 EXPAND(_pw_passwd.pw_passwd);
191 SCALAR(_pw_passwd.pw_uid);
192 SCALAR(_pw_passwd.pw_gid);
193 SCALAR(_pw_passwd.pw_change);
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 SCALAR(_pw_passwd.pw_expire);
199 return(1);
200 }
201