libzypp  15.3.0
FileChecker.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/FileChecker.h"
15 #include "zypp/ZYppFactory.h"
16 #include "zypp/Digest.h"
17 #include "zypp/KeyRing.h"
18 
19 using namespace std;
20 
21 #undef ZYPP_BASE_LOGGER_LOGGROUP
22 #define ZYPP_BASE_LOGGER_LOGGROUP "FileChecker"
23 
25 namespace zypp
26 {
27 
28  ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
29  : _checksum(checksum)
30  {}
31 
32  void ChecksumFileChecker::operator()( const Pathname &file ) const
33  {
34  //MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
36 
37  if ( _checksum.empty() )
38  {
39  MIL << "File " << file << " has no checksum available." << std::endl;
40  if ( report->askUserToAcceptNoDigest(file) )
41  {
42  MIL << "User accepted " << file << " with no checksum." << std::endl;
43  return;
44  }
45  else
46  {
47  ZYPP_THROW( ExceptionType( file.basename() + " has no checksum" ) );
48  }
49  }
50  else
51  {
52  CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
53  if ( (real_checksum != _checksum) )
54  {
55  if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
56  {
57  WAR << "User accepted " << file << " with WRONG CHECKSUM." << std::endl;
58  return;
59  }
60  else
61  {
62  ZYPP_THROW( ExceptionType( file.basename() + " has wrong checksum" ) );
63  }
64  }
65  }
66  }
67 
68  void NullFileChecker::operator()(const Pathname &file ) const
69  {
70  MIL << "+ null check on " << file << endl;
71  return;
72  }
73 
74  void CompositeFileChecker::operator()(const Pathname &file ) const
75  {
76  //MIL << _checkers.size() << " checkers" << endl;
77  for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
78  {
79  if ( *it )
80  {
81  //MIL << "+ chk" << endl;
82  (*it)(file);
83  }
84  else
85  {
86  ERR << "Invalid checker" << endl;
87  }
88  }
89  }
90 
91  void CompositeFileChecker::add( const FileChecker &checker )
92  { _checkers.push_back(checker); }
93 
94 
95  SignatureFileChecker::SignatureFileChecker( const Pathname & signature )
96  : _signature(signature)
97  {}
98 
100  {}
101 
103  { _context = keycontext; }
104 
105  void SignatureFileChecker::addPublicKey( const Pathname & publickey, const KeyContext & keycontext )
106  { addPublicKey( PublicKey(publickey), keycontext ); }
107 
108  void SignatureFileChecker::addPublicKey( const PublicKey & publickey, const KeyContext & keycontext )
109  {
110  getZYpp()->keyRing()->importKey(publickey, false);
111  _context = keycontext;
112  }
113 
114  void SignatureFileChecker::operator()(const Pathname &file ) const
115  {
116  if ( (! PathInfo(_signature).isExist()) && (!_signature.empty()) )
117  {
118  ZYPP_THROW( ExceptionType("Signature " + _signature.asString() + " not found.") );
119  }
120 
121  MIL << "checking " << file << " file validity using digital signature.." << endl;
122  _fileValidated = false;
123  _fileAccepted = getZYpp()->keyRing()->verifyFileSignatureWorkflow( file, file.basename(), _signature, _fileValidated, _context );
124 
125  if ( !_fileAccepted )
126  ZYPP_THROW( ExceptionType( "Signature verification failed for " + file.basename() ) );
127  }
128 
129  /******************************************************************
130  **
131  ** FUNCTION NAME : operator<<
132  ** FUNCTION TYPE : std::ostream &
133  */
134  std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
135  {
136  return str;
137  }
138 
140 } // namespace zypp
#define MIL
Definition: Logger.h:64
bool empty() const
Definition: CheckSum.cc:173
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
String related utilities and Regular expression matching.
SignatureFileChecker()
Default Constructor.
Definition: FileChecker.cc:99
Definition: Arch.h:330
std::list< FileChecker > _checkers
Definition: FileChecker.h:204
void operator()(const Pathname &file) const
Calls KeyRing::verifyFileSignatureWorkflow to verify the file.
Definition: FileChecker.cc:114
#define ERR
Definition: Logger.h:66
void addPublicKey(const PublicKey &publickey, const KeyContext &keycontext=KeyContext())
add a public key to the list of known keys
Definition: FileChecker.cc:108
DefaultIntegral< bool, false > _fileAccepted
Definition: FileChecker.h:165
void operator()(const Pathname &file) const
Definition: FileChecker.cc:68
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
void add(const FileChecker &checker)
Definition: FileChecker.cc:91
#define WAR
Definition: Logger.h:65
DefaultIntegral< bool, false > _fileValidated
Definition: FileChecker.h:166
std::string checksum() const
Definition: CheckSum.cc:170
std::string type() const
Definition: CheckSum.cc:167
SignatureCheckException ExceptionType
Definition: FileChecker.h:96
void operator()(const Pathname &file) const
Definition: FileChecker.cc:74
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:208
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:984
callback::SendReport< DownloadProgressReport > * report
Definition: MediaCurl.cc:178
void operator()(const Pathname &file) const
Try to validate the file.
Definition: FileChecker.cc:32
function< void(const Pathname &file)> FileChecker
Functor signature used to check files.
Definition: FileChecker.h:28
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
CheckSumCheckException ExceptionType
Definition: FileChecker.h:73
void setKeyContext(const KeyContext &keycontext)
Set context for this checker.
Definition: FileChecker.cc:102