1995-10-26 16:25:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995
|
|
|
|
* Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Bill Paul.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* reverse netgroup map generator program
|
|
|
|
*
|
|
|
|
* Written by Bill Paul <wpaul@ctr.columbia.edu>
|
|
|
|
* Center for Telecommunications Research
|
|
|
|
* Columbia University, New York City
|
|
|
|
*/
|
|
|
|
|
1997-11-25 07:14:34 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1997-11-25 07:14:34 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <err.h>
|
1995-10-26 16:25:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-11-25 07:14:34 +00:00
|
|
|
#include <unistd.h>
|
1995-10-26 16:25:29 +00:00
|
|
|
#include "hash.h"
|
|
|
|
|
|
|
|
/* Default location of netgroup file. */
|
|
|
|
char *netgroup = "/etc/netgroup";
|
|
|
|
|
|
|
|
/* Stored hash table version of 'forward' netgroup database. */
|
|
|
|
struct group_entry *gtable[TABLESIZE];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stored hash table of 'reverse' netgroup member database
|
|
|
|
* which we will construct.
|
|
|
|
*/
|
|
|
|
struct member_entry *mtable[TABLESIZE];
|
|
|
|
|
1997-11-25 07:14:34 +00:00
|
|
|
static void
|
2002-02-07 23:57:01 +00:00
|
|
|
usage(void)
|
1995-10-26 16:25:29 +00:00
|
|
|
{
|
1997-11-25 07:14:34 +00:00
|
|
|
fprintf (stderr,"usage: revnetgroup -u|-h [-f netgroup file]\n");
|
1995-10-26 16:25:29 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-12-13 02:40:39 +00:00
|
|
|
int
|
2002-02-07 23:57:01 +00:00
|
|
|
main(int argc, char *argv[])
|
1995-10-26 16:25:29 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char readbuf[LINSIZ];
|
|
|
|
struct group_entry *gcur;
|
|
|
|
struct member_entry *mcur;
|
|
|
|
char *host, *user, *domain;
|
1996-05-12 17:17:45 +00:00
|
|
|
int ch;
|
1995-10-26 16:25:29 +00:00
|
|
|
char *key = NULL, *data = NULL;
|
1996-05-12 17:17:45 +00:00
|
|
|
int hosts = -1, i;
|
1995-10-26 16:25:29 +00:00
|
|
|
|
|
|
|
if (argc < 2)
|
1997-11-25 07:14:34 +00:00
|
|
|
usage();
|
1995-10-26 16:25:29 +00:00
|
|
|
|
1997-03-28 15:48:21 +00:00
|
|
|
while ((ch = getopt(argc, argv, "uhf:")) != -1) {
|
1995-10-26 16:25:29 +00:00
|
|
|
switch(ch) {
|
|
|
|
case 'u':
|
1996-05-12 17:17:45 +00:00
|
|
|
if (hosts != -1) {
|
|
|
|
warnx("please use only one of -u or -h");
|
1997-11-25 07:14:34 +00:00
|
|
|
usage();
|
1996-05-12 17:17:45 +00:00
|
|
|
}
|
1995-10-26 16:25:29 +00:00
|
|
|
hosts = 0;
|
|
|
|
break;
|
|
|
|
case 'h':
|
1996-05-12 17:17:45 +00:00
|
|
|
if (hosts != -1) {
|
|
|
|
warnx("please use only one of -u or -h");
|
1997-11-25 07:14:34 +00:00
|
|
|
usage();
|
1996-05-12 17:17:45 +00:00
|
|
|
}
|
1995-10-26 16:25:29 +00:00
|
|
|
hosts = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
netgroup = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
1997-11-25 07:14:34 +00:00
|
|
|
usage();
|
1995-10-26 16:25:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-12 17:17:45 +00:00
|
|
|
if (hosts == -1)
|
1997-11-25 07:14:34 +00:00
|
|
|
usage();
|
1996-05-12 17:17:45 +00:00
|
|
|
|
1995-10-26 16:25:29 +00:00
|
|
|
if (strcmp(netgroup, "-")) {
|
|
|
|
if ((fp = fopen(netgroup, "r")) == NULL) {
|
2000-07-11 23:53:22 +00:00
|
|
|
err(1, "%s", netgroup);
|
1995-10-26 16:25:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fp = stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stuff all the netgroup names and members into a hash table. */
|
|
|
|
while (fgets(readbuf, LINSIZ, fp)) {
|
|
|
|
if (readbuf[0] == '#')
|
|
|
|
continue;
|
1996-12-13 02:40:39 +00:00
|
|
|
/* handle backslash line continuations */
|
|
|
|
while(readbuf[strlen(readbuf) - 2] == '\\') {
|
|
|
|
fgets((char *)&readbuf[strlen(readbuf) - 2],
|
|
|
|
sizeof(readbuf) - strlen(readbuf), fp);
|
|
|
|
}
|
|
|
|
data = NULL;
|
1995-10-26 16:25:29 +00:00
|
|
|
if ((data = (char *)(strpbrk(readbuf, " \t") + 1)) < (char *)2)
|
|
|
|
continue;
|
|
|
|
key = (char *)&readbuf;
|
|
|
|
*(data - 1) = '\0';
|
|
|
|
store(gtable, key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all members of each netgroup and keep track of which
|
|
|
|
* group they belong to.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < TABLESIZE; i++) {
|
|
|
|
gcur = gtable[i];
|
|
|
|
while(gcur) {
|
|
|
|
__setnetgrent(gcur->key);
|
1997-09-18 14:08:40 +00:00
|
|
|
while(__getnetgrent(&host, &user, &domain) != 0) {
|
1995-10-26 16:25:29 +00:00
|
|
|
if (hosts ? host && strcmp(host,"-") : user && strcmp(user, "-"))
|
|
|
|
mstore(mtable, hosts ? host : user, gcur->key, domain);
|
|
|
|
}
|
|
|
|
gcur = gcur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release resources used by the netgroup parser code. */
|
|
|
|
__endnetgrent();
|
|
|
|
|
|
|
|
/* Spew out the results. */
|
|
|
|
for (i = 0; i < TABLESIZE; i++) {
|
|
|
|
mcur = mtable[i];
|
|
|
|
while(mcur) {
|
|
|
|
struct grouplist *tmp;
|
|
|
|
printf ("%s.%s\t", mcur->key, mcur->domain);
|
|
|
|
tmp = mcur->groups;
|
|
|
|
while(tmp) {
|
|
|
|
printf ("%s", tmp->groupname);
|
|
|
|
tmp = tmp->next;
|
|
|
|
if (tmp)
|
|
|
|
printf(",");
|
|
|
|
}
|
|
|
|
mcur = mcur->next;
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Let the OS free all our resources. */
|
|
|
|
exit(0);
|
|
|
|
}
|