libzypp  17.35.12
FileChecker.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <utility>
14 #include <zypp/base/Logger.h>
15 #include <zypp/FileChecker.h>
16 #include <zypp/ZYppFactory.h>
17 #include <zypp/Digest.h>
18 #include <zypp/KeyRing.h>
22 
23 using std::endl;
24 
25 #undef ZYPP_BASE_LOGGER_LOGGROUP
26 #define ZYPP_BASE_LOGGER_LOGGROUP "FileChecker"
27 
29 namespace zypp
30 {
31 
33  : _checksum(std::move(checksum))
34  {}
35 
36  void ChecksumFileChecker::operator()( const Pathname &file ) const
37  {
38  const auto &res = zyppng::CheckSumWorkflow::verifyChecksum ( zyppng::SyncContext::create(), _checksum, file );
39  if ( !res ) {
40  std::rethrow_exception( res.error ( ) );
41  }
42  }
43 
44  void NullFileChecker::operator()(const Pathname &file ) const
45  {
46  MIL << "+ null check on " << file << endl;
47  return;
48  }
49 
50  void CompositeFileChecker::operator()(const Pathname &file ) const
51  {
52  //MIL << _checkers.size() << " checkers" << endl;
53  for ( std::list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
54  {
55  if ( *it )
56  {
57  //MIL << "+ chk" << endl;
58  (*it)(file);
59  }
60  else
61  {
62  ERR << "Invalid checker" << endl;
63  }
64  }
65  }
66 
67  void CompositeFileChecker::add( const FileChecker &checker )
68  { _checkers.push_back(checker); }
69 
70 
72  {}
73 
75  { _verifyContext.signature( std::move(signature_r) ); }
76 
77  void SignatureFileChecker::addPublicKey( const Pathname & publickey_r )
78  { addPublicKey( PublicKey(publickey_r) ); }
79 
80  void SignatureFileChecker::addPublicKey( const PublicKey & publickey_r )
81  { getZYpp()->keyRing()->importKey( publickey_r, false ); }
82 
83  void SignatureFileChecker::operator()( const Pathname & file_r ) const
84  {
85  // const_cast because the workflow is allowed to store result values here
86  SignatureFileChecker & self { const_cast<SignatureFileChecker&>(*this) };
87  self._verifyContext.file( file_r );
88 
90  if ( !res ) {
91  std::rethrow_exception( res.error ( ) );
92  }
93  self._verifyContext = std::move( *res );
94  }
95 
97  {
98  return _verifyContext;
99  }
100 
102  {
103  return _verifyContext;
104  }
105 
106  /******************************************************************
107  **
108  ** FUNCTION NAME : operator<<
109  ** FUNCTION TYPE : std::ostream &
110  */
111  std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
112  {
113  return str;
114  }
115 
117 } // namespace zypp
void operator()(const Pathname &file) const
Definition: FileChecker.cc:44
#define MIL
Definition: Logger.h:98
keyring::VerifyFileContext & verifyContext()
Definition: FileChecker.cc:96
Checks for the validity of a signature.
Definition: FileChecker.h:70
void addPublicKey(const PublicKey &publickey_r)
Add a public key to the list of known keys.
Definition: FileChecker.cc:80
void operator()(const Pathname &file_r) const
Call KeyRing::verifyFileSignatureWorkflow to verify the file.
Definition: FileChecker.cc:83
keyring::VerifyFileContext _verifyContext
Definition: FileChecker.h:108
String related utilities and Regular expression matching.
SignatureFileChecker()
Default Ctor for unsigned files.
Definition: FileChecker.cc:71
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Definition: Arch.h:363
I/O context for KeyRing::verifyFileSignatureWorkflow.
std::list< FileChecker > _checkers
Definition: FileChecker.h:146
#define ERR
Definition: Logger.h:100
void operator()(const Pathname &file) const
Try to validate the file.
Definition: FileChecker.cc:36
const Pathname & signature() const
Detached signature or empty.
ChecksumFileChecker(CheckSum checksum)
Constructor.
Definition: FileChecker.cc:32
void operator()(const Pathname &file) const
Definition: FileChecker.cc:50
void add(const FileChecker &checker)
Definition: FileChecker.cc:67
const Pathname & file() const
File to verify.
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:364
expected< zypp::keyring::VerifyFileContext > verifySignature(SyncContextRef ctx, zypp::keyring::VerifyFileContext context)
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:1056
expected< void > verifyChecksum(SyncContextRef zyppCtx, zypp::CheckSum checksum, zypp::Pathname file)
Definition: checksumwf.cc:115
function< void(const Pathname &file)> FileChecker
Functor signature used to check files.
Definition: FileChecker.h:29
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19