1df57947fSPedro F. Giffuni /*-
2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni *
4a44e4d14SBill Paul * Copyright (c) 1995
5a44e4d14SBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6a44e4d14SBill Paul *
7a44e4d14SBill Paul * Redistribution and use in source and binary forms, with or without
8a44e4d14SBill Paul * modification, are permitted provided that the following conditions
9a44e4d14SBill Paul * are met:
10a44e4d14SBill Paul * 1. Redistributions of source code must retain the above copyright
11a44e4d14SBill Paul * notice, this list of conditions and the following disclaimer.
12a44e4d14SBill Paul * 2. Redistributions in binary form must reproduce the above copyright
13a44e4d14SBill Paul * notice, this list of conditions and the following disclaimer in the
14a44e4d14SBill Paul * documentation and/or other materials provided with the distribution.
15a44e4d14SBill Paul * 3. All advertising materials mentioning features or use of this software
16a44e4d14SBill Paul * must display the following acknowledgement:
17a44e4d14SBill Paul * This product includes software developed by Bill Paul.
18a44e4d14SBill Paul * 4. Neither the name of the author nor the names of any co-contributors
19a44e4d14SBill Paul * may be used to endorse or promote products derived from this software
20a44e4d14SBill Paul * without specific prior written permission.
21a44e4d14SBill Paul *
22a44e4d14SBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23a44e4d14SBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a44e4d14SBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a44e4d14SBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26a44e4d14SBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a44e4d14SBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a44e4d14SBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a44e4d14SBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a44e4d14SBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a44e4d14SBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a44e4d14SBill Paul * SUCH DAMAGE.
33a44e4d14SBill Paul *
34a44e4d14SBill Paul * reverse netgroup map generator program
35a44e4d14SBill Paul *
36a44e4d14SBill Paul * Written by Bill Paul <wpaul@ctr.columbia.edu>
37a44e4d14SBill Paul * Center for Telecommunications Research
38a44e4d14SBill Paul * Columbia University, New York City
39a44e4d14SBill Paul */
40a44e4d14SBill Paul
413029b69fSPhilippe Charnier #include <err.h>
42a44e4d14SBill Paul #include <stdio.h>
43a44e4d14SBill Paul #include <stdlib.h>
44a44e4d14SBill Paul #include <string.h>
453029b69fSPhilippe Charnier #include <unistd.h>
46a44e4d14SBill Paul #include "hash.h"
47a44e4d14SBill Paul
48a44e4d14SBill Paul /* Default location of netgroup file. */
49a44e4d14SBill Paul char *netgroup = "/etc/netgroup";
50a44e4d14SBill Paul
51a44e4d14SBill Paul /* Stored hash table version of 'forward' netgroup database. */
52a44e4d14SBill Paul struct group_entry *gtable[TABLESIZE];
53a44e4d14SBill Paul
54a44e4d14SBill Paul /*
55a44e4d14SBill Paul * Stored hash table of 'reverse' netgroup member database
56a44e4d14SBill Paul * which we will construct.
57a44e4d14SBill Paul */
58a44e4d14SBill Paul struct member_entry *mtable[TABLESIZE];
59a44e4d14SBill Paul
603029b69fSPhilippe Charnier static void
usage(void)61266ebcd3SWarner Losh usage(void)
62a44e4d14SBill Paul {
63ee3b44f5SRuslan Ermilov fprintf (stderr,"usage: revnetgroup -u | -h [-f netgroup_file]\n");
64a44e4d14SBill Paul exit(1);
65a44e4d14SBill Paul }
66a44e4d14SBill Paul
6779cc85d3SBill Paul int
main(int argc,char * argv[])68266ebcd3SWarner Losh main(int argc, char *argv[])
69a44e4d14SBill Paul {
70a44e4d14SBill Paul FILE *fp;
71a44e4d14SBill Paul char readbuf[LINSIZ];
72a44e4d14SBill Paul struct group_entry *gcur;
73a44e4d14SBill Paul struct member_entry *mcur;
74a44e4d14SBill Paul char *host, *user, *domain;
75d7b71c67SBill Paul int ch;
76a44e4d14SBill Paul char *key = NULL, *data = NULL;
77d7b71c67SBill Paul int hosts = -1, i;
78a44e4d14SBill Paul
79a44e4d14SBill Paul if (argc < 2)
803029b69fSPhilippe Charnier usage();
81a44e4d14SBill Paul
8291477cc4SWarner Losh while ((ch = getopt(argc, argv, "uhf:")) != -1) {
83a44e4d14SBill Paul switch(ch) {
84a44e4d14SBill Paul case 'u':
85d7b71c67SBill Paul if (hosts != -1) {
86d7b71c67SBill Paul warnx("please use only one of -u or -h");
873029b69fSPhilippe Charnier usage();
88d7b71c67SBill Paul }
89a44e4d14SBill Paul hosts = 0;
90a44e4d14SBill Paul break;
91a44e4d14SBill Paul case 'h':
92d7b71c67SBill Paul if (hosts != -1) {
93d7b71c67SBill Paul warnx("please use only one of -u or -h");
943029b69fSPhilippe Charnier usage();
95d7b71c67SBill Paul }
96a44e4d14SBill Paul hosts = 1;
97a44e4d14SBill Paul break;
98a44e4d14SBill Paul case 'f':
99a44e4d14SBill Paul netgroup = optarg;
100a44e4d14SBill Paul break;
101a44e4d14SBill Paul default:
1023029b69fSPhilippe Charnier usage();
103a44e4d14SBill Paul break;
104a44e4d14SBill Paul }
105a44e4d14SBill Paul }
106a44e4d14SBill Paul
107d7b71c67SBill Paul if (hosts == -1)
1083029b69fSPhilippe Charnier usage();
109d7b71c67SBill Paul
110a44e4d14SBill Paul if (strcmp(netgroup, "-")) {
111a44e4d14SBill Paul if ((fp = fopen(netgroup, "r")) == NULL) {
112fcee96bdSKris Kennaway err(1, "%s", netgroup);
113a44e4d14SBill Paul }
114a44e4d14SBill Paul } else {
115a44e4d14SBill Paul fp = stdin;
116a44e4d14SBill Paul }
117a44e4d14SBill Paul
118a44e4d14SBill Paul /* Stuff all the netgroup names and members into a hash table. */
119a44e4d14SBill Paul while (fgets(readbuf, LINSIZ, fp)) {
120a44e4d14SBill Paul if (readbuf[0] == '#')
121a44e4d14SBill Paul continue;
12279cc85d3SBill Paul /* handle backslash line continuations */
12379cc85d3SBill Paul while(readbuf[strlen(readbuf) - 2] == '\\') {
12479cc85d3SBill Paul fgets((char *)&readbuf[strlen(readbuf) - 2],
12579cc85d3SBill Paul sizeof(readbuf) - strlen(readbuf), fp);
12679cc85d3SBill Paul }
12779cc85d3SBill Paul data = NULL;
128a44e4d14SBill Paul if ((data = (char *)(strpbrk(readbuf, " \t") + 1)) < (char *)2)
129a44e4d14SBill Paul continue;
130a44e4d14SBill Paul key = (char *)&readbuf;
131a44e4d14SBill Paul *(data - 1) = '\0';
132a44e4d14SBill Paul store(gtable, key, data);
133a44e4d14SBill Paul }
134a44e4d14SBill Paul
135a44e4d14SBill Paul fclose(fp);
136a44e4d14SBill Paul
137a44e4d14SBill Paul /*
138a44e4d14SBill Paul * Find all members of each netgroup and keep track of which
139a44e4d14SBill Paul * group they belong to.
140a44e4d14SBill Paul */
141a44e4d14SBill Paul for (i = 0; i < TABLESIZE; i++) {
142a44e4d14SBill Paul gcur = gtable[i];
143a44e4d14SBill Paul while(gcur) {
144a44e4d14SBill Paul __setnetgrent(gcur->key);
145d030d2d2SPoul-Henning Kamp while(__getnetgrent(&host, &user, &domain) != 0) {
146a44e4d14SBill Paul if (hosts ? host && strcmp(host,"-") : user && strcmp(user, "-"))
147a44e4d14SBill Paul mstore(mtable, hosts ? host : user, gcur->key, domain);
148a44e4d14SBill Paul }
149a44e4d14SBill Paul gcur = gcur->next;
150a44e4d14SBill Paul }
151a44e4d14SBill Paul }
152a44e4d14SBill Paul
153a44e4d14SBill Paul /* Release resources used by the netgroup parser code. */
154a44e4d14SBill Paul __endnetgrent();
155a44e4d14SBill Paul
156a44e4d14SBill Paul /* Spew out the results. */
157a44e4d14SBill Paul for (i = 0; i < TABLESIZE; i++) {
158a44e4d14SBill Paul mcur = mtable[i];
159a44e4d14SBill Paul while(mcur) {
160a44e4d14SBill Paul struct grouplist *tmp;
161a44e4d14SBill Paul printf ("%s.%s\t", mcur->key, mcur->domain);
162a44e4d14SBill Paul tmp = mcur->groups;
163a44e4d14SBill Paul while(tmp) {
164a44e4d14SBill Paul printf ("%s", tmp->groupname);
165a44e4d14SBill Paul tmp = tmp->next;
166a44e4d14SBill Paul if (tmp)
167a44e4d14SBill Paul printf(",");
168a44e4d14SBill Paul }
169a44e4d14SBill Paul mcur = mcur->next;
170a44e4d14SBill Paul printf ("\n");
171a44e4d14SBill Paul }
172a44e4d14SBill Paul }
173a44e4d14SBill Paul
174a44e4d14SBill Paul /* Let the OS free all our resources. */
175a44e4d14SBill Paul exit(0);
176a44e4d14SBill Paul }
177