acl.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2012, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Various sorts of access control
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 428426 $")
00033 
00034 #include "asterisk/network.h"
00035 
00036 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
00037 #include <fcntl.h>
00038 #include <net/route.h>
00039 #endif
00040 
00041 #if defined(SOLARIS)
00042 #include <sys/sockio.h>
00043 #include <net/if.h>
00044 #elif defined(HAVE_GETIFADDRS)
00045 #include <ifaddrs.h>
00046 #endif
00047 
00048 #include "asterisk/acl.h"
00049 #include "asterisk/channel.h"
00050 #include "asterisk/utils.h"
00051 #include "asterisk/lock.h"
00052 #include "asterisk/srv.h"
00053 
00054 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
00055 static int get_local_address(struct ast_sockaddr *ourip)
00056 {
00057    return -1;
00058 }
00059 #else
00060 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
00061 {
00062    const char *address;
00063    int score;
00064 
00065    address = ast_inet_ntoa(sin->sin_addr);
00066 
00067    /* RFC 1700 alias for the local network */
00068    if (address[0] == '0') {
00069       score = -25;
00070    /* RFC 1700 localnet */
00071    } else if (strncmp(address, "127", 3) == 0) {
00072       score = -20;
00073    /* RFC 1918 non-public address space */
00074    } else if (strncmp(address, "10.", 3) == 0) {
00075       score = -5;
00076    /* RFC 1918 non-public address space */
00077    } else if (strncmp(address, "172", 3) == 0) {
00078       /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
00079       if (address[4] == '1' && address[5] >= '6' && address[6] == '.') {
00080          score = -5;
00081       /* 172.20.0.0 - 172.29.255.255, but not 172.200.0.0 - 172.255.255.255 nor 172.2.0.0 - 172.2.255.255 */
00082       } else if (address[4] == '2' && address[6] == '.') {
00083          score = -5;
00084       /* 172.30.0.0 - 172.31.255.255, but not 172.3.0.0 - 172.3.255.255 */
00085       } else if (address[4] == '3' && (address[5] == '0' || address[5] == '1')) {
00086          score = -5;
00087       /* All other 172 addresses are public */
00088       } else {
00089          score = 0;
00090       }
00091    /* RFC 2544 Benchmark test range (198.18.0.0 - 198.19.255.255, but not 198.180.0.0 - 198.199.255.255) */
00092    } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') {
00093       score = -10;
00094    /* RFC 1918 non-public address space */
00095    } else if (strncmp(address, "192.168", 7) == 0) {
00096       score = -5;
00097    /* RFC 3330 Zeroconf network */
00098    } else if (strncmp(address, "169.254", 7) == 0) {
00099       /*!\note Better score than a test network, but not quite as good as RFC 1918
00100        * address space.  The reason is that some Linux distributions automatically
00101        * configure a Zeroconf address before trying DHCP, so we want to prefer a
00102        * DHCP lease to a Zeroconf address.
00103        */
00104       score = -10;
00105    /* RFC 3330 Test network */
00106    } else if (strncmp(address, "192.0.2.", 8) == 0) {
00107       score = -15;
00108    /* Every other address should be publically routable */
00109    } else {
00110       score = 0;
00111    }
00112 
00113    if (score > *best_score) {
00114       *best_score = score;
00115       memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
00116    }
00117 }
00118 
00119 static int get_local_address(struct ast_sockaddr *ourip)
00120 {
00121    int s, res = -1;
00122 #ifdef SOLARIS
00123    struct lifreq *ifr = NULL;
00124    struct lifnum ifn;
00125    struct lifconf ifc;
00126    struct sockaddr_in *sa;
00127    char *buf = NULL;
00128    int bufsz, x;
00129 #endif /* SOLARIS */
00130 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
00131    struct ifaddrs *ifap, *ifaphead;
00132    int rtnerr;
00133    const struct sockaddr_in *sin;
00134 #endif /* BSD_OR_LINUX */
00135    struct in_addr best_addr;
00136    int best_score = -100;
00137    memset(&best_addr, 0, sizeof(best_addr));
00138 
00139 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
00140    rtnerr = getifaddrs(&ifaphead);
00141    if (rtnerr) {
00142       perror(NULL);
00143       return -1;
00144    }
00145 #endif /* BSD_OR_LINUX */
00146 
00147    s = socket(AF_INET, SOCK_STREAM, 0);
00148 
00149    if (s > 0) {
00150 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) || defined(__GLIBC__)
00151       for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
00152 
00153          if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
00154             sin = (const struct sockaddr_in *) ifap->ifa_addr;
00155             score_address(sin, &best_addr, &best_score);
00156             res = 0;
00157 
00158             if (best_score == 0) {
00159                break;
00160             }
00161          }
00162       }
00163 #endif /* BSD_OR_LINUX */
00164 
00165       /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
00166 #ifdef SOLARIS
00167       /* Get a count of interfaces on the machine */
00168       ifn.lifn_family = AF_INET;
00169       ifn.lifn_flags = 0;
00170       ifn.lifn_count = 0;
00171       if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
00172          close(s);
00173          return -1;
00174       }
00175 
00176       bufsz = ifn.lifn_count * sizeof(struct lifreq);
00177       if (!(buf = ast_malloc(bufsz))) {
00178          close(s);
00179          return -1;
00180       }
00181       memset(buf, 0, bufsz);
00182 
00183       /* Get a list of interfaces on the machine */
00184       ifc.lifc_len = bufsz;
00185       ifc.lifc_buf = buf;
00186       ifc.lifc_family = AF_INET;
00187       ifc.lifc_flags = 0;
00188       if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
00189          close(s);
00190          ast_free(buf);
00191          return -1;
00192       }
00193 
00194       for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
00195          sa = (struct sockaddr_in *)&(ifr->lifr_addr);
00196          score_address(sa, &best_addr, &best_score);
00197          res = 0;
00198 
00199          if (best_score == 0) {
00200             break;
00201          }
00202       }
00203 
00204       ast_free(buf);
00205 #endif /* SOLARIS */
00206 
00207       close(s);
00208    }
00209 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
00210    freeifaddrs(ifaphead);
00211 #endif /* BSD_OR_LINUX */
00212 
00213    if (res == 0 && ourip) {
00214       ast_sockaddr_setnull(ourip);
00215       ourip->ss.ss_family = AF_INET;
00216       ((struct sockaddr_in *)&ourip->ss)->sin_addr = best_addr;
00217    }
00218    return res;
00219 }
00220 #endif /* HAVE_GETIFADDRS */
00221 
00222 /* Free HA structure */
00223 void ast_free_ha(struct ast_ha *ha)
00224 {
00225    struct ast_ha *hal;
00226    while (ha) {
00227       hal = ha;
00228       ha = ha->next;
00229       ast_free(hal);
00230    }
00231 }
00232 
00233 /* Free ACL list structure */
00234 struct ast_acl_list *ast_free_acl_list(struct ast_acl_list *acl_list)
00235 {
00236    struct ast_acl *current;
00237 
00238    if (!acl_list) {
00239       return NULL;
00240    }
00241 
00242    AST_LIST_LOCK(acl_list);
00243    while ((current = AST_LIST_REMOVE_HEAD(acl_list, list))) {
00244       ast_free_ha(current->acl);
00245       ast_free(current);
00246    }
00247    AST_LIST_UNLOCK(acl_list);
00248 
00249    AST_LIST_HEAD_DESTROY(acl_list);
00250    ast_free(acl_list);
00251 
00252    return NULL;
00253 }
00254 
00255 /* Copy HA structure */
00256 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
00257 {
00258    ast_sockaddr_copy(&to->addr, &from->addr);
00259    ast_sockaddr_copy(&to->netmask, &from->netmask);
00260    to->sense = from->sense;
00261 }
00262 
00263 /* Create duplicate of ha structure */
00264 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
00265 {
00266    struct ast_ha *new_ha;
00267 
00268    if ((new_ha = ast_calloc(1, sizeof(*new_ha)))) {
00269       /* Copy from original to new object */
00270       ast_copy_ha(original, new_ha);
00271    }
00272 
00273    return new_ha;
00274 }
00275 
00276 /* Create duplicate HA link list */
00277 /*  Used in chan_sip2 templates */
00278 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
00279 {
00280    struct ast_ha *start = original;
00281    struct ast_ha *ret = NULL;
00282    struct ast_ha *current, *prev = NULL;
00283 
00284    while (start) {
00285       current = ast_duplicate_ha(start);  /* Create copy of this object */
00286       if (prev) {
00287          prev->next = current;           /* Link previous to this object */
00288       }
00289 
00290       if (!ret) {
00291          ret = current;                  /* Save starting point */
00292       }
00293 
00294       start = start->next;                /* Go to next object */
00295       prev = current;                     /* Save pointer to this object */
00296    }
00297    return ret;                             /* Return start of list */
00298 }
00299 
00300 static int acl_new(struct ast_acl **pointer, const char *name) {
00301    struct ast_acl *acl;
00302    if (!(acl = ast_calloc(1, sizeof(*acl)))) {
00303       return 1;
00304    }
00305 
00306    *pointer = acl;
00307    ast_copy_string(acl->name, name, ACL_NAME_LENGTH);
00308    return 0;
00309 }
00310 
00311 struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original)
00312 {
00313    struct ast_acl_list *clone;
00314    struct ast_acl *current_cursor;
00315    struct ast_acl *current_clone;
00316 
00317    /* Early return if we receive a duplication request for a NULL original. */
00318    if (!original) {
00319       return NULL;
00320    }
00321 
00322    if (!(clone = ast_calloc(1, sizeof(*clone)))) {
00323       ast_log(LOG_WARNING, "Failed to allocate ast_acl_list struct while cloning an ACL\n");
00324       return NULL;
00325    }
00326    AST_LIST_HEAD_INIT(clone);
00327 
00328    AST_LIST_LOCK(original);
00329 
00330    AST_LIST_TRAVERSE(original, current_cursor, list) {
00331       if ((acl_new(&current_clone, current_cursor->name))) {
00332          ast_log(LOG_WARNING, "Failed to allocate ast_acl struct while cloning an ACL.");
00333          continue;
00334       }
00335 
00336       /* Copy data from original ACL to clone ACL */
00337       current_clone->acl = ast_duplicate_ha_list(current_cursor->acl);
00338 
00339       current_clone->is_invalid = current_cursor->is_invalid;
00340       current_clone->is_realtime = current_cursor->is_realtime;
00341 
00342       AST_LIST_INSERT_TAIL(clone, current_clone, list);
00343    }
00344 
00345    AST_LIST_UNLOCK(original);
00346 
00347    return clone;
00348 }
00349 
00350 /*!
00351  * \brief
00352  * Parse a netmask in CIDR notation
00353  *
00354  * \details
00355  * For a mask of an IPv4 address, this should be a number between 0 and 32. For
00356  * a mask of an IPv6 address, this should be a number between 0 and 128. This
00357  * function creates an IPv6 ast_sockaddr from the given netmask. For masks of
00358  * IPv4 addresses, this is accomplished by adding 96 to the original netmask.
00359  *
00360  * \param[out] addr The ast_sockaddr produced from the CIDR netmask
00361  * \param is_v4 Tells if the address we are masking is IPv4.
00362  * \param mask_str The CIDR mask to convert
00363  * \retval -1 Failure
00364  * \retval 0 Success
00365  */
00366 static int parse_cidr_mask(struct ast_sockaddr *addr, int is_v4, const char *mask_str)
00367 {
00368    int mask;
00369 
00370    if (sscanf(mask_str, "%30d", &mask) != 1) {
00371       return -1;
00372    }
00373 
00374    if (is_v4) {
00375       struct sockaddr_in sin;
00376       if (mask < 0 || mask > 32) {
00377          return -1;
00378       }
00379       memset(&sin, 0, sizeof(sin));
00380       sin.sin_family = AF_INET;
00381       /* If mask is 0, then we already have the
00382        * appropriate all 0s address in sin from
00383        * the above memset.
00384        */
00385       if (mask != 0) {
00386          sin.sin_addr.s_addr = htonl(0xFFFFFFFF << (32 - mask));
00387       }
00388       ast_sockaddr_from_sin(addr, &sin);
00389    } else {
00390       struct sockaddr_in6 sin6;
00391       int i;
00392       if (mask < 0 || mask > 128) {
00393          return -1;
00394       }
00395       memset(&sin6, 0, sizeof(sin6));
00396       sin6.sin6_family = AF_INET6;
00397       for (i = 0; i < 4; ++i) {
00398          /* Once mask reaches 0, we don't have
00399           * to explicitly set anything anymore
00400           * since sin6 was zeroed out already
00401           */
00402          if (mask > 0) {
00403             V6_WORD(&sin6, i) = htonl(0xFFFFFFFF << (mask < 32 ? (32 - mask) : 0));
00404             mask -= mask < 32 ? mask : 32;
00405          }
00406       }
00407       memcpy(&addr->ss, &sin6, sizeof(sin6));
00408       addr->len = sizeof(sin6);
00409    }
00410 
00411    return 0;
00412 }
00413 
00414 void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
00415 {
00416    struct ast_acl *acl = NULL;
00417    struct ast_acl *current;
00418    struct ast_acl_list *working_list;
00419 
00420    char *tmp, *list;
00421 
00422    /* If the ACL list is currently uninitialized, it must be initialized. */
00423    if (*path == NULL) {
00424       struct ast_acl_list *list;
00425       list = ast_calloc(1, sizeof(*list));
00426       if (!list) {
00427          /* Allocation Error */
00428          if (error) {
00429             *error = 1;
00430          }
00431          return;
00432       }
00433 
00434       AST_LIST_HEAD_INIT(list);
00435       *path = list;
00436    }
00437 
00438    working_list = *path;
00439 
00440    AST_LIST_LOCK(working_list);
00441 
00442    /* First we need to determine if we will need to add a new ACL node or if we can use an existing one. */
00443    if (strncasecmp(sense, "a", 1)) {
00444       /* The first element in the path should be the unnamed, base ACL. If that's the case, we use it. If not,
00445        * we have to make one and link it up appropriately. */
00446       current = AST_LIST_FIRST(working_list);
00447 
00448       if (!current || !ast_strlen_zero(current->name)) {
00449          if (acl_new(&acl, "")) {
00450             if (error) {
00451                *error = 1;
00452             }
00453          }
00454          // Need to INSERT the ACL at the head here.
00455          AST_LIST_INSERT_HEAD(working_list, acl, list);
00456       } else {
00457          /* If the first element was already the unnamed base ACL, we just use that one. */
00458          acl = current;
00459       }
00460 
00461       /* With the proper ACL set for modification, we can just pass this off to the ast_ha append function. */
00462       acl->acl = ast_append_ha(sense, stuff, acl->acl, error);
00463 
00464       AST_LIST_UNLOCK(working_list);
00465       return;
00466    }
00467 
00468    /* We are in ACL append mode, so we know we'll be adding one or more named ACLs. */
00469    list = ast_strdupa(stuff);
00470 
00471    while ((tmp = strsep(&list, ","))) {
00472       struct ast_ha *named_ha;
00473       int already_included = 0;
00474 
00475       /* Remove leading whitespace from the string in case the user put spaces between items */
00476       tmp = ast_skip_blanks(tmp);
00477 
00478       /* The first step is to check for a duplicate */
00479       AST_LIST_TRAVERSE(working_list, current, list) {
00480          if (!strcasecmp(current->name, tmp)) { /* ACL= */
00481             /* Inclusion of the same ACL multiple times isn't a catastrophic error, but it will raise the error flag and skip the entry. */
00482             ast_log(LOG_ERROR, "Named ACL '%s' is already included in the ast_acl container.", tmp);
00483             if (error) {
00484                *error = 1;
00485             }
00486             already_included = 1;
00487             break;
00488          }
00489       }
00490 
00491       if (already_included) {
00492          continue;
00493       }
00494 
00495       if (acl_new(&acl, tmp)) {
00496          /* This is a catastrophic allocation error and we'll return immediately if this happens. */
00497          if (error) {
00498             *error = 1;
00499          }
00500          AST_LIST_UNLOCK(working_list);
00501          return;
00502       }
00503 
00504       /* Attempt to grab the Named ACL we are looking for. */
00505       named_ha = ast_named_acl_find(tmp, &acl->is_realtime, &acl->is_invalid);
00506 
00507       /* Set the ACL's ast_ha to the duplicated named ACL retrieved above. */
00508       acl->acl = named_ha;
00509 
00510       /* Raise the named_acl_flag since we are adding a named ACL to the ACL container. */
00511       if (named_acl_flag) {
00512          *named_acl_flag = 1;
00513       }
00514 
00515       /* Now insert the new ACL at the end of the list. */
00516       AST_LIST_INSERT_TAIL(working_list, acl, list);
00517    }
00518 
00519    AST_LIST_UNLOCK(working_list);
00520 }
00521 
00522 int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
00523 {
00524    struct ast_acl *head;
00525 
00526    if (!acl_list) {
00527       return 1;
00528    }
00529 
00530    AST_LIST_LOCK(acl_list);
00531    head = AST_LIST_FIRST(acl_list);
00532    AST_LIST_UNLOCK(acl_list);
00533 
00534    if (head) {
00535       return 0;
00536    }
00537 
00538    return 1;
00539 }
00540 
00541 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
00542 {
00543    struct ast_ha *ha;
00544    struct ast_ha *prev = NULL;
00545    struct ast_ha *ret;
00546    char *tmp, *list = ast_strdupa(stuff);
00547    char *address = NULL, *mask = NULL;
00548    int addr_is_v4;
00549    int allowing = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
00550    const char *parsed_addr, *parsed_mask;
00551 
00552    ret = path;
00553    while (path) {
00554       prev = path;
00555       path = path->next;
00556    }
00557 
00558    while ((tmp = strsep(&list, ","))) {
00559       if (!(ha = ast_calloc(1, sizeof(*ha)))) {
00560          if (error) {
00561             *error = 1;
00562          }
00563          return ret;
00564       }
00565 
00566       address = strsep(&tmp, "/");
00567       if (!address) {
00568          address = tmp;
00569       } else {
00570          mask = tmp;
00571       }
00572 
00573       if (*address == '!') {
00574          ha->sense = (allowing == AST_SENSE_DENY) ? AST_SENSE_ALLOW : AST_SENSE_DENY;
00575          address++;
00576       } else {
00577          ha->sense = allowing;
00578       }
00579 
00580       if (!ast_sockaddr_parse(&ha->addr, address, PARSE_PORT_FORBID)) {
00581          ast_log(LOG_WARNING, "Invalid IP address: %s\n", address);
00582          ast_free_ha(ha);
00583          if (error) {
00584             *error = 1;
00585          }
00586          return ret;
00587       }
00588 
00589       /* If someone specifies an IPv4-mapped IPv6 address,
00590        * we just convert this to an IPv4 ACL
00591        */
00592       if (ast_sockaddr_ipv4_mapped(&ha->addr, &ha->addr)) {
00593          ast_log(LOG_NOTICE, "IPv4-mapped ACL network address specified. "
00594             "Converting to an IPv4 ACL network address.\n");
00595       }
00596 
00597       addr_is_v4 = ast_sockaddr_is_ipv4(&ha->addr);
00598 
00599       if (!mask) {
00600          parse_cidr_mask(&ha->netmask, addr_is_v4, addr_is_v4 ? "32" : "128");
00601       } else if (strchr(mask, ':') || strchr(mask, '.')) {
00602          int mask_is_v4;
00603          /* Mask is of x.x.x.x or x:x:x:x:x:x:x:x variety */
00604          if (!ast_sockaddr_parse(&ha->netmask, mask, PARSE_PORT_FORBID)) {
00605             ast_log(LOG_WARNING, "Invalid netmask: %s\n", mask);
00606             ast_free_ha(ha);
00607             if (error) {
00608                *error = 1;
00609             }
00610             return ret;
00611          }
00612          /* If someone specifies an IPv4-mapped IPv6 netmask,
00613           * we just convert this to an IPv4 ACL
00614           */
00615          if (ast_sockaddr_ipv4_mapped(&ha->netmask, &ha->netmask)) {
00616             ast_log(LOG_NOTICE, "IPv4-mapped ACL netmask specified. "
00617                "Converting to an IPv4 ACL netmask.\n");
00618          }
00619          mask_is_v4 = ast_sockaddr_is_ipv4(&ha->netmask);
00620          if (addr_is_v4 ^ mask_is_v4) {
00621             ast_log(LOG_WARNING, "Address and mask are not using same address scheme.\n");
00622             ast_free_ha(ha);
00623             if (error) {
00624                *error = 1;
00625             }
00626             return ret;
00627          }
00628       } else if (parse_cidr_mask(&ha->netmask, addr_is_v4, mask)) {
00629          ast_log(LOG_WARNING, "Invalid CIDR netmask: %s\n", mask);
00630          ast_free_ha(ha);
00631          if (error) {
00632             *error = 1;
00633          }
00634          return ret;
00635       }
00636 
00637       if (ast_sockaddr_apply_netmask(&ha->addr, &ha->netmask, &ha->addr)) {
00638          /* This shouldn't happen because ast_sockaddr_parse would
00639           * have failed much earlier on an unsupported address scheme
00640           */
00641          char *failmask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
00642          char *failaddr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
00643          ast_log(LOG_WARNING, "Unable to apply netmask %s to address %s\n", failmask, failaddr);
00644          ast_free_ha(ha);
00645          if (error) {
00646             *error = 1;
00647          }
00648          return ret;
00649       }
00650 
00651       if (prev) {
00652          prev->next = ha;
00653       } else {
00654          ret = ha;
00655       }
00656       prev = ha;
00657 
00658       parsed_addr = ast_strdupa(ast_sockaddr_stringify(&ha->addr));
00659       parsed_mask = ast_strdupa(ast_sockaddr_stringify(&ha->netmask));
00660 
00661       ast_debug(3, "%s/%s sense %u appended to ACL\n", parsed_addr, parsed_mask, ha->sense);
00662    }
00663 
00664    return ret;
00665 }
00666 
00667 void ast_ha_join(const struct ast_ha *ha, struct ast_str **buf)
00668 {
00669    for (; ha; ha = ha->next) {
00670       const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
00671       ast_str_append(buf, 0, "%s%s/%s",
00672                 ha->sense == AST_SENSE_ALLOW ? "!" : "",
00673                 addr, ast_sockaddr_stringify_addr(&ha->netmask));
00674       if (ha->next) {
00675          ast_str_append(buf, 0, ",");
00676       }
00677    }
00678 }
00679 
00680 void ast_ha_join_cidr(const struct ast_ha *ha, struct ast_str **buf)
00681 {
00682    for (; ha; ha = ha->next) {
00683       const char *addr = ast_sockaddr_stringify_addr(&ha->addr);
00684       ast_str_append(buf, 0, "%s%s/%d",
00685                 ha->sense == AST_SENSE_ALLOW ? "!" : "",
00686                 addr, ast_sockaddr_cidr_bits(&ha->netmask));
00687       if (ha->next) {
00688          ast_str_append(buf, 0, ",");
00689       }
00690    }
00691 }
00692 
00693 enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
00694 {
00695    struct ast_acl *acl;
00696 
00697    /* If the list is NULL, there are no rules, so we'll allow automatically. */
00698    if (!acl_list) {
00699       return AST_SENSE_ALLOW;
00700    }
00701 
00702    AST_LIST_LOCK(acl_list);
00703 
00704    AST_LIST_TRAVERSE(acl_list, acl, list) {
00705       if (acl->is_invalid) {
00706          /* In this case, the baseline ACL shouldn't ever trigger this, but if that somehow happens, it'll still be shown. */
00707          ast_log(LOG_WARNING, "%sRejecting '%s' due to use of an invalid ACL '%s'.\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
00708                ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
00709          AST_LIST_UNLOCK(acl_list);
00710          return AST_SENSE_DENY;
00711       }
00712 
00713       if (acl->acl) {
00714          if (ast_apply_ha(acl->acl, addr) == AST_SENSE_DENY) {
00715             ast_log(LOG_NOTICE, "%sRejecting '%s' due to a failure to pass ACL '%s'\n", purpose ? purpose : "", ast_sockaddr_stringify_addr(addr),
00716                   ast_strlen_zero(acl->name) ? "(BASELINE)" : acl->name);
00717             AST_LIST_UNLOCK(acl_list);
00718             return AST_SENSE_DENY;
00719          }
00720       }
00721    }
00722 
00723    AST_LIST_UNLOCK(acl_list);
00724 
00725    return AST_SENSE_ALLOW;
00726 }
00727 
00728 enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr)
00729 {
00730    /* Start optimistic */
00731    enum ast_acl_sense res = AST_SENSE_ALLOW;
00732    const struct ast_ha *current_ha;
00733 
00734    for (current_ha = ha; current_ha; current_ha = current_ha->next) {
00735       struct ast_sockaddr result;
00736       struct ast_sockaddr mapped_addr;
00737       const struct ast_sockaddr *addr_to_use;
00738 #if 0 /* debugging code */
00739       char iabuf[INET_ADDRSTRLEN];
00740       char iabuf2[INET_ADDRSTRLEN];
00741       /* DEBUG */
00742       ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
00743       ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
00744       ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
00745 #endif
00746       if (ast_sockaddr_is_ipv4(&current_ha->addr)) {
00747          if (ast_sockaddr_is_ipv6(addr)) {
00748             if (ast_sockaddr_is_ipv4_mapped(addr)) {
00749                /* IPv4 ACLs apply to IPv4-mapped addresses */
00750                if (!ast_sockaddr_ipv4_mapped(addr, &mapped_addr)) {
00751                   ast_log(LOG_ERROR, "%s provided to ast_sockaddr_ipv4_mapped could not be converted. That shouldn't be possible.\n",
00752                      ast_sockaddr_stringify(addr));
00753                   continue;
00754                }
00755                addr_to_use = &mapped_addr;
00756             } else {
00757                /* An IPv4 ACL does not apply to an IPv6 address */
00758                continue;
00759             }
00760          } else {
00761             /* Address is IPv4 and ACL is IPv4. No biggie */
00762             addr_to_use = addr;
00763          }
00764       } else {
00765          if (ast_sockaddr_is_ipv6(addr) && !ast_sockaddr_is_ipv4_mapped(addr)) {
00766             addr_to_use = addr;
00767          } else {
00768             /* Address is IPv4 or IPv4 mapped but ACL is IPv6. Skip */
00769             continue;
00770          }
00771       }
00772 
00773       /* For each rule, if this address and the netmask = the net address
00774          apply the current rule */
00775       if (ast_sockaddr_apply_netmask(addr_to_use, &current_ha->netmask, &result)) {
00776          /* Unlikely to happen since we know the address to be IPv4 or IPv6 */
00777          continue;
00778       }
00779       if (!ast_sockaddr_cmp_addr(&result, &current_ha->addr)) {
00780          res = current_ha->sense;
00781       }
00782    }
00783    return res;
00784 }
00785 
00786 static int resolve_first(struct ast_sockaddr *addr, const char *name, int flag,
00787           int family)
00788 {
00789    struct ast_sockaddr *addrs;
00790    int addrs_cnt;
00791 
00792    addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
00793    if (addrs_cnt > 0) {
00794       if (addrs_cnt > 1) {
00795          ast_debug(1, "Multiple addresses. Using the first only\n");
00796       }
00797       ast_sockaddr_copy(addr, &addrs[0]);
00798       ast_free(addrs);
00799    } else {
00800       ast_log(LOG_WARNING, "Unable to lookup '%s'\n", name);
00801       return -1;
00802    }
00803 
00804    return 0;
00805 }
00806 
00807 int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service)
00808 {
00809    char srv[256];
00810    char host[256];
00811    int srv_ret = 0;
00812    int tportno;
00813 
00814    if (service) {
00815       snprintf(srv, sizeof(srv), "%s.%s", service, hostname);
00816       if ((srv_ret = ast_get_srv(NULL, host, sizeof(host), &tportno, srv)) > 0) {
00817          hostname = host;
00818       }
00819    }
00820 
00821    if (resolve_first(addr, hostname, PARSE_PORT_FORBID, addr->ss.ss_family) != 0) {
00822       return -1;
00823    }
00824 
00825    if (srv_ret > 0) {
00826       ast_sockaddr_set_port(addr, tportno);
00827    }
00828 
00829    return 0;
00830 }
00831 
00832 struct dscp_codepoint {
00833    char *name;
00834    unsigned int space;
00835 };
00836 
00837 /* IANA registered DSCP codepoints */
00838 
00839 static const struct dscp_codepoint dscp_pool1[] = {
00840    { "CS0", 0x00 },
00841    { "CS1", 0x08 },
00842    { "CS2", 0x10 },
00843    { "CS3", 0x18 },
00844    { "CS4", 0x20 },
00845    { "CS5", 0x28 },
00846    { "CS6", 0x30 },
00847    { "CS7", 0x38 },
00848    { "AF11", 0x0A },
00849    { "AF12", 0x0C },
00850    { "AF13", 0x0E },
00851    { "AF21", 0x12 },
00852    { "AF22", 0x14 },
00853    { "AF23", 0x16 },
00854    { "AF31", 0x1A },
00855    { "AF32", 0x1C },
00856    { "AF33", 0x1E },
00857    { "AF41", 0x22 },
00858    { "AF42", 0x24 },
00859    { "AF43", 0x26 },
00860    { "EF", 0x2E },
00861 };
00862 
00863 int ast_str2cos(const char *value, unsigned int *cos)
00864 {
00865    int fval;
00866 
00867    if (sscanf(value, "%30d", &fval) == 1) {
00868       if (fval < 8) {
00869           *cos = fval;
00870           return 0;
00871       }
00872    }
00873 
00874    return -1;
00875 }
00876 
00877 int ast_str2tos(const char *value, unsigned int *tos)
00878 {
00879    int fval;
00880    unsigned int x;
00881 
00882    if (sscanf(value, "%30i", &fval) == 1) {
00883       *tos = fval & 0xFF;
00884       return 0;
00885    }
00886 
00887    for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
00888       if (!strcasecmp(value, dscp_pool1[x].name)) {
00889          *tos = dscp_pool1[x].space << 2;
00890          return 0;
00891       }
00892    }
00893 
00894    return -1;
00895 }
00896 
00897 const char *ast_tos2str(unsigned int tos)
00898 {
00899    unsigned int x;
00900 
00901    for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
00902       if (dscp_pool1[x].space == (tos >> 2)) {
00903          return dscp_pool1[x].name;
00904       }
00905    }
00906 
00907    return "unknown";
00908 }
00909 
00910 int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
00911 {
00912    return ast_get_ip_or_srv(addr, hostname, NULL);
00913 }
00914 
00915 int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
00916 {
00917    int port;
00918    int s;
00919 
00920    port = ast_sockaddr_port(us);
00921 
00922    if ((s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET,
00923          SOCK_DGRAM, 0)) < 0) {
00924       ast_log(LOG_ERROR, "Cannot create socket\n");
00925       return -1;
00926    }
00927 
00928    if (ast_connect(s, them)) {
00929       ast_log(LOG_WARNING, "Cannot connect\n");
00930       close(s);
00931       return -1;
00932    }
00933    if (ast_getsockname(s, us)) {
00934 
00935       ast_log(LOG_WARNING, "Cannot get socket name\n");
00936       close(s);
00937       return -1;
00938    }
00939    close(s);
00940 
00941    {
00942       const char *them_addr = ast_strdupa(ast_sockaddr_stringify_addr(them));
00943       const char *us_addr = ast_strdupa(ast_sockaddr_stringify_addr(us));
00944 
00945       ast_debug(3, "For destination '%s', our source address is '%s'.\n",
00946             them_addr, us_addr);
00947    }
00948 
00949    ast_sockaddr_set_port(us, port);
00950 
00951    return 0;
00952 }
00953 
00954 int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
00955 {
00956    char ourhost[MAXHOSTNAMELEN] = "";
00957    struct ast_sockaddr root;
00958    int res, port = ast_sockaddr_port(ourip);
00959 
00960    /* just use the bind address if it is nonzero */
00961    if (!ast_sockaddr_is_any(bindaddr)) {
00962       ast_sockaddr_copy(ourip, bindaddr);
00963       ast_debug(3, "Attached to given IP address\n");
00964       return 0;
00965    }
00966    /* try to use our hostname */
00967    if (gethostname(ourhost, sizeof(ourhost) - 1)) {
00968       ast_log(LOG_WARNING, "Unable to get hostname\n");
00969    } else {
00970       if (resolve_first(ourip, ourhost, PARSE_PORT_FORBID, family) == 0) {
00971          /* reset port since resolve_first wipes this out */
00972          ast_sockaddr_set_port(ourip, port);
00973          return 0;
00974       }
00975    }
00976    ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
00977    /* A.ROOT-SERVERS.NET. */
00978    if (!resolve_first(&root, "A.ROOT-SERVERS.NET", PARSE_PORT_FORBID, 0) &&
00979        !ast_ouraddrfor(&root, ourip)) {
00980       /* reset port since resolve_first wipes this out */
00981       ast_sockaddr_set_port(ourip, port);
00982       return 0;
00983    }
00984    res = get_local_address(ourip);
00985    ast_sockaddr_set_port(ourip, port);
00986    return res;
00987 }
00988 

Generated on Thu Apr 16 06:27:05 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6