diff --git a/ChangeLog b/ChangeLog index 69df871..aa9a687 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2020-11-17 Omar Polo + + * gmid.c (main): add flag -p to change the port + 2020-11-10 Omar Polo * ChangeLog: 1.3 tagged, fixed ChangeLog format diff --git a/README.md b/README.md index 2d4ac46..a82bd4f 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ \[**-d** *docs*] \[**-k** *key.pem*] \[**-l** *logfile*] +\[**-p** *port*] \[**-x** *cgi-bin*] # DESCRIPTION @@ -76,6 +77,10 @@ The options are as follows: > log to the given file instead of the standard error. +**-p** *port* + +> The port to bind to, by default 1965. + **-x** *dir* > Enable execution of CGI scripts inside the given directory (relative diff --git a/gmid.1 b/gmid.1 index f25285f..1a8de5b 100644 --- a/gmid.1 +++ b/gmid.1 @@ -25,6 +25,7 @@ .Op Fl d Ar docs .Op Fl k Ar key.pem .Op Fl l Ar logfile +.Op Fl p Ar port .Op Fl x Ar cgi-bin .Ek .Sh DESCRIPTION @@ -79,6 +80,8 @@ The key for the certificate, by default is .Pa key.pem . .It Fl l Ar logfile 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 Enable execution of CGI scripts inside the given directory (relative to the document root.) Cannot be provided more than once. diff --git a/gmid.c b/gmid.c index 86a900a..87a4a62 100644 --- a/gmid.c +++ b/gmid.c @@ -116,6 +116,7 @@ struct etm { /* file extension to mime */ const char *dir, *cgi; int dirfd, logfd; +int port; int connected_clients; void siginfo_handler(int); @@ -464,7 +465,7 @@ start_cgi(const char *spath, const char *relpath, const char *query, goto err; case 0: { /* child */ - char *ex, *requri; + char *ex, *requri, *portno; char addr[INET_ADDRSTRLEN]; 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) goto childerr; + if (asprintf(&portno, "%d", port) == -1) + goto childerr; + if (asprintf(&ex, "%s%s", dir, spath+1) == -1) goto childerr; @@ -489,7 +493,7 @@ start_cgi(const char *spath, const char *relpath, const char *query, /* fix the env */ setenv("SERVER_SOFTWARE", "gmid", 1); - setenv("SERVER_PORT", "1965", 1); + setenv("SERVER_PORT", portno, 1); /* setenv("SERVER_NAME", "", 1); */ setenv("SCRIPT_NAME", spath, 1); setenv("SCRIPT_EXECUTABLE", ex, 1); @@ -943,7 +947,7 @@ usage(const char *me) { fprintf(stderr, "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); } @@ -968,8 +972,9 @@ main(int argc, char **argv) dir = "docs/"; logfd = 2; /* stderr */ 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) { case 'c': cert = optarg; @@ -994,6 +999,20 @@ main(int argc, char **argv) err(1, "%s", optarg); 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': cgi = optarg; break; @@ -1023,7 +1042,7 @@ main(int argc, char **argv) if (tls_configure(ctx, conf) == -1) 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) err(1, "open: %s", dir);