get rid of -rdynamic on linking
[project/uhttpd.git] / main.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 #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
36 char uh_buf[4096];
37
38 static int run_server(void)
39 {
40         uloop_init();
41         uh_setup_listeners();
42         uloop_run();
43
44         return 0;
45 }
46
47 static void uh_config_parse(void)
48 {
49         const char *path = conf.file;
50         FILE *c;
51         char line[512];
52         char *col1;
53         char *col2;
54         char *eol;
55
56         if (!path)
57                 path = "/etc/httpd.conf";
58
59         c = fopen(path, "r");
60         if (!c)
61                 return;
62
63         memset(line, 0, sizeof(line));
64
65         while (fgets(line, sizeof(line) - 1, c)) {
66                 if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
67                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
68                                 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
69                                 !(eol = strchr(col2, '\n')) || (*eol++  = 0))
70                                 continue;
71
72                         uh_auth_add(line, col1, col2);
73                 } else if (!strncmp(line, "I:", 2)) {
74                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
75                                 !(eol = strchr(col1, '\n')) || (*eol++  = 0))
76                                 continue;
77
78                         uh_index_add(strdup(col1));
79                 } else if (!strncmp(line, "E404:", 5)) {
80                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
81                                 !(eol = strchr(col1, '\n')) || (*eol++  = 0))
82                                 continue;
83
84                         conf.error_handler = strdup(col1);
85                 }
86 #ifdef HAVE_CGI
87                 else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
88                         if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
89                                 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
90                                 !(eol = strchr(col2, '\n')) || (*eol++  = 0))
91                                 continue;
92
93                         if (!uh_interpreter_add(col1, col2))
94                                 fprintf(stderr,
95                                                 "Unable to add interpreter %s for extension %s: "
96                                                 "Out of memory\n", col2, col1
97                                 );
98                 }
99 #endif
100         }
101
102         fclose(c);
103 }
104
105 static int add_listener_arg(char *arg, bool tls)
106 {
107         char *host = NULL;
108         char *port = arg;
109         char *s;
110
111         s = strrchr(arg, ':');
112         if (s) {
113                 host = arg;
114                 port = s + 1;
115                 *s = 0;
116         }
117
118         return uh_socket_bind(host, port, tls);
119 }
120
121 static int usage(const char *name)
122 {
123         fprintf(stderr,
124                 "Usage: %s -p [addr:]port -h docroot\n"
125                 "       -f              Do not fork to background\n"
126                 "       -c file         Configuration file, default is '/etc/httpd.conf'\n"
127                 "       -p [addr:]port  Bind to specified address and port, multiple allowed\n"
128 #ifdef HAVE_TLS
129                 "       -s [addr:]port  Like -p but provide HTTPS on this port\n"
130                 "       -C file         ASN.1 server certificate file\n"
131                 "       -K file         ASN.1 server private key file\n"
132 #endif
133                 "       -h directory    Specify the document root, default is '.'\n"
134                 "       -E string       Use given virtual URL as 404 error handler\n"
135                 "       -I string       Use given filename as index for directories, multiple allowed\n"
136                 "       -S              Do not follow symbolic links outside of the docroot\n"
137                 "       -D              Do not allow directory listings, send 403 instead\n"
138                 "       -R              Enable RFC1918 filter\n"
139                 "       -n count        Maximum allowed number of concurrent requests\n"
140 #ifdef HAVE_LUA
141                 "       -l string       URL prefix for Lua handler, default is '/lua'\n"
142                 "       -L file         Lua handler script, omit to disable Lua\n"
143 #endif
144 #ifdef HAVE_UBUS
145                 "       -u string       URL prefix for HTTP/JSON handler\n"
146                 "       -U file         Override ubus socket path\n"
147 #endif
148                 "       -x string       URL prefix for CGI handler, default is '/cgi-bin'\n"
149                 "       -i .ext=path    Use interpreter at path for files with the given extension\n"
150                 "       -t seconds      CGI, Lua and UBUS script timeout in seconds, default is 60\n"
151                 "       -T seconds      Network timeout in seconds, default is 30\n"
152                 "       -d string       URL decode given string\n"
153                 "       -r string       Specify basic auth realm\n"
154                 "       -m string       MD5 crypt given string\n"
155                 "\n", name
156         );
157         return 1;
158 }
159
160 static void init_defaults(void)
161 {
162         conf.script_timeout = 60;
163         conf.network_timeout = 30;
164         conf.http_keepalive = 20;
165         conf.max_requests = 3;
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         BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
200
201         uh_dispatch_add(&cgi_dispatch);
202         init_defaults();
203         signal(SIGPIPE, SIG_IGN);
204
205         while ((ch = getopt(argc, argv, "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:T:A:u:U:")) != -1) {
206                 bool tls = false;
207
208                 switch(ch) {
209                 case 's':
210                         tls = true;
211                         /* fall through */
212                 case 'p':
213                         bound += add_listener_arg(optarg, tls);
214                         break;
215
216                 case 'h':
217                         if (!realpath(optarg, uh_buf)) {
218                                 fprintf(stderr, "Error: Invalid directory %s: %s\n",
219                                                 optarg, strerror(errno));
220                                 exit(1);
221                         }
222                         conf.docroot = strdup(uh_buf);
223                         break;
224
225                 case 'E':
226                         if (optarg[0] != '/') {
227                                 fprintf(stderr, "Error: Invalid error handler: %s\n",
228                                                 optarg);
229                                 exit(1);
230                         }
231                         conf.error_handler = optarg;
232                         break;
233
234                 case 'I':
235                         if (optarg[0] == '/') {
236                                 fprintf(stderr, "Error: Invalid index page: %s\n",
237                                                 optarg);
238                                 exit(1);
239                         }
240                         uh_index_add(optarg);
241                         break;
242
243                 case 'S':
244                         conf.no_symlinks = 1;
245                         break;
246
247                 case 'D':
248                         conf.no_dirlists = 1;
249                         break;
250
251                 case 'R':
252                         conf.rfc1918_filter = 1;
253                         break;
254
255                 case 'n':
256                         conf.max_requests = atoi(optarg);
257                         break;
258
259                 case 'x':
260                         fixup_prefix(optarg);
261                         conf.cgi_prefix = optarg;
262                         break;
263
264                 case 'i':
265                         port = strchr(optarg, '=');
266                         if (optarg[0] != '.' || !port) {
267                                 fprintf(stderr, "Error: Invalid interpreter: %s\n",
268                                                 optarg);
269                                 exit(1);
270                         }
271
272                         *port++ = 0;
273                         uh_interpreter_add(optarg, port);
274                         break;
275
276                 case 't':
277                         conf.script_timeout = atoi(optarg);
278                         break;
279
280                 case 'T':
281                         conf.network_timeout = atoi(optarg);
282                         break;
283
284                 case 'A':
285                         conf.tcp_keepalive = atoi(optarg);
286                         break;
287
288                 case 'f':
289                         nofork = 1;
290                         break;
291
292                 case 'd':
293                         port = alloca(strlen(optarg) + 1);
294                         if (!port)
295                                 return -1;
296
297                         /* "decode" plus to space to retain compat */
298                         for (opt = 0; optarg[opt]; opt++)
299                                 if (optarg[opt] == '+')
300                                         optarg[opt] = ' ';
301
302                         /* opt now contains strlen(optarg) -- no need to re-scan */
303                         if (uh_urldecode(port, opt, optarg, opt) < 0) {
304                                 fprintf(stderr, "uhttpd: invalid encoding\n");
305                                 return -1;
306                         }
307
308                         printf("%s", port);
309                         break;
310
311                 /* basic auth realm */
312                 case 'r':
313                         conf.realm = optarg;
314                         break;
315
316                 /* md5 crypt */
317                 case 'm':
318                         printf("%s\n", crypt(optarg, "$1$"));
319                         return 0;
320                         break;
321
322                 /* config file */
323                 case 'c':
324                         conf.file = optarg;
325                         break;
326
327                 default:
328                         return usage(argv[0]);
329                 }
330         }
331
332         uh_config_parse();
333
334         if (!bound) {
335                 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
336                 return 1;
337         }
338
339         /* fork (if not disabled) */
340         if (!nofork) {
341                 switch (fork()) {
342                 case -1:
343                         perror("fork()");
344                         exit(1);
345
346                 case 0:
347                         /* daemon setup */
348                         if (chdir("/"))
349                                 perror("chdir()");
350
351                         cur_fd = open("/dev/null", O_WRONLY);
352                         if (cur_fd > 0) {
353                                 dup2(cur_fd, 0);
354                                 dup2(cur_fd, 1);
355                                 dup2(cur_fd, 2);
356                         }
357
358                         break;
359
360                 default:
361                         exit(0);
362                 }
363         }
364
365         return run_server();
366 }