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