2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file acl.c Access control lists for paraslash senders. */
18 * Describes one entry in the blacklist/whitelist of a paraslash sender.
21 /** The address to be black/whitelisted. */
23 /** The netmask for this entry. */
25 /** The position of this entry in the acl. */
26 struct list_head node;
30 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
32 static int v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
37 mask <<= (32 - netmask);
38 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
42 * Find out whether the peer name of a given fd belongs to an acl.
44 * \param fd File descriptor.
45 * \param acl The access control list.
47 * \return One if \a fd belongs to \a acl, zero otherwise.
49 static int acl_lookup(int fd, struct list_head *acl)
51 struct access_info *ai, *tmp;
52 struct sockaddr_storage ss;
53 socklen_t sslen = sizeof(ss);
54 struct in_addr v4_addr;
56 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
57 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
60 v4_addr = extract_v4_addr(&ss);
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))
72 * Add an entry to an access control list.
74 * \param acl The access control list.
75 * \param addr The address to add.
76 * \param netmask The netmask to use for this entry.
78 static void acl_add_entry(struct list_head *acl, char *addr, int netmask)
80 struct access_info *ai = para_malloc(sizeof(struct access_info));
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);
89 * Delete an entry from an access control list.
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.
95 static void acl_del_entry(struct list_head *acl, char *addr, unsigned netmask)
97 struct access_info *ai, *tmp;
98 struct in_addr to_delete;
100 inet_pton(AF_INET, addr, &to_delete);
102 list_for_each_entry_safe(ai, tmp, acl, node) {
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",
115 * Compute a string containing the contents of an acl.
117 * \param acl The access control list.
119 * \return A string containing the contents of \a acl, or \p NULL
120 * if \a acl is empty.
122 char *acl_get_contents(struct list_head *acl)
124 struct access_info *ai, *tmp_ai;
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);
137 * Initialize an access control list.
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.
143 void acl_init(struct list_head *acl, char * const *acl_info, int num)
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",
154 acl_add_entry(acl, addr, mask);
158 * Check whether the peer name of a given fd is allowed by an acl.
160 * \param fd File descriptor.
161 * \param acl The access control list.
162 * \param default_deny Whether \a acl is a whitelist.
164 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
167 int acl_check_access(int fd, struct list_head *acl, int default_deny)
169 int match = acl_lookup(fd, acl);
171 return (!match || default_deny) && (match || !default_deny)?
176 * Permit access for a range of IP addresses.
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.
183 void acl_allow(char *addr, int netmask,
184 struct list_head *acl, int default_deny)
187 acl_add_entry(acl, addr, netmask);
189 acl_del_entry(acl, addr, netmask);
193 * Deny access for a range of IP addresses.
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.
200 void acl_deny(char *addr, int netmask,
201 struct list_head *acl, int default_deny)
203 acl_allow(addr, netmask, acl, !default_deny);