Merge commit 'remotes/meins/master'
[paraslash.git] / acl.c
1 /*
2 * Copyright (C) 2005-2008 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 "para.h"
10 #include "error.h"
11 #include "string.h"
12 #include "list.h"
13 #include "net.h"
14
15 /**
16 * Describes one entry in the blacklist/whitelist of a paraslash sender.
17 */
18 struct access_info {
19 /** The address to be black/whitelisted. */
20 struct in_addr addr;
21 /** The netmask for this entry. */
22 unsigned netmask;
23 /** The position of this entry in the acl. */
24 struct list_head node;
25 };
26
27
28 /**
29 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
30 */
31 static int v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
32 {
33 uint32_t mask = ~0U;
34
35 if (netmask < 32)
36 mask <<= (32 - netmask);
37 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
38 }
39
40 /**
41 * Find out whether the peer name of a given fd belongs to an acl.
42 *
43 * \param fd File descriptor.
44 * \param acl The access control list.
45 *
46 * \return One if \a fd belongs to \a acl, zero otherwise.
47 */
48 int acl_lookup(int fd, struct list_head *acl)
49 {
50 struct access_info *ai, *tmp;
51 struct sockaddr_storage ss;
52 socklen_t sslen = sizeof(ss);
53 struct in_addr v4_addr;
54
55 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
56 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
57 goto no_match;
58 }
59 v4_addr = extract_v4_addr(&ss);
60 if (!v4_addr.s_addr)
61 goto no_match;
62
63 list_for_each_entry_safe(ai, tmp, acl, node)
64 if (v4_addr_match(v4_addr.s_addr, ai->addr.s_addr, ai->netmask))
65 return 1;
66 no_match:
67 return 0;
68 }
69
70 /**
71 * Add an entry to an access control list.
72 *
73 * \param acl The access control list.
74 * \param addr The address to add.
75 * \param netmask The netmask to use for this entry.
76 */
77 void acl_add_entry(struct list_head *acl, struct in_addr addr,
78 int netmask)
79 {
80 struct access_info *ai = para_malloc(sizeof(struct access_info));
81 ai->addr = addr;
82 ai->netmask = netmask;
83 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
84 ai->netmask);
85 para_list_add(&ai->node, acl);
86 }
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 void acl_del_entry(struct list_head *acl, struct in_addr addr,
97 int netmask)
98 {
99 struct access_info *ai, *tmp;
100
101 list_for_each_entry_safe(ai, tmp, acl, node) {
102 char *nad = para_strdup(inet_ntoa(ai->addr));
103 if (!strcmp(nad, inet_ntoa(addr)) &&
104 ai->netmask == netmask) {
105 PARA_NOTICE_LOG("removing %s/%i from access list\n",
106 nad, ai->netmask);
107 list_del(&ai->node);
108 free(ai);
109 }
110 free(nad);
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 int i;
146
147 INIT_LIST_HEAD(acl);
148 for (i = 0; i < num; i++) {
149 char *arg = para_strdup(acl_info[i]);
150 char *p = strchr(arg, '/');
151 struct in_addr addr;
152 int netmask;
153
154 if (!p)
155 goto err;
156 *p = '\0';
157 if (!inet_pton(AF_INET, arg, &addr))
158 goto err;
159 netmask = atoi(++p);
160 if (netmask < 0 || netmask > 32)
161 goto err;
162 acl_add_entry(acl, addr, netmask);
163 goto success;
164 err:
165 PARA_CRIT_LOG("syntax error: %s\n", acl_info[i]);
166 success:
167 free(arg);
168 continue;
169 }
170 }
171