debmetaindex.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. #include <config.h>
  2. #include <apt-pkg/error.h>
  3. #include <apt-pkg/debmetaindex.h>
  4. #include <apt-pkg/debindexfile.h>
  5. #include <apt-pkg/strutl.h>
  6. #include <apt-pkg/fileutl.h>
  7. #include <apt-pkg/acquire-item.h>
  8. #include <apt-pkg/configuration.h>
  9. #include <apt-pkg/aptconfiguration.h>
  10. #include <apt-pkg/sourcelist.h>
  11. #include <apt-pkg/hashes.h>
  12. #include <apt-pkg/metaindex.h>
  13. #include <apt-pkg/pkgcachegen.h>
  14. #include <apt-pkg/tagfile.h>
  15. #include <apt-pkg/gpgv.h>
  16. #include <apt-pkg/macros.h>
  17. #include <map>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <sstream>
  23. #include <sys/stat.h>
  24. #include <string.h>
  25. #include <apti18n.h>
  26. class APT_HIDDEN debReleaseIndexPrivate /*{{{*/
  27. {
  28. public:
  29. struct APT_HIDDEN debSectionEntry
  30. {
  31. std::string sourcesEntry;
  32. std::string Name;
  33. std::vector<std::string> Targets;
  34. std::vector<std::string> Architectures;
  35. std::vector<std::string> Languages;
  36. bool UsePDiffs;
  37. std::string UseByHash;
  38. };
  39. std::vector<debSectionEntry> DebEntries;
  40. std::vector<debSectionEntry> DebSrcEntries;
  41. metaIndex::TriState CheckValidUntil;
  42. time_t ValidUntilMin;
  43. time_t ValidUntilMax;
  44. std::vector<std::string> Architectures;
  45. std::vector<std::string> NoSupportForAll;
  46. debReleaseIndexPrivate() : CheckValidUntil(metaIndex::TRI_UNSET), ValidUntilMin(0), ValidUntilMax(0) {}
  47. };
  48. /*}}}*/
  49. // ReleaseIndex::MetaIndex* - display helpers /*{{{*/
  50. std::string debReleaseIndex::MetaIndexInfo(const char *Type) const
  51. {
  52. std::string Info = ::URI::ArchiveOnly(URI) + ' ';
  53. if (Dist[Dist.size() - 1] == '/')
  54. {
  55. if (Dist != "/")
  56. Info += Dist;
  57. }
  58. else
  59. Info += Dist;
  60. Info += " ";
  61. Info += Type;
  62. return Info;
  63. }
  64. std::string debReleaseIndex::Describe() const
  65. {
  66. return MetaIndexInfo("Release");
  67. }
  68. std::string debReleaseIndex::MetaIndexFile(const char *Type) const
  69. {
  70. return _config->FindDir("Dir::State::lists") +
  71. URItoFileName(MetaIndexURI(Type));
  72. }
  73. std::string debReleaseIndex::MetaIndexURI(const char *Type) const
  74. {
  75. std::string Res;
  76. if (Dist == "/")
  77. Res = URI;
  78. else if (Dist[Dist.size()-1] == '/')
  79. Res = URI + Dist;
  80. else
  81. Res = URI + "dists/" + Dist + "/";
  82. Res += Type;
  83. return Res;
  84. }
  85. /*}}}*/
  86. // ReleaseIndex Con- and Destructors /*{{{*/
  87. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist) :
  88. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
  89. {}
  90. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, bool const pTrusted) :
  91. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
  92. {
  93. Trusted = pTrusted ? TRI_YES : TRI_NO;
  94. }
  95. debReleaseIndex::~debReleaseIndex() {
  96. if (d != NULL)
  97. delete d;
  98. }
  99. /*}}}*/
  100. // ReleaseIndex::GetIndexTargets /*{{{*/
  101. static void GetIndexTargetsFor(char const * const Type, std::string const &URI, std::string const &Dist,
  102. std::vector<debReleaseIndexPrivate::debSectionEntry> const &entries,
  103. std::vector<IndexTarget> &IndexTargets)
  104. {
  105. bool const flatArchive = (Dist[Dist.length() - 1] == '/');
  106. std::string baseURI = URI;
  107. if (flatArchive)
  108. {
  109. if (Dist != "/")
  110. baseURI += Dist;
  111. }
  112. else
  113. baseURI += "dists/" + Dist + "/";
  114. std::string const Release = (Dist == "/") ? "" : Dist;
  115. std::string const Site = ::URI::ArchiveOnly(URI);
  116. std::string DefCompressionTypes;
  117. {
  118. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  119. if (types.empty() == false)
  120. {
  121. std::ostringstream os;
  122. std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " "));
  123. os << *types.rbegin();
  124. DefCompressionTypes = os.str();
  125. }
  126. }
  127. std::string const NativeArch = _config->Find("APT::Architecture");
  128. bool const GzipIndex = _config->FindB("Acquire::GzipIndexes", false);
  129. for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E)
  130. {
  131. for (std::vector<std::string>::const_iterator T = E->Targets.begin(); T != E->Targets.end(); ++T)
  132. {
  133. #define APT_T_CONFIG_STR(X, Y) _config->Find(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
  134. #define APT_T_CONFIG_BOOL(X, Y) _config->FindB(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
  135. std::string const tplMetaKey = APT_T_CONFIG_STR(flatArchive ? "flatMetaKey" : "MetaKey", "");
  136. std::string const tplShortDesc = APT_T_CONFIG_STR("ShortDescription", "");
  137. std::string const tplLongDesc = "$(SITE) " + APT_T_CONFIG_STR(flatArchive ? "flatDescription" : "Description", "");
  138. bool const IsOptional = APT_T_CONFIG_BOOL("Optional", true);
  139. bool const KeepCompressed = APT_T_CONFIG_BOOL("KeepCompressed", GzipIndex);
  140. bool const DefaultEnabled = APT_T_CONFIG_BOOL("DefaultEnabled", true);
  141. bool const UsePDiffs = APT_T_CONFIG_BOOL("PDiffs", E->UsePDiffs);
  142. std::string const UseByHash = APT_T_CONFIG_STR("By-Hash", E->UseByHash);
  143. std::string const CompressionTypes = APT_T_CONFIG_STR("CompressionTypes", DefCompressionTypes);
  144. #undef APT_T_CONFIG_BOOL
  145. #undef APT_T_CONFIG_STR
  146. if (tplMetaKey.empty())
  147. continue;
  148. for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L)
  149. {
  150. if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  151. continue;
  152. for (std::vector<std::string>::const_iterator A = E->Architectures.begin(); A != E->Architectures.end(); ++A)
  153. {
  154. // available in templates
  155. std::map<std::string, std::string> Options;
  156. Options.insert(std::make_pair("SITE", Site));
  157. Options.insert(std::make_pair("RELEASE", Release));
  158. if (tplMetaKey.find("$(COMPONENT)") != std::string::npos)
  159. Options.insert(std::make_pair("COMPONENT", E->Name));
  160. if (tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  161. Options.insert(std::make_pair("LANGUAGE", *L));
  162. if (tplMetaKey.find("$(ARCHITECTURE)") != std::string::npos)
  163. Options.insert(std::make_pair("ARCHITECTURE", *A));
  164. else if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
  165. Options.insert(std::make_pair("ARCHITECTURE", NativeArch));
  166. if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
  167. Options.insert(std::make_pair("NATIVE_ARCHITECTURE", NativeArch));
  168. std::string MetaKey = tplMetaKey;
  169. std::string ShortDesc = tplShortDesc;
  170. std::string LongDesc = tplLongDesc;
  171. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  172. {
  173. MetaKey = SubstVar(MetaKey, std::string("$(") + O->first + ")", O->second);
  174. ShortDesc = SubstVar(ShortDesc, std::string("$(") + O->first + ")", O->second);
  175. LongDesc = SubstVar(LongDesc, std::string("$(") + O->first + ")", O->second);
  176. }
  177. {
  178. auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
  179. return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
  180. E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T == IT.Option(IndexTarget::CREATED_BY);
  181. });
  182. if (dup != IndexTargets.end())
  183. {
  184. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  185. break;
  186. continue;
  187. }
  188. }
  189. {
  190. auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
  191. return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
  192. E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T != IT.Option(IndexTarget::CREATED_BY);
  193. });
  194. if (dup != IndexTargets.end())
  195. {
  196. std::string const dupT = dup->Option(IndexTarget::CREATED_BY);
  197. std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
  198. //TRANSLATOR: an identifier like Packages; Releasefile key indicating
  199. // a file like main/binary-amd64/Packages; another identifier like Contents;
  200. // filename and linenumber of the sources.list entry currently parsed
  201. _error->Warning(_("Target %s wants to acquire the same file (%s) as %s from source %s"),
  202. T->c_str(), MetaKey.c_str(), dupT.c_str(), dupEntry.c_str());
  203. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  204. break;
  205. continue;
  206. }
  207. }
  208. {
  209. auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &T) {
  210. return MetaKey == T.MetaKey && baseURI == T.Option(IndexTarget::BASE_URI) &&
  211. E->sourcesEntry != T.Option(IndexTarget::SOURCESENTRY);
  212. });
  213. if (dup != IndexTargets.end())
  214. {
  215. std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
  216. //TRANSLATOR: an identifier like Packages; Releasefile key indicating
  217. // a file like main/binary-amd64/Packages; filename and linenumber of
  218. // two sources.list entries
  219. _error->Warning(_("Target %s (%s) is configured multiple times in %s and %s"),
  220. T->c_str(), MetaKey.c_str(), dupEntry.c_str(), E->sourcesEntry.c_str());
  221. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  222. break;
  223. continue;
  224. }
  225. }
  226. // not available in templates, but in the indextarget
  227. Options.insert(std::make_pair("BASE_URI", baseURI));
  228. Options.insert(std::make_pair("REPO_URI", URI));
  229. Options.insert(std::make_pair("TARGET_OF", Type));
  230. Options.insert(std::make_pair("CREATED_BY", *T));
  231. Options.insert(std::make_pair("PDIFFS", UsePDiffs ? "yes" : "no"));
  232. Options.insert(std::make_pair("BY_HASH", UseByHash));
  233. Options.insert(std::make_pair("DEFAULTENABLED", DefaultEnabled ? "yes" : "no"));
  234. Options.insert(std::make_pair("COMPRESSIONTYPES", CompressionTypes));
  235. Options.insert(std::make_pair("SOURCESENTRY", E->sourcesEntry));
  236. bool IsOpt = IsOptional;
  237. if (IsOpt == false)
  238. {
  239. auto const arch = Options.find("ARCHITECTURE");
  240. if (arch != Options.end() && arch->second == "all")
  241. IsOpt = true;
  242. }
  243. IndexTarget Target(
  244. MetaKey,
  245. ShortDesc,
  246. LongDesc,
  247. Options.find("BASE_URI")->second + MetaKey,
  248. IsOpt,
  249. KeepCompressed,
  250. Options
  251. );
  252. IndexTargets.push_back(Target);
  253. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  254. break;
  255. }
  256. if (tplMetaKey.find("$(LANGUAGE)") == std::string::npos)
  257. break;
  258. }
  259. }
  260. }
  261. }
  262. std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
  263. {
  264. std::vector<IndexTarget> IndexTargets;
  265. GetIndexTargetsFor("deb-src", URI, Dist, d->DebSrcEntries, IndexTargets);
  266. GetIndexTargetsFor("deb", URI, Dist, d->DebEntries, IndexTargets);
  267. return IndexTargets;
  268. }
  269. /*}}}*/
  270. void debReleaseIndex::AddComponent(std::string const &sourcesEntry, /*{{{*/
  271. bool const isSrc, std::string const &Name,
  272. std::vector<std::string> const &Targets,
  273. std::vector<std::string> const &Architectures,
  274. std::vector<std::string> Languages,
  275. bool const usePDiffs, std::string const &useByHash)
  276. {
  277. if (Languages.empty() == true)
  278. Languages.push_back("none");
  279. debReleaseIndexPrivate::debSectionEntry const entry = {
  280. sourcesEntry, Name, Targets, Architectures, Languages, usePDiffs, useByHash
  281. };
  282. if (isSrc)
  283. d->DebSrcEntries.push_back(entry);
  284. else
  285. d->DebEntries.push_back(entry);
  286. }
  287. /*}}}*/
  288. bool debReleaseIndex::Load(std::string const &Filename, std::string * const ErrorText)/*{{{*/
  289. {
  290. LoadedSuccessfully = TRI_NO;
  291. FileFd Fd;
  292. if (OpenMaybeClearSignedFile(Filename, Fd) == false)
  293. return false;
  294. pkgTagFile TagFile(&Fd, Fd.Size());
  295. if (Fd.IsOpen() == false || Fd.Failed())
  296. {
  297. if (ErrorText != NULL)
  298. strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
  299. return false;
  300. }
  301. pkgTagSection Section;
  302. const char *Start, *End;
  303. if (TagFile.Step(Section) == false)
  304. {
  305. if (ErrorText != NULL)
  306. strprintf(*ErrorText, _("No sections in Release file %s"), Filename.c_str());
  307. return false;
  308. }
  309. // FIXME: find better tag name
  310. SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false);
  311. Suite = Section.FindS("Suite");
  312. Codename = Section.FindS("Codename");
  313. {
  314. std::string const archs = Section.FindS("Architectures");
  315. if (archs.empty() == false)
  316. d->Architectures = VectorizeString(archs, ' ');
  317. }
  318. {
  319. std::string const targets = Section.FindS("No-Support-for-Architecture-all");
  320. if (targets.empty() == false)
  321. d->NoSupportForAll = VectorizeString(targets, ' ');
  322. }
  323. bool FoundHashSum = false;
  324. bool FoundStrongHashSum = false;
  325. auto const SupportedHashes = HashString::SupportedHashes();
  326. for (int i=0; SupportedHashes[i] != NULL; i++)
  327. {
  328. if (!Section.Find(SupportedHashes[i], Start, End))
  329. continue;
  330. std::string Name;
  331. std::string Hash;
  332. unsigned long long Size;
  333. while (Start < End)
  334. {
  335. if (!parseSumData(Start, End, Name, Hash, Size))
  336. return false;
  337. HashString const hs(SupportedHashes[i], Hash);
  338. if (Entries.find(Name) == Entries.end())
  339. {
  340. metaIndex::checkSum *Sum = new metaIndex::checkSum;
  341. Sum->MetaKeyFilename = Name;
  342. Sum->Size = Size;
  343. Sum->Hashes.FileSize(Size);
  344. APT_IGNORE_DEPRECATED(Sum->Hash = hs;)
  345. Entries[Name] = Sum;
  346. }
  347. Entries[Name]->Hashes.push_back(hs);
  348. FoundHashSum = true;
  349. if (FoundStrongHashSum == false && hs.usable() == true)
  350. FoundStrongHashSum = true;
  351. }
  352. }
  353. if(FoundHashSum == false)
  354. {
  355. if (ErrorText != NULL)
  356. strprintf(*ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
  357. return false;
  358. }
  359. if(FoundStrongHashSum == false)
  360. {
  361. if (ErrorText != NULL)
  362. strprintf(*ErrorText, _("No Hash entry in Release file %s, which is considered strong enough for security purposes"), Filename.c_str());
  363. return false;
  364. }
  365. std::string const StrDate = Section.FindS("Date");
  366. if (RFC1123StrToTime(StrDate.c_str(), Date) == false)
  367. {
  368. if (ErrorText != NULL)
  369. strprintf(*ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
  370. return false;
  371. }
  372. bool CheckValidUntil = _config->FindB("Acquire::Check-Valid-Until", true);
  373. if (d->CheckValidUntil == metaIndex::TRI_NO)
  374. CheckValidUntil = false;
  375. else if (d->CheckValidUntil == metaIndex::TRI_YES)
  376. CheckValidUntil = true;
  377. if (CheckValidUntil == true)
  378. {
  379. std::string const Label = Section.FindS("Label");
  380. std::string const StrValidUntil = Section.FindS("Valid-Until");
  381. // if we have a Valid-Until header in the Release file, use it as default
  382. if (StrValidUntil.empty() == false)
  383. {
  384. if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
  385. {
  386. if (ErrorText != NULL)
  387. strprintf(*ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
  388. return false;
  389. }
  390. }
  391. // get the user settings for this archive and use what expires earlier
  392. time_t MaxAge = d->ValidUntilMax;
  393. if (MaxAge == 0)
  394. {
  395. MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
  396. if (Label.empty() == false)
  397. MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
  398. }
  399. time_t MinAge = d->ValidUntilMin;
  400. if (MinAge == 0)
  401. {
  402. MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
  403. if (Label.empty() == false)
  404. MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
  405. }
  406. if (MinAge != 0 && ValidUntil != 0) {
  407. time_t const min_date = Date + MinAge;
  408. if (ValidUntil < min_date)
  409. ValidUntil = min_date;
  410. }
  411. if (MaxAge != 0) {
  412. time_t const max_date = Date + MaxAge;
  413. if (ValidUntil == 0 || ValidUntil > max_date)
  414. ValidUntil = max_date;
  415. }
  416. }
  417. LoadedSuccessfully = TRI_YES;
  418. return true;
  419. }
  420. /*}}}*/
  421. metaIndex * debReleaseIndex::UnloadedClone() const /*{{{*/
  422. {
  423. if (Trusted == TRI_NO)
  424. return new debReleaseIndex(URI, Dist, false);
  425. else if (Trusted == TRI_YES)
  426. return new debReleaseIndex(URI, Dist, true);
  427. else
  428. return new debReleaseIndex(URI, Dist);
  429. }
  430. /*}}}*/
  431. bool debReleaseIndex::parseSumData(const char *&Start, const char *End, /*{{{*/
  432. std::string &Name, std::string &Hash, unsigned long long &Size)
  433. {
  434. Name = "";
  435. Hash = "";
  436. Size = 0;
  437. /* Skip over the first blank */
  438. while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
  439. && Start < End)
  440. Start++;
  441. if (Start >= End)
  442. return false;
  443. /* Move EntryEnd to the end of the first entry (the hash) */
  444. const char *EntryEnd = Start;
  445. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  446. && EntryEnd < End)
  447. EntryEnd++;
  448. if (EntryEnd == End)
  449. return false;
  450. Hash.append(Start, EntryEnd-Start);
  451. /* Skip over intermediate blanks */
  452. Start = EntryEnd;
  453. while (*Start == '\t' || *Start == ' ')
  454. Start++;
  455. if (Start >= End)
  456. return false;
  457. EntryEnd = Start;
  458. /* Find the end of the second entry (the size) */
  459. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  460. && EntryEnd < End)
  461. EntryEnd++;
  462. if (EntryEnd == End)
  463. return false;
  464. Size = strtoull (Start, NULL, 10);
  465. /* Skip over intermediate blanks */
  466. Start = EntryEnd;
  467. while (*Start == '\t' || *Start == ' ')
  468. Start++;
  469. if (Start >= End)
  470. return false;
  471. EntryEnd = Start;
  472. /* Find the end of the third entry (the filename) */
  473. while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
  474. *EntryEnd != '\n' && *EntryEnd != '\r')
  475. && EntryEnd < End)
  476. EntryEnd++;
  477. Name.append(Start, EntryEnd-Start);
  478. Start = EntryEnd; //prepare for the next round
  479. return true;
  480. }
  481. /*}}}*/
  482. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll)/*{{{*/
  483. {
  484. std::vector<IndexTarget> const targets = GetIndexTargets();
  485. #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, false, std::map<std::string,std::string>())
  486. pkgAcqMetaClearSig * const TransactionManager = new pkgAcqMetaClearSig(Owner,
  487. APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
  488. targets, this);
  489. #undef APT_TARGET
  490. // special case for --print-uris
  491. if (GetAll)
  492. for (auto const &Target: targets)
  493. new pkgAcqIndex(Owner, TransactionManager, Target);
  494. return true;
  495. }
  496. /*}}}*/
  497. // ReleaseIndex::Set* TriState options /*{{{*/
  498. bool debReleaseIndex::SetTrusted(TriState const pTrusted)
  499. {
  500. if (Trusted == TRI_UNSET)
  501. Trusted = pTrusted;
  502. else if (Trusted != pTrusted)
  503. // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite
  504. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
  505. return true;
  506. }
  507. bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil)
  508. {
  509. if (d->CheckValidUntil == TRI_UNSET)
  510. d->CheckValidUntil = pCheckValidUntil;
  511. else if (d->CheckValidUntil != pCheckValidUntil)
  512. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str());
  513. return true;
  514. }
  515. bool debReleaseIndex::SetValidUntilMin(time_t const Valid)
  516. {
  517. if (d->ValidUntilMin == 0)
  518. d->ValidUntilMin = Valid;
  519. else if (d->ValidUntilMin != Valid)
  520. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str());
  521. return true;
  522. }
  523. bool debReleaseIndex::SetValidUntilMax(time_t const Valid)
  524. {
  525. if (d->ValidUntilMax == 0)
  526. d->ValidUntilMax = Valid;
  527. else if (d->ValidUntilMax != Valid)
  528. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str());
  529. return true;
  530. }
  531. bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy)
  532. {
  533. if (SignedBy.empty() == true && pSignedBy.empty() == false)
  534. {
  535. if (pSignedBy[0] == '/') // no check for existence as we could be chrooting later or such things
  536. ; // absolute path to a keyring file
  537. else
  538. {
  539. // we could go all fancy and allow short/long/string matches as gpgv/apt-key does,
  540. // but fingerprints are harder to fake than the others and this option is set once,
  541. // not interactively all the time so easy to type is not really a concern.
  542. std::string finger = pSignedBy;
  543. finger.erase(std::remove(finger.begin(), finger.end(), ' '), finger.end());
  544. std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper);
  545. if (finger.length() != 40 || finger.find_first_not_of("0123456789ABCDEF") != std::string::npos)
  546. return _error->Error(_("Invalid value set for option %s regarding source %s %s (%s)"), "Signed-By", URI.c_str(), Dist.c_str(), "not a fingerprint");
  547. }
  548. SignedBy = pSignedBy;
  549. }
  550. else if (SignedBy != pSignedBy)
  551. return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Signed-By", URI.c_str(), Dist.c_str());
  552. return true;
  553. }
  554. /*}}}*/
  555. // ReleaseIndex::IsTrusted /*{{{*/
  556. bool debReleaseIndex::IsTrusted() const
  557. {
  558. if (Trusted == TRI_YES)
  559. return true;
  560. else if (Trusted == TRI_NO)
  561. return false;
  562. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  563. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  564. return true;
  565. if (FileExists(MetaIndexFile("Release.gpg")))
  566. return true;
  567. return FileExists(MetaIndexFile("InRelease"));
  568. }
  569. /*}}}*/
  570. bool debReleaseIndex::IsArchitectureSupported(std::string const &arch) const/*{{{*/
  571. {
  572. if (d->Architectures.empty())
  573. return true;
  574. return std::find(d->Architectures.begin(), d->Architectures.end(), arch) != d->Architectures.end();
  575. }
  576. /*}}}*/
  577. bool debReleaseIndex::IsArchitectureAllSupportedFor(IndexTarget const &target) const/*{{{*/
  578. {
  579. if (d->NoSupportForAll.empty())
  580. return true;
  581. return std::find(d->NoSupportForAll.begin(), d->NoSupportForAll.end(), target.Option(IndexTarget::CREATED_BY)) == d->NoSupportForAll.end();
  582. }
  583. /*}}}*/
  584. std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() /*{{{*/
  585. {
  586. if (Indexes != NULL)
  587. return Indexes;
  588. Indexes = new std::vector<pkgIndexFile*>();
  589. bool const istrusted = IsTrusted();
  590. for (auto const &T: GetIndexTargets())
  591. {
  592. std::string const TargetName = T.Option(IndexTarget::CREATED_BY);
  593. if (TargetName == "Packages")
  594. Indexes->push_back(new debPackagesIndex(T, istrusted));
  595. else if (TargetName == "Sources")
  596. Indexes->push_back(new debSourcesIndex(T, istrusted));
  597. else if (TargetName == "Translations")
  598. Indexes->push_back(new debTranslationsIndex(T));
  599. }
  600. return Indexes;
  601. }
  602. /*}}}*/
  603. static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)/*{{{*/
  604. {
  605. ReleaseFile = That->MetaIndexFile("InRelease");
  606. bool releaseExists = false;
  607. if (FileExists(ReleaseFile) == true)
  608. releaseExists = true;
  609. else
  610. {
  611. ReleaseFile = That->MetaIndexFile("Release");
  612. if (FileExists(ReleaseFile))
  613. releaseExists = true;
  614. }
  615. return releaseExists;
  616. }
  617. /*}}}*/
  618. bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/
  619. {
  620. std::string ReleaseFile;
  621. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  622. ::URI Tmp(URI);
  623. if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false)
  624. return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str());
  625. if (releaseExists == false)
  626. return true;
  627. FileFd Rel;
  628. // Beware: The 'Release' file might be clearsigned in case the
  629. // signature for an 'InRelease' file couldn't be checked
  630. if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
  631. return false;
  632. // Store the IMS information
  633. pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
  634. pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
  635. // Rel can't be used as this is potentially a temporary file
  636. struct stat Buf;
  637. if (stat(ReleaseFile.c_str(), &Buf) != 0)
  638. return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
  639. File->Size = Buf.st_size;
  640. File->mtime = Buf.st_mtime;
  641. pkgTagFile TagFile(&Rel, Rel.Size());
  642. pkgTagSection Section;
  643. if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false)
  644. return false;
  645. std::string data;
  646. #define APT_INRELEASE(TYPE, TAG, STORE) \
  647. data = Section.FindS(TAG); \
  648. if (data.empty() == false) \
  649. { \
  650. map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
  651. if (storage == 0) return false; \
  652. STORE = storage; \
  653. }
  654. APT_INRELEASE(MIXED, "Suite", File->Archive)
  655. APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
  656. APT_INRELEASE(MIXED, "Origin", File->Origin)
  657. APT_INRELEASE(MIXED, "Codename", File->Codename)
  658. APT_INRELEASE(MIXED, "Label", File->Label)
  659. #undef APT_INRELEASE
  660. Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
  661. Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
  662. return true;
  663. }
  664. /*}}}*/
  665. // ReleaseIndex::FindInCache - Find this index /*{{{*/
  666. pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const
  667. {
  668. std::string ReleaseFile;
  669. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  670. pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
  671. for (; File.end() == false; ++File)
  672. {
  673. if (File->FileName == 0 || ReleaseFile != File.FileName())
  674. continue;
  675. // empty means the file does not exist by "design"
  676. if (ModifyCheck == false || (releaseExists == false && File->Size == 0))
  677. return File;
  678. struct stat St;
  679. if (stat(File.FileName(),&St) != 0)
  680. {
  681. if (_config->FindB("Debug::pkgCacheGen", false))
  682. std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
  683. return pkgCache::RlsFileIterator(Cache);
  684. }
  685. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  686. {
  687. if (_config->FindB("Debug::pkgCacheGen", false))
  688. std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
  689. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  690. << ") doesn't match for " << File.FileName() << std::endl;
  691. return pkgCache::RlsFileIterator(Cache);
  692. }
  693. return File;
  694. }
  695. return File;
  696. }
  697. /*}}}*/
  698. static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, /*{{{*/
  699. std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
  700. {
  701. std::map<std::string, std::string>::const_iterator val = Options.find(Name);
  702. std::vector<std::string> Values;
  703. if (val != Options.end())
  704. Values = VectorizeString(val->second, ',');
  705. else
  706. Values = defaultValues;
  707. // all is a very special architecture users shouldn't be concerned with explicitly
  708. if (Name == "arch" && std::find(Values.begin(), Values.end(), "all") == Values.end())
  709. Values.push_back("all");
  710. if ((val = Options.find(Name + "+")) != Options.end())
  711. {
  712. std::vector<std::string> const plus = VectorizeString(val->second, ',');
  713. std::copy_if(plus.begin(), plus.end(), std::back_inserter(Values), [&Values](std::string const &v) {
  714. return std::find(Values.begin(), Values.end(), v) == Values.end();
  715. });
  716. }
  717. if ((val = Options.find(Name + "-")) != Options.end())
  718. {
  719. std::vector<std::string> const minus = VectorizeString(val->second, ',');
  720. Values.erase(std::remove_if(Values.begin(), Values.end(), [&minus](std::string const &v) {
  721. return std::find(minus.begin(), minus.end(), v) != minus.end();
  722. }), Values.end());
  723. }
  724. return Values;
  725. }
  726. /*}}}*/
  727. class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/
  728. {
  729. metaIndex::TriState GetTriStateOption(std::map<std::string, std::string>const &Options, char const * const name) const
  730. {
  731. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  732. if (opt != Options.end())
  733. return StringToBool(opt->second, false) ? metaIndex::TRI_YES : metaIndex::TRI_NO;
  734. return metaIndex::TRI_DONTCARE;
  735. }
  736. time_t GetTimeOption(std::map<std::string, std::string>const &Options, char const * const name) const
  737. {
  738. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  739. if (opt == Options.end())
  740. return 0;
  741. return strtoull(opt->second.c_str(), NULL, 10);
  742. }
  743. protected:
  744. bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI,
  745. std::string const &Dist, std::string const &Section,
  746. bool const &IsSrc, std::map<std::string, std::string> const &Options) const
  747. {
  748. debReleaseIndex *Deb = NULL;
  749. for (std::vector<metaIndex *>::const_iterator I = List.begin();
  750. I != List.end(); ++I)
  751. {
  752. // We only worry about debian entries here
  753. if (strcmp((*I)->GetType(), "deb") != 0)
  754. continue;
  755. /* This check insures that there will be only one Release file
  756. queued for all the Packages files and Sources files it
  757. corresponds to. */
  758. if ((*I)->GetURI() == URI && (*I)->GetDist() == Dist)
  759. {
  760. Deb = dynamic_cast<debReleaseIndex*>(*I);
  761. if (Deb != NULL)
  762. break;
  763. }
  764. }
  765. // No currently created Release file indexes this entry, so we create a new one.
  766. if (Deb == NULL)
  767. {
  768. Deb = new debReleaseIndex(URI, Dist);
  769. List.push_back(Deb);
  770. }
  771. std::vector<std::string> const alltargets = _config->FindVector(std::string("Acquire::IndexTargets::") + Name, "", true);
  772. std::vector<std::string> deftargets;
  773. deftargets.reserve(alltargets.size());
  774. std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(deftargets), [&](std::string const &t) {
  775. std::string c = "Acquire::IndexTargets::";
  776. c.append(Name).append("::").append(t).append("::DefaultEnabled");
  777. return _config->FindB(c, true);
  778. });
  779. std::vector<std::string> mytargets = parsePlusMinusOptions("target", Options, deftargets);
  780. for (auto const &target : alltargets)
  781. {
  782. std::map<std::string, std::string>::const_iterator const opt = Options.find(target);
  783. if (opt == Options.end())
  784. continue;
  785. auto const tarItr = std::find(mytargets.begin(), mytargets.end(), target);
  786. bool const optValue = StringToBool(opt->second);
  787. if (optValue == true && tarItr == mytargets.end())
  788. mytargets.push_back(target);
  789. else if (optValue == false && tarItr != mytargets.end())
  790. mytargets.erase(std::remove(mytargets.begin(), mytargets.end(), target), mytargets.end());
  791. }
  792. bool UsePDiffs = _config->FindB("Acquire::PDiffs", true);
  793. {
  794. std::map<std::string, std::string>::const_iterator const opt = Options.find("pdiffs");
  795. if (opt != Options.end())
  796. UsePDiffs = StringToBool(opt->second);
  797. }
  798. std::string UseByHash = _config->Find("APT::Acquire::By-Hash", "yes");
  799. UseByHash = _config->Find("Acquire::By-Hash", UseByHash);
  800. {
  801. std::string const host = ::URI(URI).Host;
  802. UseByHash = _config->Find("APT::Acquire::" + host + "::By-Hash", UseByHash);
  803. UseByHash = _config->Find("Acquire::" + host + "::By-Hash", UseByHash);
  804. std::map<std::string, std::string>::const_iterator const opt = Options.find("by-hash");
  805. if (opt != Options.end())
  806. UseByHash = opt->second;
  807. }
  808. auto const entry = Options.find("sourceslist-entry");
  809. Deb->AddComponent(
  810. entry->second,
  811. IsSrc,
  812. Section,
  813. mytargets,
  814. parsePlusMinusOptions("arch", Options, APT::Configuration::getArchitectures()),
  815. parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)),
  816. UsePDiffs,
  817. UseByHash
  818. );
  819. if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false ||
  820. Deb->SetCheckValidUntil(GetTriStateOption(Options, "check-valid-until")) == false ||
  821. Deb->SetValidUntilMax(GetTimeOption(Options, "valid-until-max")) == false ||
  822. Deb->SetValidUntilMin(GetTimeOption(Options, "valid-until-min")) == false)
  823. return false;
  824. std::map<std::string, std::string>::const_iterator const signedby = Options.find("signed-by");
  825. if (signedby == Options.end())
  826. {
  827. if (Deb->SetSignedBy("") == false)
  828. return false;
  829. }
  830. else
  831. {
  832. if (Deb->SetSignedBy(signedby->second) == false)
  833. return false;
  834. }
  835. return true;
  836. }
  837. debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label)
  838. {
  839. }
  840. };
  841. /*}}}*/
  842. class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/
  843. {
  844. public:
  845. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  846. std::string const &Dist, std::string const &Section,
  847. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  848. {
  849. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  850. }
  851. debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree")
  852. {
  853. }
  854. };
  855. /*}}}*/
  856. class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian /*{{{*/
  857. {
  858. public:
  859. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  860. std::string const &Dist, std::string const &Section,
  861. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  862. {
  863. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  864. }
  865. debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree")
  866. {
  867. }
  868. };
  869. /*}}}*/
  870. APT_HIDDEN debSLTypeDeb _apt_DebType;
  871. APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;