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;
337 std::string credfile =
url.getQueryParam(
"credentials");
338 if (credfile.empty())
340 return _pimpl->getCredFromFile(credfile);
345 {
return _pimpl->getCredFromFile(file); }
354 if (credfile.
empty())
364 if (
url.isValid() ) {
365 credfile =
url.getQueryParam(
"credentials");
368 if (credfile.
empty())
369 credfile =
_pimpl->_options.userCredFilePath;
385 std::pair<CredentialIterator, bool> ret =
_pimpl->_credsGlobal.insert(c_ptr);
387 _pimpl->_globalDirty =
true;
388 else if ((*ret.first)->password() != cred.
password())
390 _pimpl->_credsGlobal.erase(ret.first);
391 _pimpl->_credsGlobal.insert(c_ptr);
392 _pimpl->_globalDirty =
true;
404 std::pair<CredentialIterator, bool> ret =
_pimpl->_credsUser.insert(c_ptr);
406 _pimpl->_userDirty =
true;
407 else if ((*ret.first)->password() != cred.
password())
409 _pimpl->_credsUser.erase(ret.first);
410 _pimpl->_credsUser.insert(c_ptr);
411 _pimpl->_userDirty =
true;
419 _pimpl->saveGlobalCredentials();
421 _pimpl->saveUserCredentials();
422 _pimpl->_globalDirty =
false;
423 _pimpl->_userDirty =
false;
445 c_ptr->setUrl(
Url());
454 creds,
_pimpl->_options.customCredFileDir / credFile, 0600);
459 ERR <<
"error saving the credentials" << endl;
469 ERR <<
"could not delete user credentials file "
470 <<
_pimpl->_options.globalCredFilePath << endl;
471 _pimpl->_credsUser.clear();
476 ERR <<
"could not delete global credentials file"
477 <<
_pimpl->_options.userCredFilePath << endl;
478 _pimpl->_credsGlobal.clear();
484 {
return _pimpl->_credsGlobal.begin(); }
487 {
return _pimpl->_credsGlobal.end(); }
490 {
return _pimpl->_credsGlobal.size(); }
493 {
return _pimpl->_credsGlobal.empty(); }
497 {
return _pimpl->_credsUser.begin(); }
500 {
return _pimpl->_credsUser.end(); }
503 {
return _pimpl->_credsUser.size(); }
506 {
return _pimpl->_credsUser.empty(); }
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
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.