fix handling of / as cgi prefix
[project/uhttpd.git] / main.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 #define _BSD_SOURCE
21 #define _GNU_SOURCE
22 #define _XOPEN_SOURCE 700
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26
27 #include <getopt.h>
28 #include <errno.h>
29 #include <netdb.h>
30 #include <signal.h>
31
32 #include <libubox/usock.h>
33
34 #include "uhttpd.h"
35 #include "tls.h"
36
37 char uh_buf[4096];
38
39 static int run_server(void)
40 {
41 uloop_init();
42 uh_setup_listeners();
43 uh_plugin_post_init();
44 uloop_run();
45
46 return 0;
47 }
48
49 static void uh_config_parse(void)
50 {
51 const char *path = conf.file;
52 FILE *c;
53 char line[512];
54 char *col1;
55 char *col2;
56 char *eol;
57
58 if (!path)
59 path = "/etc/httpd.conf";
60
61 c = fopen(path, "r");
62 if (!c)
63 return;
64
65 memset(line, 0, sizeof(line));
66
67 while (fgets(line, sizeof(line) - 1, c)) {
68 if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
69 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
70 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
71 !(eol = strchr(col2, '\n')) || (*eol++ = 0))
72 continue;
73
74 uh_auth_add(line, col1, col2);
75 } else if (!strncmp(line, "I:", 2)) {
76 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
77 !(eol = strchr(col1, '\n')) || (*eol++ = 0))
78 continue;
79
80 uh_index_add(strdup(col1));
81 } else if (!strncmp(line, "E404:", 5)) {
82 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
83 !(eol = strchr(col1, '\n')) || (*eol++ = 0))
84 continue;
85
86 conf.error_handler = strdup(col1);
87 }
88 else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
89 if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
90 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
91 !(eol = strchr(col2, '\n')) || (*eol++ = 0))
92 continue;
93
94 uh_interpreter_add(col1, col2);
95 }
96 }
97
98 fclose(c);
99 }
100
101 static int add_listener_arg(char *arg, bool tls)
102 {
103 char *host = NULL;
104 char *port = arg;
105 char *s;
106 int l;
107
108 s = strrchr(arg, ':');
109 if (s) {
110 host = arg;
111 port = s + 1;
112 *s = 0;
113 }
114
115 if (host && *host == '[') {
116 l = strlen(host);
117 if (l >= 2) {
118 host[l-1] = 0;
119 host++;
120 }
121 }
122
123 return uh_socket_bind(host, port, tls);
124 }
125
126 static int usage(const char *name)
127 {
128 fprintf(stderr,
129 "Usage: %s -p [addr:]port -h docroot\n"
130 " -f Do not fork to background\n"
131 " -c file Configuration file, default is '/etc/httpd.conf'\n"
132 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
133 #ifdef HAVE_TLS
134 " -s [addr:]port Like -p but provide HTTPS on this port\n"
135 " -C file ASN.1 server certificate file\n"
136 " -K file ASN.1 server private key file\n"
137 #endif
138 " -h directory Specify the document root, default is '.'\n"
139 " -E string Use given virtual URL as 404 error handler\n"
140 " -I string Use given filename as index for directories, multiple allowed\n"
141 " -S Do not follow symbolic links outside of the docroot\n"
142 " -D Do not allow directory listings, send 403 instead\n"
143 " -R Enable RFC1918 filter\n"
144 " -n count Maximum allowed number of concurrent script requests\n"
145 " -N count Maximum allowed number of concurrent connections\n"
146 #ifdef HAVE_LUA
147 " -l string URL prefix for Lua handler, default is '/lua'\n"
148 " -L file Lua handler script, omit to disable Lua\n"
149 #endif
150 #ifdef HAVE_UBUS
151 " -u string URL prefix for UBUS via JSON-RPC handler\n"
152 " -U file Override ubus socket path\n"
153 " -a Do not authenticate JSON-RPC requests against UBUS session api\n"
154 #endif
155 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
156 " -i .ext=path Use interpreter at path for files with the given extension\n"
157 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
158 " -T seconds Network timeout in seconds, default is 30\n"
159 " -k seconds HTTP keepalive timeout\n"
160 " -d string URL decode given string\n"
161 " -r string Specify basic auth realm\n"
162 " -m string MD5 crypt given string\n"
163 "\n", name
164 );
165 return 1;
166 }
167
168 static void init_defaults_pre(void)
169 {
170 conf.script_timeout = 60;
171 conf.network_timeout = 30;
172 conf.http_keepalive = 20;
173 conf.max_script_requests = 3;
174 conf.max_connections = 100;
175 conf.realm = "Protected Area";
176 conf.cgi_prefix = "/cgi-bin";
177 conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
178 }
179
180 static void init_defaults_post(void)
181 {
182 uh_index_add("index.html");
183 uh_index_add("index.htm");
184 uh_index_add("default.html");
185 uh_index_add("default.htm");
186
187 if (conf.cgi_prefix) {
188 char *str = malloc(strlen(conf.docroot) + strlen(conf.cgi_prefix) + 1);
189 strcpy(str, conf.docroot);
190 strcat(str, conf.cgi_prefix);
191 conf.cgi_docroot_path = str;
192 };
193 }
194
195 static void fixup_prefix(char *str)
196 {
197 int len;
198
199 if (!str || !str[0])
200 return;
201
202 len = strlen(str) - 1;
203
204 while (len >= 0 && str[len] == '/')
205 len--;
206
207 str[len + 1] = 0;
208 }
209
210 int main(int argc, char **argv)
211 {
212 bool nofork = false;
213 char *port;
214 int opt, ch;
215 int cur_fd;
216 int bound = 0;
217
218 #ifdef HAVE_TLS
219 int n_tls = 0;
220 const char *tls_key = NULL, *tls_crt = NULL;
221 #endif
222
223 BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
224
225 uh_dispatch_add(&cgi_dispatch);
226 init_defaults_pre();
227 signal(SIGPIPE, SIG_IGN);
228
229 while ((ch = getopt(argc, argv, "afSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:N:x:i:t:k:T:A:u:U:")) != -1) {
230 switch(ch) {
231 #ifdef HAVE_TLS
232 case 'C':
233 tls_crt = optarg;
234 break;
235
236 case 'K':
237 tls_key = optarg;
238 break;
239
240 case 's':
241 n_tls++;
242 /* fall through */
243 #else
244 case 'C':
245 case 'K':
246 case 's':
247 fprintf(stderr, "uhttpd: TLS support not compiled, "
248 "ignoring -%c\n", opt);
249 break;
250 #endif
251 case 'p':
252 optarg = strdup(optarg);
253 bound += add_listener_arg(optarg, (ch == 's'));
254 break;
255
256 case 'h':
257 if (!realpath(optarg, uh_buf)) {
258 fprintf(stderr, "Error: Invalid directory %s: %s\n",
259 optarg, strerror(errno));
260 exit(1);
261 }
262 conf.docroot = strdup(uh_buf);
263 break;
264
265 case 'E':
266 if (optarg[0] != '/') {
267 fprintf(stderr, "Error: Invalid error handler: %s\n",
268 optarg);
269 exit(1);
270 }
271 conf.error_handler = optarg;
272 break;
273
274 case 'I':
275 if (optarg[0] == '/') {
276 fprintf(stderr, "Error: Invalid index page: %s\n",
277 optarg);
278 exit(1);
279 }
280 uh_index_add(optarg);
281 break;
282
283 case 'S':
284 conf.no_symlinks = 1;
285 break;
286
287 case 'D':
288 conf.no_dirlists = 1;
289 break;
290
291 case 'R':
292 conf.rfc1918_filter = 1;
293 break;
294
295 case 'n':
296 conf.max_script_requests = atoi(optarg);
297 break;
298
299 case 'N':
300 conf.max_connections = atoi(optarg);
301 break;
302
303 case 'x':
304 fixup_prefix(optarg);
305 conf.cgi_prefix = optarg;
306 break;
307
308 case 'i':
309 optarg = strdup(optarg);
310 port = strchr(optarg, '=');
311 if (optarg[0] != '.' || !port) {
312 fprintf(stderr, "Error: Invalid interpreter: %s\n",
313 optarg);
314 exit(1);
315 }
316
317 *port++ = 0;
318 uh_interpreter_add(optarg, port);
319 break;
320
321 case 't':
322 conf.script_timeout = atoi(optarg);
323 break;
324
325 case 'T':
326 conf.network_timeout = atoi(optarg);
327 break;
328
329 case 'k':
330 conf.http_keepalive = atoi(optarg);
331 break;
332
333 case 'A':
334 conf.tcp_keepalive = atoi(optarg);
335 break;
336
337 case 'f':
338 nofork = 1;
339 break;
340
341 case 'd':
342 optarg = strdup(optarg);
343 port = alloca(strlen(optarg) + 1);
344 if (!port)
345 return -1;
346
347 /* "decode" plus to space to retain compat */
348 for (opt = 0; optarg[opt]; opt++)
349 if (optarg[opt] == '+')
350 optarg[opt] = ' ';
351
352 /* opt now contains strlen(optarg) -- no need to re-scan */
353 if (uh_urldecode(port, opt, optarg, opt) < 0) {
354 fprintf(stderr, "uhttpd: invalid encoding\n");
355 return -1;
356 }
357
358 printf("%s", port);
359 return 0;
360 break;
361
362 /* basic auth realm */
363 case 'r':
364 conf.realm = optarg;
365 break;
366
367 /* md5 crypt */
368 case 'm':
369 printf("%s\n", crypt(optarg, "$1$"));
370 return 0;
371 break;
372
373 /* config file */
374 case 'c':
375 conf.file = optarg;
376 break;
377
378 #ifdef HAVE_LUA
379 case 'l':
380 conf.lua_prefix = optarg;
381 break;
382
383 case 'L':
384 conf.lua_handler = optarg;
385 break;
386 #else
387 case 'l':
388 case 'L':
389 fprintf(stderr, "uhttpd: Lua support not compiled, "
390 "ignoring -%c\n", opt);
391 break;
392 #endif
393 #ifdef HAVE_UBUS
394 case 'a':
395 conf.ubus_noauth = 1;
396 break;
397
398 case 'u':
399 conf.ubus_prefix = optarg;
400 break;
401
402 case 'U':
403 conf.ubus_socket = optarg;
404 break;
405 #else
406 case 'a':
407 case 'u':
408 case 'U':
409 fprintf(stderr, "uhttpd: UBUS support not compiled, "
410 "ignoring -%c\n", opt);
411 break;
412 #endif
413 default:
414 return usage(argv[0]);
415 }
416 }
417
418 uh_config_parse();
419
420 if (!conf.docroot) {
421 if (!realpath(".", uh_buf)) {
422 fprintf(stderr, "Error: Unable to determine work dir\n");
423 return 1;
424 }
425 conf.docroot = strdup(uh_buf);
426 }
427
428 init_defaults_post();
429
430 if (!bound) {
431 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
432 return 1;
433 }
434
435 #ifdef HAVE_TLS
436 if (n_tls) {
437 if (!tls_crt || !tls_key) {
438 fprintf(stderr, "Please specify a certificate and "
439 "a key file to enable SSL support\n");
440 return 1;
441 }
442
443 if (uh_tls_init(tls_key, tls_crt))
444 return 1;
445 }
446 #endif
447
448 #ifdef HAVE_LUA
449 if (conf.lua_handler || conf.lua_prefix) {
450 if (!conf.lua_handler || !conf.lua_prefix) {
451 fprintf(stderr, "Need handler and prefix to enable Lua support\n");
452 return 1;
453 }
454 if (uh_plugin_init("uhttpd_lua.so"))
455 return 1;
456 }
457 #endif
458 #ifdef HAVE_UBUS
459 if (conf.ubus_prefix && uh_plugin_init("uhttpd_ubus.so"))
460 return 1;
461 #endif
462
463 /* fork (if not disabled) */
464 if (!nofork) {
465 switch (fork()) {
466 case -1:
467 perror("fork()");
468 exit(1);
469
470 case 0:
471 /* daemon setup */
472 if (chdir("/"))
473 perror("chdir()");
474
475 cur_fd = open("/dev/null", O_WRONLY);
476 if (cur_fd > 0) {
477 dup2(cur_fd, 0);
478 dup2(cur_fd, 1);
479 dup2(cur_fd, 2);
480 }
481
482 break;
483
484 default:
485 exit(0);
486 }
487 }
488
489 return run_server();
490 }