62be0a376f77c6c3f31478c74377744a6379f27a
[project/uhttpd.git] / cgi.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 <libubox/blobmsg.h>
21 #include "uhttpd.h"
22
23 static LIST_HEAD(interpreters);
24
25 void uh_interpreter_add(const char *ext, const char *path)
26 {
27 struct interpreter *in;
28 char *new_ext, *new_path;
29
30 in = calloc_a(sizeof(*in),
31 &new_ext, strlen(ext) + 1,
32 &new_path, strlen(path) + 1);
33
34 in->ext = strcpy(new_ext, ext);
35 in->path = strcpy(new_path, path);
36 list_add_tail(&in->list, &interpreters);
37 }
38
39 static void cgi_main(struct client *cl, struct path_info *pi, char *url)
40 {
41 const struct interpreter *ip = pi->ip;
42 struct env_var *var;
43
44 clearenv();
45 setenv("PATH", conf.cgi_path, 1);
46
47 for (var = uh_get_process_vars(cl, pi); var->name; var++) {
48 if (!var->value)
49 continue;
50
51 setenv(var->name, var->value, 1);
52 }
53
54 chdir(pi->root);
55
56 if (ip)
57 execl(ip->path, ip->path, pi->phys, NULL);
58 else
59 execl(pi->phys, pi->phys, NULL);
60
61 printf("Status: 500 Internal Server Error\r\n\r\n"
62 "Unable to launch the requested CGI program:\n"
63 " %s: %s\n", ip ? ip->path : pi->phys, strerror(errno));
64 }
65
66 static void cgi_handle_request(struct client *cl, char *url, struct path_info *pi)
67 {
68 unsigned int mode = S_IFREG | S_IXOTH;
69
70 if (!pi->ip && !((pi->stat.st_mode & mode) == mode)) {
71 uh_client_error(cl, 403, "Forbidden",
72 "You don't have permission to access %s on this server.",
73 url);
74 return;
75 }
76
77 if (!uh_create_process(cl, pi, url, cgi_main)) {
78 uh_client_error(cl, 500, "Internal Server Error",
79 "Failed to create CGI process: %s", strerror(errno));
80 return;
81 }
82
83 return;
84 }
85
86 static bool check_cgi_path(struct path_info *pi, const char *url)
87 {
88 struct interpreter *ip;
89 const char *path = pi->phys;
90 int path_len = strlen(path);
91
92 list_for_each_entry(ip, &interpreters, list) {
93 int len = strlen(ip->ext);
94
95 if (len >= path_len)
96 continue;
97
98 if (strcmp(path + path_len - len, ip->ext) != 0)
99 continue;
100
101 pi->ip = ip;
102 return true;
103 }
104
105 pi->ip = NULL;
106 return uh_path_match(conf.cgi_docroot_path, pi->phys);
107 }
108
109 struct dispatch_handler cgi_dispatch = {
110 .script = true,
111 .check_path = check_cgi_path,
112 .handle_request = cgi_handle_request,
113 };