1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/FileChecker.cc
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"
21 #undef ZYPP_BASE_LOGGER_LOGGROUP
22 #define ZYPP_BASE_LOGGER_LOGGROUP "FileChecker"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
32 void ChecksumFileChecker::operator()( const Pathname &file ) const
34 //MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
35 callback::SendReport<DigestReport> report;
37 if ( _checksum.empty() )
39 MIL << "File " << file << " has no checksum available." << std::endl;
40 if ( report->askUserToAcceptNoDigest(file) )
42 MIL << "User accepted " << file << " with no checksum." << std::endl;
47 ZYPP_THROW( ExceptionType( file.basename() + " has no checksum" ) );
52 CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
53 if ( (real_checksum != _checksum) )
55 if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
57 WAR << "User accepted " << file << " with WRONG CHECKSUM." << std::endl;
62 ZYPP_THROW( ExceptionType( file.basename() + " has wrong checksum" ) );
68 void NullFileChecker::operator()(const Pathname &file ) const
70 MIL << "+ null check on " << file << endl;
74 void CompositeFileChecker::operator()(const Pathname &file ) const
76 //MIL << _checkers.size() << " checkers" << endl;
77 for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
81 //MIL << "+ chk" << endl;
86 ERR << "Invalid checker" << endl;
91 void CompositeFileChecker::add( const FileChecker &checker )
92 { _checkers.push_back(checker); }
95 SignatureFileChecker::SignatureFileChecker( const Pathname & signature )
96 : _signature(signature)
99 SignatureFileChecker::SignatureFileChecker()
102 void SignatureFileChecker::setKeyContext(const KeyContext & keycontext)
103 { _context = keycontext; }
105 void SignatureFileChecker::addPublicKey( const Pathname & publickey, const KeyContext & keycontext )
106 { addPublicKey( PublicKey(publickey), keycontext ); }
108 void SignatureFileChecker::addPublicKey( const PublicKey & publickey, const KeyContext & keycontext )
110 getZYpp()->keyRing()->importKey(publickey, false);
111 _context = keycontext;
114 void SignatureFileChecker::operator()(const Pathname &file ) const
116 if ( (! PathInfo(_signature).isExist()) && (!_signature.empty()) )
118 ZYPP_THROW( ExceptionType("Signature " + _signature.asString() + " not found.") );
121 MIL << "checking " << file << " file validity using digital signature.." << endl;
122 _fileValidated = false;
123 _fileAccepted = getZYpp()->keyRing()->verifyFileSignatureWorkflow( file, file.basename(), _signature, _fileValidated, _context );
125 if ( !_fileAccepted )
126 ZYPP_THROW( ExceptionType( "Signature verification failed for " + file.basename() ) );
129 /******************************************************************
131 ** FUNCTION NAME : operator<<
132 ** FUNCTION TYPE : std::ostream &
134 std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
139 /////////////////////////////////////////////////////////////////
141 ///////////////////////////////////////////////////////////////////