server: Update status items on file renames.
[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 int v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
35 {
36 uint32_t mask = ~0U;
37
38 if (netmask < 32)
39 mask <<= (32 - netmask);
40 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
41 }
42
43 /**
44 * Find out whether the peer name of a given fd belongs to an acl.
45 *
46 * \param fd File descriptor.
47 * \param acl The access control list.
48 *
49 * \return One if \a fd belongs to \a acl, zero otherwise.
50 */
51 static int acl_lookup(int fd, struct list_head *acl)
52 {
53 struct access_info *ai, *tmp;
54 struct sockaddr_storage ss;
55 socklen_t sslen = sizeof(ss);
56 struct in_addr v4_addr;
57
58 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
59 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
60 goto no_match;
61 }
62 extract_v4_addr(&ss, &v4_addr);
63 if (!v4_addr.s_addr)
64 goto no_match;
65
66 list_for_each_entry_safe(ai, tmp, acl, node)
67 if (v4_addr_match(v4_addr.s_addr, ai->addr.s_addr, ai->netmask))
68 return 1;
69 no_match:
70 return 0;
71 }
72
73 /**
74 * Add an entry to an access control list.
75 *
76 * \param acl The access control list.
77 * \param addr The address to add.
78 * \param netmask The netmask to use for this entry.
79 */
80 void acl_add_entry(struct list_head *acl, char *addr, int netmask)
81 {
82 struct access_info *ai = para_malloc(sizeof(struct access_info));
83
84 inet_pton(AF_INET, addr, &ai->addr);
85 ai->netmask = netmask;
86 PARA_INFO_LOG("adding %s/%u to access list\n", addr, ai->netmask);
87 para_list_add(&ai->node, acl);
88 }
89
90 /**
91 * Delete an entry from an access control list.
92 *
93 * \param acl The access control list.
94 * \param addr The address to delete.
95 * \param netmask The netmask of the entry to be removed from the list.
96 */
97 static void acl_del_entry(struct list_head *acl, char *addr, unsigned netmask)
98 {
99 struct access_info *ai, *tmp;
100 struct in_addr to_delete;
101
102 inet_pton(AF_INET, addr, &to_delete);
103
104 list_for_each_entry_safe(ai, tmp, acl, node) {
105
106 if (v4_addr_match(to_delete.s_addr, ai->addr.s_addr,
107 PARA_MIN(netmask, ai->netmask))) {
108 PARA_NOTICE_LOG("removing %s/%u from access list\n",
109 addr, ai->netmask);
110 list_del(&ai->node);
111 free(ai);
112 }
113 }
114 }
115
116 /**
117 * Compute a string containing the contents of an acl.
118 *
119 * \param acl The access control list.
120 *
121 * \return A string containing the contents of \a acl, or \p NULL
122 * if \a acl is empty.
123 */
124 char *acl_get_contents(struct list_head *acl)
125 {
126 struct access_info *ai, *tmp_ai;
127 char *ret = NULL;
128
129 list_for_each_entry_safe(ai, tmp_ai, acl, node) {
130 char *tmp = make_message("%s%s/%u ", ret? ret : "",
131 inet_ntoa(ai->addr), ai->netmask);
132 free(ret);
133 ret = tmp;
134 }
135 return ret;
136 }
137
138 /**
139 * Check whether the peer name of a given fd is allowed by an acl.
140 *
141 * \param fd File descriptor.
142 * \param acl The access control list.
143 * \param default_deny Whether \a acl is a whitelist.
144 *
145 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
146 * otherwise.
147 */
148 int acl_check_access(int fd, struct list_head *acl, int default_deny)
149 {
150 int match = acl_lookup(fd, acl);
151
152 return (!match || default_deny) && (match || !default_deny)?
153 1 : -E_ACL_PERM;
154 }
155
156 /**
157 * Permit access for a range of IP addresses.
158 *
159 * \param addr The address to permit.
160 * \param netmask The netmask of the entry to be permitted.
161 * \param acl The access control list.
162 * \param default_deny Whether \a acl is a whitelist.
163 */
164 void acl_allow(char *addr, int netmask,
165 struct list_head *acl, int default_deny)
166 {
167 if (default_deny)
168 acl_add_entry(acl, addr, netmask);
169 else
170 acl_del_entry(acl, addr, netmask);
171 }
172
173 /**
174 * Deny access for a range of IP addresses.
175 *
176 * \param addr The address to deny.
177 * \param netmask The netmask of the entry to be denied.
178 * \param acl The access control list.
179 * \param default_deny Whether \a acl is a whitelist.
180 */
181 void acl_deny(char *addr, int netmask,
182 struct list_head *acl, int default_deny)
183 {
184 acl_allow(addr, netmask, acl, !default_deny);
185 }