3 * $Id: netg.c,v 1.2 1997/08/20 16:19:11 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.2 1997/08/20 16:19:11 mdw
33 * Patch memory leak. Replace `name_reinit' by `name_end' for more sensible
34 * restart. Don't try to trace when tracing's turned off.
36 * Revision 1.1 1997/08/07 09:45:00 mdw
37 * New source file added to maintain a netgroups database.
41 /*----- Header files ------------------------------------------------------*/
43 /* --- ANSI headers --- */
50 /* --- Unix headers --- */
54 #include <sys/types.h>
58 # include <rpcsvc/ypclnt.h>
59 # include <rpcsvc/yp_prot.h>
62 #include <netinet/in.h>
64 #include <arpa/inet.h>
70 /* --- Local headers --- */
79 /*----- Type definitions --------------------------------------------------*/
81 /* --- Quick discussion --- *
83 * I've just noticed: netgroups are horrible. They form a directed graph
84 * which is really horrible; I'll have to try and turn it into something
85 * more sensible (which will essentially involve cutting cycles).
87 * The structure looks a little bit like a good ol' List (see Knuth 1 or
88 * any decent Lisp manual), but with more information in the cons cells.
91 /* --- @netg__cons@ --- */
93 typedef struct netg__cons
{
96 struct netg__cons
*cons
;
97 struct netg__atom
*atom
;
99 struct netg__cons
*cdr
;
103 f_cons
= 1, /* The @car@ is a cons cell */
104 f_visit
= 2, /* Currently threaded on this cell */
105 f_uncycled
= 4 /* Cycles removed from here on in */
108 /* --- @netg__atom@ --- */
110 typedef struct netg__atom
{
111 char *n
; /* Unresolved netgroup reference */
112 char *h
; /* Matched hostname */
113 char *u
; /* Matched user name */
114 char *d
; /* Matched domain name */
117 /* --- @netg__sym@ --- */
119 typedef struct netg__sym
{
124 /* --- Token types for the netgroup parser --- */
131 /*----- Static variables --------------------------------------------------*/
133 static sym_table netg__table
; /* Netgroup table */
134 static sym_iter netg__iter
; /* Iterator object for users */
136 /*----- Main code ---------------------------------------------------------*/
138 /* --- @netg__lex@ --- *
140 * Arguments: @const char **p@ = pointer to next unscanned character
141 * @char *q@ = pointer to output buffer
143 * Returns: Token type (either character code or a magic number).
145 * Use: Lexes a netgroups line into tokens.
148 static int netg__lex(char **p
, char *q
)
150 /* --- Skip any leading whitespace --- */
152 while (isspace((unsigned char)**p
))
155 /* --- Now work out what we've got --- */
159 if (**p
== '(' || **p
== ')' || **p
== ',')
163 while (**p
!= 0 && **p
!= '(' && **p
!= ')' &&
164 **p
!= ',' && !isspace((unsigned char)**p
));
169 /* --- @netg__foreach@ --- *
171 * Arguments: @int st@ = YP protocol-level status code
172 * @char *k@ = pointer to string containing the key
173 * @int ksz@ = length of the key string
174 * @char *v@ = pointer to string containing the value
175 * @int vsz@ = length of the value string
176 * @char *data@ = pointer to my data information
178 * Returns: Zero to continue, nonzero for no more entries.
180 * Use: Handles each incoming netgroup, attaching it to the table.
183 static int netg__foreach(int st
, char *k
, int ksz
,
184 char *v
, int vsz
, char *data
)
189 netg__cons
*c
, **link
;
193 /* --- If something is amiss, then quit now --- */
198 /* --- Ignore empty lines from the original file --- */
203 /* --- Build my own trashable copies of the key and value --- *
205 * Note the oddness when I copy the value string. The extra byte at the
206 * beginning allows me to use the same area of memory as an output buffer
207 * for the lexer. It must be big enough; the lexer doesn't back up; and
208 * that extra byte gives me somewhere to put a terminating null byte.
211 kc
= xmalloc(ksz
+ 1);
215 vc
= xmalloc(vsz
+ 2);
216 memcpy(vc
+ 1, v
, vsz
);
219 T( trace(TRACE_DEBUG
, "debug: netgroup `%s': `%s'", kc
, vc
+ 1); )
221 /* --- Allocate a symbol in my table --- */
223 sng
= sym_find(&netg__table
, kc
, -1, sizeof(*sng
), &f
);
227 /* --- Run to the end of the list --- */
229 for (link
= &sng
->cons
; *link
; link
= &((*link
)->cdr
))
232 /* --- Now start the tricky bit --- *
234 * I have to start parsing the netgroup value string. Oh, well, it
237 * The parser is written so as to avoid saying things more often than
238 * necessary. This tends to involve @goto@s. You've been warned.
242 t
= netg__lex(&p
, vc
);
246 /* --- Start with a fresh cons cell, with an empty atom attached --- */
248 c
= xmalloc(sizeof(*c
));
249 c
->car
.atom
= xmalloc(sizeof(*c
->car
.atom
));
251 /* --- Restart here after an error --- *
253 * If I restart here, I can avoid freeing the cons cell reallocating
254 * it, which is a little silly.
258 c
->car
.atom
->n
= c
->car
.atom
->h
= c
->car
.atom
->u
= c
->car
.atom
->d
= 0;
262 /* --- Handle end-of-line --- */
267 /* --- Handle a netgroup reference --- */
269 if (t
== tok_string
) {
270 T( trace(TRACE_DEBUG
, "debug: add reference to `%s'", vc
); )
271 c
->car
.atom
->n
= xstrdup(vc
);
274 t
= netg__lex(&p
, vc
);
278 /* --- Parse our merry way through the host--user--domain triple --- */
282 t
= netg__lex(&p
, vc
);
284 if (t
== tok_string
) {
285 T( trace(TRACE_DEBUG
, "debug: add host `%s'", vc
); )
286 c
->car
.atom
->h
= xstrdup(vc
);
287 t
= netg__lex(&p
, vc
);
292 t
= netg__lex(&p
, vc
);
294 if (t
== tok_string
) {
295 T( trace(TRACE_DEBUG
, "debug: add user `%s'", vc
); )
296 c
->car
.atom
->u
= 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 domain `%s'", vc
); )
306 c
->car
.atom
->d
= xstrdup(vc
);
307 t
= netg__lex(&p
, vc
);
312 t
= netg__lex(&p
, vc
);
314 /* --- Finished that, so insert this cons cell into the list --- */
320 /* --- Tidy up during scanning of a triple --- *
322 * I'll search for the closing paren, and hope that I won't miss out too
327 while (t
!= tok_eof
&& t
!= ')')
328 t
= netg__lex(&p
, vc
);
330 /* --- Other syntax oddnesses come out here --- *
332 * Snarf the token which caused the error.
336 moan("syntax error in netgroups line for `%s'", kc
);
337 if (c
->car
.atom
->n
) free(c
->car
.atom
->n
);
338 if (c
->car
.atom
->h
) free(c
->car
.atom
->h
);
339 if (c
->car
.atom
->u
) free(c
->car
.atom
->u
);
340 if (c
->car
.atom
->d
) free(c
->car
.atom
->d
);
341 t
= netg__lex(&p
, vc
);
352 /* --- @netg__dumpGroup@ --- *
354 * Arguments: @netg__cons *c@ = pointer to a list head
355 * @int lev@ = indentation level
359 * Use: Dumps the netgroup given.
364 static void netg__dumpGroup(netg__cons
*c
, int lev
)
371 /* --- Check for a cycle --- */
373 if (c
->f
& f_visit
) {
374 trace(TRACE_DEBUG
, "debug: %*scycle!", lev
* 2, "");
378 /* --- Dump the netgroup --- */
382 for (cc
= c
; cc
; cc
= cc
->cdr
) {
383 if (cc
->f
& f_cons
) {
384 trace(TRACE_DEBUG
, "debug: %*ssubnetgroup...", lev
* 2, "");
385 netg__dumpGroup(cc
->car
.cons
, lev
+ 1);
386 } else if (cc
->car
.atom
->n
) {
387 trace(TRACE_DEBUG
, "debug: %*sunresolved subgroup `%s'",
388 lev
* 2, "", cc
->car
.atom
->n
);
390 trace(TRACE_DEBUG
, "debug: %*s(%s, %s, %s)", lev
* 2, "",
391 cc
->car
.atom
->h ? cc
->car
.atom
->h
: "<all-hosts>",
392 cc
->car
.atom
->u ? cc
->car
.atom
->u
: "<all-users>",
393 cc
->car
.atom
->d ? cc
->car
.atom
->d
: "<all-domains>");
402 /* --- @netg__dump@ --- *
408 * Use: Dumps the netgroups table.
413 static void netg__dump(void)
418 trace(TRACE_DEBUG
, "debug: dumping netgroups file");
419 for (sym_createIter(&i
, &netg__table
); (sng
= sym_next(&i
)) != 0; ) {
420 trace(TRACE_DEBUG
, "debug: netgroup `%s'...", sng
->_base
.name
);
421 sng
->cons
->f
&= ~f_visit
;
422 netg__dumpGroup(sng
->cons
, 1);
428 /* --- @netg_iterate@, @netg_iterate_r@ --- *
430 * Arguments: @netg_iter *i@ = pointer to a netgroup iterator object
434 * Use: Starts iterating over the netgroups.
437 void netg_iterate(void) { netg_iterate_r(&netg__iter
); }
438 void netg_iterate_r(netg_iter
*i
) { sym_createIter(i
, &netg__table
); }
440 /* --- @netg_next@, @netg_next_r@ --- *
442 * Arguments: @netg_iter *i@ = pointer to a netgroup iterator object
444 * Returns: An opaque pointer to the next item, or null.
446 * Use: Returns the next netgroup.
449 netg
*netg_next(void) { return (netg_next_r(&netg__iter
)); }
450 netg
*netg_next_r(netg_iter
*i
) { return (sym_next(i
)); }
452 /* --- @netg_name@ --- *
454 * Arguments: @netg *n@ = netgroup handle returned by @netg_next@.
456 * Returns: A pointer to the name; you may not modify this string.
458 * Use: Returns the name of a netgroup.
461 const char *netg_name(netg
*n
) { return (n
->_base
.name
); }
463 /* --- @netg_scan@ --- *
465 * Arguments: @netg *n@ = a netgroup handle returned by @netg_next@
466 * @int (*proc)(netg *n, const char *host, const char *user,@
467 * @const char *domain, void *ctx)@ = function to call
469 * @void *ctx@ = context pointer to pass to @proc@.
471 * Returns: Zero if all went well, or the nonzero return value from
474 * Use: Passes all the members of the netgroup to a given function.
475 * The function is given the names, directly from the NIS
476 * netgroup map, except that any empty entries are passed as
477 * null pointers rather than empty strings. You may not modify
478 * any of the strings. The enumeration function, @proc@, may
479 * return nonzero to stop itself from being called any more;
480 * if this happens, the value it returns becomes the result of
481 * this function. If all the items are enumerated OK, this
482 * function returns zero.
485 static int netg__doScan(netg__cons
*c
,
487 int (*proc
)(netg */
*n*/
, const char */
*host*/
,
488 const char */
*user*/
,
489 const char */
*domain*/
, void */
*ctx*/
),
496 e
= netg__doScan(c
->car
.cons
, n
, proc
, ctx
);
498 e
= proc(n
, c
->car
.atom
->h
, c
->car
.atom
->u
, c
->car
.atom
->d
, ctx
);
506 int netg_scan(netg
*n
,
507 int (*proc
)(netg */
*n*/
, const char */
*host*/
,
508 const char */
*user*/
, const char */
*domain*/
,
512 return (netg__doScan(n
->cons
, n
, proc
, ctx
));
515 /* --- @netg__breakCycle@ --- *
517 * Arguments: @netg__cons *c@ = pointer to a list
521 * Use: Scans the given list (recursively) and breaks any cycles it
525 static void netg__breakCycle(netg__cons
*c
)
529 if (!c
|| c
->f
& f_uncycled
)
533 for (cc
= c
; cc
; cc
= cc
->cdr
) {
536 if (cc
->car
.cons
->f
& f_visit
) {
537 T( trace(TRACE_DEBUG
, "debug: cycle in netgroups"); )
540 netg__breakCycle(cc
->car
.cons
);
546 /* --- @netg_init@ --- *
552 * Use: Reads the netgroup database and turns it into something nice.
559 /* --- Initialise my symbol table --- */
561 sym_createTable(&netg__table
);
563 /* --- Bind myself unto a YP server --- */
565 if (yp_get_default_domain(&ypdom
) ||
569 /* --- Now try to read all the netgroup entries --- */
572 static struct ypall_callback ncb
= { netg__foreach
, 0 };
573 yp_all(ypdom
, "netgroup", &ncb
);
576 /* --- Unbind from the server --- */
580 /* --- Dump the table --- */
582 IF_TRACING(TRACE_DEBUG
, netg__dump(); )
584 /* --- Now resolve all the remaining references --- */
592 for (sym_createIter(&i
, &netg__table
); (sng
= sym_next(&i
)) != 0; ) {
593 for (c
= sng
->cons
; c
; c
= c
->cdr
) {
594 if ((c
->f
& f_cons
) == 0 && c
->car
.atom
->n
) {
596 ng
= sym_find(&netg__table
, a
->n
, -1, 0, 0);
598 moan("undefined netgroup `%s' (ignored)", a
->n
);
601 c
->car
.cons
= ng
->cons
;
611 /* --- Remove cycles in the netgroups table --- */
617 for (sym_createIter(&i
, &netg__table
); (sng
= sym_next(&i
)) != 0; )
618 sng
->cons
->f
&= ~f_uncycled
;
619 for (sym_createIter(&i
, &netg__table
); (sng
= sym_next(&i
)) != 0; )
620 netg__breakCycle(sng
->cons
);
623 /* --- Dump the table again --- */
625 IF_TRACING(TRACE_DEBUG
, netg__dump(); )
628 /* --- @netg_end@ --- *
634 * Use: Empties the netgroups database.
643 /* --- Remove all the old netgroups rubbish --- */
645 for (sym_createIter(&i
, &netg__table
); (sng
= sym_next(&i
)) != 0; ) {
649 if (~c
->f
& f_cons
) {
650 if (c
->car
.atom
->n
) free(c
->car
.atom
->n
);
651 if (c
->car
.atom
->h
) free(c
->car
.atom
->h
);
652 if (c
->car
.atom
->u
) free(c
->car
.atom
->u
);
653 if (c
->car
.atom
->d
) free(c
->car
.atom
->d
);
659 sym_remove(&netg__table
, sng
);
662 sym_destroyTable(&netg__table
);
665 /*----- Test driver -------------------------------------------------------*/
669 int scanner(netg
*n
, const char *h
, const char *u
, const char *d
, void *c
)
671 fprintf(stderr
, " %s, %s, %s\n",
672 h ? h
: "<any>", u ? u
: "<any>", d ? d
: "<any>");
680 traceon(stderr
, TRACE_ALL
);
682 for (netg_iterate(); (n
= netg_next()) != 0; ) {
683 fprintf(stderr
, "netgroup %s\n", netg_name(n
));
684 netg_scan(n
, scanner
, 0);
691 /*----- That's all, folks -------------------------------------------------*/