summaryrefslogtreecommitdiff
path: root/revnetgroup/revnetgroup.c
blob: ceb98c8e2c775a6a5d66f61308917c3d745b3be9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2006 Thorsten Kukuk
   Author: Thorsten Kukuk <kukuk@suse.de>

   The YP Server is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   version 2 as published by the Free Software Foundation.

   The YP Server is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with the YP Server; see the file COPYING. If
   not, write to the Free Software Foundation, Inc., 51 Franklin Street,
   Suite 500, Boston, MA 02110-1335, USA. */

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include "hash.h"

#define PARSE_FOR_USERS 0
#define PARSE_FOR_HOSTS 1

extern void rev_setnetgrent (const char *);
extern void rev_endnetgrent (void);
extern int rev_getnetgrent (char **, char **, char **);

static int insert_netgroup (hash_t ** liste, const char *key, const char *val);
static void usage (int exit_code);

hash_t *input = NULL;
static hash_t **output = NULL;
static hash_t **empty = NULL;

int
main (int argc, char **argv)
{
#define BUFSIZE 8192
  char *host, *user, *domain, *key;
  char buffer[BUFSIZE + 1];
  int hosts;
  hash_t *work
   ;
#ifdef DEBUG_NETGROUP
  FILE *debug_file = NULL;
#endif

  /* Only -u or -h are allowed, --version will exit the program */
  /* --users = -u, --hosts = -h */
  hosts = -1;

  if (argc < 2)
    usage (1);

  while (1)
    {
      int c;
      int option_index = 0;
      static struct option long_options[] =
      {
	{"version", no_argument, NULL, '\255'},
	{"hosts", no_argument, NULL, 'h'},
	{"users", no_argument, NULL, 'u'},
	{"help", no_argument, NULL, '\254'},
	{"usage", no_argument, NULL, '\254'},
	{NULL, 0, NULL, '\0'}
      };

      c = getopt_long (argc, argv, "uh", long_options, &option_index);
      if (c == EOF)
	break;
      switch (c)
	{
	case 'u':
	  hosts = PARSE_FOR_USERS;
	  break;
	case 'h':
	  hosts = PARSE_FOR_HOSTS;
	  break;
	case '\255':
          printf ("revnetgroup (%s) %s", PACKAGE, VERSION);
	  exit (0);
	case '\254':
	  usage (0);
	  break;
	default:
	  usage (1);
	  break;
	}
    }

#ifdef DEBUG_NETGROUP
  debug_file = fopen ("/etc/netgroup", "rt");
  if (debug_file == NULL)
    {
      perror ("Error: could not open /etc/netgroup:");
      exit (0);
    }
#endif

  /* Put the netgroup names in a list: */

#ifdef DEBUG_NETGROUP
  while (fgets (buffer, BUFSIZE, debug_file))
#else
  while (fgets (buffer, BUFSIZE, stdin))
#endif
    {
      char *val;
      char *cptr;

      if (buffer[0] == '#')
	continue;

      if (strlen (buffer) == 0)
	continue;

      /* Replace first '\n' with '\0' */
      if ((cptr = strchr (buffer, '\n')) != NULL)
	*cptr = '\0';

      while (buffer[strlen (buffer) - 1] == '\\')
	{
	  char *s;
#ifdef DEBUG_NETGROUP
	  s = fgets (&buffer[strlen (buffer) - 1],
		     BUFSIZE - strlen (buffer), debug_file);
#else
	  s = fgets (&buffer[strlen (buffer) - 1],
		     BUFSIZE - strlen (buffer), stdin);
#endif
	  if (s == NULL)
	    continue;
	  if ((cptr = strchr (buffer, '\n')) != NULL)
	    *cptr = '\0';
	}

      val = (char *) (strpbrk (buffer, " \t"));
      if (val == NULL)
	continue;
      key = (char *) &buffer;
      *val = '\0';
      val++;
      insert_netgroup (&input, key, val);
#ifdef DEBUG_NETGROUP
      fprintf (stderr, "KEY: [%s]\n", key);
#endif
    }

#ifdef DEBUG_NETGROUP
  fclose (debug_file);
#endif


#ifdef DEBUG_NETGROUP
  fprintf (stderr, "About to enter while loop...\n");
#endif

  /*
   * Find all members of each netgroup and keep track of which
   * group they belong to.
   */
  empty = hash_malloc ();
  output = hash_malloc ();
  work = input;
  while (work != NULL)
    {
      rev_setnetgrent (work->key);

#ifdef DEBUG_NETGROUP
      fprintf (stderr, "Processing: [%s]\n", work->key);
#endif

      while (rev_getnetgrent (&host, &user, &domain) != 0)
	{
	  static char star[] = "*";
	  char *key = NULL;
	  char *dat = NULL;

	  /* what are we processing for? */
	  if (hosts == PARSE_FOR_HOSTS)
	    dat = host;
	  else
	    dat = user;

	  /* empty fields are wildcard fields = use '-'
	   * to prevent entries
	   */
	  if (dat == NULL)
	    dat = star;

	  /* if we have an entry with data... */
	  if (dat[0] != '-')
	    {
	      /* create the dat/domain key */
	      if (domain == NULL)
		{
		  key = malloc (strlen (dat) + 3);
		  sprintf (key, "%s.*", dat);
		}
	      else
		{
		  key = malloc (strlen (dat) + strlen (domain) + 2);
		  sprintf (key, "%s.%s", dat, domain);
		}

	      /* if we have a wildcard search */
	      if (*dat == '*')
		{
		  char *val = hash_search (empty, key);

		  if (val != NULL)
		    {
		      char *buf = NULL;
		      char *buf2 = NULL;
		      int found = 0;

		      buf2 = malloc (strlen (val) + 2);
		      sprintf (buf2, "%s,", val);
		      buf = strtok (buf2, ",");

		      while ((buf != NULL) && (found == 0))
			{
			  found = (strcmp (buf, work->key) == 0);
			  buf = strtok (NULL, ",");
			}

		      free (buf2);

		      if (!found)
			{
			  buf = malloc (strlen (work->key) + strlen (val) + 2);
			  sprintf (buf, "%s,%s", val, work->key);
			  hash_delkey (empty, key);
			  hash_insert (empty, key, buf);
			  free (buf);
			}
		    }
		  else
		    {
		      hash_insert (empty, key, work->key);
		    }
		}
	      else
		{
		  /* non-wild card search */
		  char *val = hash_search (output, key);

		  if (val != NULL)
		    {
		      char *buf = NULL;
		      char *buf2 = NULL;
		      int found = 0;

		      buf2 = malloc (strlen (val) + 2);
		      sprintf (buf2, "%s,", val);
		      buf = strtok (buf2, ",");

		      while ((buf != NULL) && (found == 0))
			{
			  found = (strcmp (buf, work->key) == 0);
			  buf = strtok (NULL, ",");
			}

		      free (buf2);

		      if (!found)
			{
			  buf = malloc (strlen (work->key) + strlen (val) + 2);
			  sprintf (buf, "%s,%s", val, work->key);
			  hash_delkey (output, key);

			  hash_insert (output, key, buf);
			  free (buf);
			}
		    }
		  else
		    {
		      hash_insert (output, key, work->key);
		    }
		}

	      free (key);
	    }
	}
      work = work->next;
      /* Release resources used by the getnetgrent code. */
      rev_endnetgrent ();
    }

#ifdef DEBUG_NETGROUP
  fprintf (stderr, "About to print results...\n");
#endif

  /* Print the results. */
  work = hash_first (output);
  while (work != NULL)
    {
      printf ("%s\t%s\n", work->key, work->val);
      work = hash_next (output, work->key);
    }

  work = hash_first (empty);
  while (work != NULL)
    {
      printf ("%s\t%s\n", work->key, work->val);
      work = hash_next (empty, work->key);
    }

#if 0
  remove_netgroup (&input);
  remove_netgroup (&output);
  remove_netgroup (&empty);
#endif

  return 0;
}

static void
usage (int exit_code)
{
  fprintf (stderr, "usage: revnetgroup -u|-h\n");
  fprintf (stderr, "       revnetgroup --version\n");
  exit (exit_code);
}

static int
insert_netgroup (hash_t ** liste, const char *key, const char *val)
{
  hash_t *work;

  work = malloc (sizeof (hash_t));
  work->next = *liste;
  work->key = malloc (strlen (key) + 1);
  work->val = malloc (strlen (val) + 1);
  strcpy (work->key, key);
  strcpy (work->val, val);
  *liste = work;

  return 0;
}