This commit is contained in:
Omar Polo 2021-01-15 15:21:51 +00:00
parent dafb57b8af
commit f7b816dc39
3 changed files with 27 additions and 34 deletions

54
gmid.c
View File

@ -103,9 +103,9 @@ logs(int priority, struct client *c,
} else { } else {
len = sizeof(c->addr); len = sizeof(c->addr);
ec = getnameinfo((struct sockaddr*)&c->addr, len, ec = getnameinfo((struct sockaddr*)&c->addr, len,
hbuf, sizeof(hbuf), hbuf, sizeof(hbuf),
sbuf, sizeof(sbuf), sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV); NI_NUMERICHOST | NI_NUMERICSERV);
if (ec != 0) if (ec != 0)
fatal("getnameinfo: %s", gai_strerror(ec)); fatal("getnameinfo: %s", gai_strerror(ec));
} }
@ -147,9 +147,8 @@ starts_with(const char *str, const char *prefix)
int int
start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason) start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
{ {
char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */ char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
int len; int len;
int ret;
client->code = code; client->code = code;
client->meta = reason; client->meta = reason;
@ -157,18 +156,17 @@ start_reply(struct pollfd *pfd, struct client *client, int code, const char *rea
len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason); len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
assert(len < (int)sizeof(buf)); assert(len < (int)sizeof(buf));
ret = tls_write(client->ctx, buf, len);
if (ret == TLS_WANT_POLLIN) { switch (tls_write(client->ctx, buf, len)) {
case TLS_WANT_POLLIN:
pfd->events = POLLIN; pfd->events = POLLIN;
return 0; return 0;
} case TLS_WANT_POLLOUT:
if (ret == TLS_WANT_POLLOUT) {
pfd->events = POLLOUT; pfd->events = POLLOUT;
return 0; return 0;
default:
return 1;
} }
return 1;
} }
ssize_t ssize_t
@ -188,7 +186,7 @@ path_ext(const char *path)
{ {
const char *end; const char *end;
end = path + strlen(path)-1; /* the last byte before the NUL */ end = path + strlen(path)-1;
for (; end != path; --end) { for (; end != path; --end) {
if (*end == '.') if (*end == '.')
return end+1; return end+1;
@ -222,7 +220,7 @@ check_path(struct client *c, const char *path, int *fd)
assert(path != NULL); assert(path != NULL);
if ((*fd = openat(c->host->dirfd, *path ? path : ".", if ((*fd = openat(c->host->dirfd, *path ? path : ".",
O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) { O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
return FILE_MISSING; return FILE_MISSING;
} }
@ -283,7 +281,6 @@ err:
return 0; return 0;
} }
int int
open_file(char *fpath, char *query, struct pollfd *fds, struct client *c) open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
{ {
@ -337,7 +334,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
struct pollfd *fds, struct client *c) struct pollfd *fds, struct client *c)
{ {
pid_t pid; pid_t pid;
int p[2]; /* read end, write end */ int p[2]; /* read end, write end */
if (pipe(p) == -1) if (pipe(p) == -1)
goto err; goto err;
@ -346,7 +343,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
case -1: case -1:
goto err; goto err;
case 0: { /* child */ case 0: { /* child */
char *ex, *requri, *portno; char *ex, *requri, *portno;
char addr[NI_MAXHOST]; char addr[NI_MAXHOST];
char *argv[] = { NULL, NULL, NULL }; char *argv[] = { NULL, NULL, NULL };
@ -373,8 +370,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
goto childerr; goto childerr;
if (asprintf(&requri, "%s%s%s", spath, if (asprintf(&requri, "%s%s%s", spath,
*relpath == '\0' ? "" : "/", *relpath == '\0' ? "" : "/", relpath) == -1)
relpath) == -1)
goto childerr; goto childerr;
argv[0] = argv[1] = ex; argv[0] = argv[1] = ex;
@ -591,7 +587,7 @@ handle_handshake(struct pollfd *fds, struct client *c)
servname = tls_conn_servername(c->ctx); servname = tls_conn_servername(c->ctx);
if (servname == NULL) if (servname == NULL)
goto wronghost; goto hostnotfound;
for (h = hosts; h->domain != NULL; ++h) { for (h = hosts; h->domain != NULL; ++h) {
if (!strcmp(h->domain, servname) || !strcmp(h->domain, "*")) if (!strcmp(h->domain, servname) || !strcmp(h->domain, "*"))
@ -605,7 +601,7 @@ handle_handshake(struct pollfd *fds, struct client *c)
return; return;
} }
wronghost: hostnotfound:
/* XXX: check the correct response */ /* XXX: check the correct response */
if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI")) if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
return; return;
@ -651,9 +647,9 @@ handle_open_conn(struct pollfd *fds, struct client *c)
} }
LOGI(c, "GET %s%s%s", LOGI(c, "GET %s%s%s",
*iri.path ? iri.path : "/", *iri.path ? iri.path : "/",
*iri.query ? "?" : "", *iri.query ? "?" : "",
*iri.query ? iri.query : ""); *iri.query ? iri.query : "");
send_file(iri.path, iri.query, fds, c); send_file(iri.path, iri.query, fds, c);
} }
@ -810,16 +806,13 @@ do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
void void
goodbye(struct pollfd *pfd, struct client *c) goodbye(struct pollfd *pfd, struct client *c)
{ {
ssize_t ret;
c->state = S_CLOSING; c->state = S_CLOSING;
ret = tls_close(c->ctx); switch (tls_close(c->ctx)) {
if (ret == TLS_WANT_POLLIN) { case TLS_WANT_POLLIN:
pfd->events = POLLIN; pfd->events = POLLIN;
return; return;
} case TLS_WANT_POLLOUT:
if (ret == TLS_WANT_POLLOUT) {
pfd->events = POLLOUT; pfd->events = POLLOUT;
return; return;
} }
@ -1092,7 +1085,6 @@ main(int argc, char **argv)
else else
sock6 = -1; sock6 = -1;
if (!conf.foreground && daemon(0, 1) == -1) if (!conf.foreground && daemon(0, 1) == -1)
exit(1); exit(1);

3
gmid.h
View File

@ -92,7 +92,7 @@ struct client {
ssize_t len, off; /* mmap/static buffer */ ssize_t len, off; /* mmap/static buffer */
int af; int af;
struct sockaddr_storage addr; struct sockaddr_storage addr;
struct vhost *host; /* host he's talking to */ struct vhost *host; /* host she's talking to */
}; };
struct iri { struct iri {
@ -155,6 +155,7 @@ void load_vhosts(struct tls_config*);
void usage(const char*); void usage(const char*);
/* provided by lex/yacc */
extern FILE *yyin; extern FILE *yyin;
extern int yylineno; extern int yylineno;
extern int yyparse(void); extern int yyparse(void);

4
iri.c
View File

@ -97,12 +97,12 @@ parse_scheme(struct parser *p)
} }
*p->iri = '\0'; *p->iri = '\0';
if (*(++p->iri) != '/' || *(++p->iri) != '/') { if (p->iri[1] != '/' || p->iri[2] != '/') {
p->err = "invalid marker after scheme"; p->err = "invalid marker after scheme";
return 0; return 0;
} }
p->iri++; p->iri += 3;
return 1; return 1;
} }