2 * Asterisk -- A telephony toolkit for Linux.
4 * Various sorts of access control
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
22 #include <asterisk/acl.h>
23 #include <asterisk/logger.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
28 #define AST_SENSE_DENY 0
29 #define AST_SENSE_ALLOW 1
32 /* Host access rule */
33 struct in_addr netaddr;
34 struct in_addr netmask;
39 void ast_free_ha(struct ast_ha *ha)
49 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
51 struct ast_ha *ha = malloc(sizeof(struct ast_ha));
53 struct ast_ha *prev = NULL;
62 nm = strtok(NULL, "/");
64 nm = "255.255.255.255";
65 if (!inet_aton(stuff, &ha->netaddr)) {
66 ast_log(LOG_WARNING, "%s not a valid IP\n", stuff);
70 if (!inet_aton(nm, &ha->netmask)) {
71 ast_log(LOG_WARNING, "%s not a valid netmask\n", nm);
75 ha->netaddr.s_addr &= ha->netmask.s_addr;
76 if (!strncasecmp(sense, "p", 1)) {
77 ha->sense = AST_SENSE_ALLOW;
79 ha->sense = AST_SENSE_DENY;
90 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
92 /* Start optimistic */
93 int res = AST_SENSE_ALLOW;
95 /* For each rule, if this address and the netmask = the net address
96 apply the current rule */
97 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == (ha->netaddr.s_addr))
104 int ast_get_ip(struct sockaddr_in *sin, char *value)
107 hp = gethostbyname(value);
109 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
111 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);