19#include <zypp-media/MediaConfig>
25#include <zypp-media/auth/CredentialFileReader>
26#include <zypp-media/MediaException>
28#include <boost/interprocess/sync/file_lock.hpp>
29#include <boost/interprocess/sync/scoped_lock.hpp>
30#include <boost/interprocess/sync/sharable_lock.hpp>
32namespace bpci = boost::interprocess;
37#define USER_CREDENTIALS_FILE ".zypp/credentials.cat"
59 int cmp = lhs->url().asString(
vopt).compare( rhs->url().asString(
vopt) );
61 cmp = lhs->username().compare( rhs->username() );
75 char * homedir =
getenv(
"HOME");
127 : _options(
std::move(options))
128 , _globalDirty(
false)
138 if (_options.globalCredFilePath.empty())
139 DBG <<
"global cred file not known";
152 DBG <<
"global cred file does not exist";
154 _credsGlobal = _credsTmp; _credsTmp.clear();
155 DBG <<
"Got " << _credsGlobal.size() <<
" global records." << endl;
161 if (_options.userCredFilePath.empty())
162 DBG <<
"user cred file not known";
174 DBG <<
"user cred file does not exist" << endl;
176 _credsUser = _credsTmp; _credsTmp.clear();
177 DBG <<
"Got " << _credsUser.size() <<
" user records." << endl;
183 _credsTmp.insert(
cred);
195 if ( !(*it)->url().isValid() )
201 if ( username.empty() || username == (*it)->username() )
231 DBG <<
"Found credentials for '" << url <<
"':" << endl << *result;
233 DBG <<
"No credentials for '" << url <<
"'" << endl;
252 if (
pi.userMayR() )
try {
260 WAR <<
pi <<
" failed to lock file for reading." << endl;
263 if (_credsTmp.empty())
264 WAR <<
pi <<
" does not contain valid credentials or is not readable." << endl;
267 result = *_credsTmp.begin();
282 const auto now =
time(
nullptr );
285 if (
pi.userMayRW() )
try {
290 std::ofstream
fs(file.
c_str());
293 (*it)->dumpAsIniOn(
fs);
294 (*it)->setLastDatabaseUpdate( now );
298 WAR <<
pi <<
" failed to write credentials to file." << endl;
304 WAR <<
pi <<
" failed to lock file for writing." << endl;
337 return _pimpl->getCred(url);
343 {
return _pimpl->getCredFromFile(file); }
348 if ( !
cred.url().isValid() )
370 if (
pi.isExist() &&
pi.isFile() )
378 if ( !
cred.url().isValid() )
383 std::pair<CredentialIterator, bool>
ret =
_pimpl->_credsGlobal.insert(
c_ptr);
385 _pimpl->_globalDirty =
true;
386 else if ((*
ret.first)->password() !=
cred.password())
390 _pimpl->_globalDirty =
true;
397 if ( !
cred.url().isValid() )
402 std::pair<CredentialIterator, bool>
ret =
_pimpl->_credsUser.insert(
c_ptr);
404 _pimpl->_userDirty =
true;
405 else if ((*
ret.first)->password() !=
cred.password())
409 _pimpl->_userDirty =
true;
417 _pimpl->saveGlobalCredentials();
419 _pimpl->saveUserCredentials();
420 _pimpl->_globalDirty =
false;
421 _pimpl->_userDirty =
false;
457 ERR <<
"error saving the credentials" << endl;
467 ERR <<
"could not delete user credentials file "
468 <<
_pimpl->_options.globalCredFilePath << endl;
469 _pimpl->_credsUser.clear();
474 ERR <<
"could not delete global credentials file"
475 <<
_pimpl->_options.userCredFilePath << endl;
476 _pimpl->_credsGlobal.clear();
482 {
return _pimpl->_credsGlobal.begin(); }
485 {
return _pimpl->_credsGlobal.end(); }
488 {
return _pimpl->_credsGlobal.size(); }
491 {
return _pimpl->_credsGlobal.empty(); }
495 {
return _pimpl->_credsUser.begin(); }
498 {
return _pimpl->_credsUser.end(); }
501 {
return _pimpl->_credsUser.size(); }
504 {
return _pimpl->_credsUser.empty(); }
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
void reset()
Reset to default Ctor values.
std::string asString() const
Returns a default string representation of the Url object.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
bool isValid() const
Verifies the Url.
Wrapper class for stat/lstat.
bool isExist() const
Return whether valid stat info exists.
bool absolute() const
Test for an absolute path.
const char * c_str() const
String representation.
std::string basename() const
Return the last component of this path.
#define USER_CREDENTIALS_FILE
int unlink(const Pathname &path)
Like 'unlink'.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Easy-to use interface to the ZYPP dependency resolver.
Url::asString() view options.
static const ViewOption WITH_PASSWORD
Option to include password in the URL string.
static const ViewOption DEFAULTS
Default combination of view options.
static const ViewOption WITH_USERNAME
Option to include username in the URL string.
static const ViewOption WITH_QUERY_STR
Option to include query string in the URL string.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.