mirror of
https://github.com/clearlinux/tallow.git
synced 2026-08-19 02:57:11 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9042a01eab | |||
| 2225ee029d | |||
| dee23b8275 | |||
| 34bd8d55bd | |||
| 2a33768293 | |||
| ea958fd2b5 | |||
| 4547892d56 | |||
| c661a20e33 | |||
| 9f37520c72 | |||
| dc8f37e41f |
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
|
|
||||||
AM_CFLAGS = -g $(LIBSYSTEMD_CFLAGS) -Wall -Wno-uninitialized -W -D_FORTIFY_SOURCE=2
|
AM_CFLAGS = -g $(LIBSYSTEMD_CFLAGS) $(LIBSYSTEMD_JOURNAL_CFLAGS) -Wall -Wno-uninitialized -W -D_FORTIFY_SOURCE=2
|
||||||
|
|
||||||
systemdsystemunitdir = @SYSTEMD_SYSTEMUNITDIR@
|
systemdsystemunitdir = @SYSTEMD_SYSTEMUNITDIR@
|
||||||
systemdsystemunit_DATA = tallow.service
|
systemdsystemunit_DATA = tallow.service
|
||||||
|
|
||||||
sbin_PROGRAMS = tallow
|
sbin_PROGRAMS = tallow
|
||||||
tallow_SOURCES = tallow.c
|
tallow_SOURCES = tallow.c
|
||||||
tallow_LDADD = $(LIBSYSTEMD_LIBS)
|
tallow_LDADD = $(LIBSYSTEMD_LIBS) $(LIBSYSTEMD_JOURNAL_LIBS)
|
||||||
|
|
||||||
EXTRA_DIST = AUTHORS COPYING INSTALL tallow.service.in tallow.1.md
|
EXTRA_DIST = AUTHORS COPYING INSTALL tallow.service.in tallow.1.md
|
||||||
|
|
||||||
|
|||||||
+4
-2
@@ -2,7 +2,7 @@
|
|||||||
# Process this file with autoconf to produce a configure script.
|
# Process this file with autoconf to produce a configure script.
|
||||||
|
|
||||||
AC_PREREQ([2.64])
|
AC_PREREQ([2.64])
|
||||||
AC_INIT([tallow], [2], [auke-jan.h.kok@intel.com])
|
AC_INIT([tallow], [4], [auke-jan.h.kok@intel.com])
|
||||||
AM_INIT_AUTOMAKE([])
|
AM_INIT_AUTOMAKE([])
|
||||||
AC_CONFIG_FILES([Makefile])
|
AC_CONFIG_FILES([Makefile])
|
||||||
|
|
||||||
@@ -10,9 +10,11 @@ AC_CONFIG_FILES([Makefile])
|
|||||||
AC_PROG_CC
|
AC_PROG_CC
|
||||||
AC_PROG_INSTALL
|
AC_PROG_INSTALL
|
||||||
|
|
||||||
PKG_CHECK_MODULES([LIBSYSTEMD], [libsystemd])
|
PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd,, [PKG_CHECK_MODULES(LIBSYSTEMD_JOURNAL, libsystemd-journal)])
|
||||||
AC_SUBST(LIBSYSTEMD_CFLAGS)
|
AC_SUBST(LIBSYSTEMD_CFLAGS)
|
||||||
AC_SUBST(LIBSYSTEMD_LIBS)
|
AC_SUBST(LIBSYSTEMD_LIBS)
|
||||||
|
AC_SUBST(LIBSYSTEMD_JOURNAL_CFLAGS)
|
||||||
|
AC_SUBST(LIBSYSTEMD_JOURNAL_LIBS)
|
||||||
|
|
||||||
AC_ARG_WITH([systemdsystemunitdir], AC_HELP_STRING([--with-systemdsystemunitdir=DIR],
|
AC_ARG_WITH([systemdsystemunitdir], AC_HELP_STRING([--with-systemdsystemunitdir=DIR],
|
||||||
[path to systemd system service directory]), [path_systemdsystemunit=${withval}],
|
[path to systemd system service directory]), [path_systemdsystemunit=${withval}],
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@@ -69,11 +70,47 @@ static void ext_ignore(char *fmt, ...)
|
|||||||
__attribute__((unused)) int ret = system(cmd);
|
__attribute__((unused)) int ret = system(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void setup(void)
|
||||||
|
{
|
||||||
|
static bool done = false;
|
||||||
|
if (done)
|
||||||
|
return;
|
||||||
|
done = true;
|
||||||
|
|
||||||
|
/* init ipset and iptables */
|
||||||
|
/* delete iptables ref to set before the ipset! */
|
||||||
|
ext_ignore("%s/iptables -t filter -D INPUT -m set --match-set tallow src -j DROP 2> /dev/null", ipt_path);
|
||||||
|
ext_ignore("%s/ipset destroy tallow 2> /dev/null", ipt_path);
|
||||||
|
if (ext("%s/ipset create tallow hash:ip family inet timeout %d", ipt_path, expires)) {
|
||||||
|
fprintf(stderr, "Unable to create ipv4 ipset.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (ext("%s/iptables -t filter -A INPUT -m set --match-set tallow src -j DROP", ipt_path)) {
|
||||||
|
fprintf(stderr, "Unable to create iptables rule.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (has_ipv6) {
|
||||||
|
ext_ignore("%s/ip6tables -t filter -D INPUT -m set --match-set tallow6 src -j DROP 2> /dev/null", ipt_path);
|
||||||
|
ext_ignore("%s/ipset destroy tallow6 2> /dev/null", ipt_path);
|
||||||
|
if (ext("%s/ipset create tallow6 hash:ip family inet6 timeout %d", ipt_path, expires)) {
|
||||||
|
fprintf(stderr, "Unable to create ipv6 ipset.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (ext("%s/ip6tables -t filter -A INPUT -m set --match-set tallow6 src -j DROP", ipt_path)) {
|
||||||
|
fprintf(stderr, "Unable to create ipt6ables rule.\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void block(struct tallow_struct *s)
|
static void block(struct tallow_struct *s)
|
||||||
{
|
{
|
||||||
if (s->count != threshold)
|
if (s->count != threshold)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
setup();
|
||||||
|
|
||||||
if (strchr(s->ip, ':')) {
|
if (strchr(s->ip, ':')) {
|
||||||
if (has_ipv6)
|
if (has_ipv6)
|
||||||
(void) ext("%s/ipset -A tallow6 %s", ipt_path, s->ip);
|
(void) ext("%s/ipset -A tallow6 %s", ipt_path, s->ip);
|
||||||
@@ -82,18 +119,6 @@ static void block(struct tallow_struct *s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Blocked %s\n", s->ip);
|
fprintf(stderr, "Blocked %s\n", s->ip);
|
||||||
|
|
||||||
/* remove entry from the list */
|
|
||||||
if (head == s) {
|
|
||||||
head = s->next;
|
|
||||||
free(s->ip);
|
|
||||||
free(s);
|
|
||||||
} else {
|
|
||||||
struct tallow_struct *p = s;
|
|
||||||
s = s->next;
|
|
||||||
free(p->ip);
|
|
||||||
free(p);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void whitelist_add(char *ip)
|
static void whitelist_add(char *ip)
|
||||||
@@ -107,7 +132,7 @@ static void whitelist_add(char *ip)
|
|||||||
n = malloc(sizeof(struct tallow_struct));
|
n = malloc(sizeof(struct tallow_struct));
|
||||||
if (!n) {
|
if (!n) {
|
||||||
fprintf(stderr, "Out of memory.\n");
|
fprintf(stderr, "Out of memory.\n");
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
memset(n, 0, sizeof(struct tallow_struct));
|
memset(n, 0, sizeof(struct tallow_struct));
|
||||||
n->ip = strdup(ip);
|
n->ip = strdup(ip);
|
||||||
@@ -125,6 +150,9 @@ static void find(char *ip)
|
|||||||
struct tallow_struct *n;
|
struct tallow_struct *n;
|
||||||
struct tallow_struct *w = whitelist;
|
struct tallow_struct *w = whitelist;
|
||||||
|
|
||||||
|
if (!ip)
|
||||||
|
return;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* not validating the IP address format here, just
|
* not validating the IP address format here, just
|
||||||
* making sure we're not passing special characters
|
* making sure we're not passing special characters
|
||||||
@@ -160,7 +188,7 @@ static void find(char *ip)
|
|||||||
n = malloc(sizeof(struct tallow_struct));
|
n = malloc(sizeof(struct tallow_struct));
|
||||||
if (!n) {
|
if (!n) {
|
||||||
fprintf(stderr, "Out of memory.\n");
|
fprintf(stderr, "Out of memory.\n");
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
memset(n, 0, sizeof(struct tallow_struct));
|
memset(n, 0, sizeof(struct tallow_struct));
|
||||||
|
|
||||||
@@ -178,35 +206,51 @@ static void find(char *ip)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump(void)
|
static void sig(int u __attribute__ ((unused)))
|
||||||
{
|
{
|
||||||
struct tallow_struct *s = head;
|
fprintf(stderr, "Exiting on request.\n");
|
||||||
fprintf(stderr, "Received SIGUSR1 - dumping address table: address: count, time\n");
|
sd_journal_close(j);
|
||||||
|
|
||||||
|
struct tallow_struct *s = head;
|
||||||
while (s) {
|
while (s) {
|
||||||
fprintf(stderr, "%s: %d, %lu.%lu\n", s->ip, s->count, s->time.tv_sec, s->time.tv_usec);
|
struct tallow_struct *n = NULL;
|
||||||
|
|
||||||
|
free(s->ip);
|
||||||
|
n = s;
|
||||||
s = s->next;
|
s = s->next;
|
||||||
|
free(n);
|
||||||
}
|
}
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sig(int s)
|
static void prune(void)
|
||||||
{
|
{
|
||||||
if (s == SIGUSR1) {
|
struct tallow_struct *s = head;
|
||||||
dump();
|
struct tallow_struct *p;
|
||||||
} else {
|
struct timeval tv;
|
||||||
fprintf(stderr, "Exiting on request.\n");
|
|
||||||
sd_journal_close(j);
|
|
||||||
|
|
||||||
struct tallow_struct *s = head;
|
(void) gettimeofday(&tv, NULL);
|
||||||
while (s) {
|
p = NULL;
|
||||||
struct tallow_struct *n = NULL;
|
|
||||||
|
|
||||||
free(s->ip);
|
while (s) {
|
||||||
n = s;
|
if ((tv.tv_sec - s->time.tv_sec) > expires) {
|
||||||
s = s->next;
|
if (p) {
|
||||||
free(n);
|
p->next = s->next;
|
||||||
|
free(s->ip);
|
||||||
|
free(s);
|
||||||
|
s = p->next;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
head = s->next;
|
||||||
|
free(s->ip);
|
||||||
|
free(s);
|
||||||
|
s = head;
|
||||||
|
p = NULL;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exit(0);
|
p = s;
|
||||||
|
s = s->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +265,6 @@ int main(void)
|
|||||||
|
|
||||||
memset(&s, 0, sizeof(struct sigaction));
|
memset(&s, 0, sizeof(struct sigaction));
|
||||||
s.sa_handler = sig;
|
s.sa_handler = sig;
|
||||||
sigaction(SIGUSR1, &s, NULL);
|
|
||||||
sigaction(SIGHUP, &s, NULL);
|
sigaction(SIGHUP, &s, NULL);
|
||||||
sigaction(SIGTERM, &s, NULL);
|
sigaction(SIGTERM, &s, NULL);
|
||||||
sigaction(SIGINT, &s, NULL);
|
sigaction(SIGINT, &s, NULL);
|
||||||
@@ -276,34 +319,9 @@ int main(void)
|
|||||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
|
fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* init ipset and iptables */
|
|
||||||
/* delete iptables ref to set before the ipset! */
|
|
||||||
ext_ignore("%s/iptables -t filter -D INPUT -m set --match-set tallow src -j DROP 2> /dev/null", ipt_path);
|
|
||||||
ext_ignore("%s/ipset destroy tallow 2> /dev/null", ipt_path);
|
|
||||||
if (ext("%s/ipset create tallow hash:ip family inet timeout %d", ipt_path, expires)) {
|
|
||||||
fprintf(stderr, "Unable to create ipv4 ipset.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (ext("%s/iptables -t filter -A INPUT -m set --match-set tallow src -j DROP", ipt_path)) {
|
|
||||||
fprintf(stderr, "Unable to create iptables rule.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (has_ipv6) {
|
|
||||||
ext_ignore("%s/ip6tables -t filter -D INPUT -m set --match-set tallow6 src -j DROP 2> /dev/null", ipt_path);
|
|
||||||
ext_ignore("%s/ipset destroy tallow6 2> /dev/null", ipt_path);
|
|
||||||
if (ext("%s/ipset create tallow6 hash:ip family inet6 timeout %d", ipt_path, expires)) {
|
|
||||||
fprintf(stderr, "Unable to create ipv6 ipset.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (ext("%s/ip6tables -t filter -A INPUT -m set --match-set tallow6 src -j DROP", ipt_path)) {
|
|
||||||
fprintf(stderr, "Unable to create ipt6ables rule.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ffwd journal */
|
/* ffwd journal */
|
||||||
sd_journal_add_match(j, FILTER_STRING, 0);
|
sd_journal_add_match(j, FILTER_STRING, 0);
|
||||||
@@ -355,9 +373,11 @@ int main(void)
|
|||||||
|
|
||||||
free(m);
|
free(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prune();
|
||||||
}
|
}
|
||||||
|
|
||||||
sd_journal_close(j);
|
sd_journal_close(j);
|
||||||
|
|
||||||
exit(0);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user