File: securenets.c

package info (click to toggle)
ypserv 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: ansic: 10,161; xml: 1,872; sh: 915; makefile: 263; awk: 21
file content (359 lines) | stat: -rw-r--r-- 8,630 bytes parent folder | download | duplicates (4)
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
350
351
352
353
354
355
356
357
358
359
/* Copyright (c) 1996-2014, 2016 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 as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   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. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "access.h"
#include "log_msg.h"

#ifndef SECURENETS
#define SECURENETS YPMAPDIR "/securenets"
#endif

const char *securenetsfile = SECURENETS;

typedef struct securenet
{
  sa_family_t family;
  struct sockaddr_storage network;
  struct sockaddr_storage netmask;
  struct securenet *next;
}
securenet_t;

static securenet_t *securenets = NULL;


void
dump_securenets (void)
{
  struct securenet *sn;
  int i = 0;

  log_msg ("--- securenets start ---");
  sn = securenets;
  while (sn)
    {
      char host[INET6_ADDRSTRLEN];
      char mask[INET6_ADDRSTRLEN];

      i++;

      switch (sn->family)
	{
	case AF_INET:
	  {
	    struct sockaddr_in *network =
	      (struct sockaddr_in *)&(sn->network);
	    struct sockaddr_in *netmask =
	      (struct sockaddr_in *)&(sn->netmask);

	    log_msg ("entry %d: %s %s", i,
		     inet_ntop (AF_INET, &network->sin_addr,
				host, sizeof (host)),
		     inet_ntop (AF_INET, &netmask->sin_addr,
				mask, sizeof (mask)));
	  }
	  break;
	case AF_INET6:
	  {
	    struct sockaddr_in6 *network =
	      (struct sockaddr_in6 *)&(sn->network);
	    struct sockaddr_in6 *netmask =
	      (struct sockaddr_in6 *)&(sn->netmask);

	    log_msg ("entry %d: %s %s", i,
		     inet_ntop (AF_INET6, &network->sin6_addr,
				host, sizeof (host)),
		     inet_ntop (AF_INET6, &netmask->sin6_addr,
				mask, sizeof (mask)));
	  }
	  break;
	default:
	  log_msg ("ERROR: Unknown family: %i", sn->family);
	  break;
	}
      sn = sn->next;
    }
  log_msg ("--- securenets end ---");
}

int
load_securenets (void)
{
  char buf[2 * NI_MAXHOST + 2];
  char col_mask[NI_MAXHOST + 1], col_host[NI_MAXHOST + 1];
  struct addrinfo hints, *res0;
  FILE *in;
  securenet_t *work, *tmp;
  int error, line = 0;


  /* If securenets isn't NULL, we should reload the securents file. */
  if (securenets != NULL)
    {
      log_msg ("Reloading securenets file\n");
      while (securenets != NULL)
	{
	  work = securenets;
	  securenets = securenets->next;
	  free (work);
	}
    }
  securenets = NULL;
  work = NULL;
  tmp = NULL;

  if ((in = fopen (securenetsfile, "r")) == NULL)
    {
      log_msg ("WARNING: no %s file found!\n", securenetsfile);
      return 1;
    }

  while (!feof (in))
    {
      int nr_entries;

      memset (col_mask, 0, sizeof (col_mask));
      memset (col_host, 0, sizeof (col_host));
      memset (buf, 0, sizeof (buf));
      if (fgets (buf, sizeof (buf) -1, in) == NULL)
	continue;
      line++;

      if (buf[0] == '\0' || buf[0] == '#' || buf[0] == '\n')
	continue;

      nr_entries = sscanf (buf, "%s %s", col_mask, col_host);

      if (nr_entries == 2)
	{
	  memset(&hints, 0, sizeof(hints));
	  hints.ai_family = PF_UNSPEC;
	  hints.ai_socktype = SOCK_STREAM;
	  hints.ai_flags = AI_NUMERICHOST;
	  if ((error = getaddrinfo (col_host, NULL, &hints, &res0)))
	    {
	      log_msg ("securenets (%d) badly formated: %s",
		       line, gai_strerror (error));
	      continue;
	    }

	  if ((tmp = malloc (sizeof (securenet_t))) == NULL)
	    {
	      log_msg ("ERROR: could not allocate enough memory! [%s|%d]\n",
		       __FILE__, __LINE__);
	      exit (1);
	    }

	  tmp->next = NULL;
	  memcpy (&tmp->network, res0->ai_addr, res0->ai_addrlen);
	  tmp->family = res0->ai_addr->sa_family;
	  freeaddrinfo(res0);

	  if (strcmp (col_mask, "host") == 0)
	    {
	      if (tmp->family == AF_INET)
		strcpy (col_mask, "255.255.255.255");
	      else if (tmp->family == AF_INET6)
		strcpy (col_mask, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
	      else
		{
		  log_msg ("securenets(%d): unsupported address family: %i", line, tmp->family);
		  free (tmp);
		  continue;
		}
	    }
	  memset (&hints, 0, sizeof(hints));
	  hints.ai_family = PF_UNSPEC;
	  hints.ai_socktype = SOCK_STREAM;
	  hints.ai_flags = AI_NUMERICHOST;
	  if ((error = getaddrinfo (col_mask, NULL, &hints, &res0)))
	    {
	      log_msg ("securenets (%d) badly formated: %s",
		       line, gai_strerror (error));
	      free (tmp);
	      continue;
	    }
	  memcpy (&tmp->netmask, res0->ai_addr, res0->ai_addrlen);
	  freeaddrinfo(res0);
	}
      else if (nr_entries == 1)
	{
	  /* 127.0.0.1/8, 2001:0db8:85a3::8a2e:0370:7334/64 */
	  int netmask_len = 0;
	  char *p;

	  if ((p = strrchr (buf, '/')))
	    {
	      *p = ' ';
	      nr_entries = sscanf(buf, "%s %i", col_host, &netmask_len);
	      if (nr_entries != 2)
		goto malformed;

	      memset (&hints, 0, sizeof(hints));
	      hints.ai_family = PF_UNSPEC;
	      hints.ai_socktype = SOCK_STREAM;
	      hints.ai_flags = AI_NUMERICHOST;
	      if ((error = getaddrinfo (col_host, NULL, &hints, &res0)))
		{
		  log_msg ("securenets (%d) badly formated: %s",
			   line, gai_strerror (error));
		  continue;
		}
	    }
	  else
	    goto malformed;

	  if ((tmp = malloc (sizeof (securenet_t))) == NULL)
	    {
	      log_msg ("ERROR: could not allocate enough memory! [%s|%d]\n",
		       __FILE__, __LINE__);
	      exit (1);
	    }

	  tmp->next = NULL;
	  memcpy (&tmp->network, res0->ai_addr, res0->ai_addrlen);
	  tmp->family = res0->ai_addr->sa_family;
	  switch (tmp->family) /* prefixlen -> netmask */
	    {
	    case AF_INET:
	      {
		struct sockaddr_in sin;

		memcpy (&sin, res0->ai_addr, res0->ai_addrlen);
		sin.sin_addr.s_addr = (0xFFFFFFFFu >> (32 - netmask_len));
		memcpy (&tmp->netmask, &sin, sizeof (struct sockaddr_in));
	      }
	      break;
	    case AF_INET6:
	      {
		struct sockaddr_in6 sin6;
		int i, j;

		memcpy (&sin6, res0->ai_addr, res0->ai_addrlen);
		for (i = netmask_len, j = 0; i > 0; i -= 8, ++j)
		  sin6.sin6_addr.s6_addr[ j ] = i >= 8 ? 0xff
		    : (unsigned long)(( 0xffU << ( 8 - i ) ) & 0xffU );
		memcpy (&tmp->netmask, &sin6, sizeof (struct sockaddr_in6));
	      }
	      break;
	    default:
	      log_msg ("securenets(%d): unsupported address family: %i",
		       line, tmp->family);
	      free (tmp);
	      continue;
	      break;
	    }
	  freeaddrinfo (res0);
 	}
      else
	{
	malformed:
	  log_msg ("securenets(%d): malformed line, ignore it\n", line);
	  continue;
	}


      if (work == NULL)
	{
	  work = tmp;
	  securenets = work;
	}
      else
	{
	  work->next = tmp;
	  work = work->next;
	}
    }
  fclose (in);

  if (debug_flag)
    dump_securenets ();

  return 0;
}

int
securenet_host (struct netconfig *nconf, struct netbuf *nbuf)
{
  securenet_t *ptr;
  struct __rpc_sockinfo si;

  if (nconf == NULL || nbuf == NULL || nbuf->len <= 0)
    return 0;

  if (!__rpc_nconf2sockinfo(nconf, &si))
    return 0;

  ptr = securenets;

  if (ptr == NULL) /* this means no securenets file, grant access */
    return 1;
  else
    while (ptr != NULL)
      {
	if (si.si_af == ptr->family)
	  switch (ptr->family)
	    {
	    case AF_INET:
	      {
		struct sockaddr_in *sin1 = nbuf->buf;
		struct sockaddr_in *sin2 = (struct sockaddr_in *)&(ptr->netmask);
		struct sockaddr_in *sin3 = (struct sockaddr_in *)&(ptr->network);

		if ((sin1->sin_addr.s_addr & sin2->sin_addr.s_addr) ==
		    sin3->sin_addr.s_addr)
		  return 1;
	      }
	      break;
	    case AF_INET6:
	      {
		int i;
		struct sockaddr_in6 *sin1 = nbuf->buf;
		struct sockaddr_in6 *sin2 =
		  (struct sockaddr_in6 *)&(ptr->netmask);
		struct sockaddr_in6 *sin3 =
		  (struct sockaddr_in6 *)&(ptr->network);

		for (i = 0; i < 16; i++)
		  if ((sin1->sin6_addr.s6_addr[i] & sin2->sin6_addr.s6_addr[i])
		      != sin3->sin6_addr.s6_addr[i])
		    goto next;
		return 1;
	      }
	      break;
	    default:
	      goto next; /* Something is wrong here, should not happen. */
	    }
      next:
	ptr = ptr->next;
      }
  return 0;
}