2 * Copyright (C) 2005-2013 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. */
19 * Describes one entry in the blacklist/whitelist of a paraslash sender.
22 /** The address to be black/whitelisted. */
24 /** The netmask for this entry. */
26 /** The position of this entry in the acl. */
27 struct list_head node;
31 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
33 static int v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
38 mask <<= (32 - netmask);
39 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
43 * Find out whether the peer name of a given fd belongs to an acl.
45 * \param fd File descriptor.
46 * \param acl The access control list.
48 * \return One if \a fd belongs to \a acl, zero otherwise.
50 static int acl_lookup(int fd, struct list_head *acl)
52 struct access_info *ai, *tmp;
53 struct sockaddr_storage ss;
54 socklen_t sslen = sizeof(ss);
55 struct in_addr v4_addr;
57 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
58 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
61 extract_v4_addr(&ss, &v4_addr);
65 list_for_each_entry_safe(ai, tmp, acl, node)
66 if (v4_addr_match(v4_addr.s_addr, ai->addr.s_addr, ai->netmask))
73 * Add an entry to an access control list.
75 * \param acl The access control list.
76 * \param addr The address to add.
77 * \param netmask The netmask to use for this entry.
79 static void acl_add_entry(struct list_head *acl, char *addr, int netmask)
81 struct access_info *ai = para_malloc(sizeof(struct access_info));
83 inet_pton(AF_INET, addr, &ai->addr);
84 ai->netmask = netmask;
85 PARA_INFO_LOG("adding %s/%i to access list\n", addr, ai->netmask);
86 para_list_add(&ai->node, acl);
90 * Delete an entry from an access control list.
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.
96 static void acl_del_entry(struct list_head *acl, char *addr, unsigned netmask)
98 struct access_info *ai, *tmp;
99 struct in_addr to_delete;
101 inet_pton(AF_INET, addr, &to_delete);
103 list_for_each_entry_safe(ai, tmp, acl, node) {
105 if (v4_addr_match(to_delete.s_addr, ai->addr.s_addr,
106 PARA_MIN(netmask, ai->netmask))) {
107 PARA_NOTICE_LOG("removing %s/%i from access list\n",
116 * Compute a string containing the contents of an acl.
118 * \param acl The access control list.
120 * \return A string containing the contents of \a acl, or \p NULL
121 * if \a acl is empty.
123 char *acl_get_contents(struct list_head *acl)
125 struct access_info *ai, *tmp_ai;
128 list_for_each_entry_safe(ai, tmp_ai, acl, node) {
129 char *tmp = make_message("%s%s/%d ", ret? ret : "",
130 inet_ntoa(ai->addr), ai->netmask);
138 * Initialize an access control list.
140 * \param acl The list to initialize.
141 * \param acl_info An array of strings of the form ip/netmask.
142 * \param num The number of strings in \a acl_info.
144 void acl_init(struct list_head *acl, char * const *acl_info, int num)
150 for (i = 0; i < num; i++)
151 if (parse_cidr(acl_info[i], addr, sizeof(addr), &mask) == NULL)
152 PARA_CRIT_LOG("ACL syntax error: %s, ignoring\n",
155 acl_add_entry(acl, addr, mask);
159 * Check whether the peer name of a given fd is allowed by an acl.
161 * \param fd File descriptor.
162 * \param acl The access control list.
163 * \param default_deny Whether \a acl is a whitelist.
165 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
168 int acl_check_access(int fd, struct list_head *acl, int default_deny)
170 int match = acl_lookup(fd, acl);
172 return (!match || default_deny) && (match || !default_deny)?
177 * Permit access for a range of IP addresses.
179 * \param addr The address to permit.
180 * \param netmask The netmask of the entry to be permitted.
181 * \param acl The access control list.
182 * \param default_deny Whether \a acl is a whitelist.
184 void acl_allow(char *addr, int netmask,
185 struct list_head *acl, int default_deny)
188 acl_add_entry(acl, addr, netmask);
190 acl_del_entry(acl, addr, netmask);
194 * Deny access for a range of IP addresses.
196 * \param addr The address to deny.
197 * \param netmask The netmask of the entry to be denied.
198 * \param acl The access control list.
199 * \param default_deny Whether \a acl is a whitelist.
201 void acl_deny(char *addr, int netmask,
202 struct list_head *acl, int default_deny)
204 acl_allow(addr, netmask, acl, !default_deny);