1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/CredentialFileReader.cc
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/InputStream.h"
16 #include "zypp/parser/IniDict.h"
18 #include "zypp/media/CredentialFileReader.h"
22 #undef ZYPP_BASE_LOGGER_LOGGROUP
23 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
25 ///////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
31 ///////////////////////////////////////////////////////////////////
34 // Looks like INI but allows multiple sections for the same URL
35 // but different user (in .cat files). So don't use an Ini
36 // Also support a global section without '[URL]' which is used
37 // in credential files.
38 // -------------------------------------
39 // username = emptyUSER
40 // password = emptyPASS
41 // -------------------------------------
42 // [http://server/tmp/sumafake222]
46 // [http://server/tmp/sumafake222]
49 // -------------------------------------
50 struct CredentialFileReaderImpl : public parser::IniParser
52 typedef CredentialFileReader::ProcessCredentials ProcessCredentials;
54 struct StopParsing {};
56 CredentialFileReaderImpl( const Pathname & input_r, const ProcessCredentials & callback_r )
58 , _callback( callback_r )
65 { /* NO error but consumer aborted parsing */ }
68 // NO-OP; new sections are opened in consume()
69 virtual void beginParse()
72 // start a new section [url]
73 virtual void consume( const std::string & section_r )
75 endParse(); // close any open section
76 _secret.reset( new AuthData );
79 _secret->setUrl( Url(section_r) );
81 catch ( const url::UrlException & )
83 ERR << "Ignore invalid URL '" << section_r << "' in file " << _input << endl;
84 _secret.reset(); // ignore this section
88 virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
90 if ( !_secret && section_r.empty() )
91 _secret.reset( new AuthData ); // a initial global section without [URL]
95 if ( key_r == "username" )
96 _secret->setUsername( value_r );
97 else if ( key_r == "password" )
98 _secret->setPassword( value_r );
100 WAR << "Ignore unknown attribute '" << key_r << "=" << value_r << "' in file " << _input << endl;
102 // else: ignored section due to wrong URL
105 // send any valid pending section
106 virtual void endParse()
110 if ( _secret->valid() )
112 if ( !_callback( _secret ) )
113 throw( StopParsing() );
116 ERR << "Ignore invalid credentials for URL '" << _secret->url() << "' in file " << _input << endl;
121 const Pathname & _input;
122 const ProcessCredentials & _callback;
123 AuthData_Ptr _secret;
126 ///////////////////////////////////////////////////////////////////
128 //////////////////////////////////////////////////////////////////////
130 // CLASS NAME : CredentialFileReader
132 //////////////////////////////////////////////////////////////////////
134 CredentialFileReader::CredentialFileReader( const Pathname & crfile_r, const ProcessCredentials & callback_r )
135 { CredentialFileReaderImpl( crfile_r, callback_r ); }
137 CredentialFileReader::~CredentialFileReader()
141 ///////////////////////////////////////////////////////////////////
143 ///////////////////////////////////////////////////////////////////