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)
127 struct ast_ha *prev = NULL;
130 char *tmp = ast_strdupa(stuff);
138 ha = ast_malloc(sizeof(*ha));
142 nm = strchr(tmp, '/');
144 /* assume /32. Yes, htonl does not do anything for this particular mask
145 but we better use it to show we remember about byte order */
146 ha->netmask.s_addr = htonl(0xFFFFFFFF);
151 if (!strchr(nm, '.')) {
152 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
153 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
155 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
161 } else if (!inet_aton(nm, &ha->netmask)) {
162 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
170 if (!inet_aton(tmp, &ha->netaddr)) {
171 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
178 ha->netaddr.s_addr &= ha->netmask.s_addr;
180 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
189 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);
194 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
196 /* Start optimistic */
197 int res = AST_SENSE_ALLOW;
199 #if 0 /* debugging code */
200 char iabuf[INET_ADDRSTRLEN];
201 char iabuf2[INET_ADDRSTRLEN];
203 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
204 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
205 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
207 /* For each rule, if this address and the netmask = the net address
208 apply the current rule */
209 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
216 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
219 struct ast_hostent ahp;
222 int tportno = ntohs(sin->sin_port);
224 snprintf(srv, sizeof(srv), "%s.%s", service, value);
225 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
226 sin->sin_port = htons(tportno);
230 hp = ast_gethostbyname(value, &ahp);
232 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
234 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
240 struct dscp_codepoint {
245 /* IANA registered DSCP codepoints */
247 static const struct dscp_codepoint dscp_pool1[] = {
271 int ast_str2cos(const char *value, unsigned int *cos)
275 if (sscanf(value, "%d", &fval) == 1) {
285 int ast_str2tos(const char *value, unsigned int *tos)
290 if (sscanf(value, "%i", &fval) == 1) {
295 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
296 if (!strcasecmp(value, dscp_pool1[x].name)) {
297 *tos = dscp_pool1[x].space << 2;
305 const char *ast_tos2str(unsigned int tos)
309 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
310 if (dscp_pool1[x].space == (tos >> 2))
311 return dscp_pool1[x].name;
317 int ast_get_ip(struct sockaddr_in *sin, const char *value)
319 return ast_get_ip_or_srv(sin, value, NULL);
322 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
325 struct sockaddr_in sin;
327 s = socket(PF_INET, SOCK_DGRAM, 0);
329 ast_log(LOG_WARNING, "Cannot create socket\n");
332 sin.sin_family = AF_INET;
334 sin.sin_addr = *them;
335 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
336 ast_log(LOG_WARNING, "Cannot connect\n");
341 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
342 ast_log(LOG_WARNING, "Cannot get socket name\n");
351 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
353 char ourhost[MAXHOSTNAMELEN] = "";
354 struct ast_hostent ahp;
356 struct in_addr saddr;
358 /* just use the bind address if it is nonzero */
359 if (ntohl(bindaddr.sin_addr.s_addr)) {
360 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
363 /* try to use our hostname */
364 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
365 ast_log(LOG_WARNING, "Unable to get hostname\n");
367 hp = ast_gethostbyname(ourhost, &ahp);
369 memcpy(ourip, hp->h_addr, sizeof(*ourip));
373 /* A.ROOT-SERVERS.NET. */
374 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))