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