Merge from vendor branch SENDMAIL:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / lcl_pw.c
1 /*
2  * Copyright (c) 1989, 1993, 1995
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 /*
35  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36  * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
37  *
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.
41  *
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.
49  */
50
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static const char rcsid[] = "$Id: lcl_pw.c,v 1.1.2.1 2004/03/09 09:17:33 marka Exp $";
53 #endif /* LIBC_SCCS and not lint */
54
55 /* Extern */
56
57 #include "port_before.h"
58
59 #ifndef WANT_IRS_PW
60 static int __bind_irs_pw_unneeded;
61 #else
62
63 #include <sys/param.h>
64 #include <sys/types.h>
65 #include <netinet/in.h>
66 #include <arpa/nameser.h>
67 #include <resolv.h>
68
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>
79
80 #include <isc/memcluster.h>
81 #include <irs.h>
82
83 #include "port_after.h"
84
85 #include "irs_p.h"
86 #include "lcl_p.h"
87
88 /*
89  * The lookup techniques and data extraction code here must be kept
90  * in sync with that in `pwd_mkdb'.
91  */
92  
93
94 /* Types */
95
96 struct  pvt {
97         struct passwd   passwd;         /* password structure */
98         DB              *pw_db;         /* password database */
99         int             pw_keynum;      /* key counter */
100         int             warned;
101         u_int           max;
102         char *          line;
103 };
104
105 /* Forward */
106
107 static void                     pw_close(struct irs_pw *);
108 static struct passwd *          pw_next(struct irs_pw *);
109 static struct passwd *          pw_byname(struct irs_pw *, const char *);
110 static struct passwd *          pw_byuid(struct irs_pw *, uid_t);
111 static void                     pw_rewind(struct irs_pw *);
112 static void                     pw_minimize(struct irs_pw *);
113
114 static int                      initdb(struct pvt *);
115 static int                      hashpw(struct irs_pw *, DBT *);
116
117 /* Public */
118 struct irs_pw *
119 irs_lcl_pw(struct irs_acc *this) {
120         struct irs_pw *pw;
121         struct pvt *pvt;
122
123         UNUSED(this);
124                  
125         if (!(pw = memget(sizeof *pw))) {
126                 errno = ENOMEM;
127                 return (NULL);
128         }
129         memset(pw, 0x5e, sizeof *pw);
130         if (!(pvt = memget(sizeof *pvt))) {
131                 free(pw);
132                 errno = ENOMEM;
133                 return (NULL);
134         }
135         memset(pvt, 0, sizeof *pvt);
136         pw->private = pvt;
137         pw->close = pw_close;
138         pw->next = pw_next;
139         pw->byname = pw_byname;
140         pw->byuid = pw_byuid;
141         pw->rewind = pw_rewind;
142         pw->minimize = pw_minimize;
143         pw->res_get = NULL;
144         pw->res_set = NULL;
145         return (pw);
146 }
147
148 /* Methods */
149
150 static void
151 pw_close(struct irs_pw *this) {
152         struct pvt *pvt = (struct pvt *)this->private;
153         
154         if (pvt->pw_db) {
155                 (void)(pvt->pw_db->close)(pvt->pw_db);
156                 pvt->pw_db = NULL;
157         }
158         if (pvt->line)
159                 memput(pvt->line, pvt->max);
160         memput(pvt, sizeof *pvt);
161         memput(this, sizeof *this);
162 }
163
164 static struct passwd *
165 pw_next(struct irs_pw *this) {
166         struct pvt *pvt = (struct pvt *)this->private;
167         
168         DBT key;
169         char bf[sizeof(pvt->pw_keynum) + 1];
170  
171         if (!initdb(pvt))
172                 return (NULL);
173  
174         ++pvt->pw_keynum;
175         bf[0] = _PW_KEYBYNUM;
176         memcpy(bf + 1, (char *)&pvt->pw_keynum, sizeof(pvt->pw_keynum));
177         key.data = (u_char *)bf;
178         key.size = sizeof(pvt->pw_keynum) + 1;
179         return (hashpw(this, &key) ? &pvt->passwd : NULL);
180 }
181  
182 static struct passwd *
183 pw_byname(struct irs_pw *this, const char *name) {
184         struct pvt *pvt = (struct pvt *)this->private;
185         DBT key;
186         int len, rval;
187         char bf[UT_NAMESIZE + 1];
188  
189         if (!initdb(pvt))
190                 return (NULL);
191  
192         bf[0] = _PW_KEYBYNAME;
193         len = strlen(name);
194         memcpy(bf + 1, name, MIN(len, UT_NAMESIZE));
195         key.data = (u_char *)bf;
196         key.size = len + 1;
197         rval = hashpw(this, &key);
198  
199         return (rval ? &pvt->passwd : NULL);
200 }
201  
202
203 static struct passwd *
204 pw_byuid(struct irs_pw *this, uid_t uid) {
205         struct pvt *pvt = (struct pvt *)this->private;
206         DBT key;
207         int keyuid, rval;
208         char bf[sizeof(keyuid) + 1];
209  
210         if (!initdb(pvt))
211                 return (NULL);
212  
213         bf[0] = _PW_KEYBYUID;
214         keyuid = uid;
215         memcpy(bf + 1, &keyuid, sizeof(keyuid));
216         key.data = (u_char *)bf;
217         key.size = sizeof(keyuid) + 1;
218         rval = hashpw(this, &key);
219  
220         return (rval ? &pvt->passwd : NULL);
221 }
222
223 static void
224 pw_rewind(struct irs_pw *this) {
225         struct pvt *pvt = (struct pvt *)this->private;
226
227         pvt->pw_keynum = 0;
228 }
229
230 static void
231 pw_minimize(struct irs_pw *this) {
232         struct pvt *pvt = (struct pvt *)this->private;
233
234         if (pvt->pw_db != NULL) {
235                 (void) (*pvt->pw_db->close)(pvt->pw_db);
236                 pvt->pw_db = NULL;
237         }
238 }
239
240 /* Private. */
241
242 static int
243 initdb(struct pvt *pvt) {
244         const char *p;
245
246         if (pvt->pw_db) {
247                 if (lseek((*pvt->pw_db->fd)(pvt->pw_db), 0L, SEEK_CUR) >= 0L)
248                         return (1);
249                 else
250                         (void) (*pvt->pw_db->close)(pvt->pw_db);
251         }
252         pvt->pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL);
253         if (!pvt->pw_db)
254                 pvt->pw_db = dbopen((p =_PATH_MP_DB), O_RDONLY,
255                                     0, DB_HASH, NULL);
256         if (pvt->pw_db)
257                 return (1);
258         if (!pvt->warned) {
259                 syslog(LOG_ERR, "%s: %m", p);
260                 pvt->warned++;
261         }
262         return (0);
263 }
264
265 static int
266 hashpw(struct irs_pw *this, DBT *key) {
267         struct pvt *pvt = (struct pvt *)this->private;
268         char *p, *t, *l;
269         DBT data;
270  
271         if ((pvt->pw_db->get)(pvt->pw_db, key, &data, 0))
272                 return (0);
273         p = (char *)data.data;
274         if (data.size > pvt->max) {
275                 size_t newlen = pvt->max + 1024;
276                 char *p = memget(newlen);
277                 if (p == NULL) {
278                         return (0);
279                 }
280                 if (pvt->line != NULL) {
281                         memcpy(p, pvt->line, pvt->max);
282                         memput(pvt->line, pvt->max);
283                 }
284                 pvt->max = newlen;
285                 pvt->line = p;
286         }
287
288         /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
289         t = pvt->line;
290         l = pvt->line + pvt->max;
291 #define EXPAND(e) if ((e = t) == NULL) return (0); else \
292                   do if (t >= l) return (0); while ((*t++ = *p++) != '\0')
293 #define SCALAR(v) if (t + sizeof v >= l) return (0); else \
294                   (memmove(&(v), p, sizeof v), p += sizeof v)
295         EXPAND(pvt->passwd.pw_name);
296         EXPAND(pvt->passwd.pw_passwd);
297         SCALAR(pvt->passwd.pw_uid);
298         SCALAR(pvt->passwd.pw_gid);
299         SCALAR(pvt->passwd.pw_change);
300         EXPAND(pvt->passwd.pw_class);
301         EXPAND(pvt->passwd.pw_gecos);
302         EXPAND(pvt->passwd.pw_dir);
303         EXPAND(pvt->passwd.pw_shell);
304         SCALAR(pvt->passwd.pw_expire);
305         return (1);
306 }
307
308 #endif /* WANT_IRS_PW */