add -Wmissing-declarations
[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
34 static int run_server(void)
35 {
36         uloop_init();
37         uh_setup_listeners();
38         uloop_run();
39
40         return 0;
41 }
42
43 static void uh_config_parse(void)
44 {
45         const char *path = conf.file;
46         FILE *c;
47         char line[512];
48         char *col1;
49         char *col2;
50         char *eol;
51
52         if (!path)
53                 path = "/etc/httpd.conf";
54
55         c = fopen(path, "r");
56         if (!c)
57                 return;
58
59         memset(line, 0, sizeof(line));
60
61         while (fgets(line, sizeof(line) - 1, c)) {
62                 if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
63                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
64                                 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
65                                 !(eol = strchr(col2, '\n')) || (*eol++  = 0))
66                                 continue;
67
68                         uh_auth_add(line, col1, col2);
69                 } else if (!strncmp(line, "I:", 2)) {
70                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
71                                 !(eol = strchr(col1, '\n')) || (*eol++  = 0))
72                                 continue;
73
74                         uh_index_add(strdup(col1));
75                 } else if (!strncmp(line, "E404:", 5)) {
76                         if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
77                                 !(eol = strchr(col1, '\n')) || (*eol++  = 0))
78                                 continue;
79
80                         conf.error_handler = strdup(col1);
81                 }
82 #ifdef HAVE_CGI
83                 else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
84                         if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
85                                 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
86                                 !(eol = strchr(col2, '\n')) || (*eol++  = 0))
87                                 continue;
88
89                         if (!uh_interpreter_add(col1, col2))
90                                 fprintf(stderr,
91                                                 "Unable to add interpreter %s for extension %s: "
92                                                 "Out of memory\n", col2, col1
93                                 );
94                 }
95 #endif
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 requests\n"
136 #ifdef HAVE_LUA
137                 "       -l string       URL prefix for Lua handler, default is '/lua'\n"
138                 "       -L file         Lua handler script, omit to disable Lua\n"
139 #endif
140 #ifdef HAVE_UBUS
141                 "       -u string       URL prefix for HTTP/JSON handler\n"
142                 "       -U file         Override ubus socket path\n"
143 #endif
144 #ifdef HAVE_CGI
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 #endif
148 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
149                 "       -t seconds      CGI, Lua and UBUS script timeout in seconds, default is 60\n"
150 #endif
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 = 0; /* fixme */
165         conf.max_requests = 3;
166         conf.realm = "Protected Area";
167         conf.cgi_prefix = "/cgi-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 int main(int argc, char **argv)
176 {
177         bool nofork = false;
178         char *port;
179         int opt, ch;
180         int cur_fd;
181         int bound = 0;
182
183         init_defaults();
184         signal(SIGPIPE, SIG_IGN);
185
186         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) {
187                 bool tls = false;
188
189                 switch(ch) {
190                 case 's':
191                         tls = true;
192                         /* fall through */
193                 case 'p':
194                         bound += add_listener_arg(optarg, tls);
195                         break;
196
197                 case 'h':
198                         if (!realpath(optarg, conf.docroot)) {
199                                 fprintf(stderr, "Error: Invalid directory %s: %s\n",
200                                                 optarg, strerror(errno));
201                                 exit(1);
202                         }
203                         break;
204
205                 case 'E':
206                         if (optarg[0] != '/') {
207                                 fprintf(stderr, "Error: Invalid error handler: %s\n",
208                                                 optarg);
209                                 exit(1);
210                         }
211                         conf.error_handler = optarg;
212                         break;
213
214                 case 'I':
215                         if (optarg[0] == '/') {
216                                 fprintf(stderr, "Error: Invalid index page: %s\n",
217                                                 optarg);
218                                 exit(1);
219                         }
220                         uh_index_add(optarg);
221                         break;
222
223                 case 'S':
224                         conf.no_symlinks = 1;
225                         break;
226
227                 case 'D':
228                         conf.no_dirlists = 1;
229                         break;
230
231                 case 'R':
232                         conf.rfc1918_filter = 1;
233                         break;
234
235                 case 'n':
236                         conf.max_requests = atoi(optarg);
237                         break;
238
239                 case 't':
240                         conf.script_timeout = atoi(optarg);
241                         break;
242
243                 case 'T':
244                         conf.network_timeout = atoi(optarg);
245                         break;
246
247                 case 'A':
248                         conf.tcp_keepalive = atoi(optarg);
249                         break;
250
251                 case 'f':
252                         nofork = 1;
253                         break;
254
255                 case 'd':
256                         port = alloca(strlen(optarg) + 1);
257                         if (!port)
258                                 return -1;
259
260                         /* "decode" plus to space to retain compat */
261                         for (opt = 0; optarg[opt]; opt++)
262                                 if (optarg[opt] == '+')
263                                         optarg[opt] = ' ';
264
265                         /* opt now contains strlen(optarg) -- no need to re-scan */
266                         if (uh_urldecode(port, opt, optarg, opt) < 0) {
267                                 fprintf(stderr, "uhttpd: invalid encoding\n");
268                                 return -1;
269                         }
270
271                         printf("%s", port);
272                         break;
273
274                 /* basic auth realm */
275                 case 'r':
276                         conf.realm = optarg;
277                         break;
278
279                 /* md5 crypt */
280                 case 'm':
281                         printf("%s\n", crypt(optarg, "$1$"));
282                         return 0;
283                         break;
284
285                 /* config file */
286                 case 'c':
287                         conf.file = optarg;
288                         break;
289
290                 default:
291                         return usage(argv[0]);
292                 }
293         }
294
295         uh_config_parse();
296
297         if (!bound) {
298                 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
299                 return 1;
300         }
301
302         /* fork (if not disabled) */
303         if (!nofork) {
304                 switch (fork()) {
305                 case -1:
306                         perror("fork()");
307                         exit(1);
308
309                 case 0:
310                         /* daemon setup */
311                         if (chdir("/"))
312                                 perror("chdir()");
313
314                         cur_fd = open("/dev/null", O_WRONLY);
315                         if (cur_fd > 0) {
316                                 dup2(cur_fd, 0);
317                                 dup2(cur_fd, 1);
318                                 dup2(cur_fd, 2);
319                         }
320
321                         break;
322
323                 default:
324                         exit(0);
325                 }
326         }
327
328         return run_server();
329 }