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. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
12 #include <arpa/inet.h>
23 * Describes one entry in the blacklist/whitelist of a paraslash sender.
26 /** The address to be black/whitelisted. */
28 /** The netmask for this entry. */
30 /** The position of this entry in the acl. */
31 struct list_head node
;
35 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
37 static int v4_addr_match(uint32_t addr_1
, uint32_t addr_2
, uint8_t netmask
)
42 mask
<<= (32 - netmask
);
43 return (htonl(addr_1
) & mask
) == (htonl(addr_2
) & mask
);
47 * Find out whether the peer name of a given fd belongs to an acl.
49 * \param fd File descriptor.
50 * \param acl The access control list.
52 * \return One if \a fd belongs to \a acl, zero otherwise.
54 static int acl_lookup(int fd
, struct list_head
*acl
)
56 struct access_info
*ai
, *tmp
;
57 struct sockaddr_storage ss
;
58 socklen_t sslen
= sizeof(ss
);
59 struct in_addr v4_addr
;
61 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
62 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno
));
65 extract_v4_addr(&ss
, &v4_addr
);
69 list_for_each_entry_safe(ai
, tmp
, acl
, node
)
70 if (v4_addr_match(v4_addr
.s_addr
, ai
->addr
.s_addr
, ai
->netmask
))
77 * Add an entry to an access control list.
79 * \param acl The access control list.
80 * \param addr The address to add.
81 * \param netmask The netmask to use for this entry.
83 static void acl_add_entry(struct list_head
*acl
, char *addr
, int netmask
)
85 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
87 inet_pton(AF_INET
, addr
, &ai
->addr
);
88 ai
->netmask
= netmask
;
89 PARA_INFO_LOG("adding %s/%i to access list\n", addr
, ai
->netmask
);
90 para_list_add(&ai
->node
, acl
);
94 * Delete an entry from an access control list.
96 * \param acl The access control list.
97 * \param addr The address to delete.
98 * \param netmask The netmask of the entry to be removed from the list.
100 static void acl_del_entry(struct list_head
*acl
, char *addr
, unsigned netmask
)
102 struct access_info
*ai
, *tmp
;
103 struct in_addr to_delete
;
105 inet_pton(AF_INET
, addr
, &to_delete
);
107 list_for_each_entry_safe(ai
, tmp
, acl
, node
) {
109 if (v4_addr_match(to_delete
.s_addr
, ai
->addr
.s_addr
,
110 PARA_MIN(netmask
, ai
->netmask
))) {
111 PARA_NOTICE_LOG("removing %s/%i from access list\n",
120 * Compute a string containing the contents of an acl.
122 * \param acl The access control list.
124 * \return A string containing the contents of \a acl, or \p NULL
125 * if \a acl is empty.
127 char *acl_get_contents(struct list_head
*acl
)
129 struct access_info
*ai
, *tmp_ai
;
132 list_for_each_entry_safe(ai
, tmp_ai
, acl
, node
) {
133 char *tmp
= make_message("%s%s/%d ", ret
? ret
: "",
134 inet_ntoa(ai
->addr
), ai
->netmask
);
142 * Initialize an access control list.
144 * \param acl The list to initialize.
145 * \param acl_info An array of strings of the form ip/netmask.
146 * \param num The number of strings in \a acl_info.
148 void acl_init(struct list_head
*acl
, char * const *acl_info
, int num
)
154 for (i
= 0; i
< num
; i
++)
155 if (parse_cidr(acl_info
[i
], addr
, sizeof(addr
), &mask
) == NULL
)
156 PARA_CRIT_LOG("ACL syntax error: %s, ignoring\n",
159 acl_add_entry(acl
, addr
, mask
);
163 * Check whether the peer name of a given fd is allowed by an acl.
165 * \param fd File descriptor.
166 * \param acl The access control list.
167 * \param default_deny Whether \a acl is a whitelist.
169 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
172 int acl_check_access(int fd
, struct list_head
*acl
, int default_deny
)
174 int match
= acl_lookup(fd
, acl
);
176 return (!match
|| default_deny
) && (match
|| !default_deny
)?
181 * Permit access for a range of IP addresses.
183 * \param addr The address to permit.
184 * \param netmask The netmask of the entry to be permitted.
185 * \param acl The access control list.
186 * \param default_deny Whether \a acl is a whitelist.
188 void acl_allow(char *addr
, int netmask
,
189 struct list_head
*acl
, int default_deny
)
192 acl_add_entry(acl
, addr
, netmask
);
194 acl_del_entry(acl
, addr
, netmask
);
198 * Deny access for a range of IP addresses.
200 * \param addr The address to deny.
201 * \param netmask The netmask of the entry to be denied.
202 * \param acl The access control list.
203 * \param default_deny Whether \a acl is a whitelist.
205 void acl_deny(char *addr
, int netmask
,
206 struct list_head
*acl
, int default_deny
)
208 acl_allow(addr
, netmask
, acl
, !default_deny
);