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