3 * $Id: netg.c,v 1.7 2004/04/08 01:36:20 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 /*----- Header files ------------------------------------------------------*/
31 /* --- ANSI headers --- */
38 /* --- Unix headers --- */
42 #include <sys/types.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
52 /* --- mLib headers --- */
54 #include <mLib/alloc.h>
55 #include <mLib/report.h>
57 #include <mLib/trace.h>
59 /* --- Local headers --- */
67 /*----- Type definitions --------------------------------------------------*/
69 /* --- Quick discussion --- *
71 * I've just noticed: netgroups are horrible. They form a directed graph
72 * which is really horrible; I'll have to try and turn it into something
73 * more sensible (which will essentially involve cutting cycles).
75 * The structure looks a little bit like a good ol' List (see Knuth 1 or
76 * any decent Lisp manual), but with more information in the cons cells.
79 /* --- @netg__cons@ --- */
81 typedef struct netg__cons {
84 struct netg__cons *cons;
85 struct netg__atom *atom;
87 struct netg__cons *cdr;
91 f_cons = 1, /* The @car@ is a cons cell */
92 f_visit = 2, /* Currently threaded on this cell */
93 f_uncycled = 4 /* Cycles removed from here on in */
96 /* --- @netg__atom@ --- */
98 typedef struct netg__atom {
99 char *n; /* Unresolved netgroup reference */
100 char *h; /* Matched hostname */
101 char *u; /* Matched user name */
102 char *d; /* Matched domain name */
105 /* --- @netg__sym@ --- */
107 typedef struct netg__sym {
112 /* --- Token types for the netgroup parser --- */
119 /*----- Static variables --------------------------------------------------*/
121 static sym_table netg__table; /* Netgroup table */
122 static sym_iter netg__iter; /* Iterator object for users */
124 /*----- Main code ---------------------------------------------------------*/
126 /* --- @netg__lex@ --- *
128 * Arguments: @const char **p@ = pointer to next unscanned character
129 * @char *q@ = pointer to output buffer
131 * Returns: Token type (either character code or a magic number).
133 * Use: Lexes a netgroups line into tokens.
136 static int netg__lex(char **p, char *q)
138 /* --- Skip any leading whitespace --- */
140 while (isspace((unsigned char)**p))
143 /* --- Now work out what we've got --- */
147 if (**p == '(' || **p == ')' || **p == ',')
151 while (**p != 0 && **p != '(' && **p != ')' &&
152 **p != ',' && !isspace((unsigned char)**p));
157 /* --- @netg__foreach@ --- *
159 * Arguments: @int st@ = YP protocol-level status code
160 * @char *k@ = pointer to string containing the key
161 * @int ksz@ = length of the key string
162 * @char *v@ = pointer to string containing the value
163 * @int vsz@ = length of the value string
164 * @char *data@ = pointer to my data information
166 * Returns: Zero to continue, nonzero for no more entries.
168 * Use: Handles each incoming netgroup, attaching it to the table.
171 static int netg__foreach(int st, char *k, int ksz,
172 char *v, int vsz, char *data)
177 netg__cons *c, **link;
181 /* --- If something is amiss, then quit now --- */
186 /* --- Ignore empty lines from the original file --- */
191 /* --- Build my own trashable copies of the key and value --- *
193 * Note the oddness when I copy the value string. The extra byte at the
194 * beginning allows me to use the same area of memory as an output buffer
195 * for the lexer. It must be big enough; the lexer doesn't back up; and
196 * that extra byte gives me somewhere to put a terminating null byte.
199 kc = xmalloc(ksz + 1);
203 vc = xmalloc(vsz + 2);
204 memcpy(vc + 1, v, vsz);
207 T( trace(TRACE_DEBUG, "debug: netgroup `%s': `%s'", kc, vc + 1); )
209 /* --- Allocate a symbol in my table --- */
211 sng = sym_find(&netg__table, kc, -1, sizeof(*sng), &f);
215 /* --- Run to the end of the list --- */
217 for (link = &sng->cons; *link; link = &((*link)->cdr))
220 /* --- Now start the tricky bit --- *
222 * I have to start parsing the netgroup value string. Oh, well, it
225 * The parser is written so as to avoid saying things more often than
226 * necessary. This tends to involve @goto@s. You've been warned.
230 t = netg__lex(&p, vc);
234 /* --- Start with a fresh cons cell, with an empty atom attached --- */
236 c = xmalloc(sizeof(*c));
237 c->car.atom = xmalloc(sizeof(*c->car.atom));
239 /* --- Restart here after an error --- *
241 * If I restart here, I can avoid freeing the cons cell reallocating
242 * it, which is a little silly.
246 c->car.atom->n = c->car.atom->h = c->car.atom->u = c->car.atom->d = 0;
250 /* --- Handle end-of-line --- */
255 /* --- Handle a netgroup reference --- */
257 if (t == tok_string) {
258 T( trace(TRACE_DEBUG, "debug: add reference to `%s'", vc); )
259 c->car.atom->n = xstrdup(vc);
262 t = netg__lex(&p, vc);
266 /* --- Parse our merry way through the host--user--domain triple --- */
270 t = netg__lex(&p, vc);
272 if (t == tok_string) {
273 T( trace(TRACE_DEBUG, "debug: add host `%s'", vc); )
274 c->car.atom->h = xstrdup(vc);
275 t = netg__lex(&p, vc);
280 t = netg__lex(&p, vc);
282 if (t == tok_string) {
283 T( trace(TRACE_DEBUG, "debug: add user `%s'", vc); )
284 c->car.atom->u = xstrdup(vc);
285 t = netg__lex(&p, vc);
290 t = netg__lex(&p, vc);
292 if (t == tok_string) {
293 T( trace(TRACE_DEBUG, "debug: add domain `%s'", vc); )
294 c->car.atom->d = xstrdup(vc);
295 t = netg__lex(&p, vc);
300 t = netg__lex(&p, vc);
302 /* --- Finished that, so insert this cons cell into the list --- */
308 /* --- Tidy up during scanning of a triple --- *
310 * I'll search for the closing paren, and hope that I won't miss out too
315 while (t != tok_eof && t != ')')
316 t = netg__lex(&p, vc);
318 /* --- Other syntax oddnesses come out here --- *
320 * Snarf the token which caused the error.
324 moan("syntax error in netgroups line for `%s'", kc);
325 if (c->car.atom->n) free(c->car.atom->n);
326 if (c->car.atom->h) free(c->car.atom->h);
327 if (c->car.atom->u) free(c->car.atom->u);
328 if (c->car.atom->d) free(c->car.atom->d);
329 t = netg__lex(&p, vc);
340 /* --- @netg__dumpGroup@ --- *
342 * Arguments: @netg__cons *c@ = pointer to a list head
343 * @int lev@ = indentation level
347 * Use: Dumps the netgroup given.
352 static void netg__dumpGroup(netg__cons *c, int lev)
359 /* --- Check for a cycle --- */
361 if (c->f & f_visit) {
362 trace(TRACE_DEBUG, "debug: %*scycle!", lev * 2, "");
366 /* --- Dump the netgroup --- */
370 for (cc = c; cc; cc = cc->cdr) {
371 if (cc->f & f_cons) {
372 trace(TRACE_DEBUG, "debug: %*ssubnetgroup...", lev * 2, "");
373 netg__dumpGroup(cc->car.cons, lev + 1);
374 } else if (cc->car.atom->n) {
375 trace(TRACE_DEBUG, "debug: %*sunresolved subgroup `%s'",
376 lev * 2, "", cc->car.atom->n);
378 trace(TRACE_DEBUG, "debug: %*s(%s, %s, %s)", lev * 2, "",
379 cc->car.atom->h ? cc->car.atom->h : "<all-hosts>",
380 cc->car.atom->u ? cc->car.atom->u : "<all-users>",
381 cc->car.atom->d ? cc->car.atom->d : "<all-domains>");
390 /* --- @netg__dump@ --- *
396 * Use: Dumps the netgroups table.
401 static void netg__dump(void)
406 trace(TRACE_DEBUG, "debug: dumping netgroups file");
407 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; ) {
408 trace(TRACE_DEBUG, "debug: netgroup `%s'...", sng->_base.name);
409 sng->cons->f &= ~f_visit;
410 netg__dumpGroup(sng->cons, 1);
416 /* --- @netg_iterate@, @netg_iterate_r@ --- *
418 * Arguments: @netg_iter *i@ = pointer to a netgroup iterator object
422 * Use: Starts iterating over the netgroups.
425 void netg_iterate(void) { netg_iterate_r(&netg__iter); }
426 void netg_iterate_r(netg_iter *i) { sym_mkiter(i, &netg__table); }
428 /* --- @netg_next@, @netg_next_r@ --- *
430 * Arguments: @netg_iter *i@ = pointer to a netgroup iterator object
432 * Returns: An opaque pointer to the next item, or null.
434 * Use: Returns the next netgroup.
437 netg *netg_next(void) { return (netg_next_r(&netg__iter)); }
438 netg *netg_next_r(netg_iter *i) { return (sym_next(i)); }
440 /* --- @netg_name@ --- *
442 * Arguments: @netg *n@ = netgroup handle returned by @netg_next@.
444 * Returns: A pointer to the name; you may not modify this string.
446 * Use: Returns the name of a netgroup.
449 const char *netg_name(netg *n) { return (n->_base.name); }
451 /* --- @netg_scan@ --- *
453 * Arguments: @netg *n@ = a netgroup handle returned by @netg_next@
454 * @int (*proc)(netg *n, const char *host, const char *user,@
455 * @const char *domain, void *ctx)@ = function to call
457 * @void *ctx@ = context pointer to pass to @proc@.
459 * Returns: Zero if all went well, or the nonzero return value from
462 * Use: Passes all the members of the netgroup to a given function.
463 * The function is given the names, directly from the NIS
464 * netgroup map, except that any empty entries are passed as
465 * null pointers rather than empty strings. You may not modify
466 * any of the strings. The enumeration function, @proc@, may
467 * return nonzero to stop itself from being called any more;
468 * if this happens, the value it returns becomes the result of
469 * this function. If all the items are enumerated OK, this
470 * function returns zero.
473 static int netg__doScan(netg__cons *c,
475 int (*proc)(netg */*n*/, const char */*host*/,
476 const char */*user*/,
477 const char */*domain*/, void */*ctx*/),
484 e = netg__doScan(c->car.cons, n, proc, ctx);
486 e = proc(n, c->car.atom->h, c->car.atom->u, c->car.atom->d, ctx);
494 int netg_scan(netg *n,
495 int (*proc)(netg */*n*/, const char */*host*/,
496 const char */*user*/, const char */*domain*/,
500 return (netg__doScan(n->cons, n, proc, ctx));
503 /* --- @netg__breakCycle@ --- *
505 * Arguments: @netg__cons *c@ = pointer to a list
509 * Use: Scans the given list (recursively) and breaks any cycles it
513 static void netg__breakCycle(netg__cons *c)
517 if (!c || c->f & f_uncycled)
521 for (cc = c; cc; cc = cc->cdr) {
524 if (cc->car.cons->f & f_visit) {
525 T( trace(TRACE_DEBUG, "debug: cycle in netgroups"); )
528 netg__breakCycle(cc->car.cons);
534 /* --- @netg_init@ --- *
540 * Use: Reads the netgroup database and turns it into something nice.
545 /* --- Initialise my symbol table --- */
547 sym_create(&netg__table);
549 /* --- Bind myself unto a YP server --- */
555 /* --- Now try to read all the netgroup entries --- */
558 static struct ypall_callback ncb = { netg__foreach, 0 };
559 yp_all(yp_domain, "netgroup", &ncb);
562 /* --- Dump the table --- */
564 IF_TRACING(TRACE_DEBUG, netg__dump(); )
566 /* --- Now resolve all the remaining references --- */
574 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; ) {
575 for (c = sng->cons; c; c = c->cdr) {
576 if ((c->f & f_cons) == 0 && c->car.atom->n) {
578 ng = sym_find(&netg__table, a->n, -1, 0, 0);
580 moan("undefined netgroup `%s' (ignored)", a->n);
583 c->car.cons = ng->cons;
593 /* --- Remove cycles in the netgroups table --- */
599 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; )
600 sng->cons->f &= ~f_uncycled;
601 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; )
602 netg__breakCycle(sng->cons);
605 /* --- Dump the table again --- */
607 IF_TRACING(TRACE_DEBUG, netg__dump(); )
610 /* --- @netg_end@ --- *
616 * Use: Empties the netgroups database.
625 /* --- Remove all the old netgroups rubbish --- */
627 for (sym_mkiter(&i, &netg__table); (sng = sym_next(&i)) != 0; ) {
631 if (~c->f & f_cons) {
632 if (c->car.atom->n) free(c->car.atom->n);
633 if (c->car.atom->h) free(c->car.atom->h);
634 if (c->car.atom->u) free(c->car.atom->u);
635 if (c->car.atom->d) free(c->car.atom->d);
641 sym_remove(&netg__table, sng);
644 sym_destroy(&netg__table);
647 /*----- Test driver -------------------------------------------------------*/
651 int scanner(netg *n, const char *h, const char *u, const char *d, void *c)
653 fprintf(stderr, " %s, %s, %s\n",
654 h ? h : "<any>", u ? u : "<any>", d ? d : "<any>");
662 trace_on(stderr, TRACE_ALL);
664 for (netg_iterate(); (n = netg_next()) != 0; ) {
665 fprintf(stderr, "netgroup %s\n", netg_name(n));
666 netg_scan(n, scanner, 0);
673 /*----- That's all, folks -------------------------------------------------*/