Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:00:59 2007

Asterisk developer's documentation :: Codename Pineapple


acl.c File Reference


Detailed Description

Various sorts of access control.

Author:
Mark Spencer <markster@digium.com>

Definition in file acl.c.

#include "asterisk.h"
#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/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 dependency graph for acl.c:

Go to the source code of this file.

Data Structures

struct  ast_ha
struct  dscp_codepoint
struct  my_ifreq

Defines

#define IPTOS_LOWCOST   0x02
#define IPTOS_MINCOST   IPTOS_LOWCOST

Functions

ast_haast_append_ha (char *sense, char *stuff, struct ast_ha *path, int *error)
 Append ACL entry to host access list.
int ast_apply_ha (struct ast_ha *ha, struct sockaddr_in *sin)
 Check IP address with host access list.
static void ast_copy_ha (struct ast_ha *from, struct ast_ha *to)
static struct ast_haast_duplicate_ha (struct ast_ha *original)
ast_haast_duplicate_ha_list (struct ast_ha *original)
 Copy host access list.
int ast_find_ourip (struct in_addr *ourip, struct sockaddr_in bindaddr)
void ast_free_ha (struct ast_ha *ha)
 Free host access list.
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_ouraddrfor (struct in_addr *them, struct in_addr *us)
int ast_str2tos (const char *value, unsigned int *tos)
const char * ast_tos2str (unsigned int tos)

Variables

static const struct dscp_codepoint dscp_pool1 []


Define Documentation

#define IPTOS_LOWCOST   0x02
 

Definition at line 58 of file acl.c.

#define IPTOS_MINCOST   IPTOS_LOWCOST
 

Definition at line 62 of file acl.c.

Referenced by reload_config(), and set_config().


Function Documentation

struct ast_ha* ast_append_ha char *  sense,
char *  stuff,
struct ast_ha path,
int *  error
 

Append ACL entry to host access list.

Definition at line 140 of file acl.c.

References ast_log(), ast_malloc, AST_SENSE_ALLOW, AST_SENSE_DENY, free, LOG_WARNING, ast_ha::netaddr, and ast_ha::next.

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

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

int ast_apply_ha struct ast_ha ha,
struct sockaddr_in *  sin
 

Check IP address with host access list.

Definition at line 205 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(), check_access(), check_user_full(), check_user_ok(), register_verify(), sip_ouraddrfor(), and skinny_register().

00206 {
00207    /* Start optimistic */
00208    int res = AST_SENSE_ALLOW;
00209    while (ha) {
00210       char iabuf[INET_ADDRSTRLEN];
00211       char iabuf2[INET_ADDRSTRLEN];
00212       /* DEBUG */
00213       ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
00214       ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
00215       ast_log(LOG_DEBUG, "##### Testing %s with %s\n", iabuf, iabuf2);
00216       /* For each rule, if this address and the netmask = the net address
00217          apply the current rule */
00218       if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
00219          res = ha->sense;
00220       ha = ha->next;
00221    }
00222    return res;
00223 }

static void ast_copy_ha struct ast_ha from,
struct ast_ha to
[static]
 

Definition at line 98 of file acl.c.

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

Referenced by ast_duplicate_ha().

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

static struct ast_ha* ast_duplicate_ha struct ast_ha original  )  [static]
 

Definition at line 106 of file acl.c.

References ast_copy_ha(), and ast_malloc.

Referenced by ast_duplicate_ha_list().

00107 {
00108    struct ast_ha *new_ha;
00109 
00110    if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
00111       /* Copy from original to new object */
00112       ast_copy_ha(original, new_ha);
00113    }
00114 
00115    return new_ha;
00116 }

struct ast_ha* ast_duplicate_ha_list struct ast_ha original  ) 
 

Copy host access list.

Definition at line 120 of file acl.c.

References ast_duplicate_ha(), and ast_ha::next.

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

int ast_find_ourip struct in_addr *  ourip,
struct sockaddr_in  bindaddr
 

Definition at line 349 of file acl.c.

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

Referenced by __oh323_rtp_create(), load_module(), and sipsock_init().

00350 {
00351    char ourhost[MAXHOSTNAMELEN] = "";
00352    struct ast_hostent ahp;
00353    struct hostent *hp;
00354    struct in_addr saddr;
00355 
00356    /* just use the bind address if it is nonzero */
00357    if (ntohl(bindaddr.sin_addr.s_addr)) {
00358       memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
00359       return 0;
00360    }
00361    /* try to use our hostname */
00362    if (gethostname(ourhost, sizeof(ourhost) - 1)) {
00363       ast_log(LOG_WARNING, "Unable to get hostname\n");
00364    } else {
00365       hp = ast_gethostbyname(ourhost, &ahp);
00366       if (hp) {
00367          memcpy(ourip, hp->h_addr, sizeof(*ourip));
00368          return 0;
00369       }
00370    }
00371    /* A.ROOT-SERVERS.NET. */
00372    if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
00373       return 0;
00374    return -1;
00375 }

void ast_free_ha struct ast_ha ha  ) 
 

Free host access list.

Definition at line 87 of file acl.c.

References free, and ast_ha::next.

Referenced by build_device(), build_peer(), build_user(), destroy_gateway(), destroy_peer(), destroy_user(), oh323_destroy_peer(), oh323_destroy_user(), sip_destroy_device(), sip_destroy_peer(), and sip_destroy_user().

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

int ast_get_ip struct sockaddr_in *  sin,
const char *  value
 

Definition at line 314 of file acl.c.

References ast_get_ip_or_srv().

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

00315 {
00316    return ast_get_ip_or_srv(sin, value, NULL);
00317 }

int ast_get_ip_or_srv struct sockaddr_in *  sin,
const char *  value,
const char *  service
 

Definition at line 225 of file acl.c.

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

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

00226 {
00227    struct hostent *hp;
00228    struct ast_hostent ahp;
00229    char srv[256];
00230    char host[256];
00231    int tportno = ntohs(sin->sin_port);
00232    if (inet_aton(value, &sin->sin_addr))
00233       return 0;
00234    if (service) {
00235       snprintf(srv, sizeof(srv), "%s.%s", service, value);
00236       if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
00237          sin->sin_port = htons(tportno);
00238          value = host;
00239       }
00240    }
00241    hp = ast_gethostbyname(value, &ahp);
00242    if (hp) {
00243       memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
00244    } else {
00245       ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
00246       return -1;
00247    }
00248    return 0;
00249 }

int ast_ouraddrfor struct in_addr *  them,
struct in_addr *  us
 

Definition at line 319 of file acl.c.

References ast_log(), LOG_WARNING, and s.

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

00320 {
00321    int s;
00322    struct sockaddr_in sin;
00323    socklen_t slen;
00324 
00325    s = socket(PF_INET, SOCK_DGRAM, 0);
00326    if (s < 0) {
00327       ast_log(LOG_WARNING, "Cannot create socket\n");
00328       return -1;
00329    }
00330    sin.sin_family = AF_INET;
00331    sin.sin_port = 5060;
00332    sin.sin_addr = *them;
00333    if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
00334       ast_log(LOG_WARNING, "Cannot connect\n");
00335       close(s);
00336       return -1;
00337    }
00338    slen = sizeof(sin);
00339    if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
00340       ast_log(LOG_WARNING, "Cannot get socket name\n");
00341       close(s);
00342       return -1;
00343    }
00344    close(s);
00345    *us = sin.sin_addr;
00346    return 0;
00347 }

int ast_str2tos const char *  value,
unsigned int *  tos
 

Definition at line 282 of file acl.c.

References dscp_pool1, name, and space.

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

00283 {
00284    int fval;
00285    unsigned int x;
00286 
00287    if (sscanf(value, "%i", &fval) == 1) {
00288       *tos = fval & 0xFF;
00289       return 0;
00290    }
00291 
00292    for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
00293       if (!strcasecmp(value, dscp_pool1[x].name)) {
00294          *tos = dscp_pool1[x].space << 2;
00295          return 0;
00296       }
00297    }
00298 
00299    return -1;
00300 }

const char* ast_tos2str unsigned int  tos  ) 
 

Definition at line 302 of file acl.c.

References dscp_pool1, name, and space.

Referenced by sip_show_settings(), and sipsocket_open().

00303 {
00304    unsigned int x;
00305 
00306    for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
00307       if (dscp_pool1[x].space == (tos >> 2))
00308          return dscp_pool1[x].name;
00309    }
00310 
00311    return "unknown";
00312 }


Variable Documentation

const struct dscp_codepoint dscp_pool1[] [static]
 

Definition at line 258 of file acl.c.

Referenced by ast_str2tos(), and ast_tos2str().


Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen