From 12201178a5950eecd9537e642b1246011490b499 Mon Sep 17 00:00:00 2001 From: Epoch Qwert Date: Tue, 21 Oct 2014 12:49:50 -0500 Subject: updated rebuild.sh to work better with libexec and bin instead of just bin. added epochttpd 2 --- src/bin/dcchelper.c | 20 +++++++++ src/bin/gethostbyname.c | 16 +++++++ src/bin/pad.c | 32 +++++++++++++ src/dcchelper.c | 20 --------- src/gethostbyname.c | 16 ------- src/libexec/httpd.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ src/pad.c | 32 ------------- 7 files changed, 185 insertions(+), 68 deletions(-) create mode 100644 src/bin/dcchelper.c create mode 100644 src/bin/gethostbyname.c create mode 100644 src/bin/pad.c delete mode 100644 src/dcchelper.c delete mode 100644 src/gethostbyname.c create mode 100644 src/libexec/httpd.c delete mode 100644 src/pad.c (limited to 'src') diff --git a/src/bin/dcchelper.c b/src/bin/dcchelper.c new file mode 100644 index 0000000..bf8f28b --- /dev/null +++ b/src/bin/dcchelper.c @@ -0,0 +1,20 @@ +#include + +int main(int argc,char *argv[]) { + char *wanip; + if(argc < 2) { + return printf("usage: %s [IP]\n",argv[0]); + } + if(!strncmp(argv[1],"127.",4)) { + wanip="127.0.0.1"; + } + else if(!strncmp(argv[1],"1.",2)) { + wanip="1.41.41.1"; + } + else if(!strncmp(argv[1],"192.168.0.",10)) { + wanip="192.168.0.2"; + } else { + wanip="98.159.69.172"; + } + return printf("%u\n",htonl(inet_addr(wanip))); +} diff --git a/src/bin/gethostbyname.c b/src/bin/gethostbyname.c new file mode 100644 index 0000000..646bf0a --- /dev/null +++ b/src/bin/gethostbyname.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc,char *argv[]) { + int i; + if(argc < 2) return 1; + struct hostent *he=gethostbyname(argv[1]); + for(i=0;he->h_addr_list[i];i++) { + printf("%u.%u.%u.%u\n", + (unsigned char)he->h_addr_list[i][0], + (unsigned char)he->h_addr_list[i][1], + (unsigned char)he->h_addr_list[i][2], + (unsigned char)he->h_addr_list[i][3]); + } + return 0; +} diff --git a/src/bin/pad.c b/src/bin/pad.c new file mode 100644 index 0000000..2f2ea13 --- /dev/null +++ b/src/bin/pad.c @@ -0,0 +1,32 @@ +#include +#include + +int main(int argc,char *argv[]) { + short in; + int i=0; + int width=argc>1?atoi(argv[1]):80; + char with=argc>2?argv[2][0]:' '; + char *line=argc>3?argv[3]:0; + if(width <= 0) width=80; + if(!line) { + while((in=fgetc(stdin)) != -1) { + if(in == '\n') { + for(;i%width || i == 0;i++) { + putchar(with); + } + i=-1; + } + i++; + putchar(in); + } + } else { + i=strlen(line); + } + if(i != 0 || line) { + if(line) fputs(line,stdout); + for(;i%width || i == 0;i++) { + putchar(with); + } + } + return 0; +} diff --git a/src/dcchelper.c b/src/dcchelper.c deleted file mode 100644 index bf8f28b..0000000 --- a/src/dcchelper.c +++ /dev/null @@ -1,20 +0,0 @@ -#include - -int main(int argc,char *argv[]) { - char *wanip; - if(argc < 2) { - return printf("usage: %s [IP]\n",argv[0]); - } - if(!strncmp(argv[1],"127.",4)) { - wanip="127.0.0.1"; - } - else if(!strncmp(argv[1],"1.",2)) { - wanip="1.41.41.1"; - } - else if(!strncmp(argv[1],"192.168.0.",10)) { - wanip="192.168.0.2"; - } else { - wanip="98.159.69.172"; - } - return printf("%u\n",htonl(inet_addr(wanip))); -} diff --git a/src/gethostbyname.c b/src/gethostbyname.c deleted file mode 100644 index 646bf0a..0000000 --- a/src/gethostbyname.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -int main(int argc,char *argv[]) { - int i; - if(argc < 2) return 1; - struct hostent *he=gethostbyname(argv[1]); - for(i=0;he->h_addr_list[i];i++) { - printf("%u.%u.%u.%u\n", - (unsigned char)he->h_addr_list[i][0], - (unsigned char)he->h_addr_list[i][1], - (unsigned char)he->h_addr_list[i][2], - (unsigned char)he->h_addr_list[i][3]); - } - return 0; -} diff --git a/src/libexec/httpd.c b/src/libexec/httpd.c new file mode 100644 index 0000000..4b14269 --- /dev/null +++ b/src/libexec/httpd.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +#define VHOST_ROOT "/var/www" +#define CGI "cgi-bin" +#define SERVER "epochttpd/2.0 (Unix)" + +void standard_headers() { + printf("Server: %s\n",SERVER); +} + +int main(int argc,char *argv[]) { + int fd; + char *name[10]; + int s,n; + short in; + char *method; + char *page; + char *version; + char *get_param; + char line[getpagesize()]; + fgets(line,sizeof(line)-1,stdin); + if(!strchr(line,'\n')) { + printf("HTTP/1.1 413 Entity Too Large\r\n"); + standard_headers(); + printf("Content-type: text/plain\r\n\r\n"); + printf("use smaller (<%d bytes) headers.\n",getpagesize()); + return 0; + } + if(strchr(line,'\r')) *strchr(line,'\r')=0; + method=strdup(line); + if((page=strchr(method,' '))) { + *page=0; + page++; + if((version=strchr(page,' '))) { + *version=0; + version++; + } + if((get_param=strchr(page,'?'))) { + *get_param=0; + get_param++; + setenv("QUERY_STRING",get_param); + } + } + while(fgets(line,sizeof(line)-1,stdin)) { + if(!strchr(line,'\n')) { + printf("HTTP/1.1 413 Entity Too Large\r\n"); + standard_headers(); + printf("Content-type: text/plain\r\n\r\n"); + printf("use smaller (<%d bytes) headers.\n",getpagesize()); + return 0; + } + if(strchr(line,'\r')) *strchr(line,'\r')=0; + if(!strncasecmp(line,"Host: ",6)) { + setenv("HTTP_HOST",line+6); + } + if(!strcmp(line,"")) { + break; + } + } + //TODO: sanitize this. + chdir(VHOST_ROOT); + chdir((char*)getenv("HTTP_HOST")); + if(strncmp(page,"/cgi-bin/",9)) { + for(;*page == '/';page++); + if(page[strlen(page)-1] == '/') { + printf("HTTP/1.1 302 Moved Permanently\r\n"); + standard_headers(); + printf("Location: /%sindex.html\r\n\r\n",page); + return 0; + } + if(fd=open(page,O_RDONLY) != -1) {//need to check that the file isn't a directory. :P + printf("HTTP/1.1 200 OK\r\n"); + standard_headers(); + name[0]="/usr/local/bin/mime-type"; + name[1]=page; + name[2]=0; + printf("Content-type: "); + fflush(stdout); + switch(fork()) { + case 0://child + execv(name[0],name); + break; + case -1://error + printf("fork failed.\n"); + break; + default://parent + break; + } + wait(&s); + fflush(stdout); + printf("\r\n"); + while((n=read(fd,line,sizeof(line))) > 0) { + write(1,line,n); + } + } else { + printf("HTTP/1.1 404 Not Found\r\n"); + standard_headers(); + printf("Content-type: text/plain\r\n\r\n"); + printf("Can't open that page.\n"); + } + } else { + //attempt to run this bastard! + chdir(CGI); + page+=strlen("/cgi-bin/"); + for(;*page == '/';page++); + name[0]=page; + name[1]=0; + printf("HTTP/1.1 200 OK\r\n"); + standard_headers(); + fflush(stdout); + execv(name[0],name); + } + return 0; +} diff --git a/src/pad.c b/src/pad.c deleted file mode 100644 index 2f2ea13..0000000 --- a/src/pad.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -int main(int argc,char *argv[]) { - short in; - int i=0; - int width=argc>1?atoi(argv[1]):80; - char with=argc>2?argv[2][0]:' '; - char *line=argc>3?argv[3]:0; - if(width <= 0) width=80; - if(!line) { - while((in=fgetc(stdin)) != -1) { - if(in == '\n') { - for(;i%width || i == 0;i++) { - putchar(with); - } - i=-1; - } - i++; - putchar(in); - } - } else { - i=strlen(line); - } - if(i != 0 || line) { - if(line) fputs(line,stdout); - for(;i%width || i == 0;i++) { - putchar(with); - } - } - return 0; -} -- cgit v1.2.3