Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:56 2009

## Asterisk developer's documentation

# acl.c File Reference

Various sorts of access control. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <sys/time.h>`
`#include <signal.h>`
`#include <errno.h>`
`#include <unistd.h>`
`#include <netinet/in.h>`
`#include <arpa/inet.h>`
`#include <sys/socket.h>`
`#include <netdb.h>`
`#include <net/if.h>`
`#include <netinet/in_systm.h>`
`#include <netinet/ip.h>`
`#include <sys/ioctl.h>`
`#include "asterisk.h"`
`#include "asterisk/acl.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/options.h"`
`#include "asterisk/utils.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/srv.h"`
`#include "asterisk/compat.h"`

Include dependency graph for acl.c:

Go to the source code of this file.

## Data Structures

struct | my_ifreq

## Defines

#define | IPTOS_LOWCOST 0x02
#define | IPTOS_MINCOST IPTOS_LOWCOST

## Functions

struct ast_ha * | ast_append_ha (char *sense, const char *stuff, struct ast_ha *path)
int | ast_apply_ha (struct ast_ha *ha, struct sockaddr_in *sin)
void | ast_copy_ha (const struct ast_ha *from, struct ast_ha *to)
static struct ast_ha * | ast_duplicate_ha (struct ast_ha *original)
struct ast_ha * | ast_duplicate_ha_list (struct ast_ha *original)
int | ast_find_ourip (struct in_addr *ourip, struct sockaddr_in bindaddr)
void | ast_free_ha (struct ast_ha *ha)
int | ast_get_ip (struct sockaddr_in *sin, const char *value)
int | ast_get_ip_or_srv (struct sockaddr_in *sin, const char *value, const char *service)
int | ast_lookup_iface (char *iface, struct in_addr *address)
int | ast_ouraddrfor (struct in_addr *them, struct in_addr *us)
int | ast_str2tos (const char *value, int *tos)

## Variables

static struct in_addr | __ourip = { 0x00000000 }

## Detailed Description

Various sorts of access control.

Definition in file acl.c.

## Define Documentation

#define IPTOS_LOWCOST 0x02

Definition at line 53 of file acl.c.

#define IPTOS_MINCOST IPTOS_LOWCOST

Definition at line 57 of file acl.c.

Referenced by ast_str2tos(), iax_template_parse(), reload_config(), and set_config().

## Function Documentation

struct ast_ha* ast_append_ha | ( | char * | sense,
|  | const char * | stuff,
|  | struct ast_ha * | path |
| ) |  |  | `[read]`

Definition at line 136 of file acl.c.

References ast_log(), AST_SENSE_ALLOW, AST_SENSE_DENY, free, LOG_DEBUG, LOG_WARNING, malloc, ast_ha::netaddr, ast_ha::netmask, ast_ha::next, and ast_ha::sense.

Referenced by add_calltoken_ignore(), authenticate(), build_callno_limits(), build_device(), build_gateway(), build_peer(), build_user(), and reload_config().

    00137 {
    00138    struct ast_ha *ha = malloc(sizeof(struct ast_ha));
    00139    char *nm = "255.255.255.255";
    00140    char tmp[256];
    00141    struct ast_ha *prev = NULL;
    00142    struct ast_ha *ret;
    00143    int x, z;
    00144    unsigned int y;
    00145    ret = path;
    00146    while (path) {
    00147       prev = path;
    00148       path = path->next;
    00149    }
    00150    if (ha) {
    00151       ast_copy_string(tmp, stuff, sizeof(tmp));
    00152       nm = strchr(tmp, '/');
    00153       if (!nm) {
    00154          nm = "255.255.255.255";
    00155       } else {
    00156          *nm = '\0';
    00157          nm++;
    00158       }
    00159       if (!strchr(nm, '.')) {
    00160          if ((sscanf(nm, "%30d", &x) == 1) && (x >= 0) && (x <= 32)) {
    00161             y = 0;
    00162             for (z=0;z<x;z++) {
    00163                y >>= 1;
    00164                y |= 0x80000000;
    00165             }
    00166             ha->netmask.s_addr = htonl(y);
    00167          }
    00168       } else if (!inet_aton(nm, &ha->netmask)) {
    00169          ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
    00170          free(ha);
    00171          return ret;
    00172       }
    00173       if (!inet_aton(tmp, &ha->netaddr)) {
    00174          ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
    00175          free(ha);
    00176          return ret;
    00177       }
    00178       ha->netaddr.s_addr &= ha->netmask.s_addr;
    00179       if (!strncasecmp(sense, "p", 1)) {
    00180          ha->sense = AST_SENSE_ALLOW;
    00181       } else {
    00182          ha->sense = AST_SENSE_DENY;
    00183       }
    00184       ha->next = NULL;
    00185       if (prev) {
    00186          prev->next = ha;
    00187       } else {
    00188          ret = ha;
    00189       }
    00190    }
    00191    ast_log(LOG_DEBUG, "%s/%s appended to acl for peer\n", stuff, nm);
    00192    return ret;
    00193 }

int ast_apply_ha | ( | struct ast_ha * | ha,
|  | struct sockaddr_in * | sin |
| ) |  |  |

Definition at line 195 of file acl.c.

References ast_inet_ntoa(), ast_log(), AST_SENSE_ALLOW, LOG_DEBUG, ast_ha::netaddr, ast_ha::netmask, ast_ha::next, and ast_ha::sense.

Referenced by ast_sip_ouraddrfor(), authenticate(), check_access(), check_user_full(), register_verify(), and skinny_register().

    00196 {
    00197    /* Start optimistic */
    00198    int res = AST_SENSE_ALLOW;
    00199    while (ha) {
    00200       char iabuf[INET_ADDRSTRLEN];
    00201       char iabuf2[INET_ADDRSTRLEN];
    00202       /* DEBUG */
    00203       ast_log(LOG_DEBUG,
    00204          "##### Testing %s with %s\n",
    00205          ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr),
    00206          ast_inet_ntoa(iabuf2, sizeof(iabuf2), ha->netaddr));
    00207       /* For each rule, if this address and the netmask = the net address
    00208          apply the current rule */
    00209       if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
    00210          res = ha->sense;
    00211       ha = ha->next;
    00212    }
    00213    return res;
    00214 }

void ast_copy_ha | ( | const struct ast_ha * | from,
|  | struct ast_ha * | to |
| ) |  |  |

Definition at line 97 of file acl.c.

References ast_ha::netaddr, ast_ha::netmask, and ast_ha::sense.

Referenced by add_calltoken_ignore(), ast_duplicate_ha(), and build_callno_limits().

    00098 {
    00099    memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
    00100    memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
    00101    to->sense = from->sense;
    00102 }

static struct ast_ha* ast_duplicate_ha | ( | struct ast_ha * | original | ) | `[static, read]`

Definition at line 105 of file acl.c.

References ast_copy_ha(), and malloc.

Referenced by ast_duplicate_ha_list().

    00106 {
    00107    struct ast_ha *new_ha = malloc(sizeof(struct ast_ha));
    00108    /* Copy from original to new object */
    00109    ast_copy_ha(original, new_ha);
    00110
    00111    return new_ha;
    00112 }

struct ast_ha* ast_duplicate_ha_list | ( | struct ast_ha * | original | ) | `[read]`

Definition at line 116 of file acl.c.

References ast_duplicate_ha(), and ast_ha::next.

    00117 {
    00118    struct ast_ha *start=original;
    00119    struct ast_ha *ret = NULL;
    00120    struct ast_ha *link,*prev=NULL;
    00121
    00122    while (start) {
    00123       link = ast_duplicate_ha(start);  /* Create copy of this object */
    00124       if (prev)
    00125          prev->next = link;      /* Link previous to this object */
    00126
    00127       if (!ret)
    00128          ret = link;    /* Save starting point */
    00129
    00130       start = start->next;    /* Go to next object */
    00131       prev = link;         /* Save pointer to this object */
    00132    }
    00133    return ret;             /* Return start of list */
    00134 }

int ast_find_ourip | ( | struct in_addr * | ourip,
|  | struct sockaddr_in | bindaddr |
| ) |  |  |

Definition at line 320 of file acl.c.

References ast_gethostbyname(), ast_log(), ast_ouraddrfor(), hp, LOG_WARNING, and ourhost.

Referenced by reload_config().

    00321 {
    00322    char ourhost[MAXHOSTNAMELEN] = "";
    00323    struct ast_hostent ahp;
    00324    struct hostent *hp;
    00325    struct in_addr saddr;
    00326
    00327    /* just use the bind address if it is nonzero */
    00328    if (ntohl(bindaddr.sin_addr.s_addr)) {
    00329       memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
    00330       return 0;
    00331    }
    00332    /* try to use our hostname */
    00333    if (gethostname(ourhost, sizeof(ourhost) - 1)) {
    00334       ast_log(LOG_WARNING, "Unable to get hostname\n");
    00335    } else {
    00336       hp = ast_gethostbyname(ourhost, &ahp);
    00337       if (hp) {
    00338          memcpy(ourip, hp->h_addr, sizeof(*ourip));
    00339          return 0;
    00340       }
    00341    }
    00342    /* A.ROOT-SERVERS.NET. */
    00343    if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
    00344       return 0;
    00345    return -1;
    00346 }

void ast_free_ha | ( | struct ast_ha * | ha | ) |

Definition at line 86 of file acl.c.

References free, and ast_ha::next.

Referenced by add_calltoken_ignore(), authenticate(), build_callno_limits(), build_peer(), build_user(), destroy_gateway(), destroy_peer(), destroy_user(), reload_config(), sip_destroy_peer(), sip_destroy_user(), and unload_module().

    00087 {
    00088    struct ast_ha *hal;
    00089    while(ha) {
    00090       hal = ha;
    00091       ha = ha->next;
    00092       free(hal);
    00093    }
    00094 }

int ast_get_ip | ( | struct sockaddr_in * | sin,
|  | const char * | value |
| ) |  |  |

Definition at line 262 of file acl.c.

References ast_get_ip_or_srv().

Referenced by build_device(), build_gateway(), build_peer(), build_user(), and peer_set_srcaddr().

    00263 {
    00264    return ast_get_ip_or_srv(sin, value, NULL);
    00265 }

int ast_get_ip_or_srv | ( | struct sockaddr_in * | sin,
|  | const char * | value,
|  | const char * | service |
| ) |  |  |

Definition at line 216 of file acl.c.

References ast_get_srv(), ast_gethostbyname(), ast_log(), host, hp, and LOG_WARNING.

Referenced by ast_get_ip(), build_peer(), and reload_config().

    00217 {
    00218    struct hostent *hp;
    00219    struct ast_hostent ahp;
    00220    char srv[256];
    00221    char host[256];
    00222    int tportno = ntohs(sin->sin_port);
    00223    if (inet_aton(value, &sin->sin_addr))
    00224       return 0;
    00225    if (service) {
    00226       snprintf(srv, sizeof(srv), "%s.%s", service, value);
    00227       if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
    00228          sin->sin_port = htons(tportno);
    00229          value = host;
    00230       }
    00231    }
    00232    hp = ast_gethostbyname(value, &ahp);
    00233    if (hp) {
    00234       memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
    00235    } else {
    00236       ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
    00237       return -1;
    00238    }
    00239    return 0;
    00240 }

int ast_lookup_iface | ( | char * | iface,
|  | struct in_addr * | address |
| ) |  |  |

Definition at line 268 of file acl.c.

References ast_log(), my_ifreq::ifrn_name, my_ifreq::ifru_addr, and LOG_WARNING.

    00269 {
    00270    int mysock, res = 0;
    00271    struct my_ifreq ifreq;
    00272
    00273    memset(&ifreq, 0, sizeof(ifreq));
    00274    ast_copy_string(ifreq.ifrn_name, iface, sizeof(ifreq.ifrn_name));
    00275
    00276    mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    00277    res = ioctl(mysock, SIOCGIFADDR, &ifreq);
    00278
    00279    close(mysock);
    00280    if (res < 0) {
    00281       ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
    00282       memcpy((char *)address, (char *)&__ourip, sizeof(__ourip));
    00283       return -1;
    00284    } else {
    00285       memcpy((char *)address, (char *)&ifreq.ifru_addr.sin_addr, sizeof(ifreq.ifru_addr.sin_addr));
    00286       return 0;
    00287    }
    00288 }

int ast_ouraddrfor | ( | struct in_addr * | them,
|  | struct in_addr * | us |
| ) |  |  |

Definition at line 290 of file acl.c.

References ast_log(), LOG_WARNING, and s.

Referenced by ast_find_ourip(), ast_sip_ouraddrfor(), build_device(), build_gateway(), and find_subchannel_and_lock().

    00291 {
    00292    int s;
    00293    struct sockaddr_in sin;
    00294    socklen_t slen;
    00295
    00296    s = socket(PF_INET, SOCK_DGRAM, 0);
    00297    if (s < 0) {
    00298       ast_log(LOG_WARNING, "Cannot create socket\n");
    00299       return -1;
    00300    }
    00301    sin.sin_family = AF_INET;
    00302    sin.sin_port = 5060;
    00303    sin.sin_addr = *them;
    00304    if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
    00305       ast_log(LOG_WARNING, "Cannot connect\n");
    00306       close(s);
    00307       return -1;
    00308    }
    00309    slen = sizeof(sin);
    00310    if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
    00311       ast_log(LOG_WARNING, "Cannot get socket name\n");
    00312       close(s);
    00313       return -1;
    00314    }
    00315    close(s);
    00316    *us = sin.sin_addr;
    00317    return 0;
    00318 }

int ast_str2tos | ( | const char * | value,
|  | int * | tos |
| ) |  |  |

Definition at line 242 of file acl.c.

References IPTOS_MINCOST.

Referenced by reload_config(), and set_config().

    00243 {
    00244    int fval;
    00245    if (sscanf(value, "%30i", &fval) == 1)
    00246       *tos = fval & 0xff;
    00247    else if (!strcasecmp(value, "lowdelay"))
    00248       *tos = IPTOS_LOWDELAY;
    00249    else if (!strcasecmp(value, "throughput"))
    00250       *tos = IPTOS_THROUGHPUT;
    00251    else if (!strcasecmp(value, "reliability"))
    00252       *tos = IPTOS_RELIABILITY;
    00253    else if (!strcasecmp(value, "mincost"))
    00254       *tos = IPTOS_MINCOST;
    00255    else if (!strcasecmp(value, "none"))
    00256       *tos = 0;
    00257    else
    00258       return -1;
    00259    return 0;
    00260 }

## Variable Documentation

struct in_addr __ourip = { 0x00000000 }`[static]`

Definition at line 78 of file acl.c.

Referenced by build_device(), build_gateway(), find_subchannel_and_lock(), handle_message(), reload_config(), sip_alloc(), sip_notify(), sip_poke_peer(), sip_request_call(), sip_send_mwi_to_peer(), transmit_register(), and transmit_response_using_temp().