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$")
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <sys/ioctl.h>
47 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
49 #include <net/route.h>
53 #include <sys/sockio.h>
56 #include "asterisk/acl.h"
57 #include "asterisk/logger.h"
58 #include "asterisk/channel.h"
59 #include "asterisk/options.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/lock.h"
62 #include "asterisk/srv.h"
65 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
66 struct sockaddr_in ifru_addr;
69 /* Free HA structure */
70 void ast_free_ha(struct ast_ha *ha)
80 /* Copy HA structure */
81 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
83 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
84 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
85 to->sense = from->sense;
88 /* Create duplicate of ha structure */
89 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
91 struct ast_ha *new_ha;
93 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
94 /* Copy from original to new object */
95 ast_copy_ha(original, new_ha);
101 /* Create duplicate HA link list */
102 /* Used in chan_sip2 templates */
103 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
105 struct ast_ha *start = original;
106 struct ast_ha *ret = NULL;
107 struct ast_ha *link, *prev = NULL;
110 link = ast_duplicate_ha(start); /* Create copy of this object */
112 prev->next = link; /* Link previous to this object */
115 ret = link; /* Save starting point */
117 start = start->next; /* Go to next object */
118 prev = link; /* Save pointer to this object */
120 return ret; /* Return start of list */
123 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path, int *error)
126 char *nm = "255.255.255.255";
128 struct ast_ha *prev = NULL;
138 if ((ha = ast_malloc(sizeof(*ha)))) {
139 ast_copy_string(tmp, stuff, sizeof(tmp));
140 nm = strchr(tmp, '/');
142 nm = "255.255.255.255";
147 if (!strchr(nm, '.')) {
148 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32)) {
150 for (z = 0; z < x; z++) {
154 ha->netmask.s_addr = htonl(y);
156 } else if (!inet_aton(nm, &ha->netmask)) {
157 ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
163 if (!inet_aton(tmp, &ha->netaddr)) {
164 ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
170 ha->netaddr.s_addr &= ha->netmask.s_addr;
171 if (!strncasecmp(sense, "p", 1)) {
172 ha->sense = AST_SENSE_ALLOW;
174 ha->sense = AST_SENSE_DENY;
183 ast_debug(1, "%s/%s appended to acl for peer\n", stuff, nm);
187 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
189 /* Start optimistic */
190 int res = AST_SENSE_ALLOW;
192 #if 0 /* debugging code */
193 char iabuf[INET_ADDRSTRLEN];
194 char iabuf2[INET_ADDRSTRLEN];
196 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
197 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
198 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
200 /* For each rule, if this address and the netmask = the net address
201 apply the current rule */
202 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
209 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
212 struct ast_hostent ahp;
215 int tportno = ntohs(sin->sin_port);
217 snprintf(srv, sizeof(srv), "%s.%s", service, value);
218 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
219 sin->sin_port = htons(tportno);
223 hp = ast_gethostbyname(value, &ahp);
225 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
227 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
233 struct dscp_codepoint {
238 /* IANA registered DSCP codepoints */
240 static const struct dscp_codepoint dscp_pool1[] = {
264 int ast_str2cos(const char *value, unsigned int *cos)
268 if (sscanf(value, "%d", &fval) == 1) {
278 int ast_str2tos(const char *value, unsigned int *tos)
283 if (sscanf(value, "%i", &fval) == 1) {
288 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
289 if (!strcasecmp(value, dscp_pool1[x].name)) {
290 *tos = dscp_pool1[x].space << 2;
298 const char *ast_tos2str(unsigned int tos)
302 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
303 if (dscp_pool1[x].space == (tos >> 2))
304 return dscp_pool1[x].name;
310 int ast_get_ip(struct sockaddr_in *sin, const char *value)
312 return ast_get_ip_or_srv(sin, value, NULL);
315 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
318 struct sockaddr_in sin;
320 s = socket(PF_INET, SOCK_DGRAM, 0);
322 ast_log(LOG_WARNING, "Cannot create socket\n");
325 sin.sin_family = AF_INET;
327 sin.sin_addr = *them;
328 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
329 ast_log(LOG_WARNING, "Cannot connect\n");
334 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
335 ast_log(LOG_WARNING, "Cannot get socket name\n");
344 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
346 char ourhost[MAXHOSTNAMELEN] = "";
347 struct ast_hostent ahp;
349 struct in_addr saddr;
351 /* just use the bind address if it is nonzero */
352 if (ntohl(bindaddr.sin_addr.s_addr)) {
353 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
356 /* try to use our hostname */
357 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
358 ast_log(LOG_WARNING, "Unable to get hostname\n");
360 hp = ast_gethostbyname(ourhost, &ahp);
362 memcpy(ourip, hp->h_addr, sizeof(*ourip));
366 /* A.ROOT-SERVERS.NET. */
367 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))