diff --git a/src/rhosts.c b/src/rhosts.c new file mode 100644 index 0000000..5e1a75f --- /dev/null +++ b/src/rhosts.c @@ -0,0 +1,82 @@ +/* rhosts - Program used to maintain a blocklist within a hostfile */ +#include "rhosts.h" + + +int main(int argc, char *argv[]){ + FILE *hostsfile; + FILE *tmpfile; + FILE *downloadfile; + FILE *configfile; + struct entry *entries; + int rc =0; + + rc = openfile(&hostsfile, "r+", HOSTSLOCATION); + if (rc != 0){return rc;} + rc = openfile(&tmpfile, "w+", TMPLOCATION); + if (rc != 0){return rc;} + rc = openfile(&downloadfile, "w+", TMPDOWNLOADLOCATION); + if (rc != 0){return rc;} + + parse_config(&entries); + + + // Closing files before exiting + + rc = closefile(&hostsfile, HOSTSLOCATION); + if (rc != 0){return rc;} + rc = closefile(&tmpfile, TMPLOCATION); + if (rc != 0){return rc;} + rc = remove(TMPLOCATION); + if (rc != 0){return rc;} + rc = closefile(&downloadfile, TMPDOWNLOADLOCATION); + if (rc != 0){return rc;} + rc = remove(TMPDOWNLOADLOCATION); + if (rc != 0){return rc;} + return 0; +} + + +int parse_config(struct entry **entries){ + int rc=0; + FILE *configfile; + configfile = fopen(CONFIGFILE, "r+"); + if (configfile == NULL){return 1;} + *entries = malloc(sizeof(struct entry)); + entries[0]->entrytype = 0; + char c='\0'; + char *buff = malloc(sizeof(char)); + short int valtyp = 1; + int *j = &entries[0]->entrytype; // Used to make easier to read + if (entries == NULL){return 1;} + do{ + c = getc(configfile); + buff = realloc(buff, sizeof(buff) + sizeof(char)); + buff[sizeof(buff)-1] = c; + if (c == ':' && valtyp == 1){ + *j += 1; + } + + + }while (c != '\0'); + + rc = fclose(configfile); + if (rc != 0){return 1;} + return 0; +} +int closefile(FILE **file, char *location){ + int rc = 0; + rc = fclose(*file); + if (rc != 0){ + printf("Failed to open %s\n", location); + return errno; + } + return 0; +} +int openfile(FILE **file, char *mode, char *location){ + *file = fopen(location, mode); + if (*file == NULL){ + printf("Failed to open %s\n", location); + return errno; + } + return 0; +} diff --git a/src/rhosts.h b/src/rhosts.h new file mode 100644 index 0000000..40fcb2e --- /dev/null +++ b/src/rhosts.h @@ -0,0 +1,35 @@ +#ifndef RHOSTS_HEADER + +#define RHOSTS_HEADER +#include +#include +#include +#include +#ifdef _WIN64 +#define TMPLOCATION "/tmp/rhosts" +#define TMPDOWNLOADLOCATION "/tmp/rhostsdownload" +#define HOSTSLOCATION "/Windows/System32/drivers/etc/hosts" +#define CONFIGFILE "/ProgramData/rhosts/rhosts.cfg" +#elif __APPLE__ +#define TMPLOCATION "/tmp/" +#elif __linux__ +#define TMPLOCATION "/tmp/rhosts" +#define TMPDOWNLOADLOCATION "/tmp/rhostsdownload" +#define HOSTSLOCATION "/etc/hosts" +#define CONFIGFILE "/etc/rhosts/rhosts.cfg" +#else +#endif + + +#define STATIC 0 + +struct entry{ + int entrytype; + char *entry; +}; + +int parse_config(struct entry **entries); + +int openfile(FILE **file, char *mode, char *location); +int closefile(FILE **file, char *location); +#endif