indexrecords.cc 7.6 KB

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