add forking
[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, "Usage: %s -p <port>\n", name);
119         return 1;
120 }
121
122 static void init_defaults(void)
123 {
124         conf.network_timeout = 30;
125         conf.http_keepalive = 0; /* fixme */
126         conf.max_requests = 3;
127
128         uh_index_add("index.html");
129         uh_index_add("index.htm");
130         uh_index_add("default.html");
131         uh_index_add("default.htm");
132 }
133
134 int main(int argc, char **argv)
135 {
136         bool nofork = false;
137         char *port;
138         int opt, ch;
139         int cur_fd;
140
141         init_defaults();
142         signal(SIGPIPE, SIG_IGN);
143
144         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) {
145                 bool tls = false;
146
147                 switch(ch) {
148                 case 's':
149                         tls = true;
150                         /* fall through */
151                 case 'p':
152                         add_listener_arg(optarg, tls);
153                         break;
154
155                 case 'h':
156                         if (!realpath(optarg, conf.docroot)) {
157                                 fprintf(stderr, "Error: Invalid directory %s: %s\n",
158                                                 optarg, strerror(errno));
159                                 exit(1);
160                         }
161                         break;
162
163                 case 'E':
164                         if (optarg[0] != '/') {
165                                 fprintf(stderr, "Error: Invalid error handler: %s\n",
166                                                 optarg);
167                                 exit(1);
168                         }
169                         conf.error_handler = optarg;
170                         break;
171
172                 case 'I':
173                         if (optarg[0] == '/') {
174                                 fprintf(stderr, "Error: Invalid index page: %s\n",
175                                                 optarg);
176                                 exit(1);
177                         }
178                         uh_index_add(optarg);
179                         break;
180
181                 case 'S':
182                         conf.no_symlinks = 1;
183                         break;
184
185                 case 'D':
186                         conf.no_dirlists = 1;
187                         break;
188
189                 case 'R':
190                         conf.rfc1918_filter = 1;
191                         break;
192
193                 case 'n':
194                         conf.max_requests = atoi(optarg);
195                         break;
196
197                 case 't':
198                         conf.script_timeout = atoi(optarg);
199                         break;
200
201                 case 'T':
202                         conf.network_timeout = atoi(optarg);
203                         break;
204
205                 case 'A':
206                         conf.tcp_keepalive = atoi(optarg);
207                         break;
208
209                 case 'f':
210                         nofork = 1;
211                         break;
212
213                 case 'd':
214                         port = alloca(strlen(optarg) + 1);
215                         if (!port)
216                                 return -1;
217
218                         /* "decode" plus to space to retain compat */
219                         for (opt = 0; optarg[opt]; opt++)
220                                 if (optarg[opt] == '+')
221                                         optarg[opt] = ' ';
222
223                         /* opt now contains strlen(optarg) -- no need to re-scan */
224                         if (uh_urldecode(port, opt, optarg, opt) < 0) {
225                                 fprintf(stderr, "uhttpd: invalid encoding\n");
226                                 return -1;
227                         }
228
229                         printf("%s", port);
230                         break;
231
232                 /* basic auth realm */
233                 case 'r':
234                         conf.realm = optarg;
235                         break;
236
237                 /* md5 crypt */
238                 case 'm':
239                         printf("%s\n", crypt(optarg, "$1$"));
240                         return 0;
241                         break;
242
243                 /* config file */
244                 case 'c':
245                         conf.file = optarg;
246                         break;
247
248                 default:
249                         return usage(argv[0]);
250                 }
251         }
252
253         uh_config_parse();
254
255         /* fork (if not disabled) */
256         if (!nofork) {
257                 switch (fork()) {
258                 case -1:
259                         perror("fork()");
260                         exit(1);
261
262                 case 0:
263                         /* daemon setup */
264                         if (chdir("/"))
265                                 perror("chdir()");
266
267                         cur_fd = open("/dev/null", O_WRONLY);
268                         if (cur_fd > 0) {
269                                 dup2(cur_fd, 0);
270                                 dup2(cur_fd, 1);
271                                 dup2(cur_fd, 2);
272                         }
273
274                         break;
275
276                 default:
277                         exit(0);
278                 }
279         }
280
281         return run_server();
282 }