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