Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / FileChecker.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/FileChecker.h
10  *
11 */
12 #ifndef ZYPP_FILECHECKER_H
13 #define ZYPP_FILECHECKER_H
14
15 #include <iosfwd>
16 #include <list>
17 #include "zypp/base/DefaultIntegral.h"
18 #include "zypp/base/Exception.h"
19 #include "zypp/base/Function.h"
20 #include "zypp/PathInfo.h"
21 #include "zypp/CheckSum.h"
22 #include "zypp/KeyContext.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   class PublicKey;
29
30   /**
31    * Functor signature used to check files.
32    * \param file File to check.
33    *
34    * \throws FileCheckException when the file does not
35    * validate and the user don't want to continue.
36    */
37   typedef function<void ( const Pathname &file )> FileChecker;
38
39   class FileCheckException : public Exception
40   {
41   public:
42     FileCheckException(const std::string &msg)
43       : Exception(msg)
44     {}
45   };
46
47   class CheckSumCheckException : public FileCheckException
48   {
49   public:
50     CheckSumCheckException(const std::string &msg)
51       : FileCheckException(msg)
52     {}
53   };
54
55   class SignatureCheckException : public FileCheckException
56   {
57   public:
58     SignatureCheckException(const std::string &msg)
59       : FileCheckException(msg)
60     {}
61   };
62
63   /**
64    * Built in file checkers
65    */
66
67   /**
68    * \short Checks for a valid checksum and interacts with the user.
69    */
70    class ChecksumFileChecker
71    {
72    public:
73      typedef CheckSumCheckException ExceptionType;
74      /**
75       * Constructor.
76       * \param checksum Checksum that validates the file
77       */
78      ChecksumFileChecker( const CheckSum &checksum );
79      /**
80       * \short Try to validate the file
81       * \param file File to validate.
82       *
83       * \throws CheckSumCheckException if validation fails
84       */
85      void operator()( const Pathname &file ) const;
86    private:
87      CheckSum _checksum;
88    };
89
90    /**
91     * \short Checks for the validity of a signature
92     */
93    class SignatureFileChecker
94    {
95      public:
96        typedef SignatureCheckException ExceptionType;
97        typedef function<void ( const SignatureFileChecker & checker,  const Pathname & file )> OnSigValidated;
98
99      public:
100       /**
101       * Constructor.
102       * \param signature Signature that validates the file
103       */
104       SignatureFileChecker( const Pathname &signature );
105
106       /**
107       * Default Constructor.
108       * \short Signature for unsigned files
109       * Use it when you dont have a signature but you want
110       * to check the user to accept an unsigned file.
111       */
112       SignatureFileChecker();
113
114       /**
115        * Set context for this checker.
116        *
117        * Use this method if you're not adding the key (with context) via
118        * one of the addPublicKey methods. The addPublicKey method overwrites
119        * the context.
120        */
121       void setKeyContext(const KeyContext & keycontext);
122
123       /** Return the current context */
124       const KeyContext & keyContext() const
125       { return _context; }
126
127       /** Return whether the last file passed to \ref operator() was accepted.
128        * If this is \ref false \ref operator() was not invoked or threw a
129        * \ref SignatureCheckException.
130        */
131       bool fileAccepted() const
132       { return _fileAccepted; }
133
134       /** Return whether the last file passed to \ref operator() was actually sucessfully verified.
135        * If this is \c false but \ref fileAccepted, the file was accepted due to user interaction or
136        * global settings, but the signature was not verified.
137        */
138       bool fileValidated() const
139       { return _fileValidated; }
140
141       /**
142        * add a public key to the list of known keys
143        */
144       void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
145       /** \overload Convenience taking the public keys pathname. */
146       void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
147
148       /**
149        * Calls \ref KeyRing::verifyFileSignatureWorkflow to verify the file.
150        *
151        * Keep in mind the the workflow may return \c true (file accepted) due to user interaction
152        * or global defaults even if a signature was not actually sucessfully verified. Whether a
153        * signature was actually sucessfully verified can be determined by checking \ref fileValidated
154        * which is invokes IFF a signature for this file actually validated.
155        *
156        * \param file File to validate.fileValidated
157        *
158        * \throws SignatureCheckException if validation fails
159        */
160       void operator()( const Pathname &file ) const;
161
162      protected:
163       Pathname _signature;
164       KeyContext _context;
165       mutable DefaultIntegral<bool,false> _fileAccepted;
166       mutable DefaultIntegral<bool,false> _fileValidated;
167    };
168
169    /**
170    * \short Checks for nothing
171    * Used as the default checker
172    */
173    class NullFileChecker
174    {
175    public:
176      void operator()( const Pathname &file )  const;
177    };
178
179    /**
180     * \short Checker composed of more checkers.
181     *
182     * Allows to create a checker composed of various
183     * checkers altothether. It will only
184     * validate if all the checkers validate.
185     *
186     * \code
187     * CompositeFileChecker com;
188     * com.add(checker1);
189     * com.add(checker2);
190     * fetcher.enqueue(location, com);
191     * \endcode
192     */
193    class CompositeFileChecker
194    {
195    public:
196      void add( const FileChecker &checker );
197     /**
198      * \throws FileCheckException if validation fails
199      */
200      void operator()( const Pathname &file ) const;
201
202      int checkersSize() const { return _checkers.size(); }
203    private:
204      std::list<FileChecker> _checkers;
205    };
206
207   /** \relates FileChecker Stream output */
208   std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
209
210   /////////////////////////////////////////////////////////////////
211 } // namespace zypp
212 ///////////////////////////////////////////////////////////////////
213 #endif // ZYPP_FILECHECKER_H