]> git.saurik.com Git - apt.git/blob - test/interactive-helper/aptwebserver.cc
Merge remote-tracking branch 'upstream/debian/sid' into debian/sid
[apt.git] / test / interactive-helper / aptwebserver.cc
1 #include <config.h>
2
3 #include <apt-pkg/cmndline.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/fileutl.h>
7 #include <apt-pkg/strutl.h>
8
9 #include <dirent.h>
10 #include <errno.h>
11 #include <netinet/in.h>
12 #include <pthread.h>
13 #include <regex.h>
14 #include <signal.h>
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <iostream>
23 #include <sstream>
24 #include <list>
25 #include <string>
26 #include <vector>
27
28 static char const * httpcodeToStr(int const httpcode) /*{{{*/
29 {
30 switch (httpcode)
31 {
32 // Informational 1xx
33 case 100: return "100 Continue";
34 case 101: return "101 Switching Protocols";
35 // Successful 2xx
36 case 200: return "200 OK";
37 case 201: return "201 Created";
38 case 202: return "202 Accepted";
39 case 203: return "203 Non-Authoritative Information";
40 case 204: return "204 No Content";
41 case 205: return "205 Reset Content";
42 case 206: return "206 Partial Content";
43 // Redirections 3xx
44 case 300: return "300 Multiple Choices";
45 case 301: return "301 Moved Permanently";
46 case 302: return "302 Found";
47 case 303: return "303 See Other";
48 case 304: return "304 Not Modified";
49 case 305: return "304 Use Proxy";
50 case 307: return "307 Temporary Redirect";
51 // Client errors 4xx
52 case 400: return "400 Bad Request";
53 case 401: return "401 Unauthorized";
54 case 402: return "402 Payment Required";
55 case 403: return "403 Forbidden";
56 case 404: return "404 Not Found";
57 case 405: return "405 Method Not Allowed";
58 case 406: return "406 Not Acceptable";
59 case 407: return "407 Proxy Authentication Required";
60 case 408: return "408 Request Time-out";
61 case 409: return "409 Conflict";
62 case 410: return "410 Gone";
63 case 411: return "411 Length Required";
64 case 412: return "412 Precondition Failed";
65 case 413: return "413 Request Entity Too Large";
66 case 414: return "414 Request-URI Too Large";
67 case 415: return "415 Unsupported Media Type";
68 case 416: return "416 Requested range not satisfiable";
69 case 417: return "417 Expectation Failed";
70 case 418: return "418 I'm a teapot";
71 // Server error 5xx
72 case 500: return "500 Internal Server Error";
73 case 501: return "501 Not Implemented";
74 case 502: return "502 Bad Gateway";
75 case 503: return "503 Service Unavailable";
76 case 504: return "504 Gateway Time-out";
77 case 505: return "505 HTTP Version not supported";
78 }
79 return NULL;
80 }
81 /*}}}*/
82 static void addFileHeaders(std::list<std::string> &headers, FileFd &data)/*{{{*/
83 {
84 std::ostringstream contentlength;
85 contentlength << "Content-Length: " << data.FileSize();
86 headers.push_back(contentlength.str());
87
88 std::string lastmodified("Last-Modified: ");
89 lastmodified.append(TimeRFC1123(data.ModificationTime()));
90 headers.push_back(lastmodified);
91 }
92 /*}}}*/
93 static void addDataHeaders(std::list<std::string> &headers, std::string &data)/*{{{*/
94 {
95 std::ostringstream contentlength;
96 contentlength << "Content-Length: " << data.size();
97 headers.push_back(contentlength.str());
98 }
99 /*}}}*/
100 static bool sendHead(int const client, int const httpcode, std::list<std::string> &headers)/*{{{*/
101 {
102 std::string response("HTTP/1.1 ");
103 response.append(httpcodeToStr(httpcode));
104 headers.push_front(response);
105 _config->Set("APTWebserver::Last-Status-Code", httpcode);
106
107 std::stringstream buffer;
108 _config->Dump(buffer, "aptwebserver::response-header", "%t: %v%n", false);
109 std::vector<std::string> addheaders = VectorizeString(buffer.str(), '\n');
110 for (std::vector<std::string>::const_iterator h = addheaders.begin(); h != addheaders.end(); ++h)
111 headers.push_back(*h);
112
113 std::string date("Date: ");
114 date.append(TimeRFC1123(time(NULL)));
115 headers.push_back(date);
116
117 std::clog << ">>> RESPONSE to " << client << " >>>" << std::endl;
118 bool Success = true;
119 for (std::list<std::string>::const_iterator h = headers.begin();
120 Success == true && h != headers.end(); ++h)
121 {
122 Success &= FileFd::Write(client, h->c_str(), h->size());
123 if (Success == true)
124 Success &= FileFd::Write(client, "\r\n", 2);
125 std::clog << *h << std::endl;
126 }
127 if (Success == true)
128 Success &= FileFd::Write(client, "\r\n", 2);
129 std::clog << "<<<<<<<<<<<<<<<<" << std::endl;
130 return Success;
131 }
132 /*}}}*/
133 static bool sendFile(int const client, FileFd &data) /*{{{*/
134 {
135 bool Success = true;
136 char buffer[500];
137 unsigned long long actual = 0;
138 while ((Success &= data.Read(buffer, sizeof(buffer), &actual)) == true)
139 {
140 if (actual == 0)
141 break;
142 Success &= FileFd::Write(client, buffer, actual);
143 }
144 if (Success == false)
145 std::cerr << "SENDFILE: READ/WRITE ERROR to " << client << std::endl;
146 return Success;
147 }
148 /*}}}*/
149 static bool sendData(int const client, std::string const &data) /*{{{*/
150 {
151 if (FileFd::Write(client, data.c_str(), data.size()) == false)
152 {
153 std::cerr << "SENDDATA: WRITE ERROR to " << client << std::endl;
154 return false;
155 }
156 return true;
157 }
158 /*}}}*/
159 static void sendError(int const client, int const httpcode, std::string const &request,/*{{{*/
160 bool content, std::string const &error = "")
161 {
162 std::list<std::string> headers;
163 std::string response("<html><head><title>");
164 response.append(httpcodeToStr(httpcode)).append("</title></head>");
165 response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1>");
166 if (httpcode != 200)
167 {
168 if (error.empty() == false)
169 response.append("<p><em>Error</em>: ").append(error).append("</p>");
170 response.append("This error is a result of the request: <pre>");
171 }
172 else
173 {
174 if (error.empty() == false)
175 response.append("<p><em>Success</em>: ").append(error).append("</p>");
176 response.append("The successfully executed operation was requested by: <pre>");
177 }
178 response.append(request).append("</pre></body></html>");
179 addDataHeaders(headers, response);
180 sendHead(client, httpcode, headers);
181 if (content == true)
182 sendData(client, response);
183 }
184 static void sendSuccess(int const client, std::string const &request,
185 bool content, std::string const &error = "")
186 {
187 sendError(client, 200, request, content, error);
188 }
189 /*}}}*/
190 static void sendRedirect(int const client, int const httpcode, std::string const &uri,/*{{{*/
191 std::string const &request, bool content)
192 {
193 std::list<std::string> headers;
194 std::string response("<html><head><title>");
195 response.append(httpcodeToStr(httpcode)).append("</title></head>");
196 response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1");
197 response.append("<p>You should be redirected to <em>").append(uri).append("</em></p>");
198 response.append("This page is a result of the request: <pre>");
199 response.append(request).append("</pre></body></html>");
200 addDataHeaders(headers, response);
201 std::string location("Location: ");
202 if (strncmp(uri.c_str(), "http://", 7) != 0 && strncmp(uri.c_str(), "https://", 8) != 0)
203 {
204 std::string const host = LookupTag(request, "Host");
205 if (host.find(":4433") != std::string::npos)
206 location.append("https://");
207 else
208 location.append("http://");
209 location.append(host).append("/");
210 if (strncmp("/home/", uri.c_str(), strlen("/home/")) == 0 && uri.find("/public_html/") != std::string::npos)
211 {
212 std::string homeuri = SubstVar(uri, "/home/", "~");
213 homeuri = SubstVar(homeuri, "/public_html/", "/");
214 location.append(homeuri);
215 }
216 else
217 location.append(uri);
218 }
219 else
220 location.append(uri);
221 headers.push_back(location);
222 sendHead(client, httpcode, headers);
223 if (content == true)
224 sendData(client, response);
225 }
226 /*}}}*/
227 static int filter_hidden_files(const struct dirent *a) /*{{{*/
228 {
229 if (a->d_name[0] == '.')
230 return 0;
231 #ifdef _DIRENT_HAVE_D_TYPE
232 // if we have the d_type check that only files and dirs will be included
233 if (a->d_type != DT_UNKNOWN &&
234 a->d_type != DT_REG &&
235 a->d_type != DT_LNK && // this includes links to regular files
236 a->d_type != DT_DIR)
237 return 0;
238 #endif
239 return 1;
240 }
241 static int grouped_alpha_case_sort(const struct dirent **a, const struct dirent **b) {
242 #ifdef _DIRENT_HAVE_D_TYPE
243 if ((*a)->d_type == DT_DIR && (*b)->d_type == DT_DIR);
244 else if ((*a)->d_type == DT_DIR && (*b)->d_type == DT_REG)
245 return -1;
246 else if ((*b)->d_type == DT_DIR && (*a)->d_type == DT_REG)
247 return 1;
248 else
249 #endif
250 {
251 struct stat f_prop; //File's property
252 stat((*a)->d_name, &f_prop);
253 int const amode = f_prop.st_mode;
254 stat((*b)->d_name, &f_prop);
255 int const bmode = f_prop.st_mode;
256 if (S_ISDIR(amode) && S_ISDIR(bmode));
257 else if (S_ISDIR(amode))
258 return -1;
259 else if (S_ISDIR(bmode))
260 return 1;
261 }
262 return strcasecmp((*a)->d_name, (*b)->d_name);
263 }
264 /*}}}*/
265 static void sendDirectoryListing(int const client, std::string const &dir,/*{{{*/
266 std::string const &request, bool content)
267 {
268 std::list<std::string> headers;
269 std::ostringstream listing;
270
271 struct dirent **namelist;
272 int const counter = scandir(dir.c_str(), &namelist, filter_hidden_files, grouped_alpha_case_sort);
273 if (counter == -1)
274 {
275 sendError(client, 500, request, content);
276 return;
277 }
278
279 listing << "<html><head><title>Index of " << dir << "</title>"
280 << "<style type=\"text/css\"><!-- td {padding: 0.02em 0.5em 0.02em 0.5em;}"
281 << "tr:nth-child(even){background-color:#dfdfdf;}"
282 << "h1, td:nth-child(3){text-align:center;}"
283 << "table {margin-left:auto;margin-right:auto;} --></style>"
284 << "</head>" << std::endl
285 << "<body><h1>Index of " << dir << "</h1>" << std::endl
286 << "<table><tr><th>#</th><th>Name</th><th>Size</th><th>Last-Modified</th></tr>" << std::endl;
287 if (dir != "./")
288 listing << "<tr><td>d</td><td><a href=\"..\">Parent Directory</a></td><td>-</td><td>-</td></tr>";
289 for (int i = 0; i < counter; ++i) {
290 struct stat fs;
291 std::string filename(dir);
292 filename.append("/").append(namelist[i]->d_name);
293 stat(filename.c_str(), &fs);
294 if (S_ISDIR(fs.st_mode))
295 {
296 listing << "<tr><td>d</td>"
297 << "<td><a href=\"" << namelist[i]->d_name << "/\">" << namelist[i]->d_name << "</a></td>"
298 << "<td>-</td>";
299 }
300 else
301 {
302 listing << "<tr><td>f</td>"
303 << "<td><a href=\"" << namelist[i]->d_name << "\">" << namelist[i]->d_name << "</a></td>"
304 << "<td>" << SizeToStr(fs.st_size) << "B</td>";
305 }
306 listing << "<td>" << TimeRFC1123(fs.st_mtime) << "</td></tr>" << std::endl;
307 }
308 listing << "</table></body></html>" << std::endl;
309
310 std::string response(listing.str());
311 addDataHeaders(headers, response);
312 sendHead(client, 200, headers);
313 if (content == true)
314 sendData(client, response);
315 }
316 /*}}}*/
317 static bool parseFirstLine(int const client, std::string const &request,/*{{{*/
318 std::string &filename, std::string &params, bool &sendContent,
319 bool &closeConnection)
320 {
321 if (strncmp(request.c_str(), "HEAD ", 5) == 0)
322 sendContent = false;
323 if (strncmp(request.c_str(), "GET ", 4) != 0)
324 {
325 sendError(client, 501, request, true);
326 return false;
327 }
328
329 size_t const lineend = request.find('\n');
330 size_t filestart = request.find(' ');
331 for (; request[filestart] == ' '; ++filestart);
332 size_t fileend = request.rfind(' ', lineend);
333 if (lineend == std::string::npos || filestart == std::string::npos ||
334 fileend == std::string::npos || filestart == fileend)
335 {
336 sendError(client, 500, request, sendContent, "Filename can't be extracted");
337 return false;
338 }
339
340 size_t httpstart = fileend;
341 for (; request[httpstart] == ' '; ++httpstart);
342 if (strncmp(request.c_str() + httpstart, "HTTP/1.1\r", 9) == 0)
343 closeConnection = strcasecmp(LookupTag(request, "Connection", "Keep-Alive").c_str(), "Keep-Alive") != 0;
344 else if (strncmp(request.c_str() + httpstart, "HTTP/1.0\r", 9) == 0)
345 closeConnection = strcasecmp(LookupTag(request, "Connection", "Keep-Alive").c_str(), "close") == 0;
346 else
347 {
348 sendError(client, 500, request, sendContent, "Not a HTTP/1.{0,1} request");
349 return false;
350 }
351
352 filename = request.substr(filestart, fileend - filestart);
353 if (filename.find(' ') != std::string::npos)
354 {
355 sendError(client, 500, request, sendContent, "Filename contains an unencoded space");
356 return false;
357 }
358
359 std::string host = LookupTag(request, "Host", "");
360 if (host.empty() == true)
361 {
362 // RFC 2616 §14.23 requires Host
363 sendError(client, 400, request, sendContent, "Host header is required");
364 return false;
365 }
366 host = "http://" + host;
367
368 // Proxies require absolute uris, so this is a simple proxy-fake option
369 std::string const absolute = _config->Find("aptwebserver::request::absolute", "uri,path");
370 if (strncmp(host.c_str(), filename.c_str(), host.length()) == 0)
371 {
372 if (absolute.find("uri") == std::string::npos)
373 {
374 sendError(client, 400, request, sendContent, "Request is absoluteURI, but configured to not accept that");
375 return false;
376 }
377 // strip the host from the request to make it an absolute path
378 filename.erase(0, host.length());
379 }
380 else if (absolute.find("path") == std::string::npos)
381 {
382 sendError(client, 400, request, sendContent, "Request is absolutePath, but configured to not accept that");
383 return false;
384 }
385
386 size_t paramspos = filename.find('?');
387 if (paramspos != std::string::npos)
388 {
389 params = filename.substr(paramspos + 1);
390 filename.erase(paramspos);
391 }
392
393 filename = DeQuoteString(filename);
394
395 // this is not a secure server, but at least prevent the obvious …
396 if (filename.empty() == true || filename[0] != '/' ||
397 strncmp(filename.c_str(), "//", 2) == 0 ||
398 filename.find_first_of("\r\n\t\f\v") != std::string::npos ||
399 filename.find("/../") != std::string::npos)
400 {
401 sendError(client, 400, request, sendContent, "Filename contains illegal character (sequence)");
402 return false;
403 }
404
405 // nuke the first character which is a / as we assured above
406 filename.erase(0, 1);
407 if (filename.empty() == true)
408 filename = "./";
409 // support ~user/ uris to refer to /home/user/public_html/ as a kind-of special directory
410 else if (filename[0] == '~')
411 {
412 // /home/user is actually not entirely correct, but good enough for now
413 size_t dashpos = filename.find('/');
414 if (dashpos != std::string::npos)
415 {
416 std::string home = filename.substr(1, filename.find('/') - 1);
417 std::string pubhtml = filename.substr(filename.find('/') + 1);
418 filename = "/home/" + home + "/public_html/" + pubhtml;
419 }
420 else
421 filename = "/home/" + filename.substr(1) + "/public_html/";
422 }
423
424 // if no filename is given, but a valid directory see if we can use an index or
425 // have to resort to a autogenerated directory listing later on
426 if (DirectoryExists(filename) == true)
427 {
428 std::string const directoryIndex = _config->Find("aptwebserver::directoryindex");
429 if (directoryIndex.empty() == false && directoryIndex == flNotDir(directoryIndex) &&
430 RealFileExists(filename + directoryIndex) == true)
431 filename += directoryIndex;
432 }
433
434 return true;
435 }
436 /*}}}*/
437 static bool handleOnTheFlyReconfiguration(int const client, std::string const &request, std::vector<std::string> const &parts)/*{{{*/
438 {
439 size_t const pcount = parts.size();
440 if (pcount == 4 && parts[1] == "set")
441 {
442 _config->Set(parts[2], parts[3]);
443 sendSuccess(client, request, true, "Option '" + parts[2] + "' was set to '" + parts[3] + "'!");
444 return true;
445 }
446 else if (pcount == 4 && parts[1] == "find")
447 {
448 std::list<std::string> headers;
449 std::string response = _config->Find(parts[2], parts[3]);
450 addDataHeaders(headers, response);
451 sendHead(client, 200, headers);
452 sendData(client, response);
453 return true;
454 }
455 else if (pcount == 3 && parts[1] == "find")
456 {
457 std::list<std::string> headers;
458 if (_config->Exists(parts[2]) == true)
459 {
460 std::string response = _config->Find(parts[2]);
461 addDataHeaders(headers, response);
462 sendHead(client, 200, headers);
463 sendData(client, response);
464 return true;
465 }
466 sendError(client, 404, request, "Requested Configuration option doesn't exist.");
467 return false;
468 }
469 else if (pcount == 3 && parts[1] == "clear")
470 {
471 _config->Clear(parts[2]);
472 sendSuccess(client, request, true, "Option '" + parts[2] + "' was cleared.");
473 return true;
474 }
475
476 sendError(client, 400, request, true, "Unknown on-the-fly configuration request");
477 return false;
478 }
479 /*}}}*/
480 static void * handleClient(void * voidclient) /*{{{*/
481 {
482 int client = *((int*)(voidclient));
483 std::clog << "ACCEPT client " << client << std::endl;
484 std::vector<std::string> messages;
485 while (ReadMessages(client, messages))
486 {
487 bool closeConnection = false;
488 for (std::vector<std::string>::const_iterator m = messages.begin();
489 m != messages.end() && closeConnection == false; ++m) {
490 std::clog << ">>> REQUEST from " << client << " >>>" << std::endl << *m
491 << std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
492 std::list<std::string> headers;
493 std::string filename;
494 std::string params;
495 bool sendContent = true;
496 if (parseFirstLine(client, *m, filename, params, sendContent, closeConnection) == false)
497 continue;
498
499 // special webserver command request
500 if (filename.length() > 1 && filename[0] == '_')
501 {
502 std::vector<std::string> parts = VectorizeString(filename, '/');
503 if (parts[0] == "_config")
504 {
505 handleOnTheFlyReconfiguration(client, *m, parts);
506 continue;
507 }
508 }
509
510 // string replacements in the requested filename
511 ::Configuration::Item const *Replaces = _config->Tree("aptwebserver::redirect::replace");
512 if (Replaces != NULL)
513 {
514 std::string redirect = "/" + filename;
515 for (::Configuration::Item *I = Replaces->Child; I != NULL; I = I->Next)
516 redirect = SubstVar(redirect, I->Tag, I->Value);
517 if (redirect.empty() == false && redirect[0] == '/')
518 redirect.erase(0,1);
519 if (redirect != filename)
520 {
521 sendRedirect(client, 301, redirect, *m, sendContent);
522 continue;
523 }
524 }
525
526 ::Configuration::Item const *Overwrite = _config->Tree("aptwebserver::overwrite");
527 if (Overwrite != NULL)
528 {
529 for (::Configuration::Item *I = Overwrite->Child; I != NULL; I = I->Next)
530 {
531 regex_t *pattern = new regex_t;
532 int const res = regcomp(pattern, I->Tag.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
533 if (res != 0)
534 {
535 char error[300];
536 regerror(res, pattern, error, sizeof(error));
537 sendError(client, 500, *m, sendContent, error);
538 continue;
539 }
540 if (regexec(pattern, filename.c_str(), 0, 0, 0) == 0)
541 {
542 filename = _config->Find("aptwebserver::overwrite::" + I->Tag + "::filename", filename);
543 if (filename[0] == '/')
544 filename.erase(0,1);
545 regfree(pattern);
546 break;
547 }
548 regfree(pattern);
549 }
550 }
551
552 // deal with the request
553 if (_config->FindB("aptwebserver::support::http", true) == false &&
554 LookupTag(*m, "Host").find(":4433") == std::string::npos)
555 {
556 sendError(client, 400, *m, sendContent, "HTTP disabled, all requests must be HTTPS");
557 continue;
558 }
559 else if (RealFileExists(filename) == true)
560 {
561 FileFd data(filename, FileFd::ReadOnly);
562 std::string condition = LookupTag(*m, "If-Modified-Since", "");
563 if (_config->FindB("aptwebserver::support::modified-since", true) == true && condition.empty() == false)
564 {
565 time_t cache;
566 if (RFC1123StrToTime(condition.c_str(), cache) == true &&
567 cache >= data.ModificationTime())
568 {
569 sendHead(client, 304, headers);
570 continue;
571 }
572 }
573
574 if (_config->FindB("aptwebserver::support::range", true) == true)
575 condition = LookupTag(*m, "Range", "");
576 else
577 condition.clear();
578 if (condition.empty() == false && strncmp(condition.c_str(), "bytes=", 6) == 0)
579 {
580 time_t cache;
581 std::string ifrange;
582 if (_config->FindB("aptwebserver::support::if-range", true) == true)
583 ifrange = LookupTag(*m, "If-Range", "");
584 bool validrange = (ifrange.empty() == true ||
585 (RFC1123StrToTime(ifrange.c_str(), cache) == true &&
586 cache <= data.ModificationTime()));
587
588 // FIXME: support multiple byte-ranges (APT clients do not do this)
589 if (condition.find(',') == std::string::npos)
590 {
591 size_t start = 6;
592 unsigned long long filestart = strtoull(condition.c_str() + start, NULL, 10);
593 // FIXME: no support for last-byte-pos being not the end of the file (APT clients do not do this)
594 size_t dash = condition.find('-') + 1;
595 unsigned long long fileend = strtoull(condition.c_str() + dash, NULL, 10);
596 unsigned long long filesize = data.FileSize();
597 if ((fileend == 0 || (fileend == filesize && fileend >= filestart)) &&
598 validrange == true)
599 {
600 if (filesize > filestart)
601 {
602 data.Skip(filestart);
603 std::ostringstream contentlength;
604 contentlength << "Content-Length: " << (filesize - filestart);
605 headers.push_back(contentlength.str());
606 std::ostringstream contentrange;
607 contentrange << "Content-Range: bytes " << filestart << "-"
608 << filesize - 1 << "/" << filesize;
609 headers.push_back(contentrange.str());
610 sendHead(client, 206, headers);
611 if (sendContent == true)
612 sendFile(client, data);
613 continue;
614 }
615 else
616 {
617 headers.push_back("Content-Length: 0");
618 std::ostringstream contentrange;
619 contentrange << "Content-Range: bytes */" << filesize;
620 headers.push_back(contentrange.str());
621 sendHead(client, 416, headers);
622 continue;
623 }
624 }
625 }
626 }
627
628 addFileHeaders(headers, data);
629 sendHead(client, 200, headers);
630 if (sendContent == true)
631 sendFile(client, data);
632 }
633 else if (DirectoryExists(filename) == true)
634 {
635 if (filename[filename.length()-1] == '/')
636 sendDirectoryListing(client, filename, *m, sendContent);
637 else
638 sendRedirect(client, 301, filename.append("/"), *m, sendContent);
639 }
640 else
641 sendError(client, 404, *m, sendContent);
642 }
643 _error->DumpErrors(std::cerr);
644 messages.clear();
645 if (closeConnection == true)
646 break;
647 }
648 close(client);
649 std::clog << "CLOSE client " << client << std::endl;
650 return NULL;
651 }
652 /*}}}*/
653
654 int main(int const argc, const char * argv[])
655 {
656 CommandLine::Args Args[] = {
657 {0, "port", "aptwebserver::port", CommandLine::HasArg},
658 {0, "request-absolute", "aptwebserver::request::absolute", CommandLine::HasArg},
659 {'c',"config-file",0,CommandLine::ConfigFile},
660 {'o',"option",0,CommandLine::ArbItem},
661 {0,0,0,0}
662 };
663
664 CommandLine CmdL(Args, _config);
665 if(CmdL.Parse(argc,argv) == false)
666 {
667 _error->DumpErrors();
668 exit(1);
669 }
670
671 // create socket, bind and listen to it {{{
672 // ignore SIGPIPE, this can happen on write() if the socket closes connection
673 signal(SIGPIPE, SIG_IGN);
674 // we don't care for our slaves, so ignore their death
675 signal(SIGCHLD, SIG_IGN);
676
677 int sock = socket(AF_INET6, SOCK_STREAM, 0);
678 if(sock < 0)
679 {
680 _error->Errno("aptwerbserver", "Couldn't create socket");
681 _error->DumpErrors(std::cerr);
682 return 1;
683 }
684
685 int const port = _config->FindI("aptwebserver::port", 8080);
686
687 // ensure that we accept all connections: v4 or v6
688 int const iponly = 0;
689 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &iponly, sizeof(iponly));
690 // to not linger on an address
691 int const enable = 1;
692 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
693
694 struct sockaddr_in6 locAddr;
695 memset(&locAddr, 0, sizeof(locAddr));
696 locAddr.sin6_family = AF_INET6;
697 locAddr.sin6_port = htons(port);
698 locAddr.sin6_addr = in6addr_any;
699
700 if (bind(sock, (struct sockaddr*) &locAddr, sizeof(locAddr)) < 0)
701 {
702 _error->Errno("aptwerbserver", "Couldn't bind");
703 _error->DumpErrors(std::cerr);
704 return 2;
705 }
706
707 FileFd pidfile;
708 if (_config->FindB("aptwebserver::fork", false) == true)
709 {
710 std::string const pidfilename = _config->Find("aptwebserver::pidfile", "aptwebserver.pid");
711 int const pidfilefd = GetLock(pidfilename);
712 if (pidfilefd < 0 || pidfile.OpenDescriptor(pidfilefd, FileFd::WriteOnly) == false)
713 {
714 _error->Errno("aptwebserver", "Couldn't acquire lock on pidfile '%s'", pidfilename.c_str());
715 _error->DumpErrors(std::cerr);
716 return 3;
717 }
718
719 pid_t child = fork();
720 if (child < 0)
721 {
722 _error->Errno("aptwebserver", "Forking failed");
723 _error->DumpErrors(std::cerr);
724 return 4;
725 }
726 else if (child != 0)
727 {
728 // successfully forked: ready to serve!
729 std::string pidcontent;
730 strprintf(pidcontent, "%d", child);
731 pidfile.Write(pidcontent.c_str(), pidcontent.size());
732 if (_error->PendingError() == true)
733 {
734 _error->DumpErrors(std::cerr);
735 return 5;
736 }
737 std::cout << "Successfully forked as " << child << std::endl;
738 return 0;
739 }
740 }
741
742 std::clog << "Serving ANY file on port: " << port << std::endl;
743
744 int const slaves = _config->FindB("aptwebserver::slaves", SOMAXCONN);
745 listen(sock, slaves);
746 /*}}}*/
747
748 _config->CndSet("aptwebserver::response-header::Server", "APT webserver");
749 _config->CndSet("aptwebserver::response-header::Accept-Ranges", "bytes");
750 _config->CndSet("aptwebserver::directoryindex", "index.html");
751
752 std::list<int> accepted_clients;
753
754 while (true)
755 {
756 int client = accept(sock, NULL, NULL);
757 if (client == -1)
758 {
759 if (errno == EINTR)
760 continue;
761 _error->Errno("accept", "Couldn't accept client on socket %d", sock);
762 _error->DumpErrors(std::cerr);
763 return 6;
764 }
765
766 pthread_attr_t attr;
767 if (pthread_attr_init(&attr) != 0 || pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
768 {
769 _error->Errno("pthread_attr", "Couldn't set detach attribute for a fresh thread to handle client %d on socket %d", client, sock);
770 _error->DumpErrors(std::cerr);
771 close(client);
772 continue;
773 }
774
775 pthread_t tid;
776 // thats rather dirty, but we need to store the client socket somewhere safe
777 accepted_clients.push_front(client);
778 if (pthread_create(&tid, &attr, &handleClient, &(*accepted_clients.begin())) != 0)
779 {
780 _error->Errno("pthread_create", "Couldn't create a fresh thread to handle client %d on socket %d", client, sock);
781 _error->DumpErrors(std::cerr);
782 close(client);
783 continue;
784 }
785 }
786 pidfile.Close();
787
788 return 0;
789 }