getnetgrent.c revision 1.18 1 /* $NetBSD: getnetgrent.c,v 1.18 1999/01/18 20:38:01 christos Exp $ */
2
3 /*
4 * Copyright (c) 1994 Christos Zoulas
5 * 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 Christos Zoulas.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * 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 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: getnetgrent.c,v 1.18 1999/01/18 20:38:01 christos Exp $");
37 #endif /* LIBC_SCCS and not lint */
38
39 #include "namespace.h"
40 #include <sys/types.h>
41 #include <stdio.h>
42 #define _NETGROUP_PRIVATE
43 #include <netgroup.h>
44 #include <string.h>
45 #include <fcntl.h>
46 #include <err.h>
47 #include <ctype.h>
48 #include <nsswitch.h>
49 #include <stdlib.h>
50 #include <stringlist.h>
51 #include <db.h>
52 #ifdef YP
53 #include <rpc/rpc.h>
54 #include <rpcsvc/ypclnt.h>
55 #include <rpcsvc/yp_prot.h>
56 #endif
57
58 #ifdef __weak_alias
59 __weak_alias(endnetgrent,_endnetgrent);
60 __weak_alias(getnetgrent,_getnetgrent);
61 __weak_alias(innetgr,_innetgr);
62 __weak_alias(setnetgrent,_setnetgrent);
63 #endif
64
65 #define _NG_STAR(s) (((s) == NULL || *(s) == '\0') ? _ngstar : s)
66 #define _NG_EMPTY(s) ((s) == NULL ? "" : s)
67 #define _NG_ISSPACE(p) (isspace((unsigned char) (p)) || (p) == '\n')
68
69 static const char _ngstar[] = "*";
70 static const char _ngoomem[] = "netgroup: %m";
71 static struct netgroup *_nghead = (struct netgroup *)NULL;
72 static struct netgroup *_nglist = (struct netgroup *)NULL;
73 static DB *_ng_db;
74
75 static int getstring __P((char **, int, __aconst char **));
76 static struct netgroup *getnetgroup __P((char **));
77 static int lookup __P((char *, char **, int));
78 static void addgroup __P((StringList *, char *));
79 static int in_check __P((const char *, const char *,
80 const char *, struct netgroup *));
81 static int in_find __P((StringList *, char *, const char *,
82 const char *, const char *));
83 static char *in_lookup1 __P((const char *, const char *, int));
84 static int in_lookup __P((const char *, const char *,
85 const char *, int));
86
87 /*
88 * getstring(): Get a string delimited by the character, skipping leading and
89 * trailing blanks and advancing the pointer
90 */
91 static int
92 getstring(pp, del, str)
93 char **pp;
94 int del;
95 char __aconst **str;
96 {
97 size_t len;
98 char *sp, *ep, *dp;
99
100 /* skip leading blanks */
101 for (sp = *pp; *sp && _NG_ISSPACE(*sp); sp++)
102 continue;
103
104 /* accumulate till delimiter or space */
105 for (ep = sp; *ep && *ep != del && !_NG_ISSPACE(*ep); ep++)
106 continue;
107
108 /* hunt for the delimiter */
109 for (dp = ep; *dp && *dp != del && _NG_ISSPACE(*dp); dp++)
110 continue;
111
112 if (*dp != del) {
113 *str = NULL;
114 return 0;
115 }
116
117 *pp = ++dp;
118
119 len = (ep - sp) + 1;
120 if (len > 1) {
121 dp = malloc(len);
122 if (dp == NULL)
123 err(1, _ngoomem);
124 memcpy(dp, sp, len);
125 dp[len - 1] = '\0';
126 } else
127 dp = NULL;
128
129 *str = dp;
130 return 1;
131 }
132
133
134 /*
135 * getnetgroup(): Parse a netgroup, and advance the pointer
136 */
137 static struct netgroup *
138 getnetgroup(pp)
139 char **pp;
140 {
141 struct netgroup *ng = malloc(sizeof(struct netgroup));
142
143 if (ng == NULL)
144 err(1, _ngoomem);
145
146 (*pp)++; /* skip '(' */
147 if (!getstring(pp, ',', &ng->ng_host))
148 goto badhost;
149
150 if (!getstring(pp, ',', &ng->ng_user))
151 goto baduser;
152
153 if (!getstring(pp, ')', &ng->ng_domain))
154 goto baddomain;
155
156 #ifdef DEBUG_NG
157 {
158 char buf[1024];
159 (void) fprintf(stderr, "netgroup %s\n",
160 _ng_print(buf, sizeof(buf), ng));
161 }
162 #endif
163 return ng;
164
165 baddomain:
166 if (ng->ng_user)
167 free((char *)ng->ng_user);
168 baduser:
169 if (ng->ng_host)
170 free((char *)ng->ng_host);
171 badhost:
172 free(ng);
173 return NULL;
174 }
175
176
177 static int _local_lookup __P((void *, void *, va_list));
178
179 /*ARGSUSED*/
180 static int
181 _local_lookup(rv, cb_data, ap)
182 void *rv;
183 void *cb_data;
184 va_list ap;
185 {
186 char *name = va_arg(ap, char *);
187 char **line = va_arg(ap, char **);
188 int bywhat = va_arg(ap, int);
189
190 DBT key, data;
191 size_t len;
192 char *ks;
193 int r;
194
195 if (_ng_db == NULL)
196 return NS_UNAVAIL;
197
198 len = strlen(name) + 2;
199 ks = malloc(len);
200 if (ks == NULL)
201 err(1, _ngoomem);
202
203 ks[0] = bywhat;
204 memcpy(&ks[1], name, len - 1);
205
206 key.data = (u_char *) ks;
207 key.size = len;
208
209 r = (_ng_db->get) (_ng_db, &key, &data, 0);
210 free(ks);
211 switch (r) {
212 case 0:
213 break;
214 case 1:
215 return NS_NOTFOUND;
216 case -1:
217 return NS_UNAVAIL;
218 }
219
220 *line = strdup(data.data);
221 if (*line == NULL)
222 return NS_UNAVAIL;
223 return NS_SUCCESS;
224 }
225
226 #ifdef YP
227 static int _nis_lookup __P((void *, void *, va_list));
228
229 /*ARGSUSED*/
230 static int
231 _nis_lookup(rv, cb_data, ap)
232 void *rv;
233 void *cb_data;
234 va_list ap;
235 {
236 char *name = va_arg(ap, char *);
237 char **line = va_arg(ap, char **);
238 int bywhat = va_arg(ap, int);
239
240 static char *__ypdomain;
241 int i;
242 char *map = NULL;
243
244 if(__ypdomain == NULL) {
245 switch (yp_get_default_domain(&__ypdomain)) {
246 case 0:
247 break;
248 case YPERR_RESRC:
249 return NS_TRYAGAIN;
250 default:
251 return NS_UNAVAIL;
252 }
253 }
254
255 switch (bywhat) {
256 case _NG_KEYBYNAME:
257 map = "netgroup";
258 break;
259
260 case _NG_KEYBYUSER:
261 map = "netgroup.byuser";
262 break;
263
264 case _NG_KEYBYHOST:
265 map = "netgroup.byhost";
266 break;
267
268 default:
269 abort();
270 break;
271 }
272
273
274 *line = NULL;
275 switch (yp_match(__ypdomain, map, name, (int)strlen(name), line, &i)) {
276 case 0:
277 return NS_SUCCESS;
278 case YPERR_KEY:
279 if (*line)
280 free(*line);
281 return NS_NOTFOUND;
282 default:
283 if (*line)
284 free(*line);
285 return NS_UNAVAIL;
286 }
287 /* NOTREACHED */
288 }
289 #endif
290
291 /*
292 * lookup(): Find the given key in the database or yp, and return its value
293 * in *line; returns 1 if key was found, 0 otherwise
294 */
295 static int
296 lookup(name, line, bywhat)
297 char *name;
298 char **line;
299 int bywhat;
300 {
301 int r;
302 static ns_dtab dtab[] = {
303 NS_FILES_CB(_local_lookup, NULL),
304 NS_DNS_CB(_nis_lookup, NULL),
305 { NULL, NULL, NULL }
306 };
307
308 r = nsdispatch(NULL, dtab, NSDB_NETGROUP, name, line, bywhat);
309 return (r == NS_SUCCESS) ? 1 : 0;
310 }
311
312 /*
313 * _ng_parse(): Parse a line and return: _NG_ERROR: Syntax Error _NG_NONE:
314 * line was empty or a comment _NG_GROUP: line had a netgroup definition,
315 * returned in ng _NG_NAME: line had a netgroup name, returned in name
316 *
317 * Public since used by netgroup_mkdb
318 */
319 int
320 _ng_parse(p, name, ng)
321 char **p;
322 char **name;
323 struct netgroup **ng;
324 {
325 while (**p) {
326 if (**p == '#')
327 /* comment */
328 return _NG_NONE;
329
330 while (**p && _NG_ISSPACE(**p))
331 /* skipblank */
332 (*p)++;
333
334 if (**p == '(') {
335 if ((*ng = getnetgroup(p)) == NULL) {
336 warnx("netgroup: Syntax error `%s'", *p);
337 return _NG_ERROR;
338 }
339 return _NG_GROUP;
340 } else {
341 char *np;
342 size_t i;
343
344 for (np = *p; **p && !_NG_ISSPACE(**p); (*p)++)
345 continue;
346 if (np != *p) {
347 i = (*p - np) + 1;
348 *name = malloc(i);
349 if (*name == NULL)
350 err(1, _ngoomem);
351 memcpy(*name, np, i);
352 (*name)[i - 1] = '\0';
353 return _NG_NAME;
354 }
355 }
356 }
357 return _NG_NONE;
358 }
359
360
361 /*
362 * addgroup(): Recursively add all the members of the netgroup to this group
363 */
364 static void
365 addgroup(sl, grp)
366 StringList *sl;
367 char *grp;
368 {
369 char *line, *p;
370 struct netgroup *ng;
371 char *name;
372
373 #ifdef DEBUG_NG
374 (void) fprintf(stderr, "addgroup(%s)\n", grp);
375 #endif
376 /* check for cycles */
377 if (sl_find(sl, grp) != NULL) {
378 free(grp);
379 warnx("netgroup: Cycle in group `%s'", grp);
380 return;
381 }
382 sl_add(sl, grp);
383
384 /* Lookup this netgroup */
385 line = NULL;
386 if (!lookup(grp, &line, _NG_KEYBYNAME)) {
387 if (line != NULL)
388 free(line);
389 return;
390 }
391
392 p = line;
393
394 for (;;) {
395 switch (_ng_parse(&p, &name, &ng)) {
396 case _NG_NONE:
397 /* Done with the line */
398 free(line);
399 return;
400
401 case _NG_GROUP:
402 /* new netgroup */
403 /* add to the list */
404 ng->ng_next = _nglist;
405 _nglist = ng;
406 break;
407
408 case _NG_NAME:
409 /* netgroup name */
410 addgroup(sl, name);
411 break;
412
413 case _NG_ERROR:
414 return;
415
416 default:
417 abort();
418 return;
419 }
420 }
421 }
422
423
424 /*
425 * in_check(): Compare the spec with the netgroup
426 */
427 static int
428 in_check(host, user, domain, ng)
429 const char *host;
430 const char *user;
431 const char *domain;
432 struct netgroup *ng;
433 {
434 if ((host != NULL) && (ng->ng_host != NULL)
435 && strcmp(ng->ng_host, host) != 0)
436 return 0;
437
438 if ((user != NULL) && (ng->ng_user != NULL)
439 && strcmp(ng->ng_user, user) != 0)
440 return 0;
441
442 if ((domain != NULL) && (ng->ng_domain != NULL)
443 && strcmp(ng->ng_domain, domain) != 0)
444 return 0;
445
446 return 1;
447 }
448
449
450 /*
451 * in_find(): Find a match for the host, user, domain spec
452 */
453 static int
454 in_find(sl, grp, host, user, domain)
455 StringList *sl;
456 char *grp;
457 const char *host;
458 const char *user;
459 const char *domain;
460 {
461 char *line, *p;
462 int i;
463 struct netgroup *ng;
464 char *name;
465
466 #ifdef DEBUG_NG
467 (void) fprintf(stderr, "in_find(%s)\n", grp);
468 #endif
469 /* check for cycles */
470 if (sl_find(sl, grp) != NULL) {
471 free(grp);
472 warnx("netgroup: Cycle in group `%s'", grp);
473 return 0;
474 }
475 sl_add(sl, grp);
476
477 /* Lookup this netgroup */
478 line = NULL;
479 if (!lookup(grp, &line, _NG_KEYBYNAME)) {
480 if (line)
481 free(line);
482 return 0;
483 }
484
485 p = line;
486
487 for (;;) {
488 switch (_ng_parse(&p, &name, &ng)) {
489 case _NG_NONE:
490 /* Done with the line */
491 free(line);
492 return 0;
493
494 case _NG_GROUP:
495 /* new netgroup */
496 i = in_check(host, user, domain, ng);
497 if (ng->ng_host != NULL)
498 free((char *)ng->ng_host);
499 if (ng->ng_user != NULL)
500 free((char *)ng->ng_user);
501 if (ng->ng_domain != NULL)
502 free((char *)ng->ng_domain);
503 free(ng);
504 if (i) {
505 free(line);
506 return 1;
507 }
508 break;
509
510 case _NG_NAME:
511 /* netgroup name */
512 if (in_find(sl, name, host, user, domain)) {
513 free(line);
514 return 1;
515 }
516 break;
517
518 case _NG_ERROR:
519 free(line);
520 return 0;
521
522 default:
523 abort();
524 return 0;
525 }
526 }
527 }
528
529
530 /*
531 * _ng_makekey(): Make a key from the two names given. The key is of the form
532 * <name1>.<name2> Names strings are replaced with * if they are empty;
533 */
534 char *
535 _ng_makekey(s1, s2, len)
536 const char *s1, *s2;
537 size_t len;
538 {
539 char *buf = malloc(len);
540 if (buf == NULL)
541 err(1, _ngoomem);
542 (void) snprintf(buf, len, "%s.%s", _NG_STAR(s1), _NG_STAR(s2));
543 return buf;
544 }
545
546 void
547 _ng_print(buf, len, ng)
548 char *buf;
549 size_t len;
550 const struct netgroup *ng;
551 {
552 (void) snprintf(buf, len, "(%s,%s,%s)", _NG_EMPTY(ng->ng_host),
553 _NG_EMPTY(ng->ng_user), _NG_EMPTY(ng->ng_domain));
554 }
555
556
557 /*
558 * in_lookup1(): Fast lookup for a key in the appropriate map
559 */
560 static char *
561 in_lookup1(key, domain, map)
562 const char *key;
563 const char *domain;
564 int map;
565 {
566 char *line;
567 size_t len;
568 char *ptr;
569 int res;
570
571 len = (key ? strlen(key) : 1) + (domain ? strlen(domain) : 1) + 2;
572 ptr = _ng_makekey(key, domain, len);
573 res = lookup(ptr, &line, map);
574 free(ptr);
575 return res ? line : NULL;
576 }
577
578
579 /*
580 * in_lookup(): Fast lookup for a key in the appropriate map
581 */
582 static int
583 in_lookup(group, key, domain, map)
584 const char *group;
585 const char *key;
586 const char *domain;
587 int map;
588 {
589 size_t len;
590 char *ptr, *line;
591
592 if (domain != NULL) {
593 /* Domain specified; look in "group.domain" and "*.domain" */
594 if ((line = in_lookup1(key, domain, map)) == NULL)
595 line = in_lookup1(NULL, domain, map);
596 }
597 else
598 line = NULL;
599
600 if (line == NULL) {
601 /*
602 * domain not specified or domain lookup failed; look in
603 * "group.*" and "*.*"
604 */
605 if (((line = in_lookup1(key, NULL, map)) == NULL) &&
606 ((line = in_lookup1(NULL, NULL, map)) == NULL))
607 return 0;
608 }
609
610 len = strlen(group);
611
612 for (ptr = line; (ptr = strstr(ptr, group)) != NULL;)
613 /* Make sure we did not find a substring */
614 if ((ptr != line && ptr[-1] != ',') ||
615 (ptr[len] != '\0' && strchr("\n\t ,", ptr[len]) == NULL))
616 ptr++;
617 else {
618 free(line);
619 return 1;
620 }
621
622 free(line);
623 return 0;
624 }
625
626
627 void
628 endnetgrent()
629 {
630 for (_nglist = _nghead; _nglist != NULL; _nglist = _nghead) {
631 _nghead = _nglist->ng_next;
632 if (_nglist->ng_host != NULL)
633 free((char *)_nglist->ng_host);
634 if (_nglist->ng_user != NULL)
635 free((char *)_nglist->ng_user);
636 if (_nglist->ng_domain != NULL)
637 free((char *)_nglist->ng_domain);
638 free(_nglist);
639 }
640
641 if (_ng_db) {
642 (void) (_ng_db->close) (_ng_db);
643 _ng_db = NULL;
644 }
645 }
646
647
648 void
649 setnetgrent(ng)
650 const char *ng;
651 {
652 StringList *sl = sl_init();
653 char *ng_copy;
654
655 /* Cleanup any previous storage */
656 if (_nghead != NULL)
657 endnetgrent();
658
659 if (_ng_db == NULL)
660 _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
661
662 ng_copy = strdup(ng);
663 if (ng_copy == NULL)
664 err(1, _ngoomem);
665 addgroup(sl, ng_copy);
666 _nghead = _nglist;
667 sl_free(sl, 1);
668 }
669
670
671 int
672 getnetgrent(host, user, domain)
673 const char **host;
674 const char **user;
675 const char **domain;
676 {
677 if (_nglist == NULL)
678 return 0;
679
680 *host = _nglist->ng_host;
681 *user = _nglist->ng_user;
682 *domain = _nglist->ng_domain;
683
684 _nglist = _nglist->ng_next;
685
686 return 1;
687 }
688
689
690 int
691 innetgr(grp, host, user, domain)
692 const char *grp, *host, *user, *domain;
693 {
694 int found;
695 StringList *sl;
696
697 if (_ng_db == NULL)
698 _ng_db = dbopen(_PATH_NETGROUP_DB, O_RDONLY, 0, DB_HASH, NULL);
699
700 /* Try the fast lookup first */
701 if (host != NULL && user == NULL) {
702 if (in_lookup(grp, host, domain, _NG_KEYBYHOST))
703 return 1;
704 } else if (host == NULL && user != NULL) {
705 if (in_lookup(grp, user, domain, _NG_KEYBYUSER))
706 return 1;
707 }
708 /* If a domainname is given, we would have found a match */
709 if (domain != NULL)
710 return 0;
711
712 /* Too bad need the slow recursive way */
713 sl = sl_init();
714 found = in_find(sl, strdup(grp), host, user, domain);
715 sl_free(sl, 1);
716
717 return found;
718 }
719