Imported Upstream version 16.3.1
[platform/upstream/libzypp.git] / zypp / FileChecker.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/FileChecker.cc
10  *
11 */
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
24 ///////////////////////////////////////////////////////////////////
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;
35     callback::SendReport<DigestReport> report;
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         WAR << "File " <<  file << " has wrong checksum " << real_checksum << " (expected " << _checksum << ")" << endl;
56         if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
57         {
58           WAR << "User accepted " <<  file << " with WRONG CHECKSUM." << std::endl;
59           return;
60         }
61         else
62         {
63           ZYPP_THROW( ExceptionType( file.basename() + " has wrong checksum" ) );
64         }
65       }
66     }
67   }
68
69   void NullFileChecker::operator()(const Pathname &file ) const
70   {
71     MIL << "+ null check on " << file << endl;
72     return;
73   }
74
75   void CompositeFileChecker::operator()(const Pathname &file ) const
76   {
77     //MIL << _checkers.size() << " checkers" << endl;
78     for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
79     {
80       if ( *it )
81       {
82         //MIL << "+ chk" << endl;
83         (*it)(file);
84       }
85       else
86       {
87         ERR << "Invalid checker" << endl;
88       }
89     }
90   }
91
92   void CompositeFileChecker::add( const FileChecker &checker )
93   { _checkers.push_back(checker); }
94
95
96   SignatureFileChecker::SignatureFileChecker( const Pathname & signature )
97        : _signature(signature)
98   {}
99
100   SignatureFileChecker::SignatureFileChecker()
101   {}
102
103   void SignatureFileChecker::setKeyContext(const KeyContext & keycontext)
104   { _context = keycontext; }
105
106   void SignatureFileChecker::addPublicKey( const Pathname & publickey, const KeyContext & keycontext )
107   { addPublicKey( PublicKey(publickey), keycontext ); }
108
109   void SignatureFileChecker::addPublicKey( const PublicKey & publickey, const KeyContext & keycontext )
110   {
111     getZYpp()->keyRing()->importKey(publickey, false);
112     _context = keycontext;
113   }
114
115   void SignatureFileChecker::operator()(const Pathname &file ) const
116   {
117     if ( (! PathInfo(_signature).isExist()) && (!_signature.empty()) )
118     {
119       ZYPP_THROW( ExceptionType("Signature " + _signature.asString() + " not found.") );
120     }
121
122     MIL << "checking " << file << " file validity using digital signature.." << endl;
123     _fileValidated = false;
124     _fileAccepted = getZYpp()->keyRing()->verifyFileSignatureWorkflow( file, file.basename(), _signature, _fileValidated, _context );
125
126     if ( !_fileAccepted )
127       ZYPP_THROW( ExceptionType( "Signature verification failed for "  + file.basename() ) );
128  }
129
130   /******************************************************************
131   **
132   **    FUNCTION NAME : operator<<
133   **    FUNCTION TYPE : std::ostream &
134   */
135   std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
136   {
137     return str;
138   }
139
140   /////////////////////////////////////////////////////////////////
141 } // namespace zypp
142 ///////////////////////////////////////////////////////////////////