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