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 89d4f0d0cf90543362a036133e88cb9fe21513d4
parent 3600ffac963bc5816c86b8e447feea77a2b34751
Author: Christoph Lohmann <20h@r-36.net>
Date:   Fri, 12 Jun 2020 20:17:01 +0200

Set GOPHERS and HTTPS in CGI, if TLS is used.

Add infrastructure, so other handlers could handle it too.

Diffstat:
geomyidae.8 | 1+
handlr.c | 16+++++++++-------
handlr.h | 11++++++-----
ind.c | 12+++++++++++-
ind.h | 5+++--
main.c | 9+++++----
6 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/geomyidae.8 b/geomyidae.8 @@ -372,6 +372,7 @@ Additionally to the above arguments several environment variables are set. .Dl SERVER_PROTOCOL = `gopher/1.0' .Dl SERVER_SOFTWARE = `geomyidae' .Dl X_GOPHER_SEARCH = search (See above.) +.Dl HTTPS and GOPHERS = set, if TLS is used .Pp .Ed . diff --git a/handlr.c b/handlr.c @@ -23,7 +23,7 @@ void handledir(int sock, char *path, char *port, char *base, char *args, - char *sear, char *ohost, char *chost) + char *sear, char *ohost, char *chost, int istls) { char *pa, *file, *e, *par, *b; struct dirent **dirent; @@ -80,7 +80,7 @@ handledir(int sock, char *path, char *port, char *base, char *args, void handlegph(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost) + char *sear, char *ohost, char *chost, int istls) { Indexs *act; int i, ret = 0; @@ -103,7 +103,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 *ohost, char *chost) + char *sear, char *ohost, char *chost, int istls) { int fd; @@ -123,7 +123,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 *ohost, char *chost) + char *sear, char *ohost, char *chost, int istls) { char *p, *path; @@ -158,7 +158,8 @@ handlecgi(int sock, char *file, char *port, char *base, char *args, break; } - setcgienviron(p, file, port, base, args, sear, ohost, chost); + setcgienviron(p, file, port, base, args, sear, ohost, chost, + istls); if (execl(file, p, sear, args, ohost, port, (char *)NULL) == -1) { @@ -177,7 +178,7 @@ 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 *ohost, char *chost) + char *sear, char *ohost, char *chost, int istls) { FILE *fp; char *p, *path, *ln = NULL; @@ -218,7 +219,8 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, break; } - setcgienviron(p, file, port, base, args, sear, ohost, chost); + setcgienviron(p, file, port, base, args, sear, ohost, chost, + istls); if (execl(file, p, sear, args, ohost, port, (char *)NULL) == -1) { diff --git a/handlr.h b/handlr.h @@ -17,17 +17,18 @@ * sear .... search part of request (»selector\tsearch\r\n«) * ohost ... host of geomiydae (See -h in geomyidae(8)) * chost ... IP of the client sending a request + * istls ... set to 1, if TLS was used for thr request */ void handledir(int sock, char *path, char *port, char *base, char *args, - char *sear, char *ohost, char *chost); + char *sear, char *ohost, char *chost, int istls); void handlegph(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost); + char *sear, char *ohost, char *chost, int istls); void handlebin(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost); + char *sear, char *ohost, char *chost, int istls); void handlecgi(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost); + char *sear, char *ohost, char *chost, int istls); void handledcgi(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost); + char *sear, char *ohost, char *chost, int istls); #endif diff --git a/ind.c b/ind.c @@ -465,7 +465,7 @@ reverselookup(char *host) void setcgienviron(char *file, char *path, char *port, char *base, char *args, - char *sear, char *ohost, char *chost) + char *sear, char *ohost, char *chost, int istls) { /* * TODO: Clean environment from possible unsafe environment variables. @@ -485,6 +485,7 @@ setcgienviron(char *file, char *path, char *port, char *base, char *args, * the script. The RFC allows us to set the IP to the value. */ setenv("REMOTE_HOST", chost, 1); + /* Please do not implement identd here. */ unsetenv("REMOTE_IDENT"); unsetenv("REMOTE_USER"); /* Make PHP happy. */ @@ -501,5 +502,14 @@ setcgienviron(char *file, char *path, char *port, char *base, char *args, setenv("SERVER_SOFTWARE", "geomyidae", 1); setenv("X_GOPHER_SEARCH", sear, 1); + + if (istls) { + setenv("GOPHERS", "on", 1); + setenv("HTTPS", "on", 1); + } else { + unsetenv("GOPHERS"); + unsetenv("HTTPS"); + } + } diff --git a/ind.h b/ind.h @@ -27,7 +27,7 @@ struct filetype { char *end; char *type; void (* f)(int, char *, char *, char *, char *, char *, char *, - char *); + char *, int); }; filetype *gettype(char *filename); @@ -48,7 +48,8 @@ void freeelem(Elems *e); char *smprintf(char *fmt, ...); char *reverselookup(char *host); void setcgienviron(char *file, char *path, char *port, char *base, - char *args, char *sear, char *ohost, char *chost); + char *args, char *sear, char *ohost, char *chost, + int istls); #endif diff --git a/main.c b/main.c @@ -121,7 +121,8 @@ logentry(char *host, char *port, char *qry, char *status) void handlerequest(int sock, char *req, int rlen, char *base, char *ohost, - char *port, char *clienth, char *clientp, int nocgi) + char *port, char *clienth, char *clientp, int nocgi, + int istls) { struct stat dir; char recvc[1025], recvb[1025], path[1025], *args = NULL, *sear, *c; @@ -247,12 +248,12 @@ handlerequest(int sock, char *req, int rlen, char *base, char *ohost, logentry(clienth, clientp, recvc, "nocgi error"); } else { type->f(sock, path, port, base, args, sear, ohost, - clienth); + clienth, istls); } } else { if (S_ISDIR(dir.st_mode)) { handledir(sock, path, port, base, args, sear, ohost, - clienth); + clienth, istls); if (loglvl & DIRS) { logentry(clienth, clientp, recvc, "dir listing"); @@ -863,7 +864,7 @@ main(int argc, char *argv[]) handlerequest(sock, recvb, rlen, base, ohost, sport, clienth, - clientp, nocgi); + clientp, nocgi, istls); if (!istls) { waitforpendingbytes(sock);