acquire-item.cc 32 KB

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