2 * Copyright (C) 2005-2008 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. */
16 * Describes one entry in the blacklist/whitelist of a paraslash sender.
19 /** The address to be black/whitelisted. */
21 /** The netmask for this entry. */
23 /** The position of this entry in the acl. */
24 struct list_head node
;
29 * Return true if addr_1 matches addr_2 in the first `netmask' bits.
31 static int v4_addr_match(uint32_t addr_1
, uint32_t addr_2
, uint8_t netmask
)
36 mask
<<= (32 - netmask
);
37 return (htonl(addr_1
) & mask
) == (htonl(addr_2
) & mask
);
41 * Find out whether the peer name of a given fd belongs to an acl.
43 * \param fd File descriptor.
44 * \param acl The access control list.
46 * \return One if \a fd belongs to \a acl, zero otherwise.
48 static int acl_lookup(int fd
, struct list_head
*acl
)
50 struct access_info
*ai
, *tmp
;
51 struct sockaddr_storage ss
;
52 socklen_t sslen
= sizeof(ss
);
53 struct in_addr v4_addr
;
55 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
56 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno
));
59 v4_addr
= extract_v4_addr(&ss
);
63 list_for_each_entry_safe(ai
, tmp
, acl
, node
)
64 if (v4_addr_match(v4_addr
.s_addr
, ai
->addr
.s_addr
, ai
->netmask
))
71 * Add an entry to an access control list.
73 * \param acl The access control list.
74 * \param addr The address to add.
75 * \param netmask The netmask to use for this entry.
77 static void acl_add_entry(struct list_head
*acl
, struct in_addr addr
,
80 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
82 ai
->netmask
= netmask
;
83 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai
->addr
),
85 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
, struct in_addr addr
,
99 struct access_info
*ai
, *tmp
;
101 list_for_each_entry_safe(ai
, tmp
, acl
, node
) {
102 char *nad
= para_strdup(inet_ntoa(ai
->addr
));
103 if (!strcmp(nad
, inet_ntoa(addr
)) &&
104 ai
->netmask
== netmask
) {
105 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
)
148 for (i
= 0; i
< num
; i
++) {
149 char *arg
= para_strdup(acl_info
[i
]);
150 char *p
= strchr(arg
, '/');
157 if (!inet_pton(AF_INET
, arg
, &addr
))
160 if (netmask
< 0 || netmask
> 32)
162 acl_add_entry(acl
, addr
, netmask
);
165 PARA_CRIT_LOG("syntax error: %s\n", acl_info
[i
]);
174 * Check whether the peer name of a given fd is allowed by an acl.
176 * \param fd File descriptor.
177 * \param acl The access control list.
178 * \param default_deny Whether \a acl is a whitelist.
180 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
183 int acl_check_access(int fd
, struct list_head
*acl
, int default_deny
)
185 int match
= acl_lookup(fd
, acl
);
187 return (!match
|| default_deny
) && (match
|| !default_deny
)?
192 * Permit access for a range of IP addresses.
194 * \param addr The address to permit.
195 * \param netmask The netmask of the entry to be permitted.
196 * \param acl The access control list.
197 * \param default_deny Whether \a acl is a whitelist.
199 void acl_allow(struct in_addr addr
, int netmask
,
200 struct list_head
*acl
, int default_deny
)
203 acl_add_entry(acl
, addr
, netmask
);
205 acl_del_entry(acl
, addr
, netmask
);
209 * Deny access for a range of IP addresses.
211 * \param addr The address to permit.
212 * \param netmask The netmask of the entry to be permitted.
213 * \param acl The access control list.
214 * \param default_deny Whether \a acl is a whitelist.
216 void acl_deny(struct in_addr addr
, int netmask
,
217 struct list_head
*acl
, int default_deny
)
219 acl_allow(addr
, netmask
, acl
, !default_deny
);