From 9cc630aa63cfd22553912b5a1fc41a71776cb272 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Wed, 28 Apr 2021 12:43:17 +0000 Subject: added ``env'' option to define environment vars for CGI scripts --- ChangeLog | 2 ++ ex.c | 5 +++++ gmid.1 | 9 ++++++++- gmid.c | 7 +++++++ gmid.h | 9 +++++++++ lex.l | 1 + parse.y | 13 ++++++++++++- 7 files changed, 44 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index fa5c71c..5437a4c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2021-04-27 Omar Polo + * parse.y (servopt): added ``env'' option to define environment vars for CGI scripts + * log.c (fatal): lower the log priority for fatal errors from CRIT to ERR 2021-04-25 Omar Polo diff --git a/ex.c b/ex.c index 73d990e..6437a42 100644 --- a/ex.c +++ b/ex.c @@ -136,6 +136,7 @@ launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost) char *ex, *pwd; char iribuf[GEMINI_URL_LEN]; char path[PATH_MAX]; + struct envlist *e; close(p[0]); if (dup2(p[1], 1) == -1) @@ -200,6 +201,10 @@ launch_cgi(struct iri *iri, struct cgireq *req, struct vhost *vhost) setenv_time("TLS_CLIENT_NOT_AFTER", req->notafter); setenv_time("TLS_CLIENT_NOT_BEFORE", req->notbefore); + TAILQ_FOREACH(e, &vhost->env, envs) { + safe_setenv(e->name, e->value); + } + strlcpy(path, ex, sizeof(path)); pwd = dirname(path); diff --git a/gmid.1 b/gmid.1 index 77473a4..cc42423 100644 --- a/gmid.1 +++ b/gmid.1 @@ -241,6 +241,13 @@ is set to Handle all the requests for the current virtual host using the CGI script at .Pa path . +.It Ic env Ar name Ar value +Set the environment variable +.Ar name +to +.Ar value +when executing CGI scripts. +Can be provided more than once. .It Ic index Ar string Set the directory index file. If not specified, it defaults to @@ -270,7 +277,7 @@ A .Ic location section may include most of the server configuration rules except -.Ic cert , Ic key , Ic root , Ic location , +.Ic cert , Ic env , Ic key , Ic root , Ic location , .Ic entrypoint No and Ic cgi . .It Ic root Pa directory Specify the root directory for this server. diff --git a/gmid.c b/gmid.c index ec22fe4..7100673 100644 --- a/gmid.c +++ b/gmid.c @@ -244,6 +244,7 @@ free_config(void) { struct vhost *h, *th; struct location *l, *tl; + struct envlist *e, *te; int v; v = conf.verbose; @@ -266,6 +267,12 @@ free_config(void) free(l); } + TAILQ_FOREACH_SAFE(e, &h->env, envs, te) { + free(e->name); + free(e->value); + free(e); + } + TAILQ_REMOVE(&hosts, h, vhosts); free(h); } diff --git a/gmid.h b/gmid.h index 6f4223e..f29c210 100644 --- a/gmid.h +++ b/gmid.h @@ -73,6 +73,13 @@ struct location { TAILQ_ENTRY(location) locations; }; +TAILQ_HEAD(envhead, envlist); +struct envlist { + char *name; + char *value; + TAILQ_ENTRY(envlist) envs; +}; + extern TAILQ_HEAD(vhosthead, vhost) hosts; struct vhost { const char *domain; @@ -89,6 +96,8 @@ struct vhost { * settings for the vhost, then follows the "real" location * rules as specified in the configuration. */ struct lochead locations; + + struct envhead env; }; struct etm { /* extension to mime */ diff --git a/lex.l b/lex.l index d61b994..e4b5e6d 100644 --- a/lex.l +++ b/lex.l @@ -60,6 +60,7 @@ chroot return TCHROOT; client return TCLIENT; default return TDEFAULT; entrypoint return TENTRYPOINT; +env return TENV; index return TINDEX; ipv6 return TIPV6; key return TKEY; diff --git a/parse.y b/parse.y index 0646f13..df62e81 100644 --- a/parse.y +++ b/parse.y @@ -59,7 +59,7 @@ void advance_loc(void); %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE %token TCHROOT TUSER TSERVER TPREFORK -%token TLOCATION TCERT TKEY TROOT TCGI TLANG TLOG TINDEX TAUTO +%token TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA %token TERR @@ -133,6 +133,17 @@ servopt : TCERT TSTRING { host->cert = ensure_absolute_path($2); } memmove($2, $2+1, strlen($2)); host->entrypoint = $2; } + | TENV TSTRING TSTRING { + struct envlist *e; + + e = xcalloc(1, sizeof(*e)); + e->name = $2; + e->value = $3; + if (TAILQ_EMPTY(&host->env)) + TAILQ_INSERT_HEAD(&host->env, e, envs); + else + TAILQ_INSERT_TAIL(&host->env, e, envs); + } | TKEY TSTRING { host->key = ensure_absolute_path($2); } | TROOT TSTRING { host->dir = ensure_absolute_path($2); } | locopt -- cgit v1.2.3