acl: Avoid undefined behaviour due to 32-bit shift.
[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 inet_pton(AF_INET, addr, &to_delete);
105
106 list_for_each_entry_safe(ai, tmp, acl, node) {
107
108 if (v4_addr_match(to_delete.s_addr, ai->addr.s_addr,
109 PARA_MIN(netmask, ai->netmask))) {
110 PARA_NOTICE_LOG("removing %s/%u from access list\n",
111 addr, ai->netmask);
112 list_del(&ai->node);
113 free(ai);
114 }
115 }
116 }
117
118 /**
119 * Compute a string containing the contents of an acl.
120 *
121 * \param acl The access control list.
122 *
123 * \return A string containing the contents of \a acl, or \p NULL
124 * if \a acl is empty.
125 */
126 char *acl_get_contents(struct list_head *acl)
127 {
128 struct access_info *ai, *tmp_ai;
129 char *ret = NULL;
130
131 list_for_each_entry_safe(ai, tmp_ai, acl, node) {
132 char *tmp = make_message("%s%s/%u ", ret? ret : "",
133 inet_ntoa(ai->addr), ai->netmask);
134 free(ret);
135 ret = tmp;
136 }
137 return ret;
138 }
139
140 /**
141 * Check whether the peer name of a given fd is allowed by an acl.
142 *
143 * \param fd File descriptor.
144 * \param acl The access control list.
145 * \param default_deny Whether \a acl is a whitelist.
146 *
147 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
148 * otherwise.
149 */
150 int acl_check_access(int fd, struct list_head *acl, int default_deny)
151 {
152 int match = acl_lookup(fd, acl);
153
154 return (!match || default_deny) && (match || !default_deny)?
155 1 : -E_ACL_PERM;
156 }
157
158 /**
159 * Permit access for a range of IP addresses.
160 *
161 * \param addr The address to permit.
162 * \param netmask The netmask of the entry to be permitted.
163 * \param acl The access control list.
164 * \param default_deny Whether \a acl is a whitelist.
165 */
166 void acl_allow(char *addr, int netmask,
167 struct list_head *acl, int default_deny)
168 {
169 if (default_deny)
170 acl_add_entry(acl, addr, netmask);
171 else
172 acl_del_entry(acl, addr, netmask);
173 }
174
175 /**
176 * Deny access for a range of IP addresses.
177 *
178 * \param addr The address to deny.
179 * \param netmask The netmask of the entry to be denied.
180 * \param acl The access control list.
181 * \param default_deny Whether \a acl is a whitelist.
182 */
183 void acl_deny(char *addr, int netmask,
184 struct list_head *acl, int default_deny)
185 {
186 acl_allow(addr, netmask, acl, !default_deny);
187 }