mood: Speed up int_sqrt().
[paraslash.git] / acl.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file acl.c Access control lists for paraslash senders. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <arpa/inet.h>
13 #include <sys/un.h>
14 #include <netdb.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "string.h"
19 #include "list.h"
20 #include "net.h"
21 #include "acl.h"
22
23 /**
24 * Describes one entry in the blacklist/whitelist of a paraslash sender.
25 */
26 struct access_info {
27 /** The address to be black/whitelisted. */
28 struct in_addr addr;
29 /** The netmask for this entry. */
30 unsigned netmask;
31 /** The position of this entry in the acl. */
32 struct list_head node;
33 };
34
35 /**
36 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
37 */
38 static int v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
39 {
40 uint32_t mask = ~0U;
41
42 if (netmask < 32)
43 mask <<= (32 - netmask);
44 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
45 }
46
47 /**
48 * Find out whether the peer name of a given fd belongs to an acl.
49 *
50 * \param fd File descriptor.
51 * \param acl The access control list.
52 *
53 * \return One if \a fd belongs to \a acl, zero otherwise.
54 */
55 static int acl_lookup(int fd, struct list_head *acl)
56 {
57 struct access_info *ai, *tmp;
58 struct sockaddr_storage ss;
59 socklen_t sslen = sizeof(ss);
60 struct in_addr v4_addr;
61
62 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
63 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
64 goto no_match;
65 }
66 extract_v4_addr(&ss, &v4_addr);
67 if (!v4_addr.s_addr)
68 goto no_match;
69
70 list_for_each_entry_safe(ai, tmp, acl, node)
71 if (v4_addr_match(v4_addr.s_addr, ai->addr.s_addr, ai->netmask))
72 return 1;
73 no_match:
74 return 0;
75 }
76
77 /**
78 * Add an entry to an access control list.
79 *
80 * \param acl The access control list.
81 * \param addr The address to add.
82 * \param netmask The netmask to use for this entry.
83 */
84 void acl_add_entry(struct list_head *acl, char *addr, int netmask)
85 {
86 struct access_info *ai = para_malloc(sizeof(struct access_info));
87
88 inet_pton(AF_INET, addr, &ai->addr);
89 ai->netmask = netmask;
90 PARA_INFO_LOG("adding %s/%u to access list\n", addr, ai->netmask);
91 para_list_add(&ai->node, acl);
92 }
93
94 /**
95 * Delete an entry from an access control list.
96 *
97 * \param acl The access control list.
98 * \param addr The address to delete.
99 * \param netmask The netmask of the entry to be removed from the list.
100 */
101 static void acl_del_entry(struct list_head *acl, char *addr, unsigned netmask)
102 {
103 struct access_info *ai, *tmp;
104 struct in_addr to_delete;
105
106 inet_pton(AF_INET, addr, &to_delete);
107
108 list_for_each_entry_safe(ai, tmp, acl, node) {
109
110 if (v4_addr_match(to_delete.s_addr, ai->addr.s_addr,
111 PARA_MIN(netmask, ai->netmask))) {
112 PARA_NOTICE_LOG("removing %s/%u from access list\n",
113 addr, ai->netmask);
114 list_del(&ai->node);
115 free(ai);
116 }
117 }
118 }
119
120 /**
121 * Compute a string containing the contents of an acl.
122 *
123 * \param acl The access control list.
124 *
125 * \return A string containing the contents of \a acl, or \p NULL
126 * if \a acl is empty.
127 */
128 char *acl_get_contents(struct list_head *acl)
129 {
130 struct access_info *ai, *tmp_ai;
131 char *ret = NULL;
132
133 list_for_each_entry_safe(ai, tmp_ai, acl, node) {
134 char *tmp = make_message("%s%s/%u ", ret? ret : "",
135 inet_ntoa(ai->addr), ai->netmask);
136 free(ret);
137 ret = tmp;
138 }
139 return ret;
140 }
141
142 /**
143 * Check whether the peer name of a given fd is allowed by an acl.
144 *
145 * \param fd File descriptor.
146 * \param acl The access control list.
147 * \param default_deny Whether \a acl is a whitelist.
148 *
149 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
150 * otherwise.
151 */
152 int acl_check_access(int fd, struct list_head *acl, int default_deny)
153 {
154 int match = acl_lookup(fd, acl);
155
156 return (!match || default_deny) && (match || !default_deny)?
157 1 : -E_ACL_PERM;
158 }
159
160 /**
161 * Permit access for a range of IP addresses.
162 *
163 * \param addr The address to permit.
164 * \param netmask The netmask of the entry to be permitted.
165 * \param acl The access control list.
166 * \param default_deny Whether \a acl is a whitelist.
167 */
168 void acl_allow(char *addr, int netmask,
169 struct list_head *acl, int default_deny)
170 {
171 if (default_deny)
172 acl_add_entry(acl, addr, netmask);
173 else
174 acl_del_entry(acl, addr, netmask);
175 }
176
177 /**
178 * Deny access for a range of IP addresses.
179 *
180 * \param addr The address to deny.
181 * \param netmask The netmask of the entry to be denied.
182 * \param acl The access control list.
183 * \param default_deny Whether \a acl is a whitelist.
184 */
185 void acl_deny(char *addr, int netmask,
186 struct list_head *acl, int default_deny)
187 {
188 acl_allow(addr, netmask, acl, !default_deny);
189 }