| 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_REGEX_H |
| 37 |
#include <regex.h> |
| 38 |
#else |
| 39 |
#ifdef HAVE_RX_RXPOSIX_H |
| 40 |
#include <rx/rxposix.h> |
| 41 |
#else |
| 42 |
#ifdef HAVE_RXPOSIX_H |
| 43 |
#include <rxposix.h> |
| 44 |
#else |
| 45 |
#include "rxposix.h" |
| 46 |
#endif |
| 47 |
#endif |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifdef AUTH |
| 51 |
|
| 52 |
static regex_t access_read, access_write, access_execute, access_monitor; |
| 53 |
BUFFER *host_list = NULL; |
| 54 |
BUFFER *b; |
| 55 |
|
| 56 |
static time_t host_read_time = 0; |
| 57 |
|
| 58 |
/* |
| 59 |
* read the rplay hosts file |
| 60 |
* |
| 61 |
* This routine will build a regular expression for each type of access. |
| 62 |
* The expressions are of the form: |
| 63 |
* \(130\.191\.224\.2\|130\.191\..*\) |
| 64 |
*/ |
| 65 |
#ifdef __STDC__ |
| 66 |
void |
| 67 |
host_read(char *filename) |
| 68 |
#else |
| 69 |
void |
| 70 |
host_read(filename) |
| 71 |
char *filename; |
| 72 |
#endif |
| 73 |
{ |
| 74 |
FILE *fp; |
| 75 |
char buf[BUFSIZ], *perms, *name, *p; |
| 76 |
char expr_read[HOST_EXPR_SIZE]; |
| 77 |
char expr_write[HOST_EXPR_SIZE]; |
| 78 |
char expr_execute[HOST_EXPR_SIZE]; |
| 79 |
char expr_monitor[HOST_EXPR_SIZE]; |
| 80 |
int error = 0; |
| 81 |
|
| 82 |
host_read_time = time(0); |
| 83 |
|
| 84 |
fp = fopen(filename, "r"); |
| 85 |
if (fp == NULL) |
| 86 |
{ |
| 87 |
report(REPORT_ERROR, "host_read: cannot open %s\n", filename); |
| 88 |
/* Don't exit anymore. Localhost will be added automatically |
| 89 |
later. */ |
| 90 |
} |
| 91 |
|
| 92 |
b = buffer_create(); |
| 93 |
b->status = BUFFER_KEEP; |
| 94 |
strcpy(b->buf, "+message=\"hosts\"\r\n"); |
| 95 |
b->nbytes += strlen(b->buf); |
| 96 |
host_list = b; |
| 97 |
|
| 98 |
//memset ((char *) &access_read, 0, sizeof (access_read)); |
| 99 |
//memset ((char *) &access_write, 0, sizeof (access_write)); |
| 100 |
//memset ((char *) &access_execute, 0, sizeof (access_execute)); |
| 101 |
|
| 102 |
strcpy(expr_read, "^\\("); |
| 103 |
strcpy(expr_write, "^\\("); |
| 104 |
strcpy(expr_execute, "^\\("); |
| 105 |
strcpy(expr_monitor, "^\\("); |
| 106 |
|
| 107 |
do |
| 108 |
{ |
| 109 |
if (fp) |
| 110 |
{ |
| 111 |
p = fgets(buf, sizeof(buf), fp); |
| 112 |
if (!p) |
| 113 |
{ |
| 114 |
fclose(fp); |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
/* rplay.hosts wasn't found and AUTH was defined in config.h. |
| 121 |
Assume that only the localhost should have access. */ |
| 122 |
SNPRINTF(SIZE(buf, sizeof(buf)), "%s:rwx", hostaddr); |
| 123 |
report(REPORT_NOTICE, "host_read: adding %s\n", buf); |
| 124 |
} |
| 125 |
|
| 126 |
switch (buf[0]) |
| 127 |
{ |
| 128 |
case '#': |
| 129 |
case ' ': |
| 130 |
case '\t': |
| 131 |
case '\n': |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
p = strchr(buf, '\n'); |
| 136 |
if (p) |
| 137 |
{ |
| 138 |
*p = '\0'; |
| 139 |
} |
| 140 |
|
| 141 |
name = buf; |
| 142 |
perms = strchr(buf, ':'); |
| 143 |
if (perms) |
| 144 |
{ |
| 145 |
*perms = '\0'; |
| 146 |
perms++; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
perms = HOST_DEFAULT_ACCESS; |
| 151 |
} |
| 152 |
|
| 153 |
host_insert(expr_read, expr_write, expr_execute, expr_monitor, |
| 154 |
name, perms); |
| 155 |
} |
| 156 |
while (fp); |
| 157 |
|
| 158 |
if (b->nbytes + 3 > BUFFER_SIZE) /* room for .\r\n */ |
| 159 |
{ |
| 160 |
b->next = buffer_create(); |
| 161 |
b = b->next; |
| 162 |
b->status = BUFFER_KEEP; |
| 163 |
} |
| 164 |
SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE - b->nbytes), ".\r\n"); |
| 165 |
b->nbytes += 3; |
| 166 |
|
| 167 |
if (strlen(expr_read) == 3) |
| 168 |
{ |
| 169 |
strcat(expr_read, "\\)"); |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
expr_read[strlen(expr_read) - 1] = ')'; |
| 174 |
} |
| 175 |
strcat(expr_read, "$"); |
| 176 |
if (strlen(expr_write) == 3) |
| 177 |
{ |
| 178 |
strcat(expr_write, "\\)"); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
expr_write[strlen(expr_write) - 1] = ')'; |
| 183 |
} |
| 184 |
strcat(expr_write, "$"); |
| 185 |
if (strlen(expr_execute) == 3) |
| 186 |
{ |
| 187 |
strcat(expr_execute, "\\)"); |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
expr_execute[strlen(expr_execute) - 1] = ')'; |
| 192 |
} |
| 193 |
strcat(expr_execute, "$"); |
| 194 |
if (strlen(expr_monitor) == 3) |
| 195 |
{ |
| 196 |
strcat(expr_monitor, "\\)"); |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
expr_monitor[strlen(expr_monitor) - 1] = ')'; |
| 201 |
} |
| 202 |
strcat(expr_monitor, "$"); |
| 203 |
|
| 204 |
error = regcomp(&access_read, expr_read, REG_ICASE | REG_NOSUB); |
| 205 |
if (error) |
| 206 |
{ |
| 207 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 208 |
done(1); |
| 209 |
} |
| 210 |
|
| 211 |
error = regcomp(&access_write, expr_write, REG_ICASE | REG_NOSUB); |
| 212 |
if (error) |
| 213 |
{ |
| 214 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 215 |
done(1); |
| 216 |
} |
| 217 |
|
| 218 |
error = regcomp(&access_execute, expr_execute, REG_ICASE | REG_NOSUB); |
| 219 |
if (error) |
| 220 |
{ |
| 221 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 222 |
done(1); |
| 223 |
} |
| 224 |
|
| 225 |
error = regcomp(&access_monitor, expr_monitor, REG_ICASE | REG_NOSUB); |
| 226 |
if (error) |
| 227 |
{ |
| 228 |
report(REPORT_ERROR, "host_read: regcomp: %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 = regexec(re, 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 */ |