2 7edc455a 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 7edc455a 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
5 7edc455a 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
6 7edc455a 2021-01-15 op * copyright notice and this permission notice appear in all copies.
8 7edc455a 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7edc455a 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7edc455a 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7edc455a 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7edc455a 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7edc455a 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7edc455a 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 7edc455a 2021-01-15 op #include <netdb.h>
19 881a9dd9 2021-01-16 op #include <err.h>
20 7edc455a 2021-01-15 op #include <errno.h>
21 7edc455a 2021-01-15 op #include <string.h>
23 7edc455a 2021-01-15 op #include "gmid.h"
26 7edc455a 2021-01-15 op * the inverse of this algorithm, i.e. starting from the start of the
27 7edc455a 2021-01-15 op * path + strlen(cgi), and checking if each component, should be
28 7edc455a 2021-01-15 op * faster. But it's tedious to write. This does the opposite: starts
29 7edc455a 2021-01-15 op * from the end and strip one component at a time, until either an
30 7edc455a 2021-01-15 op * executable is found or we emptied the path.
33 7edc455a 2021-01-15 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
36 7edc455a 2021-01-15 op end = strchr(path, '\0');
38 7edc455a 2021-01-15 op /* NB: assume CGI is enabled and path matches cgi */
40 7edc455a 2021-01-15 op while (end > path) {
41 7edc455a 2021-01-15 op /* go up one level. UNIX paths are simple and POSIX
42 7edc455a 2021-01-15 op * dirname, with its ambiguities on if the given path
43 7edc455a 2021-01-15 op * is changed or not, gives me headaches. */
44 7edc455a 2021-01-15 op while (*end != '/')
48 7edc455a 2021-01-15 op switch (check_path(c, path, &c->fd)) {
49 7edc455a 2021-01-15 op case FILE_EXECUTABLE:
50 7edc455a 2021-01-15 op return start_cgi(path, end+1, query, fds,c);
51 7edc455a 2021-01-15 op case FILE_MISSING:
62 7edc455a 2021-01-15 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
64 7edc455a 2021-01-15 op goodbye(fds, c);
69 7edc455a 2021-01-15 op start_cgi(const char *spath, const char *relpath, const char *query,
70 7edc455a 2021-01-15 op struct pollfd *fds, struct client *c)
72 881a9dd9 2021-01-16 op char addr[NI_MAXHOST];
73 881a9dd9 2021-01-16 op const char *ruser, *cissuer, *chash;
76 881a9dd9 2021-01-16 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
77 881a9dd9 2021-01-16 op addr, sizeof(addr),
79 881a9dd9 2021-01-16 op NI_NUMERICHOST);
83 881a9dd9 2021-01-16 op if (tls_peer_cert_provided(c->ctx)) {
84 881a9dd9 2021-01-16 op ruser = tls_peer_cert_subject(c->ctx);
85 881a9dd9 2021-01-16 op cissuer = tls_peer_cert_issuer(c->ctx);
86 881a9dd9 2021-01-16 op chash = tls_peer_cert_hash(c->ctx);
89 881a9dd9 2021-01-16 op cissuer = NULL;
93 881a9dd9 2021-01-16 op if (!send_string(exfd, spath)
94 881a9dd9 2021-01-16 op || !send_string(exfd, relpath)
95 881a9dd9 2021-01-16 op || !send_string(exfd, query)
96 881a9dd9 2021-01-16 op || !send_string(exfd, addr)
97 881a9dd9 2021-01-16 op || !send_string(exfd, ruser)
98 881a9dd9 2021-01-16 op || !send_string(exfd, cissuer)
99 881a9dd9 2021-01-16 op || !send_string(exfd, chash)
100 881a9dd9 2021-01-16 op || !send_vhost(exfd, c->host))
103 881a9dd9 2021-01-16 op close(c->fd);
104 881a9dd9 2021-01-16 op if ((c->fd = recv_fd(exfd)) == -1) {
105 881a9dd9 2021-01-16 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
107 881a9dd9 2021-01-16 op goodbye(fds, c);
110 881a9dd9 2021-01-16 op c->child = 1;
111 881a9dd9 2021-01-16 op c->state = S_SENDING;
112 881a9dd9 2021-01-16 op cgi_poll_on_child(fds, c);
113 881a9dd9 2021-01-16 op /* handle_cgi(fds, c); */
117 881a9dd9 2021-01-16 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
118 881a9dd9 2021-01-16 op err(1, "cannot talk to the executor process");
122 7edc455a 2021-01-15 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
126 7edc455a 2021-01-15 op if (c->waiting_on_child)
128 7edc455a 2021-01-15 op c->waiting_on_child = 1;
130 7edc455a 2021-01-15 op fds->events = POLLIN;
132 7edc455a 2021-01-15 op fd = fds->fd;
133 7edc455a 2021-01-15 op fds->fd = c->fd;
138 7edc455a 2021-01-15 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
142 7edc455a 2021-01-15 op if (!c->waiting_on_child)
144 7edc455a 2021-01-15 op c->waiting_on_child = 0;
146 7edc455a 2021-01-15 op fd = fds->fd;
147 7edc455a 2021-01-15 op fds->fd = c->fd;
152 7edc455a 2021-01-15 op handle_cgi(struct pollfd *fds, struct client *c)
156 7edc455a 2021-01-15 op /* ensure c->fd is the child and fds->fd the client */
157 7edc455a 2021-01-15 op cgi_poll_on_client(fds, c);
160 7edc455a 2021-01-15 op if (c->len == 0) {
161 7edc455a 2021-01-15 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
163 7edc455a 2021-01-15 op if (r == -1) {
164 7edc455a 2021-01-15 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
165 7edc455a 2021-01-15 op cgi_poll_on_child(fds, c);
174 7edc455a 2021-01-15 op while (c->len > 0) {
175 7edc455a 2021-01-15 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
179 7edc455a 2021-01-15 op case TLS_WANT_POLLOUT:
180 7edc455a 2021-01-15 op fds->events = POLLOUT;
183 7edc455a 2021-01-15 op case TLS_WANT_POLLIN:
184 7edc455a 2021-01-15 op fds->events = POLLIN;
196 7edc455a 2021-01-15 op goodbye(fds, c);