| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 <time.h> |
| 34 |
#include "host.h" |
| 35 |
#include "buffer.h" |
| 36 |
#include "misc.h" |
| •37 |
#ifdef HAVE_REGEX_H |
| 38 |
#include <regex.h> |
| 39 |
#else |
| 40 |
#ifdef HAVE_RX_RXPOSIX_H |
| 41 |
#include <rx/rxposix.h> |
| 42 |
#else |
| 43 |
#ifdef HAVE_RXPOSIX_H |
| 44 |
#include <rxposix.h> |
| 45 |
#else |
| 46 |
#include "rxposix.h" |
| 47 |
#endif |
| 48 |
#endif |
| •49 |
#endif |
| 50 |
|
| 51 |
#ifdef AUTH |
| 52 |
|
| 53 |
static regex_t access_read, access_write, access_execute, access_monitor; |
| 54 |
BUFFER *host_list = NULL; |
| 55 |
BUFFER *b; |
| 56 |
|
| 57 |
static time_t host_read_time = 0; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#ifdef __STDC__ |
| 67 |
void |
| 68 |
host_read(char *filename) |
| 69 |
#else |
| 70 |
void |
| 71 |
host_read(filename) |
| 72 |
char *filename; |
| 73 |
#endif |
| 74 |
{ |
| 75 |
FILE *fp; |
| 76 |
char buf[BUFSIZ], *perms, *name, *p; |
| 77 |
char expr_read[HOST_EXPR_SIZE]; |
| 78 |
char expr_write[HOST_EXPR_SIZE]; |
| 79 |
char expr_execute[HOST_EXPR_SIZE]; |
| 80 |
char expr_monitor[HOST_EXPR_SIZE]; |
| 81 |
int error = 0; |
| 82 |
|
| 83 |
host_read_time = time(0); |
| 84 |
|
| 85 |
fp = fopen(filename, "r"); |
| 86 |
if (fp == NULL) |
| 87 |
{ |
| 88 |
report(REPORT_ERROR, "host_read: cannot open %s\n", filename); |
| 89 |
|
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
b = buffer_create(); |
| 94 |
b->status = BUFFER_KEEP; |
| 95 |
strcpy(b->buf, "+message=\"hosts\"\r\n"); |
| 96 |
b->nbytes += strlen(b->buf); |
| 97 |
host_list = b; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
strcpy(expr_read, "^\\("); |
| 104 |
strcpy(expr_write, "^\\("); |
| 105 |
strcpy(expr_execute, "^\\("); |
| 106 |
strcpy(expr_monitor, "^\\("); |
| 107 |
|
| 108 |
do |
| 109 |
{ |
| 110 |
if (fp) |
| 111 |
{ |
| 112 |
p = fgets(buf, sizeof(buf), fp); |
| 113 |
if (!p) |
| 114 |
{ |
| 115 |
fclose(fp); |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
|
| 122 |
|
| 123 |
SNPRINTF(SIZE(buf, sizeof(buf)), "%s:rwx", hostaddr); |
| 124 |
report(REPORT_NOTICE, "host_read: adding %s\n", buf); |
| 125 |
} |
| 126 |
|
| 127 |
switch (buf[0]) |
| 128 |
{ |
| 129 |
case '#': |
| 130 |
case ' ': |
| 131 |
case '\t': |
| 132 |
case '\n': |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
p = strchr(buf, '\n'); |
| 137 |
if (p) |
| 138 |
{ |
| 139 |
*p = '\0'; |
| 140 |
} |
| 141 |
|
| 142 |
name = buf; |
| 143 |
perms = strchr(buf, ':'); |
| 144 |
if (perms) |
| 145 |
{ |
| 146 |
*perms = '\0'; |
| 147 |
perms++; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
perms = HOST_DEFAULT_ACCESS; |
| 152 |
} |
| 153 |
|
| 154 |
host_insert(expr_read, expr_write, expr_execute, expr_monitor, |
| 155 |
name, perms); |
| 156 |
} |
| 157 |
while (fp); |
| 158 |
|
| 159 |
if (b->nbytes + 3 > BUFFER_SIZE) |
| 160 |
{ |
| 161 |
b->next = buffer_create(); |
| 162 |
b = b->next; |
| 163 |
b->status = BUFFER_KEEP; |
| 164 |
} |
| 165 |
SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE - b->nbytes), ".\r\n"); |
| 166 |
b->nbytes += 3; |
| 167 |
|
| 168 |
if (strlen(expr_read) == 3) |
| 169 |
{ |
| 170 |
strcat(expr_read, "\\)"); |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
expr_read[strlen(expr_read) - 1] = ')'; |
| 175 |
} |
| 176 |
strcat(expr_read, "$"); |
| 177 |
if (strlen(expr_write) == 3) |
| 178 |
{ |
| 179 |
strcat(expr_write, "\\)"); |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
expr_write[strlen(expr_write) - 1] = ')'; |
| 184 |
} |
| 185 |
strcat(expr_write, "$"); |
| 186 |
if (strlen(expr_execute) == 3) |
| 187 |
{ |
| 188 |
strcat(expr_execute, "\\)"); |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
expr_execute[strlen(expr_execute) - 1] = ')'; |
| 193 |
} |
| 194 |
strcat(expr_execute, "$"); |
| 195 |
if (strlen(expr_monitor) == 3) |
| 196 |
{ |
| 197 |
strcat(expr_monitor, "\\)"); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
expr_monitor[strlen(expr_monitor) - 1] = ')'; |
| 202 |
} |
| 203 |
strcat(expr_monitor, "$"); |
| 204 |
|
| •205 |
error = regcomp(&access_read, expr_read, REG_ICASE | REG_NOSUB); |
| 206 |
if (error) |
| 207 |
{ |
| •208 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 209 |
done(1); |
| 210 |
} |
| 211 |
|
| •212 |
error = regcomp(&access_write, expr_write, REG_ICASE | REG_NOSUB); |
| 213 |
if (error) |
| 214 |
{ |
| •215 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 216 |
done(1); |
| 217 |
} |
| 218 |
|
| •219 |
error = regcomp(&access_execute, expr_execute, REG_ICASE | REG_NOSUB); |
| 220 |
if (error) |
| 221 |
{ |
| •222 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 223 |
done(1); |
| 224 |
} |
| 225 |
|
| •226 |
error = regcomp(&access_monitor, expr_monitor, REG_ICASE | REG_NOSUB); |
| 227 |
if (error) |
| 228 |
{ |
| •229 |
report(REPORT_ERROR, "host_read: regcomp: %d\n", error); |
| 230 |
done(1); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
#ifdef __STDC__ |
| 235 |
void |
| 236 |
host_reread(char *filename) |
| 237 |
#else |
| 238 |
void |
| 239 |
host_reread(filename) |
| 240 |
char *filename; |
| 241 |
#endif |
| 242 |
{ |
| 243 |
BUFFER *b, *bb; |
| 244 |
|
| 245 |
report(REPORT_DEBUG, "re-reading hosts\n"); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
for (b = host_list; b; |
| 251 |
bb = b, b = b->next, bb->status = BUFFER_FREE, buffer_destroy(bb)) ; |
| 252 |
|
| 253 |
host_list = NULL; |
| 254 |
host_read(filename); |
| 255 |
} |
| 256 |
|
| 257 |
#ifdef __STDC__ |
| 258 |
void |
| 259 |
host_stat(char *filename) |
| 260 |
#else |
| 261 |
void |
| 262 |
host_stat(filename) |
| 263 |
char *filename; |
| 264 |
#endif |
| 265 |
{ |
| 266 |
if (modified(filename, host_read_time)) |
| 267 |
{ |
| 268 |
host_reread(filename); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#ifdef __STDC__ |
| 273 |
void |
| 274 |
host_insert(char *expr_read, char *expr_write, char *expr_execute, char *expr_monitor, |
| 275 |
char *name, char *perms) |
| 276 |
#else |
| 277 |
void |
| 278 |
host_insert(expr_read, expr_write, expr_execute, expr_monitor, name, perms) |
| 279 |
char *expr_read; |
| 280 |
char *expr_write; |
| 281 |
char *expr_execute; |
| 282 |
char *expr_monitor; |
| 283 |
char *name; |
| 284 |
char *perms; |
| 285 |
#endif |
| 286 |
{ |
| 287 |
unsigned long addr; |
| 288 |
struct in_addr addr_in; |
| 289 |
struct hostent *hp; |
| 290 |
int n; |
| 291 |
char **ap, *p; |
| 292 |
char *re_name = 0; |
| 293 |
char line[RPTP_MAX_LINE]; |
| 294 |
|
| 295 |
SNPRINTF(SIZE(line, sizeof(line)), "host=%s access=%s\r\n", name, perms); |
| 296 |
n = strlen(line); |
| 297 |
|
| 298 |
if (b->nbytes + n > BUFFER_SIZE) |
| 299 |
{ |
| 300 |
b->next = buffer_create(); |
| 301 |
b = b->next; |
| 302 |
b->status = BUFFER_KEEP; |
| 303 |
} |
| 304 |
|
| 305 |
strcat(b->buf, line); |
| 306 |
b->nbytes += n; |
| 307 |
|
| 308 |
if (strchr(name, '*')) |
| 309 |
{ |
| 310 |
re_name = host_ip_to_regex(name); |
| 311 |
} |
| 312 |
else |
| 313 |
{ |
| 314 |
addr = inet_addr(name); |
| 315 |
if (addr == 0xffffffff) |
| 316 |
{ |
| 317 |
hp = gethostbyname(name); |
| 318 |
if (hp == NULL) |
| 319 |
{ |
| 320 |
report(REPORT_NOTICE, "warning: %s unknown host\n", name); |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
for (ap = hp->h_addr_list + 1; *ap; ap++) |
| 327 |
{ |
| 328 |
memcpy((char *) &addr_in, *ap, hp->h_length); |
| 329 |
host_insert(expr_read, expr_write, expr_execute, expr_monitor, |
| 330 |
inet_ntoa(addr_in), perms); |
| 331 |
} |
| 332 |
memcpy((char *) &addr_in, (char *) hp->h_addr, sizeof(addr_in)); |
| 333 |
re_name = host_ip_to_regex(inet_ntoa(addr_in)); |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
memcpy((char *) &addr_in, (char *) &addr, sizeof(addr_in)); |
| 338 |
re_name = host_ip_to_regex(inet_ntoa(addr_in)); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
if (strcmp(hostaddr, "127.0.0.1") != 0 |
| 345 |
&& strcmp(inet_ntoa(addr_in), hostaddr) == 0) |
| 346 |
{ |
| 347 |
report(REPORT_DEBUG, "host_insert: adding localhost (127.0.0.1)\n"); |
| 348 |
host_insert(expr_read, expr_write, expr_execute, expr_monitor, "127.0.0.1", perms); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
for (p = perms; *p; p++) |
| 353 |
{ |
| 354 |
switch (*p) |
| 355 |
{ |
| 356 |
case HOST_READ: |
| 357 |
strcat(expr_read, re_name); |
| 358 |
strcat(expr_read, "\\|"); |
| 359 |
break; |
| 360 |
|
| 361 |
case HOST_WRITE: |
| 362 |
strcat(expr_write, re_name); |
| 363 |
strcat(expr_write, "\\|"); |
| 364 |
break; |
| 365 |
|
| 366 |
case HOST_EXECUTE: |
| 367 |
strcat(expr_execute, re_name); |
| 368 |
strcat(expr_execute, "\\|"); |
| 369 |
break; |
| 370 |
|
| 371 |
case HOST_MONITOR: |
| 372 |
strcat(expr_monitor, re_name); |
| 373 |
strcat(expr_monitor, "\\|"); |
| 374 |
break; |
| 375 |
|
| 376 |
default: |
| 377 |
report(REPORT_ERROR, "host_insert: '%c' unknown host access permission\n", *p); |
| 378 |
done(1); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
if (re_name) |
| 383 |
{ |
| 384 |
free(re_name); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
#ifdef __STDC__ |
| 390 |
int |
| 391 |
host_access(struct sockaddr_in sin, char access_mode) |
| 392 |
#else |
| 393 |
int |
| 394 |
host_access(sin, access_mode) |
| 395 |
struct sockaddr_in sin; |
| 396 |
char access_mode; |
| 397 |
#endif |
| 398 |
{ |
| 399 |
char *p; |
| 400 |
regex_t *re; |
| 401 |
int n; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
if (!auth_enabled) |
| 407 |
{ |
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
p = inet_ntoa(sin.sin_addr); |
| 412 |
|
| 413 |
switch (access_mode) |
| 414 |
{ |
| 415 |
case HOST_READ: |
| 416 |
re = &access_read; |
| 417 |
break; |
| 418 |
|
| 419 |
case HOST_WRITE: |
| 420 |
re = &access_write; |
| 421 |
break; |
| 422 |
|
| 423 |
case HOST_EXECUTE: |
| 424 |
re = &access_execute; |
| 425 |
break; |
| 426 |
|
| 427 |
case HOST_MONITOR: |
| 428 |
re = &access_monitor; |
| 429 |
break; |
| 430 |
|
| 431 |
default: |
| 432 |
report(REPORT_ERROR, "host_access: unknown access mode '%s'\n", access_mode); |
| 433 |
done(1); |
| 434 |
} |
| 435 |
|
| •436 |
n = regexec(re, p, 0, 0, 0); |
| 437 |
|
| 438 |
return !n; |
| 439 |
} |
| 440 |
|
| 441 |
#ifdef __STDC__ |
| 442 |
char * |
| 443 |
host_ip_to_regex(char *p) |
| 444 |
#else |
| 445 |
char * |
| 446 |
host_ip_to_regex(p) |
| 447 |
char *p; |
| 448 |
#endif |
| 449 |
{ |
| 450 |
char buf[64]; |
| 451 |
char *q; |
| 452 |
|
| 453 |
for (q = buf; *p; p++, q++) |
| 454 |
{ |
| 455 |
switch (*p) |
| 456 |
{ |
| 457 |
case '.': |
| 458 |
*q++ = '\\'; |
| 459 |
*q = '.'; |
| 460 |
break; |
| 461 |
|
| 462 |
case '*': |
| 463 |
*q++ = '.'; |
| 464 |
*q = '*'; |
| 465 |
break; |
| 466 |
|
| 467 |
default: |
| 468 |
*q = *p; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
*q = '\0'; |
| 473 |
|
| 474 |
return strdup(buf); |
| 475 |
} |
| 476 |
|
| 477 |
#endif |