You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

490 lines
14 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-method.cc,v 1.27.2.1 2003/12/24 23:09:17 mdz Exp $
  4. /* ######################################################################
  5. Acquire Method
  6. This is a skeleton class that implements most of the functionality
  7. of a method and some useful functions to make method implementation
  8. simpler. The methods all derive this and specialize it. The most
  9. complex implementation is the http method which needs to provide
  10. pipelining, it runs the message engine at the same time it is
  11. downloading files..
  12. ##################################################################### */
  13. /*}}}*/
  14. // Include Files /*{{{*/
  15. #include <config.h>
  16. #include <apt-pkg/acquire-method.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/configuration.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/fileutl.h>
  21. #include <apt-pkg/hashes.h>
  22. #include <apt-pkg/md5.h>
  23. #include <apt-pkg/sha1.h>
  24. #include <apt-pkg/sha2.h>
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <string>
  30. #include <vector>
  31. #include <iostream>
  32. #include <stdio.h>
  33. /*}}}*/
  34. using namespace std;
  35. // AcqMethod::pkgAcqMethod - Constructor /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* This constructs the initialization text */
  38. pkgAcqMethod::pkgAcqMethod(const char *Ver,unsigned long Flags)
  39. {
  40. std::cout << "100 Capabilities\n"
  41. << "Version: " << Ver << "\n";
  42. if ((Flags & SingleInstance) == SingleInstance)
  43. std::cout << "Single-Instance: true\n";
  44. if ((Flags & Pipeline) == Pipeline)
  45. std::cout << "Pipeline: true\n";
  46. if ((Flags & SendConfig) == SendConfig)
  47. std::cout << "Send-Config: true\n";
  48. if ((Flags & LocalOnly) == LocalOnly)
  49. std::cout <<"Local-Only: true\n";
  50. if ((Flags & NeedsCleanup) == NeedsCleanup)
  51. std::cout << "Needs-Cleanup: true\n";
  52. if ((Flags & Removable) == Removable)
  53. std::cout << "Removable: true\n";
  54. std::cout << "\n" << std::flush;
  55. SetNonBlock(STDIN_FILENO,true);
  56. Queue = 0;
  57. QueueBack = 0;
  58. }
  59. /*}}}*/
  60. // AcqMethod::Fail - A fetch has failed /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* */
  63. void pkgAcqMethod::Fail(bool Transient)
  64. {
  65. string Err = "Undetermined Error";
  66. if (_error->empty() == false)
  67. _error->PopMessage(Err);
  68. _error->Discard();
  69. Fail(Err,Transient);
  70. }
  71. /*}}}*/
  72. // AcqMethod::Fail - A fetch has failed /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* */
  75. void pkgAcqMethod::Fail(string Err,bool Transient)
  76. {
  77. // Strip out junk from the error messages
  78. for (string::iterator I = Err.begin(); I != Err.end(); ++I)
  79. {
  80. if (*I == '\r')
  81. *I = ' ';
  82. if (*I == '\n')
  83. *I = ' ';
  84. }
  85. if (Queue != 0)
  86. {
  87. std::cout << "400 URI Failure\nURI: " << Queue->Uri << "\n"
  88. << "Message: " << Err;
  89. if (IP.empty() == false && _config->FindB("Acquire::Failure::ShowIP", true) == true)
  90. std::cout << " " << IP;
  91. std::cout << "\n";
  92. Dequeue();
  93. }
  94. else
  95. std::cout << "400 URI Failure\nURI: <UNKNOWN>\nMessage: " << Err << "\n";
  96. if(FailReason.empty() == false)
  97. std::cout << "FailReason: " << FailReason << "\n";
  98. if (UsedMirror.empty() == false)
  99. std::cout << "UsedMirror: " << UsedMirror << "\n";
  100. // Set the transient flag
  101. if (Transient == true)
  102. std::cout << "Transient-Failure: true\n";
  103. std::cout << "\n" << std::flush;
  104. }
  105. /*}}}*/
  106. // AcqMethod::DropPrivsOrDie - Drop privileges or die /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* */
  109. void pkgAcqMethod::DropPrivsOrDie()
  110. {
  111. if (!DropPrivileges()) {
  112. Fail(false);
  113. exit(112); /* call the european emergency number */
  114. }
  115. }
  116. /*}}}*/
  117. // AcqMethod::URIStart - Indicate a download is starting /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* */
  120. void pkgAcqMethod::URIStart(FetchResult &Res)
  121. {
  122. if (Queue == 0)
  123. abort();
  124. std::cout << "200 URI Start\n"
  125. << "URI: " << Queue->Uri << "\n";
  126. if (Res.Size != 0)
  127. std::cout << "Size: " << std::to_string(Res.Size) << "\n";
  128. if (Res.LastModified != 0)
  129. std::cout << "Last-Modified: " << TimeRFC1123(Res.LastModified) << "\n";
  130. if (Res.ResumePoint != 0)
  131. std::cout << "Resume-Point: " << std::to_string(Res.ResumePoint) << "\n";
  132. if (UsedMirror.empty() == false)
  133. std::cout << "UsedMirror: " << UsedMirror << "\n";
  134. std::cout << "\n" << std::flush;
  135. }
  136. /*}}}*/
  137. // AcqMethod::URIDone - A URI is finished /*{{{*/
  138. // ---------------------------------------------------------------------
  139. /* */
  140. static void printHashStringList(HashStringList const * const list)
  141. {
  142. for (HashStringList::const_iterator hash = list->begin(); hash != list->end(); ++hash)
  143. {
  144. // very old compatibility name for MD5Sum
  145. if (hash->HashType() == "MD5Sum")
  146. std::cout << "MD5-Hash: " << hash->HashValue() << "\n";
  147. std::cout << hash->HashType() << "-Hash: " << hash->HashValue() << "\n";
  148. }
  149. }
  150. void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
  151. {
  152. if (Queue == 0)
  153. abort();
  154. std::cout << "201 URI Done\n"
  155. << "URI: " << Queue->Uri << "\n";
  156. if (Res.Filename.empty() == false)
  157. std::cout << "Filename: " << Res.Filename << "\n";
  158. if (Res.Size != 0)
  159. std::cout << "Size: " << std::to_string(Res.Size) << "\n";
  160. if (Res.LastModified != 0)
  161. std::cout << "Last-Modified: " << TimeRFC1123(Res.LastModified) << "\n";
  162. printHashStringList(&Res.Hashes);
  163. if (UsedMirror.empty() == false)
  164. std::cout << "UsedMirror: " << UsedMirror << "\n";
  165. if (Res.GPGVOutput.empty() == false)
  166. {
  167. std::cout << "GPGVOutput:\n";
  168. for (vector<string>::const_iterator I = Res.GPGVOutput.begin();
  169. I != Res.GPGVOutput.end(); ++I)
  170. std::cout << " " << *I << "\n";
  171. }
  172. if (Res.ResumePoint != 0)
  173. std::cout << "Resume-Point: " << std::to_string(Res.ResumePoint) << "\n";
  174. if (Res.IMSHit == true)
  175. std::cout << "IMS-Hit: true\n";
  176. if (Alt != 0)
  177. {
  178. if (Alt->Filename.empty() == false)
  179. std::cout << "Alt-Filename: " << Alt->Filename << "\n";
  180. if (Alt->Size != 0)
  181. std::cout << "Alt-Size: " << std::to_string(Alt->Size) << "\n";
  182. if (Alt->LastModified != 0)
  183. std::cout << "Alt-Last-Modified: " << TimeRFC1123(Alt->LastModified) << "\n";
  184. printHashStringList(&Alt->Hashes);
  185. if (Alt->IMSHit == true)
  186. std::cout << "Alt-IMS-Hit: true\n";
  187. }
  188. std::cout << "\n" << std::flush;
  189. Dequeue();
  190. }
  191. /*}}}*/
  192. // AcqMethod::MediaFail - Syncronous request for new media /*{{{*/
  193. // ---------------------------------------------------------------------
  194. /* This sends a 403 Media Failure message to the APT and waits for it
  195. to be ackd */
  196. bool pkgAcqMethod::MediaFail(string Required,string Drive)
  197. {
  198. fprintf(stdout, "403 Media Failure\nMedia: %s\nDrive: %s\n",
  199. Required.c_str(),Drive.c_str());
  200. std::cout << "\n" << std::flush;
  201. vector<string> MyMessages;
  202. /* Here we read messages until we find a 603, each non 603 message is
  203. appended to the main message list for later processing */
  204. while (1)
  205. {
  206. if (WaitFd(STDIN_FILENO) == false)
  207. return false;
  208. if (ReadMessages(STDIN_FILENO,MyMessages) == false)
  209. return false;
  210. string Message = MyMessages.front();
  211. MyMessages.erase(MyMessages.begin());
  212. // Fetch the message number
  213. char *End;
  214. int Number = strtol(Message.c_str(),&End,10);
  215. if (End == Message.c_str())
  216. {
  217. cerr << "Malformed message!" << endl;
  218. exit(100);
  219. }
  220. // Change ack
  221. if (Number == 603)
  222. {
  223. while (MyMessages.empty() == false)
  224. {
  225. Messages.push_back(MyMessages.front());
  226. MyMessages.erase(MyMessages.begin());
  227. }
  228. return !StringToBool(LookupTag(Message,"Failed"),false);
  229. }
  230. Messages.push_back(Message);
  231. }
  232. }
  233. /*}}}*/
  234. // AcqMethod::Configuration - Handle the configuration message /*{{{*/
  235. // ---------------------------------------------------------------------
  236. /* This parses each configuration entry and puts it into the _config
  237. Configuration class. */
  238. bool pkgAcqMethod::Configuration(string Message)
  239. {
  240. ::Configuration &Cnf = *_config;
  241. const char *I = Message.c_str();
  242. const char *MsgEnd = I + Message.length();
  243. unsigned int Length = strlen("Config-Item");
  244. for (; I + Length < MsgEnd; I++)
  245. {
  246. // Not a config item
  247. if (I[Length] != ':' || stringcasecmp(I,I+Length,"Config-Item") != 0)
  248. continue;
  249. I += Length + 1;
  250. for (; I < MsgEnd && *I == ' '; I++);
  251. const char *Equals = (const char*) memchr(I, '=', MsgEnd - I);
  252. if (Equals == NULL)
  253. return false;
  254. const char *End = (const char*) memchr(Equals, '\n', MsgEnd - Equals);
  255. if (End == NULL)
  256. End = MsgEnd;
  257. Cnf.Set(DeQuoteString(string(I,Equals-I)),
  258. DeQuoteString(string(Equals+1,End-Equals-1)));
  259. I = End;
  260. }
  261. return true;
  262. }
  263. /*}}}*/
  264. // AcqMethod::Run - Run the message engine /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* Fetch any messages and execute them. In single mode it returns 1 if
  267. there are no more available messages - any other result is a
  268. fatal failure code! */
  269. int pkgAcqMethod::Run(bool Single)
  270. {
  271. while (1)
  272. {
  273. // Block if the message queue is empty
  274. if (Messages.empty() == true)
  275. {
  276. if (Single == false)
  277. if (WaitFd(STDIN_FILENO) == false)
  278. break;
  279. if (ReadMessages(STDIN_FILENO,Messages) == false)
  280. break;
  281. }
  282. // Single mode exits if the message queue is empty
  283. if (Single == true && Messages.empty() == true)
  284. return -1;
  285. string Message = Messages.front();
  286. Messages.erase(Messages.begin());
  287. // Fetch the message number
  288. char *End;
  289. int Number = strtol(Message.c_str(),&End,10);
  290. if (End == Message.c_str())
  291. {
  292. cerr << "Malformed message!" << endl;
  293. return 100;
  294. }
  295. switch (Number)
  296. {
  297. case 601:
  298. if (Configuration(Message) == false)
  299. return 100;
  300. break;
  301. case 600:
  302. {
  303. FetchItem *Tmp = new FetchItem;
  304. Tmp->Uri = LookupTag(Message,"URI");
  305. Tmp->DestFile = LookupTag(Message,"FileName");
  306. if (RFC1123StrToTime(LookupTag(Message,"Last-Modified").c_str(),Tmp->LastModified) == false)
  307. Tmp->LastModified = 0;
  308. Tmp->IndexFile = StringToBool(LookupTag(Message,"Index-File"),false);
  309. Tmp->FailIgnore = StringToBool(LookupTag(Message,"Fail-Ignore"),false);
  310. Tmp->ExpectedHashes = HashStringList();
  311. for (char const * const * t = HashString::SupportedHashes(); *t != NULL; ++t)
  312. {
  313. std::string tag = "Expected-";
  314. tag.append(*t);
  315. std::string const hash = LookupTag(Message, tag.c_str());
  316. if (hash.empty() == false)
  317. Tmp->ExpectedHashes.push_back(HashString(*t, hash));
  318. }
  319. char *End;
  320. if (Tmp->ExpectedHashes.FileSize() > 0)
  321. Tmp->MaximumSize = Tmp->ExpectedHashes.FileSize();
  322. else
  323. Tmp->MaximumSize = strtoll(LookupTag(Message, "Maximum-Size", "0").c_str(), &End, 10);
  324. Tmp->Next = 0;
  325. // Append it to the list
  326. FetchItem **I = &Queue;
  327. for (; *I != 0; I = &(*I)->Next);
  328. *I = Tmp;
  329. if (QueueBack == 0)
  330. QueueBack = Tmp;
  331. // Notify that this item is to be fetched.
  332. if (URIAcquire(Message, Tmp) == false)
  333. Fail();
  334. break;
  335. }
  336. }
  337. }
  338. Exit();
  339. return 0;
  340. }
  341. /*}}}*/
  342. // AcqMethod::PrintStatus - privately really send a log/status message /*{{{*/
  343. void pkgAcqMethod::PrintStatus(char const * const header, const char* Format,
  344. va_list &args) const
  345. {
  346. string CurrentURI = "<UNKNOWN>";
  347. if (Queue != 0)
  348. CurrentURI = Queue->Uri;
  349. if (UsedMirror.empty() == true)
  350. fprintf(stdout, "%s\nURI: %s\nMessage: ",
  351. header, CurrentURI.c_str());
  352. else
  353. fprintf(stdout, "%s\nURI: %s\nUsedMirror: %s\nMessage: ",
  354. header, CurrentURI.c_str(), UsedMirror.c_str());
  355. vfprintf(stdout,Format,args);
  356. std::cout << "\n\n" << std::flush;
  357. }
  358. /*}}}*/
  359. // AcqMethod::Log - Send a log message /*{{{*/
  360. // ---------------------------------------------------------------------
  361. /* */
  362. void pkgAcqMethod::Log(const char *Format,...)
  363. {
  364. va_list args;
  365. va_start(args,Format);
  366. PrintStatus("101 Log", Format, args);
  367. va_end(args);
  368. }
  369. /*}}}*/
  370. // AcqMethod::Status - Send a status message /*{{{*/
  371. // ---------------------------------------------------------------------
  372. /* */
  373. void pkgAcqMethod::Status(const char *Format,...)
  374. {
  375. va_list args;
  376. va_start(args,Format);
  377. PrintStatus("102 Status", Format, args);
  378. va_end(args);
  379. }
  380. /*}}}*/
  381. // AcqMethod::Redirect - Send a redirect message /*{{{*/
  382. // ---------------------------------------------------------------------
  383. /* This method sends the redirect message and dequeues the item as
  384. * the worker will enqueue again later on to the right queue */
  385. void pkgAcqMethod::Redirect(const string &NewURI)
  386. {
  387. std::cout << "103 Redirect\nURI: " << Queue->Uri << "\n"
  388. << "New-URI: " << NewURI << "\n"
  389. << "\n" << std::flush;
  390. Dequeue();
  391. }
  392. /*}}}*/
  393. // AcqMethod::FetchResult::FetchResult - Constructor /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* */
  396. pkgAcqMethod::FetchResult::FetchResult() : LastModified(0),
  397. IMSHit(false), Size(0), ResumePoint(0), d(NULL)
  398. {
  399. }
  400. /*}}}*/
  401. // AcqMethod::FetchResult::TakeHashes - Load hashes /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* This hides the number of hashes we are supporting from the caller.
  404. It just deals with the hash class. */
  405. void pkgAcqMethod::FetchResult::TakeHashes(class Hashes &Hash)
  406. {
  407. Hashes = Hash.GetHashStringList();
  408. }
  409. /*}}}*/
  410. void pkgAcqMethod::Dequeue() { /*{{{*/
  411. FetchItem const * const Tmp = Queue;
  412. Queue = Queue->Next;
  413. if (Tmp == QueueBack)
  414. QueueBack = Queue;
  415. delete Tmp;
  416. }
  417. /*}}}*/
  418. pkgAcqMethod::~pkgAcqMethod() {}
  419. pkgAcqMethod::FetchItem::FetchItem() :
  420. Next(nullptr), DestFileFd(-1), LastModified(0), IndexFile(false),
  421. FailIgnore(false), MaximumSize(0), d(nullptr)
  422. {}
  423. pkgAcqMethod::FetchItem::~FetchItem() {}
  424. pkgAcqMethod::FetchResult::~FetchResult() {}