libzypp 17.37.10

credentialfilereader.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include "credentialfilereader.h"
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
22 using std::endl;
23
24#undef ZYPP_BASE_LOGGER_LOGGROUP
25#define ZYPP_BASE_LOGGER_LOGGROUP "parser"
26
28 namespace 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 using ProcessCredentials = CredentialFileReader::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 void beginParse() override
75 { /*EMPTY*/ }
76
77 // start a new section [url]
78 void consume( const std::string & section_r ) override
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 void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r ) override
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 void endParse() override
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;
129 AuthData_Ptr _secret;
130 time_t _lastChange;
131 };
132 } // namespace
134
136 //
137 // CLASS NAME : CredentialFileReader
138 //
140
141 CredentialFileReader::CredentialFileReader( const Pathname & crfile_r, const ProcessCredentials & callback_r )
142 { CredentialFileReaderImpl( crfile_r, callback_r ); }
143
144 CredentialFileReader::~CredentialFileReader()
145 {}
146
147 } // namespace media
149} // namespace zypp
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::mtime
time_t mtime() const
Definition PathInfo.h:384
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::media::CredentialFileReader::~CredentialFileReader
~CredentialFileReader()
Definition credentialfilereader.cc:144
zypp::media::CredentialFileReader::ProcessCredentials
function< bool(AuthData_Ptr &)> ProcessCredentials
Callback invoked for each entry found in the file.
Definition credentialfilereader.h:36
zypp::media::CredentialFileReader::CredentialFileReader
CredentialFileReader(const Pathname &crfile_r, const ProcessCredentials &callback_r)
Definition credentialfilereader.cc:141
credentialfilereader.h
zypp::media::AuthData_Ptr
shared_ptr< AuthData > AuthData_Ptr
Definition authdata.h:81
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
Logger.h
ERR
#define ERR
Definition Logger.h:102
PathInfo.h