revnetgroup.c revision 1.6 1 /* $NetBSD: revnetgroup.c,v 1.6 1998/06/08 06:53:49 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.6 1998/06/08 06:53:49 lukem 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 __P((int, char *[]));
60 void usage __P((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()
78 {
79 extern char *__progname; /* from crt0.o */
80
81 fprintf (stderr,"usage: %s -u|-h [-f netgroup file]\n", __progname);
82 exit(1);
83 }
84
85 int
86 main(argc, argv)
87 int argc;
88 char *argv[];
89 {
90 struct group_entry *gcur;
91 struct member_entry *mcur;
92 FILE *fp;
93 char *p, *host, *user, *domain;
94 int ch, i;
95 size_t len;
96 char *key;
97 int hosts = -1;
98
99 if (argc < 2)
100 usage();
101
102 while ((ch = getopt(argc, argv, "uhf:")) != -1) {
103 switch(ch) {
104 case 'u':
105 if (hosts != -1) {
106 warnx("please use only one of -u or -h");
107 usage();
108 }
109 hosts = 0;
110 break;
111 case 'h':
112 if (hosts != -1) {
113 warnx("please use only one of -u or -h");
114 usage();
115 }
116 hosts = 1;
117 break;
118 case 'f':
119 netgroup = optarg;
120 break;
121 default:
122 usage();
123 break;
124 }
125 }
126
127 if (hosts == -1)
128 usage();
129
130 if (strcmp(netgroup, "-")) {
131 if ((fp = fopen(netgroup, "r")) == NULL) {
132 err(1,netgroup);
133 }
134 } else {
135 fp = stdin;
136 }
137
138 /* Stuff all the netgroup names and members into a hash table. */
139 for (;
140 (p = fparseln(fp, &len, NULL, NULL, FPARSELN_UNESCALL));
141 free(p)) {
142 if (len == 0)
143 continue;
144
145 for (key = p; *p && isspace(*p) == 0; p++)
146 ;
147 while (*p && isspace(*p))
148 *p++ = '\0';
149 store(gtable, key, p);
150 }
151
152 fclose(fp);
153
154 /*
155 * Find all members of each netgroup and keep track of which
156 * group they belong to.
157 */
158 for (i = 0; i < TABLESIZE; i++) {
159 gcur = gtable[i];
160 while(gcur) {
161 rng_setnetgrent(gcur->key);
162 while(rng_getnetgrent(&host, &user, &domain) != NULL) {
163 if (hosts) {
164 if (!(host && !strcmp(host,"-"))) {
165 mstore(mtable,
166 host ? host : "*",
167 gcur->key,
168 domain ? domain : "*");
169 }
170 } else {
171 if (!(user && !strcmp(user,"-"))) {
172 mstore(mtable,
173 user ? user : "*",
174 gcur->key,
175 domain ? domain : "*");
176 }
177 }
178 }
179 gcur = gcur->next;
180 }
181 }
182
183 /* Release resources used by the netgroup parser code. */
184 rng_endnetgrent();
185
186 /* Spew out the results. */
187 for (i = 0; i < TABLESIZE; i++) {
188 mcur = mtable[i];
189 while(mcur) {
190 struct grouplist *tmp;
191 printf ("%s.%s\t", mcur->key, mcur->domain);
192 tmp = mcur->groups;
193 while(tmp) {
194 printf ("%s", tmp->groupname);
195 tmp = tmp->next;
196 if (tmp)
197 printf(",");
198 }
199 mcur = mcur->next;
200 printf ("\n");
201 }
202 }
203
204 /* Let the OS free all our resources. */
205 exit(0);
206 }
207