From c286c78c1fd72752a4ebe8288dbd7346eec719e2 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Mon, 30 Aug 2021 10:44:18 -0500 Subject: [PATCH] Added libclogs Added custom logging library --- Makefile | 2 +- Readme.md | 2 ++ src/rhosts.c | 27 +++++++++++++++++++-------- src/rhosts.h | 2 ++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 4809a8d..6d24cdc 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ dir: if [ ! -d /etc/rhosts ];then mkdir /etc/rhosts;else echo "/etc/rhosts already exists";fi if [ ! -d build ];then mkdir build;else echo "build already exists";fi build: dir - gcc src/rhosts.c src/download.c -lcurl -o build/rhosts + gcc src/rhosts.c src/download.c -lcurl -lclogs -o build/rhosts clean: - rm -rf build install: build diff --git a/Readme.md b/Readme.md index b404e22..0fa2a21 100644 --- a/Readme.md +++ b/Readme.md @@ -8,6 +8,8 @@ This reroutes sites to 0.0.0.0 in order to block them from being reached by addi - libcurl4-gnutls-dev +- libclogs + ## Install sudo make install diff --git a/src/rhosts.c b/src/rhosts.c index 2e87083..a10f2dd 100644 --- a/src/rhosts.c +++ b/src/rhosts.c @@ -28,34 +28,40 @@ int main(int argc, char *argv[]){ int rc =0; + clogs_init_config(CLOGS_TIMESTAMP | CLOGS_USELOGFILE, \ + "/var/log/rhosts.log", CLOGS_DEBUG); + + clogs_print(CLOGS_DEBUG,"Initialized settting up logs"); rc = parse_config(&entries); if (rc != 0){ - printf("%d - parse_config failed",rc); + clogs_print(CLOGS_CRITICAL,"Parsing config failed"); return rc; } rc = preserve_static_entries(); if (rc != 0){ - printf("%d - preserve_static_entries failed",rc); + clogs_print(CLOGS_ERROR, "preserve_static_entries failed"); return rc; } rc = download_entries(&entries); if (rc != 0){ - printf("%d - download_entries failed",rc); + clogs_print(CLOGS_ERROR, "download_entries failed"); return rc; } rc = add_site_entries(&entries); if (rc != 0){ - printf("%d - download_entries failed",rc); + clogs_print(CLOGS_ERROR, "add_site_entries failed"); return rc; } rc = copy_tmp_to_hosts(); if (rc != 0){ - printf("%d - failed to copy to hosts file",rc); + printf("%d - ",rc); + clogs_print(CLOGS_ERROR, "failed to copy to hosts file"); return rc; } return 0; } int parse_config(struct entry **entries){ + clogs_print(CLOGS_DEBUG, "Entered parse_config"); int rc=0; FILE *configfile; @@ -116,29 +122,32 @@ int parse_config(struct entry **entries){ return 0; } int closefile(FILE **file, char *location){ - int rc = 0; + clogs_print(CLOGS_DEBUG, "Entering closefile");int rc = 0; rc = fclose(*file); if (rc != 0){ - printf("Failed to open %s\n", location); + clogs_print(CLOGS_ERROR, "Failed to close file%s", location); return errno; } return 0; } int openfile(FILE **file, char *mode, char *location){ + clogs_print(CLOGS_DEBUG, "Entering openfile: %s", location); *file = fopen(location, mode); if (*file == NULL){ - printf("Failed to open %s\n", location); + clogs_print(CLOGS_ERROR, "Failed to open %s", location); return errno; } return 0; } short int determine_config_entry_value(char *buff){ + clogs_print(CLOGS_DEBUG, "Entering determine_config_entry"); if (strncmp(buff,"#", 1) == 0){return CONTENTTYPE_COMMENT;} else if (strcmp(buff,"site") == 0){return CONTENTTYPE_SITE;} else if (strcmp(buff,"download") == 0){return CONTENTTYPE_DOWNLOAD;} else {return CONTENTTYPE_ERROR;} } int preserve_static_entries(){ + clogs_print(CLOGS_DEBUG, "Entering preserve_static_entries"); FILE *hostsf; FILE *tmpf; hostsf = fopen(HOSTSLOCATION, "r"); @@ -182,6 +191,7 @@ int preserve_static_entries(){ return 0; } int add_site_entries(struct entry **entries){ + clogs_print(CLOGS_DEBUG, "Entering add_site_entries"); int i = (*entries)[0].entrytype; int rc = 0; FILE *tmpf; @@ -217,6 +227,7 @@ int add_site_entries(struct entry **entries){ return 0; } int copy_tmp_to_hosts(){ + clogs_print(CLOGS_DEBUG, "Entering copy_tmp_to_hosts"); FILE *tmpf; tmpf = fopen(TMPLOCATION,"r"); if (tmpf == NULL) diff --git a/src/rhosts.h b/src/rhosts.h index 3f8bcf5..c930aa0 100644 --- a/src/rhosts.h +++ b/src/rhosts.h @@ -25,6 +25,8 @@ #include #include #include +#include +#include #ifdef _WIN64 #define TMPLOCATION "/tmp/rhosts"