acquire-item.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  274. string URI,string URIDesc,string ShortDesc,
  275. string MetaIndexURI, string MetaIndexURIDesc,
  276. string MetaIndexShortDesc,
  277. const vector<IndexTarget*>* IndexTargets,
  278. indexRecords* MetaIndexParser) :
  279. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  280. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
  281. {
  282. this->MetaIndexParser = MetaIndexParser;
  283. this->IndexTargets = IndexTargets;
  284. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  285. DestFile += URItoFileName(URI);
  286. // remove any partial downloaded sig-file. it may confuse proxies
  287. // and is too small to warrant a partial download anyway
  288. unlink(DestFile.c_str());
  289. // Create the item
  290. Desc.Description = URIDesc;
  291. Desc.Owner = this;
  292. Desc.ShortDesc = ShortDesc;
  293. Desc.URI = URI;
  294. string Final = _config->FindDir("Dir::State::lists");
  295. Final += URItoFileName(RealURI);
  296. struct stat Buf;
  297. if (stat(Final.c_str(),&Buf) == 0)
  298. {
  299. // File was already in place. It needs to be re-verified
  300. // because Release might have changed, so Move it into partial
  301. Rename(Final,DestFile);
  302. // unlink the file and do not try to use I-M-S and Last-Modified
  303. // if the users proxy is broken
  304. if(_config->FindB("Acquire::BrokenProxy", false) == true) {
  305. std::cerr << "forcing re-get of the signature file as requested" << std::endl;
  306. unlink(DestFile.c_str());
  307. }
  308. }
  309. QueueURI(Desc);
  310. }
  311. /*}}}*/
  312. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* The only header we use is the last-modified header. */
  315. string pkgAcqMetaSig::Custom600Headers()
  316. {
  317. struct stat Buf;
  318. if (stat(DestFile.c_str(),&Buf) != 0)
  319. return "\nIndex-File: true";
  320. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  321. }
  322. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  323. pkgAcquire::MethodConfig *Cfg)
  324. {
  325. Item::Done(Message,Size,MD5,Cfg);
  326. string FileName = LookupTag(Message,"Filename");
  327. if (FileName.empty() == true)
  328. {
  329. Status = StatError;
  330. ErrorText = "Method gave a blank filename";
  331. return;
  332. }
  333. if (FileName != DestFile)
  334. {
  335. // We have to copy it into place
  336. Local = true;
  337. Desc.URI = "copy:" + FileName;
  338. QueueURI(Desc);
  339. return;
  340. }
  341. Complete = true;
  342. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  343. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  344. DestFile, IndexTargets, MetaIndexParser);
  345. }
  346. /*}}}*/
  347. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  348. {
  349. // Delete any existing sigfile, so that this source isn't
  350. // mistakenly trusted
  351. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  352. unlink(Final.c_str());
  353. // if we get a timeout if fail
  354. if(LookupTag(Message,"FailReason") == "Timeout") {
  355. Item::Failed(Message,Cnf);
  356. return;
  357. }
  358. // queue a pkgAcqMetaIndex with no sigfile
  359. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  360. "", IndexTargets, MetaIndexParser);
  361. if (Cnf->LocalOnly == true ||
  362. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  363. {
  364. // Ignore this
  365. Status = StatDone;
  366. Complete = false;
  367. Dequeue();
  368. return;
  369. }
  370. Item::Failed(Message,Cnf);
  371. }
  372. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  373. string URI,string URIDesc,string ShortDesc,
  374. string SigFile,
  375. const vector<struct IndexTarget*>* IndexTargets,
  376. indexRecords* MetaIndexParser) :
  377. Item(Owner), RealURI(URI), SigFile(SigFile)
  378. {
  379. this->AuthPass = false;
  380. this->MetaIndexParser = MetaIndexParser;
  381. this->IndexTargets = IndexTargets;
  382. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  383. DestFile += URItoFileName(URI);
  384. // Create the item
  385. Desc.Description = URIDesc;
  386. Desc.Owner = this;
  387. Desc.ShortDesc = ShortDesc;
  388. Desc.URI = URI;
  389. QueueURI(Desc);
  390. }
  391. /*}}}*/
  392. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  393. // ---------------------------------------------------------------------
  394. /* The only header we use is the last-modified header. */
  395. string pkgAcqMetaIndex::Custom600Headers()
  396. {
  397. string Final = _config->FindDir("Dir::State::lists");
  398. Final += URItoFileName(RealURI);
  399. struct stat Buf;
  400. if (stat(Final.c_str(),&Buf) != 0)
  401. return "\nIndex-File: true";
  402. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  403. }
  404. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  405. pkgAcquire::MethodConfig *Cfg)
  406. {
  407. Item::Done(Message,Size,MD5,Cfg);
  408. // MetaIndexes are done in two passes: one to download the
  409. // metaindex with an appropriate method, and a second to verify it
  410. // with the gpgv method
  411. if (AuthPass == true)
  412. {
  413. AuthDone(Message);
  414. }
  415. else
  416. {
  417. RetrievalDone(Message);
  418. if (!Complete)
  419. // Still more retrieving to do
  420. return;
  421. if (SigFile == "")
  422. {
  423. // There was no signature file, so we are finished. Download
  424. // the indexes without verification.
  425. QueueIndexes(false);
  426. }
  427. else
  428. {
  429. // There was a signature file, so pass it to gpgv for
  430. // verification
  431. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  432. std::cerr << "Metaindex acquired, queueing gpg verification ("
  433. << SigFile << "," << DestFile << ")\n";
  434. AuthPass = true;
  435. Desc.URI = "gpgv:" + SigFile;
  436. QueueURI(Desc);
  437. Mode = "gpgv";
  438. }
  439. }
  440. }
  441. void pkgAcqMetaIndex::RetrievalDone(string Message)
  442. {
  443. // We have just finished downloading a Release file (it is not
  444. // verified yet)
  445. string FileName = LookupTag(Message,"Filename");
  446. if (FileName.empty() == true)
  447. {
  448. Status = StatError;
  449. ErrorText = "Method gave a blank filename";
  450. return;
  451. }
  452. if (FileName != DestFile)
  453. {
  454. Local = true;
  455. Desc.URI = "copy:" + FileName;
  456. QueueURI(Desc);
  457. return;
  458. }
  459. Complete = true;
  460. string FinalFile = _config->FindDir("Dir::State::lists");
  461. FinalFile += URItoFileName(RealURI);
  462. // The files timestamp matches
  463. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  464. {
  465. // Move it into position
  466. Rename(DestFile,FinalFile);
  467. }
  468. DestFile = FinalFile;
  469. }
  470. void pkgAcqMetaIndex::AuthDone(string Message)
  471. {
  472. // At this point, the gpgv method has succeeded, so there is a
  473. // valid signature from a key in the trusted keyring. We
  474. // perform additional verification of its contents, and use them
  475. // to verify the indexes we are about to download
  476. if (!MetaIndexParser->Load(DestFile))
  477. {
  478. Status = StatAuthError;
  479. ErrorText = MetaIndexParser->ErrorText;
  480. return;
  481. }
  482. if (!VerifyVendor())
  483. {
  484. return;
  485. }
  486. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  487. std::cerr << "Signature verification succeeded: "
  488. << DestFile << std::endl;
  489. // Download further indexes with verification
  490. QueueIndexes(true);
  491. // Done, move signature file into position
  492. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  493. URItoFileName(RealURI) + ".gpg";
  494. Rename(SigFile,VerifiedSigFile);
  495. chmod(VerifiedSigFile.c_str(),0644);
  496. }
  497. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  498. {
  499. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  500. Target != IndexTargets->end();
  501. Target++)
  502. {
  503. string ExpectedIndexMD5;
  504. if (verify)
  505. {
  506. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  507. if (!Record)
  508. {
  509. Status = StatAuthError;
  510. ErrorText = "Unable to find expected entry "
  511. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  512. return;
  513. }
  514. ExpectedIndexMD5 = Record->MD5Hash;
  515. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  516. {
  517. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  518. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  519. }
  520. if (ExpectedIndexMD5.empty())
  521. {
  522. Status = StatAuthError;
  523. ErrorText = "Unable to find MD5 sum for "
  524. + (*Target)->MetaKey + " in Meta-index file";
  525. return;
  526. }
  527. }
  528. // Queue Packages file
  529. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  530. (*Target)->ShortDesc, ExpectedIndexMD5);
  531. }
  532. }
  533. bool pkgAcqMetaIndex::VerifyVendor()
  534. {
  535. // // Maybe this should be made available from above so we don't have
  536. // // to read and parse it every time?
  537. // pkgVendorList List;
  538. // List.ReadMainList();
  539. // const Vendor* Vndr = NULL;
  540. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  541. // {
  542. // string::size_type pos = (*I).find("VALIDSIG ");
  543. // if (_config->FindB("Debug::Vendor", false))
  544. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  545. // << std::endl;
  546. // if (pos != std::string::npos)
  547. // {
  548. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  549. // if (_config->FindB("Debug::Vendor", false))
  550. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  551. // std::endl;
  552. // Vndr = List.FindVendor(Fingerprint) != "";
  553. // if (Vndr != NULL);
  554. // break;
  555. // }
  556. // }
  557. string Transformed = MetaIndexParser->GetExpectedDist();
  558. if (Transformed == "../project/experimental")
  559. {
  560. Transformed = "experimental";
  561. }
  562. string::size_type pos = Transformed.rfind('/');
  563. if (pos != string::npos)
  564. {
  565. Transformed = Transformed.substr(0, pos);
  566. }
  567. if (Transformed == ".")
  568. {
  569. Transformed = "";
  570. }
  571. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  572. {
  573. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  574. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  575. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  576. }
  577. if (MetaIndexParser->CheckDist(Transformed) == false)
  578. {
  579. // This might become fatal one day
  580. // Status = StatAuthError;
  581. // ErrorText = "Conflicting distribution; expected "
  582. // + MetaIndexParser->GetExpectedDist() + " but got "
  583. // + MetaIndexParser->GetDist();
  584. // return false;
  585. if (!Transformed.empty())
  586. {
  587. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  588. Desc.Description.c_str(),
  589. Transformed.c_str(),
  590. MetaIndexParser->GetDist().c_str());
  591. }
  592. }
  593. return true;
  594. }
  595. /*}}}*/
  596. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  597. // file present /*{{{*/
  598. // ---------------------------------------------------------------------
  599. /* */
  600. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  601. {
  602. if (AuthPass == true)
  603. {
  604. // gpgv method failed
  605. _error->Warning("GPG error: %s: %s",
  606. Desc.Description.c_str(),
  607. LookupTag(Message,"Message").c_str());
  608. }
  609. // No Release file was present, or verification failed, so fall
  610. // back to queueing Packages files without verification
  611. QueueIndexes(false);
  612. }
  613. /*}}}*/
  614. // AcqArchive::AcqArchive - Constructor /*{{{*/
  615. // ---------------------------------------------------------------------
  616. /* This just sets up the initial fetch environment and queues the first
  617. possibilitiy */
  618. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  619. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  620. string &StoreFilename) :
  621. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  622. StoreFilename(StoreFilename), Vf(Version.FileList()),
  623. Trusted(false)
  624. {
  625. Retries = _config->FindI("Acquire::Retries",0);
  626. if (Version.Arch() == 0)
  627. {
  628. _error->Error(_("I wasn't able to locate a file for the %s package. "
  629. "This might mean you need to manually fix this package. "
  630. "(due to missing arch)"),
  631. Version.ParentPkg().Name());
  632. return;
  633. }
  634. /* We need to find a filename to determine the extension. We make the
  635. assumption here that all the available sources for this version share
  636. the same extension.. */
  637. // Skip not source sources, they do not have file fields.
  638. for (; Vf.end() == false; Vf++)
  639. {
  640. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  641. continue;
  642. break;
  643. }
  644. // Does not really matter here.. we are going to fail out below
  645. if (Vf.end() != true)
  646. {
  647. // If this fails to get a file name we will bomb out below.
  648. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  649. if (_error->PendingError() == true)
  650. return;
  651. // Generate the final file name as: package_version_arch.foo
  652. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  653. QuoteString(Version.VerStr(),"_:") + '_' +
  654. QuoteString(Version.Arch(),"_:.") +
  655. "." + flExtension(Parse.FileName());
  656. }
  657. // check if we have one trusted source for the package. if so, switch
  658. // to "TrustedOnly" mode
  659. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  660. {
  661. pkgIndexFile *Index;
  662. if (Sources->FindIndex(i.File(),Index) == false)
  663. continue;
  664. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  665. {
  666. std::cerr << "Checking index: " << Index->Describe()
  667. << "(Trusted=" << Index->IsTrusted() << ")\n";
  668. }
  669. if (Index->IsTrusted()) {
  670. Trusted = true;
  671. break;
  672. }
  673. }
  674. // Select a source
  675. if (QueueNext() == false && _error->PendingError() == false)
  676. _error->Error(_("I wasn't able to locate file for the %s package. "
  677. "This might mean you need to manually fix this package."),
  678. Version.ParentPkg().Name());
  679. }
  680. /*}}}*/
  681. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  682. // ---------------------------------------------------------------------
  683. /* This queues the next available file version for download. It checks if
  684. the archive is already available in the cache and stashs the MD5 for
  685. checking later. */
  686. bool pkgAcqArchive::QueueNext()
  687. {
  688. for (; Vf.end() == false; Vf++)
  689. {
  690. // Ignore not source sources
  691. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  692. continue;
  693. // Try to cross match against the source list
  694. pkgIndexFile *Index;
  695. if (Sources->FindIndex(Vf.File(),Index) == false)
  696. continue;
  697. // only try to get a trusted package from another source if that source
  698. // is also trusted
  699. if(Trusted && !Index->IsTrusted())
  700. continue;
  701. // Grab the text package record
  702. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  703. if (_error->PendingError() == true)
  704. return false;
  705. string PkgFile = Parse.FileName();
  706. MD5 = Parse.MD5Hash();
  707. if (PkgFile.empty() == true)
  708. return _error->Error(_("The package index files are corrupted. No Filename: "
  709. "field for package %s."),
  710. Version.ParentPkg().Name());
  711. Desc.URI = Index->ArchiveURI(PkgFile);
  712. Desc.Description = Index->ArchiveInfo(Version);
  713. Desc.Owner = this;
  714. Desc.ShortDesc = Version.ParentPkg().Name();
  715. // See if we already have the file. (Legacy filenames)
  716. FileSize = Version->Size;
  717. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  718. struct stat Buf;
  719. if (stat(FinalFile.c_str(),&Buf) == 0)
  720. {
  721. // Make sure the size matches
  722. if ((unsigned)Buf.st_size == Version->Size)
  723. {
  724. Complete = true;
  725. Local = true;
  726. Status = StatDone;
  727. StoreFilename = DestFile = FinalFile;
  728. return true;
  729. }
  730. /* Hmm, we have a file and its size does not match, this means it is
  731. an old style mismatched arch */
  732. unlink(FinalFile.c_str());
  733. }
  734. // Check it again using the new style output filenames
  735. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  736. if (stat(FinalFile.c_str(),&Buf) == 0)
  737. {
  738. // Make sure the size matches
  739. if ((unsigned)Buf.st_size == Version->Size)
  740. {
  741. Complete = true;
  742. Local = true;
  743. Status = StatDone;
  744. StoreFilename = DestFile = FinalFile;
  745. return true;
  746. }
  747. /* Hmm, we have a file and its size does not match, this shouldnt
  748. happen.. */
  749. unlink(FinalFile.c_str());
  750. }
  751. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  752. // Check the destination file
  753. if (stat(DestFile.c_str(),&Buf) == 0)
  754. {
  755. // Hmm, the partial file is too big, erase it
  756. if ((unsigned)Buf.st_size > Version->Size)
  757. unlink(DestFile.c_str());
  758. else
  759. PartialSize = Buf.st_size;
  760. }
  761. // Create the item
  762. Local = false;
  763. Desc.URI = Index->ArchiveURI(PkgFile);
  764. Desc.Description = Index->ArchiveInfo(Version);
  765. Desc.Owner = this;
  766. Desc.ShortDesc = Version.ParentPkg().Name();
  767. QueueURI(Desc);
  768. Vf++;
  769. return true;
  770. }
  771. return false;
  772. }
  773. /*}}}*/
  774. // AcqArchive::Done - Finished fetching /*{{{*/
  775. // ---------------------------------------------------------------------
  776. /* */
  777. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  778. pkgAcquire::MethodConfig *Cfg)
  779. {
  780. Item::Done(Message,Size,Md5Hash,Cfg);
  781. // Check the size
  782. if (Size != Version->Size)
  783. {
  784. Status = StatError;
  785. ErrorText = _("Size mismatch");
  786. return;
  787. }
  788. // Check the md5
  789. if (Md5Hash.empty() == false && MD5.empty() == false)
  790. {
  791. if (Md5Hash != MD5)
  792. {
  793. Status = StatError;
  794. ErrorText = _("MD5Sum mismatch");
  795. Rename(DestFile,DestFile + ".FAILED");
  796. return;
  797. }
  798. }
  799. // Grab the output filename
  800. string FileName = LookupTag(Message,"Filename");
  801. if (FileName.empty() == true)
  802. {
  803. Status = StatError;
  804. ErrorText = "Method gave a blank filename";
  805. return;
  806. }
  807. Complete = true;
  808. // Reference filename
  809. if (FileName != DestFile)
  810. {
  811. StoreFilename = DestFile = FileName;
  812. Local = true;
  813. return;
  814. }
  815. // Done, move it into position
  816. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  817. FinalFile += flNotDir(StoreFilename);
  818. Rename(DestFile,FinalFile);
  819. StoreFilename = DestFile = FinalFile;
  820. Complete = true;
  821. }
  822. /*}}}*/
  823. // AcqArchive::Failed - Failure handler /*{{{*/
  824. // ---------------------------------------------------------------------
  825. /* Here we try other sources */
  826. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  827. {
  828. ErrorText = LookupTag(Message,"Message");
  829. /* We don't really want to retry on failed media swaps, this prevents
  830. that. An interesting observation is that permanent failures are not
  831. recorded. */
  832. if (Cnf->Removable == true &&
  833. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  834. {
  835. // Vf = Version.FileList();
  836. while (Vf.end() == false) Vf++;
  837. StoreFilename = string();
  838. Item::Failed(Message,Cnf);
  839. return;
  840. }
  841. if (QueueNext() == false)
  842. {
  843. // This is the retry counter
  844. if (Retries != 0 &&
  845. Cnf->LocalOnly == false &&
  846. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  847. {
  848. Retries--;
  849. Vf = Version.FileList();
  850. if (QueueNext() == true)
  851. return;
  852. }
  853. StoreFilename = string();
  854. Item::Failed(Message,Cnf);
  855. }
  856. }
  857. /*}}}*/
  858. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  859. // trusted source /*{{{*/
  860. // ---------------------------------------------------------------------
  861. bool pkgAcqArchive::IsTrusted()
  862. {
  863. return Trusted;
  864. }
  865. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  866. // ---------------------------------------------------------------------
  867. /* */
  868. void pkgAcqArchive::Finished()
  869. {
  870. if (Status == pkgAcquire::Item::StatDone &&
  871. Complete == true)
  872. return;
  873. StoreFilename = string();
  874. }
  875. /*}}}*/
  876. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  877. // ---------------------------------------------------------------------
  878. /* The file is added to the queue */
  879. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  880. unsigned long Size,string Dsc,string ShortDesc) :
  881. Item(Owner), Md5Hash(MD5)
  882. {
  883. Retries = _config->FindI("Acquire::Retries",0);
  884. DestFile = flNotDir(URI);
  885. // Create the item
  886. Desc.URI = URI;
  887. Desc.Description = Dsc;
  888. Desc.Owner = this;
  889. // Set the short description to the archive component
  890. Desc.ShortDesc = ShortDesc;
  891. // Get the transfer sizes
  892. FileSize = Size;
  893. struct stat Buf;
  894. if (stat(DestFile.c_str(),&Buf) == 0)
  895. {
  896. // Hmm, the partial file is too big, erase it
  897. if ((unsigned)Buf.st_size > Size)
  898. unlink(DestFile.c_str());
  899. else
  900. PartialSize = Buf.st_size;
  901. }
  902. QueueURI(Desc);
  903. }
  904. /*}}}*/
  905. // AcqFile::Done - Item downloaded OK /*{{{*/
  906. // ---------------------------------------------------------------------
  907. /* */
  908. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  909. pkgAcquire::MethodConfig *Cnf)
  910. {
  911. // Check the md5
  912. if (Md5Hash.empty() == false && MD5.empty() == false)
  913. {
  914. if (Md5Hash != MD5)
  915. {
  916. Status = StatError;
  917. ErrorText = "MD5Sum mismatch";
  918. Rename(DestFile,DestFile + ".FAILED");
  919. return;
  920. }
  921. }
  922. Item::Done(Message,Size,MD5,Cnf);
  923. string FileName = LookupTag(Message,"Filename");
  924. if (FileName.empty() == true)
  925. {
  926. Status = StatError;
  927. ErrorText = "Method gave a blank filename";
  928. return;
  929. }
  930. Complete = true;
  931. // The files timestamp matches
  932. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  933. return;
  934. // We have to copy it into place
  935. if (FileName != DestFile)
  936. {
  937. Local = true;
  938. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  939. Cnf->Removable == true)
  940. {
  941. Desc.URI = "copy:" + FileName;
  942. QueueURI(Desc);
  943. return;
  944. }
  945. // Erase the file if it is a symlink so we can overwrite it
  946. struct stat St;
  947. if (lstat(DestFile.c_str(),&St) == 0)
  948. {
  949. if (S_ISLNK(St.st_mode) != 0)
  950. unlink(DestFile.c_str());
  951. }
  952. // Symlink the file
  953. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  954. {
  955. ErrorText = "Link to " + DestFile + " failure ";
  956. Status = StatError;
  957. Complete = false;
  958. }
  959. }
  960. }
  961. /*}}}*/
  962. // AcqFile::Failed - Failure handler /*{{{*/
  963. // ---------------------------------------------------------------------
  964. /* Here we try other sources */
  965. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  966. {
  967. ErrorText = LookupTag(Message,"Message");
  968. // This is the retry counter
  969. if (Retries != 0 &&
  970. Cnf->LocalOnly == false &&
  971. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  972. {
  973. Retries--;
  974. QueueURI(Desc);
  975. return;
  976. }
  977. Item::Failed(Message,Cnf);
  978. }
  979. /*}}}*/