Home | History | Annotate | Line # | Download | only in revnetgroup
      1 /*	$NetBSD: revnetgroup.c,v 1.15 2011/08/30 21:10:29 joerg 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.15 2011/08/30 21:10:29 joerg 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 
     55 #include "hash.h"
     56 #include "protos.h"
     57 
     58 __dead static void	usage(void);
     59 
     60 
     61 
     62 /* Default location of netgroup file. */
     63 const char *netgroup = "/etc/netgroup";
     64 
     65 /* Stored hash table version of 'forward' netgroup database. */
     66 struct group_entry *gtable[TABLESIZE];
     67 
     68 /*
     69  * Stored hash table of 'reverse' netgroup member database
     70  * which we will construct.
     71  */
     72 struct member_entry *mtable[TABLESIZE];
     73 
     74 static void
     75 usage(void)
     76 {
     77 
     78 	fprintf (stderr,"usage: %s -u|-h [-f netgroup file]\n", getprogname());
     79 	exit(1);
     80 }
     81 
     82 int
     83 main(int argc, char *argv[])
     84 {
     85 	struct group_entry *gcur;
     86 	struct member_entry *mcur;
     87 	FILE	*fp;
     88 	char	*line, *p, *host, *user, *domain;
     89 	int	 ch, i;
     90 	size_t	 len;
     91 	char	*key;
     92 	int	 hosts = -1;
     93 
     94 	if (argc < 2)
     95 		usage();
     96 
     97 	while ((ch = getopt(argc, argv, "uhf:")) != -1) {
     98 		switch(ch) {
     99 		case 'u':
    100 			if (hosts != -1) {
    101 				warnx("please use only one of -u or -h");
    102 				usage();
    103 			}
    104 			hosts = 0;
    105 			break;
    106 		case 'h':
    107 			if (hosts != -1) {
    108 				warnx("please use only one of -u or -h");
    109 				usage();
    110 			}
    111 			hosts = 1;
    112 			break;
    113 		case 'f':
    114 			netgroup = optarg;
    115 			break;
    116 		default:
    117 			usage();
    118 			break;
    119 		}
    120 	}
    121 
    122 	if (hosts == -1)
    123 		usage();
    124 
    125 	if (strcmp(netgroup, "-")) {
    126 		if ((fp = fopen(netgroup, "r")) == NULL) {
    127 			err(1, "%s", netgroup);
    128 		}
    129 	} else {
    130 		fp = stdin;
    131 	}
    132 
    133 	/* Stuff all the netgroup names and members into a hash table. */
    134 	for (;
    135 	    (line = fparseln(fp, &len, NULL, NULL, FPARSELN_UNESCALL));
    136 	    free(line)) {
    137 		if (len == 0)
    138 			continue;
    139 		p = line;
    140 
    141 		for (key = p; *p && isspace((unsigned char)*p) == 0; p++)
    142 			;
    143 		while (*p && isspace((unsigned char)*p))
    144 			*p++ = '\0';
    145 		store(gtable, key, p);
    146 	}
    147 
    148 	fclose(fp);
    149 
    150 	/*
    151 	 * Find all members of each netgroup and keep track of which
    152 	 * group they belong to.
    153 	 */
    154 	for (i = 0; i < TABLESIZE; i++) {
    155 		gcur = gtable[i];
    156 		while (gcur) {
    157 			rng_setnetgrent(gcur->key);
    158 			while (rng_getnetgrent(&host, &user, &domain) != 0) {
    159 				if (hosts) {
    160 					if (!(host && !strcmp(host,"-"))) {
    161 						mstore(mtable,
    162 						       host ? host : "*",
    163 						       gcur->key,
    164 						       domain ? domain : "*");
    165 					}
    166 				} else {
    167 					if (!(user && !strcmp(user,"-"))) {
    168 						mstore(mtable,
    169 						       user ? user : "*",
    170 						       gcur->key,
    171 						       domain ? domain : "*");
    172 					}
    173 				}
    174 			}
    175 			gcur = gcur->next;
    176 		}
    177 	}
    178 
    179 	/* Release resources used by the netgroup parser code. */
    180 	rng_endnetgrent();
    181 
    182 	/* Spew out the results. */
    183 	for (i = 0; i < TABLESIZE; i++) {
    184 		mcur = mtable[i];
    185 		while (mcur) {
    186 			struct grouplist *tmp;
    187 			printf ("%s.%s\t", mcur->key, mcur->domain);
    188 			tmp = mcur->groups;
    189 			while (tmp) {
    190 				printf ("%s", tmp->groupname);
    191 				tmp = tmp->next;
    192 				if (tmp)
    193 					printf(",");
    194 			}
    195 			mcur = mcur->next;
    196 			printf ("\n");
    197 		}
    198 	}
    199 
    200 	/* Let the OS free all our resources. */
    201 	exit(0);
    202 }
    203