acquire-item.cc 35 KB

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