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> 32 namespace 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() );
72 : globalCredFilePath(rootdir /
MediaConfig::instance().credentialsGlobalFile())
73 , customCredFileDir(rootdir /
MediaConfig::instance().credentialsGlobalDir())
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" << endl;
152 DBG <<
"global cred file does not exist (" << _options.globalCredFilePath <<
")" << endl;
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" << endl;
174 DBG <<
"user cred file does not exist (" << _options.userCredFilePath <<
")" << endl;
176 _credsUser = _credsTmp; _credsTmp.clear();
177 DBG <<
"Got " << _credsUser.size() <<
" user records." << endl;
183 _credsTmp.insert(cred);
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;
224 result =
findIn(_credsGlobal, url, vopt);
228 result =
findIn(_credsUser, url, vopt);
231 DBG <<
"Found credentials for '" << url <<
"':" << endl << *result << endl;
233 DBG <<
"No credentials for '" << url <<
"'" << endl;
249 credfile = _options.customCredFileDir / file.
basename();
252 if ( pi.userMayR() )
try {
254 bpci::file_lock lockFile ( credfile.
c_str() );
255 bpci::scoped_lock lock( lockFile );
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 {
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;
336 if (credfile.empty())
337 return _pimpl->getCred(url);
338 return _pimpl->getCredFromFile(credfile);
343 {
return _pimpl->getCredFromFile(file); }
352 if (credfile.
empty())
366 if (credfile.
empty())
367 credfile =
_pimpl->_options.userCredFilePath;
370 if ( pi.isExist() && pi.isFile() )
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(); }
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
#define USER_CREDENTIALS_FILE
const char * c_str() const
String representation.
std::string basename() const
Return the last component of this path.
Url::asString() view options.
bool empty() const
Test for an empty path.
std::string asString() const
Returns a default string representation of the Url object.
std::string getQueryParam(const std::string ¶m, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
bool isExist() const
Return whether valid stat info exists.
zypp::media::AuthData AuthData
bool absolute() const
Test for an absolute path.
bool isValid() const
Verifies the Url.
int unlink(const Pathname &path)
Like 'unlink'.
Wrapper class for ::stat/::lstat.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.