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, int *error)
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);
180 if (!inet_aton(tmp, &ha->netaddr)) {
181 ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
187 ha->netaddr.s_addr &= ha->netmask.s_addr;
188 if (!strncasecmp(sense, "p", 1)) {
189 ha->sense = AST_SENSE_ALLOW;
191 ha->sense = AST_SENSE_DENY;
200 ast_debug(1, "%s/%s appended to acl for peer\n", stuff, nm);
204 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
206 /* Start optimistic */
207 int res = AST_SENSE_ALLOW;
209 #if 0 /* debugging code */
210 char iabuf[INET_ADDRSTRLEN];
211 char iabuf2[INET_ADDRSTRLEN];
213 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
214 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
215 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
217 /* For each rule, if this address and the netmask = the net address
218 apply the current rule */
219 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
226 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
229 struct ast_hostent ahp;
232 int tportno = ntohs(sin->sin_port);
234 snprintf(srv, sizeof(srv), "%s.%s", service, value);
235 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
236 sin->sin_port = htons(tportno);
240 hp = ast_gethostbyname(value, &ahp);
242 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
244 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
250 struct dscp_codepoint {
255 /* IANA registered DSCP codepoints */
257 static const struct dscp_codepoint dscp_pool1[] = {
281 int ast_str2cos(const char *value, unsigned int *cos)
285 if (sscanf(value, "%d", &fval) == 1) {
295 int ast_str2tos(const char *value, unsigned int *tos)
300 if (sscanf(value, "%i", &fval) == 1) {
305 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
306 if (!strcasecmp(value, dscp_pool1[x].name)) {
307 *tos = dscp_pool1[x].space << 2;
315 const char *ast_tos2str(unsigned int tos)
319 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
320 if (dscp_pool1[x].space == (tos >> 2))
321 return dscp_pool1[x].name;
327 int ast_get_ip(struct sockaddr_in *sin, const char *value)
329 return ast_get_ip_or_srv(sin, value, NULL);
332 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
335 struct sockaddr_in sin;
337 s = socket(PF_INET, SOCK_DGRAM, 0);
339 ast_log(LOG_WARNING, "Cannot create socket\n");
342 sin.sin_family = AF_INET;
344 sin.sin_addr = *them;
345 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
346 ast_log(LOG_WARNING, "Cannot connect\n");
351 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
352 ast_log(LOG_WARNING, "Cannot get socket name\n");
361 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
363 char ourhost[MAXHOSTNAMELEN] = "";
364 struct ast_hostent ahp;
366 struct in_addr saddr;
368 /* just use the bind address if it is nonzero */
369 if (ntohl(bindaddr.sin_addr.s_addr)) {
370 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
373 /* try to use our hostname */
374 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
375 ast_log(LOG_WARNING, "Unable to get hostname\n");
377 hp = ast_gethostbyname(ourhost, &ahp);
379 memcpy(ourip, hp->h_addr, sizeof(*ourip));
383 /* A.ROOT-SERVERS.NET. */
384 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))