From b5eba018a0a948d7757448d4cd70fe33cb6b71a0 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 27 Jan 2020 15:46:06 +0000 Subject: Add shibari, prepare for 2.3.2.0 --- src/caches/deps-exe/shibari | 6 ++++ src/caches/deps-lib/shibari | 4 +++ src/caches/shibari-internal.h | 25 ++++++++++++++ src/caches/shibari.c | 14 ++++++++ src/caches/shibari_whitelist_add6.c | 28 +++++++++++++++ src/caches/shibari_whitelist_ip4_match.c | 15 +++++++++ src/caches/shibari_whitelist_ip6_match.c | 16 +++++++++ src/caches/shibari_whitelist_read.c | 58 ++++++++++++++++++++++++++++++++ 8 files changed, 166 insertions(+) create mode 100644 src/caches/deps-exe/shibari create mode 100644 src/caches/deps-lib/shibari create mode 100644 src/caches/shibari-internal.h create mode 100644 src/caches/shibari.c create mode 100644 src/caches/shibari_whitelist_add6.c create mode 100644 src/caches/shibari_whitelist_ip4_match.c create mode 100644 src/caches/shibari_whitelist_ip6_match.c create mode 100644 src/caches/shibari_whitelist_read.c (limited to 'src') diff --git a/src/caches/deps-exe/shibari b/src/caches/deps-exe/shibari new file mode 100644 index 0000000..ae7e7f9 --- /dev/null +++ b/src/caches/deps-exe/shibari @@ -0,0 +1,6 @@ +libshibari.a.xyzzy +${LIBDCACHE} +${LIBS6DNS} +-lskarnet +${SOCKET_LIB} +${SYSCLOCK_LIB} diff --git a/src/caches/deps-lib/shibari b/src/caches/deps-lib/shibari new file mode 100644 index 0000000..9e34094 --- /dev/null +++ b/src/caches/deps-lib/shibari @@ -0,0 +1,4 @@ +shibari_whitelist_add6.o +shibari_whitelist_ip4_match.o +shibari_whitelist_ip6_match.o +shibari_whitelist_read.o diff --git a/src/caches/shibari-internal.h b/src/caches/shibari-internal.h new file mode 100644 index 0000000..048faa5 --- /dev/null +++ b/src/caches/shibari-internal.h @@ -0,0 +1,25 @@ +/* ISC license. */ + +#ifndef S6DNS_SHIBARI_INTERNAL_H +#define S6DNS_SHIBARI_INTERNAL_H + +#include + +#include +#include + +typedef struct shibari_ip6_s shibari_ip6_t, *shibari_ip6_t_ref ; +struct shibari_ip6_s +{ + uint64_t addr0 ; + uint64_t addr1 ; + uint64_t mask0 ; + uint64_t mask1 ; +} ; + +extern int shibari_whitelist_add6 (genalloc *g, char const *, uint16_t) ; +extern int shibari_whitelist_read (char const *, genalloc *, genalloc *) ; +extern int shibari_whitelist_ip4_match (diuint32 const *, size_t, char const *) ; +extern int shibari_whitelist_ip6_match (shibari_ip6_t const *, size_t, char const *) ; + +#endif diff --git a/src/caches/shibari.c b/src/caches/shibari.c new file mode 100644 index 0000000..77334f6 --- /dev/null +++ b/src/caches/shibari.c @@ -0,0 +1,14 @@ +/* ISC license. */ + +#include +#include + +#include + +#define USAGE "shibari [ -m max ] [ -i ipsend ] [ [ -u uid ] [ -g gid ] | [ -U ] ]" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv) +{ + return 0 ; +} diff --git a/src/caches/shibari_whitelist_add6.c b/src/caches/shibari_whitelist_add6.c new file mode 100644 index 0000000..0c9e53a --- /dev/null +++ b/src/caches/shibari_whitelist_add6.c @@ -0,0 +1,28 @@ +/* ISC license. */ + +#include + +#include +#include + +#include "shibari-internal.h" + +int shibari_whitelist_add6 (genalloc *g, char const *ip6, uint16_t mask) +{ + shibari_ip6_t shix ; + if (mask >= 64) + { + shix.mask0 = ~(uint64_t)0 ; + shix.mask1 = ((uint64_t)1 << (mask - 64)) - 1 ; + } + else + { + shix.mask0 = ((uint64_t)1 << mask) - 1 ; + shix.mask1 = 0 ; + } + uint64_unpack_big(ip6, &shix.addr0) ; + shix.addr0 &= shix.mask0 ; + uint64_unpack_big(ip6 + 8, &shix.addr1) ; + shix.addr1 &= shix.mask1 ; + return genalloc_append(shibari_ip6_t, g, &shix) ; +} diff --git a/src/caches/shibari_whitelist_ip4_match.c b/src/caches/shibari_whitelist_ip4_match.c new file mode 100644 index 0000000..fbf11ac --- /dev/null +++ b/src/caches/shibari_whitelist_ip4_match.c @@ -0,0 +1,15 @@ +/* ISC license. */ + +#include + +#include + +#include "shibari-internal.h" + +int shibari_whitelist_ip4_match (diuint32 const *s, size_t len, char const *ip) +{ + uint32_t ip4 ; + uint32_unpack_big(ip, &ip4) ; + for (; len-- ; s++) if ((ip4 & s->right) == s->left) return 1 ; + return 0 ; +} diff --git a/src/caches/shibari_whitelist_ip6_match.c b/src/caches/shibari_whitelist_ip6_match.c new file mode 100644 index 0000000..c728081 --- /dev/null +++ b/src/caches/shibari_whitelist_ip6_match.c @@ -0,0 +1,16 @@ +/* ISC license. */ + +#include + +#include + +#include "shibari-internal.h" + +int shibari_whitelist_ip6_match (shibari_ip6_t const *s, size_t len, char const *ip) +{ + uint64_t addr0, addr1 ; + uint64_unpack_big(ip, &addr0) ; + uint64_unpack_big(ip + 8, &addr1) ; + for (; len-- ; s++) if ((addr0 & s->mask0) == s->addr0 && (addr1 & s->mask1) == s->addr1) return 1 ; + return 0 ; +} diff --git a/src/caches/shibari_whitelist_read.c b/src/caches/shibari_whitelist_read.c new file mode 100644 index 0000000..58dd2a0 --- /dev/null +++ b/src/caches/shibari_whitelist_read.c @@ -0,0 +1,58 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "shibari-internal.h" + +static int shibari_whitelist_add4 (genalloc *g, char const *ip4, uint16_t mask) +{ + diuint32 d = { .right = ((uint32_t)1 << mask) - 1 } ; + uint32_unpack_big(ip4, &d.left) ; + d.left &= d.right ; + return genalloc_append(diuint32, g, &d) ; +} + +int shibari_whitelist_read (char const *path, genalloc *ip4, genalloc *ip6) +{ + DIR *dir = opendir(path) ; + if (!dir) return 0 ; + genalloc_setlen(diuint32, ip4, 0) ; + genalloc_setlen(shibari_ip6_t, ip6, 0) ; + for (;;) + { + direntry *d ; + size_t pos ; + ip46_t ip ; + uint16_t mask ; + errno = 0 ; + d = readdir(dir) ; + if (!d) break ; + if (d->d_name[0] == '.' && (!d->d_name[1] || (d->d_name[1] == '.' && !d->d_name[2]))) continue ; + pos = ip46_scan(d->d_name, &ip) ; + if (!pos) continue ; + if (d->d_name[pos] && d->d_name[pos] != '_') continue ; + if (!d->d_name[pos]) mask = ip46_is6(&ip) ? 128 : 32 ; + else + { + if (!uint160_scan(d->d_name + pos + 1, &mask)) continue ; + if (mask > (ip46_is6(&ip) ? 128 : 32)) continue ; + } + if (!(ip46_is6(&ip) ? shibari_whitelist_add6(ip6, ip.ip, mask) : shibari_whitelist_add4(ip4, ip.ip, mask))) goto err ; + } + if (errno) goto err ; + dir_close(dir) ; + return 1 ; + + err: + dir_close(dir) ; + return 0 ; +} -- cgit v1.2.3