Merge branch 'feature/delete-host' of jacob.windle/hb into master

master
Jacob Windle 2020-01-17 13:24:26 +00:00 committed by Gitea
commit 76feb40f1a
1 changed files with 61 additions and 68 deletions

129
main.c
View File

@ -14,13 +14,6 @@
#define ARG_IS(argname) (strcmp(argv[i], argname) == 0) #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 **/ /** GLOBALS **/
// Our rule we use to blackhole domains // Our rule we use to blackhole domains
const char *blockString = "0.0.0.0 "; const char *blockString = "0.0.0.0 ";
@ -32,9 +25,6 @@ static char *HOSTFILE = "/etc/hosts";
static char *CONFIG; static char *CONFIG;
// Our configuration // Our configuration
void replacehost(char *oldhost, char *newhost);
/** /**
* Return an open handle to the hosts file. * Return an open handle to the hosts file.
* *
@ -46,6 +36,53 @@ FILE *fopenHostsFile(char *mode)
return fopen(HOSTFILE, mode); return fopen(HOSTFILE, mode);
} }
/**
* Replace a host in the hosts file with the NEW host.
*
* @param oldhost
* @param newhost
* @param hostsFile
*/
void modifyHostsFile(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 * Block a hosts using the hostsFile in FILE
*/ */
@ -63,9 +100,12 @@ void blockHost(char *host)
fclose(hostsFile); fclose(hostsFile);
} }
void deleteHost(char *host) void deleteHost(char *host) {
{ modifyHostsFile(host, NULL, 1);
}
void replaceHost(char *oldhost, char *newhost) {
modifyHostsFile(oldhost, newhost, 0);
} }
void showHosts() void showHosts()
@ -115,7 +155,7 @@ int update_hosts_file() {
return 0; return 0;
} }
} }
replacehost(ptr->data, buf); replaceHost(ptr->data, buf);
strcpy(ptr->data, buf); strcpy(ptr->data, buf);
prev = ptr; prev = ptr;
ptr = ptr->next; ptr = ptr->next;
@ -199,11 +239,9 @@ int main(int argc, char **argv)
} }
// Process our command line arguments. // 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. // Opens a hosts file in append mode, adding a host.
if (ARG_IS("add")) if (ARG_IS("add")) {
{
if (argc < 3) { if (argc < 3) {
printf("Please provide a host!\n"); printf("Please provide a host!\n");
exit(1); exit(1);
@ -211,25 +249,21 @@ int main(int argc, char **argv)
blockHost(argv[++i]); blockHost(argv[++i]);
} }
// Replaces a host // Replaces a host
else if (ARG_IS("edit")) else if (ARG_IS("edit")) {
{ replaceHost(argv[i + 1], argv[i + 2]);
replacehost(argv[i + 1], argv[i + 2]);
i += 2; i += 2;
} }
// Deletes a host. // Deletes a host.
else if (ARG_IS("delete")) else if (ARG_IS("delete")) {
{ deleteHost(argv[i+1]);
fprintf(stdout, "Soon to be implemented!\n");
} }
// Shows usage. // Shows usage.
else if (ARG_IS("-h")) else if (ARG_IS("-h")) {
{
usage(); usage();
exit(0); exit(0);
} }
// Show the entire hosts file. // Show the entire hosts file.
else if (ARG_IS("show")) else if (ARG_IS("show")) {
{
showHosts(); showHosts();
} }
else if (ARG_IS("-config")) { else if (ARG_IS("-config")) {
@ -251,45 +285,4 @@ int main(int argc, char **argv)
free_list(&hosts); 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);
}