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 /* netinet/ip.h may not define the following (See RFCs 791 and 1349) */
57 #if !defined(IPTOS_LOWCOST)
58 #define IPTOS_LOWCOST 0x02
61 #if !defined(IPTOS_MINCOST)
62 #define IPTOS_MINCOST IPTOS_LOWCOST
65 #include "asterisk/acl.h"
66 #include "asterisk/logger.h"
67 #include "asterisk/channel.h"
68 #include "asterisk/options.h"
69 #include "asterisk/utils.h"
70 #include "asterisk/lock.h"
71 #include "asterisk/srv.h"
74 /* Host access rule */
75 struct in_addr netaddr;
76 struct in_addr netmask;
82 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
83 struct sockaddr_in ifru_addr;
86 /* Free HA structure */
87 void ast_free_ha(struct ast_ha *ha)
97 /* Copy HA structure */
98 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
100 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
101 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
102 to->sense = from->sense;
105 /* Create duplicate of ha structure */
106 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
108 struct ast_ha *new_ha;
110 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
111 /* Copy from original to new object */
112 ast_copy_ha(original, new_ha);
118 /* Create duplicate HA link list */
119 /* Used in chan_sip2 templates */
120 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
122 struct ast_ha *start = original;
123 struct ast_ha *ret = NULL;
124 struct ast_ha *link, *prev = NULL;
127 link = ast_duplicate_ha(start); /* Create copy of this object */
129 prev->next = link; /* Link previous to this object */
132 ret = link; /* Save starting point */
134 start = start->next; /* Go to next object */
135 prev = link; /* Save pointer to this object */
137 return ret; /* Return start of list */
140 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
143 char *nm = "255.255.255.255";
145 struct ast_ha *prev = NULL;
155 if ((ha = ast_malloc(sizeof(*ha)))) {
156 ast_copy_string(tmp, stuff, sizeof(tmp));
157 nm = strchr(tmp, '/');
159 nm = "255.255.255.255";
164 if (!strchr(nm, '.')) {
165 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32)) {
167 for (z = 0; z < x; z++) {
171 ha->netmask.s_addr = htonl(y);
173 } else if (!inet_aton(nm, &ha->netmask)) {
174 ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
178 if (!inet_aton(tmp, &ha->netaddr)) {
179 ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
183 ha->netaddr.s_addr &= ha->netmask.s_addr;
184 if (!strncasecmp(sense, "p", 1)) {
185 ha->sense = AST_SENSE_ALLOW;
187 ha->sense = AST_SENSE_DENY;
196 ast_log(LOG_DEBUG, "%s/%s appended to acl for peer\n", stuff, nm);
200 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
202 /* Start optimistic */
203 int res = AST_SENSE_ALLOW;
205 char iabuf[INET_ADDRSTRLEN];
206 char iabuf2[INET_ADDRSTRLEN];
208 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
209 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
210 ast_log(LOG_DEBUG, "##### Testing %s with %s\n", iabuf, iabuf2);
211 /* For each rule, if this address and the netmask = the net address
212 apply the current rule */
213 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
220 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
223 struct ast_hostent ahp;
226 int tportno = ntohs(sin->sin_port);
227 if (inet_aton(value, &sin->sin_addr))
230 snprintf(srv, sizeof(srv), "%s.%s", service, value);
231 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
232 sin->sin_port = htons(tportno);
236 hp = ast_gethostbyname(value, &ahp);
238 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
240 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
246 struct dscp_codepoint {
251 /* IANA registered DSCP codepoints */
253 static const struct dscp_codepoint dscp_pool1[] = {
277 int ast_str2tos(const char *value, unsigned int *tos)
282 if (sscanf(value, "%i", &fval) == 1) {
287 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
288 if (!strcasecmp(value, dscp_pool1[x].name)) {
289 *tos = dscp_pool1[x].space << 2;
297 const char *ast_tos2str(unsigned int tos)
301 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
302 if (dscp_pool1[x].space == (tos >> 2))
303 return dscp_pool1[x].name;
309 int ast_get_ip(struct sockaddr_in *sin, const char *value)
311 return ast_get_ip_or_srv(sin, value, NULL);
314 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
317 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))