16#include <zypp/base/Function.h>
17#include <zypp/base/Logger.h>
18#include <zypp/base/Easy.h>
19#include <zypp/PathInfo.h>
25#include <boost/interprocess/sync/file_lock.hpp>
26#include <boost/interprocess/sync/scoped_lock.hpp>
27#include <boost/interprocess/sync/sharable_lock.hpp>
29namespace bpci = boost::interprocess;
34#define USER_CREDENTIALS_FILE ".zypp/credentials.cat"
56 int cmp = lhs->url().asString(vopt).compare( rhs->url().asString(vopt) );
58 cmp = lhs->username().compare( rhs->username() );
69 : globalCredFilePath(rootdir /
ZConfig::instance().credentialsGlobalFile())
70 , customCredFileDir(rootdir /
ZConfig::instance().credentialsGlobalDir())
72 char * homedir = getenv(
"HOME");
120 , _globalDirty(false)
130 if (_options.globalCredFilePath.empty())
131 DBG <<
"global cred file not known";
144 DBG <<
"global cred file does not exist";
146 _credsGlobal = _credsTmp; _credsTmp.clear();
147 DBG <<
"Got " << _credsGlobal.size() <<
" global records." << endl;
153 if (_options.userCredFilePath.empty())
154 DBG <<
"user cred file not known";
166 DBG <<
"user cred file does not exist" << endl;
168 _credsUser = _credsTmp; _credsTmp.clear();
169 DBG <<
"Got " << _credsUser.size() <<
" user records." << endl;
175 _credsTmp.insert(cred);
187 if ( !(*it)->url().isValid() )
191 if ( url.
asString(vopt).find((*it)->url().asString(vopt)) == 0 )
193 if ( username.empty() || username == (*it)->username() )
217 result =
findIn(_credsGlobal, url, vopt);
221 result =
findIn(_credsUser, url, vopt);
224 DBG <<
"Found credentials for '" << url <<
"':" << endl << *result;
226 DBG <<
"No credentials for '" << url <<
"'" << endl;
242 credfile = _options.customCredFileDir / file.
basename();
245 bpci::file_lock lockFile ( credfile.
c_str() );
246 bpci::scoped_lock lock( lockFile );
249 if (_credsTmp.empty())
250 WAR << file <<
" does not contain valid credentials or is not readable." << endl;
253 result = *_credsTmp.begin();
268 std::ofstream fs(file.
c_str());
273 bpci::file_lock lockFile ( file.
c_str() );
274 bpci::scoped_lock lock( lockFile );
277 for_(it, creds.begin(), creds.end())
279 (*it)->dumpAsIniOn(fs);
280 (*it)->setLastDatabaseUpdate( time(
nullptr ) );
315 if (credfile.empty())
316 return _pimpl->getCred(url);
317 return _pimpl->getCredFromFile(credfile);
322 {
return _pimpl->getCredFromFile(file); }
331 if (credfile.
empty())
346 std::pair<CredentialIterator, bool> ret =
_pimpl->_credsGlobal.insert(c_ptr);
348 _pimpl->_globalDirty =
true;
349 else if ((*ret.first)->password() != cred.
password())
351 _pimpl->_credsGlobal.erase(ret.first);
352 _pimpl->_credsGlobal.insert(c_ptr);
353 _pimpl->_globalDirty =
true;
365 std::pair<CredentialIterator, bool> ret =
_pimpl->_credsUser.insert(c_ptr);
367 _pimpl->_userDirty =
true;
368 else if ((*ret.first)->password() != cred.
password())
370 _pimpl->_credsUser.erase(ret.first);
371 _pimpl->_credsUser.insert(c_ptr);
372 _pimpl->_userDirty =
true;
380 _pimpl->saveGlobalCredentials();
382 _pimpl->saveUserCredentials();
383 _pimpl->_globalDirty =
false;
384 _pimpl->_userDirty =
false;
406 c_ptr->setUrl(
Url());
415 creds,
_pimpl->_options.customCredFileDir / credFile, 0600);
420 ERR <<
"error saving the credentials" << endl;
430 ERR <<
"could not delete user credentials file "
431 <<
_pimpl->_options.globalCredFilePath << endl;
432 _pimpl->_credsUser.clear();
437 ERR <<
"could not delete global credentials file"
438 <<
_pimpl->_options.userCredFilePath << endl;
439 _pimpl->_credsGlobal.clear();
445 {
return _pimpl->_credsGlobal.begin(); }
448 {
return _pimpl->_credsGlobal.end(); }
451 {
return _pimpl->_credsGlobal.size(); }
454 {
return _pimpl->_credsGlobal.empty(); }
458 {
return _pimpl->_credsUser.begin(); }
461 {
return _pimpl->_credsUser.end(); }
464 {
return _pimpl->_credsUser.size(); }
467 {
return _pimpl->_credsUser.empty(); }
#define USER_CREDENTIALS_FILE
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.
Interim helper class to collect global options and settings.
Wrapper class for ::stat/::lstat.
bool isExist() const
Return whether valid stat info exists.
Pathname dirname() const
Return all but the last component od this path.
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.
bool empty() const
Test for an empty path.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
int unlink(const Pathname &path)
Like 'unlink'.
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
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.