]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
a2c93a76 A |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
b7080c8e A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
a2c93a76 A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
b7080c8e A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | /* $OpenBSD: revnetgroup.c,v 1.1 1997/04/15 22:06:15 maja Exp $ */ | |
25 | /* | |
26 | * Copyright (c) 1995 | |
27 | * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by Bill Paul. | |
40 | * 4. Neither the name of the author nor the names of any co-contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | * | |
56 | * reverse netgroup map generator program | |
57 | * | |
58 | * Written by Bill Paul <wpaul@ctr.columbia.edu> | |
59 | * Center for Telecommunications Research | |
60 | * Columbia University, New York City | |
61 | * | |
62 | * $FreeBSD: revnetgroup.c,v 1.7 1997/03/28 15:48:15 imp Exp $ | |
63 | */ | |
64 | ||
65 | #include <stdio.h> | |
66 | #include <stdlib.h> | |
67 | #include <string.h> | |
68 | #include <errno.h> | |
69 | #include <err.h> | |
70 | #include "hash.h" | |
71 | ||
72 | #ifndef lint | |
73 | static const char rcsid[] = "$OpenBSD: revnetgroup.c,v 1.1 1997/04/15 22:06:15 maja Exp $"; | |
74 | #endif | |
75 | ||
76 | /* Default location of netgroup file. */ | |
77 | char *netgroup = "/etc/netgroup"; | |
78 | ||
79 | /* Stored hash table version of 'forward' netgroup database. */ | |
80 | struct group_entry *gtable[TABLESIZE]; | |
81 | ||
82 | /* | |
83 | * Stored hash table of 'reverse' netgroup member database | |
84 | * which we will construct. | |
85 | */ | |
86 | struct member_entry *mtable[TABLESIZE]; | |
87 | ||
88 | void usage(prog) | |
89 | char *prog; | |
90 | { | |
91 | fprintf (stderr,"usage: %s -u|-h [-f netgroup file]\n",prog); | |
92 | exit(1); | |
93 | } | |
94 | ||
95 | extern char *optarg; | |
96 | ||
97 | int | |
98 | main(argc, argv) | |
99 | int argc; | |
100 | char *argv[]; | |
101 | { | |
102 | FILE *fp; | |
103 | char readbuf[LINSIZ]; | |
104 | struct group_entry *gcur; | |
105 | struct member_entry *mcur; | |
106 | char *host, *user, *domain; | |
107 | int ch; | |
108 | char *key = NULL, *data = NULL; | |
109 | int hosts = -1, i; | |
110 | ||
111 | if (argc < 2) | |
112 | usage(argv[0]); | |
113 | ||
114 | while ((ch = getopt(argc, argv, "uhf:")) != -1) { | |
115 | switch(ch) { | |
116 | case 'u': | |
117 | if (hosts != -1) { | |
118 | warnx("please use only one of -u or -h"); | |
119 | usage(argv[0]); | |
120 | } | |
121 | hosts = 0; | |
122 | break; | |
123 | case 'h': | |
124 | if (hosts != -1) { | |
125 | warnx("please use only one of -u or -h"); | |
126 | usage(argv[0]); | |
127 | } | |
128 | hosts = 1; | |
129 | break; | |
130 | case 'f': | |
131 | netgroup = optarg; | |
132 | break; | |
133 | default: | |
134 | usage(argv[0]); | |
135 | break; | |
136 | } | |
137 | } | |
138 | ||
139 | if (hosts == -1) | |
140 | usage(argv[0]); | |
141 | ||
142 | if (strcmp(netgroup, "-")) { | |
143 | if ((fp = fopen(netgroup, "r")) == NULL) { | |
144 | err(1,netgroup); | |
145 | } | |
146 | } else { | |
147 | fp = stdin; | |
148 | } | |
149 | ||
150 | /* Stuff all the netgroup names and members into a hash table. */ | |
151 | while (fgets(readbuf, LINSIZ, fp)) { | |
152 | if (readbuf[0] == '#') | |
153 | continue; | |
154 | /* handle backslash line continuations */ | |
155 | while(readbuf[strlen(readbuf) - 2] == '\\') { | |
156 | fgets((char *)&readbuf[strlen(readbuf) - 2], | |
157 | sizeof(readbuf) - strlen(readbuf), fp); | |
158 | } | |
159 | data = NULL; | |
160 | if ((data = (char *)(strpbrk(readbuf, " \t") + 1)) < (char *)2) | |
161 | continue; | |
162 | key = (char *)&readbuf; | |
163 | *(data - 1) = '\0'; | |
164 | store(gtable, key, data); | |
165 | } | |
166 | ||
167 | fclose(fp); | |
168 | ||
169 | /* | |
170 | * Find all members of each netgroup and keep track of which | |
171 | * group they belong to. | |
172 | */ | |
173 | for (i = 0; i < TABLESIZE; i++) { | |
174 | gcur = gtable[i]; | |
175 | while(gcur) { | |
176 | __setnetgrent(gcur->key); | |
177 | while(__getnetgrent(&host, &user, &domain) != NULL) { | |
178 | if (hosts) { | |
179 | if (!(host && !strcmp(host,"-"))) { | |
180 | mstore(mtable, | |
181 | host ? host : "*", | |
182 | gcur->key, | |
183 | domain ? domain : "*"); | |
184 | } | |
185 | } else { | |
186 | if (!(user && !strcmp(user,"-"))) { | |
187 | mstore(mtable, | |
188 | user ? user : "*", | |
189 | gcur->key, | |
190 | domain ? domain : "*"); | |
191 | } | |
192 | } | |
193 | } | |
194 | gcur = gcur->next; | |
195 | } | |
196 | } | |
197 | ||
198 | /* Release resources used by the netgroup parser code. */ | |
199 | __endnetgrent(); | |
200 | ||
201 | /* Spew out the results. */ | |
202 | for (i = 0; i < TABLESIZE; i++) { | |
203 | mcur = mtable[i]; | |
204 | while(mcur) { | |
205 | struct grouplist *tmp; | |
206 | printf ("%s.%s\t", mcur->key, mcur->domain); | |
207 | tmp = mcur->groups; | |
208 | while(tmp) { | |
209 | printf ("%s", tmp->groupname); | |
210 | tmp = tmp->next; | |
211 | if (tmp) | |
212 | printf(","); | |
213 | } | |
214 | mcur = mcur->next; | |
215 | printf ("\n"); | |
216 | } | |
217 | } | |
218 | ||
219 | /* Let the OS free all our resources. */ | |
220 | exit(0); | |
221 | } |