revnetgroup.c revision 1.5 1 /* $NetBSD: revnetgroup.c,v 1.5 1998/02/03 05:19:01 perry 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.5 1998/02/03 05:19:01 perry Exp $");
45 #endif
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.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, i;
93 size_t len;
94 char *key;
95 int hosts = -1;
96
97 if (argc < 2)
98 usage();
99
100 while ((ch = getopt(argc, argv, "uhf:")) != -1) {
101 switch(ch) {
102 case 'u':
103 if (hosts != -1) {
104 warnx("please use only one of -u or -h");
105 usage();
106 }
107 hosts = 0;
108 break;
109 case 'h':
110 if (hosts != -1) {
111 warnx("please use only one of -u or -h");
112 usage();
113 }
114 hosts = 1;
115 break;
116 case 'f':
117 netgroup = optarg;
118 break;
119 default:
120 usage();
121 break;
122 }
123 }
124
125 if (hosts == -1)
126 usage();
127
128 if (strcmp(netgroup, "-")) {
129 if ((fp = fopen(netgroup, "r")) == NULL) {
130 err(1,netgroup);
131 }
132 } else {
133 fp = stdin;
134 }
135
136 /* Stuff all the netgroup names and members into a hash table. */
137 while ((p = read_line(fp, &len, NULL)) != NULL) {
138 if (len == 0 || *p == '#')
139 continue;
140
141 for (key = p; *p && isspace(*p) == 0; p++)
142 ;
143 while (*p && isspace(*p))
144 *p++ = '\0';
145 store(gtable, key, p);
146 }
147
148 fclose(fp);
149
150 /*
151 * Find all members of each netgroup and keep track of which
152 * group they belong to.
153 */
154 for (i = 0; i < TABLESIZE; i++) {
155 gcur = gtable[i];
156 while(gcur) {
157 rng_setnetgrent(gcur->key);
158 while(rng_getnetgrent(&host, &user, &domain) != NULL) {
159 if (hosts) {
160 if (!(host && !strcmp(host,"-"))) {
161 mstore(mtable,
162 host ? host : "*",
163 gcur->key,
164 domain ? domain : "*");
165 }
166 } else {
167 if (!(user && !strcmp(user,"-"))) {
168 mstore(mtable,
169 user ? user : "*",
170 gcur->key,
171 domain ? domain : "*");
172 }
173 }
174 }
175 gcur = gcur->next;
176 }
177 }
178
179 /* Release resources used by the netgroup parser code. */
180 rng_endnetgrent();
181
182 /* Spew out the results. */
183 for (i = 0; i < TABLESIZE; i++) {
184 mcur = mtable[i];
185 while(mcur) {
186 struct grouplist *tmp;
187 printf ("%s.%s\t", mcur->key, mcur->domain);
188 tmp = mcur->groups;
189 while(tmp) {
190 printf ("%s", tmp->groupname);
191 tmp = tmp->next;
192 if (tmp)
193 printf(",");
194 }
195 mcur = mcur->next;
196 printf ("\n");
197 }
198 }
199
200 /* Let the OS free all our resources. */
201 exit(0);
202 }
203