xref: /netbsd-src/external/bsd/libbind/dist/irs/lcl_pw.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: lcl_pw.c,v 1.1.1.2 2012/09/09 16:07:58 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
38  * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
39  *
40  * Permission to use, copy, modify, and distribute this software for any
41  * purpose with or without fee is hereby granted, provided that the above
42  * copyright notice and this permission notice appear in all copies.
43  *
44  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
45  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
47  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
50  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51  */
52 
53 #if defined(LIBC_SCCS) && !defined(lint)
54 static const char rcsid[] = "Id: lcl_pw.c,v 1.3 2005/04/27 04:56:31 sra Exp ";
55 #endif /* LIBC_SCCS and not lint */
56 
57 /* Extern */
58 
59 #include "port_before.h"
60 
61 #ifndef WANT_IRS_PW
62 static int __bind_irs_pw_unneeded;
63 #else
64 
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
69 #include <resolv.h>
70 
71 #include <db.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <limits.h>
75 #include <pwd.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <syslog.h>
79 #include <utmp.h>
80 #include <unistd.h>
81 
82 #include <isc/memcluster.h>
83 #include <irs.h>
84 
85 #include "port_after.h"
86 
87 #include "irs_p.h"
88 #include "lcl_p.h"
89 
90 /*! \file
91  * \brief
92  * The lookup techniques and data extraction code here must be kept
93  * in sync with that in `pwd_mkdb'.
94  */
95 
96 
97 /* Types */
98 
99 struct  pvt {
100 	struct passwd	passwd;		/*%< password structure */
101 	DB 		*pw_db;		/*%< password database */
102 	int		pw_keynum;	/*%< key counter */
103 	int		warned;
104 	u_int		max;
105 	char *		line;
106 };
107 
108 /* Forward */
109 
110 static void			pw_close(struct irs_pw *);
111 static struct passwd *		pw_next(struct irs_pw *);
112 static struct passwd *		pw_byname(struct irs_pw *, const char *);
113 static struct passwd *		pw_byuid(struct irs_pw *, uid_t);
114 static void			pw_rewind(struct irs_pw *);
115 static void			pw_minimize(struct irs_pw *);
116 
117 static int			initdb(struct pvt *);
118 static int			hashpw(struct irs_pw *, DBT *);
119 
120 /* Public */
121 struct irs_pw *
irs_lcl_pw(struct irs_acc * this)122 irs_lcl_pw(struct irs_acc *this) {
123 	struct irs_pw *pw;
124 	struct pvt *pvt;
125 
126 	UNUSED(this);
127 
128         if (!(pw = memget(sizeof *pw))) {
129 		errno = ENOMEM;
130 		return (NULL);
131 	}
132 	memset(pw, 0x5e, sizeof *pw);
133 	if (!(pvt = memget(sizeof *pvt))) {
134 		free(pw);
135 		errno = ENOMEM;
136 		return (NULL);
137 	}
138 	memset(pvt, 0, sizeof *pvt);
139 	pw->private = pvt;
140 	pw->close = pw_close;
141 	pw->next = pw_next;
142 	pw->byname = pw_byname;
143 	pw->byuid = pw_byuid;
144 	pw->rewind = pw_rewind;
145 	pw->minimize = pw_minimize;
146 	pw->res_get = NULL;
147 	pw->res_set = NULL;
148 	return (pw);
149 }
150 
151 /* Methods */
152 
153 static void
pw_close(struct irs_pw * this)154 pw_close(struct irs_pw *this) {
155 	struct pvt *pvt = (struct pvt *)this->private;
156 
157 	if (pvt->pw_db) {
158 		(void)(pvt->pw_db->close)(pvt->pw_db);
159 		pvt->pw_db = NULL;
160 	}
161 	if (pvt->line)
162 		memput(pvt->line, pvt->max);
163 	memput(pvt, sizeof *pvt);
164 	memput(this, sizeof *this);
165 }
166 
167 static struct passwd *
pw_next(struct irs_pw * this)168 pw_next(struct irs_pw *this) {
169 	struct pvt *pvt = (struct pvt *)this->private;
170 
171 	DBT key;
172 	char bf[sizeof(pvt->pw_keynum) + 1];
173 
174 	if (!initdb(pvt))
175 		return (NULL);
176 
177 	++pvt->pw_keynum;
178 	bf[0] = _PW_KEYBYNUM;
179 	memcpy(bf + 1, (char *)&pvt->pw_keynum, sizeof(pvt->pw_keynum));
180 	key.data = (u_char *)bf;
181 	key.size = sizeof(pvt->pw_keynum) + 1;
182 	return (hashpw(this, &key) ? &pvt->passwd : NULL);
183 }
184 
185 static struct passwd *
pw_byname(struct irs_pw * this,const char * name)186 pw_byname(struct irs_pw *this, const char *name) {
187 	struct pvt *pvt = (struct pvt *)this->private;
188 	DBT key;
189 	int len, rval;
190 	char bf[UT_NAMESIZE + 1];
191 
192 	if (!initdb(pvt))
193 		return (NULL);
194 
195 	bf[0] = _PW_KEYBYNAME;
196 	len = strlen(name);
197 	memcpy(bf + 1, name, MIN(len, UT_NAMESIZE));
198 	key.data = (u_char *)bf;
199 	key.size = len + 1;
200 	rval = hashpw(this, &key);
201 
202 	return (rval ? &pvt->passwd : NULL);
203 }
204 
205 
206 static struct passwd *
pw_byuid(struct irs_pw * this,uid_t uid)207 pw_byuid(struct irs_pw *this, uid_t uid) {
208 	struct pvt *pvt = (struct pvt *)this->private;
209 	DBT key;
210 	int keyuid, rval;
211 	char bf[sizeof(keyuid) + 1];
212 
213 	if (!initdb(pvt))
214 		return (NULL);
215 
216 	bf[0] = _PW_KEYBYUID;
217 	keyuid = uid;
218 	memcpy(bf + 1, &keyuid, sizeof(keyuid));
219 	key.data = (u_char *)bf;
220 	key.size = sizeof(keyuid) + 1;
221 	rval = hashpw(this, &key);
222 
223 	return (rval ? &pvt->passwd : NULL);
224 }
225 
226 static void
pw_rewind(struct irs_pw * this)227 pw_rewind(struct irs_pw *this) {
228 	struct pvt *pvt = (struct pvt *)this->private;
229 
230 	pvt->pw_keynum = 0;
231 }
232 
233 static void
pw_minimize(struct irs_pw * this)234 pw_minimize(struct irs_pw *this) {
235 	struct pvt *pvt = (struct pvt *)this->private;
236 
237 	if (pvt->pw_db != NULL) {
238 		(void) (*pvt->pw_db->close)(pvt->pw_db);
239 		pvt->pw_db = NULL;
240 	}
241 }
242 
243 /* Private. */
244 
245 static int
initdb(struct pvt * pvt)246 initdb(struct pvt *pvt) {
247 	const char *p;
248 
249 	if (pvt->pw_db) {
250 		if (lseek((*pvt->pw_db->fd)(pvt->pw_db), 0L, SEEK_CUR) >= 0L)
251 			return (1);
252 		else
253 			(void) (*pvt->pw_db->close)(pvt->pw_db);
254 	}
255 	pvt->pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL);
256 	if (!pvt->pw_db)
257 		pvt->pw_db = dbopen((p =_PATH_MP_DB), O_RDONLY,
258 				    0, DB_HASH, NULL);
259 	if (pvt->pw_db)
260 		return (1);
261 	if (!pvt->warned) {
262 		syslog(LOG_ERR, "%s: %m", p);
263 		pvt->warned++;
264 	}
265 	return (0);
266 }
267 
268 static int
hashpw(struct irs_pw * this,DBT * key)269 hashpw(struct irs_pw *this, DBT *key) {
270 	struct pvt *pvt = (struct pvt *)this->private;
271 	char *p, *t, *l;
272 	DBT data;
273 
274 	if ((pvt->pw_db->get)(pvt->pw_db, key, &data, 0))
275 		return (0);
276 	p = (char *)data.data;
277 	if (data.size > pvt->max) {
278 		size_t newlen = pvt->max + 1024;
279 		char *p = memget(newlen);
280 		if (p == NULL) {
281 			return (0);
282 		}
283 		if (pvt->line != NULL) {
284 			memcpy(p, pvt->line, pvt->max);
285 			memput(pvt->line, pvt->max);
286 		}
287 		pvt->max = newlen;
288 		pvt->line = p;
289 	}
290 
291 	/* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
292 	t = pvt->line;
293 	l = pvt->line + pvt->max;
294 #define EXPAND(e) if ((e = t) == NULL) return (0); else \
295 		  do if (t >= l) return (0); while ((*t++ = *p++) != '\0')
296 #define SCALAR(v) if (t + sizeof v >= l) return (0); else \
297 		  (memmove(&(v), p, sizeof v), p += sizeof v)
298 	EXPAND(pvt->passwd.pw_name);
299 	EXPAND(pvt->passwd.pw_passwd);
300 	SCALAR(pvt->passwd.pw_uid);
301 	SCALAR(pvt->passwd.pw_gid);
302 	SCALAR(pvt->passwd.pw_change);
303 	EXPAND(pvt->passwd.pw_class);
304 	EXPAND(pvt->passwd.pw_gecos);
305 	EXPAND(pvt->passwd.pw_dir);
306 	EXPAND(pvt->passwd.pw_shell);
307 	SCALAR(pvt->passwd.pw_expire);
308 	return (1);
309 }
310 
311 #endif /* WANT_IRS_PW */
312