gods

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

commit 636064570f20078bbb86262efb4487ae7aee24de
parent 097ab99b0158ec54817aa77cc142cec156964229
Author: Naveen Narayanan <zerous@nocebo.space>
Date:   Sun, 26 Sep 2021 18:55:14 +0200

Move helper func to individual translation units

isip()
range()

Remove param.h as it is a kludgy solution for squeezing
things that don't belong together into one.

Diffstat:
MMakefile | 2++
Aip.c | 42++++++++++++++++++++++++++++++++++++++++++
Aip.h | 6++++++
Dparam.h | 15---------------
Mparser.c | 76++++++++--------------------------------------------------------------------
Arange.c | 26++++++++++++++++++++++++++
Autil.h | 6++++++
7 files changed, 90 insertions(+), 83 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,8 +1,10 @@ include config.mk OBJ = \ + ip.o\ main.o \ parser.o \ + range.o \ BIN = sdog diff --git a/ip.c b/ip.c @@ -0,0 +1,42 @@ +#include <string.h> + +#include "util.h" + +int +isip(char *s) +{ + char ip[16]; + char *o; + int n, t; + + if ((n = strlen(s)) < 7 || n > 15) + return 0; + + strcpy(ip, s); + + o = strtok(ip, "."); + if (!o) + return 0; + if ((t = range(o, 0, 255)) == -1) + return 0; + + o = strtok(NULL, "."); + if (!o) + return 0; + if ((t = range(o, 0, 255)) == -1) + return 0; + + o = strtok(NULL, "."); + if (!o) + return 0; + if ((t = range(o, 0, 255)) == -1) + return 0; + + o = strtok(NULL, "."); + if (!o) + return 0; + if ((t = range(o, 0, 255)) == -1) + return 0; + + return 1; +} diff --git a/ip.h b/ip.h @@ -0,0 +1,6 @@ +#ifndef _IP_H +#define _IP_H + +int isip(char *); + +#endif diff --git a/param.h b/param.h @@ -1,15 +0,0 @@ -#ifndef _PARAM_H -#define _PARAM_H - -struct user { - char *ipaddr; - int attacker; - int blocked; - char nprobe; -}; - -struct user *users; - -int parse(char *); - -#endif diff --git a/parser.c b/parser.c @@ -8,16 +8,17 @@ #include <time.h> #include <unistd.h> -#include "param.h" +#include "ip.h" +#include "util.h" +#define BUFSZ 256 #define MAXTOKENLEN 256 char ip[16]; char statmsg[BUFSZ]; static char token[MAXTOKENLEN]; -static char *tp; -static char *lp; -time_t probtim; +static char *lp, *tp; +time_t attack; char *month[] = { "JAN", @@ -45,30 +46,9 @@ accept(int c) } static int -range(char *s, int min, int max) -{ - int t; - - if (!*s) - return -1; - - errno = 0; - t = atoi(s); - if (errno) { - fprintf(stderr, "atoi failed: %s\n", strerror(errno)); - return -1; - } - if (t < min || t > max) - return -1; - - return t; -} - -static int mon(struct tm *tm) { int n, i; - char str[4]; if ((n = strlen(tp)) != 3) @@ -192,7 +172,7 @@ timestamp() return 0; tm.tm_year = 121; /* XXX Call time and setup year? */ - probtim = mktime(&tm); + attack = mktime(&tm); return 1; } @@ -231,49 +211,9 @@ procid() } static int -isip(char *s) -{ - char ip[16]; - char *o; - int n, t; - - if ((n = strlen(token)) < 7 || n > 15) - return 0; - - strcpy(ip, token); - - o = strtok(ip, "."); - if (!o) - return 0; - if ((t = range(o, 0, 255)) == -1) - return 0; - - o = strtok(NULL, "."); - if (!o) - return 0; - if ((t = range(o, 0, 255)) == -1) - return 0; - - o = strtok(NULL, "."); - if (!o) - return 0; - if ((t = range(o, 0, 255)) == -1) - return 0; - - o = strtok(NULL, "."); - if (!o) - return 0; - if ((t = range(o, 0, 255)) == -1) - return 0; - - return 1; -} - -static int constat() { - char str[128]; - char *sp = str; + char *sp = statmsg; while (word()) { if (*tp) { @@ -288,7 +228,7 @@ constat() } *sp = '\0'; - if (sp == str) + if (sp == statmsg) return 0; return 1; diff --git a/range.c b/range.c @@ -0,0 +1,26 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int +range(char *s, int min, int max) +{ + int t; + + if (!*s) + return -1; + + errno = 0; + printf("range str: %s\n", s); + t = atoi(s); + printf("range t: %d\n", t); + if (errno) { + fprintf(stderr, "atoi failed: %s\n", strerror(errno)); + return -1; + } + if (t < min || t > max) + return -1; + + return t; +} diff --git a/util.h b/util.h @@ -0,0 +1,6 @@ +#ifndef _UTIL_H +#define _UTIL_H + +int range(char *, int, int); + +#endif