2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Various sorts of access control
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <sys/ioctl.h>
35 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
37 #include <net/route.h>
41 #include <sys/sockio.h>
44 #include "asterisk/acl.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/options.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/srv.h"
53 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
54 struct sockaddr_in ifru_addr;
57 /* Free HA structure */
58 void ast_free_ha(struct ast_ha *ha)
68 /* Copy HA structure */
69 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
71 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
72 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
73 to->sense = from->sense;
76 /* Create duplicate of ha structure */
77 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
79 struct ast_ha *new_ha;
81 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
82 /* Copy from original to new object */
83 ast_copy_ha(original, new_ha);
89 /* Create duplicate HA link list */
90 /* Used in chan_sip2 templates */
91 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
93 struct ast_ha *start = original;
94 struct ast_ha *ret = NULL;
95 struct ast_ha *link, *prev = NULL;
98 link = ast_duplicate_ha(start); /* Create copy of this object */
100 prev->next = link; /* Link previous to this object */
103 ret = link; /* Save starting point */
105 start = start->next; /* Go to next object */
106 prev = link; /* Save pointer to this object */
108 return ret; /* Return start of list */
111 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
115 struct ast_ha *prev = NULL;
118 char *tmp = ast_strdupa(stuff);
126 ha = ast_malloc(sizeof(*ha));
130 nm = strchr(tmp, '/');
132 /* assume /32. Yes, htonl does not do anything for this particular mask
133 but we better use it to show we remember about byte order */
134 ha->netmask.s_addr = htonl(0xFFFFFFFF);
139 if (!strchr(nm, '.')) {
140 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
141 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
143 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
149 } else if (!inet_aton(nm, &ha->netmask)) {
150 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
158 if (!inet_aton(tmp, &ha->netaddr)) {
159 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
166 ha->netaddr.s_addr &= ha->netmask.s_addr;
168 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
177 ast_debug(1, "%s/%s sense %d appended to acl for peer\n", ast_strdupa(ast_inet_ntoa(ha->netaddr)), ast_strdupa(ast_inet_ntoa(ha->netmask)), ha->sense);
182 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
184 /* Start optimistic */
185 int res = AST_SENSE_ALLOW;
187 #if 0 /* debugging code */
188 char iabuf[INET_ADDRSTRLEN];
189 char iabuf2[INET_ADDRSTRLEN];
191 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
192 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
193 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
195 /* For each rule, if this address and the netmask = the net address
196 apply the current rule */
197 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
204 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
207 struct ast_hostent ahp;
210 int tportno = ntohs(sin->sin_port);
212 snprintf(srv, sizeof(srv), "%s.%s", service, value);
213 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
214 sin->sin_port = htons(tportno);
218 hp = ast_gethostbyname(value, &ahp);
220 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
222 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
228 struct dscp_codepoint {
233 /* IANA registered DSCP codepoints */
235 static const struct dscp_codepoint dscp_pool1[] = {
259 int ast_str2cos(const char *value, unsigned int *cos)
263 if (sscanf(value, "%d", &fval) == 1) {
273 int ast_str2tos(const char *value, unsigned int *tos)
278 if (sscanf(value, "%i", &fval) == 1) {
283 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
284 if (!strcasecmp(value, dscp_pool1[x].name)) {
285 *tos = dscp_pool1[x].space << 2;
293 const char *ast_tos2str(unsigned int tos)
297 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
298 if (dscp_pool1[x].space == (tos >> 2))
299 return dscp_pool1[x].name;
305 int ast_get_ip(struct sockaddr_in *sin, const char *value)
307 return ast_get_ip_or_srv(sin, value, NULL);
310 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
313 struct sockaddr_in sin;
315 s = socket(PF_INET, SOCK_DGRAM, 0);
317 ast_log(LOG_WARNING, "Cannot create socket\n");
320 sin.sin_family = AF_INET;
322 sin.sin_addr = *them;
323 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
324 ast_log(LOG_WARNING, "Cannot connect\n");
329 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
330 ast_log(LOG_WARNING, "Cannot get socket name\n");
339 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
341 char ourhost[MAXHOSTNAMELEN] = "";
342 struct ast_hostent ahp;
344 struct in_addr saddr;
346 /* just use the bind address if it is nonzero */
347 if (ntohl(bindaddr.sin_addr.s_addr)) {
348 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
351 /* try to use our hostname */
352 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
353 ast_log(LOG_WARNING, "Unable to get hostname\n");
355 hp = ast_gethostbyname(ourhost, &ahp);
357 memcpy(ourip, hp->h_addr, sizeof(*ourip));
361 /* A.ROOT-SERVERS.NET. */
362 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))