1*8583b49cSjoerg /* $NetBSD: revnetgroup.c,v 1.15 2011/08/30 21:10:29 joerg Exp $ */
27307c9a9Slukem
342f1aa04Slukem /*
442f1aa04Slukem * Copyright (c) 1995
542f1aa04Slukem * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
642f1aa04Slukem *
742f1aa04Slukem * Redistribution and use in source and binary forms, with or without
842f1aa04Slukem * modification, are permitted provided that the following conditions
942f1aa04Slukem * are met:
1042f1aa04Slukem * 1. Redistributions of source code must retain the above copyright
1142f1aa04Slukem * notice, this list of conditions and the following disclaimer.
1242f1aa04Slukem * 2. Redistributions in binary form must reproduce the above copyright
1342f1aa04Slukem * notice, this list of conditions and the following disclaimer in the
1442f1aa04Slukem * documentation and/or other materials provided with the distribution.
1542f1aa04Slukem * 3. All advertising materials mentioning features or use of this software
1642f1aa04Slukem * must display the following acknowledgement:
1742f1aa04Slukem * This product includes software developed by Bill Paul.
1842f1aa04Slukem * 4. Neither the name of the author nor the names of any co-contributors
1942f1aa04Slukem * may be used to endorse or promote products derived from this software
2042f1aa04Slukem * without specific prior written permission.
2142f1aa04Slukem *
2242f1aa04Slukem * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2342f1aa04Slukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2442f1aa04Slukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2542f1aa04Slukem * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2642f1aa04Slukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2742f1aa04Slukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2842f1aa04Slukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2942f1aa04Slukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3042f1aa04Slukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3142f1aa04Slukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3242f1aa04Slukem * SUCH DAMAGE.
3342f1aa04Slukem *
3442f1aa04Slukem * reverse netgroup map generator program
3542f1aa04Slukem *
3642f1aa04Slukem * Written by Bill Paul <wpaul@ctr.columbia.edu>
3742f1aa04Slukem * Center for Telecommunications Research
3842f1aa04Slukem * Columbia University, New York City
3942f1aa04Slukem *
4042f1aa04Slukem */
4142f1aa04Slukem
427307c9a9Slukem #include <sys/cdefs.h>
437307c9a9Slukem #ifndef lint
44*8583b49cSjoerg __RCSID("$NetBSD: revnetgroup.c,v 1.15 2011/08/30 21:10:29 joerg Exp $");
457307c9a9Slukem #endif
467307c9a9Slukem
47f4fb444bSlukem #include <ctype.h>
487307c9a9Slukem #include <err.h>
497307c9a9Slukem #include <errno.h>
5042f1aa04Slukem #include <stdio.h>
51211a72a8Slukem #include <stdlib.h>
5242f1aa04Slukem #include <string.h>
5375a6e035Sperry #include <unistd.h>
547307c9a9Slukem
5542f1aa04Slukem #include "hash.h"
56f4fb444bSlukem #include "protos.h"
5742f1aa04Slukem
58*8583b49cSjoerg __dead static void usage(void);
597307c9a9Slukem
607307c9a9Slukem
6142f1aa04Slukem
6242f1aa04Slukem /* Default location of netgroup file. */
63d3d3aa62Slukem const char *netgroup = "/etc/netgroup";
6442f1aa04Slukem
6542f1aa04Slukem /* Stored hash table version of 'forward' netgroup database. */
6642f1aa04Slukem struct group_entry *gtable[TABLESIZE];
6742f1aa04Slukem
6842f1aa04Slukem /*
6942f1aa04Slukem * Stored hash table of 'reverse' netgroup member database
7042f1aa04Slukem * which we will construct.
7142f1aa04Slukem */
7242f1aa04Slukem struct member_entry *mtable[TABLESIZE];
7342f1aa04Slukem
74*8583b49cSjoerg static void
usage(void)753a0b8f6eSwiz usage(void)
7642f1aa04Slukem {
777307c9a9Slukem
7825bdbb66Scgd fprintf (stderr,"usage: %s -u|-h [-f netgroup file]\n", getprogname());
7942f1aa04Slukem exit(1);
8042f1aa04Slukem }
8142f1aa04Slukem
8242f1aa04Slukem int
main(int argc,char * argv[])833a0b8f6eSwiz main(int argc, char *argv[])
8442f1aa04Slukem {
8542f1aa04Slukem struct group_entry *gcur;
8642f1aa04Slukem struct member_entry *mcur;
87f4fb444bSlukem FILE *fp;
88811d7dcaSlukem char *line, *p, *host, *user, *domain;
89ade3ce97Sthorpej int ch, i;
90ade3ce97Sthorpej size_t len;
91f4fb444bSlukem char *key;
92f4fb444bSlukem int hosts = -1;
9342f1aa04Slukem
9442f1aa04Slukem if (argc < 2)
957307c9a9Slukem usage();
9642f1aa04Slukem
9742f1aa04Slukem while ((ch = getopt(argc, argv, "uhf:")) != -1) {
9842f1aa04Slukem switch(ch) {
9942f1aa04Slukem case 'u':
10042f1aa04Slukem if (hosts != -1) {
10142f1aa04Slukem warnx("please use only one of -u or -h");
1027307c9a9Slukem usage();
10342f1aa04Slukem }
10442f1aa04Slukem hosts = 0;
10542f1aa04Slukem break;
10642f1aa04Slukem case 'h':
10742f1aa04Slukem if (hosts != -1) {
10842f1aa04Slukem warnx("please use only one of -u or -h");
1097307c9a9Slukem usage();
11042f1aa04Slukem }
11142f1aa04Slukem hosts = 1;
11242f1aa04Slukem break;
11342f1aa04Slukem case 'f':
11442f1aa04Slukem netgroup = optarg;
11542f1aa04Slukem break;
11642f1aa04Slukem default:
1177307c9a9Slukem usage();
11842f1aa04Slukem break;
11942f1aa04Slukem }
12042f1aa04Slukem }
12142f1aa04Slukem
12242f1aa04Slukem if (hosts == -1)
1237307c9a9Slukem usage();
12442f1aa04Slukem
12542f1aa04Slukem if (strcmp(netgroup, "-")) {
12642f1aa04Slukem if ((fp = fopen(netgroup, "r")) == NULL) {
127bbef2fbaSitojun err(1, "%s", netgroup);
12842f1aa04Slukem }
12942f1aa04Slukem } else {
13042f1aa04Slukem fp = stdin;
13142f1aa04Slukem }
13242f1aa04Slukem
13342f1aa04Slukem /* Stuff all the netgroup names and members into a hash table. */
134211a72a8Slukem for (;
135811d7dcaSlukem (line = fparseln(fp, &len, NULL, NULL, FPARSELN_UNESCALL));
136811d7dcaSlukem free(line)) {
137211a72a8Slukem if (len == 0)
13842f1aa04Slukem continue;
139811d7dcaSlukem p = line;
140f4fb444bSlukem
1413cca093eSdsl for (key = p; *p && isspace((unsigned char)*p) == 0; p++)
142f4fb444bSlukem ;
1433cca093eSdsl while (*p && isspace((unsigned char)*p))
144f4fb444bSlukem *p++ = '\0';
145f4fb444bSlukem store(gtable, key, p);
14642f1aa04Slukem }
14742f1aa04Slukem
14842f1aa04Slukem fclose(fp);
14942f1aa04Slukem
15042f1aa04Slukem /*
15142f1aa04Slukem * Find all members of each netgroup and keep track of which
15242f1aa04Slukem * group they belong to.
15342f1aa04Slukem */
15442f1aa04Slukem for (i = 0; i < TABLESIZE; i++) {
15542f1aa04Slukem gcur = gtable[i];
15642f1aa04Slukem while (gcur) {
1577307c9a9Slukem rng_setnetgrent(gcur->key);
158705a39bcSfvdl while (rng_getnetgrent(&host, &user, &domain) != 0) {
15942f1aa04Slukem if (hosts) {
16042f1aa04Slukem if (!(host && !strcmp(host,"-"))) {
16142f1aa04Slukem mstore(mtable,
16242f1aa04Slukem host ? host : "*",
16342f1aa04Slukem gcur->key,
16442f1aa04Slukem domain ? domain : "*");
16542f1aa04Slukem }
16642f1aa04Slukem } else {
16742f1aa04Slukem if (!(user && !strcmp(user,"-"))) {
16842f1aa04Slukem mstore(mtable,
16942f1aa04Slukem user ? user : "*",
17042f1aa04Slukem gcur->key,
17142f1aa04Slukem domain ? domain : "*");
17242f1aa04Slukem }
17342f1aa04Slukem }
17442f1aa04Slukem }
17542f1aa04Slukem gcur = gcur->next;
17642f1aa04Slukem }
17742f1aa04Slukem }
17842f1aa04Slukem
17942f1aa04Slukem /* Release resources used by the netgroup parser code. */
1807307c9a9Slukem rng_endnetgrent();
18142f1aa04Slukem
18242f1aa04Slukem /* Spew out the results. */
18342f1aa04Slukem for (i = 0; i < TABLESIZE; i++) {
18442f1aa04Slukem mcur = mtable[i];
18542f1aa04Slukem while (mcur) {
18642f1aa04Slukem struct grouplist *tmp;
18742f1aa04Slukem printf ("%s.%s\t", mcur->key, mcur->domain);
18842f1aa04Slukem tmp = mcur->groups;
18942f1aa04Slukem while (tmp) {
19042f1aa04Slukem printf ("%s", tmp->groupname);
19142f1aa04Slukem tmp = tmp->next;
19242f1aa04Slukem if (tmp)
19342f1aa04Slukem printf(",");
19442f1aa04Slukem }
19542f1aa04Slukem mcur = mcur->next;
19642f1aa04Slukem printf ("\n");
19742f1aa04Slukem }
19842f1aa04Slukem }
19942f1aa04Slukem
20042f1aa04Slukem /* Let the OS free all our resources. */
20142f1aa04Slukem exit(0);
20242f1aa04Slukem }
203