2006-07-20 23:21:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-07-20 22:58:37 +00:00
|
|
|
#include <sys/types.h>
|
2006-07-20 23:21:57 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2012-07-29 16:34:17 +08:00
|
|
|
#include <check.h>
|
|
|
|
|
|
2006-07-20 23:21:57 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2006-07-20 16:48:26 +00:00
|
|
|
#include <netinet/in.h>
|
2006-07-20 23:21:57 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_NETINET_IN_SYSTM_H
|
2006-07-20 22:58:37 +00:00
|
|
|
#include <netinet/in_systm.h>
|
2006-07-20 23:21:57 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_NETINET_IP_H
|
2006-07-20 16:48:26 +00:00
|
|
|
#include <netinet/ip.h>
|
2006-07-20 23:21:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
2011-10-04 14:01:03 +02:00
|
|
|
#include <pulsecore/log.h>
|
2011-01-04 17:03:13 +01:00
|
|
|
#include <pulsecore/macro.h>
|
2011-10-04 14:01:03 +02:00
|
|
|
#include <pulsecore/socket.h>
|
2006-07-20 16:48:26 +00:00
|
|
|
#include <pulsecore/ipacl.h>
|
2011-03-01 16:06:19 +01:00
|
|
|
#include <pulsecore/arpa-inet.h>
|
2006-07-20 16:48:26 +00:00
|
|
|
|
2011-10-28 16:24:18 +02:00
|
|
|
static void do_ip_acl_check(const char *s, int fd, int expected) {
|
|
|
|
|
pa_ip_acl *acl;
|
|
|
|
|
int result;
|
|
|
|
|
|
2012-07-29 16:34:17 +08:00
|
|
|
acl = pa_ip_acl_new(s);
|
|
|
|
|
fail_unless(acl != NULL);
|
2011-10-28 16:24:18 +02:00
|
|
|
result = pa_ip_acl_check(acl, fd);
|
|
|
|
|
pa_ip_acl_free(acl);
|
|
|
|
|
|
2011-10-04 14:01:03 +02:00
|
|
|
pa_log_info("%-20s result=%u (should be %u)", s, result, expected);
|
2012-07-29 16:34:17 +08:00
|
|
|
fail_unless(result == expected);
|
2011-10-28 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-29 16:34:17 +08:00
|
|
|
START_TEST (ipacl_test) {
|
2006-07-20 16:48:26 +00:00
|
|
|
struct sockaddr_in sa;
|
2009-02-13 21:58:09 +01:00
|
|
|
#ifdef HAVE_IPV6
|
2006-07-20 16:48:26 +00:00
|
|
|
struct sockaddr_in6 sa6;
|
2009-02-13 21:58:09 +01:00
|
|
|
#endif
|
2006-07-20 16:48:26 +00:00
|
|
|
int fd;
|
|
|
|
|
int r;
|
|
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
fd = socket(PF_INET, SOCK_STREAM, 0);
|
2012-07-29 16:34:17 +08:00
|
|
|
fail_unless(fd >= 0);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-20 16:48:26 +00:00
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
|
sa.sin_port = htons(22);
|
|
|
|
|
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-20 16:48:26 +00:00
|
|
|
r = connect(fd, (struct sockaddr*) &sa, sizeof(sa));
|
2012-07-29 16:34:17 +08:00
|
|
|
fail_unless(r >= 0);
|
2006-07-20 16:48:26 +00:00
|
|
|
|
2011-10-28 16:24:18 +02:00
|
|
|
do_ip_acl_check("127.0.0.1", fd, 1);
|
|
|
|
|
do_ip_acl_check("127.0.0.2/0", fd, 1);
|
|
|
|
|
do_ip_acl_check("127.0.0.1/32", fd, 1);
|
|
|
|
|
do_ip_acl_check("127.0.0.1/7", fd, 1);
|
|
|
|
|
do_ip_acl_check("127.0.0.2", fd, 0);
|
|
|
|
|
do_ip_acl_check("127.0.0.0/8;0.0.0.0/32", fd, 1);
|
|
|
|
|
do_ip_acl_check("128.0.0.2/9", fd, 0);
|
|
|
|
|
do_ip_acl_check("::1/9", fd, 0);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-20 16:48:26 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
2009-02-13 21:58:09 +01:00
|
|
|
#ifdef HAVE_IPV6
|
2009-06-24 18:03:40 +02:00
|
|
|
if ( (fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0 ) {
|
2011-10-04 14:01:03 +02:00
|
|
|
pa_log_error("Unable to open IPv6 socket, IPv6 tests ignored");
|
2012-07-29 16:34:17 +08:00
|
|
|
return;
|
2009-06-24 18:03:40 +02:00
|
|
|
}
|
2006-07-20 16:48:26 +00:00
|
|
|
|
|
|
|
|
memset(&sa6, 0, sizeof(sa6));
|
|
|
|
|
sa6.sin6_family = AF_INET6;
|
|
|
|
|
sa6.sin6_port = htons(22);
|
2012-07-29 16:34:17 +08:00
|
|
|
fail_unless(inet_pton(AF_INET6, "::1", &sa6.sin6_addr) == 1);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-20 16:48:26 +00:00
|
|
|
r = connect(fd, (struct sockaddr*) &sa6, sizeof(sa6));
|
2012-07-29 16:34:17 +08:00
|
|
|
fail_unless(r >= 0);
|
2006-07-20 16:48:26 +00:00
|
|
|
|
2011-10-28 16:24:18 +02:00
|
|
|
do_ip_acl_check("::1", fd, 1);
|
|
|
|
|
do_ip_acl_check("::1/9", fd, 1);
|
|
|
|
|
do_ip_acl_check("::/0", fd, 1);
|
|
|
|
|
do_ip_acl_check("::2/128", fd, 0);
|
|
|
|
|
do_ip_acl_check("::2/127", fd, 0);
|
|
|
|
|
do_ip_acl_check("::2/126", fd, 1);
|
2006-07-20 16:48:26 +00:00
|
|
|
|
|
|
|
|
close(fd);
|
2009-02-13 21:58:09 +01:00
|
|
|
#endif
|
2012-07-29 16:34:17 +08:00
|
|
|
}
|
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
int failed = 0;
|
|
|
|
|
Suite *s;
|
|
|
|
|
TCase *tc;
|
|
|
|
|
SRunner *sr;
|
|
|
|
|
|
|
|
|
|
if (!getenv("MAKE_CHECK"))
|
|
|
|
|
pa_log_set_level(PA_LOG_DEBUG);
|
|
|
|
|
|
|
|
|
|
s = suite_create("IP ACL");
|
|
|
|
|
tc = tcase_create("ipacl");
|
|
|
|
|
tcase_add_test(tc, ipacl_test);
|
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
|
|
|
|
|
|
sr = srunner_create(s);
|
|
|
|
|
srunner_run_all(sr, CK_NORMAL);
|
|
|
|
|
failed = srunner_ntests_failed(sr);
|
|
|
|
|
srunner_free(sr);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-07-29 16:34:17 +08:00
|
|
|
return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
2006-07-20 16:48:26 +00:00
|
|
|
}
|