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 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
30 void ChecksumFileChecker::operator()( const Pathname &file ) const
32 //MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
33 callback::SendReport<DigestReport> report;
35 if ( _checksum.empty() )
37 MIL << "File " << file << " has no checksum available." << std::endl;
38 if ( report->askUserToAcceptNoDigest(file) )
40 MIL << "User accepted " << file << " with no checksum." << std::endl;
45 ZYPP_THROW( FileCheckException( file.basename() + " has no checksum" ) );
50 CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
51 if ( (real_checksum != _checksum) )
53 if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
55 WAR << "User accepted " << file << " with WRONG CHECKSUM." << std::endl;
60 ZYPP_THROW( FileCheckException( file.basename() + " has wrong checksum" ) );
66 void NullFileChecker::operator()(const Pathname &file ) const
68 MIL << "+ null check on " << file << endl;
72 void CompositeFileChecker::operator()(const Pathname &file ) const
74 //MIL << _checkers.size() << " checkers" << endl;
75 for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
79 //MIL << "+ chk" << endl;
84 ERR << "Invalid checker" << endl;
89 void CompositeFileChecker::add( const FileChecker &checker )
91 //MIL << "||# " << _checkers.size() << endl;
92 _checkers.push_back(checker);
93 //MIL << "||* " << _checkers.size() << endl;
97 SignatureFileChecker::SignatureFileChecker( const Pathname &signature )
98 : _signature(signature)
104 SignatureFileChecker::SignatureFileChecker()
108 void SignatureFileChecker::setKeyContext(const KeyContext & keycontext)
109 { _context = keycontext; }
111 void SignatureFileChecker::addPublicKey( const Pathname & publickey, const KeyContext & keycontext )
112 { addPublicKey( PublicKey(publickey), keycontext ); }
114 void SignatureFileChecker::addPublicKey( const PublicKey & publickey, const KeyContext & keycontext )
116 getZYpp()->keyRing()->importKey(publickey, false);
117 _context = keycontext;
120 void SignatureFileChecker::operator()(const Pathname &file ) const
122 ZYpp::Ptr z = getZYpp();
124 if ( (! PathInfo(_signature).isExist()) && (!_signature.empty()))
126 ZYPP_THROW(FileCheckException("Signature " + _signature.asString() + " not found."));
129 MIL << "checking " << file << " file validity using digital signature.." << endl;
130 bool valid = z->keyRing()->verifyFileSignatureWorkflow( file, file.basename(), _signature, _context);
133 ZYPP_THROW( FileCheckException( "Signature verification failed for " + file.basename() ) );
136 /******************************************************************
138 ** FUNCTION NAME : operator<<
139 ** FUNCTION TYPE : std::ostream &
141 std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
146 /////////////////////////////////////////////////////////////////
148 ///////////////////////////////////////////////////////////////////