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$")
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <sys/ioctl.h>
44 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
46 #include <net/route.h>
50 #include <sys/sockio.h>
53 #include "asterisk/acl.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/channel.h"
56 #include "asterisk/options.h"
57 #include "asterisk/utils.h"
58 #include "asterisk/lock.h"
59 #include "asterisk/srv.h"
62 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
63 struct sockaddr_in ifru_addr;
66 /* Free HA structure */
67 void ast_free_ha(struct ast_ha *ha)
77 /* Copy HA structure */
78 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
80 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
81 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
82 to->sense = from->sense;
85 /* Create duplicate of ha structure */
86 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
88 struct ast_ha *new_ha;
90 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
91 /* Copy from original to new object */
92 ast_copy_ha(original, new_ha);
98 /* Create duplicate HA link list */
99 /* Used in chan_sip2 templates */
100 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
102 struct ast_ha *start = original;
103 struct ast_ha *ret = NULL;
104 struct ast_ha *link, *prev = NULL;
107 link = ast_duplicate_ha(start); /* Create copy of this object */
109 prev->next = link; /* Link previous to this object */
112 ret = link; /* Save starting point */
114 start = start->next; /* Go to next object */
115 prev = link; /* Save pointer to this object */
117 return ret; /* Return start of list */
120 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
124 struct ast_ha *prev = NULL;
127 char *tmp = ast_strdupa(stuff);
135 ha = ast_malloc(sizeof(*ha));
139 nm = strchr(tmp, '/');
141 /* assume /32. Yes, htonl does not do anything for this particular mask
142 but we better use it to show we remember about byte order */
143 ha->netmask.s_addr = htonl(0xFFFFFFFF);
148 if (!strchr(nm, '.')) {
149 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
150 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
152 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
158 } else if (!inet_aton(nm, &ha->netmask)) {
159 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
167 if (!inet_aton(tmp, &ha->netaddr)) {
168 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
175 ha->netaddr.s_addr &= ha->netmask.s_addr;
177 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
186 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);
191 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
193 /* Start optimistic */
194 int res = AST_SENSE_ALLOW;
196 #if 0 /* debugging code */
197 char iabuf[INET_ADDRSTRLEN];
198 char iabuf2[INET_ADDRSTRLEN];
200 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
201 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
202 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
204 /* For each rule, if this address and the netmask = the net address
205 apply the current rule */
206 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
213 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
216 struct ast_hostent ahp;
219 int tportno = ntohs(sin->sin_port);
221 snprintf(srv, sizeof(srv), "%s.%s", service, value);
222 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
223 sin->sin_port = htons(tportno);
227 hp = ast_gethostbyname(value, &ahp);
229 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
231 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
237 struct dscp_codepoint {
242 /* IANA registered DSCP codepoints */
244 static const struct dscp_codepoint dscp_pool1[] = {
268 int ast_str2cos(const char *value, unsigned int *cos)
272 if (sscanf(value, "%d", &fval) == 1) {
282 int ast_str2tos(const char *value, unsigned int *tos)
287 if (sscanf(value, "%i", &fval) == 1) {
292 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
293 if (!strcasecmp(value, dscp_pool1[x].name)) {
294 *tos = dscp_pool1[x].space << 2;
302 const char *ast_tos2str(unsigned int tos)
306 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
307 if (dscp_pool1[x].space == (tos >> 2))
308 return dscp_pool1[x].name;
314 int ast_get_ip(struct sockaddr_in *sin, const char *value)
316 return ast_get_ip_or_srv(sin, value, NULL);
319 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
322 struct sockaddr_in sin;
324 s = socket(PF_INET, SOCK_DGRAM, 0);
326 ast_log(LOG_WARNING, "Cannot create socket\n");
329 sin.sin_family = AF_INET;
331 sin.sin_addr = *them;
332 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
333 ast_log(LOG_WARNING, "Cannot connect\n");
338 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
339 ast_log(LOG_WARNING, "Cannot get socket name\n");
348 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
350 char ourhost[MAXHOSTNAMELEN] = "";
351 struct ast_hostent ahp;
353 struct in_addr saddr;
355 /* just use the bind address if it is nonzero */
356 if (ntohl(bindaddr.sin_addr.s_addr)) {
357 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
360 /* try to use our hostname */
361 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
362 ast_log(LOG_WARNING, "Unable to get hostname\n");
364 hp = ast_gethostbyname(ourhost, &ahp);
366 memcpy(ourip, hp->h_addr, sizeof(*ourip));
370 /* A.ROOT-SERVERS.NET. */
371 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))