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"
55 - url::ViewOption::WITH_USERNAME
56 - url::ViewOption::WITH_PASSWORD
57 - url::ViewOption::WITH_QUERY_STR;
59 int cmp = lhs->url().asString(vopt).compare( rhs->url().asString(vopt) );
61 cmp = lhs->username().compare( rhs->username() );
75 char * homedir = getenv(
"HOME");
138 if (
_options.globalCredFilePath.empty())
139 DBG <<
"global cred file not known" << endl;
152 DBG <<
"global cred file does not exist (" <<
_options.globalCredFilePath <<
")" << endl;
161 if (
_options.userCredFilePath.empty())
162 DBG <<
"user cred file not known" << endl;
174 DBG <<
"user cred file does not exist (" <<
_options.userCredFilePath <<
")" << endl;
177 DBG <<
"Got " <<
_credsUser.size() <<
" user records." << endl;
192 const std::string & username =
url.getUsername();
195 if ( !(*it)->url().isValid() )
199 if (
url.asString(vopt).find((*it)->url().asString(vopt)) == 0 )
201 if ( username.empty() || username == (*it)->username() )
219 - url::ViewOption::WITH_USERNAME
220 - url::ViewOption::WITH_PASSWORD
221 - url::ViewOption::WITH_QUERY_STR;
231 DBG <<
"Found credentials for '" <<
url <<
"':" << endl << *result << endl;
233 DBG <<
"No credentials for '" <<
url <<
"'" << endl;
254 bpci::file_lock lockFile ( credfile.
c_str() );
255 bpci::scoped_lock lock( lockFile );
260 WAR << pi <<
" failed to lock file for reading." << endl;
264 WAR << pi <<
" does not contain valid credentials or is not readable." << endl;
282 const auto now = time(
nullptr );
287 bpci::file_lock lockFile ( file.
c_str() );
288 bpci::scoped_lock lock( lockFile );
290 std::ofstream fs(file.
c_str());
291 for (
auto& credentials : creds )
293 credentials->dumpAsIniOn( fs );
294 credentials->setLastDatabaseUpdate( now );
298 WAR << pi <<
" failed to write credentials to file." << endl;
304 WAR << pi <<
" failed to lock file for writing." << endl;
335 std::string credfile =
url.getQueryParam(
"credentials");
336 if (credfile.empty())
338 return _pimpl->getCredFromFile(credfile);
343 {
return _pimpl->getCredFromFile(file); }
352 if (credfile.
empty())
362 if (
url.isValid() ) {
363 credfile =
url.getQueryParam(
"credentials");
366 if (credfile.
empty())
367 credfile =
_pimpl->_options.userCredFilePath;
383 std::pair<CredentialIterator, bool> ret =
_pimpl->_credsGlobal.insert(c_ptr);
385 _pimpl->_globalDirty =
true;
386 else if ((*ret.first)->password() != cred.
password())
388 _pimpl->_credsGlobal.erase(ret.first);
389 _pimpl->_credsGlobal.insert(c_ptr);
390 _pimpl->_globalDirty =
true;
402 std::pair<CredentialIterator, bool> ret =
_pimpl->_credsUser.insert(c_ptr);
404 _pimpl->_userDirty =
true;
405 else if ((*ret.first)->password() != cred.
password())
407 _pimpl->_credsUser.erase(ret.first);
408 _pimpl->_credsUser.insert(c_ptr);
409 _pimpl->_userDirty =
true;
417 _pimpl->saveGlobalCredentials();
419 _pimpl->saveUserCredentials();
420 _pimpl->_globalDirty =
false;
421 _pimpl->_userDirty =
false;
443 c_ptr->setUrl(
Url());
452 creds,
_pimpl->_options.customCredFileDir / credFile, 0600);
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(); }
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.
bool empty() const
Test for an empty path.
#define USER_CREDENTIALS_FILE
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
int unlink(const Pathname &path)
Like 'unlink'.
Easy-to use interface to the ZYPP dependency resolver.
Url::asString() view options.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.