File: permission.c

package info (click to toggle)
ndtpd 3.1.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,524 kB
  • ctags: 690
  • sloc: ansic: 8,565; sh: 7,275; perl: 655; makefile: 175
file content (531 lines) | stat: -rw-r--r-- 12,741 bytes parent folder | download
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 * Copyright (c) 1997, 98, 2000, 01  
 *    Motoyuki Kasahara
 *
 * This program 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, or (at your option)
 * any later version.
 *
 * This program 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.
 */

/*
 * This program requires the following Autoconf macros:
 *   AC_C_CONST
 *   AC_HEADER_STDC
 *   AC_CHECK_HEADERS(string.h, memory.h, stdlib.h, netinet6/in6.h)
 *   AC_CHECK_FUNCS(inet_pton strchr)
 *
 *   and HAVE_STRUCT_IN6_ADDR check.
 */

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

#include <stdio.h>
#include <sys/types.h>
#include <syslog.h>

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include "dummyin6.h"

#include "permission.h"

#ifdef USE_FAKELOG
#include "fakelog.h"
#endif

#ifndef HAVE_STRCHR
#define strchr index
#define strrchr rindex
#endif /* HAVE_STRCHR */

#ifndef HAVE_MEMCPY
#define memcpy(d, s, n) bcopy((s), (d), (n))
#ifdef __STDC__
void *memchr(const void *, int, size_t);
int memcmp(const void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memset(void *, int, size_t);
#else /* not __STDC__ */
char *memchr();
int memcmp();
char *memmove();
char *memset();
#endif /* not __STDC__ */
#endif /* not HAVE_MEMCPY */

#ifndef HAVE_INET_PTON
#ifdef __STDC__
int inet_pton(int, const char *, void *);
#else
int inet_pton();
#endif
#endif

#ifdef __STDC__
static void ipv6_net_mask_length_to_address(int, struct in6_addr *);
static const char *strnopbrk(const char *, const char *);
#else
static void ipv6_net_mask_length_to_address();
static const char *strnopbrk();
#endif

/*
 * Initialize `permission'.
 */
void
initialize_permission(permission)
    Permission *permission;
{
    permission->next = NULL;
}


/*
 * Free all nodes in `permission'.
 */
void
finalize_permission(permission)
    Permission *permission;
{
    Permission *permission_p;
    Permission *next;

    permission_p = permission->next;
    while (permission_p != NULL) {
	next = permission_p->next;
	if (permission_p->host_name != NULL)
	    free(permission_p->host_name);
	if (permission_p->scope_name != NULL)
	    free(permission_p->scope_name);
	free(permission_p);
	permission_p = next;
    }

    permission->next = NULL;
}


/*
 * Add `pattern' to the list `permission'.
 *
 * If `permission' is a valid IPv4/IPv6 prefix or address, it is recognized
 * as it is.  Otherwise `pattern' is recognized as a host name.
 *
 * If succeeded, 0 is returned.  Otherwise, -1 is returned.
 */
int
add_permission(permission, pattern)
    Permission *permission;
    const char *pattern;
{
    Permission *permission_p = NULL;
    char *copied_pattern = NULL;
    struct in_addr ipv4_address;
    int net_mask_length = 0;
    unsigned char mask_buffer[16];
    char *percent;
    char *slash;
    int i;

    /*
     * Allocate memeories for new node and a host name.
     */
    permission_p = (Permission *)malloc(sizeof(Permission));
    if (permission_p == NULL) {
	syslog(LOG_ERR, "memory exhausted");
	goto failed;
    }
    permission_p->not = 0;
    permission_p->host_name = NULL;
    permission_p->scope_name = NULL;
    memcpy(&permission_p->address, &in6addr_any, sizeof(struct in6_addr));
    ipv6_net_mask_length_to_address(128, &permission_p->net_mask);

    /*
     * Allocate memories for duplication of `pattern'.
     */
    copied_pattern = (char *)malloc(strlen(pattern) + 1);
    if (copied_pattern == NULL) {
	syslog(LOG_ERR, "memory exhausted");
	goto failed;
    }
    if (*pattern == '!')
	strcpy(copied_pattern, pattern + 1);
    else
	strcpy(copied_pattern, pattern);

    /*
     * Set <NOT> flag.
     */
    if (*pattern == '!')
	permission_p->not = 1;

    /*
     * Get net mask length if exists.
     */
    slash = strchr(copied_pattern, '/');
    if (slash != NULL) {
	char *p = slash + 1;

	while ('0' <= *p && *p <= '9')
	    p++;

	if (*p == '\0') {
	    net_mask_length = atoi(slash + 1);
	    *slash = '\0';
	} else {
	    syslog(LOG_ERR, "invalid netmask: %s", slash);
	    goto failed;
	}
    }

    /*
     * Get scope name if exists.
     */
    percent = strchr(copied_pattern, '%');
    if (percent != NULL)
	*percent = '\0';

    /*
     * Parse the given pattern.
     */
    if (strchr(copied_pattern, ':') != NULL) {
	/*
	 * The pattern seems to be IPv6 address.
	 */
	if (inet_pton(AF_INET6, copied_pattern, &permission_p->address) != 1) {
	    syslog(LOG_ERR, "invalid IPv6 adderss: %s", copied_pattern);
	    goto failed;
	}

	if (slash == NULL)
	    net_mask_length = 128;
	if (net_mask_length < 0 || 128 < net_mask_length) {
	    syslog(LOG_ERR, "invalid netmask: %d", net_mask_length);
	    goto failed;
	}
	ipv6_net_mask_length_to_address(net_mask_length,
	    &permission_p->net_mask);

	if (percent != NULL) {
	    memmove(copied_pattern, percent + 1, strlen(percent + 1) + 1);
	    permission_p->scope_name = copied_pattern;
	}

    } else if (strnopbrk(copied_pattern, "0123456789.") == NULL) {
	/*
	 * The pattern seems to be IPv4 address.
	 */
	if (inet_pton(AF_INET, copied_pattern, &ipv4_address) != 1) {
	    syslog(LOG_ERR, "invalid IPv4 adderss: %s", copied_pattern);
	    goto failed;
	}
	memset(permission_p->address.s6_addr, 0, 10);
	*((unsigned char *)permission_p->address.s6_addr + 10) = 0xff;
	*((unsigned char *)permission_p->address.s6_addr + 11) = 0xff;
	memcpy((unsigned char *)permission_p->address.s6_addr + 12,
	    &ipv4_address.s_addr, 4);

	if (slash == NULL)
	    net_mask_length = 32;
	if (net_mask_length < 0 || 32 < net_mask_length) {
	    syslog(LOG_ERR, "invalid netmask: %d", net_mask_length);
	    goto failed;
	}
	ipv6_net_mask_length_to_address(net_mask_length + 96,
	    &permission_p->net_mask);

	if (percent != NULL) {
	    syslog(LOG_ERR, "IPv4 adderss with scope is not permitted: %s",
		pattern);
	    goto failed;
	}

    } else {
	/*
	 * The pattern seems to be host name.
	 */
	if (slash != NULL)
	    *slash = '/';
	if (percent != NULL)
	    *percent = '/';
	permission_p->host_name = copied_pattern;
    }

    /*
     * Check address/netmask pair, if the given pattern has address.
     */
    if (permission_p->host_name == NULL) {
	for (i = 0; i < 16; i++) {
	    mask_buffer[i]
		= *((unsigned char *)permission_p->address.s6_addr + i)
		& *((unsigned char *)permission_p->net_mask.s6_addr + i);
	}
	if (memcmp(mask_buffer, &permission_p->address.s6_addr, 16) != 0) {
	    syslog(LOG_ERR, "address/netmask mismatch: %s", pattern);
	    goto failed;
	}
    }

    /*
     * Insert the new node into the permission list.
     */
    permission_p->next = permission->next;
    permission->next = permission_p;

    if (permission_p->host_name == NULL && permission_p->scope_name == NULL)
	free(copied_pattern);

    return 0;

    /*
     * If an error occurs...
     */
  failed:
    if (permission_p != NULL)
	free(permission_p);
    if (copied_pattern != NULL)
	free(copied_pattern);
    return -1;
}


/*
 * Convert IPv6 netmask length to IPv6 address.
 * For example, 60 is converted to FFFF:FFFF:FFFF:FFF0::.
 */
static void
ipv6_net_mask_length_to_address(length, address)
    int length;
    struct in6_addr *address;
{
    static const unsigned char mod8_masks[]
	= {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe};
    unsigned char *address_p = (unsigned char *)address->s6_addr;
    int rest_length = length;

    memset(address->s6_addr, 0, 16);

    while (8 <= rest_length) {
	*address_p++ = 0xff;
	rest_length -= 8;
    }
    if (0 < rest_length)
	*address_p = mod8_masks[rest_length];
}


/*
 * strnopbrk() is similar to strpbrk(), but it locates the string `s'
 * the first occurrence of any character *NOT* listed in `charset'.
 */
static const char *
strnopbrk(string, charset)
    const char *string;
    const char *charset;
{
    unsigned char table[256];	/* assumes CHAR_BITS is 8 */
    const unsigned char *string_p = (const unsigned char *)string;
    const unsigned char *charset_p = (const unsigned char *)charset;

    memset(table, 0, sizeof(table));

    while (*charset_p != '\0')
	table[*charset_p++]  = 1;

    while (*string_p != '\0') {
	if (!table[*string_p])
	    return (const char *)string_p;
	string_p++;
    }

    return NULL;
}


/*
 * Examine access permission to `host_name' and `address'.
 * If access is permitted, 1 is returned.  Otherwise 0 is returned.
 */
int
test_permission(permission, host_name, address_string, function)
    const Permission *permission;
    const char *host_name;
    const char *address_string;
#ifdef __STDC__
    int (*function)(const char *, const char *);
#else
    int (*function)();
#endif
{
    const Permission *permission_p;
    int result = 0;
    struct in6_addr ipv6_address;
    struct in_addr ipv4_address;
    char address_buffer[INET6_ADDRSTRLEN];
    size_t address_length;
    const char *scope_name;
    unsigned char mask_buffer[16];
    int i;

    /*
     * Eliminate IPv6 scope name from `address_string'.
     */
    scope_name = strchr(address_string, '%');
    if (scope_name == NULL) {
	address_length = strlen(address_string);
	scope_name = "";
    } else {
	address_length = scope_name - address_string;
	scope_name++;
    }
    if (address_length + 1 > INET6_ADDRSTRLEN)
	return 0;

    memcpy(address_buffer, address_string, address_length);
    *(address_buffer + address_length) = '\0';

    /*
     * Convert `address_string' to binary format address.
     */
    if (inet_pton(AF_INET6, address_buffer, &ipv6_address) == 1) {
	/* nothing to be done */;
    } else if (inet_pton(AF_INET, address_buffer, &ipv4_address) == 1) {
	memset(ipv6_address.s6_addr, 0, 16);
	*((unsigned char *)ipv6_address.s6_addr + 10) = 0xff;
	*((unsigned char *)ipv6_address.s6_addr + 11) = 0xff;
	memcpy((unsigned char *)ipv6_address.s6_addr + 12,
	    &ipv4_address.s_addr, 4);
    } else {
	return 0;
    }

    /*
     * Test.
     */
    for (permission_p = permission->next; permission_p != NULL;
	 permission_p = permission_p->next) {
	if (permission_p->host_name != NULL) {
	    if (function(permission_p->host_name, host_name))
		result = permission_p->not ? 0 : 1;
	} else {
	    for (i = 0; i < 16; i++) {
		mask_buffer[i] = *((unsigned char *)ipv6_address.s6_addr + i)
		    & *((unsigned char *)permission_p->net_mask.s6_addr + i);
	    }
	    if (memcmp(mask_buffer, permission_p->address.s6_addr, 16) == 0) {
		if (permission_p->scope_name == NULL
		    || function(permission_p->scope_name, scope_name)) {
		    result = permission_p->not ? 0 : 1;
		}
	    }
	}
    }

    return result;
}


/*
 * Count the entires of the permission list, and return it.
 */
int
count_permission(permission)
    const Permission *permission;
{
    const Permission *permission_p;
    int count = 0;

    for (permission_p = permission->next; permission_p != NULL;
	 permission_p = permission_p->next)
	count++;

    return count;
}


/*
 * Test program.
 */
#ifdef TEST
int streq(s1, s2)
    const char *s1;
    const char *s2;
{
    return (strcmp(s1, s2) == 0);
}

int
main(argc, argv)
    int argc;
    char *argv[];
{
    Permission permission;
    FILE *file;
    char buffer[512];
    char *newline;

    if (argc != 2) {
	fprintf(stderr, "Usage: %s access-list-file\n", argv[0]);
	return 1;
    }

    initialize_permission(&permission);
    set_fakelog_mode(FAKELOG_TO_STDERR);

    file = fopen(argv[1], "r");
    if (file == NULL) {
	fprintf(stderr, "%s: failed to open the file: %s\n", argv[0], argv[1]);
	return 1;
    }

    while (fgets(buffer, sizeof(buffer), file) != NULL) {
	newline = strchr(buffer, '\n');
	if (newline != NULL)
	    *newline = '\0';
	if (add_permission(&permission, buffer) < 0) {
	    fprintf(stderr, "%s: failed to add permission to the list: %s\n",
		argv[0], buffer);
	    fclose(file);
	    return 1;
	}
    }
    fclose(file);

    while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
	newline = strchr(buffer, '\n');
	if (newline != NULL)
	    *newline = '\0';
	if (test_permission(&permission, "?", buffer, streq))
	    printf("allowed\n");
	else
	    printf("denied\n");
    }

    return 0;
}

#endif /* TEST */