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