2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #define _XOPEN_SOURCE 700
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
32 #include <libubox/usock.h>
39 static int run_server(void)
43 uh_plugin_post_init();
49 static void uh_config_parse(void)
51 const char *path
= conf
.file
;
59 path
= "/etc/httpd.conf";
65 memset(line
, 0, sizeof(line
));
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))
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))
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))
86 conf
.error_handler
= strdup(col1
);
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))
94 uh_interpreter_add(col1
, col2
);
101 static int add_listener_arg(char *arg
, bool tls
)
107 s
= strrchr(arg
, ':');
114 return uh_socket_bind(host
, port
, tls
);
117 static int usage(const char *name
)
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"
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"
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"
137 " -l string URL prefix for Lua handler, default is '/lua'\n"
138 " -L file Lua handler script, omit to disable Lua\n"
141 " -u string URL prefix for HTTP/JSON handler\n"
142 " -U file Override ubus socket path\n"
144 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
145 " -i .ext=path Use interpreter at path for files with the given extension\n"
146 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
147 " -T seconds Network timeout in seconds, default is 30\n"
148 " -k seconds HTTP keepalive timeout\n"
149 " -d string URL decode given string\n"
150 " -r string Specify basic auth realm\n"
151 " -m string MD5 crypt given string\n"
157 static void init_defaults(void)
159 conf
.script_timeout
= 60;
160 conf
.network_timeout
= 30;
161 conf
.http_keepalive
= 20;
162 conf
.max_requests
= 3;
163 conf
.realm
= "Protected Area";
164 conf
.cgi_prefix
= "/cgi-bin";
165 conf
.cgi_path
= "/sbin:/usr/sbin:/bin:/usr/bin";
167 uh_index_add("index.html");
168 uh_index_add("index.htm");
169 uh_index_add("default.html");
170 uh_index_add("default.htm");
173 static void fixup_prefix(char *str
)
180 len
= strlen(str
) - 1;
182 while (len
> 0 && str
[len
] == '/')
188 int main(int argc
, char **argv
)
190 const char *tls_key
= NULL
, *tls_crt
= NULL
;
198 BUILD_BUG_ON(sizeof(uh_buf
) < PATH_MAX
);
200 uh_dispatch_add(&cgi_dispatch
);
202 signal(SIGPIPE
, SIG_IGN
);
204 while ((ch
= getopt(argc
, argv
, "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:k:T:A:u:U:")) != -1) {
213 bound
+= add_listener_arg(optarg
, tls
);
217 if (!realpath(optarg
, uh_buf
)) {
218 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
219 optarg
, strerror(errno
));
222 conf
.docroot
= strdup(uh_buf
);
226 if (optarg
[0] != '/') {
227 fprintf(stderr
, "Error: Invalid error handler: %s\n",
231 conf
.error_handler
= optarg
;
235 if (optarg
[0] == '/') {
236 fprintf(stderr
, "Error: Invalid index page: %s\n",
240 uh_index_add(optarg
);
244 conf
.no_symlinks
= 1;
248 conf
.no_dirlists
= 1;
252 conf
.rfc1918_filter
= 1;
256 conf
.max_requests
= atoi(optarg
);
260 fixup_prefix(optarg
);
261 conf
.cgi_prefix
= optarg
;
265 port
= strchr(optarg
, '=');
266 if (optarg
[0] != '.' || !port
) {
267 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
273 uh_interpreter_add(optarg
, port
);
277 conf
.script_timeout
= atoi(optarg
);
281 conf
.network_timeout
= atoi(optarg
);
285 conf
.http_keepalive
= atoi(optarg
);
289 conf
.tcp_keepalive
= atoi(optarg
);
297 port
= alloca(strlen(optarg
) + 1);
301 /* "decode" plus to space to retain compat */
302 for (opt
= 0; optarg
[opt
]; opt
++)
303 if (optarg
[opt
] == '+')
306 /* opt now contains strlen(optarg) -- no need to re-scan */
307 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0) {
308 fprintf(stderr
, "uhttpd: invalid encoding\n");
315 /* basic auth realm */
322 printf("%s\n", crypt(optarg
, "$1$"));
340 conf
.lua_prefix
= optarg
;
344 conf
.lua_handler
= optarg
;
349 conf
.ubus_prefix
= optarg
;
353 conf
.ubus_socket
= optarg
;
357 return usage(argv
[0]);
364 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
369 if (!tls_crt
|| !tls_key
) {
370 fprintf(stderr
, "Please specify a certificate and "
371 "a key file to enable SSL support\n");
376 if (uh_tls_init(tls_key
, tls_crt
))
379 fprintf(stderr
, "Error: TLS support not compiled in.\n");
385 if (conf
.lua_handler
|| conf
.lua_prefix
) {
386 if (!conf
.lua_handler
|| !conf
.lua_prefix
) {
387 fprintf(stderr
, "Need handler and prefix to enable Lua support\n");
390 if (uh_plugin_init("uhttpd_lua.so"))
395 if (conf
.ubus_prefix
&& uh_plugin_init("uhttpd_ubus.so"))
399 /* fork (if not disabled) */
411 cur_fd
= open("/dev/null", O_WRONLY
);