1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/CredentialManager.h
15 #include "zypp/base/Function.h"
16 #include "zypp/base/Logger.h"
17 #include "zypp/base/Easy.h"
18 #include "zypp/PathInfo.h"
20 #include "zypp/media/CredentialFileReader.h"
22 #include "zypp/media/CredentialManager.h"
24 #define CUSTOM_CREDENTIALS_FILE_DIR "/etc/zypp/credentials.d"
25 #define GLOBAL_CREDENTIALS_FILE "/etc/zypp/credentials.cat"
26 #define USER_CREDENTIALS_FILE ".zypp/credentials.cat"
30 //////////////////////////////////////////////////////////////////////
32 { ////////////////////////////////////////////////////////////////////
33 //////////////////////////////////////////////////////////////////////
35 { ////////////////////////////////////////////////////////////////////
38 //////////////////////////////////////////////////////////////////////
40 // CLASS NAME : CredManagerOptions
42 //////////////////////////////////////////////////////////////////////
44 CredManagerOptions::CredManagerOptions(const Pathname & rootdir)
45 : globalCredFilePath(rootdir / GLOBAL_CREDENTIALS_FILE)
46 , customCredFileDir(rootdir / CUSTOM_CREDENTIALS_FILE_DIR)
48 char * homedir = getenv("HOME");
50 userCredFilePath = rootdir / homedir / USER_CREDENTIALS_FILE;
54 //////////////////////////////////////////////////////////////////////
56 // CLASS NAME : CredentialManager::Impl
58 struct CredentialManager::Impl
60 Impl(const CredManagerOptions & options);
65 void init_globalCredentials();
66 void init_userCredentials();
68 bool processCredentials(AuthData_Ptr & cred);
70 AuthData_Ptr getCred(const Url & url) const;
71 AuthData_Ptr getCredFromFile(const Pathname & file);
72 void saveGlobalCredentials();
73 void saveUserCredentials();
76 CredManagerOptions _options;
78 CredentialSet _credsGlobal;
79 CredentialSet _credsUser;
80 CredentialSet _credsTmp;
82 //////////////////////////////////////////////////////////////////////
85 //////////////////////////////////////////////////////////////////////
87 // CLASS NAME : CredentialManager::Impl
89 //////////////////////////////////////////////////////////////////////
91 CredentialManager::Impl::Impl(const CredManagerOptions & options)
94 init_globalCredentials();
95 init_userCredentials();
99 void CredentialManager::Impl::init_globalCredentials()
101 if (_options.globalCredFilePath.empty())
102 DBG << "global cred file not known";
103 else if (PathInfo(_options.globalCredFilePath).isExist())
105 /* list<Pathname> entries;
106 if (filesystem::readdir(entries, _options.globalCredFilePath, false) != 0)
107 ZYPP_THROW(Exception("failed to read directory"));
109 for_(it, entries.begin(), entries.end())*/
111 CredentialFileReader(_options.globalCredFilePath,
112 bind(&Impl::processCredentials, this, _1));
115 DBG << "global cred file does not exist";
117 _credsGlobal = _credsTmp; _credsTmp.clear();
118 DBG << "Got " << _credsGlobal.size() << " global records." << endl;
122 void CredentialManager::Impl::init_userCredentials()
124 if (_options.userCredFilePath.empty())
125 DBG << "user cred file not known";
126 else if (PathInfo(_options.userCredFilePath).isExist())
128 /* list<Pathname> entries;
129 if (filesystem::readdir(entries, _options.userCredFilePath, false ) != 0)
130 ZYPP_THROW(Exception("failed to read directory"));
132 for_(it, entries.begin(), entries.end())*/
133 CredentialFileReader(_options.userCredFilePath,
134 bind(&Impl::processCredentials, this, _1));
137 DBG << "user cred file does not exist";
139 _credsUser = _credsTmp; _credsTmp.clear();
140 DBG << "Got " << _credsUser.size() << " user records." << endl;
144 bool CredentialManager::Impl::processCredentials(AuthData_Ptr & cred)
146 _credsTmp.insert(cred);
151 static AuthData_Ptr findIn(const CredentialManager::CredentialSet & set,
153 url::ViewOption vopt)
155 for(CredentialManager::CredentialIterator it = set.begin(); it != set.end(); ++it)
157 if (url.asString(vopt) == (*it)->url().asString(vopt))
161 return AuthData_Ptr();
165 AuthData_Ptr CredentialManager::Impl::getCred(const Url & url) const
169 // compare the urls via asString(), but ignore password
170 // default url::ViewOption will take care of that.
171 // operator==(Url,Url) compares the whole Url
173 // if the wanted URL does not contain username, ignore that, too
174 url::ViewOption vopt;
175 // if (url.getUsername().empty())
176 vopt = vopt - url::ViewOption::WITH_USERNAME;
178 // search in global credentials
179 result = findIn(_credsGlobal, url, vopt);
181 // search in home credentials
183 result = findIn(_credsUser, url, vopt);
186 DBG << "Found credentials for '" << url << "':" << endl << *result;
188 DBG << "No credentials for '" << url << "'" << endl;
194 AuthData_Ptr CredentialManager::Impl::getCredFromFile(const Pathname & file)
200 // get from that file
203 // get from /etc/zypp/credentials.d
204 credfile = _options.customCredFileDir / file;
206 CredentialFileReader(credfile, bind(&Impl::processCredentials, this, _1));
207 if (_credsTmp.empty())
208 WAR << file << " does not contain valid credentials or is not readable." << endl;
211 result = *_credsTmp.begin();
218 static int save_creds_in_file(
219 const CredentialManager::CredentialSet creds,
220 const Pathname & file,
224 filesystem::assert_dir(file.dirname());
226 std::ofstream fs(file.c_str());
230 for_(it, creds.begin(), creds.end())
232 (*it)->dumpAsIniOn(fs);
237 filesystem::chmod(file, mode);
242 void CredentialManager::Impl::saveGlobalCredentials()
244 save_creds_in_file(_credsGlobal, _options.globalCredFilePath, 0640);
247 void CredentialManager::Impl::saveUserCredentials()
249 save_creds_in_file(_credsUser, _options.userCredFilePath, 0600);
253 //////////////////////////////////////////////////////////////////////
255 // CLASS NAME : CredentialManager
257 //////////////////////////////////////////////////////////////////////
259 CredentialManager::CredentialManager(const CredManagerOptions & opts)
260 : _pimpl(new Impl(opts))
264 AuthData_Ptr CredentialManager::getCred(const Url & url)
265 { return _pimpl->getCred(url); }
268 AuthData_Ptr CredentialManager::getCredFromFile(const Pathname & file)
269 { return _pimpl->getCredFromFile(file); }
272 void CredentialManager::save(const AuthData & cred, bool global)
273 { global ? saveInGlobal(cred) : saveInUser(cred); }
276 void CredentialManager::saveInGlobal(const AuthData & cred)
279 c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
280 _pimpl->_credsGlobal.insert(c_ptr); //! \todo avoid adding duplicates
281 _pimpl->saveGlobalCredentials();
285 void CredentialManager::saveInUser(const AuthData & cred)
288 c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
289 _pimpl->_credsUser.insert(c_ptr); //! \todo avoid adding duplicates
290 _pimpl->saveUserCredentials();
294 void CredentialManager::saveIn(const AuthData & cred, const Pathname & credFile)
297 c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
298 CredentialManager::CredentialSet creds;
302 if (credFile.absolute())
303 ret = save_creds_in_file(creds, credFile, 0640);
305 ret = save_creds_in_file(
306 creds, _pimpl->_options.customCredFileDir / credFile, 0600);
310 //! \todo figure out the reason(?), call back to user
311 ERR << "error saving the credentials" << endl;
316 void CredentialManager::clearAll(bool global)
320 if (!filesystem::unlink(_pimpl->_options.globalCredFilePath))
321 ERR << "could not delete user credentials file "
322 << _pimpl->_options.globalCredFilePath << endl;
323 _pimpl->_credsUser.clear();
327 if (!filesystem::unlink(_pimpl->_options.userCredFilePath))
328 ERR << "could not delete global credentials file"
329 << _pimpl->_options.userCredFilePath << endl;
330 _pimpl->_credsGlobal.clear();
335 CredentialManager::CredentialIterator CredentialManager::credsGlobalBegin() const
336 { return _pimpl->_credsGlobal.begin(); }
338 CredentialManager::CredentialIterator CredentialManager::credsGlobalEnd() const
339 { return _pimpl->_credsGlobal.end(); }
341 CredentialManager::CredentialSize CredentialManager::credsGlobalSize() const
342 { return _pimpl->_credsGlobal.size(); }
344 bool CredentialManager::credsGlobalEmpty() const
345 { return _pimpl->_credsGlobal.empty(); }
348 CredentialManager::CredentialIterator CredentialManager::credsUserBegin() const
349 { return _pimpl->_credsUser.begin(); }
351 CredentialManager::CredentialIterator CredentialManager::credsUserEnd() const
352 { return _pimpl->_credsUser.end(); }
354 CredentialManager::CredentialSize CredentialManager::credsUserSize() const
355 { return _pimpl->_credsUser.size(); }
357 bool CredentialManager::credsUserEmpty() const
358 { return _pimpl->_credsUser.empty(); }
361 ////////////////////////////////////////////////////////////////////
363 //////////////////////////////////////////////////////////////////////
364 ////////////////////////////////////////////////////////////////////
366 //////////////////////////////////////////////////////////////////////