acquire-item.cc 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
  4. /* ######################################################################
  5. Acquire Item - Item to acquire
  6. Each item can download to exactly one file at a time. This means you
  7. cannot create an item that fetches two uri's to two files at the same
  8. time. The pkgAcqIndex class creates a second class upon instantiation
  9. to fetch the other index files because of this.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/acquire-item.h"
  15. #endif
  16. #include <apt-pkg/acquire-item.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/sourcelist.h>
  19. #include <apt-pkg/vendorlist.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <apt-pkg/md5.h>
  24. #include <apti18n.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string>
  29. #include <stdio.h>
  30. /*}}}*/
  31. using namespace std;
  32. // Acquire::Item::Item - Constructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
  36. PartialSize(0), Mode(0), ID(0), Complete(false),
  37. Local(false), QueueCounter(0)
  38. {
  39. Owner->Add(this);
  40. Status = StatIdle;
  41. }
  42. /*}}}*/
  43. // Acquire::Item::~Item - Destructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. pkgAcquire::Item::~Item()
  47. {
  48. Owner->Remove(this);
  49. }
  50. /*}}}*/
  51. // Acquire::Item::Failed - Item failed to download /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* We return to an idle state if there are still other queues that could
  54. fetch this object */
  55. void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  56. {
  57. Status = StatIdle;
  58. ErrorText = LookupTag(Message,"Message");
  59. if (QueueCounter <= 1)
  60. {
  61. /* This indicates that the file is not available right now but might
  62. be sometime later. If we do a retry cycle then this should be
  63. retried [CDROMs] */
  64. if (Cnf->LocalOnly == true &&
  65. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  66. {
  67. Status = StatIdle;
  68. Dequeue();
  69. return;
  70. }
  71. Status = StatError;
  72. Dequeue();
  73. }
  74. }
  75. /*}}}*/
  76. // Acquire::Item::Start - Item has begun to download /*{{{*/
  77. // ---------------------------------------------------------------------
  78. /* Stash status and the file size. Note that setting Complete means
  79. sub-phases of the acquire process such as decompresion are operating */
  80. void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
  81. {
  82. Status = StatFetching;
  83. if (FileSize == 0 && Complete == false)
  84. FileSize = Size;
  85. }
  86. /*}}}*/
  87. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* */
  90. void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
  91. pkgAcquire::MethodConfig *Cnf)
  92. {
  93. // We just downloaded something..
  94. string FileName = LookupTag(Message,"Filename");
  95. if (Complete == false && FileName == DestFile)
  96. {
  97. if (Owner->Log != 0)
  98. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  99. }
  100. if (FileSize == 0)
  101. FileSize= Size;
  102. Status = StatDone;
  103. ErrorText = string();
  104. Owner->Dequeue(this);
  105. }
  106. /*}}}*/
  107. // Acquire::Item::Rename - Rename a file /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* This helper function is used by alot of item methods as thier final
  110. step */
  111. void pkgAcquire::Item::Rename(string From,string To)
  112. {
  113. if (rename(From.c_str(),To.c_str()) != 0)
  114. {
  115. char S[300];
  116. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  117. From.c_str(),To.c_str());
  118. Status = StatError;
  119. ErrorText = S;
  120. }
  121. }
  122. /*}}}*/
  123. // AcqIndex::AcqIndex - Constructor /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* The package file is added to the queue and a second class is
  126. instantiated to fetch the revision file */
  127. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  128. string URI,string URIDesc,string ShortDesc,
  129. string ExpectedMD5, string comprExt) :
  130. Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
  131. {
  132. Decompression = false;
  133. Erase = false;
  134. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  135. DestFile += URItoFileName(URI);
  136. if(comprExt.empty())
  137. {
  138. // autoselect
  139. if(FileExists("/usr/bin/bzip2"))
  140. Desc.URI = URI + ".bz2";
  141. else
  142. Desc.URI = URI + ".gz";
  143. } else {
  144. Desc.URI = URI + comprExt;
  145. }
  146. Desc.Description = URIDesc;
  147. Desc.Owner = this;
  148. Desc.ShortDesc = ShortDesc;
  149. QueueURI(Desc);
  150. }
  151. /*}}}*/
  152. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  153. // ---------------------------------------------------------------------
  154. /* The only header we use is the last-modified header. */
  155. string pkgAcqIndex::Custom600Headers()
  156. {
  157. string Final = _config->FindDir("Dir::State::lists");
  158. Final += URItoFileName(RealURI);
  159. struct stat Buf;
  160. if (stat(Final.c_str(),&Buf) != 0)
  161. return "\nIndex-File: true";
  162. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  163. }
  164. /*}}}*/
  165. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  166. {
  167. // no .bz2 found, retry with .gz
  168. if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
  169. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  170. // retry with a gzip one
  171. new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
  172. ExpectedMD5, string(".gz"));
  173. Status = StatDone;
  174. Complete = false;
  175. Dequeue();
  176. return;
  177. }
  178. Item::Failed(Message,Cnf);
  179. }
  180. // AcqIndex::Done - Finished a fetch /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* This goes through a number of states.. On the initial fetch the
  183. method could possibly return an alternate filename which points
  184. to the uncompressed version of the file. If this is so the file
  185. is copied into the partial directory. In all other cases the file
  186. is decompressed with a gzip uri. */
  187. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  188. pkgAcquire::MethodConfig *Cfg)
  189. {
  190. Item::Done(Message,Size,MD5,Cfg);
  191. if (Decompression == true)
  192. {
  193. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  194. {
  195. std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
  196. std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
  197. }
  198. if (MD5.empty())
  199. {
  200. MD5Summation sum;
  201. FileFd Fd(DestFile, FileFd::ReadOnly);
  202. sum.AddFD(Fd.Fd(), Fd.Size());
  203. Fd.Close();
  204. MD5 = (string)sum.Result();
  205. }
  206. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  207. {
  208. Status = StatAuthError;
  209. ErrorText = _("MD5Sum mismatch");
  210. Rename(DestFile,DestFile + ".FAILED");
  211. return;
  212. }
  213. // Done, move it into position
  214. string FinalFile = _config->FindDir("Dir::State::lists");
  215. FinalFile += URItoFileName(RealURI);
  216. Rename(DestFile,FinalFile);
  217. chmod(FinalFile.c_str(),0644);
  218. /* We restore the original name to DestFile so that the clean operation
  219. will work OK */
  220. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  221. DestFile += URItoFileName(RealURI);
  222. // Remove the compressed version.
  223. if (Erase == true)
  224. unlink(DestFile.c_str());
  225. return;
  226. }
  227. Erase = false;
  228. Complete = true;
  229. // Handle the unzipd case
  230. string FileName = LookupTag(Message,"Alt-Filename");
  231. if (FileName.empty() == false)
  232. {
  233. // The files timestamp matches
  234. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  235. return;
  236. Decompression = true;
  237. Local = true;
  238. DestFile += ".decomp";
  239. Desc.URI = "copy:" + FileName;
  240. QueueURI(Desc);
  241. Mode = "copy";
  242. return;
  243. }
  244. FileName = LookupTag(Message,"Filename");
  245. if (FileName.empty() == true)
  246. {
  247. Status = StatError;
  248. ErrorText = "Method gave a blank filename";
  249. }
  250. // The files timestamp matches
  251. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  252. return;
  253. if (FileName == DestFile)
  254. Erase = true;
  255. else
  256. Local = true;
  257. string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
  258. char *decompProg;
  259. if(compExt == "bz2")
  260. decompProg = "bzip2";
  261. else if(compExt == ".gz")
  262. decompProg = "gzip";
  263. else {
  264. _error->Error("Unsupported extension: %s", compExt.c_str());
  265. return;
  266. }
  267. Decompression = true;
  268. DestFile += ".decomp";
  269. Desc.URI = string(decompProg) + ":" + FileName;
  270. QueueURI(Desc);
  271. Mode = decompProg;
  272. }
  273. // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
  274. // ---------------------------------------------------------------------
  275. /* The Translation file is added to the queue */
  276. pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
  277. string URI,string URIDesc,string ShortDesc) :
  278. pkgAcqIndex(Owner, URI, URIDesc, ShortDesc)
  279. {
  280. }
  281. /*}}}*/
  282. // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
  283. // ---------------------------------------------------------------------
  284. /* */
  285. void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  286. {
  287. if (Cnf->LocalOnly == true ||
  288. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  289. {
  290. // Ignore this
  291. Status = StatDone;
  292. Complete = false;
  293. Dequeue();
  294. return;
  295. }
  296. Item::Failed(Message,Cnf);
  297. }
  298. /*}}}*/
  299. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  300. string URI,string URIDesc,string ShortDesc,
  301. string MetaIndexURI, string MetaIndexURIDesc,
  302. string MetaIndexShortDesc,
  303. const vector<IndexTarget*>* IndexTargets,
  304. indexRecords* MetaIndexParser) :
  305. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  306. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
  307. {
  308. this->MetaIndexParser = MetaIndexParser;
  309. this->IndexTargets = IndexTargets;
  310. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  311. DestFile += URItoFileName(URI);
  312. // remove any partial downloaded sig-file. it may confuse proxies
  313. // and is too small to warrant a partial download anyway
  314. unlink(DestFile.c_str());
  315. // Create the item
  316. Desc.Description = URIDesc;
  317. Desc.Owner = this;
  318. Desc.ShortDesc = ShortDesc;
  319. Desc.URI = URI;
  320. string Final = _config->FindDir("Dir::State::lists");
  321. Final += URItoFileName(RealURI);
  322. struct stat Buf;
  323. if (stat(Final.c_str(),&Buf) == 0)
  324. {
  325. // File was already in place. It needs to be re-verified
  326. // because Release might have changed, so Move it into partial
  327. Rename(Final,DestFile);
  328. }
  329. QueueURI(Desc);
  330. }
  331. /*}}}*/
  332. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  333. // ---------------------------------------------------------------------
  334. /* The only header we use is the last-modified header. */
  335. string pkgAcqMetaSig::Custom600Headers()
  336. {
  337. struct stat Buf;
  338. if (stat(DestFile.c_str(),&Buf) != 0)
  339. return "\nIndex-File: true";
  340. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  341. }
  342. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  343. pkgAcquire::MethodConfig *Cfg)
  344. {
  345. Item::Done(Message,Size,MD5,Cfg);
  346. string FileName = LookupTag(Message,"Filename");
  347. if (FileName.empty() == true)
  348. {
  349. Status = StatError;
  350. ErrorText = "Method gave a blank filename";
  351. return;
  352. }
  353. if (FileName != DestFile)
  354. {
  355. // We have to copy it into place
  356. Local = true;
  357. Desc.URI = "copy:" + FileName;
  358. QueueURI(Desc);
  359. return;
  360. }
  361. Complete = true;
  362. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  363. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  364. DestFile, IndexTargets, MetaIndexParser);
  365. }
  366. /*}}}*/
  367. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  368. {
  369. // Delete any existing sigfile, so that this source isn't
  370. // mistakenly trusted
  371. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  372. unlink(Final.c_str());
  373. // queue a pkgAcqMetaIndex with no sigfile
  374. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  375. "", IndexTargets, MetaIndexParser);
  376. if (Cnf->LocalOnly == true ||
  377. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  378. {
  379. // Ignore this
  380. Status = StatDone;
  381. Complete = false;
  382. Dequeue();
  383. return;
  384. }
  385. Item::Failed(Message,Cnf);
  386. }
  387. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  388. string URI,string URIDesc,string ShortDesc,
  389. string SigFile,
  390. const vector<struct IndexTarget*>* IndexTargets,
  391. indexRecords* MetaIndexParser) :
  392. Item(Owner), RealURI(URI), SigFile(SigFile)
  393. {
  394. this->AuthPass = false;
  395. this->MetaIndexParser = MetaIndexParser;
  396. this->IndexTargets = IndexTargets;
  397. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  398. DestFile += URItoFileName(URI);
  399. // Create the item
  400. Desc.Description = URIDesc;
  401. Desc.Owner = this;
  402. Desc.ShortDesc = ShortDesc;
  403. Desc.URI = URI;
  404. QueueURI(Desc);
  405. }
  406. /*}}}*/
  407. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  408. // ---------------------------------------------------------------------
  409. /* The only header we use is the last-modified header. */
  410. string pkgAcqMetaIndex::Custom600Headers()
  411. {
  412. string Final = _config->FindDir("Dir::State::lists");
  413. Final += URItoFileName(RealURI);
  414. struct stat Buf;
  415. if (stat(Final.c_str(),&Buf) != 0)
  416. return "\nIndex-File: true";
  417. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  418. }
  419. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  420. pkgAcquire::MethodConfig *Cfg)
  421. {
  422. Item::Done(Message,Size,MD5,Cfg);
  423. // MetaIndexes are done in two passes: one to download the
  424. // metaindex with an appropriate method, and a second to verify it
  425. // with the gpgv method
  426. if (AuthPass == true)
  427. {
  428. AuthDone(Message);
  429. }
  430. else
  431. {
  432. RetrievalDone(Message);
  433. if (!Complete)
  434. // Still more retrieving to do
  435. return;
  436. if (SigFile == "")
  437. {
  438. // There was no signature file, so we are finished. Download
  439. // the indexes without verification.
  440. QueueIndexes(false);
  441. }
  442. else
  443. {
  444. // There was a signature file, so pass it to gpgv for
  445. // verification
  446. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  447. std::cerr << "Metaindex acquired, queueing gpg verification ("
  448. << SigFile << "," << DestFile << ")\n";
  449. AuthPass = true;
  450. Desc.URI = "gpgv:" + SigFile;
  451. QueueURI(Desc);
  452. Mode = "gpgv";
  453. }
  454. }
  455. }
  456. void pkgAcqMetaIndex::RetrievalDone(string Message)
  457. {
  458. // We have just finished downloading a Release file (it is not
  459. // verified yet)
  460. string FileName = LookupTag(Message,"Filename");
  461. if (FileName.empty() == true)
  462. {
  463. Status = StatError;
  464. ErrorText = "Method gave a blank filename";
  465. return;
  466. }
  467. if (FileName != DestFile)
  468. {
  469. Local = true;
  470. Desc.URI = "copy:" + FileName;
  471. QueueURI(Desc);
  472. return;
  473. }
  474. Complete = true;
  475. string FinalFile = _config->FindDir("Dir::State::lists");
  476. FinalFile += URItoFileName(RealURI);
  477. // The files timestamp matches
  478. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  479. {
  480. // Move it into position
  481. Rename(DestFile,FinalFile);
  482. }
  483. DestFile = FinalFile;
  484. }
  485. void pkgAcqMetaIndex::AuthDone(string Message)
  486. {
  487. // At this point, the gpgv method has succeeded, so there is a
  488. // valid signature from a key in the trusted keyring. We
  489. // perform additional verification of its contents, and use them
  490. // to verify the indexes we are about to download
  491. if (!MetaIndexParser->Load(DestFile))
  492. {
  493. Status = StatAuthError;
  494. ErrorText = MetaIndexParser->ErrorText;
  495. return;
  496. }
  497. if (!VerifyVendor())
  498. {
  499. return;
  500. }
  501. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  502. std::cerr << "Signature verification succeeded: "
  503. << DestFile << std::endl;
  504. // Download further indexes with verification
  505. QueueIndexes(true);
  506. // Done, move signature file into position
  507. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  508. URItoFileName(RealURI) + ".gpg";
  509. Rename(SigFile,VerifiedSigFile);
  510. chmod(VerifiedSigFile.c_str(),0644);
  511. }
  512. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  513. {
  514. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  515. Target != IndexTargets->end();
  516. Target++)
  517. {
  518. string ExpectedIndexMD5;
  519. if (verify)
  520. {
  521. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  522. if (!Record)
  523. {
  524. Status = StatAuthError;
  525. ErrorText = "Unable to find expected entry "
  526. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  527. return;
  528. }
  529. ExpectedIndexMD5 = Record->MD5Hash;
  530. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  531. {
  532. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  533. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  534. }
  535. if (ExpectedIndexMD5.empty())
  536. {
  537. Status = StatAuthError;
  538. ErrorText = "Unable to find MD5 sum for "
  539. + (*Target)->MetaKey + " in Meta-index file";
  540. return;
  541. }
  542. }
  543. // Queue Packages file
  544. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  545. (*Target)->ShortDesc, ExpectedIndexMD5);
  546. }
  547. }
  548. bool pkgAcqMetaIndex::VerifyVendor()
  549. {
  550. // // Maybe this should be made available from above so we don't have
  551. // // to read and parse it every time?
  552. // pkgVendorList List;
  553. // List.ReadMainList();
  554. // const Vendor* Vndr = NULL;
  555. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  556. // {
  557. // string::size_type pos = (*I).find("VALIDSIG ");
  558. // if (_config->FindB("Debug::Vendor", false))
  559. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  560. // << std::endl;
  561. // if (pos != std::string::npos)
  562. // {
  563. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  564. // if (_config->FindB("Debug::Vendor", false))
  565. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  566. // std::endl;
  567. // Vndr = List.FindVendor(Fingerprint) != "";
  568. // if (Vndr != NULL);
  569. // break;
  570. // }
  571. // }
  572. string Transformed = MetaIndexParser->GetExpectedDist();
  573. if (Transformed == "../project/experimental")
  574. {
  575. Transformed = "experimental";
  576. }
  577. string::size_type pos = Transformed.rfind('/');
  578. if (pos != string::npos)
  579. {
  580. Transformed = Transformed.substr(0, pos);
  581. }
  582. if (Transformed == ".")
  583. {
  584. Transformed = "";
  585. }
  586. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  587. {
  588. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  589. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  590. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  591. }
  592. if (MetaIndexParser->CheckDist(Transformed) == false)
  593. {
  594. // This might become fatal one day
  595. // Status = StatAuthError;
  596. // ErrorText = "Conflicting distribution; expected "
  597. // + MetaIndexParser->GetExpectedDist() + " but got "
  598. // + MetaIndexParser->GetDist();
  599. // return false;
  600. if (!Transformed.empty())
  601. {
  602. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  603. Desc.Description.c_str(),
  604. Transformed.c_str(),
  605. MetaIndexParser->GetDist().c_str());
  606. }
  607. }
  608. return true;
  609. }
  610. /*}}}*/
  611. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  612. // file present /*{{{*/
  613. // ---------------------------------------------------------------------
  614. /* */
  615. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  616. {
  617. if (AuthPass == true)
  618. {
  619. // gpgv method failed
  620. _error->Warning("GPG error: %s: %s",
  621. Desc.Description.c_str(),
  622. LookupTag(Message,"Message").c_str());
  623. }
  624. // No Release file was present, or verification failed, so fall
  625. // back to queueing Packages files without verification
  626. QueueIndexes(false);
  627. }
  628. /*}}}*/
  629. // AcqArchive::AcqArchive - Constructor /*{{{*/
  630. // ---------------------------------------------------------------------
  631. /* This just sets up the initial fetch environment and queues the first
  632. possibilitiy */
  633. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  634. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  635. string &StoreFilename) :
  636. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  637. StoreFilename(StoreFilename), Vf(Version.FileList()),
  638. Trusted(false)
  639. {
  640. Retries = _config->FindI("Acquire::Retries",0);
  641. if (Version.Arch() == 0)
  642. {
  643. _error->Error(_("I wasn't able to locate a file for the %s package. "
  644. "This might mean you need to manually fix this package. "
  645. "(due to missing arch)"),
  646. Version.ParentPkg().Name());
  647. return;
  648. }
  649. /* We need to find a filename to determine the extension. We make the
  650. assumption here that all the available sources for this version share
  651. the same extension.. */
  652. // Skip not source sources, they do not have file fields.
  653. for (; Vf.end() == false; Vf++)
  654. {
  655. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  656. continue;
  657. break;
  658. }
  659. // Does not really matter here.. we are going to fail out below
  660. if (Vf.end() != true)
  661. {
  662. // If this fails to get a file name we will bomb out below.
  663. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  664. if (_error->PendingError() == true)
  665. return;
  666. // Generate the final file name as: package_version_arch.foo
  667. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  668. QuoteString(Version.VerStr(),"_:") + '_' +
  669. QuoteString(Version.Arch(),"_:.") +
  670. "." + flExtension(Parse.FileName());
  671. }
  672. // check if we have one trusted source for the package. if so, switch
  673. // to "TrustedOnly" mode
  674. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  675. {
  676. pkgIndexFile *Index;
  677. if (Sources->FindIndex(i.File(),Index) == false)
  678. continue;
  679. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  680. {
  681. std::cerr << "Checking index: " << Index->Describe()
  682. << "(Trusted=" << Index->IsTrusted() << ")\n";
  683. }
  684. if (Index->IsTrusted()) {
  685. Trusted = true;
  686. break;
  687. }
  688. }
  689. // Select a source
  690. if (QueueNext() == false && _error->PendingError() == false)
  691. _error->Error(_("I wasn't able to locate file for the %s package. "
  692. "This might mean you need to manually fix this package."),
  693. Version.ParentPkg().Name());
  694. }
  695. /*}}}*/
  696. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  697. // ---------------------------------------------------------------------
  698. /* This queues the next available file version for download. It checks if
  699. the archive is already available in the cache and stashs the MD5 for
  700. checking later. */
  701. bool pkgAcqArchive::QueueNext()
  702. {
  703. for (; Vf.end() == false; Vf++)
  704. {
  705. // Ignore not source sources
  706. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  707. continue;
  708. // Try to cross match against the source list
  709. pkgIndexFile *Index;
  710. if (Sources->FindIndex(Vf.File(),Index) == false)
  711. continue;
  712. // only try to get a trusted package from another source if that source
  713. // is also trusted
  714. if(Trusted && !Index->IsTrusted())
  715. continue;
  716. // Grab the text package record
  717. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  718. if (_error->PendingError() == true)
  719. return false;
  720. string PkgFile = Parse.FileName();
  721. MD5 = Parse.MD5Hash();
  722. if (PkgFile.empty() == true)
  723. return _error->Error(_("The package index files are corrupted. No Filename: "
  724. "field for package %s."),
  725. Version.ParentPkg().Name());
  726. Desc.URI = Index->ArchiveURI(PkgFile);
  727. Desc.Description = Index->ArchiveInfo(Version);
  728. Desc.Owner = this;
  729. Desc.ShortDesc = Version.ParentPkg().Name();
  730. // See if we already have the file. (Legacy filenames)
  731. FileSize = Version->Size;
  732. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  733. struct stat Buf;
  734. if (stat(FinalFile.c_str(),&Buf) == 0)
  735. {
  736. // Make sure the size matches
  737. if ((unsigned)Buf.st_size == Version->Size)
  738. {
  739. Complete = true;
  740. Local = true;
  741. Status = StatDone;
  742. StoreFilename = DestFile = FinalFile;
  743. return true;
  744. }
  745. /* Hmm, we have a file and its size does not match, this means it is
  746. an old style mismatched arch */
  747. unlink(FinalFile.c_str());
  748. }
  749. // Check it again using the new style output filenames
  750. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  751. if (stat(FinalFile.c_str(),&Buf) == 0)
  752. {
  753. // Make sure the size matches
  754. if ((unsigned)Buf.st_size == Version->Size)
  755. {
  756. Complete = true;
  757. Local = true;
  758. Status = StatDone;
  759. StoreFilename = DestFile = FinalFile;
  760. return true;
  761. }
  762. /* Hmm, we have a file and its size does not match, this shouldnt
  763. happen.. */
  764. unlink(FinalFile.c_str());
  765. }
  766. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  767. // Check the destination file
  768. if (stat(DestFile.c_str(),&Buf) == 0)
  769. {
  770. // Hmm, the partial file is too big, erase it
  771. if ((unsigned)Buf.st_size > Version->Size)
  772. unlink(DestFile.c_str());
  773. else
  774. PartialSize = Buf.st_size;
  775. }
  776. // Create the item
  777. Local = false;
  778. Desc.URI = Index->ArchiveURI(PkgFile);
  779. Desc.Description = Index->ArchiveInfo(Version);
  780. Desc.Owner = this;
  781. Desc.ShortDesc = Version.ParentPkg().Name();
  782. QueueURI(Desc);
  783. Vf++;
  784. return true;
  785. }
  786. return false;
  787. }
  788. /*}}}*/
  789. // AcqArchive::Done - Finished fetching /*{{{*/
  790. // ---------------------------------------------------------------------
  791. /* */
  792. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  793. pkgAcquire::MethodConfig *Cfg)
  794. {
  795. Item::Done(Message,Size,Md5Hash,Cfg);
  796. // Check the size
  797. if (Size != Version->Size)
  798. {
  799. Status = StatError;
  800. ErrorText = _("Size mismatch");
  801. return;
  802. }
  803. // Check the md5
  804. if (Md5Hash.empty() == false && MD5.empty() == false)
  805. {
  806. if (Md5Hash != MD5)
  807. {
  808. Status = StatError;
  809. ErrorText = _("MD5Sum mismatch");
  810. Rename(DestFile,DestFile + ".FAILED");
  811. return;
  812. }
  813. }
  814. // Grab the output filename
  815. string FileName = LookupTag(Message,"Filename");
  816. if (FileName.empty() == true)
  817. {
  818. Status = StatError;
  819. ErrorText = "Method gave a blank filename";
  820. return;
  821. }
  822. Complete = true;
  823. // Reference filename
  824. if (FileName != DestFile)
  825. {
  826. StoreFilename = DestFile = FileName;
  827. Local = true;
  828. return;
  829. }
  830. // Done, move it into position
  831. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  832. FinalFile += flNotDir(StoreFilename);
  833. Rename(DestFile,FinalFile);
  834. StoreFilename = DestFile = FinalFile;
  835. Complete = true;
  836. }
  837. /*}}}*/
  838. // AcqArchive::Failed - Failure handler /*{{{*/
  839. // ---------------------------------------------------------------------
  840. /* Here we try other sources */
  841. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  842. {
  843. ErrorText = LookupTag(Message,"Message");
  844. /* We don't really want to retry on failed media swaps, this prevents
  845. that. An interesting observation is that permanent failures are not
  846. recorded. */
  847. if (Cnf->Removable == true &&
  848. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  849. {
  850. // Vf = Version.FileList();
  851. while (Vf.end() == false) Vf++;
  852. StoreFilename = string();
  853. Item::Failed(Message,Cnf);
  854. return;
  855. }
  856. if (QueueNext() == false)
  857. {
  858. // This is the retry counter
  859. if (Retries != 0 &&
  860. Cnf->LocalOnly == false &&
  861. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  862. {
  863. Retries--;
  864. Vf = Version.FileList();
  865. if (QueueNext() == true)
  866. return;
  867. }
  868. StoreFilename = string();
  869. Item::Failed(Message,Cnf);
  870. }
  871. }
  872. /*}}}*/
  873. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  874. // trusted source /*{{{*/
  875. // ---------------------------------------------------------------------
  876. bool pkgAcqArchive::IsTrusted()
  877. {
  878. return Trusted;
  879. }
  880. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  881. // ---------------------------------------------------------------------
  882. /* */
  883. void pkgAcqArchive::Finished()
  884. {
  885. if (Status == pkgAcquire::Item::StatDone &&
  886. Complete == true)
  887. return;
  888. StoreFilename = string();
  889. }
  890. /*}}}*/
  891. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  892. // ---------------------------------------------------------------------
  893. /* The file is added to the queue */
  894. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  895. unsigned long Size,string Dsc,string ShortDesc) :
  896. Item(Owner), Md5Hash(MD5)
  897. {
  898. Retries = _config->FindI("Acquire::Retries",0);
  899. DestFile = flNotDir(URI);
  900. // Create the item
  901. Desc.URI = URI;
  902. Desc.Description = Dsc;
  903. Desc.Owner = this;
  904. // Set the short description to the archive component
  905. Desc.ShortDesc = ShortDesc;
  906. // Get the transfer sizes
  907. FileSize = Size;
  908. struct stat Buf;
  909. if (stat(DestFile.c_str(),&Buf) == 0)
  910. {
  911. // Hmm, the partial file is too big, erase it
  912. if ((unsigned)Buf.st_size > Size)
  913. unlink(DestFile.c_str());
  914. else
  915. PartialSize = Buf.st_size;
  916. }
  917. QueueURI(Desc);
  918. }
  919. /*}}}*/
  920. // AcqFile::Done - Item downloaded OK /*{{{*/
  921. // ---------------------------------------------------------------------
  922. /* */
  923. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  924. pkgAcquire::MethodConfig *Cnf)
  925. {
  926. // Check the md5
  927. if (Md5Hash.empty() == false && MD5.empty() == false)
  928. {
  929. if (Md5Hash != MD5)
  930. {
  931. Status = StatError;
  932. ErrorText = "MD5Sum mismatch";
  933. Rename(DestFile,DestFile + ".FAILED");
  934. return;
  935. }
  936. }
  937. Item::Done(Message,Size,MD5,Cnf);
  938. string FileName = LookupTag(Message,"Filename");
  939. if (FileName.empty() == true)
  940. {
  941. Status = StatError;
  942. ErrorText = "Method gave a blank filename";
  943. return;
  944. }
  945. Complete = true;
  946. // The files timestamp matches
  947. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  948. return;
  949. // We have to copy it into place
  950. if (FileName != DestFile)
  951. {
  952. Local = true;
  953. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  954. Cnf->Removable == true)
  955. {
  956. Desc.URI = "copy:" + FileName;
  957. QueueURI(Desc);
  958. return;
  959. }
  960. // Erase the file if it is a symlink so we can overwrite it
  961. struct stat St;
  962. if (lstat(DestFile.c_str(),&St) == 0)
  963. {
  964. if (S_ISLNK(St.st_mode) != 0)
  965. unlink(DestFile.c_str());
  966. }
  967. // Symlink the file
  968. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  969. {
  970. ErrorText = "Link to " + DestFile + " failure ";
  971. Status = StatError;
  972. Complete = false;
  973. }
  974. }
  975. }
  976. /*}}}*/
  977. // AcqFile::Failed - Failure handler /*{{{*/
  978. // ---------------------------------------------------------------------
  979. /* Here we try other sources */
  980. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  981. {
  982. ErrorText = LookupTag(Message,"Message");
  983. // This is the retry counter
  984. if (Retries != 0 &&
  985. Cnf->LocalOnly == false &&
  986. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  987. {
  988. Retries--;
  989. QueueURI(Desc);
  990. return;
  991. }
  992. Item::Failed(Message,Cnf);
  993. }
  994. /*}}}*/