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 int v4_addr_match(uint32_t addr_1
, uint32_t addr_2
, uint8_t netmask
)
39 mask
<<= (32 - netmask
);
40 return (htonl(addr_1
) & mask
) == (htonl(addr_2
) & mask
);
44 * Find out whether the peer name of a given fd belongs to an acl.
46 * \param fd File descriptor.
47 * \param acl The access control list.
49 * \return One if \a fd belongs to \a acl, zero otherwise.
51 static int acl_lookup(int fd
, struct list_head
*acl
)
53 struct access_info
*ai
, *tmp
;
54 struct sockaddr_storage ss
;
55 socklen_t sslen
= sizeof(ss
);
56 struct in_addr v4_addr
;
58 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
59 PARA_ERROR_LOG("Can not determine peer address: %s\n", strerror(errno
));
62 extract_v4_addr(&ss
, &v4_addr
);
66 list_for_each_entry_safe(ai
, tmp
, acl
, node
)
67 if (v4_addr_match(v4_addr
.s_addr
, ai
->addr
.s_addr
, ai
->netmask
))
74 * Add an entry to an access control list.
76 * \param acl The access control list.
77 * \param addr The address to add.
78 * \param netmask The netmask to use for this entry.
80 void acl_add_entry(struct list_head
*acl
, char *addr
, int netmask
)
82 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
84 inet_pton(AF_INET
, addr
, &ai
->addr
);
85 ai
->netmask
= netmask
;
86 PARA_INFO_LOG("adding %s/%u to access list\n", addr
, ai
->netmask
);
87 para_list_add(&ai
->node
, acl
);
91 * Delete an entry from an access control list.
93 * \param acl The access control list.
94 * \param addr The address to delete.
95 * \param netmask The netmask of the entry to be removed from the list.
97 static void acl_del_entry(struct list_head
*acl
, char *addr
, unsigned netmask
)
99 struct access_info
*ai
, *tmp
;
100 struct in_addr to_delete
;
102 inet_pton(AF_INET
, addr
, &to_delete
);
104 list_for_each_entry_safe(ai
, tmp
, acl
, node
) {
106 if (v4_addr_match(to_delete
.s_addr
, ai
->addr
.s_addr
,
107 PARA_MIN(netmask
, ai
->netmask
))) {
108 PARA_NOTICE_LOG("removing %s/%u from access list\n",
117 * Compute a string containing the contents of an acl.
119 * \param acl The access control list.
121 * \return A string containing the contents of \a acl, or \p NULL
122 * if \a acl is empty.
124 char *acl_get_contents(struct list_head
*acl
)
126 struct access_info
*ai
, *tmp_ai
;
129 list_for_each_entry_safe(ai
, tmp_ai
, acl
, node
) {
130 char *tmp
= make_message("%s%s/%u ", ret
? ret
: "",
131 inet_ntoa(ai
->addr
), ai
->netmask
);
139 * Check whether the peer name of a given fd is allowed by an acl.
141 * \param fd File descriptor.
142 * \param acl The access control list.
143 * \param default_deny Whether \a acl is a whitelist.
145 * \return Positive if the peer of \a fd is permitted by \a acl, \p -E_ACL_PERM
148 int acl_check_access(int fd
, struct list_head
*acl
, int default_deny
)
150 int match
= acl_lookup(fd
, acl
);
152 return (!match
|| default_deny
) && (match
|| !default_deny
)?
157 * Permit access for a range of IP addresses.
159 * \param addr The address to permit.
160 * \param netmask The netmask of the entry to be permitted.
161 * \param acl The access control list.
162 * \param default_deny Whether \a acl is a whitelist.
164 void acl_allow(char *addr
, int netmask
,
165 struct list_head
*acl
, int default_deny
)
168 acl_add_entry(acl
, addr
, netmask
);
170 acl_del_entry(acl
, addr
, netmask
);
174 * Deny access for a range of IP addresses.
176 * \param addr The address to deny.
177 * \param netmask The netmask of the entry to be denied.
178 * \param acl The access control list.
179 * \param default_deny Whether \a acl is a whitelist.
181 void acl_deny(char *addr
, int netmask
,
182 struct list_head
*acl
, int default_deny
)
184 acl_allow(addr
, netmask
, acl
, !default_deny
);