acquire-item.cc 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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 the compression method
  139. if(FileExists("/bin/bzip2"))
  140. CompressionExtension = ".bz2";
  141. else
  142. CompressionExtension = ".gz";
  143. } else {
  144. CompressionExtension = comprExt;
  145. }
  146. Desc.URI = URI + CompressionExtension;
  147. Desc.Description = URIDesc;
  148. Desc.Owner = this;
  149. Desc.ShortDesc = ShortDesc;
  150. QueueURI(Desc);
  151. }
  152. /*}}}*/
  153. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* The only header we use is the last-modified header. */
  156. string pkgAcqIndex::Custom600Headers()
  157. {
  158. string Final = _config->FindDir("Dir::State::lists");
  159. Final += URItoFileName(RealURI);
  160. struct stat Buf;
  161. if (stat(Final.c_str(),&Buf) != 0)
  162. return "\nIndex-File: true";
  163. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  164. }
  165. /*}}}*/
  166. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  167. {
  168. // no .bz2 found, retry with .gz
  169. if(Desc.URI.substr(Desc.URI.size()-3) == "bz2") {
  170. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  171. // retry with a gzip one
  172. new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
  173. ExpectedMD5, string(".gz"));
  174. Status = StatDone;
  175. Complete = false;
  176. Dequeue();
  177. return;
  178. }
  179. Item::Failed(Message,Cnf);
  180. }
  181. // AcqIndex::Done - Finished a fetch /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* This goes through a number of states.. On the initial fetch the
  184. method could possibly return an alternate filename which points
  185. to the uncompressed version of the file. If this is so the file
  186. is copied into the partial directory. In all other cases the file
  187. is decompressed with a gzip uri. */
  188. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  189. pkgAcquire::MethodConfig *Cfg)
  190. {
  191. Item::Done(Message,Size,MD5,Cfg);
  192. if (Decompression == true)
  193. {
  194. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  195. {
  196. std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
  197. std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
  198. }
  199. if (MD5.empty())
  200. {
  201. MD5Summation sum;
  202. FileFd Fd(DestFile, FileFd::ReadOnly);
  203. sum.AddFD(Fd.Fd(), Fd.Size());
  204. Fd.Close();
  205. MD5 = (string)sum.Result();
  206. }
  207. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  208. {
  209. Status = StatAuthError;
  210. ErrorText = _("MD5Sum mismatch");
  211. Rename(DestFile,DestFile + ".FAILED");
  212. return;
  213. }
  214. // Done, move it into position
  215. string FinalFile = _config->FindDir("Dir::State::lists");
  216. FinalFile += URItoFileName(RealURI);
  217. Rename(DestFile,FinalFile);
  218. chmod(FinalFile.c_str(),0644);
  219. /* We restore the original name to DestFile so that the clean operation
  220. will work OK */
  221. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  222. DestFile += URItoFileName(RealURI);
  223. // Remove the compressed version.
  224. if (Erase == true)
  225. unlink(DestFile.c_str());
  226. return;
  227. }
  228. Erase = false;
  229. Complete = true;
  230. // Handle the unzipd case
  231. string FileName = LookupTag(Message,"Alt-Filename");
  232. if (FileName.empty() == false)
  233. {
  234. // The files timestamp matches
  235. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  236. return;
  237. Decompression = true;
  238. Local = true;
  239. DestFile += ".decomp";
  240. Desc.URI = "copy:" + FileName;
  241. QueueURI(Desc);
  242. Mode = "copy";
  243. return;
  244. }
  245. FileName = LookupTag(Message,"Filename");
  246. if (FileName.empty() == true)
  247. {
  248. Status = StatError;
  249. ErrorText = "Method gave a blank filename";
  250. }
  251. // The files timestamp matches
  252. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  253. return;
  254. if (FileName == DestFile)
  255. Erase = true;
  256. else
  257. Local = true;
  258. string compExt = Desc.URI.substr(Desc.URI.size()-3);
  259. char *decompProg;
  260. if(compExt == "bz2")
  261. decompProg = "bzip2";
  262. else if(compExt == ".gz")
  263. decompProg = "gzip";
  264. else {
  265. _error->Error("Unsupported extension: %s", compExt.c_str());
  266. return;
  267. }
  268. Decompression = true;
  269. DestFile += ".decomp";
  270. Desc.URI = string(decompProg) + ":" + FileName;
  271. QueueURI(Desc);
  272. Mode = decompProg;
  273. }
  274. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  275. string URI,string URIDesc,string ShortDesc,
  276. string MetaIndexURI, string MetaIndexURIDesc,
  277. string MetaIndexShortDesc,
  278. const vector<IndexTarget*>* IndexTargets,
  279. indexRecords* MetaIndexParser) :
  280. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  281. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
  282. MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
  283. {
  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. struct stat Buf;
  312. if (stat(DestFile.c_str(),&Buf) != 0)
  313. return "\nIndex-File: true";
  314. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  315. }
  316. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  317. pkgAcquire::MethodConfig *Cfg)
  318. {
  319. Item::Done(Message,Size,MD5,Cfg);
  320. string FileName = LookupTag(Message,"Filename");
  321. if (FileName.empty() == true)
  322. {
  323. Status = StatError;
  324. ErrorText = "Method gave a blank filename";
  325. return;
  326. }
  327. if (FileName != DestFile)
  328. {
  329. // We have to copy it into place
  330. Local = true;
  331. Desc.URI = "copy:" + FileName;
  332. QueueURI(Desc);
  333. return;
  334. }
  335. Complete = true;
  336. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  337. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  338. DestFile, IndexTargets, MetaIndexParser);
  339. }
  340. /*}}}*/
  341. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  342. {
  343. // if we get a network error we fail gracefully
  344. if(LookupTag(Message,"FailReason") == "Timeout" ||
  345. LookupTag(Message,"FailReason") == "TmpResolveFailure" ||
  346. LookupTag(Message,"FailReason") == "ConnectionRefused") {
  347. Item::Failed(Message,Cnf);
  348. return;
  349. }
  350. // Delete any existing sigfile when the acquire failed
  351. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  352. unlink(Final.c_str());
  353. // queue a pkgAcqMetaIndex with no sigfile
  354. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  355. "", IndexTargets, MetaIndexParser);
  356. if (Cnf->LocalOnly == true ||
  357. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  358. {
  359. // Ignore this
  360. Status = StatDone;
  361. Complete = false;
  362. Dequeue();
  363. return;
  364. }
  365. Item::Failed(Message,Cnf);
  366. }
  367. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  368. string URI,string URIDesc,string ShortDesc,
  369. string SigFile,
  370. const vector<struct IndexTarget*>* IndexTargets,
  371. indexRecords* MetaIndexParser) :
  372. Item(Owner), RealURI(URI), SigFile(SigFile), AuthPass(false),
  373. MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets), IMSHit(false)
  374. {
  375. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  376. DestFile += URItoFileName(URI);
  377. // Create the item
  378. Desc.Description = URIDesc;
  379. Desc.Owner = this;
  380. Desc.ShortDesc = ShortDesc;
  381. Desc.URI = URI;
  382. QueueURI(Desc);
  383. }
  384. /*}}}*/
  385. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  386. // ---------------------------------------------------------------------
  387. /* The only header we use is the last-modified header. */
  388. string pkgAcqMetaIndex::Custom600Headers()
  389. {
  390. string Final = _config->FindDir("Dir::State::lists");
  391. Final += URItoFileName(RealURI);
  392. struct stat Buf;
  393. if (stat(Final.c_str(),&Buf) != 0)
  394. return "\nIndex-File: true";
  395. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  396. }
  397. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  398. pkgAcquire::MethodConfig *Cfg)
  399. {
  400. Item::Done(Message,Size,MD5,Cfg);
  401. // MetaIndexes are done in two passes: one to download the
  402. // metaindex with an appropriate method, and a second to verify it
  403. // with the gpgv method
  404. if (AuthPass == true)
  405. {
  406. AuthDone(Message);
  407. }
  408. else
  409. {
  410. RetrievalDone(Message);
  411. if (!Complete)
  412. // Still more retrieving to do
  413. return;
  414. if (SigFile == "")
  415. {
  416. // There was no signature file, so we are finished. Download
  417. // the indexes without verification.
  418. QueueIndexes(false);
  419. }
  420. else
  421. {
  422. // There was a signature file, so pass it to gpgv for
  423. // verification
  424. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  425. std::cerr << "Metaindex acquired, queueing gpg verification ("
  426. << SigFile << "," << DestFile << ")\n";
  427. AuthPass = true;
  428. Desc.URI = "gpgv:" + SigFile;
  429. QueueURI(Desc);
  430. Mode = "gpgv";
  431. }
  432. }
  433. }
  434. void pkgAcqMetaIndex::RetrievalDone(string Message)
  435. {
  436. // We have just finished downloading a Release file (it is not
  437. // verified yet)
  438. string FileName = LookupTag(Message,"Filename");
  439. if (FileName.empty() == true)
  440. {
  441. Status = StatError;
  442. ErrorText = "Method gave a blank filename";
  443. return;
  444. }
  445. if (FileName != DestFile)
  446. {
  447. Local = true;
  448. Desc.URI = "copy:" + FileName;
  449. QueueURI(Desc);
  450. return;
  451. }
  452. // see if the download was a IMSHit
  453. IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
  454. Complete = true;
  455. string FinalFile = _config->FindDir("Dir::State::lists");
  456. FinalFile += URItoFileName(RealURI);
  457. // The files timestamp matches
  458. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  459. {
  460. // Move it into position
  461. Rename(DestFile,FinalFile);
  462. }
  463. DestFile = FinalFile;
  464. }
  465. void pkgAcqMetaIndex::AuthDone(string Message)
  466. {
  467. // At this point, the gpgv method has succeeded, so there is a
  468. // valid signature from a key in the trusted keyring. We
  469. // perform additional verification of its contents, and use them
  470. // to verify the indexes we are about to download
  471. if (!MetaIndexParser->Load(DestFile))
  472. {
  473. Status = StatAuthError;
  474. ErrorText = MetaIndexParser->ErrorText;
  475. return;
  476. }
  477. if (!VerifyVendor(Message))
  478. {
  479. return;
  480. }
  481. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  482. std::cerr << "Signature verification succeeded: "
  483. << DestFile << std::endl;
  484. // Download further indexes with verification
  485. QueueIndexes(true);
  486. // Done, move signature file into position
  487. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  488. URItoFileName(RealURI) + ".gpg";
  489. Rename(SigFile,VerifiedSigFile);
  490. chmod(VerifiedSigFile.c_str(),0644);
  491. }
  492. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  493. {
  494. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  495. Target != IndexTargets->end();
  496. Target++)
  497. {
  498. string ExpectedIndexMD5;
  499. if (verify)
  500. {
  501. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  502. if (!Record)
  503. {
  504. Status = StatAuthError;
  505. ErrorText = "Unable to find expected entry "
  506. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  507. return;
  508. }
  509. ExpectedIndexMD5 = Record->MD5Hash;
  510. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  511. {
  512. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  513. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  514. }
  515. if (ExpectedIndexMD5.empty())
  516. {
  517. Status = StatAuthError;
  518. ErrorText = "Unable to find MD5 sum for "
  519. + (*Target)->MetaKey + " in Meta-index file";
  520. return;
  521. }
  522. }
  523. // Queue Packages file
  524. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  525. (*Target)->ShortDesc, ExpectedIndexMD5);
  526. }
  527. }
  528. bool pkgAcqMetaIndex::VerifyVendor(string Message)
  529. {
  530. // // Maybe this should be made available from above so we don't have
  531. // // to read and parse it every time?
  532. // pkgVendorList List;
  533. // List.ReadMainList();
  534. // const Vendor* Vndr = NULL;
  535. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  536. // {
  537. // string::size_type pos = (*I).find("VALIDSIG ");
  538. // if (_config->FindB("Debug::Vendor", false))
  539. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  540. // << std::endl;
  541. // if (pos != std::string::npos)
  542. // {
  543. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  544. // if (_config->FindB("Debug::Vendor", false))
  545. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  546. // std::endl;
  547. // Vndr = List.FindVendor(Fingerprint) != "";
  548. // if (Vndr != NULL);
  549. // break;
  550. // }
  551. // }
  552. string::size_type pos;
  553. // check for missing sigs (that where not fatal because otherwise we had
  554. // bombed earlier)
  555. string missingkeys;
  556. string msg = _("There are no public key available for the "
  557. "following key IDs:\n");
  558. pos = Message.find("NO_PUBKEY ");
  559. if (pos != std::string::npos)
  560. {
  561. string::size_type start = pos+strlen("NO_PUBKEY ");
  562. string Fingerprint = Message.substr(start, Message.find("\n")-start);
  563. missingkeys += (Fingerprint);
  564. }
  565. if(!missingkeys.empty())
  566. _error->Warning("%s", string(msg+missingkeys).c_str());
  567. string Transformed = MetaIndexParser->GetExpectedDist();
  568. if (Transformed == "../project/experimental")
  569. {
  570. Transformed = "experimental";
  571. }
  572. pos = Transformed.rfind('/');
  573. if (pos != string::npos)
  574. {
  575. Transformed = Transformed.substr(0, pos);
  576. }
  577. if (Transformed == ".")
  578. {
  579. Transformed = "";
  580. }
  581. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  582. {
  583. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  584. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  585. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  586. }
  587. if (MetaIndexParser->CheckDist(Transformed) == false)
  588. {
  589. // This might become fatal one day
  590. // Status = StatAuthError;
  591. // ErrorText = "Conflicting distribution; expected "
  592. // + MetaIndexParser->GetExpectedDist() + " but got "
  593. // + MetaIndexParser->GetDist();
  594. // return false;
  595. if (!Transformed.empty())
  596. {
  597. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  598. Desc.Description.c_str(),
  599. Transformed.c_str(),
  600. MetaIndexParser->GetDist().c_str());
  601. }
  602. }
  603. return true;
  604. }
  605. /*}}}*/
  606. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  607. // file present /*{{{*/
  608. // ---------------------------------------------------------------------
  609. /* */
  610. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  611. {
  612. if (AuthPass == true)
  613. {
  614. // if we fail the authentication but got the file via a IMS-Hit
  615. // this means that the file wasn't downloaded and that it might be
  616. // just stale (server problem, proxy etc). we delete what we have
  617. // queue it again without i-m-s
  618. // alternatively we could just unlink the file and let the user try again
  619. if (IMSHit)
  620. {
  621. Complete = false;
  622. Local = false;
  623. AuthPass = false;
  624. unlink(DestFile.c_str());
  625. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  626. DestFile += URItoFileName(RealURI);
  627. Desc.URI = RealURI;
  628. QueueURI(Desc);
  629. return;
  630. }
  631. // gpgv method failed
  632. _error->Warning("GPG error: %s: %s",
  633. Desc.Description.c_str(),
  634. LookupTag(Message,"Message").c_str());
  635. }
  636. // No Release file was present, or verification failed, so fall
  637. // back to queueing Packages files without verification
  638. QueueIndexes(false);
  639. }
  640. /*}}}*/
  641. // AcqArchive::AcqArchive - Constructor /*{{{*/
  642. // ---------------------------------------------------------------------
  643. /* This just sets up the initial fetch environment and queues the first
  644. possibilitiy */
  645. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  646. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  647. string &StoreFilename) :
  648. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  649. StoreFilename(StoreFilename), Vf(Version.FileList()),
  650. Trusted(false)
  651. {
  652. Retries = _config->FindI("Acquire::Retries",0);
  653. if (Version.Arch() == 0)
  654. {
  655. _error->Error(_("I wasn't able to locate a file for the %s package. "
  656. "This might mean you need to manually fix this package. "
  657. "(due to missing arch)"),
  658. Version.ParentPkg().Name());
  659. return;
  660. }
  661. /* We need to find a filename to determine the extension. We make the
  662. assumption here that all the available sources for this version share
  663. the same extension.. */
  664. // Skip not source sources, they do not have file fields.
  665. for (; Vf.end() == false; Vf++)
  666. {
  667. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  668. continue;
  669. break;
  670. }
  671. // Does not really matter here.. we are going to fail out below
  672. if (Vf.end() != true)
  673. {
  674. // If this fails to get a file name we will bomb out below.
  675. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  676. if (_error->PendingError() == true)
  677. return;
  678. // Generate the final file name as: package_version_arch.foo
  679. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  680. QuoteString(Version.VerStr(),"_:") + '_' +
  681. QuoteString(Version.Arch(),"_:.") +
  682. "." + flExtension(Parse.FileName());
  683. }
  684. // check if we have one trusted source for the package. if so, switch
  685. // to "TrustedOnly" mode
  686. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  687. {
  688. pkgIndexFile *Index;
  689. if (Sources->FindIndex(i.File(),Index) == false)
  690. continue;
  691. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  692. {
  693. std::cerr << "Checking index: " << Index->Describe()
  694. << "(Trusted=" << Index->IsTrusted() << ")\n";
  695. }
  696. if (Index->IsTrusted()) {
  697. Trusted = true;
  698. break;
  699. }
  700. }
  701. // "allow-unauthenticated" restores apts old fetching behaviour
  702. // that means that e.g. unauthenticated file:// uris are higher
  703. // priority than authenticated http:// uris
  704. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  705. Trusted = false;
  706. // Select a source
  707. if (QueueNext() == false && _error->PendingError() == false)
  708. _error->Error(_("I wasn't able to locate file for the %s package. "
  709. "This might mean you need to manually fix this package."),
  710. Version.ParentPkg().Name());
  711. }
  712. /*}}}*/
  713. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  714. // ---------------------------------------------------------------------
  715. /* This queues the next available file version for download. It checks if
  716. the archive is already available in the cache and stashs the MD5 for
  717. checking later. */
  718. bool pkgAcqArchive::QueueNext()
  719. {
  720. for (; Vf.end() == false; Vf++)
  721. {
  722. // Ignore not source sources
  723. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  724. continue;
  725. // Try to cross match against the source list
  726. pkgIndexFile *Index;
  727. if (Sources->FindIndex(Vf.File(),Index) == false)
  728. continue;
  729. // only try to get a trusted package from another source if that source
  730. // is also trusted
  731. if(Trusted && !Index->IsTrusted())
  732. continue;
  733. // Grab the text package record
  734. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  735. if (_error->PendingError() == true)
  736. return false;
  737. string PkgFile = Parse.FileName();
  738. MD5 = Parse.MD5Hash();
  739. if (PkgFile.empty() == true)
  740. return _error->Error(_("The package index files are corrupted. No Filename: "
  741. "field for package %s."),
  742. Version.ParentPkg().Name());
  743. Desc.URI = Index->ArchiveURI(PkgFile);
  744. Desc.Description = Index->ArchiveInfo(Version);
  745. Desc.Owner = this;
  746. Desc.ShortDesc = Version.ParentPkg().Name();
  747. // See if we already have the file. (Legacy filenames)
  748. FileSize = Version->Size;
  749. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  750. struct stat Buf;
  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 means it is
  763. an old style mismatched arch */
  764. unlink(FinalFile.c_str());
  765. }
  766. // Check it again using the new style output filenames
  767. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  768. if (stat(FinalFile.c_str(),&Buf) == 0)
  769. {
  770. // Make sure the size matches
  771. if ((unsigned)Buf.st_size == Version->Size)
  772. {
  773. Complete = true;
  774. Local = true;
  775. Status = StatDone;
  776. StoreFilename = DestFile = FinalFile;
  777. return true;
  778. }
  779. /* Hmm, we have a file and its size does not match, this shouldnt
  780. happen.. */
  781. unlink(FinalFile.c_str());
  782. }
  783. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  784. // Check the destination file
  785. if (stat(DestFile.c_str(),&Buf) == 0)
  786. {
  787. // Hmm, the partial file is too big, erase it
  788. if ((unsigned)Buf.st_size > Version->Size)
  789. unlink(DestFile.c_str());
  790. else
  791. PartialSize = Buf.st_size;
  792. }
  793. // Create the item
  794. Local = false;
  795. Desc.URI = Index->ArchiveURI(PkgFile);
  796. Desc.Description = Index->ArchiveInfo(Version);
  797. Desc.Owner = this;
  798. Desc.ShortDesc = Version.ParentPkg().Name();
  799. QueueURI(Desc);
  800. Vf++;
  801. return true;
  802. }
  803. return false;
  804. }
  805. /*}}}*/
  806. // AcqArchive::Done - Finished fetching /*{{{*/
  807. // ---------------------------------------------------------------------
  808. /* */
  809. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  810. pkgAcquire::MethodConfig *Cfg)
  811. {
  812. Item::Done(Message,Size,Md5Hash,Cfg);
  813. // Check the size
  814. if (Size != Version->Size)
  815. {
  816. Status = StatError;
  817. ErrorText = _("Size mismatch");
  818. return;
  819. }
  820. // Check the md5
  821. if (Md5Hash.empty() == false && MD5.empty() == false)
  822. {
  823. if (Md5Hash != MD5)
  824. {
  825. Status = StatError;
  826. ErrorText = _("MD5Sum mismatch");
  827. if(FileExists(DestFile))
  828. Rename(DestFile,DestFile + ".FAILED");
  829. return;
  830. }
  831. }
  832. // Grab the output filename
  833. string FileName = LookupTag(Message,"Filename");
  834. if (FileName.empty() == true)
  835. {
  836. Status = StatError;
  837. ErrorText = "Method gave a blank filename";
  838. return;
  839. }
  840. Complete = true;
  841. // Reference filename
  842. if (FileName != DestFile)
  843. {
  844. StoreFilename = DestFile = FileName;
  845. Local = true;
  846. return;
  847. }
  848. // Done, move it into position
  849. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  850. FinalFile += flNotDir(StoreFilename);
  851. Rename(DestFile,FinalFile);
  852. StoreFilename = DestFile = FinalFile;
  853. Complete = true;
  854. }
  855. /*}}}*/
  856. // AcqArchive::Failed - Failure handler /*{{{*/
  857. // ---------------------------------------------------------------------
  858. /* Here we try other sources */
  859. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  860. {
  861. ErrorText = LookupTag(Message,"Message");
  862. /* We don't really want to retry on failed media swaps, this prevents
  863. that. An interesting observation is that permanent failures are not
  864. recorded. */
  865. if (Cnf->Removable == true &&
  866. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  867. {
  868. // Vf = Version.FileList();
  869. while (Vf.end() == false) Vf++;
  870. StoreFilename = string();
  871. Item::Failed(Message,Cnf);
  872. return;
  873. }
  874. if (QueueNext() == false)
  875. {
  876. // This is the retry counter
  877. if (Retries != 0 &&
  878. Cnf->LocalOnly == false &&
  879. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  880. {
  881. Retries--;
  882. Vf = Version.FileList();
  883. if (QueueNext() == true)
  884. return;
  885. }
  886. StoreFilename = string();
  887. Item::Failed(Message,Cnf);
  888. }
  889. }
  890. /*}}}*/
  891. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  892. // trusted source /*{{{*/
  893. // ---------------------------------------------------------------------
  894. bool pkgAcqArchive::IsTrusted()
  895. {
  896. return Trusted;
  897. }
  898. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  899. // ---------------------------------------------------------------------
  900. /* */
  901. void pkgAcqArchive::Finished()
  902. {
  903. if (Status == pkgAcquire::Item::StatDone &&
  904. Complete == true)
  905. return;
  906. StoreFilename = string();
  907. }
  908. /*}}}*/
  909. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  910. // ---------------------------------------------------------------------
  911. /* The file is added to the queue */
  912. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  913. unsigned long Size,string Dsc,string ShortDesc,
  914. const string &DestDir, const string &DestFilename) :
  915. Item(Owner), Md5Hash(MD5)
  916. {
  917. Retries = _config->FindI("Acquire::Retries",0);
  918. if(!DestFilename.empty())
  919. DestFile = DestFilename;
  920. else if(!DestDir.empty())
  921. DestFile = DestDir + "/" + flNotDir(URI);
  922. else
  923. DestFile = flNotDir(URI);
  924. // Create the item
  925. Desc.URI = URI;
  926. Desc.Description = Dsc;
  927. Desc.Owner = this;
  928. // Set the short description to the archive component
  929. Desc.ShortDesc = ShortDesc;
  930. // Get the transfer sizes
  931. FileSize = Size;
  932. struct stat Buf;
  933. if (stat(DestFile.c_str(),&Buf) == 0)
  934. {
  935. // Hmm, the partial file is too big, erase it
  936. if ((unsigned)Buf.st_size > Size)
  937. unlink(DestFile.c_str());
  938. else
  939. PartialSize = Buf.st_size;
  940. }
  941. QueueURI(Desc);
  942. }
  943. /*}}}*/
  944. // AcqFile::Done - Item downloaded OK /*{{{*/
  945. // ---------------------------------------------------------------------
  946. /* */
  947. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  948. pkgAcquire::MethodConfig *Cnf)
  949. {
  950. // Check the md5
  951. if (Md5Hash.empty() == false && MD5.empty() == false)
  952. {
  953. if (Md5Hash != MD5)
  954. {
  955. Status = StatError;
  956. ErrorText = "MD5Sum mismatch";
  957. Rename(DestFile,DestFile + ".FAILED");
  958. return;
  959. }
  960. }
  961. Item::Done(Message,Size,MD5,Cnf);
  962. string FileName = LookupTag(Message,"Filename");
  963. if (FileName.empty() == true)
  964. {
  965. Status = StatError;
  966. ErrorText = "Method gave a blank filename";
  967. return;
  968. }
  969. Complete = true;
  970. // The files timestamp matches
  971. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  972. return;
  973. // We have to copy it into place
  974. if (FileName != DestFile)
  975. {
  976. Local = true;
  977. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  978. Cnf->Removable == true)
  979. {
  980. Desc.URI = "copy:" + FileName;
  981. QueueURI(Desc);
  982. return;
  983. }
  984. // Erase the file if it is a symlink so we can overwrite it
  985. struct stat St;
  986. if (lstat(DestFile.c_str(),&St) == 0)
  987. {
  988. if (S_ISLNK(St.st_mode) != 0)
  989. unlink(DestFile.c_str());
  990. }
  991. // Symlink the file
  992. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  993. {
  994. ErrorText = "Link to " + DestFile + " failure ";
  995. Status = StatError;
  996. Complete = false;
  997. }
  998. }
  999. }
  1000. /*}}}*/
  1001. // AcqFile::Failed - Failure handler /*{{{*/
  1002. // ---------------------------------------------------------------------
  1003. /* Here we try other sources */
  1004. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1005. {
  1006. ErrorText = LookupTag(Message,"Message");
  1007. // This is the retry counter
  1008. if (Retries != 0 &&
  1009. Cnf->LocalOnly == false &&
  1010. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1011. {
  1012. Retries--;
  1013. QueueURI(Desc);
  1014. return;
  1015. }
  1016. Item::Failed(Message,Cnf);
  1017. }
  1018. /*}}}*/