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