Load plugins with RTLD_GLOBAL, fixes Lua library exports
[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 UBUS via JSON-RPC handler\n"
143 " -U file Override ubus socket path\n"
144 " -a Do not authenticate JSON-RPC requests against UBUS session api\n"
145 #endif
146 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
147 " -i .ext=path Use interpreter at path for files with the given extension\n"
148 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
149 " -T seconds Network timeout in seconds, default is 30\n"
150 " -k seconds HTTP keepalive timeout\n"
151 " -d string URL decode given string\n"
152 " -r string Specify basic auth realm\n"
153 " -m string MD5 crypt given string\n"
154 "\n", name
155 );
156 return 1;
157 }
158
159 static void init_defaults(void)
160 {
161 conf.script_timeout = 60;
162 conf.network_timeout = 30;
163 conf.http_keepalive = 20;
164 conf.max_script_requests = 3;
165 conf.max_connections = 100;
166 conf.realm = "Protected Area";
167 conf.cgi_prefix = "/cgi-bin";
168 conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
169
170 uh_index_add("index.html");
171 uh_index_add("index.htm");
172 uh_index_add("default.html");
173 uh_index_add("default.htm");
174 }
175
176 static void fixup_prefix(char *str)
177 {
178 int len;
179
180 if (!str || !str[0])
181 return;
182
183 len = strlen(str) - 1;
184
185 while (len > 0 && str[len] == '/')
186 len--;
187
188 str[len + 1] = 0;
189 }
190
191 int main(int argc, char **argv)
192 {
193 bool nofork = false;
194 char *port;
195 int opt, ch;
196 int cur_fd;
197 int bound = 0;
198
199 #ifdef HAVE_TLS
200 int n_tls = 0;
201 const char *tls_key = NULL, *tls_crt = NULL;
202 #endif
203
204 BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
205
206 uh_dispatch_add(&cgi_dispatch);
207 init_defaults();
208 signal(SIGPIPE, SIG_IGN);
209
210 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) {
211 switch(ch) {
212 #ifdef HAVE_TLS
213 case 'C':
214 tls_crt = optarg;
215 break;
216
217 case 'K':
218 tls_key = optarg;
219 break;
220
221 case 's':
222 n_tls++;
223 /* fall through */
224 #else
225 case 'C':
226 case 'K':
227 case 's':
228 fprintf(stderr, "uhttpd: TLS support not compiled, "
229 "ignoring -%c\n", opt);
230 break;
231 #endif
232 case 'p':
233 bound += add_listener_arg(optarg, (ch == 's'));
234 break;
235
236 case 'h':
237 if (!realpath(optarg, uh_buf)) {
238 fprintf(stderr, "Error: Invalid directory %s: %s\n",
239 optarg, strerror(errno));
240 exit(1);
241 }
242 conf.docroot = strdup(uh_buf);
243 break;
244
245 case 'E':
246 if (optarg[0] != '/') {
247 fprintf(stderr, "Error: Invalid error handler: %s\n",
248 optarg);
249 exit(1);
250 }
251 conf.error_handler = optarg;
252 break;
253
254 case 'I':
255 if (optarg[0] == '/') {
256 fprintf(stderr, "Error: Invalid index page: %s\n",
257 optarg);
258 exit(1);
259 }
260 uh_index_add(optarg);
261 break;
262
263 case 'S':
264 conf.no_symlinks = 1;
265 break;
266
267 case 'D':
268 conf.no_dirlists = 1;
269 break;
270
271 case 'R':
272 conf.rfc1918_filter = 1;
273 break;
274
275 case 'n':
276 conf.max_script_requests = atoi(optarg);
277 break;
278
279 case 'N':
280 conf.max_connections = atoi(optarg);
281 break;
282
283 case 'x':
284 fixup_prefix(optarg);
285 conf.cgi_prefix = optarg;
286 break;
287
288 case 'i':
289 port = strchr(optarg, '=');
290 if (optarg[0] != '.' || !port) {
291 fprintf(stderr, "Error: Invalid interpreter: %s\n",
292 optarg);
293 exit(1);
294 }
295
296 *port++ = 0;
297 uh_interpreter_add(optarg, port);
298 break;
299
300 case 't':
301 conf.script_timeout = atoi(optarg);
302 break;
303
304 case 'T':
305 conf.network_timeout = atoi(optarg);
306 break;
307
308 case 'k':
309 conf.http_keepalive = atoi(optarg);
310 break;
311
312 case 'A':
313 conf.tcp_keepalive = atoi(optarg);
314 break;
315
316 case 'f':
317 nofork = 1;
318 break;
319
320 case 'd':
321 port = alloca(strlen(optarg) + 1);
322 if (!port)
323 return -1;
324
325 /* "decode" plus to space to retain compat */
326 for (opt = 0; optarg[opt]; opt++)
327 if (optarg[opt] == '+')
328 optarg[opt] = ' ';
329
330 /* opt now contains strlen(optarg) -- no need to re-scan */
331 if (uh_urldecode(port, opt, optarg, opt) < 0) {
332 fprintf(stderr, "uhttpd: invalid encoding\n");
333 return -1;
334 }
335
336 printf("%s", port);
337 break;
338
339 /* basic auth realm */
340 case 'r':
341 conf.realm = optarg;
342 break;
343
344 /* md5 crypt */
345 case 'm':
346 printf("%s\n", crypt(optarg, "$1$"));
347 return 0;
348 break;
349
350 /* config file */
351 case 'c':
352 conf.file = optarg;
353 break;
354
355 #ifdef HAVE_LUA
356 case 'l':
357 conf.lua_prefix = optarg;
358 break;
359
360 case 'L':
361 conf.lua_handler = optarg;
362 break;
363 #else
364 case 'l':
365 case 'L':
366 fprintf(stderr, "uhttpd: Lua support not compiled, "
367 "ignoring -%c\n", opt);
368 break;
369 #endif
370 #ifdef HAVE_UBUS
371 case 'a':
372 conf.ubus_noauth = 1;
373 break;
374
375 case 'u':
376 conf.ubus_prefix = optarg;
377 break;
378
379 case 'U':
380 conf.ubus_socket = optarg;
381 break;
382 #else
383 case 'a':
384 case 'u':
385 case 'U':
386 fprintf(stderr, "uhttpd: UBUS support not compiled, "
387 "ignoring -%c\n", opt);
388 break;
389 #endif
390 default:
391 return usage(argv[0]);
392 }
393 }
394
395 uh_config_parse();
396
397 if (!bound) {
398 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
399 return 1;
400 }
401
402 if (!conf.docroot) {
403 if (!realpath(".", uh_buf)) {
404 fprintf(stderr, "Error: Unable to determine work dir\n");
405 return 1;
406 }
407 conf.docroot = strdup(uh_buf);
408 }
409
410 #ifdef HAVE_TLS
411 if (n_tls) {
412 if (!tls_crt || !tls_key) {
413 fprintf(stderr, "Please specify a certificate and "
414 "a key file to enable SSL support\n");
415 return 1;
416 }
417
418 if (uh_tls_init(tls_key, tls_crt))
419 return 1;
420 }
421 #endif
422
423 #ifdef HAVE_LUA
424 if (conf.lua_handler || conf.lua_prefix) {
425 if (!conf.lua_handler || !conf.lua_prefix) {
426 fprintf(stderr, "Need handler and prefix to enable Lua support\n");
427 return 1;
428 }
429 if (uh_plugin_init("uhttpd_lua.so"))
430 return 1;
431 }
432 #endif
433 #ifdef HAVE_UBUS
434 if (conf.ubus_prefix && uh_plugin_init("uhttpd_ubus.so"))
435 return 1;
436 #endif
437
438 /* fork (if not disabled) */
439 if (!nofork) {
440 switch (fork()) {
441 case -1:
442 perror("fork()");
443 exit(1);
444
445 case 0:
446 /* daemon setup */
447 if (chdir("/"))
448 perror("chdir()");
449
450 cur_fd = open("/dev/null", O_WRONLY);
451 if (cur_fd > 0) {
452 dup2(cur_fd, 0);
453 dup2(cur_fd, 1);
454 dup2(cur_fd, 2);
455 }
456
457 break;
458
459 default:
460 exit(0);
461 }
462 }
463
464 return run_server();
465 }