/*-1* SPDX-License-Identifier: BSD-4-Clause2*3* Copyright (c) 19954* Bill Paul <[email protected]>. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. All advertising materials mentioning features or use of this software15* must display the following acknowledgement:16* This product includes software developed by Bill Paul.17* 4. Neither the name of the author nor the names of any co-contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33* reverse netgroup map generator program34*35* Written by Bill Paul <[email protected]>36* Center for Telecommunications Research37* Columbia University, New York City38*/3940#include <err.h>41#include <stdio.h>42#include <stdlib.h>43#include <string.h>44#include <unistd.h>45#include "hash.h"4647/* Default location of netgroup file. */48char *netgroup = "/etc/netgroup";4950/* Stored hash table version of 'forward' netgroup database. */51struct group_entry *gtable[TABLESIZE];5253/*54* Stored hash table of 'reverse' netgroup member database55* which we will construct.56*/57struct member_entry *mtable[TABLESIZE];5859static void60usage(void)61{62fprintf (stderr,"usage: revnetgroup -u | -h [-f netgroup_file]\n");63exit(1);64}6566int67main(int argc, char *argv[])68{69FILE *fp;70char readbuf[LINSIZ];71struct group_entry *gcur;72struct member_entry *mcur;73char *host, *user, *domain;74int ch;75char *key = NULL, *data = NULL;76int hosts = -1, i;7778if (argc < 2)79usage();8081while ((ch = getopt(argc, argv, "uhf:")) != -1) {82switch(ch) {83case 'u':84if (hosts != -1) {85warnx("please use only one of -u or -h");86usage();87}88hosts = 0;89break;90case 'h':91if (hosts != -1) {92warnx("please use only one of -u or -h");93usage();94}95hosts = 1;96break;97case 'f':98netgroup = optarg;99break;100default:101usage();102break;103}104}105106if (hosts == -1)107usage();108109if (strcmp(netgroup, "-")) {110if ((fp = fopen(netgroup, "r")) == NULL) {111err(1, "%s", netgroup);112}113} else {114fp = stdin;115}116117/* Stuff all the netgroup names and members into a hash table. */118while (fgets(readbuf, LINSIZ, fp)) {119if (readbuf[0] == '#')120continue;121/* handle backslash line continuations */122while(readbuf[strlen(readbuf) - 2] == '\\') {123fgets((char *)&readbuf[strlen(readbuf) - 2],124sizeof(readbuf) - strlen(readbuf), fp);125}126data = NULL;127if ((data = (char *)(strpbrk(readbuf, " \t") + 1)) < (char *)2)128continue;129key = (char *)&readbuf;130*(data - 1) = '\0';131store(gtable, key, data);132}133134fclose(fp);135136/*137* Find all members of each netgroup and keep track of which138* group they belong to.139*/140for (i = 0; i < TABLESIZE; i++) {141gcur = gtable[i];142while(gcur) {143__setnetgrent(gcur->key);144while(__getnetgrent(&host, &user, &domain) != 0) {145if (hosts ? host && strcmp(host,"-") : user && strcmp(user, "-"))146mstore(mtable, hosts ? host : user, gcur->key, domain);147}148gcur = gcur->next;149}150}151152/* Release resources used by the netgroup parser code. */153__endnetgrent();154155/* Spew out the results. */156for (i = 0; i < TABLESIZE; i++) {157mcur = mtable[i];158while(mcur) {159struct grouplist *tmp;160printf ("%s.%s\t", mcur->key, mcur->domain);161tmp = mcur->groups;162while(tmp) {163printf ("%s", tmp->groupname);164tmp = tmp->next;165if (tmp)166printf(",");167}168mcur = mcur->next;169printf ("\n");170}171}172173/* Let the OS free all our resources. */174exit(0);175}176177178