You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

297 lines
7.6 KiB

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