Detect FPU by checking CPUID features.
[dragonfly.git] / contrib / bind-9.5.2 / lib / bind / irs / lcl_pw.c
blobffca5541bbfe96f3adae283efba344ac99c838b4
1 /*
2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
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.
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.
35 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36 * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
45 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static const char rcsid[] = "$Id: lcl_pw.c,v 1.3 2005/04/27 04:56:31 sra Exp $";
53 #endif /* LIBC_SCCS and not lint */
55 /* Extern */
57 #include "port_before.h"
59 #ifndef WANT_IRS_PW
60 static int __bind_irs_pw_unneeded;
61 #else
63 #include <sys/param.h>
64 #include <sys/types.h>
65 #include <netinet/in.h>
66 #include <arpa/nameser.h>
67 #include <resolv.h>
69 #include <db.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <limits.h>
73 #include <pwd.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <syslog.h>
77 #include <utmp.h>
78 #include <unistd.h>
80 #include <isc/memcluster.h>
81 #include <irs.h>
83 #include "port_after.h"
85 #include "irs_p.h"
86 #include "lcl_p.h"
88 /*! \file
89 * \brief
90 * The lookup techniques and data extraction code here must be kept
91 * in sync with that in `pwd_mkdb'.
95 /* Types */
97 struct pvt {
98 struct passwd passwd; /*%< password structure */
99 DB *pw_db; /*%< password database */
100 int pw_keynum; /*%< key counter */
101 int warned;
102 u_int max;
103 char * line;
106 /* Forward */
108 static void pw_close(struct irs_pw *);
109 static struct passwd * pw_next(struct irs_pw *);
110 static struct passwd * pw_byname(struct irs_pw *, const char *);
111 static struct passwd * pw_byuid(struct irs_pw *, uid_t);
112 static void pw_rewind(struct irs_pw *);
113 static void pw_minimize(struct irs_pw *);
115 static int initdb(struct pvt *);
116 static int hashpw(struct irs_pw *, DBT *);
118 /* Public */
119 struct irs_pw *
120 irs_lcl_pw(struct irs_acc *this) {
121 struct irs_pw *pw;
122 struct pvt *pvt;
124 UNUSED(this);
126 if (!(pw = memget(sizeof *pw))) {
127 errno = ENOMEM;
128 return (NULL);
130 memset(pw, 0x5e, sizeof *pw);
131 if (!(pvt = memget(sizeof *pvt))) {
132 free(pw);
133 errno = ENOMEM;
134 return (NULL);
136 memset(pvt, 0, sizeof *pvt);
137 pw->private = pvt;
138 pw->close = pw_close;
139 pw->next = pw_next;
140 pw->byname = pw_byname;
141 pw->byuid = pw_byuid;
142 pw->rewind = pw_rewind;
143 pw->minimize = pw_minimize;
144 pw->res_get = NULL;
145 pw->res_set = NULL;
146 return (pw);
149 /* Methods */
151 static void
152 pw_close(struct irs_pw *this) {
153 struct pvt *pvt = (struct pvt *)this->private;
155 if (pvt->pw_db) {
156 (void)(pvt->pw_db->close)(pvt->pw_db);
157 pvt->pw_db = NULL;
159 if (pvt->line)
160 memput(pvt->line, pvt->max);
161 memput(pvt, sizeof *pvt);
162 memput(this, sizeof *this);
165 static struct passwd *
166 pw_next(struct irs_pw *this) {
167 struct pvt *pvt = (struct pvt *)this->private;
169 DBT key;
170 char bf[sizeof(pvt->pw_keynum) + 1];
172 if (!initdb(pvt))
173 return (NULL);
175 ++pvt->pw_keynum;
176 bf[0] = _PW_KEYBYNUM;
177 memcpy(bf + 1, (char *)&pvt->pw_keynum, sizeof(pvt->pw_keynum));
178 key.data = (u_char *)bf;
179 key.size = sizeof(pvt->pw_keynum) + 1;
180 return (hashpw(this, &key) ? &pvt->passwd : NULL);
183 static struct passwd *
184 pw_byname(struct irs_pw *this, const char *name) {
185 struct pvt *pvt = (struct pvt *)this->private;
186 DBT key;
187 int len, rval;
188 char bf[UT_NAMESIZE + 1];
190 if (!initdb(pvt))
191 return (NULL);
193 bf[0] = _PW_KEYBYNAME;
194 len = strlen(name);
195 memcpy(bf + 1, name, MIN(len, UT_NAMESIZE));
196 key.data = (u_char *)bf;
197 key.size = len + 1;
198 rval = hashpw(this, &key);
200 return (rval ? &pvt->passwd : NULL);
204 static struct passwd *
205 pw_byuid(struct irs_pw *this, uid_t uid) {
206 struct pvt *pvt = (struct pvt *)this->private;
207 DBT key;
208 int keyuid, rval;
209 char bf[sizeof(keyuid) + 1];
211 if (!initdb(pvt))
212 return (NULL);
214 bf[0] = _PW_KEYBYUID;
215 keyuid = uid;
216 memcpy(bf + 1, &keyuid, sizeof(keyuid));
217 key.data = (u_char *)bf;
218 key.size = sizeof(keyuid) + 1;
219 rval = hashpw(this, &key);
221 return (rval ? &pvt->passwd : NULL);
224 static void
225 pw_rewind(struct irs_pw *this) {
226 struct pvt *pvt = (struct pvt *)this->private;
228 pvt->pw_keynum = 0;
231 static void
232 pw_minimize(struct irs_pw *this) {
233 struct pvt *pvt = (struct pvt *)this->private;
235 if (pvt->pw_db != NULL) {
236 (void) (*pvt->pw_db->close)(pvt->pw_db);
237 pvt->pw_db = NULL;
241 /* Private. */
243 static int
244 initdb(struct pvt *pvt) {
245 const char *p;
247 if (pvt->pw_db) {
248 if (lseek((*pvt->pw_db->fd)(pvt->pw_db), 0L, SEEK_CUR) >= 0L)
249 return (1);
250 else
251 (void) (*pvt->pw_db->close)(pvt->pw_db);
253 pvt->pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL);
254 if (!pvt->pw_db)
255 pvt->pw_db = dbopen((p =_PATH_MP_DB), O_RDONLY,
256 0, DB_HASH, NULL);
257 if (pvt->pw_db)
258 return (1);
259 if (!pvt->warned) {
260 syslog(LOG_ERR, "%s: %m", p);
261 pvt->warned++;
263 return (0);
266 static int
267 hashpw(struct irs_pw *this, DBT *key) {
268 struct pvt *pvt = (struct pvt *)this->private;
269 char *p, *t, *l;
270 DBT data;
272 if ((pvt->pw_db->get)(pvt->pw_db, key, &data, 0))
273 return (0);
274 p = (char *)data.data;
275 if (data.size > pvt->max) {
276 size_t newlen = pvt->max + 1024;
277 char *p = memget(newlen);
278 if (p == NULL) {
279 return (0);
281 if (pvt->line != NULL) {
282 memcpy(p, pvt->line, pvt->max);
283 memput(pvt->line, pvt->max);
285 pvt->max = newlen;
286 pvt->line = p;
289 /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
290 t = pvt->line;
291 l = pvt->line + pvt->max;
292 #define EXPAND(e) if ((e = t) == NULL) return (0); else \
293 do if (t >= l) return (0); while ((*t++ = *p++) != '\0')
294 #define SCALAR(v) if (t + sizeof v >= l) return (0); else \
295 (memmove(&(v), p, sizeof v), p += sizeof v)
296 EXPAND(pvt->passwd.pw_name);
297 EXPAND(pvt->passwd.pw_passwd);
298 SCALAR(pvt->passwd.pw_uid);
299 SCALAR(pvt->passwd.pw_gid);
300 SCALAR(pvt->passwd.pw_change);
301 EXPAND(pvt->passwd.pw_class);
302 EXPAND(pvt->passwd.pw_gecos);
303 EXPAND(pvt->passwd.pw_dir);
304 EXPAND(pvt->passwd.pw_shell);
305 SCALAR(pvt->passwd.pw_expire);
306 return (1);
309 #endif /* WANT_IRS_PW */