acquire-item.cc 34 KB

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