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