added ``env'' option to define environment vars for CGI scripts

This commit is contained in:
Omar Polo 2021-04-28 12:43:17 +00:00
parent e6ca8eb156
commit 9cc630aa63
7 changed files with 44 additions and 2 deletions

View File

@ -1,5 +1,7 @@
2021-04-27 Omar Polo <op@omarpolo.com>
* 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 <op@omarpolo.com>

5
ex.c
View File

@ -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);

9
gmid.1
View File

@ -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.

7
gmid.c
View File

@ -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);
}

9
gmid.h
View File

@ -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 */

1
lex.l
View File

@ -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;

13
parse.y
View File

@ -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