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)
48 static void uh_config_parse(void)
50 const char *path = conf.file;
58 path = "/etc/httpd.conf";
64 memset(line, 0, sizeof(line));
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))
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))
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))
85 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 if (!uh_interpreter_add(col1, col2))
96 "Unable to add interpreter %s for extension %s: "
97 "Out of memory\n", col2, col1
106 static int add_listener_arg(char *arg, bool tls)
112 s = strrchr(arg, ':');
119 return uh_socket_bind(host, port, tls);
122 static int usage(const char *name)
125 "Usage: %s -p [addr:]port -h docroot\n"
126 " -f Do not fork to background\n"
127 " -c file Configuration file, default is '/etc/httpd.conf'\n"
128 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
130 " -s [addr:]port Like -p but provide HTTPS on this port\n"
131 " -C file ASN.1 server certificate file\n"
132 " -K file ASN.1 server private key file\n"
134 " -h directory Specify the document root, default is '.'\n"
135 " -E string Use given virtual URL as 404 error handler\n"
136 " -I string Use given filename as index for directories, multiple allowed\n"
137 " -S Do not follow symbolic links outside of the docroot\n"
138 " -D Do not allow directory listings, send 403 instead\n"
139 " -R Enable RFC1918 filter\n"
140 " -n count Maximum allowed number of concurrent requests\n"
142 " -l string URL prefix for Lua handler, default is '/lua'\n"
143 " -L file Lua handler script, omit to disable Lua\n"
146 " -u string URL prefix for HTTP/JSON handler\n"
147 " -U file Override ubus socket path\n"
149 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
150 " -i .ext=path Use interpreter at path for files with the given extension\n"
151 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
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"
161 static void init_defaults(void)
163 conf.script_timeout = 60;
164 conf.network_timeout = 30;
165 conf.http_keepalive = 20;
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";
171 uh_index_add("index.html");
172 uh_index_add("index.htm");
173 uh_index_add("default.html");
174 uh_index_add("default.htm");
177 static void fixup_prefix(char *str)
184 len = strlen(str) - 1;
186 while (len > 0 && str[len] == '/')
192 int main(int argc, char **argv)
194 const char *tls_key, *tls_crt;
202 BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
204 uh_dispatch_add(&cgi_dispatch);
206 signal(SIGPIPE, SIG_IGN);
208 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) {
217 bound += add_listener_arg(optarg, tls);
221 if (!realpath(optarg, uh_buf)) {
222 fprintf(stderr, "Error: Invalid directory %s: %s\n",
223 optarg, strerror(errno));
226 conf.docroot = strdup(uh_buf);
230 if (optarg[0] != '/') {
231 fprintf(stderr, "Error: Invalid error handler: %s\n",
235 conf.error_handler = optarg;
239 if (optarg[0] == '/') {
240 fprintf(stderr, "Error: Invalid index page: %s\n",
244 uh_index_add(optarg);
248 conf.no_symlinks = 1;
252 conf.no_dirlists = 1;
256 conf.rfc1918_filter = 1;
260 conf.max_requests = atoi(optarg);
264 fixup_prefix(optarg);
265 conf.cgi_prefix = optarg;
269 port = strchr(optarg, '=');
270 if (optarg[0] != '.' || !port) {
271 fprintf(stderr, "Error: Invalid interpreter: %s\n",
277 uh_interpreter_add(optarg, port);
281 conf.script_timeout = atoi(optarg);
285 conf.network_timeout = 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$"));
339 return usage(argv[0]);
346 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
351 if (!tls_crt || !tls_key) {
352 fprintf(stderr, "Please specify a certificate and "
353 "a key file to enable SSL support\n");
358 if (uh_tls_init(tls_key, tls_crt))
361 fprintf(stderr, "Error: TLS support not compiled in.\n");
366 /* fork (if not disabled) */
378 cur_fd = open("/dev/null", O_WRONLY);