39 # define __attribute__(x)
43 #include "lsst/daf/base/Citizen.h"
52 #include <sys/types.h>
57 #include "lsst/pex/exceptions.h"
59 namespace dafBase = lsst::daf::base;
60 namespace dafPersist = lsst::daf::persistence;
61 namespace pexPolicy = lsst::pex::policy;
63 static pexPolicy::Policy::Ptr authPolicy(static_cast<pexPolicy::Policy*>(0));
65 static std::pair<std::string, std::string>
66 search(std::string
const& host, std::string
const& port) {
67 if (authPolicy == 0) {
70 long maxbuf = sysconf(_SC_GETPW_R_SIZE_MAX);
71 std::unique_ptr<char[]> buffer(
new char[maxbuf]);
72 int ret = getpwuid_r(geteuid(), &pwd, buffer.get(), maxbuf, &pw);
73 if (ret != 0 || pw->pw_dir == 0) {
74 throw LSST_EXCEPT(pexExcept::RuntimeError,
75 "Could not get home directory");
77 std::string dir = std::string(pw->pw_dir) +
"/.lsst";
78 std::string filename = dir +
"/db-auth.paf";
80 ret = stat(dir.c_str(), &st);
81 if (ret != 0 || (st.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
82 throw LSST_EXCEPT(pexExcept::RuntimeError,
83 dir +
" directory is missing or accessible by others");
85 ret = stat(filename.c_str(), &st);
86 if (ret != 0 || (st.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
87 throw LSST_EXCEPT(pexExcept::RuntimeError,
88 filename +
" is missing or accessible by others");
91 dafBase::PersistentCitizenScope scopeGuard;
92 authPolicy = pexPolicy::Policy::Ptr(
new pexPolicy::Policy(filename));
95 int portNum = atoi(port.c_str());
96 pexPolicy::Policy::PolicyPtrArray authArray =
97 authPolicy->getPolicyArray(
"database.authInfo");
98 for (pexPolicy::Policy::PolicyPtrArray::const_iterator i =
99 authArray.begin(); i != authArray.end(); ++i) {
100 if ((*i)->getString(
"host") == host &&
101 (*i)->getInt(
"port") == portNum) {
102 std::string username = (*i)->getString(
"user");
103 std::string password = (*i)->getString(
"password");
104 if (username.empty()) {
105 throw LSST_EXCEPT(pexExcept::RuntimeError,
106 "Empty username for host/port: " + host +
":" + port);
108 return std::pair<std::string, std::string>(username, password);
111 throw LSST_EXCEPT(pexExcept::RuntimeError,
112 "No credentials found for host/port: " + host +
":" + port);
113 return std::pair<std::string, std::string>(
"",
"");
120 dafBase::PersistentCitizenScope scopeGuard;
121 authPolicy = pexPolicy::Policy::Ptr(
new pexPolicy::Policy(*policy,
true));
127 dafBase::PersistentCitizenScope scopeGuard;
128 authPolicy =
nullptr;
138 std::string
const& port) {
140 std::pair<std::string, std::string> result = search(host, port);
155 std::string
const& port) {
156 std::pair<std::string, std::string> result = search(host, port);
157 return result.first +
":" + result.second;
166 std::string
const& port) {
167 std::pair<std::string, std::string> result = search(host, port);
177 std::string
const& port) {
178 std::pair<std::string, std::string> result = search(host, port);
179 return result.second;
Interface for DbAuth class.
static bool available(std::string const &host, std::string const &port)
Determine whether an authenticator string is available for database access.
static void setPolicy(lsst::pex::policy::Policy::Ptr policy)
Set the authenticator Policy.
static void resetPolicy()
Set the authenticator Policy back to null.
static std::string authString(std::string const &host, std::string const &port)
Get the authenticator string for a database.
static std::string username(std::string const &host, std::string const &port)
Get the username to use to authenticate to a database.
static std::string password(std::string const &host, std::string const &port)
Get the password to use to authenticate to a database.