libzypp 9.41.1

CredentialFileReader.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 
00014 #include "zypp/base/Logger.h"
00015 #include "zypp/base/InputStream.h"
00016 #include "zypp/parser/IniDict.h"
00017 
00018 #include "zypp/media/CredentialFileReader.h"
00019 
00020 using std::endl;
00021 
00022 #undef ZYPP_BASE_LOGGER_LOGGROUP
00023 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
00024 
00026 namespace zypp
00027 {
00029   namespace media
00030   {
00032     namespace
00033     {
00034       // Looks like INI but allows multiple sections for the same URL
00035       // but different user (in .cat files). So don't use an Ini
00036       // Also support a global section without '[URL]' which is used
00037       // in credential files.
00038       // -------------------------------------
00039       // username = emptyUSER
00040       // password = emptyPASS
00041       // -------------------------------------
00042       // [http://server/tmp/sumafake222]
00043       // username = USER
00044       // password = PASS
00045       //
00046       // [http://server/tmp/sumafake222]
00047       // username = USER2
00048       // password = PASS
00049       // -------------------------------------
00050       struct CredentialFileReaderImpl : public parser::IniParser
00051       {
00052         typedef CredentialFileReader::ProcessCredentials ProcessCredentials;
00053 
00054         struct StopParsing {};
00055 
00056         CredentialFileReaderImpl( const Pathname & input_r, const ProcessCredentials & callback_r )
00057         : _input( input_r )
00058         , _callback( callback_r )
00059         {
00060           try
00061           {
00062             parse( input_r );
00063           }
00064           catch ( StopParsing )
00065           { /* NO error but consumer aborted parsing */ }
00066         }
00067 
00068         // NO-OP; new sections are opened in consume()
00069         virtual void beginParse()
00070         { /*EMPTY*/ }
00071 
00072         // start a new section [url]
00073         virtual void consume( const std::string & section_r )
00074         {
00075           endParse();   // close any open section
00076           _secret.reset( new AuthData );
00077           try
00078           {
00079             _secret->setUrl( Url(section_r) );
00080           }
00081           catch ( const url::UrlException & )
00082           {
00083             ERR << "Ignore invalid URL '" << section_r << "' in file " << _input << endl;
00084             _secret.reset();    // ignore this section
00085           }
00086         }
00087 
00088         virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
00089         {
00090           if ( !_secret && section_r.empty() )
00091             _secret.reset( new AuthData );      // a initial global section without [URL]
00092 
00093           if ( _secret )
00094           {
00095             if ( key_r == "username" )
00096               _secret->setUsername( value_r );
00097             else if ( key_r == "password" )
00098               _secret->setPassword( value_r );
00099             else
00100               WAR << "Ignore unknown attribute '" << key_r << "=" << value_r << "' in file " << _input << endl;
00101           }
00102           // else: ignored section due to wrong URL
00103         }
00104 
00105         // send any valid pending section
00106         virtual void endParse()
00107         {
00108           if ( _secret )
00109           {
00110             if ( _secret->valid() )
00111             {
00112               if ( !_callback( _secret ) )
00113                 throw( StopParsing() );
00114             }
00115             else
00116               ERR << "Ignore invalid credentials for URL '" << _secret->url() << "' in file " << _input << endl;
00117           }
00118         }
00119 
00120       private:
00121         const Pathname &                _input;
00122         const ProcessCredentials &      _callback;
00123         AuthData_Ptr                    _secret;
00124       };
00125     } // namespace
00127 
00129     //
00130     // CLASS NAME : CredentialFileReader
00131     //
00133 
00134     CredentialFileReader::CredentialFileReader( const Pathname & crfile_r, const ProcessCredentials & callback_r )
00135     { CredentialFileReaderImpl( crfile_r, callback_r ); }
00136 
00137     CredentialFileReader::~CredentialFileReader()
00138     {}
00139 
00140   } // namespace media
00142 } // namespace zypp
00144