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