Merge branch 't/alsa_improvements'
[paraslash.git] / acl.c
1 /*
2 * Copyright (C) 2005-2014 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 #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 static 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/%i 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/%i 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/%d ", ret? ret : "",
135 inet_ntoa(ai->addr), ai->netmask);
136 free(ret);
137 ret = tmp;
138 }
139 return ret;
140 }
141
142 /**
143 * Initialize an access control list.
144 *
145 * \param acl The list to initialize.
146 * \param acl_info An array of strings of the form ip/netmask.
147 * \param num The number of strings in \a acl_info.
148 */
149 void acl_init(struct list_head *acl, char * const *acl_info, int num)
150 {
151 char addr[16];
152 int mask, i;
153
154 INIT_LIST_HEAD(acl);
155 for (i = 0; i < num; i++)
156 if (parse_cidr(acl_info[i], addr, sizeof(addr), &mask) == NULL)
157 PARA_CRIT_LOG("ACL syntax error: %s, ignoring\n",
158 acl_info[i]);
159 else
160 acl_add_entry(acl, addr, mask);
161 }
162
163 /**
164 * Check whether the peer name of a given fd is allowed by an acl.
165 *
166 * \param fd File descriptor.
167 * \param acl The access control list.
168 * \param default_deny Whether \a acl is a whitelist.
169 *
170 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
171 * otherwise.
172 */
173 int acl_check_access(int fd, struct list_head *acl, int default_deny)
174 {
175 int match = acl_lookup(fd, acl);
176
177 return (!match || default_deny) && (match || !default_deny)?
178 1 : -E_ACL_PERM;
179 }
180
181 /**
182 * Permit access for a range of IP addresses.
183 *
184 * \param addr The address to permit.
185 * \param netmask The netmask of the entry to be permitted.
186 * \param acl The access control list.
187 * \param default_deny Whether \a acl is a whitelist.
188 */
189 void acl_allow(char *addr, int netmask,
190 struct list_head *acl, int default_deny)
191 {
192 if (default_deny)
193 acl_add_entry(acl, addr, netmask);
194 else
195 acl_del_entry(acl, addr, netmask);
196 }
197
198 /**
199 * Deny access for a range of IP addresses.
200 *
201 * \param addr The address to deny.
202 * \param netmask The netmask of the entry to be denied.
203 * \param acl The access control list.
204 * \param default_deny Whether \a acl is a whitelist.
205 */
206 void acl_deny(char *addr, int netmask,
207 struct list_head *acl, int default_deny)
208 {
209 acl_allow(addr, netmask, acl, !default_deny);
210 }