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