geomyidae

A small C-based gopherd. (gopher://bitreich.org/1/scm/geomyidae)
git clone git://r-36.net/geomyidae
Log | Files | Refs | README | LICENSE

commit 70f62c4a95799dd02259f41f5c7890052699f9f7
parent a59102fbee661d642dceda1cf561f036b54ea65e
Author: Christoph Lohmann <20h@r-36.net>
Date:   Sun, 27 Mar 2011 22:25:52 +0200

Merge branch 'master' of ssh://r-36.net/srv/git/geomyidae

Diffstat:
CGI | 14+++++++++++---
handlr.c | 77++++++++++++++++++++++++++++++++---------------------------------------------
handlr.h | 10+++++-----
ind.h | 2+-
main.c | 19+++++++++++++++----
5 files changed, 64 insertions(+), 58 deletions(-)

diff --git a/CGI b/CGI @@ -23,20 +23,24 @@ CALLING CONVENTION Geomyidae will call the script like this: - % $gopherroot/test.cgi $search $arguments + % $gopherroot/test.cgi $search $arguments $host $port When it is a plain request, the arguments will have these values: C: /test.cgi - -> $search = server host + -> $search = "" -> $arguments = "" + -> $host = server host + -> $port = server port If the request is for a type 7 search element, then the entered string by the user will be seen as following: C: /test.cgi searchterm (There is a Tab inbetween) -> $search = »searchterm« - -> $arguments = server host + -> $arguments = "" + -> $host = server host + -> $port = server port When you are trying to give your script some calling arguments, the syntax is: @@ -44,12 +48,16 @@ is: C: /test.cgi?hello -> $search = "" -> $arguments = »hello« + -> $host = server host + -> $port = server port If borth ways of input are combined, the variables are set as following: C: /test.cgi?hello=world searchterm (Beware! A Tab!) -> $search = »searchterm« -> $arguments = »hello=world« + -> $host = server host + -> $port = server port STANDARD CGI diff --git a/handlr.c b/handlr.c @@ -21,46 +21,33 @@ void handledir(int sock, char *path, char *port, char *base, char *args, - char *sear) + char *sear, char *ohost) { - char *pa, *file, *e, *addr, *par, *b; + char *pa, *file, *e, *par, *b; struct dirent **dirent; int ndir, i; struct stat st; filetype *type; USED(sear); - addr = nil; pa = gstrdup(path); e = strrchr(pa, '/'); if(e != nil) { *e = '\0'; - if(args == nil) { - addr = gmallocz(512, 2); - if(gethostname(addr, 512) == -1) { - perror("gethostname"); - free(addr); - free(pa); - return; - } - } else - addr = gstrdup(args); - par = gstrdup(pa); b = strrchr(par + strlen(base), '/'); if(b != nil) { *b = '\0'; tprintf(sock, "1..\t%s\t%s\t%s\r\n", - par + strlen(base), addr, port); + par + strlen(base), ohost, port); } free(par); ndir = scandir(pa, &dirent, 0, alphasort); if(ndir < 0) { perror("scandir"); - free(addr); free(pa); return; } else { @@ -77,7 +64,7 @@ handledir(int sock, char *path, char *port, char *base, char *args, type = gettype("index.gph"); e = file + strlen(base); tprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->type, - dirent[i]->d_name, e, addr, port); + dirent[i]->d_name, e, ohost, port); free(file); free(dirent[i]); } @@ -86,18 +73,15 @@ handledir(int sock, char *path, char *port, char *base, char *args, tprintf(sock, "\r\n"); } - if(addr != nil) - free(addr); free(pa); } void handlegph(int sock, char *file, char *port, char *base, char *args, - char *sear) + char *sear, char *ohost) { Indexs *act; int i; - char addr[512]; USED(base); USED(args); @@ -105,16 +89,8 @@ handlegph(int sock, char *file, char *port, char *base, char *args, act = scanfile(file); if(act != nil) { - if(args == nil) { - if(gethostname(addr, sizeof(addr)) == -1) { - perror("gethostname"); - return; - } - } else - snprintf(addr, sizeof(addr), "%s", args); - for(i = 0; i < act->num; i++) { - printelem(sock, act->n[i], addr, port); + printelem(sock, act->n[i], ohost, port); freeelem(act->n[i]); act->n[i] = nil; } @@ -126,7 +102,7 @@ handlegph(int sock, char *file, char *port, char *base, char *args, void handlebin(int sock, char *file, char *port, char *base, char *args, - char *sear) + char *sear, char *ohost) { char sendb[1024]; int len, fd; @@ -136,6 +112,7 @@ handlebin(int sock, char *file, char *port, char *base, char *args, USED(base); USED(args); USED(sear); + USED(ohost); fd = open(file, O_RDONLY); if(fd >= 0) { @@ -147,7 +124,7 @@ handlebin(int sock, char *file, char *port, char *base, char *args, void handlecgi(int sock, char *file, char *port, char *base, char *args, - char *sear) + char *sear, char *ohost) { char *p, *path; @@ -169,6 +146,8 @@ handlecgi(int sock, char *file, char *port, char *base, char *args, if(sear == nil) sear = ""; + if(args == nil) + args = ""; dup2(sock, 0); dup2(sock, 1); @@ -177,7 +156,7 @@ handlecgi(int sock, char *file, char *port, char *base, char *args, case 0: if (path != nil) chdir(path); - execl(file, p, sear, args, (char *)nil); + execl(file, p, sear, args, ohost, port, (char *)nil); case -1: break; default: @@ -192,9 +171,9 @@ handlecgi(int sock, char *file, char *port, char *base, char *args, void handledcgi(int sock, char *file, char *port, char *base, char *args, - char *sear) + char *sear, char *ohost) { - char *p, *ln, addr[512]; + char *p, *path, *ln; int outpipe[2]; Elems *el; @@ -203,20 +182,23 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, if(pipe(outpipe) < 0) return; + path = gstrdup(file); + p = strrchr(path, '/'); + if (p != nil) + p[1] = '\0'; + else { + free(path); + path = nil; + } + p = strrchr(file, '/'); if(p == nil) p = file; - if(args == nil) { - if(gethostname(addr, sizeof(addr)) == -1) { - perror("gethostname"); - return; - } - } else - snprintf(addr, sizeof(addr), "%s", args); - if(sear == nil) sear = ""; + if(args == nil) + args = ""; dup2(sock, 0); dup2(sock, 2); @@ -224,7 +206,9 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, case 0: dup2(outpipe[1], 1); close(outpipe[0]); - execl(file, p, sear, args, (char *)nil); + if (path != nil) + chdir(path); + execl(file, p, sear, args, ohost, port, (char *)nil); case -1: break; default: @@ -236,14 +220,17 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, if (el == nil) continue; - printelem(sock, el, addr, port); + printelem(sock, el, ohost, port); freeelem(el); } tprintf(sock, "\r\n.\r\n\r\n"); wait(NULL); + if (path != nil) + free(path); shutdown(sock, SHUT_RDWR); close(sock); break; } } + diff --git a/handlr.h b/handlr.h @@ -7,14 +7,14 @@ #define HANDLR_H void handledir(int sock, char *path, char *port, char *base, char *args, - char *sear); + char *sear, char *ohost); void handlegph(int sock, char *file, char *port, char *base, char *args, - char *sear); + char *sear, char *ohost); void handlebin(int sock, char *file, char *port, char *base, char *args, - char *sear); + char *sear, char *ohost); void handlecgi(int sock, char *file, char *port, char *base, char *args, - char *sear); + char *sear, char *ohost); void handledcgi(int sock, char *file, char *port, char *base, char *args, - char *sear); + char *sear, char *ohost); #endif diff --git a/ind.h b/ind.h @@ -27,7 +27,7 @@ typedef struct filetype filetype; struct filetype { char *end; char *type; - void (* f)(int, char *, char *, char *, char *, char *); + void (* f)(int, char *, char *, char *, char *, char *, char *); }; filetype *gettype(char *filename); diff --git a/main.c b/main.c @@ -137,6 +137,7 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth, bzero(&dir, sizeof(dir)); bzero(recvb, sizeof(recvb)); bzero(recvc, sizeof(recvc)); + args = nil; len = recv(sock, recvb, sizeof(recvb)-1, 0); if(len > 0) { @@ -164,8 +165,6 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth, args = strchr(recvb, '?'); if(args != nil) *args++ = '\0'; - else - args = ohost; securepath(recvb, len - 2); snprintf(path, sizeof(path), "%s%s", base, recvb); @@ -182,10 +181,10 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth, if(c == nil) c = path; type = gettype(c); - type->f(sock, path, port, base, args, sear); + type->f(sock, path, port, base, args, sear, ohost); } else { if(S_ISDIR(dir.st_mode)) { - handledir(sock, path, port, base, args, sear); + handledir(sock, path, port, base, args, sear, ohost); if(loglvl & DIRS) logentry(clienth, clientp, recvc, "dir listing"); @@ -307,6 +306,16 @@ main(int argc, char *argv[]) usage(); } ARGEND; + if(ohost == nil) { + ohost = gmallocz(513, 2); + if(gethostname(ohost, 512) < 0) { + perror("gethostname"); + free(ohost); + return 1; + } + } else + ohost = gstrdup(ohost); + if(group != nil) { if((gr = getgrnam(group)) == nil) { perror("no such group"); @@ -419,6 +428,8 @@ main(int argc, char *argv[]) close(listfd); if(logfile != nil) stoplogging(glfd); + free(ohost); + return 0; }