GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ypserv/revnetgroup/revnetgroup.c Lines: 0 65 0.0 %
Date: 2017-11-07 Branches: 0 46 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: revnetgroup.c,v 1.11 2013/12/05 14:20:53 jca Exp $ */
2
/*
3
 * Copyright (c) 1995
4
 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. All advertising materials mentioning features or use of this software
15
 *    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-contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 *
33
 * reverse netgroup map generator program
34
 *
35
 * Written by Bill Paul <wpaul@ctr.columbia.edu>
36
 * Center for Telecommunications Research
37
 * Columbia University, New York City
38
 *
39
 *	$FreeBSD: revnetgroup.c,v 1.7 1997/03/28 15:48:15 imp Exp $
40
 */
41
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <unistd.h>
46
#include <util.h>
47
#include <errno.h>
48
#include <err.h>
49
#include "hash.h"
50
51
/* Default location of netgroup file. */
52
char *netgroup = "/etc/netgroup";
53
54
/* Stored hash table version of 'forward' netgroup database. */
55
struct group_entry *gtable[TABLESIZE];
56
57
/*
58
 * Stored hash table of 'reverse' netgroup member database
59
 * which we will construct.
60
 */
61
struct member_entry *mtable[TABLESIZE];
62
63
static void
64
usage(void)
65
{
66
	fprintf (stderr,"usage: revnetgroup -h | -u [-f netgroup_file]\n");
67
	exit(1);
68
}
69
70
int
71
main(int argc, char *argv[])
72
{
73
	FILE *fp;
74
	char *readbuf;
75
	struct group_entry *gcur;
76
	struct member_entry *mcur;
77
	char *host, *user, *domain;
78
	extern char *optarg;
79
	int ch;
80
	char *key = NULL, *data = NULL;
81
	int hosts = -1, i;
82
83
	if (argc < 2)
84
		usage();
85
86
	while ((ch = getopt(argc, argv, "uhf:")) != -1) {
87
		switch (ch) {
88
		case 'u':
89
			if (hosts != -1) {
90
				warnx("please use only one of -h or -u");
91
				usage();
92
			}
93
			hosts = 0;
94
			break;
95
		case 'h':
96
			if (hosts != -1) {
97
				warnx("please use only one of -u or -h");
98
				usage();
99
			}
100
			hosts = 1;
101
			break;
102
		case 'f':
103
			netgroup = optarg;
104
			break;
105
		default:
106
			usage();
107
			break;
108
		}
109
	}
110
111
	if (hosts == -1)
112
		usage();
113
114
	if (strcmp(netgroup, "-")) {
115
		if ((fp = fopen(netgroup, "r")) == NULL) {
116
			err(1, "%s", netgroup);
117
		}
118
	} else {
119
		fp = stdin;
120
	}
121
122
	/* Stuff all the netgroup names and members into a hash table. */
123
	while ((readbuf = fparseln(fp, NULL, NULL, NULL, 0)) != NULL) {
124
		data = strpbrk(readbuf, " \t");
125
		if (data == NULL) {
126
			free(readbuf);
127
			continue;
128
		}
129
		*data = '\0';
130
		++data;
131
		key = readbuf;
132
		ngstore(gtable, key, data);
133
		free(readbuf);
134
	}
135
136
	fclose(fp);
137
138
	/*
139
	 * Find all members of each netgroup and keep track of which
140
	 * group they belong to.
141
	 */
142
	for (i = 0; i < TABLESIZE; i++) {
143
		gcur = gtable[i];
144
		while (gcur) {
145
			__setnetgrent(gcur->key);
146
			while (__getnetgrent(&host, &user, &domain) != 0) {
147
				if (hosts) {
148
					if (!(host && !strcmp(host,"-"))) {
149
						mstore(mtable,
150
						    host ? host : "*",
151
						    gcur->key,
152
						    domain ? domain : "*");
153
					}
154
				} else {
155
					if (!(user && !strcmp(user,"-"))) {
156
						mstore(mtable,
157
						    user ? user : "*",
158
						    gcur->key,
159
						    domain ? domain : "*");
160
					}
161
				}
162
			}
163
			gcur = gcur->next;
164
		}
165
	}
166
167
	/* Release resources used by the netgroup parser code. */
168
	__endnetgrent();
169
170
	/* Spew out the results. */
171
	for (i = 0; i < TABLESIZE; i++) {
172
		mcur = mtable[i];
173
		while (mcur) {
174
			struct grouplist *tmp;
175
			printf ("%s.%s\t", mcur->key, mcur->domain);
176
			tmp = mcur->groups;
177
			while (tmp) {
178
				printf ("%s", tmp->groupname);
179
				tmp = tmp->next;
180
				if (tmp)
181
					printf(",");
182
			}
183
			mcur = mcur->next;
184
			printf ("\n");
185
		}
186
	}
187
188
	/* Let the OS free all our resources. */
189
	exit(0);
190
}