libzypp 17.31.23
credentialfilereader.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
14
15#include <iostream>
16
17#include <zypp-core/base/Logger.h>
18#include <zypp-core/base/InputStream>
19#include <zypp-core/parser/IniDict>
20#include <zypp-core/fs/PathInfo.h>
21
22using std::endl;
23
24#undef ZYPP_BASE_LOGGER_LOGGROUP
25#define ZYPP_BASE_LOGGER_LOGGROUP "parser"
26
28namespace zypp
29{
31 namespace media
32 {
34 namespace
35 {
36 // Looks like INI but allows multiple sections for the same URL
37 // but different user (in .cat files). So don't use an Ini
38 // Also support a global section without '[URL]' which is used
39 // in credential files.
40 // -------------------------------------
41 // username = emptyUSER
42 // password = emptyPASS
43 // -------------------------------------
44 // [http://server/tmp/sumafake222]
45 // username = USER
46 // password = PASS
47 //
48 // [http://server/tmp/sumafake222]
49 // username = USER2
50 // password = PASS
51 // -------------------------------------
52 struct CredentialFileReaderImpl : public parser::IniParser
53 {
54 typedef CredentialFileReader::ProcessCredentials ProcessCredentials;
55
56 struct StopParsing {};
57
58 CredentialFileReaderImpl( const Pathname & input_r, const ProcessCredentials & callback_r )
59 : _input( input_r )
60 , _callback( callback_r )
61 {
62 zypp::PathInfo pi( input_r );
63 _lastChange = pi.mtime();
64
65 try
66 {
67 parse( input_r );
68 }
69 catch ( StopParsing )
70 { /* NO error but consumer aborted parsing */ }
71 }
72
73 // NO-OP; new sections are opened in consume()
74 virtual void beginParse()
75 { /*EMPTY*/ }
76
77 // start a new section [url]
78 virtual void consume( const std::string & section_r )
79 {
80 endParse(); // close any open section
81 _secret.reset( new AuthData );
82 try
83 {
84 _secret->setUrl( Url(section_r) );
85 }
86 catch ( const url::UrlException & )
87 {
88 ERR << "Ignore invalid URL '" << section_r << "' in file " << _input << endl;
89 _secret.reset(); // ignore this section
90 }
91 }
92
93 virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
94 {
95 if ( !_secret && section_r.empty() )
96 _secret.reset( new AuthData ); // a initial global section without [URL]
97
98 if ( _secret )
99 {
100 if ( key_r == "username" )
101 _secret->setUsername( value_r );
102 else if ( key_r == "password" )
103 _secret->setPassword( value_r );
104 else
105 _secret->extraValues()[key_r] = value_r;
106 }
107 // else: ignored section due to wrong URL
108 }
109
110 // send any valid pending section
111 virtual void endParse()
112 {
113 if ( _secret )
114 {
115 if ( _secret->valid() )
116 {
117 _secret->setLastDatabaseUpdate( _lastChange );
118 if ( !_callback( _secret ) )
119 throw( StopParsing() );
120 }
121 else
122 ERR << "Ignore invalid credentials for URL '" << _secret->url() << "' in file " << _input << endl;
123 }
124 }
125
126 private:
127 const Pathname & _input;
128 const ProcessCredentials & _callback;
131 };
132 } // namespace
134
136 //
137 // CLASS NAME : CredentialFileReader
138 //
140
142 { CredentialFileReaderImpl( crfile_r, callback_r ); }
143
145 {}
146
147 } // namespace media
149} // namespace zypp
Wrapper class for stat/lstat.
Definition: PathInfo.h:221
time_t mtime() const
Definition: PathInfo.h:376
function< bool(AuthData_Ptr &)> ProcessCredentials
Callback invoked for each entry found in the file.
CredentialFileReader(const Pathname &crfile_r, const ProcessCredentials &callback_r)
const Pathname & _input
const ProcessCredentials & _callback
AuthData_Ptr _secret
time_t _lastChange
shared_ptr< AuthData > AuthData_Ptr
Definition: authdata.h:79
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
#define ERR
Definition: Logger.h:98