add full usage help text
[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 void 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 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 #ifdef HAVE_CGI
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 #endif
147 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
148 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
149 #endif
150 " -T seconds Network timeout in seconds, default is 30\n"
151 " -d string URL decode given string\n"
152 " -r string Specify basic auth realm\n"
153 " -m string MD5 crypt given string\n"
154 "\n", name
155 );
156 return 1;
157 }
158
159 static void init_defaults(void)
160 {
161 conf.network_timeout = 30;
162 conf.http_keepalive = 0; /* fixme */
163 conf.max_requests = 3;
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 int main(int argc, char **argv)
172 {
173 bool nofork = false;
174 char *port;
175 int opt, ch;
176 int cur_fd;
177
178 init_defaults();
179 signal(SIGPIPE, SIG_IGN);
180
181 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) {
182 bool tls = false;
183
184 switch(ch) {
185 case 's':
186 tls = true;
187 /* fall through */
188 case 'p':
189 add_listener_arg(optarg, tls);
190 break;
191
192 case 'h':
193 if (!realpath(optarg, conf.docroot)) {
194 fprintf(stderr, "Error: Invalid directory %s: %s\n",
195 optarg, strerror(errno));
196 exit(1);
197 }
198 break;
199
200 case 'E':
201 if (optarg[0] != '/') {
202 fprintf(stderr, "Error: Invalid error handler: %s\n",
203 optarg);
204 exit(1);
205 }
206 conf.error_handler = optarg;
207 break;
208
209 case 'I':
210 if (optarg[0] == '/') {
211 fprintf(stderr, "Error: Invalid index page: %s\n",
212 optarg);
213 exit(1);
214 }
215 uh_index_add(optarg);
216 break;
217
218 case 'S':
219 conf.no_symlinks = 1;
220 break;
221
222 case 'D':
223 conf.no_dirlists = 1;
224 break;
225
226 case 'R':
227 conf.rfc1918_filter = 1;
228 break;
229
230 case 'n':
231 conf.max_requests = atoi(optarg);
232 break;
233
234 case 't':
235 conf.script_timeout = atoi(optarg);
236 break;
237
238 case 'T':
239 conf.network_timeout = atoi(optarg);
240 break;
241
242 case 'A':
243 conf.tcp_keepalive = atoi(optarg);
244 break;
245
246 case 'f':
247 nofork = 1;
248 break;
249
250 case 'd':
251 port = alloca(strlen(optarg) + 1);
252 if (!port)
253 return -1;
254
255 /* "decode" plus to space to retain compat */
256 for (opt = 0; optarg[opt]; opt++)
257 if (optarg[opt] == '+')
258 optarg[opt] = ' ';
259
260 /* opt now contains strlen(optarg) -- no need to re-scan */
261 if (uh_urldecode(port, opt, optarg, opt) < 0) {
262 fprintf(stderr, "uhttpd: invalid encoding\n");
263 return -1;
264 }
265
266 printf("%s", port);
267 break;
268
269 /* basic auth realm */
270 case 'r':
271 conf.realm = optarg;
272 break;
273
274 /* md5 crypt */
275 case 'm':
276 printf("%s\n", crypt(optarg, "$1$"));
277 return 0;
278 break;
279
280 /* config file */
281 case 'c':
282 conf.file = optarg;
283 break;
284
285 default:
286 return usage(argv[0]);
287 }
288 }
289
290 uh_config_parse();
291
292 /* fork (if not disabled) */
293 if (!nofork) {
294 switch (fork()) {
295 case -1:
296 perror("fork()");
297 exit(1);
298
299 case 0:
300 /* daemon setup */
301 if (chdir("/"))
302 perror("chdir()");
303
304 cur_fd = open("/dev/null", O_WRONLY);
305 if (cur_fd > 0) {
306 dup2(cur_fd, 0);
307 dup2(cur_fd, 1);
308 dup2(cur_fd, 2);
309 }
310
311 break;
312
313 default:
314 exit(0);
315 }
316 }
317
318 return run_server();
319 }