9ec04d5fedf32607eaa133bf2925e0ffeeafbb88
[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
98      public:
99       /**
100       * Constructor.
101       * \param signature Signature that validates the file
102       */
103       SignatureFileChecker( const Pathname &signature );
104
105       /**
106       * Default Constructor.
107       * \short Signature for unsigned files
108       * Use it when you dont have a signature but you want
109       * to check the user to accept an unsigned file.
110       */
111       SignatureFileChecker();
112
113       /**
114        * Set context for this checker.
115        *
116        * Use this method if you're not adding the key (with context) via
117        * one of the addPublicKey methods. The addPublicKey method overwrites
118        * the context.
119        */
120       void setKeyContext(const KeyContext & keycontext);
121
122       /** Return the current context */
123       const KeyContext & keyContext() const
124       { return _context; }
125
126       /** Return whether the last file passed to \ref operator() was accepted.
127        * If this is \ref false \ref operator() was not invoked or threw a
128        * \ref SignatureCheckException.
129        */
130       bool fileAccepted() const
131       { return _fileAccepted; }
132
133       /** Return whether the last file passed to \ref operator() was actually sucessfully verified.
134        * If this is \c false but \ref fileAccepted, the file was accepted due to user interaction or
135        * global settings, but the signature was not verified.
136        */
137       bool fileValidated() const
138       { return _fileValidated; }
139
140       /**
141        * add a public key to the list of known keys
142        */
143       void addPublicKey( const PublicKey & publickey, const KeyContext & keycontext = KeyContext());
144       /** \overload Convenience taking the public keys pathname. */
145       void addPublicKey( const Pathname & publickey, const KeyContext & keycontext = KeyContext());
146
147       /**
148        * Calls \ref KeyRing::verifyFileSignatureWorkflow to verify the file.
149        *
150        * Keep in mind the the workflow may return \c true (file accepted) due to user interaction
151        * or global defaults even if a signature was not actually sucessfully verified. Whether a
152        * signature was actually sucessfully verified can be determined by checking \ref fileValidated
153        * which is invokes IFF a signature for this file actually validated.
154        *
155        * \param file File to validate.fileValidated
156        *
157        * \throws SignatureCheckException if validation fails
158        */
159       void operator()( const Pathname &file ) const;
160
161      protected:
162       Pathname _signature;
163       KeyContext _context;
164       mutable DefaultIntegral<bool,false> _fileAccepted;
165       mutable DefaultIntegral<bool,false> _fileValidated;
166    };
167
168    /**
169    * \short Checks for nothing
170    * Used as the default checker
171    */
172    class NullFileChecker
173    {
174    public:
175      void operator()( const Pathname &file )  const;
176    };
177
178    /**
179     * \short Checker composed of more checkers.
180     *
181     * Allows to create a checker composed of various
182     * checkers altothether. It will only
183     * validate if all the checkers validate.
184     *
185     * \code
186     * CompositeFileChecker com;
187     * com.add(checker1);
188     * com.add(checker2);
189     * fetcher.enqueue(location, com);
190     * \endcode
191     */
192    class CompositeFileChecker
193    {
194    public:
195      void add( const FileChecker &checker );
196     /**
197      * \throws FileCheckException if validation fails
198      */
199      void operator()( const Pathname &file ) const;
200
201      int checkersSize() const { return _checkers.size(); }
202    private:
203      std::list<FileChecker> _checkers;
204    };
205
206   /** \relates FileChecker Stream output */
207   std::ostream & operator<<( std::ostream & str, const FileChecker & obj );
208
209   /////////////////////////////////////////////////////////////////
210 } // namespace zypp
211 ///////////////////////////////////////////////////////////////////
212 #endif // ZYPP_FILECHECKER_H