Merge branch 't/aes'
[paraslash.git] / acl.c
1 /*
2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
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
22 /**
23 * Describes one entry in the blacklist/whitelist of a paraslash sender.
24 */
25 struct access_info {
26 /** The address to be black/whitelisted. */
27 struct in_addr addr;
28 /** The netmask for this entry. */
29 unsigned netmask;
30 /** The position of this entry in the acl. */
31 struct list_head node;
32 };
33
34 /**
35 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
36 */
37 static int v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
38 {
39 uint32_t mask = ~0U;
40
41 if (netmask < 32)
42 mask <<= (32 - netmask);
43 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
44 }
45
46 /**
47 * Find out whether the peer name of a given fd belongs to an acl.
48 *
49 * \param fd File descriptor.
50 * \param acl The access control list.
51 *
52 * \return One if \a fd belongs to \a acl, zero otherwise.
53 */
54 static int acl_lookup(int fd, struct list_head *acl)
55 {
56 struct access_info *ai, *tmp;
57 struct sockaddr_storage ss;
58 socklen_t sslen = sizeof(ss);
59 struct in_addr v4_addr;
60
61 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
62 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
63 goto no_match;
64 }
65 extract_v4_addr(&ss, &v4_addr);
66 if (!v4_addr.s_addr)
67 goto no_match;
68
69 list_for_each_entry_safe(ai, tmp, acl, node)
70 if (v4_addr_match(v4_addr.s_addr, ai->addr.s_addr, ai->netmask))
71 return 1;
72 no_match:
73 return 0;
74 }
75
76 /**
77 * Add an entry to an access control list.
78 *
79 * \param acl The access control list.
80 * \param addr The address to add.
81 * \param netmask The netmask to use for this entry.
82 */
83 static void acl_add_entry(struct list_head *acl, char *addr, int netmask)
84 {
85 struct access_info *ai = para_malloc(sizeof(struct access_info));
86
87 inet_pton(AF_INET, addr, &ai->addr);
88 ai->netmask = netmask;
89 PARA_INFO_LOG("adding %s/%i to access list\n", addr, ai->netmask);
90 para_list_add(&ai->node, acl);
91 }
92
93 /**
94 * Delete an entry from an access control list.
95 *
96 * \param acl The access control list.
97 * \param addr The address to delete.
98 * \param netmask The netmask of the entry to be removed from the list.
99 */
100 static void acl_del_entry(struct list_head *acl, char *addr, unsigned netmask)
101 {
102 struct access_info *ai, *tmp;
103 struct in_addr to_delete;
104
105 inet_pton(AF_INET, addr, &to_delete);
106
107 list_for_each_entry_safe(ai, tmp, acl, node) {
108
109 if (v4_addr_match(to_delete.s_addr, ai->addr.s_addr,
110 PARA_MIN(netmask, ai->netmask))) {
111 PARA_NOTICE_LOG("removing %s/%i from access list\n",
112 addr, ai->netmask);
113 list_del(&ai->node);
114 free(ai);
115 }
116 }
117 }
118
119 /**
120 * Compute a string containing the contents of an acl.
121 *
122 * \param acl The access control list.
123 *
124 * \return A string containing the contents of \a acl, or \p NULL
125 * if \a acl is empty.
126 */
127 char *acl_get_contents(struct list_head *acl)
128 {
129 struct access_info *ai, *tmp_ai;
130 char *ret = NULL;
131
132 list_for_each_entry_safe(ai, tmp_ai, acl, node) {
133 char *tmp = make_message("%s%s/%d ", ret? ret : "",
134 inet_ntoa(ai->addr), ai->netmask);
135 free(ret);
136 ret = tmp;
137 }
138 return ret;
139 }
140
141 /**
142 * Initialize an access control list.
143 *
144 * \param acl The list to initialize.
145 * \param acl_info An array of strings of the form ip/netmask.
146 * \param num The number of strings in \a acl_info.
147 */
148 void acl_init(struct list_head *acl, char * const *acl_info, int num)
149 {
150 char addr[16];
151 int mask, i;
152
153 INIT_LIST_HEAD(acl);
154 for (i = 0; i < num; i++)
155 if (parse_cidr(acl_info[i], addr, sizeof(addr), &mask) == NULL)
156 PARA_CRIT_LOG("ACL syntax error: %s, ignoring\n",
157 acl_info[i]);
158 else
159 acl_add_entry(acl, addr, mask);
160 }
161
162 /**
163 * Check whether the peer name of a given fd is allowed by an acl.
164 *
165 * \param fd File descriptor.
166 * \param acl The access control list.
167 * \param default_deny Whether \a acl is a whitelist.
168 *
169 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
170 * otherwise.
171 */
172 int acl_check_access(int fd, struct list_head *acl, int default_deny)
173 {
174 int match = acl_lookup(fd, acl);
175
176 return (!match || default_deny) && (match || !default_deny)?
177 1 : -E_ACL_PERM;
178 }
179
180 /**
181 * Permit access for a range of IP addresses.
182 *
183 * \param addr The address to permit.
184 * \param netmask The netmask of the entry to be permitted.
185 * \param acl The access control list.
186 * \param default_deny Whether \a acl is a whitelist.
187 */
188 void acl_allow(char *addr, int netmask,
189 struct list_head *acl, int default_deny)
190 {
191 if (default_deny)
192 acl_add_entry(acl, addr, netmask);
193 else
194 acl_del_entry(acl, addr, netmask);
195 }
196
197 /**
198 * Deny access for a range of IP addresses.
199 *
200 * \param addr The address to deny.
201 * \param netmask The netmask of the entry to be denied.
202 * \param acl The access control list.
203 * \param default_deny Whether \a acl is a whitelist.
204 */
205 void acl_deny(char *addr, int netmask,
206 struct list_head *acl, int default_deny)
207 {
208 acl_allow(addr, netmask, acl, !default_deny);
209 }