Codebase list rplay / 65c4a4f6-3fa5-4157-9040-d1acc7fc47ef/main rplayd / host.c
65c4a4f6-3fa5-4157-9040-d1acc7fc47ef/main

Tree @65c4a4f6-3fa5-4157-9040-d1acc7fc47ef/main (Download .tar.gz)

host.c @65c4a4f6-3fa5-4157-9040-d1acc7fc47ef/mainraw · history · blame

  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
/* $Id: host.c,v 1.6 1999/06/09 06:27:44 boyns Exp $ */

/*
 * Copyright (C) 1993-99 Mark R. Boyns <boyns@doit.org>
 *
 * This file is part of rplay.
 *
 * rplay 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.
 *
 * rplay 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 rplay; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "rplayd.h"
#include <netdb.h>
#include <sys/types.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "host.h"
#include "buffer.h"
#include "misc.h"
#ifdef HAVE_RX_RXPOSIX_H
#include <rx/rxposix.h>
#else
#ifdef HAVE_RXPOSIX_H
#include <rxposix.h>
#else
#include "rxposix.h"
#endif
#endif

#ifdef AUTH

static regex_t access_read, access_write, access_execute, access_monitor;
BUFFER *host_list = NULL;
BUFFER *b;

static time_t host_read_time = 0;

/*
 * read the rplay hosts file
 *
 * This routine will build a regular expression for each type of access.
 * The expressions are of the form:
 *      \(130\.191\.224\.2\|130\.191\..*\)
 */
#ifdef __STDC__
void
host_read(char *filename)
#else
void
host_read(filename)
    char *filename;
#endif
{
    FILE *fp;
    char buf[BUFSIZ], *perms, *name, *p;
    char expr_read[HOST_EXPR_SIZE];
    char expr_write[HOST_EXPR_SIZE];
    char expr_execute[HOST_EXPR_SIZE];
    char expr_monitor[HOST_EXPR_SIZE];
    int error = 0;

    host_read_time = time(0);

    fp = fopen(filename, "r");
    if (fp == NULL)
    {
	report(REPORT_ERROR, "host_read: cannot open %s\n", filename);
	/* Don't exit anymore.  Localhost will be added automatically
	   later. */
    }

    b = buffer_create();
    b->status = BUFFER_KEEP;
    strcpy(b->buf, "+message=\"hosts\"\r\n");
    b->nbytes += strlen(b->buf);
    host_list = b;

    //memset ((char *) &access_read, 0, sizeof (access_read));
    //memset ((char *) &access_write, 0, sizeof (access_write));
    //memset ((char *) &access_execute, 0, sizeof (access_execute));

    strcpy(expr_read, "^\\(");
    strcpy(expr_write, "^\\(");
    strcpy(expr_execute, "^\\(");
    strcpy(expr_monitor, "^\\(");

    do
    {
	if (fp)
	{
	    p = fgets(buf, sizeof(buf), fp);
	    if (!p)
	    {
		fclose(fp);
		break;
	    }
	}
	else
	{
	    /* rplay.hosts wasn't found and AUTH was defined in config.h.
	       Assume that only the localhost should have access. */
	    SNPRINTF(SIZE(buf, sizeof(buf)), "%s:rwx", hostaddr);
	    report(REPORT_NOTICE, "host_read: adding %s\n", buf);
	}

	switch (buf[0])
	{
	case '#':
	case ' ':
	case '\t':
	case '\n':
	    continue;
	}

	p = strchr(buf, '\n');
	if (p)
	{
	    *p = '\0';
	}

	name = buf;
	perms = strchr(buf, ':');
	if (perms)
	{
	    *perms = '\0';
	    perms++;
	}
	else
	{
	    perms = HOST_DEFAULT_ACCESS;
	}

	host_insert(expr_read, expr_write, expr_execute, expr_monitor,
		    name, perms);
    }
    while (fp);

    if (b->nbytes + 3 > BUFFER_SIZE)	/* room for .\r\n */
    {
	b->next = buffer_create();
	b = b->next;
	b->status = BUFFER_KEEP;
    }
    SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE - b->nbytes), ".\r\n");
    b->nbytes += 3;

    if (strlen(expr_read) == 3)
    {
	strcat(expr_read, "\\)");
    }
    else
    {
	expr_read[strlen(expr_read) - 1] = ')';
    }
    strcat(expr_read, "$");
    if (strlen(expr_write) == 3)
    {
	strcat(expr_write, "\\)");
    }
    else
    {
	expr_write[strlen(expr_write) - 1] = ')';
    }
    strcat(expr_write, "$");
    if (strlen(expr_execute) == 3)
    {
	strcat(expr_execute, "\\)");
    }
    else
    {
	expr_execute[strlen(expr_execute) - 1] = ')';
    }
    strcat(expr_execute, "$");
    if (strlen(expr_monitor) == 3)
    {
	strcat(expr_monitor, "\\)");
    }
    else
    {
	expr_monitor[strlen(expr_monitor) - 1] = ')';
    }
    strcat(expr_monitor, "$");

    error = regncomp(&access_read, expr_read, strlen(expr_read),
		     REG_ICASE | REG_NOSUB);
    if (error)
    {
	report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
	done(1);
    }

    error = regncomp(&access_write, expr_write, strlen(expr_write),
		     REG_ICASE | REG_NOSUB);
    if (error)
    {
	report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
	done(1);
    }

    error = regncomp(&access_execute, expr_execute, strlen(expr_execute),
		     REG_ICASE | REG_NOSUB);
    if (error)
    {
	report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
	done(1);
    }

    error = regncomp(&access_monitor, expr_monitor, strlen(expr_monitor),
		     REG_ICASE | REG_NOSUB);
    if (error)
    {
	report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
	done(1);
    }
}

#ifdef __STDC__
void
host_reread(char *filename)
#else
void
host_reread(filename)
    char *filename;
#endif
{
    BUFFER *b, *bb;

    report(REPORT_DEBUG, "re-reading hosts\n");

    /*
     * Free the old host buffer list.
     */
    for (b = host_list; b;
	 bb = b, b = b->next, bb->status = BUFFER_FREE, buffer_destroy(bb)) ;

    host_list = NULL;
    host_read(filename);
}

#ifdef __STDC__
void
host_stat(char *filename)
#else
void
host_stat(filename)
    char *filename;
#endif
{
    if (modified(filename, host_read_time))
    {
	host_reread(filename);
    }
}

#ifdef __STDC__
void
host_insert(char *expr_read, char *expr_write, char *expr_execute, char *expr_monitor,
	    char *name, char *perms)
#else
void
host_insert(expr_read, expr_write, expr_execute, expr_monitor, name, perms)
    char *expr_read;
    char *expr_write;
    char *expr_execute;
    char *expr_monitor;
    char *name;
    char *perms;
#endif
{
    unsigned long addr;
    struct in_addr addr_in;
    struct hostent *hp;
    int n;
    char **ap, *p;
    char *re_name = 0;
    char line[RPTP_MAX_LINE];

    SNPRINTF(SIZE(line, sizeof(line)), "host=%s access=%s\r\n", name, perms);
    n = strlen(line);

    if (b->nbytes + n > BUFFER_SIZE)
    {
	b->next = buffer_create();
	b = b->next;
	b->status = BUFFER_KEEP;
    }

    strcat(b->buf, line);
    b->nbytes += n;

    if (strchr(name, '*'))
    {
	re_name = host_ip_to_regex(name);
    }
    else
    {
	addr = inet_addr(name);
	if (addr == 0xffffffff)
	{
	    hp = gethostbyname(name);
	    if (hp == NULL)
	    {
		report(REPORT_NOTICE, "warning: %s unknown host\n", name);
		return;
	    }
	    /*
	     * Handle multiple IP address.
	     */
	    for (ap = hp->h_addr_list + 1; *ap; ap++)
	    {
		memcpy((char *) &addr_in, *ap, hp->h_length);
		host_insert(expr_read, expr_write, expr_execute, expr_monitor,
			    inet_ntoa(addr_in), perms);
	    }
	    memcpy((char *) &addr_in, (char *) hp->h_addr, sizeof(addr_in));
	    re_name = host_ip_to_regex(inet_ntoa(addr_in));
	}
	else
	{
	    memcpy((char *) &addr_in, (char *) &addr, sizeof(addr_in));
	    re_name = host_ip_to_regex(inet_ntoa(addr_in));
	}

	/*
	 * Add localhost automatically.
	 */
	if (strcmp(hostaddr, "127.0.0.1") != 0
	    && strcmp(inet_ntoa(addr_in), hostaddr) == 0)
	{
	    report(REPORT_DEBUG, "host_insert: adding localhost (127.0.0.1)\n");
	    host_insert(expr_read, expr_write, expr_execute, expr_monitor, "127.0.0.1", perms);
	}
    }

    for (p = perms; *p; p++)
    {
	switch (*p)
	{
	case HOST_READ:
	    strcat(expr_read, re_name);
	    strcat(expr_read, "\\|");
	    break;

	case HOST_WRITE:
	    strcat(expr_write, re_name);
	    strcat(expr_write, "\\|");
	    break;

	case HOST_EXECUTE:
	    strcat(expr_execute, re_name);
	    strcat(expr_execute, "\\|");
	    break;

	case HOST_MONITOR:
	    strcat(expr_monitor, re_name);
	    strcat(expr_monitor, "\\|");
	    break;

	default:
	    report(REPORT_ERROR, "host_insert: '%c' unknown host access permission\n", *p);
	    done(1);
	}
    }

    if (re_name)
    {
	free(re_name);
    }
}

/* return 1 if allowed */
#ifdef __STDC__
int
host_access(struct sockaddr_in sin, char access_mode)
#else
int
host_access(sin, access_mode)
    struct sockaddr_in sin;
    char access_mode;
#endif
{
    char *p;
    regex_t *re;
    int n;

    /*
     * Accept all accesses when authentication is not enabled.
     */
    if (!auth_enabled)
    {
	return 1;
    }

    p = inet_ntoa(sin.sin_addr);

    switch (access_mode)
    {
    case HOST_READ:
	re = &access_read;
	break;

    case HOST_WRITE:
	re = &access_write;
	break;

    case HOST_EXECUTE:
	re = &access_execute;
	break;

    case HOST_MONITOR:
	re = &access_monitor;
	break;

    default:
	report(REPORT_ERROR, "host_access: unknown access mode '%s'\n", access_mode);
	done(1);
    }

    n = regnexec(re, p, strlen(p), 0, 0, 0);

    return !n;
}

#ifdef __STDC__
char *
host_ip_to_regex(char *p)
#else
char *
host_ip_to_regex(p)
    char *p;
#endif
{
    char buf[64];
    char *q;

    for (q = buf; *p; p++, q++)
    {
	switch (*p)
	{
	case '.':
	    *q++ = '\\';
	    *q = '.';
	    break;

	case '*':
	    *q++ = '.';
	    *q = '*';
	    break;

	default:
	    *q = *p;
	}
    }

    *q = '\0';

    return strdup(buf);
}

#endif /* AUTH */