1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file acl.c Access control lists for paraslash senders. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
20 * Describes one entry in the blacklist/whitelist of a paraslash sender.
23 /** The address to be black/whitelisted. */
25 /** The netmask for this entry. */
27 /** The position of this entry in the acl. */
28 struct list_head node;
32 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
34 static bool v4_addr_match(uint32_t addr_1, uint32_t addr_2, uint8_t netmask)
38 if (netmask == 0) /* avoid 32-bit shift, which is undefined in C. */
41 mask <<= (32 - netmask);
42 return (htonl(addr_1) & mask) == (htonl(addr_2) & mask);
46 * Find out whether the peer name of a given fd belongs to an acl.
48 * \param fd File descriptor.
49 * \param acl The access control list.
51 * \return One if \a fd belongs to \a acl, zero otherwise.
53 static int acl_lookup(int fd, struct list_head *acl)
55 struct access_info *ai, *tmp;
56 struct sockaddr_storage ss;
57 socklen_t sslen = sizeof(ss);
58 struct in_addr v4_addr;
60 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
61 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno));
64 extract_v4_addr(&ss, &v4_addr);
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))
76 * Add an entry to an access control list.
78 * \param acl The access control list.
79 * \param addr The address to add.
80 * \param netmask The netmask to use for this entry.
82 void acl_add_entry(struct list_head *acl, char *addr, int netmask)
84 struct access_info *ai = alloc(sizeof(struct access_info));
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);
93 * Delete an entry from an access control list.
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.
99 static void acl_del_entry(struct list_head *acl, char *addr, unsigned netmask)
101 struct access_info *ai, *tmp;
102 struct in_addr to_delete;
104 PARA_INFO_LOG("removing entries matching %s/%u\n", addr, netmask);
105 inet_pton(AF_INET, addr, &to_delete);
107 list_for_each_entry_safe(ai, tmp, acl, node) {
108 if (v4_addr_match(to_delete.s_addr, ai->addr.s_addr,
109 PARA_MIN(netmask, ai->netmask))) {
110 char dst[INET_ADDRSTRLEN + 1];
111 const char *p = inet_ntop(AF_INET, &ai->addr.s_addr,
114 PARA_DEBUG_LOG("removing %s/%u\n", p,
123 * Compute a string containing the contents of an acl.
125 * \param acl The access control list.
127 * \return A string containing the contents of \a acl, or \p NULL
128 * if \a acl is empty.
130 char *acl_get_contents(struct list_head *acl)
132 struct access_info *ai, *tmp_ai;
135 list_for_each_entry_safe(ai, tmp_ai, acl, node) {
136 char *tmp = make_message("%s%s/%u ", ret? ret : "",
137 inet_ntoa(ai->addr), ai->netmask);
145 * Check whether the peer name of a given fd is allowed by an acl.
147 * \param fd File descriptor.
148 * \param acl The access control list.
149 * \param default_deny Whether \a acl is a whitelist.
151 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
154 int acl_check_access(int fd, struct list_head *acl, int default_deny)
156 int match = acl_lookup(fd, acl);
158 return (!match || default_deny) && (match || !default_deny)?
163 * Permit access for a range of IP addresses.
165 * \param addr The address to permit.
166 * \param netmask The netmask of the entry to be permitted.
167 * \param acl The access control list.
168 * \param default_deny Whether \a acl is a whitelist.
170 void acl_allow(char *addr, int netmask,
171 struct list_head *acl, int default_deny)
174 acl_add_entry(acl, addr, netmask);
176 acl_del_entry(acl, addr, netmask);
180 * Deny access for a range of IP addresses.
182 * \param addr The address to deny.
183 * \param netmask The netmask of the entry to be denied.
184 * \param acl The access control list.
185 * \param default_deny Whether \a acl is a whitelist.
187 void acl_deny(char *addr, int netmask,
188 struct list_head *acl, int default_deny)
190 acl_allow(addr, netmask, acl, !default_deny);