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