diff --git a/Jmakefile b/Jmakefile index fcc047c..0fcf8be 100644 --- a/Jmakefile +++ b/Jmakefile @@ -2,7 +2,7 @@ baseDir := ~ libsDir := $(baseDir)/libs projectName := ban2fail versions := debug release -cc_exe := ban2fail +cc_exe := ban2fail fsckdns install_dir := /usr/local/bin ######################################## @@ -34,6 +34,16 @@ src := \ libs := z crypto GeoIP pthread endif +ifeq ($(exe), fsckdns) +src := \ + ez_libc.c \ + fsckdns.c \ + str.c \ + util.c \ + +# libs := z crypto GeoIP pthread +endif + ######################################## # Set up custom compile flags here. # ######################################## diff --git a/ban2fail.sh b/ban2fail.sh index ecb87e3..192c8f2 100755 --- a/ban2fail.sh +++ b/ban2fail.sh @@ -37,7 +37,12 @@ while true; do echo "Monitoring: $MON_FNAMES" - while read FILE OPS; do + # Launch inotifywait in the background outputting to fd #3 + exec 3< <(exec $INOTIFYWAIT -m $MON_FNAMES) + INOTIFYWAIT_PID=$! + + # Read the output of inotifywait + while read -u 3 FILE OPS; do case "$OPS" in MOVE_SELF) break;; @@ -75,12 +80,19 @@ while true; do echo "Running $BAN2FAIL" # Here is where we check for offenses. - # If ban2fail failes it is probably because logrotated + # If ban2fail fails it is probably because logrotated # is managing the log files, so bail out... RAN_NS=$(date +%s%N) $TIME $BAN2FAIL || break - done < <($INOTIFYWAIT -m $MON_FNAMES) + done + + # Shut down inotifywait + if ps $INOTIFYWAIT_PID &>/dev/null; then + kill $INOTIFYWAIT_PID + wait + fi + exec 3<&- echo 'Exiting main loop' # Pause to let things settle down diff --git a/ez_libc.c b/ez_libc.c index 352ce45..6407364 100644 --- a/ez_libc.c +++ b/ez_libc.c @@ -132,6 +132,22 @@ int _ez_fclose ( return rtn; } +/***************************************************/ +int _ez_fflush ( + const char *fileName, + int lineNo, + const char *funcName, + FILE *stream + ) +{ + int rtn= fflush (stream); + if (EOF == rtn) { + _sys_eprintf((const char*(*)(int))strerror, fileName, lineNo, funcName, "fflush() failed"); + abort(); + } + return rtn; +} + /***************************************************/ size_t _ez_fread ( const char *fileName, diff --git a/ez_libc.h b/ez_libc.h index 42d8461..94c4ff8 100644 --- a/ez_libc.h +++ b/ez_libc.h @@ -102,6 +102,15 @@ int _ez_fclose ( FILE *stream ); +#define ez_fflush(stream) \ + _ez_fflush(__FILE__, __LINE__, __FUNCTION__, stream) +int _ez_fflush ( + const char *fileName, + int lineNo, + const char *funcName, + FILE *stream + ); + #define ez_fread(ptr, size, nmemb, stream) \ _ez_fread(__FILE__, __LINE__, __FUNCTION__, ptr, size, nmemb, stream) size_t _ez_fread( @@ -237,7 +246,6 @@ int _ez_stat ( struct stat *statbuf ); -// FIXME: xxxdir() function should be in ez_unistd.h #define ez_mkdir(pathname, mode) \ _ez_mkdir(__FILE__, __LINE__, __FUNCTION__, pathname, mode) int _ez_mkdir ( diff --git a/fsckdns.c b/fsckdns.c new file mode 100644 index 0000000..e2db4ed --- /dev/null +++ b/fsckdns.c @@ -0,0 +1,89 @@ +/*************************************************************************** + * Copyright (C) 2019 by John D. Robertson * + * john@rrci.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#define _GNU_SOURCE +#include +#include +#include + +#include "ez_libc.h" +#include "util.h" +const static struct addrinfo rev_hints= { + .ai_flags = AI_NUMERICHOST, /* doing reverse lookups */ + .ai_family = AF_UNSPEC, /* Allow IPv4 or IPv6 */ + .ai_socktype= SOCK_DGRAM, + .ai_protocol= IPPROTO_UDP +}; + +const static struct addrinfo fwd_hints= { + .ai_family= AF_UNSPEC, /* Allow IPv4 or IPv6 */ + .ai_socktype= SOCK_DGRAM, + .ai_protocol= IPPROTO_UDP +}; + +int +main(int argc, char **argv) +{ + int rtn= EXIT_FAILURE; + const char *addr= NULL; + static char hostBuf[PATH_MAX]; + + extern char *optarg; + extern int optind, optopt; + + for(optind= 1; optind < argc; ++optind) + { + addr= argv[optind]; + + /*============ Reverse DNS lookup ================*/ + /* Get a populated addrinfo object */ + struct addrinfo *res= NULL; + int rc= ez_getaddrinfo(addr, NULL, &rev_hints, &res); + assert(0 == rc); + assert(res && res->ai_addr && res->ai_addrlen); + addrinfo_print(res, stdout); + ez_fflush(stdout); + + /* Now do blocking reverse lookup */ + rc= ez_getnameinfo(res->ai_addr, res->ai_addrlen, hostBuf, sizeof(hostBuf)-1, NULL, 0, NI_NAMEREQD); + if(rc) { + ez_fprintf(stdout, "%s\n", gai_strerror(rc)); + continue; + } + + ez_fprintf(stdout, "RevDNS= \"%s\"\n", hostBuf); + ez_fflush(stdout); + + /*============ Forward DNS lookup ================*/ + rc= ez_getaddrinfo(hostBuf, NULL, &fwd_hints, &res); + const char *msg= NULL; + + if(rc) { + ez_fprintf(stdout, "%s\n", gai_strerror(rc)); + continue; + } + + addrinfo_print(res, stdout); + ez_fflush(stdout); + + } + + rtn= EXIT_SUCCESS; +abort: + return rtn; +}