fts(3) updates
[unix-history] / lib / libc / gen / getpwent.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * 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)
35static char sccsid[] = "@(#)getpwent.c 5.21 (Berkeley) 3/14/91";
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
50static struct passwd _pw_passwd; /* password structure */
51static DB *_pw_db; /* password database */
52static int _pw_keynum; /* key counter */
53static int _pw_stayopen; /* keep fd's open */
54static int __hashpw(), __initdb();
55
56struct passwd *
57getpwent()
58{
59 DBT key;
60 char bf[sizeof(_pw_keynum) + 1];
61
62 if (!_pw_db && !__initdb())
63 return((struct passwd *)NULL);
64
65 ++_pw_keynum;
66 bf[0] = _PW_KEYBYNUM;
67 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
68 key.data = (u_char *)bf;
69 key.size = sizeof(_pw_keynum) + 1;
70 return(__hashpw(&key) ? &_pw_passwd : (struct passwd *)NULL);
71}
72
73struct passwd *
74getpwnam(name)
75 const char *name;
76{
77 DBT key;
78 int len, rval;
79 char bf[UT_NAMESIZE + 1];
80
81 if (!_pw_db && !__initdb())
82 return((struct passwd *)NULL);
83
84 bf[0] = _PW_KEYBYNAME;
85 len = strlen(name);
86 bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
87 key.data = (u_char *)bf;
88 key.size = len + 1;
89 rval = __hashpw(&key);
90
91 if (!_pw_stayopen) {
92 (void)(_pw_db->close)(_pw_db);
93 _pw_db = (DB *)NULL;
94 }
95 return(rval ? &_pw_passwd : (struct passwd *)NULL);
96}
97
98struct passwd *
99#ifdef __STDC__
100getpwuid(uid_t uid)
101#else
102getpwuid(uid)
103 int uid;
104#endif
105{
106 DBT key;
107 int keyuid, rval;
108 char bf[sizeof(keyuid) + 1];
109
110 if (!_pw_db && !__initdb())
111 return((struct passwd *)NULL);
112
113 bf[0] = _PW_KEYBYUID;
114 keyuid = uid;
115 bcopy(&keyuid, bf + 1, sizeof(keyuid));
116 key.data = (u_char *)bf;
117 key.size = sizeof(keyuid) + 1;
118 rval = __hashpw(&key);
119
120 if (!_pw_stayopen) {
121 (void)(_pw_db->close)(_pw_db);
122 _pw_db = (DB *)NULL;
123 }
124 return(rval ? &_pw_passwd : (struct passwd *)NULL);
125}
126
127int
128setpassent(stayopen)
129 int stayopen;
130{
131 _pw_keynum = 0;
132 _pw_stayopen = stayopen;
133 return(1);
134}
135
136int
137setpwent()
138{
139 _pw_keynum = 0;
140 _pw_stayopen = 0;
141 return(1);
142}
143
144void
145endpwent()
146{
147 _pw_keynum = 0;
148 if (_pw_db) {
149 (void)(_pw_db->close)(_pw_db);
150 _pw_db = (DB *)NULL;
151 }
152}
153
154static
155__initdb()
156{
157 static int warned;
158 char *p;
159
160 p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
28e48405 161 _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
15637ed4
RG
162 if (_pw_db)
163 return(1);
164 if (!warned)
165 syslog(LOG_ERR, "%s: %m", p);
166 return(0);
167}
168
169static
170__hashpw(key)
171 DBT *key;
172{
173 register char *p, *t;
174 static u_int max;
175 static char *line;
176 DBT data;
177
178 if ((_pw_db->get)(_pw_db, key, &data, 0))
179 return(0);
180 p = (char *)data.data;
181 if (data.size > max && !(line = realloc(line, max += 1024)))
182 return(0);
183
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);
200 return(1);
201}