| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/network.h" |
| 33 |
|
| 34 |
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) |
| 35 |
#include <fcntl.h> |
| 36 |
#include <net/route.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(SOLARIS) |
| 40 |
#include <sys/sockio.h> |
| 41 |
#include <net/if.h> |
| 42 |
#elif defined(HAVE_GETIFADDRS) |
| 43 |
#include <ifaddrs.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#include "asterisk/acl.h" |
| 47 |
#include "asterisk/channel.h" |
| 48 |
#include "asterisk/utils.h" |
| 49 |
#include "asterisk/lock.h" |
| 50 |
#include "asterisk/srv.h" |
| 51 |
#include "asterisk/cli.h" |
| 52 |
|
| 53 |
#if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS)) |
| 54 |
static int get_local_address(struct ast_sockaddr *ourip) |
| 55 |
{ |
| 56 |
return -1; |
| 57 |
} |
| 58 |
#else |
| 59 |
static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score) |
| 60 |
{ |
| 61 |
const char *address; |
| 62 |
int score; |
| 63 |
|
| 64 |
address = ast_inet_ntoa(sin->sin_addr); |
| 65 |
|
| 66 |
|
| 67 |
if (address[0] == '0') { |
| 68 |
score = -25; |
| 69 |
|
| 70 |
} else if (strncmp(address, "127", 3) == 0) { |
| 71 |
score = -20; |
| 72 |
|
| 73 |
} else if (strncmp(address, "10.", 3) == 0) { |
| 74 |
score = -5; |
| 75 |
|
| 76 |
} else if (strncmp(address, "172", 3) == 0) { |
| 77 |
|
| 78 |
if (address[4] == '1' && address[5] >= '6' && address[6] == '.') { |
| 79 |
score = -5; |
| 80 |
|
| 81 |
} else if (address[4] == '2' && address[6] == '.') { |
| 82 |
score = -5; |
| 83 |
|
| 84 |
} else if (address[4] == '3' && (address[5] == '0' || address[5] == '1')) { |
| 85 |
score = -5; |
| 86 |
|
| 87 |
} else { |
| 88 |
score = 0; |
| 89 |
} |
| 90 |
|
| 91 |
} else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') { |
| 92 |
score = -10; |
| 93 |
|
| 94 |
} else if (strncmp(address, "192.168", 7) == 0) { |
| 95 |
score = -5; |
| 96 |
|
| 97 |
} else if (strncmp(address, "169.254", 7) == 0) { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
score = -10; |
| 104 |
|
| 105 |
} else if (strncmp(address, "192.0.2.", 8) == 0) { |
| 106 |
score = -15; |
| 107 |
|
| 108 |
} else { |
| 109 |
score = 0; |
| 110 |
} |
| 111 |
|
| 112 |
if (score > *best_score) { |
| 113 |
*best_score = score; |
| 114 |
memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr)); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
static int get_local_address(struct ast_sockaddr *ourip) |
| 119 |
{ |
| 120 |
int s, res = -1; |
| 121 |
#ifdef SOLARIS |
| 122 |
struct lifreq *ifr = NULL; |
| 123 |
struct lifnum ifn; |
| 124 |
struct lifconf ifc; |
| 125 |
struct sockaddr_in *sa; |
| 126 |
char *buf = NULL; |
| 127 |
int bufsz, x; |
| 128 |
#endif |
| 129 |
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__) |
| 130 |
struct ifaddrs *ifap, *ifaphead; |
| 131 |
int rtnerr; |
| 132 |
const struct sockaddr_in *sin; |
| 133 |
#endif |
| 134 |
struct in_addr best_addr; |
| 135 |
int best_score = -100; |
| 136 |
memset(&best_addr, 0, sizeof(best_addr)); |
| 137 |
|
| 138 |
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__) |
| 139 |
rtnerr = getifaddrs(&ifaphead); |
| 140 |
if (rtnerr) { |
| 141 |
perror(NULL); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
s = socket(AF_INET, SOCK_STREAM, 0); |
| 147 |
|
| 148 |
if (s > 0) { |
| 149 |
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__) |
| 150 |
for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) { |
| 151 |
|
| 152 |
if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) { |
| 153 |
sin = (const struct sockaddr_in *) ifap->ifa_addr; |
| 154 |
score_address(sin, &best_addr, &best_score); |
| 155 |
res = 0; |
| 156 |
|
| 157 |
if (best_score == 0) { |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
#endif |
| 163 |
|
| 164 |
|
| 165 |
#ifdef SOLARIS |
| 166 |
|
| 167 |
ifn.lifn_family = AF_INET; |
| 168 |
ifn.lifn_flags = 0; |
| 169 |
ifn.lifn_count = 0; |
| 170 |
if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) { |
| 171 |
close(s); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
bufsz = ifn.lifn_count * sizeof(struct lifreq); |
| 176 |
if (!(buf = ast_malloc(bufsz))) { |
| 177 |
close(s); |
| 178 |
return -1; |
| 179 |
} |
| 180 |
memset(buf, 0, bufsz); |
| 181 |
|
| 182 |
|
| 183 |
ifc.lifc_len = bufsz; |
| 184 |
ifc.lifc_buf = buf; |
| 185 |
ifc.lifc_family = AF_INET; |
| 186 |
ifc.lifc_flags = 0; |
| 187 |
if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) { |
| 188 |
close(s); |
| 189 |
ast_free(buf); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
|
| 193 |
for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) { |
| 194 |
sa = (struct sockaddr_in *)&(ifr->lifr_addr); |
| 195 |
score_address(sa, &best_addr, &best_score); |
| 196 |
res = 0; |
| 197 |
|
| 198 |
if (best_score == 0) { |
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
ast_free(buf); |
| 204 |
#endif |
| 205 |
|
| 206 |
close(s); |
| 207 |
} |
| 208 |
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) |
| 209 |
freeifaddrs(ifaphead); |
| 210 |
#endif |
| 211 |
|
| 212 |
if (res == 0 && ourip) { |
| 213 |
ast_sockaddr_setnull(ourip); |
| 214 |
ourip->ss.ss_family = AF_INET; |
| 215 |
((struct sockaddr_in *)&ourip->ss)->sin_addr = best_addr; |
| 216 |
} |
| 217 |
return res; |
| 218 |
} |
| 219 |
#endif |
| 220 |
|
| 221 |
|
| 222 |
void ast_free_ha(struct ast_ha *ha) |
| 223 |
{ |
| 224 |
struct ast_ha *hal; |
| 225 |
while (ha) { |
| 226 |
hal = ha; |
| 227 |
ha = ha->next; |
| 228 |
ast_free(hal); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
struct ast_acl_list *ast_free_acl_list(struct ast_acl_list *acl_list) |
| 234 |
{ |
| 235 |
struct ast_acl *current; |
| 236 |
|
| 237 |
if (!acl_list) { |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
AST_LIST_LOCK(acl_list); |
| 242 |
while ((current = AST_LIST_REMOVE_HEAD(acl_list, list))) { |
| 243 |
ast_free_ha(current->acl); |
| 244 |
ast_free(current); |
| 245 |
} |
| 246 |
AST_LIST_UNLOCK(acl_list); |
| 247 |
|
| 248 |
AST_LIST_HEAD_DESTROY(acl_list); |
| 249 |
ast_free(acl_list); |
| 250 |
|
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to) |
| 256 |
{ |
| 257 |
ast_sockaddr_copy(&to->addr, &from->addr); |
| 258 |
ast_sockaddr_copy(&to->netmask, &from->netmask); |
| 259 |
to->sense = from->sense; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
static struct ast_ha *ast_duplicate_ha(struct ast_ha *original) |
| 264 |
{ |
| 265 |
struct ast_ha *new_ha; |
| 266 |
|
| 267 |
if ((new_ha = ast_calloc(1, sizeof(*new_ha)))) { |
| 268 |
|
| 269 |
ast_copy_ha(original, new_ha); |
| 270 |
} |
| 271 |
|
| 272 |
return new_ha; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original) |
| 277 |
{ |
| 278 |
struct ast_ha *start = original; |
| 279 |
struct ast_ha *ret = NULL; |
| 280 |
struct ast_ha *current, *prev = NULL; |
| 281 |
|
| 282 |
while (start) { |
| 283 |
current = ast_duplicate_ha(start); |
| 284 |
if (!current) { |
| 285 |
ast_free_ha(ret); |
| 286 |
|
| 287 |
return NULL; |
| 288 |
} |
| 289 |
|
| 290 |
if (prev) { |
| 291 |
prev->next = current; |
| 292 |
} |
| 293 |
|
| 294 |
if (!ret) { |
| 295 |
ret = current; |
| 296 |
} |
| 297 |
|
| 298 |
start = start->next; |
| 299 |
prev = current; |
| 300 |
} |
| 301 |
return ret; |
| 302 |
} |
| 303 |
|
| 304 |
static int acl_new(struct ast_acl **pointer, const char *name) { |
| 305 |
struct ast_acl *acl; |
| 306 |
if (!(acl = ast_calloc(1, sizeof(*acl)))) { |
| 307 |
return 1; |
| 308 |
} |
| 309 |
|
| 310 |
*pointer = acl; |
| 311 |
ast_copy_string(acl->name, name, ACL_NAME_LENGTH); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original) |
| 316 |
{ |
| 317 |
struct ast_acl_list *clone; |
| 318 |
struct ast_acl *current_cursor; |
| 319 |
struct ast_acl *current_clone; |
| 320 |
|
| 321 |
|
| 322 |
if (!original) { |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
if (!(clone = ast_calloc(1, sizeof(*clone)))) { |
| 327 |
ast_log(LOG_ERROR, "Failed to allocate ast_acl_list struct while cloning an ACL\n"); |
| 328 |
return NULL; |
| 329 |
} |
| 330 |
AST_LIST_HEAD_INIT(clone); |
| 331 |
|
| 332 |
AST_LIST_LOCK(original); |
| 333 |
|
| 334 |
AST_LIST_TRAVERSE(original, current_cursor, list) { |
| 335 |
if ((acl_new(¤t_clone, current_cursor->name))) { |
| 336 |
ast_log(LOG_ERROR, "Failed to allocate ast_acl struct while cloning an ACL.\n"); |
| 337 |
ast_free_acl_list(clone); |
| 338 |
clone = NULL; |
| 339 |
break; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
current_clone->acl = ast_duplicate_ha_list(current_cursor->acl); |
| 344 |
|
| 345 |
current_clone->is_invalid = current_cursor->is_invalid; |
| 346 |
current_clone->is_realtime = current_cursor->is_realtime; |
| 347 |
|
| 348 |
AST_LIST_INSERT_TAIL(clone, current_clone, list); |
| 349 |
|
| 350 |
if (current_cursor->acl && !current_clone->acl) { |
| 351 |
|
| 352 |
|
| 353 |
ast_log(LOG_ERROR, "Failed to duplicate HA list while cloning ACL.\n"); |
| 354 |
ast_free_acl_list(clone); |
| 355 |
clone = NULL; |
| 356 |
break; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
AST_LIST_UNLOCK(original); |
| 361 |
|
| 362 |
return clone; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static int parse_cidr_mask(struct ast_sockaddr *addr, int is_v4, const char *mask_str) |
| 382 |
{ |
| 383 |
int mask; |
| 384 |
|
| 385 |
if (sscanf(mask_str, "%30d", &mask) != 1) { |
| 386 |
return -1; |
| 387 |
} |
| 388 |
|
| 389 |
if (is_v4) { |
| 390 |
struct sockaddr_in sin; |
| 391 |
if (mask < 0 || mask > 32) { |
| 392 |
return -1; |
| 393 |
} |
| 394 |
memset(&sin, 0, sizeof(sin)); |
| 395 |
sin.sin_family = AF_INET; |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
if (mask != 0) { |
| 401 |
sin.sin_addr.s_addr = htonl(0xFFFFFFFF << (32 - mask)); |
| 402 |
} |
| 403 |
ast_sockaddr_from_sin(addr, &sin); |
| 404 |
} else { |
| 405 |
struct sockaddr_in6 sin6; |
| 406 |
int i; |
| 407 |
if (mask < 0 || mask > 128) { |
| 408 |
return -1; |
| 409 |
} |
| 410 |
memset(&sin6, 0, sizeof(sin6)); |
| 411 |
sin6.sin6_family = AF_INET6; |
| 412 |
for (i = 0; i < 4; ++i) { |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
if (mask > 0) { |
| 418 |
V6_WORD(&sin6, i) = htonl(0xFFFFFFFF << (mask < 32 ? (32 - mask) : 0)); |
| 419 |
mask -= mask < 32 ? mask : 32; |
| 420 |
} |
| 421 |
} |
| 422 |
memcpy(&addr->ss, &sin6, sizeof(sin6)); |
| 423 |
addr->len = sizeof(sin6); |
| 424 |
} |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag) |
| 430 |
{ |
| 431 |
struct ast_acl *acl = NULL; |
| 432 |
struct ast_acl *current; |
| 433 |
struct ast_acl_list *working_list; |
| 434 |
|
| 435 |
char *tmp, *list; |
| 436 |
|
| 437 |
|
| 438 |
if (*path == NULL) { |
| 439 |
struct ast_acl_list *list; |
| 440 |
list = ast_calloc(1, sizeof(*list)); |
| 441 |
if (!list) { |
| 442 |
|
| 443 |
if (error) { |
| 444 |
*error = 1; |
| 445 |
} |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
AST_LIST_HEAD_INIT(list); |
| 450 |
*path = list; |
| 451 |
} |
| 452 |
|
| 453 |
working_list = *path; |
| 454 |
|
| 455 |
AST_LIST_LOCK(working_list); |
| 456 |
|
| 457 |
|
| 458 |
if (strncasecmp(sense, "a", 1)) { |
| 459 |
|
| 460 |
|
| 461 |
current = AST_LIST_FIRST(working_list); |
| 462 |
|
| 463 |
if (!current || !ast_strlen_zero(current->name)) { |
| 464 |
if (acl_new(&acl, "")) { |
| 465 |
if (error) { |
| 466 |
*error = 1; |
| 467 |
} |
| 468 |
AST_LIST_UNLOCK(working_list); |
| 469 |
return; |
| 470 |
} |
| 471 |
|
| 472 |
AST_LIST_INSERT_HEAD(working_list, acl, list); |
| 473 |
} else { |
| 474 |
|
| 475 |
acl = current; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
acl->acl = ast_append_ha(sense, stuff, acl->acl, error); |
| 480 |
|
| 481 |
AST_LIST_UNLOCK(working_list); |
| 482 |
return; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
list = ast_strdupa(stuff); |
| 487 |
|
| 488 |
while ((tmp = strsep(&list, ","))) { |
| 489 |
struct ast_ha *named_ha; |
| 490 |
int already_included = 0; |
| 491 |
|
| 492 |
|
| 493 |
tmp = ast_skip_blanks(tmp); |
| 494 |
|
| 495 |
|
| 496 |
AST_LIST_TRAVERSE(working_list, current, list) { |
| 497 |
if (!strcasecmp(current->name, tmp)) { |
| 498 |
|
| 499 |
ast_log(LOG_ERROR, "Named ACL '%s' occurs multiple times in ACL definition. " |
| 500 |
"Please update your ACL configuration.\n", tmp); |
| 501 |
if (error) { |
| 502 |
*error = 1; |
| 503 |
} |
| 504 |
already_included = 1; |
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
if (already_included) { |
| 510 |
continue; |
| 511 |
} |
| 512 |
|
| 513 |
if (acl_new(&acl, tmp)) { |
| 514 |
|
| 515 |
if (error) { |
| 516 |
*error = 1; |
| 517 |
} |
| 518 |
AST_LIST_UNLOCK(working_list); |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
named_ha = ast_named_acl_find(tmp, &acl->is_realtime, &acl->is_invalid); |
| 524 |
|
| 525 |
|
| 526 |
acl->acl = named_ha; |
| 527 |
|
| 528 |
|
| 529 |
if (named_acl_flag) { |
| 530 |
*named_acl_flag = 1; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
AST_LIST_INSERT_TAIL(working_list, acl, list); |
| 535 |
} |
| 536 |
|
| 537 |
AST_LIST_UNLOCK(working_list); |
| 538 |
} |
| 539 |
|
| 540 |
int ast_acl_list_is_empty(struct ast_acl_list *acl_list) |
| 541 |
{ |
| 542 |
struct ast_acl *head; |
| 543 |
|
| 544 |
if (!acl_list) { |
| 545 |
return 1; |
| 546 |
} |
| 547 |
|
| 548 |
AST_LIST_LOCK(acl_list); |
| 549 |
head = AST_LIST_FIRST(acl_list); |
| 550 |
AST_LIST_UNLOCK(acl_list); |
| 551 |
|
| 552 |
if (head) { |
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
return 1; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
static void debug_ha_sense_appended(struct ast_ha *ha) |
| 566 |
{ |
| 567 |
const char *parsed_mask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask)); |
| 568 |
|
| 569 |
ast_log(LOG_DEBUG, "%s/%s sense %u appended to ACL\n", |
| 570 |
ast_sockaddr_stringify(&ha->addr), |
| 571 |
parsed_mask, |
| 572 |
ha->sense); |
| 573 |
} |
| 574 |
|
| 575 |
static struct ast_ha *append_ha_core(const char *sense, const char *stuff, struct ast_ha *path, int *error, int port_flags) |
| 576 |
{ |
| 577 |
struct ast_ha *ha; |
| 578 |
struct ast_ha *prev = NULL; |
| 579 |
struct ast_ha *ret; |
| 580 |
char *tmp, *list = ast_strdupa(stuff ?: ""); |
| 581 |
char *address = NULL, *mask = NULL; |
| 582 |
int addr_is_v4; |
| 583 |
int allowing = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW; |
| 584 |
|
| 585 |
ret = path; |
| 586 |
while (path) { |
| 587 |
prev = path; |
| 588 |
path = path->next; |
| 589 |
} |
| 590 |
|
| 591 |
while ((tmp = strsep(&list, ","))) { |
| 592 |
uint16_t save_port; |
| 593 |
|
| 594 |
if (!(ha = ast_calloc(1, sizeof(*ha)))) { |
| 595 |
if (error) { |
| 596 |
*error = 1; |
| 597 |
} |
| 598 |
return ret; |
| 599 |
} |
| 600 |
|
| 601 |
address = strsep(&tmp, "/"); |
| 602 |
if (!address) { |
| 603 |
address = tmp; |
| 604 |
} else { |
| 605 |
mask = tmp; |
| 606 |
} |
| 607 |
|
| 608 |
if (*address == '!') { |
| 609 |
ha->sense = (allowing == AST_SENSE_DENY) ? AST_SENSE_ALLOW : AST_SENSE_DENY; |
| 610 |
address++; |
| 611 |
} else { |
| 612 |
ha->sense = allowing; |
| 613 |
} |
| 614 |
|
| 615 |
if (!ast_sockaddr_parse(&ha->addr, address, port_flags)) { |
| 616 |
ast_log(LOG_WARNING, "Invalid IP address: %s\n", address); |
| 617 |
ast_free_ha(ha); |
| 618 |
if (error) { |
| 619 |
*error = 1; |
| 620 |
} |
| 621 |
return ret; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
if ((port_flags & PARSE_PORT_MASK) == PARSE_PORT_FORBID) { |
| 626 |
ast_sockaddr_set_port(&ha->addr, 0); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
if (ast_sockaddr_ipv4_mapped(&ha->addr, &ha->addr)) { |
| 633 |
ast_log(LOG_NOTICE, "IPv4-mapped ACL network address specified. " |
| 634 |
"Converting to an IPv4 ACL network address.\n"); |
| 635 |
} |
| 636 |
|
| 637 |
addr_is_v4 = ast_sockaddr_is_ipv4(&ha->addr); |
| 638 |
|
| 639 |
if (!mask) { |
| 640 |
parse_cidr_mask(&ha->netmask, addr_is_v4, addr_is_v4 ? "32" : "128"); |
| 641 |
} else if (strchr(mask, ':') || strchr(mask, '.')) { |
| 642 |
int mask_is_v4; |
| 643 |
|
| 644 |
if (!ast_sockaddr_parse(&ha->netmask, mask, PARSE_PORT_FORBID)) { |
| 645 |
ast_log(LOG_WARNING, "Invalid netmask: %s\n", mask); |
| 646 |
ast_free_ha(ha); |
| 647 |
if (error) { |
| 648 |
*error = 1; |
| 649 |
} |
| 650 |
return ret; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
if (ast_sockaddr_ipv4_mapped(&ha->netmask, &ha->netmask)) { |
| 656 |
ast_log(LOG_NOTICE, "IPv4-mapped ACL netmask specified. " |
| 657 |
"Converting to an IPv4 ACL netmask.\n"); |
| 658 |
} |
| 659 |
mask_is_v4 = ast_sockaddr_is_ipv4(&ha->netmask); |
| 660 |
if (addr_is_v4 ^ mask_is_v4) { |
| 661 |
ast_log(LOG_WARNING, "Address and mask are not using same address scheme.\n"); |
| 662 |
ast_free_ha(ha); |
| 663 |
if (error) { |
| 664 |
*error = 1; |
| 665 |
} |
| 666 |
return ret; |
| 667 |
} |
| 668 |
} else if (parse_cidr_mask(&ha->netmask, addr_is_v4, mask)) { |
| 669 |
ast_log(LOG_WARNING, "Invalid CIDR netmask: %s\n", mask); |
| 670 |
ast_free_ha(ha); |
| 671 |
if (error) { |
| 672 |
*error = 1; |
| 673 |
} |
| 674 |
return ret; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
save_port = ast_sockaddr_port(&ha->addr); |
| 680 |
|
| 681 |
if (ast_sockaddr_apply_netmask(&ha->addr, &ha->netmask, &ha->addr)) { |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
char *failmask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask)); |
| 686 |
char *failaddr = ast_strdupa(ast_sockaddr_stringify(&ha->addr)); |
| 687 |
ast_log(LOG_WARNING, "Unable to apply netmask %s to address %s\n", failmask, failaddr); |
| 688 |
ast_free_ha(ha); |
| 689 |
if (error) { |
| 690 |
*error = 1; |
| 691 |
} |
| 692 |
return ret; |
| 693 |
} |
| 694 |
|
| 695 |
ast_sockaddr_set_port(&ha->addr, save_port); |
| 696 |
|
| 697 |
if (prev) { |
| 698 |
prev->next = ha; |
| 699 |
} else { |
| 700 |
ret = ha; |
| 701 |
} |
| 702 |
prev = ha; |
| 703 |
|
| 704 |
if (DEBUG_ATLEAST(3)) { |
| 705 |
debug_ha_sense_appended(ha); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
return ret; |
| 710 |
} |
| 711 |
|
| 712 |
struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error) |
| 713 |
{ |
| 714 |
return append_ha_core(sense, stuff, path, error, PARSE_PORT_FORBID); |
| 715 |
} |
| 716 |
|
| 717 |
struct ast_ha *ast_append_ha_with_port(const char *sense, const char *stuff, struct ast_ha *path, int *error) |
| 718 |
{ |
| 719 |
return append_ha_core(sense, stuff, path, error, 0); |
| 720 |
} |
| 721 |
|
| 722 |
void ast_ha_join(const struct ast_ha *ha, struct ast_str **buf) |
| 723 |
{ |
| 724 |
for (; ha; ha = ha->next) { |
| 725 |
const char *addr; |
| 726 |
|
| 727 |
if (ast_sockaddr_port(&ha->addr)) { |
| 728 |
addr = ast_sockaddr_stringify(&ha->addr); |
| 729 |
} else { |
| 730 |
addr = ast_sockaddr_stringify_addr(&ha->addr); |
| 731 |
} |
| 732 |
|
| 733 |
ast_str_append(buf, 0, "%s%s/", |
| 734 |
ha->sense == AST_SENSE_ALLOW ? "!" : "", |
| 735 |
addr); |
| 736 |
|
| 737 |
ast_str_append(buf, 0, "%s", ast_sockaddr_stringify_addr(&ha->netmask)); |
| 738 |
if (ha->next) { |
| 739 |
ast_str_append(buf, 0, ","); |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
void ast_ha_join_cidr(const struct ast_ha *ha, struct ast_str **buf) |
| 745 |
{ |
| 746 |
for (; ha; ha = ha->next) { |
| 747 |
const char *addr = ast_sockaddr_stringify_addr(&ha->addr); |
| 748 |
ast_str_append(buf, 0, "%s%s/%d", |
| 749 |
ha->sense == AST_SENSE_ALLOW ? "!" : "", |
| 750 |
addr, ast_sockaddr_cidr_bits(&ha->netmask)); |
| 751 |
if (ha->next) { |
| 752 |
ast_str_append(buf, 0, ","); |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
static enum ast_acl_sense ast_apply_acl_internal(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *log_prefix) |
| 758 |
{ |
| 759 |
struct ast_acl *acl; |
| 760 |
|
| 761 |
|
| 762 |
if (!acl_list) { |
| 763 |
return AST_SENSE_ALLOW; |
| 764 |
} |
| 765 |
|
| 766 |
AST_LIST_LOCK(acl_list); |
| 767 |
|
| 768 |
AST_LIST_TRAVERSE(acl_list, acl, list) { |
| 769 |
if (acl->is_invalid) { |
| 770 |
|
| 771 |
if (log_prefix) { |
| 772 |
ast_log(LOG_WARNING, "%sRejecting '%s' due to use of an invalid ACL '%s'.\n", |
| 773 |
log_prefix, ast_sockaddr_stringify_addr(addr), |
| 774 |
ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name); |
| 775 |
} |
| 776 |
AST_LIST_UNLOCK(acl_list); |
| 777 |
return AST_SENSE_DENY; |
| 778 |
} |
| 779 |
|
| 780 |
if (acl->acl) { |
| 781 |
if (ast_apply_ha(acl->acl, addr) == AST_SENSE_DENY) { |
| 782 |
if (log_prefix) { |
| 783 |
ast_log(LOG_NOTICE, "%sRejecting '%s' due to a failure to pass ACL '%s'\n", |
| 784 |
log_prefix, ast_sockaddr_stringify_addr(addr), |
| 785 |
ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name); |
| 786 |
} |
| 787 |
AST_LIST_UNLOCK(acl_list); |
| 788 |
return AST_SENSE_DENY; |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
AST_LIST_UNLOCK(acl_list); |
| 794 |
|
| 795 |
return AST_SENSE_ALLOW; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose) { |
| 800 |
return ast_apply_acl_internal(acl_list, addr, purpose ?: ""); |
| 801 |
} |
| 802 |
|
| 803 |
enum ast_acl_sense ast_apply_acl_nolog(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr) { |
| 804 |
return ast_apply_acl_internal(acl_list, addr, NULL); |
| 805 |
} |
| 806 |
|
| 807 |
enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr) |
| 808 |
{ |
| 809 |
|
| 810 |
enum ast_acl_sense res = AST_SENSE_ALLOW; |
| 811 |
const struct ast_ha *current_ha; |
| 812 |
|
| 813 |
for (current_ha = ha; current_ha; current_ha = current_ha->next) { |
| 814 |
struct ast_sockaddr result; |
| 815 |
struct ast_sockaddr mapped_addr; |
| 816 |
const struct ast_sockaddr *addr_to_use; |
| 817 |
uint16_t save_port; |
| 818 |
#if 0 |
| 819 |
char iabuf[INET_ADDRSTRLEN]; |
| 820 |
char iabuf2[INET_ADDRSTRLEN]; |
| 821 |
|
| 822 |
ast_copy_string(iabuf, ast_sockaddr_stringify(addr), sizeof(iabuf)); |
| 823 |
ast_copy_string(iabuf2, ast_sockaddr_stringify(¤t_ha->addr), sizeof(iabuf2)); |
| 824 |
ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2); |
| 825 |
#endif |
| 826 |
if (ast_sockaddr_is_ipv4(¤t_ha->addr)) { |
| 827 |
if (ast_sockaddr_is_ipv6(addr)) { |
| 828 |
if (ast_sockaddr_is_ipv4_mapped(addr)) { |
| 829 |
|
| 830 |
if (!ast_sockaddr_ipv4_mapped(addr, &mapped_addr)) { |
| 831 |
ast_log(LOG_ERROR, "%s provided to ast_sockaddr_ipv4_mapped could not be converted. That shouldn't be possible.\n", |
| 832 |
ast_sockaddr_stringify(addr)); |
| 833 |
continue; |
| 834 |
} |
| 835 |
addr_to_use = &mapped_addr; |
| 836 |
} else { |
| 837 |
|
| 838 |
continue; |
| 839 |
} |
| 840 |
} else { |
| 841 |
|
| 842 |
addr_to_use = addr; |
| 843 |
} |
| 844 |
} else { |
| 845 |
if (ast_sockaddr_is_ipv6(addr) && !ast_sockaddr_is_ipv4_mapped(addr)) { |
| 846 |
addr_to_use = addr; |
| 847 |
} else { |
| 848 |
|
| 849 |
continue; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
save_port = ast_sockaddr_port(addr_to_use); |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
if (ast_sockaddr_apply_netmask(addr_to_use, ¤t_ha->netmask, &result)) { |
| 860 |
|
| 861 |
continue; |
| 862 |
} |
| 863 |
|
| 864 |
ast_sockaddr_set_port(&result, save_port); |
| 865 |
|
| 866 |
if (!ast_sockaddr_cmp_addr(&result, ¤t_ha->addr) |
| 867 |
&& (!ast_sockaddr_port(¤t_ha->addr) |
| 868 |
|| ast_sockaddr_port(¤t_ha->addr) == ast_sockaddr_port(&result))) { |
| 869 |
res = current_ha->sense; |
| 870 |
} |
| 871 |
} |
| 872 |
return res; |
| 873 |
} |
| 874 |
|
| 875 |
static int resolve_match_or_first(struct ast_sockaddr *addr, const char *name, int flag, |
| 876 |
int family, struct ast_sockaddr *preference) |
| 877 |
{ |
| 878 |
struct ast_sockaddr *addrs; |
| 879 |
int addrs_cnt; |
| 880 |
|
| 881 |
addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family); |
| 882 |
if (addrs_cnt > 0) { |
| 883 |
struct ast_sockaddr *resolved = &addrs[0]; |
| 884 |
|
| 885 |
if (preference) { |
| 886 |
int i; |
| 887 |
|
| 888 |
for (i = 0; i < addrs_cnt; i++) { |
| 889 |
if (!ast_sockaddr_cmp_addr(preference, &addrs[i])) { |
| 890 |
resolved = &addrs[i]; |
| 891 |
break; |
| 892 |
} |
| 893 |
} |
| 894 |
} else if (addrs_cnt > 1) { |
| 895 |
ast_debug(1, "Multiple addresses. Using the first only\n"); |
| 896 |
} |
| 897 |
ast_sockaddr_copy(addr, resolved); |
| 898 |
ast_free(addrs); |
| 899 |
} else { |
| 900 |
ast_log(LOG_WARNING, "Unable to lookup '%s'\n", name); |
| 901 |
return -1; |
| 902 |
} |
| 903 |
|
| 904 |
return 0; |
| 905 |
} |
| 906 |
|
| 907 |
int ast_get_ip_or_srv_with_preference(struct ast_sockaddr *addr, const char *hostname, |
| 908 |
const char *service, struct ast_sockaddr *preference) |
| 909 |
{ |
| 910 |
char srv[256]; |
| 911 |
char host[256]; |
| 912 |
int srv_ret = 0; |
| 913 |
int tportno; |
| 914 |
|
| 915 |
if (service) { |
| 916 |
snprintf(srv, sizeof(srv), "%s.%s", service, hostname); |
| 917 |
if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, srv)) > 0) { |
| 918 |
hostname = host; |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
if (resolve_match_or_first(addr, hostname, PARSE_PORT_FORBID, addr->ss.ss_family, preference) != 0) { |
| 923 |
return -1; |
| 924 |
} |
| 925 |
|
| 926 |
if (srv_ret > 0) { |
| 927 |
ast_sockaddr_set_port(addr, tportno); |
| 928 |
} |
| 929 |
|
| 930 |
return 0; |
| 931 |
} |
| 932 |
|
| 933 |
int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service) |
| 934 |
{ |
| 935 |
return ast_get_ip_or_srv_with_preference(addr, hostname, service, NULL); |
| 936 |
} |
| 937 |
|
| 938 |
struct dscp_codepoint { |
| 939 |
char *name; |
| 940 |
unsigned int space; |
| 941 |
}; |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
static const struct dscp_codepoint dscp_pool1[] = { |
| 946 |
{ "CS0", 0x00 }, |
| 947 |
{ "CS1", 0x08 }, |
| 948 |
{ "CS2", 0x10 }, |
| 949 |
{ "CS3", 0x18 }, |
| 950 |
{ "CS4", 0x20 }, |
| 951 |
{ "CS5", 0x28 }, |
| 952 |
{ "CS6", 0x30 }, |
| 953 |
{ "CS7", 0x38 }, |
| 954 |
{ "AF11", 0x0A }, |
| 955 |
{ "AF12", 0x0C }, |
| 956 |
{ "AF13", 0x0E }, |
| 957 |
{ "AF21", 0x12 }, |
| 958 |
{ "AF22", 0x14 }, |
| 959 |
{ "AF23", 0x16 }, |
| 960 |
{ "AF31", 0x1A }, |
| 961 |
{ "AF32", 0x1C }, |
| 962 |
{ "AF33", 0x1E }, |
| 963 |
{ "AF41", 0x22 }, |
| 964 |
{ "AF42", 0x24 }, |
| 965 |
{ "AF43", 0x26 }, |
| 966 |
{ "EF", 0x2E }, |
| 967 |
}; |
| 968 |
|
| 969 |
int ast_str2cos(const char *value, unsigned int *cos) |
| 970 |
{ |
| 971 |
int fval; |
| 972 |
|
| 973 |
if (sscanf(value, "%30d", &fval) == 1) { |
| 974 |
if (fval < 8) { |
| 975 |
*cos = fval; |
| 976 |
return 0; |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
return -1; |
| 981 |
} |
| 982 |
|
| 983 |
int ast_str2tos(const char *value, unsigned int *tos) |
| 984 |
{ |
| 985 |
int fval; |
| 986 |
unsigned int x; |
| 987 |
|
| 988 |
if (sscanf(value, "%30i", &fval) == 1) { |
| 989 |
*tos = fval & 0xFF; |
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) { |
| 994 |
if (!strcasecmp(value, dscp_pool1[x].name)) { |
| 995 |
*tos = dscp_pool1[x].space << 2; |
| 996 |
return 0; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
return -1; |
| 1001 |
} |
| 1002 |
|
| 1003 |
const char *ast_tos2str(unsigned int tos) |
| 1004 |
{ |
| 1005 |
unsigned int x; |
| 1006 |
|
| 1007 |
for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) { |
| 1008 |
if (dscp_pool1[x].space == (tos >> 2)) { |
| 1009 |
return dscp_pool1[x].name; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
return "unknown"; |
| 1014 |
} |
| 1015 |
|
| 1016 |
int ast_get_ip(struct ast_sockaddr *addr, const char *hostname) |
| 1017 |
{ |
| 1018 |
return ast_get_ip_or_srv(addr, hostname, NULL); |
| 1019 |
} |
| 1020 |
|
| 1021 |
int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us) |
| 1022 |
{ |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
const char *sock_err; |
| 1029 |
int port; |
| 1030 |
int s; |
| 1031 |
|
| 1032 |
|
| 1033 |
port = ast_sockaddr_port(us); |
| 1034 |
|
| 1035 |
s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET, SOCK_DGRAM, 0); |
| 1036 |
if (s < 0) { |
| 1037 |
sock_err = ast_strdupa(strerror(errno)); |
| 1038 |
ast_log(LOG_ERROR, "Cannot create socket to %s: %s\n", |
| 1039 |
ast_sockaddr_stringify_addr(them), sock_err); |
| 1040 |
return -1; |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (ast_connect(s, them)) { |
| 1044 |
sock_err = ast_strdupa(strerror(errno)); |
| 1045 |
ast_log(LOG_WARNING, "Cannot connect to %s: %s\n", |
| 1046 |
ast_sockaddr_stringify_addr(them), sock_err); |
| 1047 |
close(s); |
| 1048 |
return -1; |
| 1049 |
} |
| 1050 |
if (ast_getsockname(s, us)) { |
| 1051 |
sock_err = ast_strdupa(strerror(errno)); |
| 1052 |
ast_log(LOG_WARNING, "Cannot get socket name for connection to %s: %s\n", |
| 1053 |
ast_sockaddr_stringify_addr(them), sock_err); |
| 1054 |
close(s); |
| 1055 |
return -1; |
| 1056 |
} |
| 1057 |
close(s); |
| 1058 |
|
| 1059 |
ast_sockaddr_set_port(us, port); |
| 1060 |
|
| 1061 |
ast_debug(3, "For destination '%s', our source address is '%s'.\n", |
| 1062 |
ast_strdupa(ast_sockaddr_stringify_addr(them)), |
| 1063 |
ast_strdupa(ast_sockaddr_stringify_addr(us))); |
| 1064 |
|
| 1065 |
return 0; |
| 1066 |
} |
| 1067 |
|
| 1068 |
int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family) |
| 1069 |
{ |
| 1070 |
char ourhost[MAXHOSTNAMELEN] = ""; |
| 1071 |
struct ast_sockaddr root; |
| 1072 |
int res, port = ast_sockaddr_port(ourip); |
| 1073 |
|
| 1074 |
|
| 1075 |
if (!ast_sockaddr_is_any(bindaddr)) { |
| 1076 |
ast_sockaddr_copy(ourip, bindaddr); |
| 1077 |
ast_debug(3, "Attached to given IP address\n"); |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (gethostname(ourhost, sizeof(ourhost) - 1)) { |
| 1082 |
ast_log(LOG_WARNING, "Unable to get hostname\n"); |
| 1083 |
} else { |
| 1084 |
if (resolve_match_or_first(ourip, ourhost, PARSE_PORT_FORBID, family, NULL) == 0) { |
| 1085 |
|
| 1086 |
ast_sockaddr_set_port(ourip, port); |
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n"); |
| 1091 |
|
| 1092 |
if (!resolve_match_or_first(&root, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID, 0, NULL) && |
| 1093 |
!ast_ouraddrfor(&root, ourip)) { |
| 1094 |
|
| 1095 |
ast_sockaddr_set_port(ourip, port); |
| 1096 |
return 0; |
| 1097 |
} |
| 1098 |
res = get_local_address(ourip); |
| 1099 |
ast_sockaddr_set_port(ourip, port); |
| 1100 |
return res; |
| 1101 |
} |
| 1102 |
|
| 1103 |
void ast_ha_output(int fd, const struct ast_ha *ha, const char *prefix) |
| 1104 |
{ |
| 1105 |
char addr[AST_SOCKADDR_BUFLEN]; |
| 1106 |
char *mask; |
| 1107 |
int index = 0; |
| 1108 |
for (; ha; ha = ha->next, ++index) { |
| 1109 |
strcpy(addr, ast_sockaddr_stringify_addr(&ha->addr)); |
| 1110 |
mask = ast_sockaddr_stringify_addr(&ha->netmask); |
| 1111 |
ast_cli(fd, "%s%3d: %s - %s/%s\n", prefix ?: "", index, ha->sense == AST_SENSE_ALLOW ? "allow" : " deny", addr, mask); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
void ast_acl_output(int fd, struct ast_acl_list *acl_list, const char *prefix) |
| 1116 |
{ |
| 1117 |
struct ast_acl *acl; |
| 1118 |
|
| 1119 |
AST_LIST_LOCK(acl_list); |
| 1120 |
AST_LIST_TRAVERSE(acl_list, acl, list) { |
| 1121 |
ast_cli(fd, "%sACL: %s%s\n---------------------------------------------\n", |
| 1122 |
prefix ?: "", ast_strlen_zero(acl->name) ? "(unnamed)" : acl->name, |
| 1123 |
acl->is_realtime ? " (realtime)" : ""); |
| 1124 |
|
| 1125 |
ast_ha_output(fd, acl->acl, prefix); |
| 1126 |
} |
| 1127 |
AST_LIST_UNLOCK(acl_list); |
| 1128 |
|
| 1129 |
} |