2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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>
24 * Describes one entry in the blacklist/whitelist of a paraslash sender.
27 /** The address to be black/whitelisted. */
29 /** The netmask for this entry. */
31 /** The position of this entry in the acl. */
32 struct list_head node
;
36 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
38 static int v4_addr_match(uint32_t addr_1
, uint32_t addr_2
, uint8_t netmask
)
43 mask
<<= (32 - netmask
);
44 return (htonl(addr_1
) & mask
) == (htonl(addr_2
) & mask
);
48 * Find out whether the peer name of a given fd belongs to an acl.
50 * \param fd File descriptor.
51 * \param acl The access control list.
53 * \return One if \a fd belongs to \a acl, zero otherwise.
55 static int acl_lookup(int fd
, struct list_head
*acl
)
57 struct access_info
*ai
, *tmp
;
58 struct sockaddr_storage ss
;
59 socklen_t sslen
= sizeof(ss
);
60 struct in_addr v4_addr
;
62 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
63 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno
));
66 extract_v4_addr(&ss
, &v4_addr
);
70 list_for_each_entry_safe(ai
, tmp
, acl
, node
)
71 if (v4_addr_match(v4_addr
.s_addr
, ai
->addr
.s_addr
, ai
->netmask
))
78 * Add an entry to an access control list.
80 * \param acl The access control list.
81 * \param addr The address to add.
82 * \param netmask The netmask to use for this entry.
84 static void acl_add_entry(struct list_head
*acl
, char *addr
, int netmask
)
86 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
88 inet_pton(AF_INET
, addr
, &ai
->addr
);
89 ai
->netmask
= netmask
;
90 PARA_INFO_LOG("adding %s/%u to access list\n", addr
, ai
->netmask
);
91 para_list_add(&ai
->node
, acl
);
95 * Delete an entry from an access control list.
97 * \param acl The access control list.
98 * \param addr The address to delete.
99 * \param netmask The netmask of the entry to be removed from the list.
101 static void acl_del_entry(struct list_head
*acl
, char *addr
, unsigned netmask
)
103 struct access_info
*ai
, *tmp
;
104 struct in_addr to_delete
;
106 inet_pton(AF_INET
, addr
, &to_delete
);
108 list_for_each_entry_safe(ai
, tmp
, acl
, node
) {
110 if (v4_addr_match(to_delete
.s_addr
, ai
->addr
.s_addr
,
111 PARA_MIN(netmask
, ai
->netmask
))) {
112 PARA_NOTICE_LOG("removing %s/%u from access list\n",
121 * Compute a string containing the contents of an acl.
123 * \param acl The access control list.
125 * \return A string containing the contents of \a acl, or \p NULL
126 * if \a acl is empty.
128 char *acl_get_contents(struct list_head
*acl
)
130 struct access_info
*ai
, *tmp_ai
;
133 list_for_each_entry_safe(ai
, tmp_ai
, acl
, node
) {
134 char *tmp
= make_message("%s%s/%u ", ret
? ret
: "",
135 inet_ntoa(ai
->addr
), ai
->netmask
);
143 * Initialize an access control list.
145 * \param acl The list to initialize.
146 * \param acl_info An array of strings of the form ip/netmask.
147 * \param num The number of strings in \a acl_info.
149 void acl_init(struct list_head
*acl
, char * const *acl_info
, int num
)
155 for (i
= 0; i
< num
; i
++)
156 if (parse_cidr(acl_info
[i
], addr
, sizeof(addr
), &mask
) == NULL
)
157 PARA_CRIT_LOG("ACL syntax error: %s, ignoring\n",
160 acl_add_entry(acl
, addr
, mask
);
164 * Check whether the peer name of a given fd is allowed by an acl.
166 * \param fd File descriptor.
167 * \param acl The access control list.
168 * \param default_deny Whether \a acl is a whitelist.
170 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
173 int acl_check_access(int fd
, struct list_head
*acl
, int default_deny
)
175 int match
= acl_lookup(fd
, acl
);
177 return (!match
|| default_deny
) && (match
|| !default_deny
)?
182 * Permit access for a range of IP addresses.
184 * \param addr The address to permit.
185 * \param netmask The netmask of the entry to be permitted.
186 * \param acl The access control list.
187 * \param default_deny Whether \a acl is a whitelist.
189 void acl_allow(char *addr
, int netmask
,
190 struct list_head
*acl
, int default_deny
)
193 acl_add_entry(acl
, addr
, netmask
);
195 acl_del_entry(acl
, addr
, netmask
);
199 * Deny access for a range of IP addresses.
201 * \param addr The address to deny.
202 * \param netmask The netmask of the entry to be denied.
203 * \param acl The access control list.
204 * \param default_deny Whether \a acl is a whitelist.
206 void acl_deny(char *addr
, int netmask
,
207 struct list_head
*acl
, int default_deny
)
209 acl_allow(addr
, netmask
, acl
, !default_deny
);