Debian Codegraph @

rplayd/host.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* $Id: host.c,v 1.6 1999/06/09 06:27:44 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include "rplayd.h"
28 #include <netdb.h>
29 #include <sys/types.h>
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #include "host.h"
34 #include "buffer.h"
35 #include "misc.h"
36 #ifdef HAVE_RX_RXPOSIX_H
37 #include <rx/rxposix.h>
38 #else
39 #ifdef HAVE_RXPOSIX_H
40 #include <rxposix.h>
41 #else
42 #include "rxposix.h"
43 #endif
44 #endif
45
46 #ifdef AUTH
47
48 static regex_t access_read, access_write, access_execute, access_monitor;
49 BUFFER *host_list = NULL;
50 BUFFER *b;
51
52 static time_t host_read_time = 0;
53
54 /*
55 * read the rplay hosts file
56 *
57 * This routine will build a regular expression for each type of access.
58 * The expressions are of the form:
59 * \(130\.191\.224\.2\|130\.191\..*\)
60 */
61 #ifdef __STDC__
62 void
63 host_read(char *filename)
64 #else
65 void
66 host_read(filename)
67 char *filename;
68 #endif
69 {
70 FILE *fp;
71 char buf[BUFSIZ], *perms, *name, *p;
72 char expr_read[HOST_EXPR_SIZE];
73 char expr_write[HOST_EXPR_SIZE];
74 char expr_execute[HOST_EXPR_SIZE];
75 char expr_monitor[HOST_EXPR_SIZE];
76 int error = 0;
77
78 host_read_time = time(0);
79
80 fp = fopen(filename, "r");
81 if (fp == NULL)
82 {
83 report(REPORT_ERROR, "host_read: cannot open %s\n", filename);
84 /* Don't exit anymore. Localhost will be added automatically
85 later. */
86 }
87
88 b = buffer_create();
89 b->status = BUFFER_KEEP;
90 strcpy(b->buf, "+message=\"hosts\"\r\n");
91 b->nbytes += strlen(b->buf);
92 host_list = b;
93
94 //memset ((char *) &access_read, 0, sizeof (access_read));
95 //memset ((char *) &access_write, 0, sizeof (access_write));
96 //memset ((char *) &access_execute, 0, sizeof (access_execute));
97
98 strcpy(expr_read, "^\\(");
99 strcpy(expr_write, "^\\(");
100 strcpy(expr_execute, "^\\(");
101 strcpy(expr_monitor, "^\\(");
102
103 do
104 {
105 if (fp)
106 {
107 p = fgets(buf, sizeof(buf), fp);
108 if (!p)
109 {
110 fclose(fp);
111 break;
112 }
113 }
114 else
115 {
116 /* rplay.hosts wasn't found and AUTH was defined in config.h.
117 Assume that only the localhost should have access. */
118 SNPRINTF(SIZE(buf, sizeof(buf)), "%s:rwx", hostaddr);
119 report(REPORT_NOTICE, "host_read: adding %s\n", buf);
120 }
121
122 switch (buf[0])
123 {
124 case '#':
125 case ' ':
126 case '\t':
127 case '\n':
128 continue;
129 }
130
131 p = strchr(buf, '\n');
132 if (p)
133 {
134 *p = '\0';
135 }
136
137 name = buf;
138 perms = strchr(buf, ':');
139 if (perms)
140 {
141 *perms = '\0';
142 perms++;
143 }
144 else
145 {
146 perms = HOST_DEFAULT_ACCESS;
147 }
148
149 host_insert(expr_read, expr_write, expr_execute, expr_monitor,
150 name, perms);
151 }
152 while (fp);
153
154 if (b->nbytes + 3 > BUFFER_SIZE) /* room for .\r\n */
155 {
156 b->next = buffer_create();
157 b = b->next;
158 b->status = BUFFER_KEEP;
159 }
160 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE - b->nbytes), ".\r\n");
161 b->nbytes += 3;
162
163 if (strlen(expr_read) == 3)
164 {
165 strcat(expr_read, "\\)");
166 }
167 else
168 {
169 expr_read[strlen(expr_read) - 1] = ')';
170 }
171 strcat(expr_read, "$");
172 if (strlen(expr_write) == 3)
173 {
174 strcat(expr_write, "\\)");
175 }
176 else
177 {
178 expr_write[strlen(expr_write) - 1] = ')';
179 }
180 strcat(expr_write, "$");
181 if (strlen(expr_execute) == 3)
182 {
183 strcat(expr_execute, "\\)");
184 }
185 else
186 {
187 expr_execute[strlen(expr_execute) - 1] = ')';
188 }
189 strcat(expr_execute, "$");
190 if (strlen(expr_monitor) == 3)
191 {
192 strcat(expr_monitor, "\\)");
193 }
194 else
195 {
196 expr_monitor[strlen(expr_monitor) - 1] = ')';
197 }
198 strcat(expr_monitor, "$");
199
200 error = regncomp(&access_read, expr_read, strlen(expr_read),
201 REG_ICASE | REG_NOSUB);
202 if (error)
203 {
204 report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
205 done(1);
206 }
207
208 error = regncomp(&access_write, expr_write, strlen(expr_write),
209 REG_ICASE | REG_NOSUB);
210 if (error)
211 {
212 report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
213 done(1);
214 }
215
216 error = regncomp(&access_execute, expr_execute, strlen(expr_execute),
217 REG_ICASE | REG_NOSUB);
218 if (error)
219 {
220 report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
221 done(1);
222 }
223
224 error = regncomp(&access_monitor, expr_monitor, strlen(expr_monitor),
225 REG_ICASE | REG_NOSUB);
226 if (error)
227 {
228 report(REPORT_ERROR, "host_read: regncomp: %d\n", error);
229 done(1);
230 }
231 }
232
233 #ifdef __STDC__
234 void
235 host_reread(char *filename)
236 #else
237 void
238 host_reread(filename)
239 char *filename;
240 #endif
241 {
242 BUFFER *b, *bb;
243
244 report(REPORT_DEBUG, "re-reading hosts\n");
245
246 /*
247 * Free the old host buffer list.
248 */
249 for (b = host_list; b;
250 bb = b, b = b->next, bb->status = BUFFER_FREE, buffer_destroy(bb)) ;
251
252 host_list = NULL;
253 host_read(filename);
254 }
255
256 #ifdef __STDC__
257 void
258 host_stat(char *filename)
259 #else
260 void
261 host_stat(filename)
262 char *filename;
263 #endif
264 {
265 if (modified(filename, host_read_time))
266 {
267 host_reread(filename);
268 }
269 }
270
271 #ifdef __STDC__
272 void
273 host_insert(char *expr_read, char *expr_write, char *expr_execute, char *expr_monitor,
274 char *name, char *perms)
275 #else
276 void
277 host_insert(expr_read, expr_write, expr_execute, expr_monitor, name, perms)
278 char *expr_read;
279 char *expr_write;
280 char *expr_execute;
281 char *expr_monitor;
282 char *name;
283 char *perms;
284 #endif
285 {
286 unsigned long addr;
287 struct in_addr addr_in;
288 struct hostent *hp;
289 int n;
290 char **ap, *p;
291 char *re_name = 0;
292 char line[RPTP_MAX_LINE];
293
294 SNPRINTF(SIZE(line, sizeof(line)), "host=%s access=%s\r\n", name, perms);
295 n = strlen(line);
296
297 if (b->nbytes + n > BUFFER_SIZE)
298 {
299 b->next = buffer_create();
300 b = b->next;
301 b->status = BUFFER_KEEP;
302 }
303
304 strcat(b->buf, line);
305 b->nbytes += n;
306
307 if (strchr(name, '*'))
308 {
309 re_name = host_ip_to_regex(name);
310 }
311 else
312 {
313 addr = inet_addr(name);
314 if (addr == 0xffffffff)
315 {
316 hp = gethostbyname(name);
317 if (hp == NULL)
318 {
319 report(REPORT_NOTICE, "warning: %s unknown host\n", name);
320 return;
321 }
322 /*
323 * Handle multiple IP address.
324 */
325 for (ap = hp->h_addr_list + 1; *ap; ap++)
326 {
327 memcpy((char *) &addr_in, *ap, hp->h_length);
328 host_insert(expr_read, expr_write, expr_execute, expr_monitor,
329 inet_ntoa(addr_in), perms);
330 }
331 memcpy((char *) &addr_in, (char *) hp->h_addr, sizeof(addr_in));
332 re_name = host_ip_to_regex(inet_ntoa(addr_in));
333 }
334 else
335 {
336 memcpy((char *) &addr_in, (char *) &addr, sizeof(addr_in));
337 re_name = host_ip_to_regex(inet_ntoa(addr_in));
338 }
339
340 /*
341 * Add localhost automatically.
342 */
343 if (strcmp(hostaddr, "127.0.0.1") != 0
344 && strcmp(inet_ntoa(addr_in), hostaddr) == 0)
345 {
346 report(REPORT_DEBUG, "host_insert: adding localhost (127.0.0.1)\n");
347 host_insert(expr_read, expr_write, expr_execute, expr_monitor, "127.0.0.1", perms);
348 }
349 }
350
351 for (p = perms; *p; p++)
352 {
353 switch (*p)
354 {
355 case HOST_READ:
356 strcat(expr_read, re_name);
357 strcat(expr_read, "\\|");
358 break;
359
360 case HOST_WRITE:
361 strcat(expr_write, re_name);
362 strcat(expr_write, "\\|");
363 break;
364
365 case HOST_EXECUTE:
366 strcat(expr_execute, re_name);
367 strcat(expr_execute, "\\|");
368 break;
369
370 case HOST_MONITOR:
371 strcat(expr_monitor, re_name);
372 strcat(expr_monitor, "\\|");
373 break;
374
375 default:
376 report(REPORT_ERROR, "host_insert: '%c' unknown host access permission\n", *p);
377 done(1);
378 }
379 }
380
381 if (re_name)
382 {
383 free(re_name);
384 }
385 }
386
387 /* return 1 if allowed */
388 #ifdef __STDC__
389 int
390 host_access(struct sockaddr_in sin, char access_mode)
391 #else
392 int
393 host_access(sin, access_mode)
394 struct sockaddr_in sin;
395 char access_mode;
396 #endif
397 {
398 char *p;
399 regex_t *re;
400 int n;
401
402 /*
403 * Accept all accesses when authentication is not enabled.
404 */
405 if (!auth_enabled)
406 {
407 return 1;
408 }
409
410 p = inet_ntoa(sin.sin_addr);
411
412 switch (access_mode)
413 {
414 case HOST_READ:
415 re = &access_read;
416 break;
417
418 case HOST_WRITE:
419 re = &access_write;
420 break;
421
422 case HOST_EXECUTE:
423 re = &access_execute;
424 break;
425
426 case HOST_MONITOR:
427 re = &access_monitor;
428 break;
429
430 default:
431 report(REPORT_ERROR, "host_access: unknown access mode '%s'\n", access_mode);
432 done(1);
433 }
434
435 n = regnexec(re, p, strlen(p), 0, 0, 0);
436
437 return !n;
438 }
439
440 #ifdef __STDC__
441 char *
442 host_ip_to_regex(char *p)
443 #else
444 char *
445 host_ip_to_regex(p)
446 char *p;
447 #endif
448 {
449 char buf[64];
450 char *q;
451
452 for (q = buf; *p; p++, q++)
453 {
454 switch (*p)
455 {
456 case '.':
457 *q++ = '\\';
458 *q = '.';
459 break;
460
461 case '*':
462 *q++ = '.';
463 *q = '*';
464 break;
465
466 default:
467 *q = *p;
468 }
469 }
470
471 *q = '\0';
472
473 return strdup(buf);
474 }
475
476 #endif /* AUTH */