bc6893a9f93af2012e26c150ea914d976220e608
[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 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23
24 #include <getopt.h>
25 #include <errno.h>
26 #include <netdb.h>
27 #include <signal.h>
28
29 #include <libubox/usock.h>
30
31 #include "uhttpd.h"
32
33 char uh_buf[4096];
34
35 static int run_server(void)
36 {
37         uloop_init();
38         uh_setup_listeners();
39         uloop_run();
40
41         return 0;
42 }
43
44 static void uh_config_parse(void)
45 {
46         const char *path = conf.file;
47         FILE *c;
48         char line[512];
49         char *col1;
50         char *col2;
51         char *eol;
52
53         if (!path)
54                 path = "/etc/httpd.conf";
55
56         c = fopen(path, "r");
57         if (!c)
58                 return;
59
60         memset(line, 0, sizeof(line));
61
62         while (fgets(line, sizeof(line) - 1, c)) {
63                 if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
64                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
65                                 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
66                                 !(eol = strchr(col2, '\n')) || (*eol++  = 0))
67                                 continue;
68
69                         uh_auth_add(line, col1, col2);
70                 } else if (!strncmp(line, "I:", 2)) {
71                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
72                                 !(eol = strchr(col1, '\n')) || (*eol++  = 0))
73                                 continue;
74
75                         uh_index_add(strdup(col1));
76                 } else if (!strncmp(line, "E404:", 5)) {
77                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
78                                 !(eol = strchr(col1, '\n')) || (*eol++  = 0))
79                                 continue;
80
81                         conf.error_handler = strdup(col1);
82                 }
83 #ifdef HAVE_CGI
84                 else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
85                         if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
86                                 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
87                                 !(eol = strchr(col2, '\n')) || (*eol++  = 0))
88                                 continue;
89
90                         if (!uh_interpreter_add(col1, col2))
91                                 fprintf(stderr,
92                                                 "Unable to add interpreter %s for extension %s: "
93                                                 "Out of memory\n", col2, col1
94                                 );
95                 }
96 #endif
97         }
98
99         fclose(c);
100 }
101
102 static int add_listener_arg(char *arg, bool tls)
103 {
104         char *host = NULL;
105         char *port = arg;
106         char *s;
107
108         s = strrchr(arg, ':');
109         if (s) {
110                 host = arg;
111                 port = s + 1;
112                 *s = 0;
113         }
114
115         return uh_socket_bind(host, port, tls);
116 }
117
118 static int usage(const char *name)
119 {
120         fprintf(stderr,
121                 "Usage: %s -p [addr:]port -h docroot\n"
122                 "       -f              Do not fork to background\n"
123                 "       -c file         Configuration file, default is '/etc/httpd.conf'\n"
124                 "       -p [addr:]port  Bind to specified address and port, multiple allowed\n"
125 #ifdef HAVE_TLS
126                 "       -s [addr:]port  Like -p but provide HTTPS on this port\n"
127                 "       -C file         ASN.1 server certificate file\n"
128                 "       -K file         ASN.1 server private key file\n"
129 #endif
130                 "       -h directory    Specify the document root, default is '.'\n"
131                 "       -E string       Use given virtual URL as 404 error handler\n"
132                 "       -I string       Use given filename as index for directories, multiple allowed\n"
133                 "       -S              Do not follow symbolic links outside of the docroot\n"
134                 "       -D              Do not allow directory listings, send 403 instead\n"
135                 "       -R              Enable RFC1918 filter\n"
136                 "       -n count        Maximum allowed number of concurrent requests\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 #ifdef HAVE_CGI
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 #endif
149 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
150                 "       -t seconds      CGI, Lua and UBUS script timeout in seconds, default is 60\n"
151 #endif
152                 "       -T seconds      Network timeout in seconds, default is 30\n"
153                 "       -d string       URL decode given string\n"
154                 "       -r string       Specify basic auth realm\n"
155                 "       -m string       MD5 crypt given string\n"
156                 "\n", name
157         );
158         return 1;
159 }
160
161 static void init_defaults(void)
162 {
163         conf.script_timeout = 60;
164         conf.network_timeout = 30;
165         conf.http_keepalive = 0; /* fixme */
166         conf.max_requests = 3;
167         conf.realm = "Protected Area";
168         conf.cgi_prefix = "/cgi-bin";
169         conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
170
171         uh_index_add("index.html");
172         uh_index_add("index.htm");
173         uh_index_add("default.html");
174         uh_index_add("default.htm");
175 }
176
177 int main(int argc, char **argv)
178 {
179         bool nofork = false;
180         char *port;
181         int opt, ch;
182         int cur_fd;
183         int bound = 0;
184
185         BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
186
187         init_defaults();
188         signal(SIGPIPE, SIG_IGN);
189
190         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) {
191                 bool tls = false;
192
193                 switch(ch) {
194                 case 's':
195                         tls = true;
196                         /* fall through */
197                 case 'p':
198                         bound += add_listener_arg(optarg, tls);
199                         break;
200
201                 case 'h':
202                         if (!realpath(optarg, uh_buf)) {
203                                 fprintf(stderr, "Error: Invalid directory %s: %s\n",
204                                                 optarg, strerror(errno));
205                                 exit(1);
206                         }
207                         conf.docroot = strdup(uh_buf);
208                         break;
209
210                 case 'E':
211                         if (optarg[0] != '/') {
212                                 fprintf(stderr, "Error: Invalid error handler: %s\n",
213                                                 optarg);
214                                 exit(1);
215                         }
216                         conf.error_handler = optarg;
217                         break;
218
219                 case 'I':
220                         if (optarg[0] == '/') {
221                                 fprintf(stderr, "Error: Invalid index page: %s\n",
222                                                 optarg);
223                                 exit(1);
224                         }
225                         uh_index_add(optarg);
226                         break;
227
228                 case 'S':
229                         conf.no_symlinks = 1;
230                         break;
231
232                 case 'D':
233                         conf.no_dirlists = 1;
234                         break;
235
236                 case 'R':
237                         conf.rfc1918_filter = 1;
238                         break;
239
240                 case 'n':
241                         conf.max_requests = atoi(optarg);
242                         break;
243
244                 case 't':
245                         conf.script_timeout = atoi(optarg);
246                         break;
247
248                 case 'T':
249                         conf.network_timeout = atoi(optarg);
250                         break;
251
252                 case 'A':
253                         conf.tcp_keepalive = atoi(optarg);
254                         break;
255
256                 case 'f':
257                         nofork = 1;
258                         break;
259
260                 case 'd':
261                         port = alloca(strlen(optarg) + 1);
262                         if (!port)
263                                 return -1;
264
265                         /* "decode" plus to space to retain compat */
266                         for (opt = 0; optarg[opt]; opt++)
267                                 if (optarg[opt] == '+')
268                                         optarg[opt] = ' ';
269
270                         /* opt now contains strlen(optarg) -- no need to re-scan */
271                         if (uh_urldecode(port, opt, optarg, opt) < 0) {
272                                 fprintf(stderr, "uhttpd: invalid encoding\n");
273                                 return -1;
274                         }
275
276                         printf("%s", port);
277                         break;
278
279                 /* basic auth realm */
280                 case 'r':
281                         conf.realm = optarg;
282                         break;
283
284                 /* md5 crypt */
285                 case 'm':
286                         printf("%s\n", crypt(optarg, "$1$"));
287                         return 0;
288                         break;
289
290                 /* config file */
291                 case 'c':
292                         conf.file = optarg;
293                         break;
294
295                 default:
296                         return usage(argv[0]);
297                 }
298         }
299
300         uh_config_parse();
301
302         if (!bound) {
303                 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
304                 return 1;
305         }
306
307         /* fork (if not disabled) */
308         if (!nofork) {
309                 switch (fork()) {
310                 case -1:
311                         perror("fork()");
312                         exit(1);
313
314                 case 0:
315                         /* daemon setup */
316                         if (chdir("/"))
317                                 perror("chdir()");
318
319                         cur_fd = open("/dev/null", O_WRONLY);
320                         if (cur_fd > 0) {
321                                 dup2(cur_fd, 0);
322                                 dup2(cur_fd, 1);
323                                 dup2(cur_fd, 2);
324                         }
325
326                         break;
327
328                 default:
329                         exit(0);
330                 }
331         }
332
333         return run_server();
334 }