getgrent.c revision 1.46 1 1.46 simonb /* $NetBSD: getgrent.c,v 1.46 2003/02/17 00:11:54 simonb Exp $ */
2 1.11 cgd
3 1.1 cgd /*
4 1.11 cgd * Copyright (c) 1989, 1993
5 1.11 cgd * The Regents of the University of California. All rights reserved.
6 1.13 phil * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.20 christos #include <sys/cdefs.h>
38 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
39 1.11 cgd #if 0
40 1.11 cgd static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
41 1.11 cgd #else
42 1.46 simonb __RCSID("$NetBSD: getgrent.c,v 1.46 2003/02/17 00:11:54 simonb Exp $");
43 1.11 cgd #endif
44 1.1 cgd #endif /* LIBC_SCCS and not lint */
45 1.1 cgd
46 1.21 jtc #include "namespace.h"
47 1.33 lukem
48 1.1 cgd #include <sys/types.h>
49 1.33 lukem
50 1.37 lukem #include <assert.h>
51 1.33 lukem #include <errno.h>
52 1.27 lukem #include <grp.h>
53 1.18 lukem #include <limits.h>
54 1.27 lukem #include <nsswitch.h>
55 1.1 cgd #include <stdio.h>
56 1.1 cgd #include <stdlib.h>
57 1.2 deraadt #include <string.h>
58 1.27 lukem #include <syslog.h>
59 1.35 lukem
60 1.35 lukem #include <stdarg.h>
61 1.33 lukem
62 1.27 lukem #ifdef HESIOD
63 1.27 lukem #include <hesiod.h>
64 1.27 lukem #endif
65 1.2 deraadt #ifdef YP
66 1.2 deraadt #include <rpc/rpc.h>
67 1.2 deraadt #include <rpcsvc/yp_prot.h>
68 1.2 deraadt #include <rpcsvc/ypclnt.h>
69 1.21 jtc #endif
70 1.21 jtc
71 1.34 lukem #if defined(YP) || defined(HESIOD)
72 1.34 lukem #define _GROUP_COMPAT
73 1.34 lukem #endif
74 1.34 lukem
75 1.45 elric struct group *_getgrent_user(const char *);
76 1.45 elric
77 1.21 jtc #ifdef __weak_alias
78 1.39 mycroft __weak_alias(endgrent,_endgrent)
79 1.39 mycroft __weak_alias(getgrent,_getgrent)
80 1.39 mycroft __weak_alias(getgrgid,_getgrgid)
81 1.39 mycroft __weak_alias(getgrnam,_getgrnam)
82 1.39 mycroft __weak_alias(setgrent,_setgrent)
83 1.39 mycroft __weak_alias(setgroupent,_setgroupent)
84 1.2 deraadt #endif
85 1.1 cgd
86 1.27 lukem static FILE *_gr_fp;
87 1.27 lukem static struct group _gr_group;
88 1.27 lukem static int _gr_stayopen;
89 1.36 lukem static int _gr_filesdone;
90 1.27 lukem
91 1.41 lukem static void grcleanup(void);
92 1.45 elric static int grscan(int, gid_t, const char *, const char *);
93 1.42 lukem static int grstart(void);
94 1.45 elric static int grmatchline(int, gid_t, const char *, const char *);
95 1.1 cgd
96 1.1 cgd #define MAXGRP 200
97 1.1 cgd #define MAXLINELENGTH 1024
98 1.27 lukem
99 1.27 lukem static __aconst char *members[MAXGRP];
100 1.27 lukem static char line[MAXLINELENGTH];
101 1.1 cgd
102 1.2 deraadt #ifdef YP
103 1.34 lukem static char *__ypcurrent, *__ypdomain;
104 1.34 lukem static int __ypcurrentlen;
105 1.36 lukem static int _gr_ypdone;
106 1.27 lukem #endif
107 1.27 lukem
108 1.27 lukem #ifdef HESIOD
109 1.45 elric static int _gr_hesnum;
110 1.45 elric static struct group *_gr_hesgrplist = NULL;
111 1.45 elric static int _gr_hesgrplistnum;
112 1.45 elric static int _gr_hesgrplistmax;
113 1.2 deraadt #endif
114 1.2 deraadt
115 1.34 lukem #ifdef _GROUP_COMPAT
116 1.34 lukem enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
117 1.34 lukem static enum _grmode __grmode;
118 1.34 lukem #endif
119 1.34 lukem
120 1.1 cgd struct group *
121 1.41 lukem getgrent(void)
122 1.1 cgd {
123 1.41 lukem
124 1.45 elric if ((!_gr_fp && !grstart()) || !grscan(0, 0, NULL, NULL))
125 1.45 elric return (NULL);
126 1.45 elric return &_gr_group;
127 1.45 elric }
128 1.45 elric
129 1.45 elric /*
130 1.46 simonb * _getgrent_user() is designed only to be called by getgrouplist(3) and
131 1.45 elric * hence makes no guarantees about filling the entire structure that it
132 1.45 elric * returns. It may only fill in the group name and gid fields.
133 1.45 elric */
134 1.45 elric
135 1.45 elric struct group *
136 1.45 elric _getgrent_user(const char *user)
137 1.45 elric {
138 1.45 elric
139 1.45 elric if ((!_gr_fp && !grstart()) || !grscan(0, 0, NULL, user))
140 1.36 lukem return (NULL);
141 1.27 lukem return &_gr_group;
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd struct group *
145 1.41 lukem getgrnam(const char *name)
146 1.1 cgd {
147 1.1 cgd int rval;
148 1.1 cgd
149 1.37 lukem _DIAGASSERT(name != NULL);
150 1.37 lukem
151 1.42 lukem if (!grstart())
152 1.27 lukem return NULL;
153 1.45 elric rval = grscan(1, 0, name, NULL);
154 1.1 cgd if (!_gr_stayopen)
155 1.1 cgd endgrent();
156 1.27 lukem return (rval) ? &_gr_group : NULL;
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd struct group *
160 1.41 lukem getgrgid(gid_t gid)
161 1.1 cgd {
162 1.1 cgd int rval;
163 1.1 cgd
164 1.42 lukem if (!grstart())
165 1.27 lukem return NULL;
166 1.45 elric rval = grscan(1, gid, NULL, NULL);
167 1.1 cgd if (!_gr_stayopen)
168 1.1 cgd endgrent();
169 1.27 lukem return (rval) ? &_gr_group : NULL;
170 1.1 cgd }
171 1.1 cgd
172 1.36 lukem void
173 1.41 lukem grcleanup(void)
174 1.1 cgd {
175 1.41 lukem
176 1.36 lukem _gr_filesdone = 0;
177 1.27 lukem #ifdef YP
178 1.27 lukem if (__ypcurrent)
179 1.27 lukem free(__ypcurrent);
180 1.27 lukem __ypcurrent = NULL;
181 1.36 lukem _gr_ypdone = 0;
182 1.27 lukem #endif
183 1.27 lukem #ifdef HESIOD
184 1.36 lukem _gr_hesnum = 0;
185 1.45 elric if (!_gr_hesgrplist)
186 1.45 elric free(_gr_hesgrplist);
187 1.45 elric _gr_hesgrplist = NULL;
188 1.45 elric _gr_hesgrplistnum = -1;
189 1.45 elric _gr_hesgrplistmax = 0;
190 1.27 lukem #endif
191 1.34 lukem #ifdef _GROUP_COMPAT
192 1.34 lukem __grmode = GRMODE_NONE;
193 1.34 lukem #endif
194 1.36 lukem }
195 1.36 lukem
196 1.36 lukem static int
197 1.42 lukem grstart(void)
198 1.36 lukem {
199 1.41 lukem
200 1.36 lukem grcleanup();
201 1.1 cgd if (_gr_fp) {
202 1.1 cgd rewind(_gr_fp);
203 1.27 lukem return 1;
204 1.1 cgd }
205 1.27 lukem return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
206 1.1 cgd }
207 1.1 cgd
208 1.5 jtc void
209 1.41 lukem setgrent(void)
210 1.1 cgd {
211 1.41 lukem
212 1.5 jtc (void) setgroupent(0);
213 1.1 cgd }
214 1.1 cgd
215 1.1 cgd int
216 1.41 lukem setgroupent(int stayopen)
217 1.1 cgd {
218 1.41 lukem
219 1.42 lukem if (!grstart())
220 1.27 lukem return 0;
221 1.1 cgd _gr_stayopen = stayopen;
222 1.27 lukem return 1;
223 1.1 cgd }
224 1.1 cgd
225 1.1 cgd void
226 1.41 lukem endgrent(void)
227 1.1 cgd {
228 1.41 lukem
229 1.36 lukem grcleanup();
230 1.1 cgd if (_gr_fp) {
231 1.1 cgd (void)fclose(_gr_fp);
232 1.1 cgd _gr_fp = NULL;
233 1.1 cgd }
234 1.1 cgd }
235 1.1 cgd
236 1.27 lukem
237 1.41 lukem static int _local_grscan(void *, void *, va_list);
238 1.27 lukem
239 1.29 christos /*ARGSUSED*/
240 1.27 lukem static int
241 1.41 lukem _local_grscan(void *rv, void *cb_data, va_list ap)
242 1.27 lukem {
243 1.27 lukem int search = va_arg(ap, int);
244 1.27 lukem gid_t gid = va_arg(ap, gid_t);
245 1.27 lukem const char *name = va_arg(ap, const char *);
246 1.45 elric const char *user = va_arg(ap, const char *);
247 1.27 lukem
248 1.36 lukem if (_gr_filesdone)
249 1.36 lukem return NS_NOTFOUND;
250 1.27 lukem for (;;) {
251 1.27 lukem if (!fgets(line, sizeof(line), _gr_fp)) {
252 1.36 lukem if (!search)
253 1.36 lukem _gr_filesdone = 1;
254 1.27 lukem return NS_NOTFOUND;
255 1.27 lukem }
256 1.27 lukem /* skip lines that are too big */
257 1.27 lukem if (!strchr(line, '\n')) {
258 1.27 lukem int ch;
259 1.27 lukem
260 1.27 lukem while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
261 1.27 lukem ;
262 1.27 lukem continue;
263 1.27 lukem }
264 1.45 elric if (grmatchline(search, gid, name, user))
265 1.27 lukem return NS_SUCCESS;
266 1.27 lukem }
267 1.27 lukem /* NOTREACHED */
268 1.27 lukem }
269 1.27 lukem
270 1.27 lukem #ifdef HESIOD
271 1.41 lukem static int _dns_grscan(void *, void *, va_list);
272 1.45 elric static int _dns_grplist(const char *);
273 1.27 lukem
274 1.29 christos /*ARGSUSED*/
275 1.2 deraadt static int
276 1.41 lukem _dns_grscan(void *rv, void *cb_data, va_list ap)
277 1.1 cgd {
278 1.27 lukem int search = va_arg(ap, int);
279 1.27 lukem gid_t gid = va_arg(ap, gid_t);
280 1.27 lukem const char *name = va_arg(ap, const char *);
281 1.45 elric const char *user = va_arg(ap, const char *);
282 1.27 lukem
283 1.27 lukem char **hp;
284 1.33 lukem void *context;
285 1.33 lukem int r;
286 1.33 lukem
287 1.33 lukem r = NS_UNAVAIL;
288 1.45 elric if (!search && user && _gr_hesgrplistmax != -1) {
289 1.45 elric r = _dns_grplist(user);
290 1.45 elric /* if we did not find user.grplist, just iterate */
291 1.45 elric if (!_gr_hesgrplist) {
292 1.45 elric _gr_hesgrplistmax = -1;
293 1.45 elric if (r != NS_NOTFOUND)
294 1.45 elric return r;
295 1.45 elric } else
296 1.45 elric return r;
297 1.45 elric }
298 1.36 lukem if (!search && _gr_hesnum == -1)
299 1.36 lukem return NS_NOTFOUND;
300 1.33 lukem if (hesiod_init(&context) == -1)
301 1.33 lukem return (r);
302 1.1 cgd
303 1.1 cgd for (;;) {
304 1.27 lukem if (search) {
305 1.27 lukem if (name)
306 1.43 itojun strlcpy(line, name, sizeof(line));
307 1.27 lukem else
308 1.28 lukem snprintf(line, sizeof(line), "%u",
309 1.28 lukem (unsigned int)gid);
310 1.27 lukem } else {
311 1.36 lukem snprintf(line, sizeof(line), "group-%u", _gr_hesnum);
312 1.36 lukem _gr_hesnum++;
313 1.27 lukem }
314 1.2 deraadt
315 1.44 elric hp = NULL;
316 1.44 elric if (search && !name) {
317 1.44 elric hp = hesiod_resolve(context, line, "gid");
318 1.44 elric if (hp == NULL && errno != ENOENT)
319 1.44 elric break;
320 1.44 elric }
321 1.44 elric if (hp == NULL)
322 1.44 elric hp = hesiod_resolve(context, line, "group");
323 1.27 lukem if (hp == NULL) {
324 1.33 lukem if (errno == ENOENT) {
325 1.36 lukem if (!search)
326 1.36 lukem _gr_hesnum = -1;
327 1.36 lukem r = NS_NOTFOUND;
328 1.2 deraadt }
329 1.33 lukem break;
330 1.27 lukem }
331 1.27 lukem
332 1.27 lukem /* only check first elem */
333 1.43 itojun strlcpy(line, hp[0], sizeof(line));
334 1.33 lukem hesiod_free_list(context, hp);
335 1.45 elric if (grmatchline(search, gid, name, user)) {
336 1.33 lukem r = NS_SUCCESS;
337 1.33 lukem break;
338 1.33 lukem } else if (search) {
339 1.33 lukem r = NS_NOTFOUND;
340 1.33 lukem break;
341 1.33 lukem }
342 1.27 lukem }
343 1.33 lukem hesiod_end(context);
344 1.33 lukem return (r);
345 1.27 lukem }
346 1.45 elric
347 1.45 elric static int
348 1.45 elric _dns_grplist(const char *user)
349 1.45 elric {
350 1.45 elric void *context;
351 1.45 elric int r;
352 1.45 elric char **hp;
353 1.45 elric char *cp;
354 1.45 elric
355 1.45 elric r = NS_UNAVAIL;
356 1.45 elric if (!_gr_hesgrplist) {
357 1.45 elric if (hesiod_init(&context) == -1)
358 1.45 elric return r;
359 1.45 elric
360 1.45 elric _gr_hesgrplistnum = -1;
361 1.45 elric hp = hesiod_resolve(context, user, "grplist");
362 1.45 elric if (!hp) {
363 1.45 elric if (errno == ENOENT)
364 1.45 elric r = NS_NOTFOUND;
365 1.45 elric hesiod_end(context);
366 1.45 elric return r;
367 1.45 elric }
368 1.45 elric
369 1.45 elric strlcpy(line, hp[0], sizeof(line));
370 1.45 elric hesiod_free_list(context, hp);
371 1.45 elric
372 1.45 elric _gr_hesgrplistmax = 0;
373 1.45 elric for (cp=line; *cp; cp++)
374 1.45 elric if (*cp == ':')
375 1.45 elric _gr_hesgrplistmax++;
376 1.45 elric _gr_hesgrplistmax /= 2;
377 1.45 elric _gr_hesgrplistmax++;
378 1.45 elric
379 1.45 elric _gr_hesgrplist = malloc(_gr_hesgrplistmax *
380 1.45 elric sizeof(*_gr_hesgrplist));
381 1.45 elric if (!_gr_hesgrplist) {
382 1.45 elric hesiod_end(context);
383 1.45 elric return NS_UNAVAIL;
384 1.45 elric }
385 1.45 elric
386 1.45 elric cp = line;
387 1.45 elric _gr_hesgrplistmax = 0;
388 1.45 elric for (;;) {
389 1.45 elric char *name;
390 1.45 elric char *num;
391 1.45 elric gid_t gid;
392 1.45 elric char *ep;
393 1.45 elric
394 1.45 elric /* XXXrcd: error handling */
395 1.45 elric if (!(name = strsep(&cp, ":")))
396 1.45 elric break;
397 1.45 elric if (!(num = strsep(&cp, ":")))
398 1.45 elric break;
399 1.45 elric gid = (gid_t) strtoul(num, &ep, 10);
400 1.45 elric if (gid > GID_MAX || *ep != '\0')
401 1.45 elric break;
402 1.45 elric
403 1.45 elric _gr_hesgrplist[_gr_hesgrplistmax].gr_name = name;
404 1.45 elric _gr_hesgrplist[_gr_hesgrplistmax].gr_gid = gid;
405 1.45 elric _gr_hesgrplistmax++;
406 1.45 elric }
407 1.45 elric
408 1.45 elric hesiod_end(context);
409 1.45 elric }
410 1.45 elric
411 1.45 elric /* we assume that _gr_hesgrplist is now defined */
412 1.45 elric if (++_gr_hesgrplistnum >= _gr_hesgrplistmax)
413 1.45 elric return NS_NOTFOUND;
414 1.45 elric
415 1.45 elric /*
416 1.45 elric * Now we copy the relevant information into _gr_group, so that
417 1.45 elric * it can be returned. Note that we only fill in the bare necessities
418 1.45 elric * as this will be used exclusively by getgrouplist(3) and we do
419 1.45 elric * not want to have to look up all of the information.
420 1.45 elric */
421 1.45 elric _gr_group.gr_name = _gr_hesgrplist[_gr_hesgrplistnum].gr_name;
422 1.45 elric _gr_group.gr_passwd = NULL;
423 1.45 elric _gr_group.gr_gid = _gr_hesgrplist[_gr_hesgrplistnum].gr_gid;
424 1.45 elric _gr_group.gr_mem = NULL;
425 1.45 elric
426 1.45 elric return NS_SUCCESS;
427 1.45 elric }
428 1.41 lukem #endif /* HESIOD */
429 1.27 lukem
430 1.27 lukem #ifdef YP
431 1.41 lukem static int _nis_grscan(void *, void *, va_list);
432 1.27 lukem
433 1.29 christos /*ARGSUSED*/
434 1.27 lukem static int
435 1.41 lukem _nis_grscan(void *rv, void *cb_data, va_list ap)
436 1.27 lukem {
437 1.27 lukem int search = va_arg(ap, int);
438 1.27 lukem gid_t gid = va_arg(ap, gid_t);
439 1.27 lukem const char *name = va_arg(ap, const char *);
440 1.45 elric const char *user = va_arg(ap, const char *);
441 1.27 lukem
442 1.27 lukem char *key, *data;
443 1.27 lukem int keylen, datalen;
444 1.27 lukem int r;
445 1.27 lukem
446 1.27 lukem if(__ypdomain == NULL) {
447 1.27 lukem switch (yp_get_default_domain(&__ypdomain)) {
448 1.27 lukem case 0:
449 1.27 lukem break;
450 1.27 lukem case YPERR_RESRC:
451 1.27 lukem return NS_TRYAGAIN;
452 1.27 lukem default:
453 1.27 lukem return NS_UNAVAIL;
454 1.27 lukem }
455 1.27 lukem }
456 1.27 lukem
457 1.27 lukem if (search) { /* specific group or gid */
458 1.27 lukem if (name)
459 1.43 itojun strlcpy(line, name, sizeof(line));
460 1.27 lukem else
461 1.28 lukem snprintf(line, sizeof(line), "%u", (unsigned int)gid);
462 1.27 lukem data = NULL;
463 1.27 lukem r = yp_match(__ypdomain,
464 1.27 lukem (name) ? "group.byname" : "group.bygid",
465 1.27 lukem line, (int)strlen(line), &data, &datalen);
466 1.27 lukem switch (r) {
467 1.27 lukem case 0:
468 1.27 lukem break;
469 1.27 lukem case YPERR_KEY:
470 1.27 lukem if (data)
471 1.27 lukem free(data);
472 1.27 lukem return NS_NOTFOUND;
473 1.27 lukem default:
474 1.27 lukem if (data)
475 1.16 lukem free(data);
476 1.27 lukem return NS_UNAVAIL;
477 1.27 lukem }
478 1.27 lukem data[datalen] = '\0'; /* clear trailing \n */
479 1.43 itojun strlcpy(line, data, sizeof(line));
480 1.27 lukem free(data);
481 1.45 elric if (grmatchline(search, gid, name, user))
482 1.27 lukem return NS_SUCCESS;
483 1.27 lukem else
484 1.27 lukem return NS_NOTFOUND;
485 1.27 lukem }
486 1.27 lukem
487 1.36 lukem /* ! search */
488 1.36 lukem if (_gr_ypdone)
489 1.36 lukem return NS_NOTFOUND;
490 1.36 lukem for (;;) {
491 1.27 lukem data = NULL;
492 1.27 lukem if(__ypcurrent) {
493 1.27 lukem key = NULL;
494 1.27 lukem r = yp_next(__ypdomain, "group.byname",
495 1.27 lukem __ypcurrent, __ypcurrentlen,
496 1.27 lukem &key, &keylen, &data, &datalen);
497 1.27 lukem free(__ypcurrent);
498 1.27 lukem switch (r) {
499 1.27 lukem case 0:
500 1.13 phil break;
501 1.27 lukem case YPERR_NOMORE:
502 1.27 lukem __ypcurrent = NULL;
503 1.27 lukem if (key)
504 1.27 lukem free(key);
505 1.27 lukem if (data)
506 1.2 deraadt free(data);
507 1.36 lukem _gr_ypdone = 1;
508 1.36 lukem return NS_NOTFOUND;
509 1.27 lukem default:
510 1.27 lukem if (key)
511 1.27 lukem free(key);
512 1.27 lukem if (data)
513 1.27 lukem free(data);
514 1.27 lukem return NS_UNAVAIL;
515 1.27 lukem }
516 1.27 lukem __ypcurrent = key;
517 1.27 lukem __ypcurrentlen = keylen;
518 1.27 lukem } else {
519 1.27 lukem if (yp_first(__ypdomain, "group.byname",
520 1.27 lukem &__ypcurrent, &__ypcurrentlen,
521 1.27 lukem &data, &datalen)) {
522 1.40 lukem if (data)
523 1.27 lukem free(data);
524 1.27 lukem return NS_UNAVAIL;
525 1.27 lukem }
526 1.27 lukem }
527 1.27 lukem data[datalen] = '\0'; /* clear trailing \n */
528 1.43 itojun strlcpy(line, data, sizeof(line));
529 1.27 lukem free(data);
530 1.45 elric if (grmatchline(search, gid, name, user))
531 1.27 lukem return NS_SUCCESS;
532 1.27 lukem }
533 1.27 lukem /* NOTREACHED */
534 1.27 lukem }
535 1.41 lukem #endif /* YP */
536 1.27 lukem
537 1.34 lukem #ifdef _GROUP_COMPAT
538 1.27 lukem /*
539 1.27 lukem * log an error if "files" or "compat" is specified in group_compat database
540 1.27 lukem */
541 1.41 lukem static int _bad_grscan(void *, void *, va_list);
542 1.27 lukem
543 1.29 christos /*ARGSUSED*/
544 1.27 lukem static int
545 1.41 lukem _bad_grscan(void *rv, void *cb_data, va_list ap)
546 1.27 lukem {
547 1.27 lukem static int warned;
548 1.27 lukem
549 1.37 lukem _DIAGASSERT(cb_data != NULL);
550 1.37 lukem
551 1.27 lukem if (!warned) {
552 1.27 lukem syslog(LOG_ERR,
553 1.27 lukem "nsswitch.conf group_compat database can't use '%s'",
554 1.27 lukem (char *)cb_data);
555 1.27 lukem }
556 1.27 lukem warned = 1;
557 1.27 lukem return NS_UNAVAIL;
558 1.27 lukem }
559 1.27 lukem
560 1.27 lukem /*
561 1.27 lukem * when a name lookup in compat mode is required, look it up in group_compat
562 1.27 lukem * nsswitch database. only Hesiod and NIS is supported - it doesn't make
563 1.27 lukem * sense to lookup compat names from 'files' or 'compat'
564 1.27 lukem */
565 1.27 lukem
566 1.45 elric static int __grscancompat(int, gid_t, const char *, const char *);
567 1.27 lukem
568 1.27 lukem static int
569 1.45 elric __grscancompat(int search, gid_t gid, const char *name, const char *user)
570 1.27 lukem {
571 1.31 lukem static const ns_dtab dtab[] = {
572 1.30 lukem NS_FILES_CB(_bad_grscan, "files")
573 1.30 lukem NS_DNS_CB(_dns_grscan, NULL)
574 1.30 lukem NS_NIS_CB(_nis_grscan, NULL)
575 1.30 lukem NS_COMPAT_CB(_bad_grscan, "compat")
576 1.27 lukem { 0 }
577 1.27 lukem };
578 1.32 lukem static const ns_src defaultnis[] = {
579 1.32 lukem { NSSRC_NIS, NS_SUCCESS },
580 1.32 lukem { 0 }
581 1.32 lukem };
582 1.27 lukem
583 1.37 lukem _DIAGASSERT(name != NULL);
584 1.37 lukem
585 1.30 lukem return (nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "grscancompat",
586 1.45 elric defaultnis, search, gid, name, user));
587 1.27 lukem }
588 1.41 lukem #endif /* GROUP_COMPAT */
589 1.27 lukem
590 1.27 lukem
591 1.41 lukem static int _compat_grscan(void *, void *, va_list);
592 1.27 lukem
593 1.29 christos /*ARGSUSED*/
594 1.27 lukem static int
595 1.41 lukem _compat_grscan(void *rv, void *cb_data, va_list ap)
596 1.27 lukem {
597 1.27 lukem int search = va_arg(ap, int);
598 1.27 lukem gid_t gid = va_arg(ap, gid_t);
599 1.27 lukem const char *name = va_arg(ap, const char *);
600 1.45 elric const char *user = va_arg(ap, const char *);
601 1.27 lukem
602 1.34 lukem #ifdef _GROUP_COMPAT
603 1.27 lukem static char *grname = NULL;
604 1.34 lukem #endif
605 1.27 lukem
606 1.27 lukem for (;;) {
607 1.34 lukem #ifdef _GROUP_COMPAT
608 1.27 lukem if(__grmode != GRMODE_NONE) {
609 1.27 lukem int r;
610 1.27 lukem
611 1.27 lukem switch(__grmode) {
612 1.27 lukem case GRMODE_FULL:
613 1.45 elric r = __grscancompat(search, gid, name, user);
614 1.27 lukem if (r == NS_SUCCESS)
615 1.27 lukem return r;
616 1.27 lukem __grmode = GRMODE_NONE;
617 1.27 lukem break;
618 1.27 lukem case GRMODE_NAME:
619 1.27 lukem if(grname == (char *)NULL) {
620 1.27 lukem __grmode = GRMODE_NONE;
621 1.27 lukem break;
622 1.27 lukem }
623 1.45 elric r = __grscancompat(1, 0, grname, user);
624 1.27 lukem free(grname);
625 1.27 lukem grname = (char *)NULL;
626 1.27 lukem if (r != NS_SUCCESS)
627 1.27 lukem break;
628 1.27 lukem if (!search)
629 1.27 lukem return NS_SUCCESS;
630 1.27 lukem if (name) {
631 1.27 lukem if (! strcmp(_gr_group.gr_name, name))
632 1.27 lukem return NS_SUCCESS;
633 1.13 phil } else {
634 1.27 lukem if (_gr_group.gr_gid == gid)
635 1.27 lukem return NS_SUCCESS;
636 1.2 deraadt }
637 1.20 christos break;
638 1.27 lukem case GRMODE_NONE:
639 1.27 lukem abort();
640 1.2 deraadt }
641 1.27 lukem continue;
642 1.2 deraadt }
643 1.41 lukem #endif /* _GROUP_COMPAT */
644 1.27 lukem
645 1.1 cgd if (!fgets(line, sizeof(line), _gr_fp))
646 1.27 lukem return NS_NOTFOUND;
647 1.1 cgd /* skip lines that are too big */
648 1.6 jtc if (!strchr(line, '\n')) {
649 1.1 cgd int ch;
650 1.1 cgd
651 1.1 cgd while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
652 1.1 cgd ;
653 1.1 cgd continue;
654 1.1 cgd }
655 1.27 lukem
656 1.34 lukem #ifdef _GROUP_COMPAT
657 1.13 phil if (line[0] == '+') {
658 1.27 lukem char *tptr, *bp;
659 1.27 lukem
660 1.13 phil switch(line[1]) {
661 1.13 phil case ':':
662 1.13 phil case '\0':
663 1.13 phil case '\n':
664 1.27 lukem __grmode = GRMODE_FULL;
665 1.13 phil break;
666 1.13 phil default:
667 1.27 lukem __grmode = GRMODE_NAME;
668 1.27 lukem bp = line;
669 1.27 lukem tptr = strsep(&bp, ":\n");
670 1.27 lukem grname = strdup(tptr + 1);
671 1.13 phil break;
672 1.2 deraadt }
673 1.9 deraadt continue;
674 1.1 cgd }
675 1.41 lukem #endif /* _GROUP_COMPAT */
676 1.45 elric if (grmatchline(search, gid, name, user))
677 1.27 lukem return NS_SUCCESS;
678 1.1 cgd }
679 1.1 cgd /* NOTREACHED */
680 1.27 lukem }
681 1.27 lukem
682 1.27 lukem static int
683 1.45 elric grscan(int search, gid_t gid, const char *name, const char *user)
684 1.27 lukem {
685 1.27 lukem int r;
686 1.31 lukem static const ns_dtab dtab[] = {
687 1.30 lukem NS_FILES_CB(_local_grscan, NULL)
688 1.30 lukem NS_DNS_CB(_dns_grscan, NULL)
689 1.30 lukem NS_NIS_CB(_nis_grscan, NULL)
690 1.30 lukem NS_COMPAT_CB(_compat_grscan, NULL)
691 1.27 lukem { 0 }
692 1.27 lukem };
693 1.32 lukem static const ns_src compatsrc[] = {
694 1.32 lukem { NSSRC_COMPAT, NS_SUCCESS },
695 1.32 lukem { 0 }
696 1.32 lukem };
697 1.27 lukem
698 1.37 lukem /* name may be NULL if search is nonzero */
699 1.37 lukem
700 1.32 lukem r = nsdispatch(NULL, dtab, NSDB_GROUP, "grscan", compatsrc,
701 1.45 elric search, gid, name, user);
702 1.27 lukem return (r == NS_SUCCESS) ? 1 : 0;
703 1.27 lukem }
704 1.27 lukem
705 1.27 lukem static int
706 1.45 elric grmatchline(int search, gid_t gid, const char *name, const char *user)
707 1.27 lukem {
708 1.27 lukem unsigned long id;
709 1.27 lukem __aconst char **m;
710 1.27 lukem char *cp, *bp, *ep;
711 1.37 lukem
712 1.37 lukem /* name may be NULL if search is nonzero */
713 1.27 lukem
714 1.27 lukem if (line[0] == '+')
715 1.27 lukem return 0; /* sanity check to prevent recursion */
716 1.27 lukem bp = line;
717 1.27 lukem _gr_group.gr_name = strsep(&bp, ":\n");
718 1.27 lukem if (search && name && strcmp(_gr_group.gr_name, name))
719 1.27 lukem return 0;
720 1.27 lukem _gr_group.gr_passwd = strsep(&bp, ":\n");
721 1.27 lukem if (!(cp = strsep(&bp, ":\n")))
722 1.27 lukem return 0;
723 1.27 lukem id = strtoul(cp, &ep, 10);
724 1.27 lukem if (id > GID_MAX || *ep != '\0')
725 1.27 lukem return 0;
726 1.27 lukem _gr_group.gr_gid = (gid_t)id;
727 1.27 lukem if (search && name == NULL && _gr_group.gr_gid != gid)
728 1.27 lukem return 0;
729 1.27 lukem cp = NULL;
730 1.27 lukem if (bp == NULL)
731 1.27 lukem return 0;
732 1.27 lukem for (_gr_group.gr_mem = m = members;; bp++) {
733 1.27 lukem if (m == &members[MAXGRP - 1])
734 1.27 lukem break;
735 1.27 lukem if (*bp == ',') {
736 1.27 lukem if (cp) {
737 1.27 lukem *bp = '\0';
738 1.27 lukem *m++ = cp;
739 1.27 lukem cp = NULL;
740 1.27 lukem }
741 1.27 lukem } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
742 1.27 lukem if (cp) {
743 1.27 lukem *bp = '\0';
744 1.27 lukem *m++ = cp;
745 1.27 lukem }
746 1.27 lukem break;
747 1.27 lukem } else if (cp == NULL)
748 1.27 lukem cp = bp;
749 1.27 lukem }
750 1.27 lukem *m = NULL;
751 1.45 elric if (user) {
752 1.45 elric for (m = members; *m; m++)
753 1.45 elric if (!strcmp(user, *m))
754 1.45 elric return 1;
755 1.45 elric return 0;
756 1.45 elric }
757 1.27 lukem return 1;
758 1.1 cgd }
759