Blob


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