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 9ffef532d812330c00bd1043c884f9e685787018
parent 8b9629bd2a0dae917cbe7e7db44417afc28064f6
Author: Christoph Lohmann <20h@r-36.net>
Date:   Wed, 26 Oct 2022 19:03:26 +0200

Add SERVER_LISTEN_NAME feature.

Diffstat:
Mgeomyidae.8 | 1+
Mhandlr.c | 17++++++++++-------
Mhandlr.h | 19+++++++++++++------
Mind.c | 3++-
Mind.h | 4++--
Mmain.c | 27+++++++++++++++++++--------
6 files changed, 47 insertions(+), 24 deletions(-)

diff --git a/geomyidae.8 b/geomyidae.8 @@ -381,6 +381,7 @@ Additionally to the above arguments several environment variables are set. .Dl SCRIPT_NAME = script which is executed .Dl SERVER_NAME = server's hostname .Dl SERVER_PORT = server's port +.Dl SERVER_LISTEN_NAME = ip the server received the connection on .Dl SERVER_PROTOCOL = `gopher/1.0' .Dl SERVER_SOFTWARE = `geomyidae' .Dl X_GOPHER_SEARCH = search (See above.) 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, int istls) + char *sear, char *ohost, char *chost, char *bhost, int istls) { char *pa, *file, *e, *par, *b; struct dirent **dirent; @@ -33,6 +33,7 @@ handledir(int sock, char *path, char *port, char *base, char *args, USED(args); USED(sear); + USED(bhost); pa = xstrdup(path); e = pa + strlen(pa) - 1; @@ -85,13 +86,14 @@ 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, int istls) + char *sear, char *ohost, char *chost, char *bhost, int istls) { Indexs *act; int i, ret = 0; USED(args); USED(sear); + USED(bhost); act = scanfile(file); if (act != NULL) { @@ -108,7 +110,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, int istls) + char *sear, char *ohost, char *chost, char *bhost, int istls) { int fd; @@ -117,6 +119,7 @@ handlebin(int sock, char *file, char *port, char *base, char *args, USED(args); USED(sear); USED(ohost); + USED(bhost); fd = open(file, O_RDONLY); if (fd >= 0) { @@ -128,7 +131,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, int istls) + char *sear, char *ohost, char *chost, char *bhost, int istls) { char *p, *path; @@ -164,7 +167,7 @@ handlecgi(int sock, char *file, char *port, char *base, char *args, } setcgienviron(p, file, port, base, args, sear, ohost, chost, - istls); + bhost, istls); if (execl(file, p, sear, args, ohost, port, (char *)NULL) == -1) { @@ -183,7 +186,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, int istls) + char *sear, char *ohost, char *chost, char *bhost, int istls) { FILE *fp; char *p, *path, *ln = NULL; @@ -225,7 +228,7 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, } setcgienviron(p, file, port, base, args, sear, ohost, chost, - istls); + bhost, istls); if (execl(file, p, sear, args, ohost, port, (char *)NULL) == -1) { diff --git a/handlr.h b/handlr.h @@ -17,20 +17,27 @@ * 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 + * bhost ... server IP the server received the connection to * 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, int istls); + char *sear, char *ohost, char *chost, char *bhost, + int istls); void handlegph(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost, int istls); + char *sear, char *ohost, char *chost, char *bhost, + int istls); void handlebin(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost, int istls); + char *sear, char *ohost, char *chost, char *bhost, + int istls); void handletxt(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost, int istls); + char *sear, char *ohost, char *chost, char *bhost, + int istls); void handlecgi(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost, int istls); + char *sear, char *ohost, char *chost, char *bhost, + int istls); void handledcgi(int sock, char *file, char *port, char *base, char *args, - char *sear, char *ohost, char *chost, int istls); + char *sear, char *ohost, char *chost, char *bhost, + int istls); #endif diff --git a/ind.c b/ind.c @@ -531,7 +531,7 @@ reverselookup(char *host) void setcgienviron(char *file, char *path, char *port, char *base, char *args, - char *sear, char *ohost, char *chost, int istls) + char *sear, char *ohost, char *chost, char *bhost, int istls) { /* * TODO: Clean environment from possible unsafe environment variables. @@ -569,6 +569,7 @@ setcgienviron(char *file, char *path, char *port, char *base, char *args, setenv("SCRIPT_NAME", file, 1); setenv("SERVER_NAME", ohost, 1); setenv("SERVER_PORT", port, 1); + setenv("SERVER_LISTEN_NAME", bhost, 1); setenv("SERVER_PROTOCOL", "gopher/1.0", 1); setenv("SERVER_SOFTWARE", "geomyidae", 1); 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 *, int); + char *, char *, int); }; filetype *gettype(char *filename); @@ -49,7 +49,7 @@ 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, - int istls); + char *bhost, int istls); char *humansize(off_t n); char *humantime(const time_t *clock); diff --git a/main.c b/main.c @@ -133,8 +133,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, - int istls) + char *port, char *clienth, char *clientp, char *serverh, + char *serverp, int nocgi, int istls) { struct stat dir; char recvc[1025], recvb[1025], path[1025], args[1025], argsc[1025], @@ -367,7 +367,7 @@ 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, istls); + clienth, serverh, istls); } } else { /* @@ -376,7 +376,7 @@ handlerequest(int sock, char *req, int rlen, char *base, char *ohost, */ if (!pathfallthrough && S_ISDIR(dir.st_mode)) { handledir(sock, path, port, base, args, sear, ohost, - clienth, istls); + clienth, serverh, istls); if (loglvl & DIRS) { logentry(clienth, clientp, recvc, "dir listing"); @@ -540,9 +540,9 @@ int main(int argc, char *argv[]) { struct addrinfo hints; - struct sockaddr_storage clt; + struct sockaddr_storage clt, slt; struct linger lingerie; - socklen_t cltlen; + socklen_t cltlen, sltlen; int sock, dofork = 1, inetf = AF_UNSPEC, usechroot = 0, nocgi = 0, errno_save, nbindips = 0, i, j, nlfdret, *lfdret, listfd, maxlfd, istls = 0, @@ -560,7 +560,7 @@ main(int argc, char *argv[]) #ifdef ENABLE_TLS *certfile = NULL, *keyfile = NULL, #endif /* ENABLE_TLS */ - byte0, recvb[1025]; + byte0, recvb[1025], serverh[NI_MAXHOST], serverp[NI_MAXSERV]; struct passwd *us = NULL; struct group *gr = NULL; #ifdef ENABLE_TLS @@ -858,6 +858,16 @@ main(int argc, char *argv[]) } } + sltlen = sizeof(slt); + serverh[0] = serverp[0] = '\0'; + if (getsockname(sock, (struct sockaddr *)&slt, &sltlen) == 0) { + getnameinfo((struct sockaddr *)&slt, sltlen, serverh, + sizeof(serverh), serverp, sizeof(serverp), + NI_NUMERICHOST|NI_NUMERICSERV); + } + if (!strncmp(serverh, "::ffff:", 7)) + memmove(serverh, serverh+7, strlen(serverh)-6); + if (getnameinfo((struct sockaddr *)&clt, cltlen, clienth, sizeof(clienth), clientp, sizeof(clientp), NI_NUMERICHOST|NI_NUMERICSERV)) { @@ -996,7 +1006,8 @@ main(int argc, char *argv[]) handlerequest(sock, recvb, rlen, base, ohost, sport, clienth, - clientp, nocgi, istls); + clientp, serverh, serverp, + nocgi, istls); if (!istls) { /*