3 * $Id: netg.c,v 1.5 2003/10/12 00:14:55 mdw Exp $
5 * A local database of netgroups
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.5 2003/10/12 00:14:55 mdw
33 * Major overhaul. Now uses DSA signatures rather than the bogus symmetric
34 * encrypt-and-hope thing. Integrated with mLib and Catacomb.
36 * Revision 1.4 1998/04/23 13:24:49 mdw
37 * Switch to using the ypstuff interface to YP server.
39 * Revision 1.3 1998/01/12 16:46:17 mdw
42 * Revision 1.2 1997/08/20 16:19:11 mdw
43 * Patch memory leak. Replace `name_reinit' by `name_end' for more sensible
44 * restart. Don't try to trace when tracing's turned off.
46 * Revision 1.1 1997/08/07 09:45:00 mdw
47 * New source file added to maintain a netgroups database.
51 /*----- Header files ------------------------------------------------------*/
53 /* --- ANSI headers --- */
60 /* --- Unix headers --- */
64 #include <sys/types.h>
66 #include <netinet/in.h>
68 #include <arpa/inet.h>
74 /* --- mLib headers --- */
76 #include <mLib/alloc.h>
77 #include <mLib/report.h>
79 #include <mLib/trace.h>
81 /* --- Local headers --- */
89 /*----- Type definitions --------------------------------------------------*/
91 /* --- Quick discussion --- *
93 * I've just noticed: netgroups are horrible. They form a directed graph
94 * which is really horrible; I'll have to try and turn it into something
95 * more sensible (which will essentially involve cutting cycles).
97 * The structure looks a little bit like a good ol' List (see Knuth 1 or
98 * any decent Lisp manual), but with more information in the cons cells.
101 /* --- @netg__cons@ --- */
103 typedef struct netg__cons {
106 struct netg__cons *cons;
107 struct netg__atom *atom;
109 struct netg__cons *cdr;
113 f_cons = 1, /* The @car@ is a cons cell */
114 f_visit = 2, /* Currently threaded on this cell */
115 f_uncycled = 4 /* Cycles removed from here on in */
118 /* --- @netg__atom@ --- */
120 typedef struct netg__atom {
121 char *n; /* Unresolved netgroup reference */
122 char *h; /* Matched hostname */
123 char *u; /* Matched user name */
124 char *d; /* Matched domain name */
127 /* --- @netg__sym@ --- */
129 typedef struct netg__sym {
134 /* --- Token types for the netgroup parser --- */
141 /*----- Static variables --------------------------------------------------*/
143 static sym_table netg__table; /* Netgroup table */
144 static sym_iter netg__iter; /* Iterator object for users */
146 /*----- Main code ---------------------------------------------------------*/
148 /* --- @netg__lex@ --- *
150 * Arguments: @const char **p@ = pointer to next unscanned character
151 * @char *q@ = pointer to output buffer
153 * Returns: Token type (either character code or a magic number).
155 * Use: Lexes a netgroups line into tokens.
158 static int netg__lex(char **p, char *q)
160 /* --- Skip any leading whitespace --- */
162 while (isspace((unsigned char)**p))
165 /* --- Now work out what we've got --- */
169 if (**p == '(' || **p == ')' || **p == ',')
173 while (**p != 0 && **p != '(' && **p != ')' &&
174 **p != ',' && !isspace((unsigned char)**p));
179 /* --- @netg__foreach@ --- *
181 * Arguments: @int st@ = YP protocol-level status code
182 * @char *k@ = pointer to string containing the key
183 * @int ksz@ = length of the key string
184 * @char *v@ = pointer to string containing the value
185 * @int vsz@ = length of the value string
186 * @char *data@ = pointer to my data information
188 * Returns: Zero to continue, nonzero for no more entries.
190 * Use: Handles each incoming netgroup, attaching it to the table.
193 static int netg__foreach(int st, char *k, int ksz,
194 char *v, int vsz, char *data)
199 netg__cons *c, **link;
203 /* --- If something is amiss, then quit now --- */
208 /* --- Ignore empty lines from the original file --- */
213 /* --- Build my own trashable copies of the key and value --- *
215 * Note the oddness when I copy the value string. The extra byte at the
216 * beginning allows me to use the same area of memory as an output buffer
217 * for the lexer. It must be big enough; the lexer doesn't back up; and
218 * that extra byte gives me somewhere to put a terminating null byte.
221 kc = xmalloc(ksz + 1);
225 vc = xmalloc(vsz + 2);
226 memcpy(vc + 1, v, vsz);
229 T( trace(TRACE_DEBUG, "debug: netgroup `%s': `%s'", kc, vc + 1); )
231 /* --- Allocate a symbol in my table --- */
233 sng = sym_find(&netg__table, kc, -1, sizeof(*sng), &f);
237 /* --- Run to the end of the list --- */
239 for (link = &sng->cons; *link; link = &((*link)->cdr))
242 /* --- Now start the tricky bit --- *
244 * I have to start parsing the netgroup value string. Oh, well, it
247 * The parser is written so as to avoid saying things more often than
248 * necessary. This tends to involve @goto@s. You've been warned.
252 t = netg__lex(&p, vc);
256 /* --- Start with a fresh cons cell, with an empty atom attached --- */
258 c = xmalloc(sizeof(*c));
259 c->car.atom = xmalloc(sizeof(*c->car.atom));
261 /* --- Restart here after an error --- *
263 * If I restart here, I can avoid freeing the cons cell reallocating
264 * it, which is a little silly.
268 c->car.atom->n = c->car.atom->h = c->car.atom->u = c->car.atom->d = 0;
272 /* --- Handle end-of-line --- */
277 /* --- Handle a netgroup reference --- */
279 if (t == tok_string) {
280 T( trace(TRACE_DEBUG, "debug: add reference to `%s'", vc); )
281 c->car.atom->n = xstrdup(vc);
284 t = netg__lex(&p, vc);
288 /* --- Parse our merry way through the host--user--domain triple --- */
292 t = netg__lex(&p, vc);
294 if (t == tok_string) {
295 T( trace(TRACE_DEBUG, "debug: add host `%s'", vc); )
296 c->car.atom->h = xstrdup(vc);
297 t = netg__lex(&p, vc);
302 t = netg__lex(&p, vc);
304 if (t == tok_string) {
305 T( trace(TRACE_DEBUG, "debug: add user `%s'", vc); )
306 c->car.atom->u = xstrdup(vc);
307 t = netg__lex(&p, vc);
312 t = netg__lex(&p, vc);
314 if (t == tok_string) {
315 T( trace(TRACE_DEBUG, "debug: add domain `%s'", vc); )
316 c->car.atom->d = xstrdup(vc);
317 t = netg__lex(&p, vc);
322 t = netg__lex(&p, vc);
324 /* --- Finished that, so insert this cons cell into the list --- */
330 /* --- Tidy up during scanning of a triple --- *
332 * I'll search for the closing paren, and hope that I won't miss out too
337 while (t != tok_eof && t != ')')
338 t = netg__lex(&p, vc);
340 /* --- Other syntax oddnesses come out here --- *
342 * Snarf the token which caused the error.
346 moan("syntax error in netgroups line for `%s'", kc);
347 if (c->car.atom->n) free(c->car.atom->n);
348 if (c->car.atom->h) free(c->car.atom->h);
349 if (c->car.atom->u) free(c->car.atom->u);
350 if (c->car.atom->d) free(c->car.atom->d);
351 t = netg__lex(&p, vc);
362 /* --- @netg__dumpGroup@ --- *
364 * Arguments: @netg__cons *c@ = pointer to a list head
365 * @int lev@ = indentation level
369 * Use: Dumps the netgroup given.
374 static void netg__dumpGroup(netg__cons *c, int lev)
381 /* --- Check for a cycle --- */
383 if (c->f & f_visit) {
384 trace(TRACE_DEBUG, "debug: %*scycle!", lev * 2, "");
388 /* --- Dump the netgroup --- */
392 for (cc = c; cc; cc = cc->cdr) {
393 if (cc->f & f_cons) {
394 trace(TRACE_DEBUG, "debug: %*ssubnetgroup...", lev * 2, "");
395 netg__dumpGroup(cc->car.cons, lev + 1);
396 } else if (cc->car.atom->n) {
397 trace(TRACE_DEBUG, "debug: %*sunresolved subgroup `%s'",
398 lev * 2, "", cc->car.atom->n);
400 trace(TRACE_DEBUG, "debug: %*s(%s, %s, %s)", lev * 2, "",
401 cc->car.atom->h ? cc->car.atom->h : "<all-hosts>",
402 cc->car.atom->u ? cc->car.atom->u : "<all-users>",
403 cc->car.atom->d ? cc->car.atom->d : "<all-domains>");
412 /* --- @netg__dump@ --- *
418 * Use: Dumps the netgroups table.
423 static void netg__dump(void)
428 trace(TRACE_DEBUG, "debug: dumping netgroups file");
429 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; ) {
430 trace(TRACE_DEBUG, "debug: netgroup `%s'...", sng->_base.name);
431 sng->cons->f &= ~f_visit;
432 netg__dumpGroup(sng->cons, 1);
438 /* --- @netg_iterate@, @netg_iterate_r@ --- *
440 * Arguments: @netg_iter *i@ = pointer to a netgroup iterator object
444 * Use: Starts iterating over the netgroups.
447 void netg_iterate(void) { netg_iterate_r(&netg__iter); }
448 void netg_iterate_r(netg_iter *i) { sym_mkiter(i, &netg__table); }
450 /* --- @netg_next@, @netg_next_r@ --- *
452 * Arguments: @netg_iter *i@ = pointer to a netgroup iterator object
454 * Returns: An opaque pointer to the next item, or null.
456 * Use: Returns the next netgroup.
459 netg *netg_next(void) { return (netg_next_r(&netg__iter)); }
460 netg *netg_next_r(netg_iter *i) { return (sym_next(i)); }
462 /* --- @netg_name@ --- *
464 * Arguments: @netg *n@ = netgroup handle returned by @netg_next@.
466 * Returns: A pointer to the name; you may not modify this string.
468 * Use: Returns the name of a netgroup.
471 const char *netg_name(netg *n) { return (n->_base.name); }
473 /* --- @netg_scan@ --- *
475 * Arguments: @netg *n@ = a netgroup handle returned by @netg_next@
476 * @int (*proc)(netg *n, const char *host, const char *user,@
477 * @const char *domain, void *ctx)@ = function to call
479 * @void *ctx@ = context pointer to pass to @proc@.
481 * Returns: Zero if all went well, or the nonzero return value from
484 * Use: Passes all the members of the netgroup to a given function.
485 * The function is given the names, directly from the NIS
486 * netgroup map, except that any empty entries are passed as
487 * null pointers rather than empty strings. You may not modify
488 * any of the strings. The enumeration function, @proc@, may
489 * return nonzero to stop itself from being called any more;
490 * if this happens, the value it returns becomes the result of
491 * this function. If all the items are enumerated OK, this
492 * function returns zero.
495 static int netg__doScan(netg__cons *c,
497 int (*proc)(netg */*n*/, const char */*host*/,
498 const char */*user*/,
499 const char */*domain*/, void */*ctx*/),
506 e = netg__doScan(c->car.cons, n, proc, ctx);
508 e = proc(n, c->car.atom->h, c->car.atom->u, c->car.atom->d, ctx);
516 int netg_scan(netg *n,
517 int (*proc)(netg */*n*/, const char */*host*/,
518 const char */*user*/, const char */*domain*/,
522 return (netg__doScan(n->cons, n, proc, ctx));
525 /* --- @netg__breakCycle@ --- *
527 * Arguments: @netg__cons *c@ = pointer to a list
531 * Use: Scans the given list (recursively) and breaks any cycles it
535 static void netg__breakCycle(netg__cons *c)
539 if (!c || c->f & f_uncycled)
543 for (cc = c; cc; cc = cc->cdr) {
546 if (cc->car.cons->f & f_visit) {
547 T( trace(TRACE_DEBUG, "debug: cycle in netgroups"); )
550 netg__breakCycle(cc->car.cons);
556 /* --- @netg_init@ --- *
562 * Use: Reads the netgroup database and turns it into something nice.
567 /* --- Initialise my symbol table --- */
569 sym_create(&netg__table);
571 /* --- Bind myself unto a YP server --- */
577 /* --- Now try to read all the netgroup entries --- */
580 static struct ypall_callback ncb = { netg__foreach, 0 };
581 yp_all(yp_domain, "netgroup", &ncb);
584 /* --- Dump the table --- */
586 IF_TRACING(TRACE_DEBUG, netg__dump(); )
588 /* --- Now resolve all the remaining references --- */
596 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; ) {
597 for (c = sng->cons; c; c = c->cdr) {
598 if ((c->f & f_cons) == 0 && c->car.atom->n) {
600 ng = sym_find(&netg__table, a->n, -1, 0, 0);
602 moan("undefined netgroup `%s' (ignored)", a->n);
605 c->car.cons = ng->cons;
615 /* --- Remove cycles in the netgroups table --- */
621 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; )
622 sng->cons->f &= ~f_uncycled;
623 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; )
624 netg__breakCycle(sng->cons);
627 /* --- Dump the table again --- */
629 IF_TRACING(TRACE_DEBUG, netg__dump(); )
632 /* --- @netg_end@ --- *
638 * Use: Empties the netgroups database.
647 /* --- Remove all the old netgroups rubbish --- */
649 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; ) {
653 if (~c->f & f_cons) {
654 if (c->car.atom->n) free(c->car.atom->n);
655 if (c->car.atom->h) free(c->car.atom->h);
656 if (c->car.atom->u) free(c->car.atom->u);
657 if (c->car.atom->d) free(c->car.atom->d);
663 sym_remove(&netg__table, sng);
666 sym_destroy(&netg__table);
669 /*----- Test driver -------------------------------------------------------*/
673 int scanner(netg *n, const char *h, const char *u, const char *d, void *c)
675 fprintf(stderr, " %s, %s, %s\n",
676 h ? h : "<any>", u ? u : "<any>", d ? d : "<any>");
684 traceon(stderr, TRACE_ALL);
686 for (netg_iterate(); (n = netg_next()) != 0; ) {
687 fprintf(stderr, "netgroup %s\n", netg_name(n));
688 netg_scan(n, scanner, 0);
695 /*----- That's all, folks -------------------------------------------------*/