- provide context about the repository (name/alias) if available when
[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 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   ChecksumFileChecker::ChecksumFileChecker( const CheckSum &checksum )
26     : _checksum(checksum)
27   {
28   }
29
30   void ChecksumFileChecker::operator()( const Pathname &file ) const
31   {
32     MIL << "checking " << file << " file against checksum '" << _checksum << "'" << endl;
33     callback::SendReport<DigestReport> report;
34     CheckSum real_checksum( _checksum.type(), filesystem::checksum( file, _checksum.type() ));
35     
36     if ( _checksum.empty() )
37     {
38       MIL << "File " <<  file << " has no checksum available." << std::endl;
39       if ( report->askUserToAcceptNoDigest(file) )
40       {
41         MIL << "User accepted " <<  file << " with no checksum." << std::endl;
42         return;
43       }
44       else
45       {
46         ZYPP_THROW( FileCheckException( "No checksum available for " + file.basename() ) );
47       }
48     }
49     else
50     {
51       if ( (real_checksum != _checksum) )
52       {
53         if ( report->askUserToAcceptWrongDigest( file, _checksum.checksum(), real_checksum.checksum() ) )
54         {
55           WAR << "User accepted " <<  file << " with WRONG CHECKSUM." << std::endl;
56           return;
57         }
58         else
59         {
60           ZYPP_THROW( FileCheckException( "Wrong checksum for "  + file.basename() ) );
61         }
62       }
63     }
64   }
65
66   void NullFileChecker::operator()(const Pathname &file ) const
67   {
68     MIL << "+ null check on " << file << endl;
69     return;
70   }
71
72   void CompositeFileChecker::operator()(const Pathname &file ) const
73   {
74     //MIL << _checkers.size() << " checkers" << endl;
75     for ( list<FileChecker>::const_iterator it = _checkers.begin(); it != _checkers.end(); ++it )
76     {
77       if ( *it )
78       {
79         //MIL << "+ chk" << endl;
80         (*it)(file);
81       }
82       else
83       {
84         ERR << "Invalid checker" << endl;
85       }
86     }
87   }
88   
89   void CompositeFileChecker::add( const FileChecker &checker )
90   {
91     //MIL << "||# " << _checkers.size() << endl;
92     _checkers.push_back(checker);
93     //MIL << "||* " << _checkers.size() << endl;
94     
95   }
96
97    SignatureFileChecker::SignatureFileChecker( const Pathname &signature,
98                                                const std::string &description )
99        : _signature(signature)
100        , _description(description)
101   {
102
103   }
104   
105   SignatureFileChecker::SignatureFileChecker( const std::string &description )
106       : _description(description)
107   {
108   }
109   
110
111   SignatureFileChecker::SignatureFileChecker()
112   {
113   }
114   
115   void SignatureFileChecker::addPublicKey( const Pathname &publickey )
116   {
117     ZYpp::Ptr z = getZYpp();
118     z->keyRing()->importKey(publickey, false);
119   }
120   
121   void SignatureFileChecker::operator()(const Pathname &file ) const
122   {
123     ZYpp::Ptr z = getZYpp();
124
125     if ( (! PathInfo(_signature).isExist()) && (!_signature.empty()))
126     {
127       ZYPP_THROW(FileCheckException("Signature " + _signature.asString() + " not found."));
128     }
129
130     MIL << "checking " << file << " file validity using digital signature.." << endl;
131     bool valid = z->keyRing()->verifyFileSignatureWorkflow( file, _description.empty() ? file.basename() : _description, _signature);
132
133     if (!valid)
134       ZYPP_THROW( FileCheckException( "Signature verification failed for "  + file.basename() ) );
135   }
136
137   /******************************************************************
138   **
139   **    FUNCTION NAME : operator<<
140   **    FUNCTION TYPE : std::ostream &
141   */
142   std::ostream & operator<<( std::ostream & str, const FileChecker & obj )
143   {
144     return str;
145   }
146
147   /////////////////////////////////////////////////////////////////
148 } // namespace zypp
149 ///////////////////////////////////////////////////////////////////