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