]> git.saurik.com Git - apt.git/blame - ftparchive/cachedb.cc
Make the weak signature message less ambigious
[apt.git] / ftparchive / cachedb.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
c9569a1e 3// $Id: cachedb.cc,v 1.7 2004/05/08 19:41:01 mdz Exp $
b2e465d6
AL
4/* ######################################################################
5
6 CacheDB
7
8 Simple uniform interface to a cache database.
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
ea542140 13#include <config.h>
b2e465d6
AL
14
15#include <apt-pkg/error.h>
16#include <apt-pkg/md5.h>
cde41ae8 17#include <apt-pkg/sha1.h>
84a0890e 18#include <apt-pkg/sha2.h>
b2e465d6
AL
19#include <apt-pkg/strutl.h>
20#include <apt-pkg/configuration.h>
472ff00e 21#include <apt-pkg/fileutl.h>
453b82a3 22#include <apt-pkg/debfile.h>
ce928105 23#include <apt-pkg/gpgv.h>
a311fb96 24#include <apt-pkg/hashes.h>
a00a9b44 25
b2e465d6 26#include <netinet/in.h> // htonl, etc
453b82a3
DK
27#include <ctype.h>
28#include <stddef.h>
29#include <sys/stat.h>
a311fb96 30#include <strings.h>
ea542140 31
ea542140 32#include "cachedb.h"
a00a9b44
DK
33
34#include <apti18n.h>
b2e465d6
AL
35 /*}}}*/
36
bf3ad91f 37CacheDB::CacheDB(std::string const &DB)
21ea1dbb
MV
38 : Dbp(0), Fd(NULL), DebFile(0)
39{
40 TmpKey[0]='\0';
41 ReadyDB(DB);
bf3ad91f 42}
21ea1dbb
MV
43
44CacheDB::~CacheDB()
45{
46 ReadyDB();
47 delete DebFile;
3d8232bf 48 CloseFile();
bf3ad91f 49}
21ea1dbb 50
b2e465d6
AL
51// CacheDB::ReadyDB - Ready the DB2 /*{{{*/
52// ---------------------------------------------------------------------
53/* This opens the DB2 file for caching package information */
8f3ba4e8 54bool CacheDB::ReadyDB(std::string const &DB)
b2e465d6 55{
c9569a1e
AL
56 int err;
57
b2e465d6
AL
58 ReadOnly = _config->FindB("APT::FTPArchive::ReadOnlyDB",false);
59
60 // Close the old DB
61 if (Dbp != 0)
62 Dbp->close(Dbp,0);
63
64 /* Check if the DB was disabled while running and deal with a
65 corrupted DB */
66 if (DBFailed() == true)
67 {
dc738e7a 68 _error->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile.c_str());
b2e465d6
AL
69 rename(DBFile.c_str(),(DBFile+".old").c_str());
70 }
71
72 DBLoaded = false;
73 Dbp = 0;
8f3ba4e8 74 DBFile = std::string();
b2e465d6
AL
75
76 if (DB.empty())
77 return true;
c9569a1e
AL
78
79 db_create(&Dbp, NULL, 0);
cde41ae8 80 if ((err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_BTREE,
b2e465d6 81 (ReadOnly?DB_RDONLY:DB_CREATE),
c9569a1e 82 0644)) != 0)
b2e465d6 83 {
c9569a1e
AL
84 if (err == DB_OLD_VERSION)
85 {
86 _error->Warning(_("DB is old, attempting to upgrade %s"),DBFile.c_str());
87 err = Dbp->upgrade(Dbp, DB.c_str(), 0);
88 if (!err)
89 err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_HASH,
90 (ReadOnly?DB_RDONLY:DB_CREATE), 0644);
91
92 }
eb2bc4f2
MV
93 // the database format has changed from DB_HASH to DB_BTREE in
94 // apt 0.6.44
95 if (err == EINVAL)
96 {
c6474fb6 97 _error->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
eb2bc4f2 98 }
c9569a1e
AL
99 if (err)
100 {
101 Dbp = 0;
102 return _error->Error(_("Unable to open DB file %s: %s"),DB.c_str(), db_strerror(err));
103 }
b2e465d6 104 }
243b2a38 105
b2e465d6
AL
106 DBFile = DB;
107 DBLoaded = true;
108 return true;
109}
110 /*}}}*/
c6474fb6 111// CacheDB::OpenFile - Open the file /*{{{*/
b2e465d6 112// ---------------------------------------------------------------------
cde41ae8
MV
113/* */
114bool CacheDB::OpenFile()
115{
acea28d0
MV
116 // always close existing file first
117 CloseFile();
215b0faf
MV
118
119 // open a new file
120 Fd = new FileFd(FileName,FileFd::ReadOnly);
121 if (_error->PendingError() == true)
122 {
123 CloseFile();
124 return false;
125 }
126 return true;
cde41ae8
MV
127}
128 /*}}}*/
215b0faf 129// CacheDB::CloseFile - Close the file /*{{{*/
ce928105
MV
130void CacheDB::CloseFile()
131{
215b0faf
MV
132 if(Fd != NULL)
133 {
134 delete Fd;
135 Fd = NULL;
136 }
ce928105 137}
215b0faf
MV
138 /*}}}*/
139// CacheDB::OpenDebFile - Open a debfile /*{{{*/
ce928105
MV
140bool CacheDB::OpenDebFile()
141{
acea28d0
MV
142 // always close existing file first
143 CloseDebFile();
215b0faf
MV
144
145 // first open the fd, then pass it to the debDebFile
146 if(OpenFile() == false)
147 return false;
ce928105
MV
148 DebFile = new debDebFile(*Fd);
149 if (_error->PendingError() == true)
150 return false;
151 return true;
152}
215b0faf
MV
153 /*}}}*/
154// CacheDB::CloseDebFile - Close a debfile again /*{{{*/
ce928105
MV
155void CacheDB::CloseDebFile()
156{
215b0faf 157 CloseFile();
ce928105 158
215b0faf
MV
159 if(DebFile != NULL)
160 {
161 delete DebFile;
162 DebFile = NULL;
163 }
164}
165 /*}}}*/
cde41ae8
MV
166// CacheDB::GetFileStat - Get stats from the file /*{{{*/
167// ---------------------------------------------------------------------
168/* This gets the size from the database if it's there. If we need
169 * to look at the file, also get the mtime from the file. */
ff574e76 170bool CacheDB::GetFileStat(bool const &doStat)
cde41ae8 171{
215b0faf
MV
172 if ((CurStat.Flags & FlSize) == FlSize && doStat == false)
173 return true;
174
175 /* Get it from the file. */
176 if (OpenFile() == false)
177 return false;
178
179 // Stat the file
180 struct stat St;
181 if (fstat(Fd->Fd(),&St) != 0)
182 {
183 CloseFile();
184 return _error->Errno("fstat",
185 _("Failed to stat %s"),FileName.c_str());
186 }
187 CurStat.FileSize = St.st_size;
188 CurStat.mtime = htonl(St.st_mtime);
189 CurStat.Flags |= FlSize;
190
243b2a38
MV
191 return true;
192}
193 /*}}}*/
194// CacheDB::GetCurStatCompatOldFormat /*{{{*/
195// ---------------------------------------------------------------------
196/* Read the old (32bit FileSize) StateStore format from disk */
197bool CacheDB::GetCurStatCompatOldFormat()
198{
199 InitQueryStats();
200 Data.data = &CurStatOldFormat;
201 Data.flags = DB_DBT_USERMEM;
202 Data.ulen = sizeof(CurStatOldFormat);
203 if (Get() == false)
204 {
205 CurStat.Flags = 0;
206 } else {
207 CurStat.Flags = CurStatOldFormat.Flags;
208 CurStat.mtime = CurStatOldFormat.mtime;
209 CurStat.FileSize = CurStatOldFormat.FileSize;
210 memcpy(CurStat.MD5, CurStatOldFormat.MD5, sizeof(CurStat.MD5));
211 memcpy(CurStat.SHA1, CurStatOldFormat.SHA1, sizeof(CurStat.SHA1));
212 memcpy(CurStat.SHA256, CurStatOldFormat.SHA256, sizeof(CurStat.SHA256));
213 }
214 return true;
215}
216 /*}}}*/
217// CacheDB::GetCurStatCompatOldFormat /*{{{*/
218// ---------------------------------------------------------------------
219/* Read the new (64bit FileSize) StateStore format from disk */
220bool CacheDB::GetCurStatCompatNewFormat()
221{
222 InitQueryStats();
223 Data.data = &CurStat;
224 Data.flags = DB_DBT_USERMEM;
225 Data.ulen = sizeof(CurStat);
226 if (Get() == false)
227 {
228 CurStat.Flags = 0;
229 }
215b0faf 230 return true;
cde41ae8
MV
231}
232 /*}}}*/
233// CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
234// ---------------------------------------------------------------------
235/* Sets the CurStat variable. Either to 0 if no database is used
236 * or to the value in the database if one is used */
237bool CacheDB::GetCurStat()
b2e465d6 238{
b2e465d6
AL
239 memset(&CurStat,0,sizeof(CurStat));
240
215b0faf
MV
241 if (DBLoaded)
242 {
243b2a38 243 // do a first query to just get the size of the data on disk
37497bd5 244 InitQueryStats();
215b0faf 245 Data.data = &CurStat;
215b0faf 246 Data.flags = DB_DBT_USERMEM;
243b2a38
MV
247 Data.ulen = 0;
248 Get();
249
250 if (Data.size == 0)
b2e465d6 251 {
243b2a38
MV
252 // nothing needs to be done, we just have not data for this deb
253 }
254 // check if the record is written in the old format (32bit filesize)
255 else if(Data.size == sizeof(CurStatOldFormat))
256 {
257 GetCurStatCompatOldFormat();
258 }
259 else if(Data.size == sizeof(CurStat))
260 {
261 GetCurStatCompatNewFormat();
262 } else {
263 return _error->Error("Cache record size mismatch (%ul)", Data.size);
264 }
265
215b0faf
MV
266 CurStat.Flags = ntohl(CurStat.Flags);
267 CurStat.FileSize = ntohl(CurStat.FileSize);
b2e465d6 268 }
215b0faf 269 return true;
cde41ae8
MV
270}
271 /*}}}*/
272// CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
273// ---------------------------------------------------------------------
a311fb96
DK
274bool CacheDB::GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents,
275 bool const &GenContentsOnly, bool const DoSource, unsigned int const DoHashes,
9a961efc 276 bool const &checkMtime)
cde41ae8 277{
ce928105 278 this->FileName = FileName;
cde41ae8 279
ce928105 280 if (GetCurStat() == false)
ce928105 281 return false;
b2e465d6 282 OldStat = CurStat;
ff574e76 283
ce928105 284 if (GetFileStat(checkMtime) == false)
a311fb96 285 return false;
cde41ae8 286
215b0faf
MV
287 /* if mtime changed, update CurStat from disk */
288 if (checkMtime == true && OldStat.mtime != CurStat.mtime)
289 CurStat.Flags = FlSize;
290
291 Stats.Bytes += CurStat.FileSize;
a311fb96 292 ++Stats.Packages;
215b0faf
MV
293
294 if ((DoControl && LoadControl() == false)
a311fb96
DK
295 || (DoContents && LoadContents(GenContentsOnly) == false)
296 || (DoSource && LoadSource() == false)
297 || (DoHashes != 0 && GetHashes(false, DoHashes) == false)
298 )
215b0faf 299 {
a311fb96 300 return false;
215b0faf 301 }
a311fb96
DK
302
303 return true;
ce928105
MV
304}
305 /*}}}*/
a311fb96 306bool CacheDB::LoadSource() /*{{{*/
ce928105
MV
307{
308 // Try to read the control information out of the DB.
309 if ((CurStat.Flags & FlSource) == FlSource)
310 {
311 // Lookup the control information
37497bd5 312 InitQuerySource();
ce928105 313 if (Get() == true && Dsc.TakeDsc(Data.data, Data.size) == true)
53ba4e2c 314 {
ce928105 315 return true;
53ba4e2c 316 }
ce928105
MV
317 CurStat.Flags &= ~FlSource;
318 }
215b0faf 319 if (OpenFile() == false)
ce928105 320 return false;
ce928105 321
ce928105
MV
322 Stats.Misses++;
323 if (Dsc.Read(FileName) == false)
324 return false;
cde41ae8 325
31be38d2 326 if (Dsc.Length == 0)
ce928105 327 return _error->Error(_("Failed to read .dsc"));
31be38d2 328
ce928105 329 // Write back the control information
37497bd5 330 InitQuerySource();
31be38d2 331 if (Put(Dsc.Data.c_str(), Dsc.Length) == true)
ce928105 332 CurStat.Flags |= FlSource;
cde41ae8 333
b2e465d6
AL
334 return true;
335}
a311fb96 336 /*}}}*/
b2e465d6
AL
337// CacheDB::LoadControl - Load Control information /*{{{*/
338// ---------------------------------------------------------------------
339/* */
340bool CacheDB::LoadControl()
341{
342 // Try to read the control information out of the DB.
343 if ((CurStat.Flags & FlControl) == FlControl)
344 {
345 // Lookup the control information
37497bd5 346 InitQueryControl();
b2e465d6
AL
347 if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
348 return true;
349 CurStat.Flags &= ~FlControl;
350 }
351
215b0faf 352 if(OpenDebFile() == false)
cde41ae8 353 return false;
b2e465d6
AL
354
355 Stats.Misses++;
356 if (Control.Read(*DebFile) == false)
357 return false;
358
359 if (Control.Control == 0)
dc738e7a 360 return _error->Error(_("Archive has no control record"));
b2e465d6
AL
361
362 // Write back the control information
37497bd5 363 InitQueryControl();
b2e465d6
AL
364 if (Put(Control.Control,Control.Length) == true)
365 CurStat.Flags |= FlControl;
366 return true;
367}
368 /*}}}*/
369// CacheDB::LoadContents - Load the File Listing /*{{{*/
370// ---------------------------------------------------------------------
371/* */
9209ec47 372bool CacheDB::LoadContents(bool const &GenOnly)
b2e465d6
AL
373{
374 // Try to read the control information out of the DB.
375 if ((CurStat.Flags & FlContents) == FlContents)
376 {
377 if (GenOnly == true)
378 return true;
379
380 // Lookup the contents information
37497bd5 381 InitQueryContent();
b2e465d6
AL
382 if (Get() == true)
383 {
384 if (Contents.TakeContents(Data.data,Data.size) == true)
385 return true;
386 }
387
388 CurStat.Flags &= ~FlContents;
389 }
390
215b0faf 391 if(OpenDebFile() == false)
cde41ae8 392 return false;
b2e465d6 393
0a3b93fc 394 Stats.Misses++;
b2e465d6
AL
395 if (Contents.Read(*DebFile) == false)
396 return false;
397
398 // Write back the control information
37497bd5 399 InitQueryContent();
b2e465d6
AL
400 if (Put(Contents.Data,Contents.CurSize) == true)
401 CurStat.Flags |= FlContents;
402 return true;
403}
404 /*}}}*/
a311fb96 405// CacheDB::GetHashes - Get the hashs /*{{{*/
8f3ba4e8 406static std::string bytes2hex(uint8_t *bytes, size_t length) {
76ef756a 407 char buf[3];
0fffbc8c 408 std::string space;
76ef756a
MV
409
410 space.reserve(length*2 + 1);
411 for (size_t i = 0; i < length; i++) {
412 snprintf(buf, sizeof(buf), "%02x", bytes[i]);
413 space.append(buf);
414 }
415 return space;
cde41ae8
MV
416}
417
9209ec47 418static inline unsigned char xdig2num(char const &dig) {
cde41ae8
MV
419 if (isdigit(dig)) return dig - '0';
420 if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
421 if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
422 return 0;
423}
424
425static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
426 while (length-- > 0) {
427 *bytes = 0;
428 if (isxdigit(hex[0]) && isxdigit(hex[1])) {
429 *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
430 hex += 2;
431 }
432 bytes++;
433 }
434}
a311fb96 435bool CacheDB::GetHashes(bool const GenOnly, unsigned int const DoHashes)
b2e465d6 436{
a311fb96
DK
437 unsigned int FlHashes = DoHashes & (Hashes::MD5SUM | Hashes::SHA1SUM | Hashes::SHA256SUM | Hashes::SHA512SUM);
438 HashesList.clear();
215b0faf 439
a311fb96 440 if (FlHashes != 0)
cde41ae8 441 {
a311fb96
DK
442 if (OpenFile() == false)
443 return false;
cde41ae8 444
9224ce3d
DK
445 Hashes hashes(FlHashes);
446 if (Fd->Seek(0) == false || hashes.AddFD(*Fd, CurStat.FileSize) == false)
a311fb96 447 return false;
215b0faf 448
a311fb96
DK
449 HashStringList hl = hashes.GetHashStringList();
450 for (HashStringList::const_iterator hs = hl.begin(); hs != hl.end(); ++hs)
451 {
452 HashesList.push_back(*hs);
453 if (strcasecmp(hs->HashType().c_str(), "SHA512") == 0)
454 {
455 Stats.SHA512Bytes += CurStat.FileSize;
456 hex2bytes(CurStat.SHA512, hs->HashValue().data(), sizeof(CurStat.SHA512));
457 CurStat.Flags |= FlSHA512;
458 }
459 else if (strcasecmp(hs->HashType().c_str(), "SHA256") == 0)
460 {
461 Stats.SHA256Bytes += CurStat.FileSize;
462 hex2bytes(CurStat.SHA256, hs->HashValue().data(), sizeof(CurStat.SHA256));
463 CurStat.Flags |= FlSHA256;
464 }
465 else if (strcasecmp(hs->HashType().c_str(), "SHA1") == 0)
466 {
467 Stats.SHA1Bytes += CurStat.FileSize;
468 hex2bytes(CurStat.SHA1, hs->HashValue().data(), sizeof(CurStat.SHA1));
469 CurStat.Flags |= FlSHA1;
470 }
471 else if (strcasecmp(hs->HashType().c_str(), "MD5Sum") == 0)
472 {
473 Stats.MD5Bytes += CurStat.FileSize;
474 hex2bytes(CurStat.MD5, hs->HashValue().data(), sizeof(CurStat.MD5));
475 CurStat.Flags |= FlMD5;
476 }
23397c9d
DK
477 else if (strcasecmp(hs->HashType().c_str(), "Checksum-FileSize") == 0)
478 {
479 // we store it in a different field already
480 }
a311fb96
DK
481 else
482 return _error->Error("Got unknown unrequested hashtype %s", hs->HashType().c_str());
483 }
cde41ae8 484 }
a311fb96 485 if (GenOnly == true)
9a961efc 486 return true;
215b0faf 487
d5a46051
DK
488 bool ret = true;
489#define PUSH_BACK_HASH(FLAG, TYPE, VALUE) \
490 if ((CurStat.Flags & FLAG) == FLAG) \
491 ret &= HashesList.push_back(HashString(TYPE, bytes2hex(VALUE, sizeof(VALUE))));
492 PUSH_BACK_HASH(FlMD5, "MD5Sum", CurStat.MD5);
493 PUSH_BACK_HASH(FlSHA1, "SHA1", CurStat.SHA1);
494 PUSH_BACK_HASH(FlSHA256, "SHA256", CurStat.SHA256);
495 PUSH_BACK_HASH(FlSHA512, "SHA512", CurStat.SHA512);
496 return ret;
9a961efc
MV
497}
498 /*}}}*/
b2e465d6
AL
499// CacheDB::Finish - Write back the cache structure /*{{{*/
500// ---------------------------------------------------------------------
501/* */
502bool CacheDB::Finish()
503{
504 // Optimize away some writes.
505 if (CurStat.Flags == OldStat.Flags &&
9bfe66dc 506 CurStat.mtime == OldStat.mtime)
b2e465d6 507 return true;
cf6bbca0 508
b2e465d6
AL
509 // Write the stat information
510 CurStat.Flags = htonl(CurStat.Flags);
cde41ae8 511 CurStat.FileSize = htonl(CurStat.FileSize);
37497bd5 512 InitQueryStats();
b2e465d6
AL
513 Put(&CurStat,sizeof(CurStat));
514 CurStat.Flags = ntohl(CurStat.Flags);
cde41ae8
MV
515 CurStat.FileSize = ntohl(CurStat.FileSize);
516
b2e465d6
AL
517 return true;
518}
519 /*}}}*/
520// CacheDB::Clean - Clean the Database /*{{{*/
521// ---------------------------------------------------------------------
522/* Tidy the database by removing files that no longer exist at all. */
523bool CacheDB::Clean()
524{
525 if (DBLoaded == false)
526 return true;
527
528 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
529 needs the lower one and 2.7.7 needs the upper.. */
b2e465d6 530 DBC *Cursor;
c9569a1e 531 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
dc738e7a 532 return _error->Error(_("Unable to get a cursor"));
b2e465d6
AL
533
534 DBT Key;
535 DBT Data;
536 memset(&Key,0,sizeof(Key));
537 memset(&Data,0,sizeof(Data));
538 while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
539 {
592b401a
MV
540 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
541 if (Colon)
b2e465d6 542 {
592b401a
MV
543 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
544 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
53ba4e2c 545 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cs") == 0 ||
592b401a 546 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
b2e465d6 547 {
53ba4e2c
MV
548 std::string FileName = std::string((const char *)Key.data,Colon);
549 if (FileExists(FileName) == true) {
550 continue;
551 }
b2e465d6
AL
552 }
553 }
b2e465d6
AL
554 Cursor->c_del(Cursor,0);
555 }
53ba4e2c
MV
556 int res = Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
557 if (res < 0)
558 _error->Warning("compact failed with result %i", res);
559
560 if(_config->FindB("Debug::APT::FTPArchive::Clean", false) == true)
561 Dbp->stat_print(Dbp, 0);
562
b2e465d6
AL
563
564 return true;
565}
566 /*}}}*/