aptwebserver.cc 36 KB

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