getgrent.c revision 1.39 1 1.39 mycroft /* $NetBSD: getgrent.c,v 1.39 2000/01/22 22:19:10 mycroft 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.39 mycroft __RCSID("$NetBSD: getgrent.c,v 1.39 2000/01/22 22:19:10 mycroft 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 #ifdef __STDC__
61 1.35 lukem #include <stdarg.h>
62 1.35 lukem #else
63 1.35 lukem #include <varargs.h>
64 1.35 lukem #endif
65 1.33 lukem
66 1.27 lukem #ifdef HESIOD
67 1.27 lukem #include <hesiod.h>
68 1.27 lukem #endif
69 1.2 deraadt #ifdef YP
70 1.2 deraadt #include <rpc/rpc.h>
71 1.2 deraadt #include <rpcsvc/yp_prot.h>
72 1.2 deraadt #include <rpcsvc/ypclnt.h>
73 1.21 jtc #endif
74 1.21 jtc
75 1.34 lukem #if defined(YP) || defined(HESIOD)
76 1.34 lukem #define _GROUP_COMPAT
77 1.34 lukem #endif
78 1.34 lukem
79 1.21 jtc #ifdef __weak_alias
80 1.39 mycroft __weak_alias(endgrent,_endgrent)
81 1.39 mycroft __weak_alias(getgrent,_getgrent)
82 1.39 mycroft __weak_alias(getgrgid,_getgrgid)
83 1.39 mycroft __weak_alias(getgrnam,_getgrnam)
84 1.39 mycroft __weak_alias(setgrent,_setgrent)
85 1.39 mycroft __weak_alias(setgroupent,_setgroupent)
86 1.2 deraadt #endif
87 1.1 cgd
88 1.27 lukem static FILE *_gr_fp;
89 1.27 lukem static struct group _gr_group;
90 1.27 lukem static int _gr_stayopen;
91 1.36 lukem static int _gr_filesdone;
92 1.27 lukem
93 1.36 lukem static void grcleanup __P((void));
94 1.27 lukem static int grscan __P((int, gid_t, const char *));
95 1.27 lukem static int matchline __P((int, gid_t, const char *));
96 1.27 lukem static int start_gr __P((void));
97 1.1 cgd
98 1.1 cgd #define MAXGRP 200
99 1.1 cgd #define MAXLINELENGTH 1024
100 1.27 lukem
101 1.27 lukem static __aconst char *members[MAXGRP];
102 1.27 lukem static char line[MAXLINELENGTH];
103 1.1 cgd
104 1.2 deraadt #ifdef YP
105 1.34 lukem static char *__ypcurrent, *__ypdomain;
106 1.34 lukem static int __ypcurrentlen;
107 1.36 lukem static int _gr_ypdone;
108 1.27 lukem #endif
109 1.27 lukem
110 1.27 lukem #ifdef HESIOD
111 1.36 lukem static int _gr_hesnum;
112 1.2 deraadt #endif
113 1.2 deraadt
114 1.34 lukem #ifdef _GROUP_COMPAT
115 1.34 lukem enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
116 1.34 lukem static enum _grmode __grmode;
117 1.34 lukem #endif
118 1.34 lukem
119 1.1 cgd struct group *
120 1.1 cgd getgrent()
121 1.1 cgd {
122 1.36 lukem if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL))
123 1.36 lukem return (NULL);
124 1.27 lukem return &_gr_group;
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd struct group *
128 1.1 cgd getgrnam(name)
129 1.1 cgd const char *name;
130 1.1 cgd {
131 1.1 cgd int rval;
132 1.1 cgd
133 1.37 lukem _DIAGASSERT(name != NULL);
134 1.37 lukem
135 1.1 cgd if (!start_gr())
136 1.27 lukem return NULL;
137 1.1 cgd rval = grscan(1, 0, name);
138 1.1 cgd if (!_gr_stayopen)
139 1.1 cgd endgrent();
140 1.27 lukem return (rval) ? &_gr_group : NULL;
141 1.1 cgd }
142 1.1 cgd
143 1.1 cgd struct group *
144 1.1 cgd getgrgid(gid)
145 1.1 cgd gid_t gid;
146 1.1 cgd {
147 1.1 cgd int rval;
148 1.1 cgd
149 1.1 cgd if (!start_gr())
150 1.27 lukem return NULL;
151 1.1 cgd rval = grscan(1, gid, NULL);
152 1.1 cgd if (!_gr_stayopen)
153 1.1 cgd endgrent();
154 1.27 lukem return (rval) ? &_gr_group : NULL;
155 1.1 cgd }
156 1.1 cgd
157 1.36 lukem void
158 1.36 lukem grcleanup()
159 1.1 cgd {
160 1.36 lukem _gr_filesdone = 0;
161 1.27 lukem #ifdef YP
162 1.27 lukem if (__ypcurrent)
163 1.27 lukem free(__ypcurrent);
164 1.27 lukem __ypcurrent = NULL;
165 1.36 lukem _gr_ypdone = 0;
166 1.27 lukem #endif
167 1.27 lukem #ifdef HESIOD
168 1.36 lukem _gr_hesnum = 0;
169 1.27 lukem #endif
170 1.34 lukem #ifdef _GROUP_COMPAT
171 1.34 lukem __grmode = GRMODE_NONE;
172 1.34 lukem #endif
173 1.36 lukem }
174 1.36 lukem
175 1.36 lukem static int
176 1.36 lukem start_gr()
177 1.36 lukem {
178 1.36 lukem grcleanup();
179 1.1 cgd if (_gr_fp) {
180 1.1 cgd rewind(_gr_fp);
181 1.27 lukem return 1;
182 1.1 cgd }
183 1.27 lukem return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
184 1.1 cgd }
185 1.1 cgd
186 1.5 jtc void
187 1.1 cgd setgrent()
188 1.1 cgd {
189 1.5 jtc (void) setgroupent(0);
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd int
193 1.1 cgd setgroupent(stayopen)
194 1.1 cgd int stayopen;
195 1.1 cgd {
196 1.1 cgd if (!start_gr())
197 1.27 lukem return 0;
198 1.1 cgd _gr_stayopen = stayopen;
199 1.27 lukem return 1;
200 1.1 cgd }
201 1.1 cgd
202 1.1 cgd void
203 1.1 cgd endgrent()
204 1.1 cgd {
205 1.36 lukem grcleanup();
206 1.1 cgd if (_gr_fp) {
207 1.1 cgd (void)fclose(_gr_fp);
208 1.1 cgd _gr_fp = NULL;
209 1.1 cgd }
210 1.1 cgd }
211 1.1 cgd
212 1.27 lukem
213 1.27 lukem static int _local_grscan __P((void *, void *, va_list));
214 1.27 lukem
215 1.29 christos /*ARGSUSED*/
216 1.27 lukem static int
217 1.27 lukem _local_grscan(rv, cb_data, ap)
218 1.27 lukem void *rv;
219 1.27 lukem void *cb_data;
220 1.27 lukem va_list ap;
221 1.27 lukem {
222 1.27 lukem int search = va_arg(ap, int);
223 1.27 lukem gid_t gid = va_arg(ap, gid_t);
224 1.27 lukem const char *name = va_arg(ap, const char *);
225 1.27 lukem
226 1.36 lukem if (_gr_filesdone)
227 1.36 lukem return NS_NOTFOUND;
228 1.27 lukem for (;;) {
229 1.27 lukem if (!fgets(line, sizeof(line), _gr_fp)) {
230 1.36 lukem if (!search)
231 1.36 lukem _gr_filesdone = 1;
232 1.27 lukem return NS_NOTFOUND;
233 1.27 lukem }
234 1.27 lukem /* skip lines that are too big */
235 1.27 lukem if (!strchr(line, '\n')) {
236 1.27 lukem int ch;
237 1.27 lukem
238 1.27 lukem while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
239 1.27 lukem ;
240 1.27 lukem continue;
241 1.27 lukem }
242 1.27 lukem if (matchline(search, gid, name))
243 1.27 lukem return NS_SUCCESS;
244 1.27 lukem }
245 1.27 lukem /* NOTREACHED */
246 1.27 lukem }
247 1.27 lukem
248 1.27 lukem #ifdef HESIOD
249 1.27 lukem static int _dns_grscan __P((void *, void *, va_list));
250 1.27 lukem
251 1.29 christos /*ARGSUSED*/
252 1.2 deraadt static int
253 1.27 lukem _dns_grscan(rv, cb_data, ap)
254 1.27 lukem void *rv;
255 1.27 lukem void *cb_data;
256 1.27 lukem va_list ap;
257 1.1 cgd {
258 1.27 lukem int search = va_arg(ap, int);
259 1.27 lukem gid_t gid = va_arg(ap, gid_t);
260 1.27 lukem const char *name = va_arg(ap, const char *);
261 1.27 lukem
262 1.27 lukem char **hp;
263 1.33 lukem void *context;
264 1.33 lukem int r;
265 1.33 lukem
266 1.33 lukem r = NS_UNAVAIL;
267 1.36 lukem if (!search && _gr_hesnum == -1)
268 1.36 lukem return NS_NOTFOUND;
269 1.33 lukem if (hesiod_init(&context) == -1)
270 1.33 lukem return (r);
271 1.1 cgd
272 1.1 cgd for (;;) {
273 1.27 lukem if (search) {
274 1.27 lukem if (name)
275 1.27 lukem strncpy(line, name, sizeof(line));
276 1.27 lukem else
277 1.28 lukem snprintf(line, sizeof(line), "%u",
278 1.28 lukem (unsigned int)gid);
279 1.27 lukem } else {
280 1.36 lukem snprintf(line, sizeof(line), "group-%u", _gr_hesnum);
281 1.36 lukem _gr_hesnum++;
282 1.27 lukem }
283 1.2 deraadt
284 1.27 lukem line[sizeof(line) - 1] = '\0';
285 1.33 lukem hp = hesiod_resolve(context, line, "group");
286 1.27 lukem if (hp == NULL) {
287 1.33 lukem if (errno == ENOENT) {
288 1.36 lukem if (!search)
289 1.36 lukem _gr_hesnum = -1;
290 1.36 lukem r = NS_NOTFOUND;
291 1.2 deraadt }
292 1.33 lukem break;
293 1.27 lukem }
294 1.27 lukem
295 1.27 lukem /* only check first elem */
296 1.27 lukem strncpy(line, hp[0], sizeof(line));
297 1.27 lukem line[sizeof(line) - 1] = '\0';
298 1.33 lukem hesiod_free_list(context, hp);
299 1.33 lukem if (matchline(search, gid, name)) {
300 1.33 lukem r = NS_SUCCESS;
301 1.33 lukem break;
302 1.33 lukem } else if (search) {
303 1.33 lukem r = NS_NOTFOUND;
304 1.33 lukem break;
305 1.33 lukem }
306 1.27 lukem }
307 1.33 lukem hesiod_end(context);
308 1.33 lukem return (r);
309 1.27 lukem }
310 1.27 lukem #endif
311 1.27 lukem
312 1.27 lukem #ifdef YP
313 1.27 lukem static int _nis_grscan __P((void *, void *, va_list));
314 1.27 lukem
315 1.29 christos /*ARGSUSED*/
316 1.27 lukem static int
317 1.27 lukem _nis_grscan(rv, cb_data, ap)
318 1.27 lukem void *rv;
319 1.27 lukem void *cb_data;
320 1.27 lukem va_list ap;
321 1.27 lukem {
322 1.27 lukem int search = va_arg(ap, int);
323 1.27 lukem gid_t gid = va_arg(ap, gid_t);
324 1.27 lukem const char *name = va_arg(ap, const char *);
325 1.27 lukem
326 1.27 lukem char *key, *data;
327 1.27 lukem int keylen, datalen;
328 1.27 lukem int r;
329 1.27 lukem
330 1.27 lukem if(__ypdomain == NULL) {
331 1.27 lukem switch (yp_get_default_domain(&__ypdomain)) {
332 1.27 lukem case 0:
333 1.27 lukem break;
334 1.27 lukem case YPERR_RESRC:
335 1.27 lukem return NS_TRYAGAIN;
336 1.27 lukem default:
337 1.27 lukem return NS_UNAVAIL;
338 1.27 lukem }
339 1.27 lukem }
340 1.27 lukem
341 1.27 lukem if (search) { /* specific group or gid */
342 1.27 lukem if (name)
343 1.27 lukem strncpy(line, name, sizeof(line));
344 1.27 lukem else
345 1.28 lukem snprintf(line, sizeof(line), "%u", (unsigned int)gid);
346 1.27 lukem line[sizeof(line) - 1] = '\0';
347 1.27 lukem data = NULL;
348 1.27 lukem r = yp_match(__ypdomain,
349 1.27 lukem (name) ? "group.byname" : "group.bygid",
350 1.27 lukem line, (int)strlen(line), &data, &datalen);
351 1.27 lukem switch (r) {
352 1.27 lukem case 0:
353 1.27 lukem break;
354 1.27 lukem case YPERR_KEY:
355 1.27 lukem if (data)
356 1.27 lukem free(data);
357 1.27 lukem return NS_NOTFOUND;
358 1.27 lukem default:
359 1.27 lukem if (data)
360 1.16 lukem free(data);
361 1.27 lukem return NS_UNAVAIL;
362 1.27 lukem }
363 1.27 lukem data[datalen] = '\0'; /* clear trailing \n */
364 1.27 lukem strncpy(line, data, sizeof(line));
365 1.27 lukem line[sizeof(line) - 1] = '\0';
366 1.27 lukem free(data);
367 1.27 lukem if (matchline(search, gid, name))
368 1.27 lukem return NS_SUCCESS;
369 1.27 lukem else
370 1.27 lukem return NS_NOTFOUND;
371 1.27 lukem }
372 1.27 lukem
373 1.36 lukem /* ! search */
374 1.36 lukem if (_gr_ypdone)
375 1.36 lukem return NS_NOTFOUND;
376 1.36 lukem for (;;) {
377 1.27 lukem data = NULL;
378 1.27 lukem if(__ypcurrent) {
379 1.27 lukem key = NULL;
380 1.27 lukem r = yp_next(__ypdomain, "group.byname",
381 1.27 lukem __ypcurrent, __ypcurrentlen,
382 1.27 lukem &key, &keylen, &data, &datalen);
383 1.27 lukem free(__ypcurrent);
384 1.27 lukem switch (r) {
385 1.27 lukem case 0:
386 1.13 phil break;
387 1.27 lukem case YPERR_NOMORE:
388 1.27 lukem __ypcurrent = NULL;
389 1.27 lukem if (key)
390 1.27 lukem free(key);
391 1.27 lukem if (data)
392 1.2 deraadt free(data);
393 1.36 lukem _gr_ypdone = 1;
394 1.36 lukem return NS_NOTFOUND;
395 1.27 lukem default:
396 1.27 lukem if (key)
397 1.27 lukem free(key);
398 1.27 lukem if (data)
399 1.27 lukem free(data);
400 1.27 lukem return NS_UNAVAIL;
401 1.27 lukem }
402 1.27 lukem __ypcurrent = key;
403 1.27 lukem __ypcurrentlen = keylen;
404 1.27 lukem } else {
405 1.27 lukem if (yp_first(__ypdomain, "group.byname",
406 1.27 lukem &__ypcurrent, &__ypcurrentlen,
407 1.27 lukem &data, &datalen)) {
408 1.27 lukem if (data);
409 1.27 lukem free(data);
410 1.27 lukem return NS_UNAVAIL;
411 1.27 lukem }
412 1.27 lukem }
413 1.27 lukem data[datalen] = '\0'; /* clear trailing \n */
414 1.27 lukem strncpy(line, data, sizeof(line));
415 1.27 lukem line[sizeof(line) - 1] = '\0';
416 1.27 lukem free(data);
417 1.27 lukem if (matchline(search, gid, name))
418 1.27 lukem return NS_SUCCESS;
419 1.27 lukem }
420 1.27 lukem /* NOTREACHED */
421 1.27 lukem }
422 1.27 lukem #endif
423 1.27 lukem
424 1.34 lukem #ifdef _GROUP_COMPAT
425 1.27 lukem /*
426 1.27 lukem * log an error if "files" or "compat" is specified in group_compat database
427 1.27 lukem */
428 1.27 lukem static int _bad_grscan __P((void *, void *, va_list));
429 1.27 lukem
430 1.29 christos /*ARGSUSED*/
431 1.27 lukem static int
432 1.27 lukem _bad_grscan(rv, cb_data, ap)
433 1.27 lukem void *rv;
434 1.27 lukem void *cb_data;
435 1.27 lukem va_list ap;
436 1.27 lukem {
437 1.27 lukem static int warned;
438 1.27 lukem
439 1.37 lukem _DIAGASSERT(cb_data != NULL);
440 1.37 lukem
441 1.27 lukem if (!warned) {
442 1.27 lukem syslog(LOG_ERR,
443 1.27 lukem "nsswitch.conf group_compat database can't use '%s'",
444 1.27 lukem (char *)cb_data);
445 1.27 lukem }
446 1.27 lukem warned = 1;
447 1.27 lukem return NS_UNAVAIL;
448 1.27 lukem }
449 1.27 lukem
450 1.27 lukem /*
451 1.27 lukem * when a name lookup in compat mode is required, look it up in group_compat
452 1.27 lukem * nsswitch database. only Hesiod and NIS is supported - it doesn't make
453 1.27 lukem * sense to lookup compat names from 'files' or 'compat'
454 1.27 lukem */
455 1.27 lukem
456 1.27 lukem static int __grscancompat __P((int, gid_t, const char *));
457 1.27 lukem
458 1.27 lukem static int
459 1.27 lukem __grscancompat(search, gid, name)
460 1.27 lukem int search;
461 1.27 lukem gid_t gid;
462 1.27 lukem const char *name;
463 1.27 lukem {
464 1.31 lukem static const ns_dtab dtab[] = {
465 1.30 lukem NS_FILES_CB(_bad_grscan, "files")
466 1.30 lukem NS_DNS_CB(_dns_grscan, NULL)
467 1.30 lukem NS_NIS_CB(_nis_grscan, NULL)
468 1.30 lukem NS_COMPAT_CB(_bad_grscan, "compat")
469 1.27 lukem { 0 }
470 1.27 lukem };
471 1.32 lukem static const ns_src defaultnis[] = {
472 1.32 lukem { NSSRC_NIS, NS_SUCCESS },
473 1.32 lukem { 0 }
474 1.32 lukem };
475 1.27 lukem
476 1.37 lukem _DIAGASSERT(name != NULL);
477 1.37 lukem
478 1.30 lukem return (nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "grscancompat",
479 1.32 lukem defaultnis, search, gid, name));
480 1.27 lukem }
481 1.34 lukem #endif
482 1.27 lukem
483 1.27 lukem
484 1.27 lukem static int _compat_grscan __P((void *, void *, va_list));
485 1.27 lukem
486 1.29 christos /*ARGSUSED*/
487 1.27 lukem static int
488 1.27 lukem _compat_grscan(rv, cb_data, ap)
489 1.27 lukem void *rv;
490 1.27 lukem void *cb_data;
491 1.27 lukem va_list ap;
492 1.27 lukem {
493 1.27 lukem int search = va_arg(ap, int);
494 1.27 lukem gid_t gid = va_arg(ap, gid_t);
495 1.27 lukem const char *name = va_arg(ap, const char *);
496 1.27 lukem
497 1.34 lukem #ifdef _GROUP_COMPAT
498 1.27 lukem static char *grname = NULL;
499 1.34 lukem #endif
500 1.27 lukem
501 1.27 lukem for (;;) {
502 1.34 lukem #ifdef _GROUP_COMPAT
503 1.27 lukem if(__grmode != GRMODE_NONE) {
504 1.27 lukem int r;
505 1.27 lukem
506 1.27 lukem switch(__grmode) {
507 1.27 lukem case GRMODE_FULL:
508 1.27 lukem r = __grscancompat(search, gid, name);
509 1.27 lukem if (r == NS_SUCCESS)
510 1.27 lukem return r;
511 1.27 lukem __grmode = GRMODE_NONE;
512 1.27 lukem break;
513 1.27 lukem case GRMODE_NAME:
514 1.27 lukem if(grname == (char *)NULL) {
515 1.27 lukem __grmode = GRMODE_NONE;
516 1.27 lukem break;
517 1.27 lukem }
518 1.27 lukem r = __grscancompat(1, 0, grname);
519 1.27 lukem free(grname);
520 1.27 lukem grname = (char *)NULL;
521 1.27 lukem if (r != NS_SUCCESS)
522 1.27 lukem break;
523 1.27 lukem if (!search)
524 1.27 lukem return NS_SUCCESS;
525 1.27 lukem if (name) {
526 1.27 lukem if (! strcmp(_gr_group.gr_name, name))
527 1.27 lukem return NS_SUCCESS;
528 1.13 phil } else {
529 1.27 lukem if (_gr_group.gr_gid == gid)
530 1.27 lukem return NS_SUCCESS;
531 1.2 deraadt }
532 1.20 christos break;
533 1.27 lukem case GRMODE_NONE:
534 1.27 lukem abort();
535 1.2 deraadt }
536 1.27 lukem continue;
537 1.2 deraadt }
538 1.34 lukem #endif /* _GROUP_COMPAT */
539 1.27 lukem
540 1.1 cgd if (!fgets(line, sizeof(line), _gr_fp))
541 1.27 lukem return NS_NOTFOUND;
542 1.1 cgd /* skip lines that are too big */
543 1.6 jtc if (!strchr(line, '\n')) {
544 1.1 cgd int ch;
545 1.1 cgd
546 1.1 cgd while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
547 1.1 cgd ;
548 1.1 cgd continue;
549 1.1 cgd }
550 1.27 lukem
551 1.34 lukem #ifdef _GROUP_COMPAT
552 1.13 phil if (line[0] == '+') {
553 1.27 lukem char *tptr, *bp;
554 1.27 lukem
555 1.13 phil switch(line[1]) {
556 1.13 phil case ':':
557 1.13 phil case '\0':
558 1.13 phil case '\n':
559 1.27 lukem __grmode = GRMODE_FULL;
560 1.13 phil break;
561 1.13 phil default:
562 1.27 lukem __grmode = GRMODE_NAME;
563 1.27 lukem bp = line;
564 1.27 lukem tptr = strsep(&bp, ":\n");
565 1.27 lukem grname = strdup(tptr + 1);
566 1.13 phil break;
567 1.2 deraadt }
568 1.9 deraadt continue;
569 1.1 cgd }
570 1.34 lukem #endif /* _GROUP_COMPAT */
571 1.27 lukem if (matchline(search, gid, name))
572 1.27 lukem return NS_SUCCESS;
573 1.1 cgd }
574 1.1 cgd /* NOTREACHED */
575 1.27 lukem }
576 1.27 lukem
577 1.27 lukem static int
578 1.27 lukem grscan(search, gid, name)
579 1.27 lukem int search;
580 1.27 lukem gid_t gid;
581 1.27 lukem const char *name;
582 1.27 lukem {
583 1.27 lukem int r;
584 1.31 lukem static const ns_dtab dtab[] = {
585 1.30 lukem NS_FILES_CB(_local_grscan, NULL)
586 1.30 lukem NS_DNS_CB(_dns_grscan, NULL)
587 1.30 lukem NS_NIS_CB(_nis_grscan, NULL)
588 1.30 lukem NS_COMPAT_CB(_compat_grscan, NULL)
589 1.27 lukem { 0 }
590 1.27 lukem };
591 1.32 lukem static const ns_src compatsrc[] = {
592 1.32 lukem { NSSRC_COMPAT, NS_SUCCESS },
593 1.32 lukem { 0 }
594 1.32 lukem };
595 1.27 lukem
596 1.37 lukem /* name may be NULL if search is nonzero */
597 1.37 lukem
598 1.32 lukem r = nsdispatch(NULL, dtab, NSDB_GROUP, "grscan", compatsrc,
599 1.30 lukem search, gid, name);
600 1.27 lukem return (r == NS_SUCCESS) ? 1 : 0;
601 1.27 lukem }
602 1.27 lukem
603 1.27 lukem static int
604 1.27 lukem matchline(search, gid, name)
605 1.27 lukem int search;
606 1.27 lukem gid_t gid;
607 1.27 lukem const char *name;
608 1.27 lukem {
609 1.27 lukem unsigned long id;
610 1.27 lukem __aconst char **m;
611 1.27 lukem char *cp, *bp, *ep;
612 1.37 lukem
613 1.37 lukem /* name may be NULL if search is nonzero */
614 1.27 lukem
615 1.27 lukem if (line[0] == '+')
616 1.27 lukem return 0; /* sanity check to prevent recursion */
617 1.27 lukem bp = line;
618 1.27 lukem _gr_group.gr_name = strsep(&bp, ":\n");
619 1.27 lukem if (search && name && strcmp(_gr_group.gr_name, name))
620 1.27 lukem return 0;
621 1.27 lukem _gr_group.gr_passwd = strsep(&bp, ":\n");
622 1.27 lukem if (!(cp = strsep(&bp, ":\n")))
623 1.27 lukem return 0;
624 1.27 lukem id = strtoul(cp, &ep, 10);
625 1.27 lukem if (id > GID_MAX || *ep != '\0')
626 1.27 lukem return 0;
627 1.27 lukem _gr_group.gr_gid = (gid_t)id;
628 1.27 lukem if (search && name == NULL && _gr_group.gr_gid != gid)
629 1.27 lukem return 0;
630 1.27 lukem cp = NULL;
631 1.27 lukem if (bp == NULL)
632 1.27 lukem return 0;
633 1.27 lukem for (_gr_group.gr_mem = m = members;; bp++) {
634 1.27 lukem if (m == &members[MAXGRP - 1])
635 1.27 lukem break;
636 1.27 lukem if (*bp == ',') {
637 1.27 lukem if (cp) {
638 1.27 lukem *bp = '\0';
639 1.27 lukem *m++ = cp;
640 1.27 lukem cp = NULL;
641 1.27 lukem }
642 1.27 lukem } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
643 1.27 lukem if (cp) {
644 1.27 lukem *bp = '\0';
645 1.27 lukem *m++ = cp;
646 1.27 lukem }
647 1.27 lukem break;
648 1.27 lukem } else if (cp == NULL)
649 1.27 lukem cp = bp;
650 1.27 lukem }
651 1.27 lukem *m = NULL;
652 1.27 lukem return 1;
653 1.1 cgd }
654