proc: expose HTTP_AUTH_USER and HTTP_AUTH_PASS
[project/uhttpd.git] / proc.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 #include <arpa/inet.h>
21 #include <libubox/blobmsg.h>
22 #include "uhttpd.h"
23
24 #define __headers \
25         __header(accept, accept) \
26         __header(accept_charset, accept-charset) \
27         __header(accept_encoding, accept-encoding) \
28         __header(accept_language, accept-language) \
29         __header(authorization, authorization) \
30         __header(connection, connection) \
31         __header(cookie, cookie) \
32         __header(host, host) \
33         __header(referer, referer) \
34         __header(user_agent, user-agent) \
35         __header(content_type, content-type) \
36         __header(content_length, content-length) \
37         __header(x_http_method_override, x-http-method-override) \
38         __header(http_auth_user, http-auth-user) \
39         __header(http_auth_pass, http-auth-pass)
40
41 #undef __header
42 #define __header __enum_header
43 enum client_hdr {
44         __headers
45         __HDR_MAX,
46 };
47
48 #undef __header
49 #define __header __blobmsg_header
50 static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
51         __headers
52 };
53
54 static const struct {
55         const char *name;
56         int idx;
57 } proc_header_env[] = {
58         { "HTTP_ACCEPT", HDR_accept },
59         { "HTTP_ACCEPT_CHARSET", HDR_accept_charset },
60         { "HTTP_ACCEPT_ENCODING", HDR_accept_encoding },
61         { "HTTP_ACCEPT_LANGUAGE", HDR_accept_language },
62         { "HTTP_AUTHORIZATION", HDR_authorization },
63         { "HTTP_CONNECTION", HDR_connection },
64         { "HTTP_COOKIE", HDR_cookie },
65         { "HTTP_HOST", HDR_host },
66         { "HTTP_REFERER", HDR_referer },
67         { "HTTP_USER_AGENT", HDR_user_agent },
68         { "HTTP_X_HTTP_METHOD_OVERRIDE", HDR_x_http_method_override },
69         { "HTTP_AUTH_USER", HDR_http_auth_user },
70         { "HTTP_AUTH_PASS", HDR_http_auth_pass },
71         { "CONTENT_TYPE", HDR_content_type },
72         { "CONTENT_LENGTH", HDR_content_length },
73 };
74
75 enum extra_vars {
76         /* no update needed */
77         _VAR_GW,
78         _VAR_SOFTWARE,
79
80         /* updated by uh_get_process_vars */
81         VAR_SCRIPT_NAME,
82         VAR_SCRIPT_FILE,
83         VAR_DOCROOT,
84         VAR_QUERY,
85         VAR_REQUEST,
86         VAR_PROTO,
87         VAR_METHOD,
88         VAR_PATH_INFO,
89         VAR_USER,
90         VAR_HTTPS,
91         VAR_REDIRECT,
92         VAR_SERVER_NAME,
93         VAR_SERVER_ADDR,
94         VAR_SERVER_PORT,
95         VAR_REMOTE_NAME,
96         VAR_REMOTE_ADDR,
97         VAR_REMOTE_PORT,
98
99         __VAR_MAX,
100 };
101
102 static char local_addr[INET6_ADDRSTRLEN], remote_addr[INET6_ADDRSTRLEN];
103 static char local_port[6], remote_port[6];
104 static char redirect_status[4];
105
106 static struct env_var extra_vars[] = {
107         [_VAR_GW] = { "GATEWAY_INTERFACE", "CGI/1.1" },
108         [_VAR_SOFTWARE] = { "SERVER_SOFTWARE", "uhttpd" },
109         [VAR_SCRIPT_NAME] = { "SCRIPT_NAME" },
110         [VAR_SCRIPT_FILE] = { "SCRIPT_FILENAME" },
111         [VAR_DOCROOT] = { "DOCUMENT_ROOT" },
112         [VAR_QUERY] = { "QUERY_STRING" },
113         [VAR_REQUEST] = { "REQUEST_URI" },
114         [VAR_PROTO] = { "SERVER_PROTOCOL" },
115         [VAR_METHOD] = { "REQUEST_METHOD" },
116         [VAR_PATH_INFO] = { "PATH_INFO" },
117         [VAR_USER] = { "REMOTE_USER" },
118         [VAR_HTTPS] = { "HTTPS" },
119         [VAR_REDIRECT] = { "REDIRECT_STATUS", redirect_status },
120         [VAR_SERVER_NAME] = { "SERVER_NAME", local_addr },
121         [VAR_SERVER_ADDR] = { "SERVER_ADDR", local_addr },
122         [VAR_SERVER_PORT] = { "SERVER_PORT", local_port },
123         [VAR_REMOTE_NAME] = { "REMOTE_HOST", remote_addr },
124         [VAR_REMOTE_ADDR] = { "REMOTE_ADDR", remote_addr },
125         [VAR_REMOTE_PORT] = { "REMOTE_PORT", remote_port },
126 };
127
128 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
129 {
130         struct http_request *req = &cl->request;
131         struct blob_attr *data = cl->hdr.head;
132         struct env_var *vars = (void *) uh_buf;
133         struct blob_attr *tb[__HDR_MAX];
134         const char *url;
135         int len;
136         int i;
137
138         url = blobmsg_data(blob_data(cl->hdr.head));
139         len = ARRAY_SIZE(proc_header_env);
140         len += ARRAY_SIZE(extra_vars);
141         len *= sizeof(struct env_var);
142
143         BUILD_BUG_ON(sizeof(uh_buf) < len);
144
145         extra_vars[VAR_SCRIPT_NAME].value = pi->name;
146         extra_vars[VAR_SCRIPT_FILE].value = pi->phys;
147         extra_vars[VAR_DOCROOT].value = pi->root;
148         extra_vars[VAR_QUERY].value = pi->query ? pi->query : "";
149         extra_vars[VAR_REQUEST].value = url;
150         extra_vars[VAR_PROTO].value = http_versions[req->version];
151         extra_vars[VAR_METHOD].value = http_methods[req->method];
152         extra_vars[VAR_PATH_INFO].value = pi->info;
153         extra_vars[VAR_USER].value = req->realm ? req->realm->user : NULL;
154         extra_vars[VAR_HTTPS].value = cl->tls ? "on" : NULL;
155
156         snprintf(redirect_status, sizeof(redirect_status),
157                  "%d", req->redirect_status);
158         inet_ntop(cl->srv_addr.family, &cl->srv_addr.in, local_addr, sizeof(local_addr));
159         snprintf(local_port, sizeof(local_port), "%d", cl->srv_addr.port);
160         inet_ntop(cl->peer_addr.family, &cl->peer_addr.in, remote_addr, sizeof(remote_addr));
161         snprintf(remote_port, sizeof(remote_port), "%d", cl->peer_addr.port);
162
163         blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data));
164         for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) {
165                 struct blob_attr *cur;
166
167                 cur = tb[proc_header_env[i].idx];
168                 vars[i].name = proc_header_env[i].name;
169                 vars[i].value = cur ? blobmsg_data(cur) : NULL;
170         }
171
172         memcpy(&vars[i], extra_vars, sizeof(extra_vars));
173         i += ARRAY_SIZE(extra_vars);
174         vars[i].name = NULL;
175         vars[i].value = NULL;
176
177         return vars;
178 }
179
180 static void proc_close_fds(struct client *cl)
181 {
182         struct dispatch_proc *p = &cl->dispatch.proc;
183
184         close(p->r.sfd.fd.fd);
185         if (p->wrfd.fd >= 0)
186                 close(p->wrfd.fd);
187 }
188
189 static void proc_handle_close(struct relay *r, int ret)
190 {
191         if (r->header_cb) {
192                 uh_client_error(r->cl, 502, "Bad Gateway",
193                                 "The process did not produce any response");
194                 return;
195         }
196
197         uh_request_done(r->cl);
198 }
199
200 static void proc_handle_header(struct relay *r, const char *name, const char *val)
201 {
202         static char status_buf[64];
203         struct client *cl = r->cl;
204         char *sep;
205         char buf[4];
206
207         if (!strcmp(name, "Status")) {
208                 sep = strchr(val, ' ');
209                 if (sep != val + 3)
210                         return;
211
212                 memcpy(buf, val, 3);
213                 buf[3] = 0;
214                 snprintf(status_buf, sizeof(status_buf), "%s", sep + 1);
215                 cl->dispatch.proc.status_msg = status_buf;
216                 cl->dispatch.proc.status_code = atoi(buf);
217                 return;
218         }
219
220         blobmsg_add_string(&cl->dispatch.proc.hdr, name, val);
221 }
222
223 static void proc_handle_header_end(struct relay *r)
224 {
225         struct client *cl = r->cl;
226         struct dispatch_proc *p = &cl->dispatch.proc;
227         struct blob_attr *cur;
228         int rem;
229
230         uloop_timeout_cancel(&p->timeout);
231         uh_http_header(cl, cl->dispatch.proc.status_code, cl->dispatch.proc.status_msg);
232         blob_for_each_attr(cur, cl->dispatch.proc.hdr.head, rem)
233                 ustream_printf(cl->us, "%s: %s\r\n", blobmsg_name(cur), blobmsg_data(cur));
234
235         ustream_printf(cl->us, "\r\n");
236
237         if (cl->request.method == UH_HTTP_MSG_HEAD)
238                 r->skip_data = true;
239 }
240
241 static void proc_write_close(struct client *cl)
242 {
243         struct dispatch_proc *p = &cl->dispatch.proc;
244
245         if (p->wrfd.fd < 0)
246                 return;
247
248         uloop_fd_delete(&p->wrfd);
249         close(p->wrfd.fd);
250         p->wrfd.fd = -1;
251 }
252
253 static void proc_free(struct client *cl)
254 {
255         struct dispatch_proc *p = &cl->dispatch.proc;
256
257         uloop_timeout_cancel(&p->timeout);
258         blob_buf_free(&p->hdr);
259         proc_write_close(cl);
260         uh_relay_free(&p->r);
261 }
262
263 static void proc_write_cb(struct uloop_fd *fd, unsigned int events)
264 {
265         struct client *cl = container_of(fd, struct client, dispatch.proc.wrfd);
266
267         client_poll_post_data(cl);
268 }
269
270 static void proc_relay_write_cb(struct client *cl)
271 {
272         struct dispatch_proc *p = &cl->dispatch.proc;
273
274         if (ustream_pending_data(cl->us, true))
275                 return;
276
277         ustream_set_read_blocked(&p->r.sfd.stream, false);
278         p->r.sfd.stream.notify_read(&p->r.sfd.stream, 0);
279 }
280
281 static int proc_data_send(struct client *cl, const char *data, int len)
282 {
283         struct dispatch_proc *p = &cl->dispatch.proc;
284         int retlen = 0;
285         int ret;
286
287         while (len) {
288                 ret = write(p->wrfd.fd, data, len);
289
290                 if (ret < 0) {
291                         if (errno == EINTR)
292                                 continue;
293
294                         if (errno == EAGAIN || errno == EWOULDBLOCK)
295                                 break;
296
297                         /* consume all data */
298                         ret = len;
299                 }
300
301                 if (!ret)
302                         break;
303
304                 retlen += ret;
305                 len -= ret;
306                 data += ret;
307         }
308
309         if (len)
310                 uloop_fd_add(&p->wrfd, ULOOP_WRITE);
311         else
312                 uloop_fd_delete(&p->wrfd);
313
314         return retlen;
315 }
316
317 static void proc_timeout_cb(struct uloop_timeout *timeout)
318 {
319         struct dispatch_proc *proc = container_of(timeout, struct dispatch_proc, timeout);
320         struct client *cl = container_of(proc, struct client, dispatch.proc);
321
322         uh_relay_kill(cl, &proc->r);
323 }
324
325 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
326                        void (*cb)(struct client *cl, struct path_info *pi, char *url))
327 {
328         struct dispatch *d = &cl->dispatch;
329         struct dispatch_proc *proc = &d->proc;
330         int rfd[2], wfd[2];
331         int pid;
332
333         blob_buf_init(&proc->hdr, 0);
334         proc->status_code = 200;
335         proc->status_msg = "OK";
336
337         if (pipe(rfd))
338                 return false;
339
340         if (pipe(wfd))
341                 goto close_rfd;
342
343         pid = fork();
344         if (pid < 0)
345                 goto close_wfd;
346
347         if (!pid) {
348                 close(0);
349                 close(1);
350
351                 dup2(rfd[1], 1);
352                 dup2(wfd[0], 0);
353
354                 close(rfd[0]);
355                 close(rfd[1]);
356                 close(wfd[0]);
357                 close(wfd[1]);
358
359                 uh_close_fds();
360                 cb(cl, pi, url);
361                 exit(0);
362         }
363
364         close(rfd[1]);
365         close(wfd[0]);
366
367         proc->wrfd.fd = wfd[1];
368         uh_relay_open(cl, &proc->r, rfd[0], pid);
369
370         d->free = proc_free;
371         d->close_fds = proc_close_fds;
372         d->data_send = proc_data_send;
373         d->data_done = proc_write_close;
374         d->write_cb = proc_relay_write_cb;
375         proc->r.header_cb = proc_handle_header;
376         proc->r.header_end = proc_handle_header_end;
377         proc->r.close = proc_handle_close;
378         proc->wrfd.cb = proc_write_cb;
379         proc->timeout.cb = proc_timeout_cb;
380         if (conf.script_timeout > 0)
381                 uloop_timeout_set(&proc->timeout, conf.script_timeout * 1000);
382
383         return true;
384
385 close_wfd:
386         close(wfd[0]);
387         close(wfd[1]);
388 close_rfd:
389         close(rfd[0]);
390         close(rfd[1]);
391
392         return false;
393 }