add support for deferring script requests, limit maximum number of script calls to...
[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
107 s = strrchr(arg, ':');
108 if (s) {
109 host = arg;
110 port = s + 1;
111 *s = 0;
112 }
113
114 return uh_socket_bind(host, port, tls);
115 }
116
117 static int usage(const char *name)
118 {
119 fprintf(stderr,
120 "Usage: %s -p [addr:]port -h docroot\n"
121 " -f Do not fork to background\n"
122 " -c file Configuration file, default is '/etc/httpd.conf'\n"
123 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
124 #ifdef HAVE_TLS
125 " -s [addr:]port Like -p but provide HTTPS on this port\n"
126 " -C file ASN.1 server certificate file\n"
127 " -K file ASN.1 server private key file\n"
128 #endif
129 " -h directory Specify the document root, default is '.'\n"
130 " -E string Use given virtual URL as 404 error handler\n"
131 " -I string Use given filename as index for directories, multiple allowed\n"
132 " -S Do not follow symbolic links outside of the docroot\n"
133 " -D Do not allow directory listings, send 403 instead\n"
134 " -R Enable RFC1918 filter\n"
135 " -n count Maximum allowed number of concurrent script requests\n"
136 " -N count Maximum allowed number of concurrent connections\n"
137 #ifdef HAVE_LUA
138 " -l string URL prefix for Lua handler, default is '/lua'\n"
139 " -L file Lua handler script, omit to disable Lua\n"
140 #endif
141 #ifdef HAVE_UBUS
142 " -u string URL prefix for HTTP/JSON handler\n"
143 " -U file Override ubus socket path\n"
144 #endif
145 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
146 " -i .ext=path Use interpreter at path for files with the given extension\n"
147 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
148 " -T seconds Network timeout in seconds, default is 30\n"
149 " -k seconds HTTP keepalive timeout\n"
150 " -d string URL decode given string\n"
151 " -r string Specify basic auth realm\n"
152 " -m string MD5 crypt given string\n"
153 "\n", name
154 );
155 return 1;
156 }
157
158 static void init_defaults(void)
159 {
160 conf.script_timeout = 60;
161 conf.network_timeout = 30;
162 conf.http_keepalive = 20;
163 conf.max_script_requests = 3;
164 conf.max_connections = 100;
165 conf.realm = "Protected Area";
166 conf.cgi_prefix = "/cgi-bin";
167 conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
168
169 uh_index_add("index.html");
170 uh_index_add("index.htm");
171 uh_index_add("default.html");
172 uh_index_add("default.htm");
173 }
174
175 static void fixup_prefix(char *str)
176 {
177 int len;
178
179 if (!str || !str[0])
180 return;
181
182 len = strlen(str) - 1;
183
184 while (len > 0 && str[len] == '/')
185 len--;
186
187 str[len + 1] = 0;
188 }
189
190 int main(int argc, char **argv)
191 {
192 const char *tls_key = NULL, *tls_crt = NULL;
193 bool nofork = false;
194 char *port;
195 int opt, ch;
196 int cur_fd;
197 int bound = 0;
198 int n_tls = 0;
199
200 BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
201
202 uh_dispatch_add(&cgi_dispatch);
203 init_defaults();
204 signal(SIGPIPE, SIG_IGN);
205
206 while ((ch = getopt(argc, argv, "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:N:x:i:t:k:T:A:u:U:")) != -1) {
207 bool tls = false;
208
209 switch(ch) {
210 case 's':
211 n_tls++;
212 tls = true;
213 /* fall through */
214 case 'p':
215 bound += add_listener_arg(optarg, tls);
216 break;
217
218 case 'h':
219 if (!realpath(optarg, uh_buf)) {
220 fprintf(stderr, "Error: Invalid directory %s: %s\n",
221 optarg, strerror(errno));
222 exit(1);
223 }
224 conf.docroot = strdup(uh_buf);
225 break;
226
227 case 'E':
228 if (optarg[0] != '/') {
229 fprintf(stderr, "Error: Invalid error handler: %s\n",
230 optarg);
231 exit(1);
232 }
233 conf.error_handler = optarg;
234 break;
235
236 case 'I':
237 if (optarg[0] == '/') {
238 fprintf(stderr, "Error: Invalid index page: %s\n",
239 optarg);
240 exit(1);
241 }
242 uh_index_add(optarg);
243 break;
244
245 case 'S':
246 conf.no_symlinks = 1;
247 break;
248
249 case 'D':
250 conf.no_dirlists = 1;
251 break;
252
253 case 'R':
254 conf.rfc1918_filter = 1;
255 break;
256
257 case 'n':
258 conf.max_script_requests = atoi(optarg);
259 break;
260
261 case 'N':
262 conf.max_connections = atoi(optarg);
263 break;
264
265 case 'x':
266 fixup_prefix(optarg);
267 conf.cgi_prefix = optarg;
268 break;
269
270 case 'i':
271 port = strchr(optarg, '=');
272 if (optarg[0] != '.' || !port) {
273 fprintf(stderr, "Error: Invalid interpreter: %s\n",
274 optarg);
275 exit(1);
276 }
277
278 *port++ = 0;
279 uh_interpreter_add(optarg, port);
280 break;
281
282 case 't':
283 conf.script_timeout = atoi(optarg);
284 break;
285
286 case 'T':
287 conf.network_timeout = atoi(optarg);
288 break;
289
290 case 'k':
291 conf.http_keepalive = atoi(optarg);
292 break;
293
294 case 'A':
295 conf.tcp_keepalive = atoi(optarg);
296 break;
297
298 case 'f':
299 nofork = 1;
300 break;
301
302 case 'd':
303 port = alloca(strlen(optarg) + 1);
304 if (!port)
305 return -1;
306
307 /* "decode" plus to space to retain compat */
308 for (opt = 0; optarg[opt]; opt++)
309 if (optarg[opt] == '+')
310 optarg[opt] = ' ';
311
312 /* opt now contains strlen(optarg) -- no need to re-scan */
313 if (uh_urldecode(port, opt, optarg, opt) < 0) {
314 fprintf(stderr, "uhttpd: invalid encoding\n");
315 return -1;
316 }
317
318 printf("%s", port);
319 break;
320
321 /* basic auth realm */
322 case 'r':
323 conf.realm = optarg;
324 break;
325
326 /* md5 crypt */
327 case 'm':
328 printf("%s\n", crypt(optarg, "$1$"));
329 return 0;
330 break;
331
332 /* config file */
333 case 'c':
334 conf.file = optarg;
335 break;
336
337 case 'C':
338 tls_crt = optarg;
339 break;
340
341 case 'K':
342 tls_key = optarg;
343 break;
344 #ifdef HAVE_LUA
345 case 'l':
346 conf.lua_prefix = optarg;
347 break;
348
349 case 'L':
350 conf.lua_handler = optarg;
351 break;
352 #endif
353 #ifdef HAVE_UBUS
354 case 'u':
355 conf.ubus_prefix = optarg;
356 break;
357
358 case 'U':
359 conf.ubus_socket = optarg;
360 break;
361 #endif
362 default:
363 return usage(argv[0]);
364 }
365 }
366
367 uh_config_parse();
368
369 if (!bound) {
370 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
371 return 1;
372 }
373
374 if (n_tls) {
375 if (!tls_crt || !tls_key) {
376 fprintf(stderr, "Please specify a certificate and "
377 "a key file to enable SSL support\n");
378 return 1;
379 }
380
381 #ifdef HAVE_TLS
382 if (uh_tls_init(tls_key, tls_crt))
383 return 1;
384 #else
385 fprintf(stderr, "Error: TLS support not compiled in.\n");
386 return 1;
387 #endif
388 }
389
390 #ifdef HAVE_LUA
391 if (conf.lua_handler || conf.lua_prefix) {
392 if (!conf.lua_handler || !conf.lua_prefix) {
393 fprintf(stderr, "Need handler and prefix to enable Lua support\n");
394 return 1;
395 }
396 if (uh_plugin_init("uhttpd_lua.so"))
397 return 1;
398 }
399 #endif
400 #ifdef HAVE_UBUS
401 if (conf.ubus_prefix && uh_plugin_init("uhttpd_ubus.so"))
402 return 1;
403 #endif
404
405 /* fork (if not disabled) */
406 if (!nofork) {
407 switch (fork()) {
408 case -1:
409 perror("fork()");
410 exit(1);
411
412 case 0:
413 /* daemon setup */
414 if (chdir("/"))
415 perror("chdir()");
416
417 cur_fd = open("/dev/null", O_WRONLY);
418 if (cur_fd > 0) {
419 dup2(cur_fd, 0);
420 dup2(cur_fd, 1);
421 dup2(cur_fd, 2);
422 }
423
424 break;
425
426 default:
427 exit(0);
428 }
429 }
430
431 return run_server();
432 }