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");
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() )
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;
233 DBG <<
"No credentials for '" << url <<
"'" << endl;
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_(it, creds.begin(), creds.end())
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;
336 if (credfile.empty())
352 if (credfile.
empty())
366 if (credfile.
empty())
370 if ( pi.isExist() && pi.isFile() )
386 else if ((*ret.first)->password() != cred.
password())
405 else if ((*ret.first)->password() != cred.
password())
443 c_ptr->setUrl(
Url());
457 ERR <<
"error saving the credentials" << endl;
467 ERR <<
"could not delete user credentials file " 474 ERR <<
"could not delete global credentials file"
#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
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
const char * c_str() const
String representation.
std::string basename() const
Return the last component of this path.
Url::asString() view options.
zypp::RepoManager::RefreshServiceOptions _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.
Easy-to use interface to the ZYPP dependency resolver.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.