Home | History | Annotate | Line # | Download | only in revnetgroup
revnetgroup.c revision 1.2
      1  1.2  lukem /*	$NetBSD: revnetgroup.c,v 1.2 1997/10/06 06:54:15 lukem Exp $ */
      2  1.2  lukem 
      3  1.1  lukem /*
      4  1.1  lukem  * Copyright (c) 1995
      5  1.1  lukem  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      8  1.1  lukem  * modification, are permitted provided that the following conditions
      9  1.1  lukem  * are met:
     10  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     11  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     12  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     15  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     16  1.1  lukem  *    must display the following acknowledgement:
     17  1.1  lukem  *	This product includes software developed by Bill Paul.
     18  1.1  lukem  * 4. Neither the name of the author nor the names of any co-contributors
     19  1.1  lukem  *    may be used to endorse or promote products derived from this software
     20  1.1  lukem  *    without specific prior written permission.
     21  1.1  lukem  *
     22  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  lukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
     26  1.1  lukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  lukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  lukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  lukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  lukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  lukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  lukem  * SUCH DAMAGE.
     33  1.1  lukem  *
     34  1.1  lukem  * reverse netgroup map generator program
     35  1.1  lukem  *
     36  1.1  lukem  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     37  1.1  lukem  * Center for Telecommunications Research
     38  1.1  lukem  * Columbia University, New York City
     39  1.1  lukem  *
     40  1.1  lukem  */
     41  1.1  lukem 
     42  1.2  lukem #include <sys/cdefs.h>
     43  1.2  lukem #ifndef lint
     44  1.2  lukem __RCSID("$NetBSD: revnetgroup.c,v 1.2 1997/10/06 06:54:15 lukem Exp $");
     45  1.2  lukem #endif
     46  1.2  lukem 
     47  1.2  lukem #include <err.h>
     48  1.2  lukem #include <errno.h>
     49  1.1  lukem #include <stdio.h>
     50  1.1  lukem #include <stdlib.h>
     51  1.1  lukem #include <string.h>
     52  1.2  lukem 
     53  1.1  lukem #include "hash.h"
     54  1.1  lukem 
     55  1.2  lukem int	main __P((int, char *[]));
     56  1.2  lukem void	usage __P((void));
     57  1.2  lukem 
     58  1.2  lukem 
     59  1.1  lukem 
     60  1.1  lukem /* Default location of netgroup file. */
     61  1.1  lukem char *netgroup = "/etc/netgroup";
     62  1.1  lukem 
     63  1.1  lukem /* Stored hash table version of 'forward' netgroup database. */
     64  1.1  lukem struct group_entry *gtable[TABLESIZE];
     65  1.1  lukem 
     66  1.1  lukem /*
     67  1.1  lukem  * Stored hash table of 'reverse' netgroup member database
     68  1.1  lukem  * which we will construct.
     69  1.1  lukem  */
     70  1.1  lukem struct member_entry *mtable[TABLESIZE];
     71  1.1  lukem 
     72  1.2  lukem void
     73  1.2  lukem usage()
     74  1.1  lukem {
     75  1.2  lukem 	extern char *__progname;		/* from crt0.o */
     76  1.2  lukem 
     77  1.2  lukem 	fprintf (stderr,"usage: %s -u|-h [-f netgroup file]\n", __progname);
     78  1.1  lukem 	exit(1);
     79  1.1  lukem }
     80  1.1  lukem 
     81  1.1  lukem int
     82  1.1  lukem main(argc, argv)
     83  1.1  lukem 	int argc;
     84  1.1  lukem 	char *argv[];
     85  1.1  lukem {
     86  1.1  lukem 	FILE *fp;
     87  1.1  lukem 	char readbuf[LINSIZ];
     88  1.1  lukem 	struct group_entry *gcur;
     89  1.1  lukem 	struct member_entry *mcur;
     90  1.1  lukem 	char *host, *user, *domain;
     91  1.1  lukem 	int ch;
     92  1.1  lukem 	char *key = NULL, *data = NULL;
     93  1.1  lukem 	int hosts = -1, i;
     94  1.1  lukem 
     95  1.1  lukem 	if (argc < 2)
     96  1.2  lukem 		usage();
     97  1.1  lukem 
     98  1.1  lukem 	while ((ch = getopt(argc, argv, "uhf:")) != -1) {
     99  1.1  lukem 		switch(ch) {
    100  1.1  lukem 		case 'u':
    101  1.1  lukem 			if (hosts != -1) {
    102  1.1  lukem 				warnx("please use only one of -u or -h");
    103  1.2  lukem 				usage();
    104  1.1  lukem 			}
    105  1.1  lukem 			hosts = 0;
    106  1.1  lukem 			break;
    107  1.1  lukem 		case 'h':
    108  1.1  lukem 			if (hosts != -1) {
    109  1.1  lukem 				warnx("please use only one of -u or -h");
    110  1.2  lukem 				usage();
    111  1.1  lukem 			}
    112  1.1  lukem 			hosts = 1;
    113  1.1  lukem 			break;
    114  1.1  lukem 		case 'f':
    115  1.1  lukem 			netgroup = optarg;
    116  1.1  lukem 			break;
    117  1.1  lukem 		default:
    118  1.2  lukem 			usage();
    119  1.1  lukem 			break;
    120  1.1  lukem 		}
    121  1.1  lukem 	}
    122  1.1  lukem 
    123  1.1  lukem 	if (hosts == -1)
    124  1.2  lukem 		usage();
    125  1.1  lukem 
    126  1.1  lukem 	if (strcmp(netgroup, "-")) {
    127  1.1  lukem 		if ((fp = fopen(netgroup, "r")) == NULL) {
    128  1.1  lukem 			err(1,netgroup);
    129  1.1  lukem 		}
    130  1.1  lukem 	} else {
    131  1.1  lukem 		fp = stdin;
    132  1.1  lukem 	}
    133  1.1  lukem 
    134  1.1  lukem 	/* Stuff all the netgroup names and members into a hash table. */
    135  1.1  lukem 	while (fgets(readbuf, LINSIZ, fp)) {
    136  1.1  lukem 		if (readbuf[0] == '#')
    137  1.1  lukem 			continue;
    138  1.1  lukem 		/* handle backslash line continuations */
    139  1.1  lukem 		while(readbuf[strlen(readbuf) - 2] == '\\') {
    140  1.1  lukem 			fgets((char *)&readbuf[strlen(readbuf) - 2],
    141  1.1  lukem 					sizeof(readbuf) - strlen(readbuf), fp);
    142  1.1  lukem 		}
    143  1.1  lukem 		data = NULL;
    144  1.1  lukem 		if ((data = (char *)(strpbrk(readbuf, " \t") + 1)) < (char *)2)
    145  1.1  lukem 			continue;
    146  1.1  lukem 		key = (char *)&readbuf;
    147  1.1  lukem 		*(data - 1) = '\0';
    148  1.1  lukem 		store(gtable, key, data);
    149  1.1  lukem 	}
    150  1.1  lukem 
    151  1.1  lukem 	fclose(fp);
    152  1.1  lukem 
    153  1.1  lukem 	/*
    154  1.1  lukem 	 * Find all members of each netgroup and keep track of which
    155  1.1  lukem 	 * group they belong to.
    156  1.1  lukem 	 */
    157  1.1  lukem 	for (i = 0; i < TABLESIZE; i++) {
    158  1.1  lukem 		gcur = gtable[i];
    159  1.1  lukem 		while(gcur) {
    160  1.2  lukem 			rng_setnetgrent(gcur->key);
    161  1.2  lukem 			while(rng_getnetgrent(&host, &user, &domain) != NULL) {
    162  1.1  lukem 				if (hosts) {
    163  1.1  lukem 					if (!(host && !strcmp(host,"-"))) {
    164  1.1  lukem 						mstore(mtable,
    165  1.1  lukem 						       host ? host : "*",
    166  1.1  lukem 						       gcur->key,
    167  1.1  lukem 						       domain ? domain : "*");
    168  1.1  lukem 					}
    169  1.1  lukem 				} else {
    170  1.1  lukem 					if (!(user && !strcmp(user,"-"))) {
    171  1.1  lukem 						mstore(mtable,
    172  1.1  lukem 						       user ? user : "*",
    173  1.1  lukem 						       gcur->key,
    174  1.1  lukem 						       domain ? domain : "*");
    175  1.1  lukem 					}
    176  1.1  lukem 				}
    177  1.1  lukem 			}
    178  1.1  lukem 			gcur = gcur->next;
    179  1.1  lukem 		}
    180  1.1  lukem 	}
    181  1.1  lukem 
    182  1.1  lukem 	/* Release resources used by the netgroup parser code. */
    183  1.2  lukem 	rng_endnetgrent();
    184  1.1  lukem 
    185  1.1  lukem 	/* Spew out the results. */
    186  1.1  lukem 	for (i = 0; i < TABLESIZE; i++) {
    187  1.1  lukem 		mcur = mtable[i];
    188  1.1  lukem 		while(mcur) {
    189  1.1  lukem 			struct grouplist *tmp;
    190  1.1  lukem 			printf ("%s.%s\t", mcur->key, mcur->domain);
    191  1.1  lukem 			tmp = mcur->groups;
    192  1.1  lukem 			while(tmp) {
    193  1.1  lukem 				printf ("%s", tmp->groupname);
    194  1.1  lukem 				tmp = tmp->next;
    195  1.1  lukem 				if (tmp)
    196  1.1  lukem 					printf(",");
    197  1.1  lukem 			}
    198  1.1  lukem 			mcur = mcur->next;
    199  1.1  lukem 			printf ("\n");
    200  1.1  lukem 		}
    201  1.1  lukem 	}
    202  1.1  lukem 
    203  1.1  lukem 	/* Let the OS free all our resources. */
    204  1.1  lukem 	exit(0);
    205  1.1  lukem }
    206