make port number configurable

This commit is contained in:
Omar Polo 2020-11-18 09:12:27 +01:00
parent d431188c66
commit 721e232529
No known key found for this signature in database
GPG Key ID: 35F98C96A1786F0D
4 changed files with 36 additions and 5 deletions

View File

@ -1,3 +1,7 @@
2020-11-17 Omar Polo <op@omarpolo.com>
* gmid.c (main): add flag -p to change the port
2020-11-10 Omar Polo <op@omarpolo.com> 2020-11-10 Omar Polo <op@omarpolo.com>
* ChangeLog: 1.3 tagged, fixed ChangeLog format * ChangeLog: 1.3 tagged, fixed ChangeLog format

View File

@ -11,6 +11,7 @@
\[**-d**&nbsp;*docs*] \[**-d**&nbsp;*docs*]
\[**-k**&nbsp;*key.pem*] \[**-k**&nbsp;*key.pem*]
\[**-l**&nbsp;*logfile*] \[**-l**&nbsp;*logfile*]
\[**-p**&nbsp;*port*]
\[**-x**&nbsp;*cgi-bin*] \[**-x**&nbsp;*cgi-bin*]
# DESCRIPTION # DESCRIPTION
@ -76,6 +77,10 @@ The options are as follows:
> log to the given file instead of the standard error. > log to the given file instead of the standard error.
**-p** *port*
> The port to bind to, by default 1965.
**-x** *dir* **-x** *dir*
> Enable execution of CGI scripts inside the given directory (relative > Enable execution of CGI scripts inside the given directory (relative

3
gmid.1
View File

@ -25,6 +25,7 @@
.Op Fl d Ar docs .Op Fl d Ar docs
.Op Fl k Ar key.pem .Op Fl k Ar key.pem
.Op Fl l Ar logfile .Op Fl l Ar logfile
.Op Fl p Ar port
.Op Fl x Ar cgi-bin .Op Fl x Ar cgi-bin
.Ek .Ek
.Sh DESCRIPTION .Sh DESCRIPTION
@ -79,6 +80,8 @@ The key for the certificate, by default is
.Pa key.pem . .Pa key.pem .
.It Fl l Ar logfile .It Fl l Ar logfile
log to the given file instead of the standard error. log to the given file instead of the standard error.
.It Fl p Ar port
The port to bind to, by default 1965.
.It Fl x Ar dir .It Fl x Ar dir
Enable execution of CGI scripts inside the given directory (relative Enable execution of CGI scripts inside the given directory (relative
to the document root.) Cannot be provided more than once. to the document root.) Cannot be provided more than once.

29
gmid.c
View File

@ -116,6 +116,7 @@ struct etm { /* file extension to mime */
const char *dir, *cgi; const char *dir, *cgi;
int dirfd, logfd; int dirfd, logfd;
int port;
int connected_clients; int connected_clients;
void siginfo_handler(int); void siginfo_handler(int);
@ -464,7 +465,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
goto err; goto err;
case 0: { /* child */ case 0: { /* child */
char *ex, *requri; char *ex, *requri, *portno;
char addr[INET_ADDRSTRLEN]; char addr[INET_ADDRSTRLEN];
char *argv[] = { NULL, NULL, NULL }; char *argv[] = { NULL, NULL, NULL };
@ -477,6 +478,9 @@ start_cgi(const char *spath, const char *relpath, const char *query,
if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL) if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
goto childerr; goto childerr;
if (asprintf(&portno, "%d", port) == -1)
goto childerr;
if (asprintf(&ex, "%s%s", dir, spath+1) == -1) if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
goto childerr; goto childerr;
@ -489,7 +493,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
/* fix the env */ /* fix the env */
setenv("SERVER_SOFTWARE", "gmid", 1); setenv("SERVER_SOFTWARE", "gmid", 1);
setenv("SERVER_PORT", "1965", 1); setenv("SERVER_PORT", portno, 1);
/* setenv("SERVER_NAME", "", 1); */ /* setenv("SERVER_NAME", "", 1); */
setenv("SCRIPT_NAME", spath, 1); setenv("SCRIPT_NAME", spath, 1);
setenv("SCRIPT_EXECUTABLE", ex, 1); setenv("SCRIPT_EXECUTABLE", ex, 1);
@ -943,7 +947,7 @@ usage(const char *me)
{ {
fprintf(stderr, fprintf(stderr,
"USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] " "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
"[-l logfile] [-x cgi-bin]\n", "[-l logfile] [-p port] [-x cgi-bin]\n",
me); me);
} }
@ -968,8 +972,9 @@ main(int argc, char **argv)
dir = "docs/"; dir = "docs/";
logfd = 2; /* stderr */ logfd = 2; /* stderr */
cgi = NULL; cgi = NULL;
port = 1965;
while ((ch = getopt(argc, argv, "c:d:hk:l:x:")) != -1) { while ((ch = getopt(argc, argv, "c:d:hk:l:p:x:")) != -1) {
switch (ch) { switch (ch) {
case 'c': case 'c':
cert = optarg; cert = optarg;
@ -994,6 +999,20 @@ main(int argc, char **argv)
err(1, "%s", optarg); err(1, "%s", optarg);
break; break;
case 'p': {
char *ep;
long lval;
errno = 0;
lval = strtol(optarg, &ep, 10);
if (optarg[0] == '\0' || *ep != '\0')
err(1, "not a number: %s", optarg);
if (lval < 0 || lval > UINT16_MAX)
err(1, "port number out of range: %s", optarg);
port = lval;
break;
}
case 'x': case 'x':
cgi = optarg; cgi = optarg;
break; break;
@ -1023,7 +1042,7 @@ main(int argc, char **argv)
if (tls_configure(ctx, conf) == -1) if (tls_configure(ctx, conf) == -1)
errx(1, "tls_configure: %s", tls_error(ctx)); errx(1, "tls_configure: %s", tls_error(ctx));
sock = make_socket(1965, AF_INET); sock = make_socket(port, AF_INET);
if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1) if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
err(1, "open: %s", dir); err(1, "open: %s", dir);