Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 285 lines (246 sloc) 7.775 kB
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
1 /*
2e94d62 @stealth fixed typos and (C)
authored
2 * Copyright (C) 2008-2012 Sebastian Krahmer.
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Sebastian Krahmer.
16 * 4. The name Sebastian Krahmer may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <cstdio>
34 #include <cstdlib>
35 #include <iostream>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <pwd.h>
40 #include <grp.h>
41 #include <sys/types.h>
9bc6d57 @stealth - Added possibility to bind to specific address
authored
42 #include <sys/socket.h>
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
43 #include <sys/time.h>
44 #include <sys/resource.h>
45 #include <signal.h>
46 #include <cstring>
433281c @stealth Added necessary files and basic code for reverse proxy to work.
authored
47 #include "config.h"
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
48 #include "lonely.h"
49 #include "misc.h"
50 #include "multicore.h"
51
52
53 using namespace std;
54
55
56 void die(const char *s, bool please_die = 1)
57 {
58 perror(s);
59 if (please_die)
60 exit(errno);
61 }
62
63
64 void help(const char *p)
65 {
5551cb5 @stealth - lophttpd: qdded quiet mode which doesnt log or drop index.html files
authored
66 cerr<<"Usage: "<<p<<" [-6] [-R web-root] [-B html-base-tag] [-iH] [-I IP] [-u user]\n"
72239e3 @stealth - lophttpd: added -E 'no-error-kill' to keep connection alive even
authored
67 <<"\t\t [-l logfile] [-p port] [-L provider] [-n nCores] [-S n]\n"
68 <<"\t\t [-U upload] [-r] [-E] [-Q]\n\n"
69 <<"\tcommonly used options:\n\n"
8d557cc @stealth - changed Config namespace to httpd_config
authored
70 <<"\t\t -R : web-root, default "<<httpd_config::root<<endl
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
71 <<"\t\t -i : use autoindexing\n"
9bc6d57 @stealth - Added possibility to bind to specific address
authored
72 <<"\t\t -I : IP(6) to bind to, default {INADDR_ANY}\n"
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
73 <<"\t\t -H : use vhosts (requires vhost setup in web-root)\n"
8d557cc @stealth - changed Config namespace to httpd_config
authored
74 <<"\t\t -u : run as this user, default "<<httpd_config::user<<endl
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
75 <<"\t\t -n : number of CPU cores to use, default all"<<endl
72239e3 @stealth - lophttpd: added -E 'no-error-kill' to keep connection alive even
authored
76 <<"\t\t -p : port, default "<<httpd_config::port<<endl<<endl
77 <<"\trarely used options:\n\n"
78 <<"\t\t -6 : use IPv6, default is IPv4\n"
79 <<"\t\t -l : logfile, default "<<httpd_config::logfile<<endl
80 <<"\t\t -L : logprovider, default '"<<httpd_config::log_provider<<"'"<<endl
81 <<"\t\t -B : <base> 'http://...' tag, if operating behind a proxy\n"
5551cb5 @stealth - lophttpd: qdded quiet mode which doesnt log or drop index.html files
authored
82 <<"\t\t -q : quiet mode; don't generate any logs or index.html files\n"
532bf93 @stealth - lophttpd: also return error if sendfile() returns 0 (can happen if …
authored
83 <<"\t\t -S : sendfile() chunksize (no need to change), default: "<<httpd_config::mss<<endl
f833d57 @stealth updated changelog and README files
authored
84 <<"\t\t -U : upload dir inside web-root, default disabled"<<endl
72239e3 @stealth - lophttpd: added -E 'no-error-kill' to keep connection alive even
authored
85 <<"\t\t -E : do not close connection on invalid requests, default disabled"<<endl
86 <<"\t\t -Q : (implies -r) do not tell client the rand token (for write-only uploads)"<<endl
532bf93 @stealth - lophttpd: also return error if sendfile() returns 0 (can happen if …
authored
87 <<"\t\t -r : add rand token to uploaded filenames (default off)"<<endl<<endl;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
88 exit(errno);
89 }
90
91
92 void close_fds()
93 {
94 struct rlimit rl;
95
96 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
97 die("getrlimit");
98 for (unsigned int i = 3; i <= rl.rlim_max; ++i)
99 close(i);
100 close(0);
101 open("/dev/null", O_RDWR);
102 dup2(0, 1);
103 }
104
c4b87fe @stealth - Added HINTS file
authored
105 static lonely_http *httpd = NULL;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
106
107 void sigusr1(int x)
108 {
c4b87fe @stealth - Added HINTS file
authored
109 if (!httpd)
110 return;
111
8d557cc @stealth - changed Config namespace to httpd_config
authored
112 if (httpd_config::gen_index) {
50b084e @stealth - merged lophttd and frontend Makefiles
authored
113 misc::dir2index.clear();
8d557cc @stealth - changed Config namespace to httpd_config
authored
114 if (httpd_config::is_chrooted)
50b084e @stealth - merged lophttd and frontend Makefiles
authored
115 misc::generate_index("/");
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
116 else
50b084e @stealth - merged lophttd and frontend Makefiles
authored
117 misc::generate_index(httpd_config::root);
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
118 }
c4b87fe @stealth - Added HINTS file
authored
119 httpd->clear_cache();
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
120 }
121
122
123 int main(int argc, char **argv)
124 {
125 int c = 0;
de73370 @stealth - removing sin and sin6 from state struct; only using ip string now
authored
126 cout<<"\nlophttpd -- lots of performance httpd (C) 2008-2012 Sebastian Krahmer\n\n";
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
127
128 if (getuid() != 0) {
129 cerr<<"\a!!! WARNING: !!! Must be called as root in order to chroot() and drop privs properly!\n";
130 cerr<<"Continuing in UNSAFE mode!\n\n";
131 }
132
72239e3 @stealth - lophttpd: added -E 'no-error-kill' to keep connection alive even
authored
133 while ((c = getopt(argc, argv, "iHhR:p:l:L:u:n:S:I:6B:qU:rEQ")) != -1) {
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
134 switch (c) {
9bc6d57 @stealth - Added possibility to bind to specific address
authored
135 case '6':
8d557cc @stealth - changed Config namespace to httpd_config
authored
136 httpd_config::host = "::0";
137 httpd_config::af = AF_INET6;
9bc6d57 @stealth - Added possibility to bind to specific address
authored
138 break;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
139 case 'i':
8d557cc @stealth - changed Config namespace to httpd_config
authored
140 httpd_config::gen_index = 1;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
141 break;
142 case 'R':
8d557cc @stealth - changed Config namespace to httpd_config
authored
143 httpd_config::root = optarg;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
144 break;
145 case 'H':
8d557cc @stealth - changed Config namespace to httpd_config
authored
146 httpd_config::virtual_hosts = 1;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
147 break;
148 case 'u':
8d557cc @stealth - changed Config namespace to httpd_config
authored
149 httpd_config::user = optarg;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
150 break;
9bc6d57 @stealth - Added possibility to bind to specific address
authored
151 case 'I':
8d557cc @stealth - changed Config namespace to httpd_config
authored
152 httpd_config::host = optarg;
9bc6d57 @stealth - Added possibility to bind to specific address
authored
153 break;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
154 case 'p':
8d557cc @stealth - changed Config namespace to httpd_config
authored
155 httpd_config::port = optarg;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
156 break;
157 case 'l':
8d557cc @stealth - changed Config namespace to httpd_config
authored
158 httpd_config::logfile = optarg;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
159 break;
160 case 'L':
8d557cc @stealth - changed Config namespace to httpd_config
authored
161 httpd_config::log_provider = optarg;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
162 break;
163 case 'n':
8d557cc @stealth - changed Config namespace to httpd_config
authored
164 httpd_config::cores = strtoul(optarg, NULL, 10);
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
165 break;
c4b87fe @stealth - Added HINTS file
authored
166 case 'S':
8d557cc @stealth - changed Config namespace to httpd_config
authored
167 httpd_config::mss = strtoul(optarg, NULL, 10);
c4b87fe @stealth - Added HINTS file
authored
168 break;
c1d5f80 @stealth - added -B base argument to lophttpd
authored
169 case 'B':
8d557cc @stealth - changed Config namespace to httpd_config
authored
170 httpd_config::base = optarg;
c1d5f80 @stealth - added -B base argument to lophttpd
authored
171 break;
5551cb5 @stealth - lophttpd: qdded quiet mode which doesnt log or drop index.html files
authored
172 case 'q':
173 httpd_config::quiet = 1;
174 break;
532bf93 @stealth - lophttpd: also return error if sendfile() returns 0 (can happen if …
authored
175 case 'U':
176 httpd_config::upload = optarg;
177 break;
178 case 'r':
179 httpd_config::rand_upload = 1;
180 break;
72239e3 @stealth - lophttpd: added -E 'no-error-kill' to keep connection alive even
authored
181 case 'Q':
182 httpd_config::rand_upload = 1;
183 httpd_config::rand_upload_quiet = 1;
184 break;
185 case 'E':
186 httpd_config::no_error_kill = 1;
187 break;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
188 case 'h':
189 default:
190 help(*argv);
191 }
192 }
193
5551cb5 @stealth - lophttpd: qdded quiet mode which doesnt log or drop index.html files
authored
194 cout<<"Using webroot '"<<httpd_config::root<<"' and user '"<<httpd_config::user
195 <<"'.\nRun with '-h' if you need help. Starting up ...\n\n";
196
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
197 uid_t euid = geteuid();
198
ea85fab @stealth Changed gmtime to localtime; every core gets his own logfile.
authored
199 tzset();
19e349a @stealth Set nice value.
authored
200 nice(-20);
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
201 close_fds();
202
8d557cc @stealth - changed Config namespace to httpd_config
authored
203 httpd = new (nothrow) lonely_http(httpd_config::mss);
c4b87fe @stealth - Added HINTS file
authored
204 if (!httpd) {
205 cerr<<"OOM: Cannot create webserver object!\n";
206 return -1;
207 }
208
8d557cc @stealth - changed Config namespace to httpd_config
authored
209 if (httpd->init(httpd_config::host, httpd_config::port, httpd_config::af) < 0) {
c4b87fe @stealth - Added HINTS file
authored
210 cerr<<httpd->why()<<endl;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
211 return -1;
212 }
213
ea85fab @stealth Changed gmtime to localtime; every core gets his own logfile.
authored
214 // Needs to be called before chroot
50b084e @stealth - merged lophttd and frontend Makefiles
authored
215 misc::init_multicore();
216 misc::setup_multicore(httpd_config::cores);
ea85fab @stealth Changed gmtime to localtime; every core gets his own logfile.
authored
217
218 // Every core has its own logfile to avoid locking
5551cb5 @stealth - lophttpd: qdded quiet mode which doesnt log or drop index.html files
authored
219 if (!httpd_config::quiet) {
220 if (httpd->open_log(httpd_config::logfile, httpd_config::log_provider, misc::my_core) < 0) {
221 cerr<<"ERROR: opening logfile: "<<httpd->why()<<endl;
222 cerr<<"continuing without logging!\n";
223 }
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
224 }
225
8d557cc @stealth - changed Config namespace to httpd_config
authored
226 struct passwd *pw = getpwnam(httpd_config::user.c_str());
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
227 if (!pw) {
8d557cc @stealth - changed Config namespace to httpd_config
authored
228 cerr<<"Fatal: Unknown user '"<<httpd_config::user<<"'. Exiting.\n";
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
229 return -1;
230 }
8d557cc @stealth - changed Config namespace to httpd_config
authored
231 httpd_config::user_uid = pw->pw_uid;
232 httpd_config::user_gid = pw->pw_gid;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
233
8d557cc @stealth - changed Config namespace to httpd_config
authored
234 if (chdir(httpd_config::root.c_str()) < 0)
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
235 die("chdir");
236
8d557cc @stealth - changed Config namespace to httpd_config
authored
237 if (chroot(httpd_config::root.c_str()) < 0) {
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
238 die("chroot", euid == 0);
239 } else
8d557cc @stealth - changed Config namespace to httpd_config
authored
240 httpd_config::is_chrooted = 1;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
241
8d557cc @stealth - changed Config namespace to httpd_config
authored
242 if (httpd_config::gen_index) {
243 if (httpd_config::is_chrooted)
50b084e @stealth - merged lophttd and frontend Makefiles
authored
244 misc::generate_index("/");
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
245 else
50b084e @stealth - merged lophttd and frontend Makefiles
authored
246 misc::generate_index(httpd_config::root);
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
247 }
248
249
250 if (setgid(pw->pw_gid) < 0)
251 die("setgid", euid == 0);
8d557cc @stealth - changed Config namespace to httpd_config
authored
252 if (initgroups(httpd_config::user.c_str(), pw->pw_gid) < 0)
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
253 die("initgroups", euid == 0);
254 if (setuid(pw->pw_uid) < 0)
255 die("setuid", euid == 0);
256
8d557cc @stealth - changed Config namespace to httpd_config
authored
257 if (httpd_config::virtual_hosts)
c4b87fe @stealth - Added HINTS file
authored
258 httpd->vhosts = 1;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
259
260 struct sigaction sa;
261 memset(&sa, 0, sizeof(sa));
262 sa.sa_handler = sigusr1;
263 sa.sa_flags = SA_RESTART;
264 if (sigaction(SIGUSR1, &sa, NULL) < 0)
265 die("sigaction");
266
267 sa.sa_handler = SIG_IGN;
268 if (sigaction(SIGHUP, &sa, NULL) < 0)
269 die("sigaction");
270
271 dup2(0, 2);
272
8d557cc @stealth - changed Config namespace to httpd_config
authored
273 if (httpd_config::master) {
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
274 if (fork() > 0)
275 exit(0);
276 setsid();
277 }
278
c4b87fe @stealth - Added HINTS file
authored
279 httpd->loop();
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
280
c4b87fe @stealth - Added HINTS file
authored
281 delete httpd;
7fe1662 @stealth Added source files, Makefile etc. from cvs tag rel-0-87.
authored
282 return 0;
283 }
284
Something went wrong with that request. Please try again.