add preliminary cgi support, needs fixing for close handling
[project/uhttpd.git] / cgi.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
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, int fd)
40 {
41 struct interpreter *ip = pi->ip;
42 struct env_var *var;
43
44 dup2(fd, 0);
45 dup2(fd, 1);
46 close(fd);
47 clearenv();
48 setenv("PATH", conf.cgi_path, 1);
49
50 for (var = uh_get_process_vars(cl, pi); var->name; var++) {
51 if (!var->value)
52 continue;
53
54 setenv(var->name, var->value, 1);
55 }
56
57 chdir(pi->root);
58
59 if (ip)
60 execl(ip->path, ip->path, pi->phys, NULL);
61 else
62 execl(pi->phys, pi->phys, NULL);
63
64 printf("Status: 500 Internal Server Error\r\n\r\n"
65 "Unable to launch the requested CGI program:\n"
66 " %s: %s\n", ip ? ip->path : pi->phys, strerror(errno));
67 }
68
69 static void cgi_handle_request(struct client *cl, const char *url, struct path_info *pi)
70 {
71 unsigned int mode = S_IFREG | S_IXOTH;
72
73 if (!pi->ip && !((pi->stat.st_mode & mode) == mode)) {
74 uh_client_error(cl, 403, "Forbidden",
75 "You don't have permission to access %s on this server.",
76 url);
77 return;
78 }
79
80 if (!uh_create_process(cl, pi, cgi_main)) {
81 uh_client_error(cl, 500, "Internal Server Error",
82 "Failed to create CGI process: %s", strerror(errno));
83 return;
84 }
85
86 return;
87 }
88
89 static bool check_cgi_path(struct path_info *pi, const char *url)
90 {
91 struct interpreter *ip;
92 const char *path = pi->phys;
93 int path_len = strlen(path);
94
95 list_for_each_entry(ip, &interpreters, list) {
96 int len = strlen(ip->ext);
97
98 if (len >= path_len)
99 continue;
100
101 if (strcmp(path + path_len - len, ip->ext) != 0)
102 continue;
103
104 pi->ip = ip;
105 return true;
106 }
107
108 pi->ip = NULL;
109 return uh_path_match(conf.cgi_prefix, url);
110 }
111
112 struct dispatch_handler cgi_dispatch = {
113 .check_path = check_cgi_path,
114 .handle_request = cgi_handle_request,
115 };