Blame


1 7edc455a 2021-01-15 op /*
2 7edc455a 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 7edc455a 2021-01-15 op *
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.
7 7edc455a 2021-01-15 op *
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.
15 7edc455a 2021-01-15 op */
16 7edc455a 2021-01-15 op
17 7edc455a 2021-01-15 op #include <netdb.h>
18 7edc455a 2021-01-15 op
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>
22 7edc455a 2021-01-15 op
23 7edc455a 2021-01-15 op #include "gmid.h"
24 7edc455a 2021-01-15 op
25 7edc455a 2021-01-15 op /*
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.
31 7edc455a 2021-01-15 op */
32 7edc455a 2021-01-15 op int
33 7edc455a 2021-01-15 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
34 7edc455a 2021-01-15 op {
35 7edc455a 2021-01-15 op char *end;
36 7edc455a 2021-01-15 op end = strchr(path, '\0');
37 7edc455a 2021-01-15 op
38 7edc455a 2021-01-15 op /* NB: assume CGI is enabled and path matches cgi */
39 7edc455a 2021-01-15 op
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 != '/')
45 7edc455a 2021-01-15 op end--;
46 7edc455a 2021-01-15 op *end = '\0';
47 7edc455a 2021-01-15 op
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:
52 7edc455a 2021-01-15 op break;
53 7edc455a 2021-01-15 op default:
54 7edc455a 2021-01-15 op goto err;
55 7edc455a 2021-01-15 op }
56 7edc455a 2021-01-15 op
57 7edc455a 2021-01-15 op *end = '/';
58 7edc455a 2021-01-15 op end--;
59 7edc455a 2021-01-15 op }
60 7edc455a 2021-01-15 op
61 7edc455a 2021-01-15 op err:
62 7edc455a 2021-01-15 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
63 7edc455a 2021-01-15 op return 0;
64 7edc455a 2021-01-15 op goodbye(fds, c);
65 7edc455a 2021-01-15 op return 0;
66 7edc455a 2021-01-15 op }
67 7edc455a 2021-01-15 op
68 7edc455a 2021-01-15 op int
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)
71 7edc455a 2021-01-15 op {
72 881a9dd9 2021-01-16 op char addr[NI_MAXHOST];
73 881a9dd9 2021-01-16 op const char *ruser, *cissuer, *chash;
74 881a9dd9 2021-01-16 op int e;
75 7edc455a 2021-01-15 op
76 881a9dd9 2021-01-16 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
77 881a9dd9 2021-01-16 op addr, sizeof(addr),
78 881a9dd9 2021-01-16 op NULL, 0,
79 881a9dd9 2021-01-16 op NI_NUMERICHOST);
80 881a9dd9 2021-01-16 op if (e != 0)
81 7edc455a 2021-01-15 op goto err;
82 7edc455a 2021-01-15 op
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);
87 881a9dd9 2021-01-16 op } else {
88 881a9dd9 2021-01-16 op ruser = NULL;
89 881a9dd9 2021-01-16 op cissuer = NULL;
90 881a9dd9 2021-01-16 op chash = NULL;
91 7edc455a 2021-01-15 op }
92 7edc455a 2021-01-15 op
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))
101 881a9dd9 2021-01-16 op goto err;
102 7edc455a 2021-01-15 op
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"))
106 881a9dd9 2021-01-16 op return 0;
107 881a9dd9 2021-01-16 op goodbye(fds, c);
108 7edc455a 2021-01-15 op return 0;
109 881a9dd9 2021-01-16 op }
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); */
114 7edc455a 2021-01-15 op return 0;
115 7edc455a 2021-01-15 op
116 881a9dd9 2021-01-16 op err:
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");
119 7edc455a 2021-01-15 op }
120 7edc455a 2021-01-15 op
121 7edc455a 2021-01-15 op void
122 7edc455a 2021-01-15 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
123 7edc455a 2021-01-15 op {
124 7edc455a 2021-01-15 op int fd;
125 7edc455a 2021-01-15 op
126 7edc455a 2021-01-15 op if (c->waiting_on_child)
127 7edc455a 2021-01-15 op return;
128 7edc455a 2021-01-15 op c->waiting_on_child = 1;
129 7edc455a 2021-01-15 op
130 7edc455a 2021-01-15 op fds->events = POLLIN;
131 7edc455a 2021-01-15 op
132 7edc455a 2021-01-15 op fd = fds->fd;
133 7edc455a 2021-01-15 op fds->fd = c->fd;
134 7edc455a 2021-01-15 op c->fd = fd;
135 7edc455a 2021-01-15 op }
136 7edc455a 2021-01-15 op
137 7edc455a 2021-01-15 op void
138 7edc455a 2021-01-15 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
139 7edc455a 2021-01-15 op {
140 7edc455a 2021-01-15 op int fd;
141 7edc455a 2021-01-15 op
142 7edc455a 2021-01-15 op if (!c->waiting_on_child)
143 7edc455a 2021-01-15 op return;
144 7edc455a 2021-01-15 op c->waiting_on_child = 0;
145 7edc455a 2021-01-15 op
146 7edc455a 2021-01-15 op fd = fds->fd;
147 7edc455a 2021-01-15 op fds->fd = c->fd;
148 7edc455a 2021-01-15 op c->fd = fd;
149 7edc455a 2021-01-15 op }
150 7edc455a 2021-01-15 op
151 7edc455a 2021-01-15 op void
152 7edc455a 2021-01-15 op handle_cgi(struct pollfd *fds, struct client *c)
153 7edc455a 2021-01-15 op {
154 7edc455a 2021-01-15 op ssize_t r;
155 7edc455a 2021-01-15 op
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);
158 7edc455a 2021-01-15 op
159 7edc455a 2021-01-15 op while (1) {
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)
162 7edc455a 2021-01-15 op goto end;
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);
166 7edc455a 2021-01-15 op return;
167 7edc455a 2021-01-15 op }
168 7edc455a 2021-01-15 op goto end;
169 7edc455a 2021-01-15 op }
170 7edc455a 2021-01-15 op c->len = r;
171 7edc455a 2021-01-15 op c->off = 0;
172 7edc455a 2021-01-15 op }
173 7edc455a 2021-01-15 op
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)) {
176 7edc455a 2021-01-15 op case -1:
177 7edc455a 2021-01-15 op goto end;
178 7edc455a 2021-01-15 op
179 7edc455a 2021-01-15 op case TLS_WANT_POLLOUT:
180 7edc455a 2021-01-15 op fds->events = POLLOUT;
181 7edc455a 2021-01-15 op return;
182 7edc455a 2021-01-15 op
183 7edc455a 2021-01-15 op case TLS_WANT_POLLIN:
184 7edc455a 2021-01-15 op fds->events = POLLIN;
185 7edc455a 2021-01-15 op return;
186 7edc455a 2021-01-15 op
187 7edc455a 2021-01-15 op default:
188 7edc455a 2021-01-15 op c->off += r;
189 7edc455a 2021-01-15 op c->len -= r;
190 7edc455a 2021-01-15 op break;
191 7edc455a 2021-01-15 op }
192 7edc455a 2021-01-15 op }
193 7edc455a 2021-01-15 op }
194 7edc455a 2021-01-15 op
195 7edc455a 2021-01-15 op end:
196 7edc455a 2021-01-15 op goodbye(fds, c);
197 7edc455a 2021-01-15 op }