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