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