gods

a simple blocklist for ssh
Log | Files | Refs | README | LICENSE

commit 98c4949aa2185b3268f95f79f0ff5f9979071659
parent b994f7efa9c60d5152661c090d2a7cb75a4c970d
Author: Naveen Narayanan <zerous@nocebo.space>
Date:   Sat, 23 Oct 2021 13:18:55 +0200

Daemonize sdog

Diffstat:
Aarg.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.c | 42+++++++++++++++++++++++++++++++++++++++++-
2 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/arg.h b/arg.h @@ -0,0 +1,50 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + int i_;\ + for (i_ = 1, brk_ = 0, argv_ = argv;\ + argv[0][i_] && !brk_;\ + i_++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][i_];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/main.c b/main.c @@ -1,4 +1,5 @@ #include <sys/stat.h> +#include <sys/types.h> #include <err.h> #include <errno.h> @@ -9,6 +10,8 @@ #include <string.h> #include <unistd.h> +#include "arg.h" + #include "attack.h" #include "config.h" #include "fw.h" @@ -21,6 +24,7 @@ int fd_black; int fd_white; int ignore; +char *argv0; static char buf[BUFSZ]; SLIST_HEAD(lhead, attacker) head = SLIST_HEAD_INITIALIZER(head); @@ -135,16 +139,52 @@ cleanup() free(prev); } +void +usage(void) +{ + fprintf(stderr, "usage: sdog [-d]\n"); + exit(1); +} + +void +daemon_init(void) +{ + pid_t p; + + if ((p = fork()) == -1) + err(1, "fork failed"); + else if (p != 0) + exit(0); + if (setsid() == -1) + err(1, "setsid failed"); + if (chdir("/etc/gods") == -1) + err(1, "chdir failed: %s", "/etc/gods"); + umask(0); +} + int main(int argc, char **argv) { FILE *fp; char *line; char bf[16]; - int fd, found, ret; + int debug, fd, found, ret; struct attacker *a; time_t now; + debug = 0; + + ARGBEGIN { + case 'd': + debug = 1; + break; + default: + usage(); + } ARGEND + + if (!debug) + daemon_init(); + fd = open(sshlog, O_RDONLY); if (fd == -1) err(1, "open failed: %s", sshlog);