parent
d2d092ed31
commit
50cdb303b9
2
Makefile
2
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 -lcurl -o build/rhosts
|
||||
gcc src/rhosts.c src/download.c -lcurl -o build/rhosts
|
||||
clean:
|
||||
- rm -rf build
|
||||
install: build
|
||||
|
|
|
|||
|
|
@ -0,0 +1,138 @@
|
|||
#ifndef DOWNLOAD_HEADER
|
||||
#include "download.h"
|
||||
#endif
|
||||
|
||||
// This will download entries from the config
|
||||
int download_entries(struct entry **entries){
|
||||
int i = (*entries)[0].entrytype;
|
||||
int rc = 0;
|
||||
FILE *tmpf;
|
||||
tmpf = fopen(TMPLOCATION,"a");
|
||||
if (tmpf == NULL){
|
||||
return 1;
|
||||
}
|
||||
FILE *tmpdf;
|
||||
tmpdf = fopen(TMPDOWNLOADLOCATION,"w+");
|
||||
if (tmpdf == NULL){
|
||||
printf("Failed to open %s\n",TMPDOWNLOADLOCATION);
|
||||
fclose(tmpf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
for (;i >0 ; i--){
|
||||
if ((*entries)[i].entrytype == CONTENTTYPE_DOWNLOAD){
|
||||
printf("Download: %s\n",(*entries)[i].entry);
|
||||
rc = fprintf(tmpf, "# rhosts download - %s", \
|
||||
(*entries)[i].entry);
|
||||
fflush(tmpf);
|
||||
if (rc == EOF){
|
||||
printf("Failed to write to %s\n", \
|
||||
TMPDOWNLOADLOCATION);
|
||||
fclose(tmpdf);
|
||||
fclose(tmpf);
|
||||
return 1;
|
||||
}
|
||||
download_libcurl((*entries)[i].entry);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(tmpf);
|
||||
fclose(tmpdf);
|
||||
remove(TMPDOWNLOADLOCATION);
|
||||
return 0;
|
||||
}
|
||||
// Uses libcurl to download and add file to tmpf
|
||||
int download_libcurl(char *e){
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
curl = curl_easy_init();
|
||||
if(curl){
|
||||
// Add the url
|
||||
curl_easy_setopt(curl, CURLOPT_URL, e);
|
||||
// Skip cert check
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
// Ignore if cert has a different HostName
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
// Send what is recieved to function
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parse_download);
|
||||
|
||||
// Download the file
|
||||
res = curl_easy_perform(curl);
|
||||
if(res != CURLE_OK){
|
||||
printf("Failed to download the file: %d\n", res);
|
||||
fflush(stdin);
|
||||
copy_old_download(e);
|
||||
return 1;
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
curl_global_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
// Parse what was downloaded
|
||||
int parse_download(char *buff, size_t size, size_t nmemb){
|
||||
FILE *tmpf;
|
||||
int i=0;
|
||||
int rc=0;
|
||||
tmpf = fopen(TMPLOCATION, "a");
|
||||
for(i=0;i<nmemb;i++){
|
||||
rc = fputc(buff[i], tmpf);
|
||||
if(rc == EOF)
|
||||
break;
|
||||
rc = i +1;
|
||||
}
|
||||
fclose(tmpf);
|
||||
return nmemb;
|
||||
}
|
||||
// Checks the hosts file for a download section matching the url
|
||||
// then copies it to the tmp file
|
||||
int copy_old_download(char *url){
|
||||
FILE *hostsf;
|
||||
FILE *tmpf;
|
||||
hostsf = fopen(HOSTSLOCATION, "r");
|
||||
if (hostsf == NULL){return 1;}
|
||||
tmpf = fopen(TMPLOCATION,"a");
|
||||
if (tmpf == NULL){
|
||||
fclose(hostsf);
|
||||
return 1;
|
||||
}
|
||||
char buff[MAXSTRSIZE] = "";
|
||||
char search[MAXSTRSIZE] = "# rhosts download - ";
|
||||
strncat(search,url,MAXSTRSIZE - 21);
|
||||
char c = '\0';
|
||||
|
||||
do{
|
||||
c = fgetc(hostsf);
|
||||
while(c != '\n' && c != EOF && strlen(buff) < MAXSTRSIZE){
|
||||
strncat(buff, &c, 1);
|
||||
c = fgetc(hostsf);
|
||||
}
|
||||
strncat(buff, &c, 1);
|
||||
if(strncmp(buff,search, strlen(search)) == 0){
|
||||
printf("Found a local match for %s\n",url);
|
||||
c = EOF;
|
||||
}
|
||||
buff[0] = '\0';
|
||||
}while(c !=EOF);
|
||||
do{
|
||||
do{
|
||||
c = fgetc(hostsf);
|
||||
if (c != EOF)
|
||||
strncat(buff, &c, 1);
|
||||
} while(c != '\n' && c != EOF && strlen(buff) < MAXSTRSIZE);
|
||||
if(strncmp(buff,"# rhosts", 8) != 0){
|
||||
fputs(buff, tmpf);
|
||||
}
|
||||
else
|
||||
c = EOF;
|
||||
buff[0] = '\0';
|
||||
}while(c !=EOF);
|
||||
|
||||
fclose(hostsf);
|
||||
fclose(tmpf);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef RHOSTS_HEADER
|
||||
#include "rhosts.h"
|
||||
#endif
|
||||
#ifndef DOWNLOAD_HEADER
|
||||
#define DOWNLOAD_HEADER
|
||||
#include <curl/curl.h>
|
||||
|
||||
int download_entries(struct entry **entries);
|
||||
int download_libcurl(char *e);
|
||||
int parse_download(char *buff, size_t size, size_t nmemb);
|
||||
int copy_old_download(char *url);
|
||||
#endif
|
||||
136
src/rhosts.c
136
src/rhosts.c
|
|
@ -1,5 +1,7 @@
|
|||
/* rhosts - Program used to maintain a blocklist within a hostfile */
|
||||
#ifndef RHOSTS_HEADER
|
||||
#include "rhosts.h"
|
||||
#endif
|
||||
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
|
|
@ -169,46 +171,6 @@ int preserve_static_entries(){
|
|||
return 0;
|
||||
}
|
||||
|
||||
// This will download entries from the config
|
||||
int download_entries(struct entry **entries){
|
||||
int i = (*entries)[0].entrytype;
|
||||
int rc = 0;
|
||||
FILE *tmpf;
|
||||
tmpf = fopen(TMPLOCATION,"a");
|
||||
if (tmpf == NULL){
|
||||
return 1;
|
||||
}
|
||||
FILE *tmpdf;
|
||||
tmpdf = fopen(TMPDOWNLOADLOCATION,"w+");
|
||||
if (tmpdf == NULL){
|
||||
printf("Failed to open %s\n",TMPDOWNLOADLOCATION);
|
||||
fclose(tmpf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
for (;i >0 ; i--){
|
||||
if ((*entries)[i].entrytype == CONTENTTYPE_DOWNLOAD){
|
||||
printf("Download: %s\n",(*entries)[i].entry);
|
||||
rc = fprintf(tmpf, "# rhosts download - %s", \
|
||||
(*entries)[i].entry);
|
||||
fflush(tmpf);
|
||||
if (rc == EOF){
|
||||
printf("Failed to write to %s\n", \
|
||||
TMPDOWNLOADLOCATION);
|
||||
fclose(tmpdf);
|
||||
fclose(tmpf);
|
||||
return 1;
|
||||
}
|
||||
download_libcurl((*entries)[i].entry);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(tmpf);
|
||||
fclose(tmpdf);
|
||||
remove(TMPDOWNLOADLOCATION);
|
||||
return 0;
|
||||
}
|
||||
int add_site_entries(struct entry **entries){
|
||||
int i = (*entries)[0].entrytype;
|
||||
int rc = 0;
|
||||
|
|
@ -244,100 +206,6 @@ int add_site_entries(struct entry **entries){
|
|||
fclose(tmpf);
|
||||
return 0;
|
||||
}
|
||||
// Uses libcurl to download and add file to tmpf
|
||||
int download_libcurl(char *e){
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
curl = curl_easy_init();
|
||||
if(curl){
|
||||
// Add the url
|
||||
curl_easy_setopt(curl, CURLOPT_URL, e);
|
||||
// Skip cert check
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
// Ignore if cert has a different HostName
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
// Send what is recieved to function
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parse_download);
|
||||
|
||||
// Download the file
|
||||
res = curl_easy_perform(curl);
|
||||
if(res != CURLE_OK){
|
||||
printf("Failed to download the file: %d\n", res);
|
||||
fflush(stdin);
|
||||
copy_old_download(e);
|
||||
return 1;
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
curl_global_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
// Parse what was downloaded
|
||||
int parse_download(char *buff, size_t size, size_t nmemb){
|
||||
FILE *tmpf;
|
||||
int i=0;
|
||||
int rc=0;
|
||||
tmpf = fopen(TMPLOCATION, "a");
|
||||
for(i=0;i<nmemb;i++){
|
||||
rc = fputc(buff[i], tmpf);
|
||||
if(rc == EOF)
|
||||
break;
|
||||
rc = i +1;
|
||||
}
|
||||
fclose(tmpf);
|
||||
return nmemb;
|
||||
}
|
||||
// Checks the hosts file for a download section matching the url
|
||||
// then copies it to the tmp file
|
||||
int copy_old_download(char *url){
|
||||
FILE *hostsf;
|
||||
FILE *tmpf;
|
||||
hostsf = fopen(HOSTSLOCATION, "r");
|
||||
if (hostsf == NULL){return 1;}
|
||||
tmpf = fopen(TMPLOCATION,"a");
|
||||
if (tmpf == NULL){
|
||||
fclose(hostsf);
|
||||
return 1;
|
||||
}
|
||||
char buff[MAXSTRSIZE] = "";
|
||||
char search[MAXSTRSIZE] = "# rhosts download - ";
|
||||
strncat(search,url,MAXSTRSIZE - 21);
|
||||
char c = '\0';
|
||||
|
||||
do{
|
||||
c = fgetc(hostsf);
|
||||
while(c != '\n' && c != EOF && strlen(buff) < MAXSTRSIZE){
|
||||
strncat(buff, &c, 1);
|
||||
c = fgetc(hostsf);
|
||||
}
|
||||
strncat(buff, &c, 1);
|
||||
if(strncmp(buff,search, strlen(search)) == 0){
|
||||
printf("Found a local match for %s\n",url);
|
||||
c = EOF;
|
||||
}
|
||||
buff[0] = '\0';
|
||||
}while(c !=EOF);
|
||||
do{
|
||||
do{
|
||||
c = fgetc(hostsf);
|
||||
if (c != EOF)
|
||||
strncat(buff, &c, 1);
|
||||
} while(c != '\n' && c != EOF && strlen(buff) < MAXSTRSIZE);
|
||||
if(strncmp(buff,"# rhosts", 8) != 0){
|
||||
fputs(buff, tmpf);
|
||||
}
|
||||
else
|
||||
c = EOF;
|
||||
buff[0] = '\0';
|
||||
}while(c !=EOF);
|
||||
|
||||
fclose(hostsf);
|
||||
fclose(tmpf);
|
||||
return 0;
|
||||
}
|
||||
// Copies the contents of the temp file to the hosts file
|
||||
int copy_tmp_to_hosts(){
|
||||
FILE *tmpf;
|
||||
|
|
|
|||
14
src/rhosts.h
14
src/rhosts.h
|
|
@ -1,11 +1,11 @@
|
|||
#ifndef RHOSTS_HEADER
|
||||
|
||||
#ifndef RHOSTS_HEADER
|
||||
#define RHOSTS_HEADER
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <curl/curl.h>
|
||||
|
||||
#ifdef _WIN64
|
||||
#define TMPLOCATION "/tmp/rhosts"
|
||||
|
|
@ -19,10 +19,11 @@
|
|||
#define TMPDOWNLOADLOCATION "/tmp/rhostsdownload"
|
||||
#define HOSTSLOCATION "/etc/hosts"
|
||||
#define CONFIGFILE "/etc/rhosts/rhosts.cfg"
|
||||
#define MAXSTRSIZE 500
|
||||
#else
|
||||
#endif
|
||||
|
||||
#define STRUCTS_HEADER
|
||||
#define MAXSTRSIZE 500
|
||||
// entry types
|
||||
#define CONTENTTYPE_ERROR 5
|
||||
#define CONTENTTYPE_BLANK 0
|
||||
|
|
@ -41,10 +42,9 @@ int openfile(FILE **file, char *mode, char *location);
|
|||
int closefile(FILE **file, char *location);
|
||||
short int determine_config_entry_value(char *buff);
|
||||
int preserve_static_entries();
|
||||
int download_entries(struct entry **entries);
|
||||
int add_site_entries(struct entry **entries);
|
||||
int download_libcurl(char *e);
|
||||
int parse_download(char *buff, size_t size, size_t nmemb);
|
||||
int copy_old_download(char *url);
|
||||
int copy_tmp_to_hosts();
|
||||
#endif
|
||||
#ifndef DOWNLOAD_HEADER
|
||||
#include "download.h"
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue