]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/indexrecords.cc
rework hashsum verification in the acquire system
[apt.git] / apt-pkg / indexrecords.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: indexrecords.cc,v 1.1.2.4 2003/12/30 02:11:43 mdz Exp $
4 /*}}}*/
5// Include Files /*{{{*/
6#include<config.h>
7
8#include <apt-pkg/indexrecords.h>
9#include <apt-pkg/tagfile.h>
10#include <apt-pkg/error.h>
11#include <apt-pkg/strutl.h>
12#include <apt-pkg/configuration.h>
13#include <apt-pkg/fileutl.h>
14#include <apt-pkg/hashes.h>
15#include <apt-pkg/gpgv.h>
16
17#include <stdlib.h>
18#include <time.h>
19#include <clocale>
20#include <map>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include <apti18n.h>
26 /*}}}*/
27
28using std::string;
29
30APT_PURE string indexRecords::GetDist() const
31{
32 return this->Dist;
33}
34
35APT_PURE string indexRecords::GetSuite() const
36{
37 return this->Suite;
38}
39
40APT_PURE bool indexRecords::GetSupportsAcquireByHash() const
41{
42 return this->SupportsAcquireByHash;
43}
44
45APT_PURE bool indexRecords::CheckDist(const string MaybeDist) const
46{
47 return (this->Dist == MaybeDist
48 || this->Suite == MaybeDist);
49}
50
51APT_PURE string indexRecords::GetExpectedDist() const
52{
53 return this->ExpectedDist;
54}
55
56APT_PURE time_t indexRecords::GetValidUntil() const
57{
58 return this->ValidUntil;
59}
60
61APT_PURE time_t indexRecords::GetDate() const
62{
63 return this->Date;
64}
65
66APT_PURE indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
67{
68 std::map<std::string, indexRecords::checkSum* >::const_iterator sum = Entries.find(MetaKey);
69 if (sum == Entries.end())
70 return NULL;
71 return sum->second;
72}
73
74APT_PURE bool indexRecords::Exists(string const &MetaKey) const
75{
76 return Entries.find(MetaKey) != Entries.end();
77}
78
79bool indexRecords::Load(const string Filename) /*{{{*/
80{
81 FileFd Fd;
82 if (OpenMaybeClearSignedFile(Filename, Fd) == false)
83 return false;
84
85 pkgTagFile TagFile(&Fd, Fd.Size());
86 if (_error->PendingError() == true)
87 {
88 strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
89 return false;
90 }
91
92 pkgTagSection Section;
93 const char *Start, *End;
94 if (TagFile.Step(Section) == false)
95 {
96 strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
97 return false;
98 }
99 // FIXME: find better tag name
100 SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false);
101
102 Suite = Section.FindS("Suite");
103 Dist = Section.FindS("Codename");
104
105 bool FoundHashSum = false;
106 for (int i=0;HashString::SupportedHashes()[i] != NULL; i++)
107 {
108 if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
109 continue;
110
111 string Name;
112 string Hash;
113 unsigned long long Size;
114 while (Start < End)
115 {
116 if (!parseSumData(Start, End, Name, Hash, Size))
117 return false;
118
119 if (Entries.find(Name) == Entries.end())
120 {
121 indexRecords::checkSum *Sum = new indexRecords::checkSum;
122 Sum->MetaKeyFilename = Name;
123 Sum->Size = Size;
124 std::string SizeStr;
125 strprintf(SizeStr, "%llu", Size);
126 Sum->Hashes.push_back(HashString("Checksum-FileSize", SizeStr));
127 APT_IGNORE_DEPRECATED(Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);)
128 Entries[Name] = Sum;
129 }
130 Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
131 FoundHashSum = true;
132 }
133 }
134
135 if(FoundHashSum == false)
136 {
137 strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
138 return false;
139 }
140
141 string const StrDate = Section.FindS("Date");
142 if (RFC1123StrToTime(StrDate.c_str(), Date) == false)
143 {
144 strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
145 return false;
146 }
147
148 string const Label = Section.FindS("Label");
149 string const StrValidUntil = Section.FindS("Valid-Until");
150
151 // if we have a Valid-Until header in the Release file, use it as default
152 if (StrValidUntil.empty() == false)
153 {
154 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
155 {
156 strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
157 return false;
158 }
159 }
160 // get the user settings for this archive and use what expires earlier
161 int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
162 if (Label.empty() == false)
163 MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
164 int MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
165 if (Label.empty() == false)
166 MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
167
168 if(MaxAge == 0 &&
169 (MinAge == 0 || ValidUntil == 0)) // No user settings, use the one from the Release file
170 return true;
171
172 if (MinAge != 0 && ValidUntil != 0) {
173 time_t const min_date = Date + MinAge;
174 if (ValidUntil < min_date)
175 ValidUntil = min_date;
176 }
177 if (MaxAge != 0) {
178 time_t const max_date = Date + MaxAge;
179 if (ValidUntil == 0 || ValidUntil > max_date)
180 ValidUntil = max_date;
181 }
182
183 return true;
184}
185 /*}}}*/
186std::vector<string> indexRecords::MetaKeys() /*{{{*/
187{
188 std::vector<std::string> keys;
189 std::map<string,checkSum *>::iterator I = Entries.begin();
190 while(I != Entries.end()) {
191 keys.push_back((*I).first);
192 ++I;
193 }
194 return keys;
195}
196 /*}}}*/
197bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
198 string &Name, string &Hash, unsigned long long &Size)
199{
200 Name = "";
201 Hash = "";
202 Size = 0;
203 /* Skip over the first blank */
204 while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
205 && Start < End)
206 Start++;
207 if (Start >= End)
208 return false;
209
210 /* Move EntryEnd to the end of the first entry (the hash) */
211 const char *EntryEnd = Start;
212 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
213 && EntryEnd < End)
214 EntryEnd++;
215 if (EntryEnd == End)
216 return false;
217
218 Hash.append(Start, EntryEnd-Start);
219
220 /* Skip over intermediate blanks */
221 Start = EntryEnd;
222 while (*Start == '\t' || *Start == ' ')
223 Start++;
224 if (Start >= End)
225 return false;
226
227 EntryEnd = Start;
228 /* Find the end of the second entry (the size) */
229 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
230 && EntryEnd < End)
231 EntryEnd++;
232 if (EntryEnd == End)
233 return false;
234
235 Size = strtoull (Start, NULL, 10);
236
237 /* Skip over intermediate blanks */
238 Start = EntryEnd;
239 while (*Start == '\t' || *Start == ' ')
240 Start++;
241 if (Start >= End)
242 return false;
243
244 EntryEnd = Start;
245 /* Find the end of the third entry (the filename) */
246 while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
247 *EntryEnd != '\n' && *EntryEnd != '\r')
248 && EntryEnd < End)
249 EntryEnd++;
250
251 Name.append(Start, EntryEnd-Start);
252 Start = EntryEnd; //prepare for the next round
253 return true;
254}
255 /*}}}*/
256
257APT_PURE bool indexRecords::IsAlwaysTrusted() const
258{
259 if (Trusted == ALWAYS_TRUSTED)
260 return true;
261 return false;
262}
263APT_PURE bool indexRecords::IsNeverTrusted() const
264{
265 if (Trusted == NEVER_TRUSTED)
266 return true;
267 return false;
268}
269void indexRecords::SetTrusted(bool const Trusted)
270{
271 if (Trusted == true)
272 this->Trusted = ALWAYS_TRUSTED;
273 else
274 this->Trusted = NEVER_TRUSTED;
275}
276
277#if APT_PKG_ABI >= 413
278indexRecords::indexRecords(const string &ExpectedDist) :
279 Trusted(CHECK_TRUST), d(NULL), ExpectedDist(ExpectedDist), ValidUntil(0),
280 SupportsAcquireByHash(false)
281{
282}
283#else
284indexRecords::indexRecords() :
285 Trusted(CHECK_TRUST), d(NULL), ExpectedDist(""), ValidUntil(0),
286 SupportsAcquireByHash(false)
287{
288}
289indexRecords::indexRecords(const string ExpectedDist) :
290 Trusted(CHECK_TRUST), d(NULL), ExpectedDist(ExpectedDist), ValidUntil(0),
291 SupportsAcquireByHash(false)
292{
293}
294#endif
295
296indexRecords::~indexRecords() {}