From f5036783f9410ac09c206f21054ad60698c0d665 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 17 Jan 2020 08:08:11 -0500 Subject: [PATCH 1/3] Implementing deleteHost --- main.c | 94 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/main.c b/main.c index 47dd306..47c0377 100644 --- a/main.c +++ b/main.c @@ -32,9 +32,6 @@ static char *HOSTFILE = "/etc/hosts"; static char *CONFIG; // Our configuration - -void replacehost(char *oldhost, char *newhost); - /** * Return an open handle to the hosts file. * @@ -46,6 +43,53 @@ FILE *fopenHostsFile(char *mode) return fopen(HOSTFILE, mode); } +/** + * Replace a host in the hosts file with the NEW host. + * + * @param oldhost + * @param newhost + * @param hostsFile + */ +void replacehost(char *oldhost, char *newhost, int deleteHost) { + FILE *hostsFile = fopenHostsFile("r"); + char buf[256]; + char *ptr, *f; + + char newHostsFile[4096]; + memset(newHostsFile, 0, 4096); + +// fseek(hostsFile, 0, SEEK_SET); + + while (fgets(buf, 256, hostsFile) != NULL) + { + f = strcasestr(buf, oldhost); + if (f != NULL) { + // Just skip writing this host to the hosts file if we are deleting it + if (deleteHost) + continue; + memset(buf, 0, sizeof(buf)); + sprintf(buf, "%s %s\n", blockString, newhost); + } + strcat(newHostsFile, buf); + memset(buf, 0, sizeof(buf)); + } + + // Ensure we have an EOF at the end of the file. + newHostsFile[strlen(newHostsFile)] = EOF; + +// // Seek to 0 +// fseek(hostsFile, 0, SEEK_SET); + fclose(hostsFile); + + // If this failed, write an error message to stderr +// ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) { +// fprintf(stderr, "Did not write anything!\n"); +// } + fprintf(stderr, "New hosts file: \n%s\n", newHostsFile); +// fclose(hostsFile); +} + + /** * Block a hosts using the hostsFile in FILE */ @@ -65,9 +109,10 @@ void blockHost(char *host) void deleteHost(char *host) { - + replacehost(host, NULL, 1); } + void showHosts() { pid_t child = fork(); @@ -251,45 +296,4 @@ int main(int argc, char **argv) free_list(&hosts); } -/** - * Replace a host in the hosts file with the NEW host. - * - * @param oldhost - * @param newhost - * @param hostsFile - */ -void replacehost(char *oldhost, char *newhost) { - FILE *hostsFile = fopenHostsFile("r"); - char buf[256]; - char *ptr, *f; - char newHostsFile[4096]; - memset(newHostsFile, 0, 4096); - -// fseek(hostsFile, 0, SEEK_SET); - - while (fgets(buf, 256, hostsFile) != NULL) - { - f = strcasestr(buf, oldhost); - if (f != NULL) { - memset(buf, 0, sizeof(buf)); - sprintf(buf, "%s %s\n", blockString, newhost); - } - strcat(newHostsFile, buf); - memset(buf, 0, sizeof(buf)); - } - - // Ensure we have an EOF at the end of the file. - newHostsFile[strlen(newHostsFile)] = EOF; - -// // Seek to 0 -// fseek(hostsFile, 0, SEEK_SET); - fclose(hostsFile); - - // If this failed, write an error message to stderr -// ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) { -// fprintf(stderr, "Did not write anything!\n"); -// } - fprintf(stderr, "New hosts file: \n%s\n", newHostsFile); -// fclose(hostsFile); -} -- 2.17.1 From fac37b8b40c8d3a2ebed32becf6b0c3f4b81f1e8 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 17 Jan 2020 08:12:05 -0500 Subject: [PATCH 2/3] Cleanup, renaming other fn to be general. --- main.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/main.c b/main.c index 47c0377..9afed09 100644 --- a/main.c +++ b/main.c @@ -50,7 +50,7 @@ FILE *fopenHostsFile(char *mode) * @param newhost * @param hostsFile */ -void replacehost(char *oldhost, char *newhost, int deleteHost) { +void modifyHostsFile(char *oldhost, char *newhost, int deleteHost) { FILE *hostsFile = fopenHostsFile("r"); char buf[256]; char *ptr, *f; @@ -109,9 +109,12 @@ void blockHost(char *host) void deleteHost(char *host) { - replacehost(host, NULL, 1); + modifyHostsFile(host, NULL, 1); } +void replaceHost(char *oldhost, char *newhost) { + modifyHostsFile(oldhost, newhost, 0); +} void showHosts() { @@ -244,11 +247,9 @@ int main(int argc, char **argv) } // Process our command line arguments. - for (int i = 0; i < argc; i++) - { + for (int i = 0; i < argc; i++) { // Opens a hosts file in append mode, adding a host. - if (ARG_IS("add")) - { + if (ARG_IS("add")) { if (argc < 3) { printf("Please provide a host!\n"); exit(1); @@ -256,25 +257,21 @@ int main(int argc, char **argv) blockHost(argv[++i]); } // Replaces a host - else if (ARG_IS("edit")) - { - replacehost(argv[i + 1], argv[i + 2]); + else if (ARG_IS("edit")) { + replacehost(argv[i + 1], argv[i + 2], 0); i += 2; } // Deletes a host. - else if (ARG_IS("delete")) - { + else if (ARG_IS("delete")) { fprintf(stdout, "Soon to be implemented!\n"); } // Shows usage. - else if (ARG_IS("-h")) - { + else if (ARG_IS("-h")) { usage(); exit(0); } // Show the entire hosts file. - else if (ARG_IS("show")) - { + else if (ARG_IS("show")) { showHosts(); } else if (ARG_IS("-config")) { -- 2.17.1 From 31186418c3d5a168df0880220881e008c548aaf7 Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 17 Jan 2020 08:21:19 -0500 Subject: [PATCH 3/3] Cleanup, implements delete functionality --- main.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index 9afed09..ae1ab9c 100644 --- a/main.c +++ b/main.c @@ -14,13 +14,6 @@ #define ARG_IS(argname) (strcmp(argv[i], argname) == 0) -/** - * TODO: Add config file reading for daemonization. - * TODO: Allow daemonized process to call file reading/editing routines at will. - * TODO: Generalized routine for reading through a file and replacing a line. - * TODO: Generalized routine for reading a file. - */ - /** GLOBALS **/ // Our rule we use to blackhole domains const char *blockString = "0.0.0.0 "; @@ -107,8 +100,7 @@ void blockHost(char *host) fclose(hostsFile); } -void deleteHost(char *host) -{ +void deleteHost(char *host) { modifyHostsFile(host, NULL, 1); } @@ -163,7 +155,7 @@ int update_hosts_file() { return 0; } } - replacehost(ptr->data, buf); + replaceHost(ptr->data, buf); strcpy(ptr->data, buf); prev = ptr; ptr = ptr->next; @@ -258,12 +250,12 @@ int main(int argc, char **argv) } // Replaces a host else if (ARG_IS("edit")) { - replacehost(argv[i + 1], argv[i + 2], 0); + replaceHost(argv[i + 1], argv[i + 2]); i += 2; } // Deletes a host. else if (ARG_IS("delete")) { - fprintf(stdout, "Soon to be implemented!\n"); + deleteHost(argv[i+1]); } // Shows usage. else if (ARG_IS("-h")) { -- 2.17.1