39 # define __attribute__(x)
52 #include <sys/types.h>
59 namespace dafBase = lsst::daf::base;
60 namespace dafPersist = lsst::daf::persistence;
61 namespace pexPolicy = lsst::pex::policy;
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) {
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) {
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) {
88 filename +
" is missing or accessible by others");
95 int portNum = atoi(port.c_str());
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()) {
106 "Empty username for host/port: " + host +
":" + port);
108 return std::pair<std::string, std::string>(username, password);
112 "No credentials found for host/port: " + host +
":" + port);
113 return std::pair<std::string, std::string>(
"",
"");
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;
std::shared_ptr< Policy > Ptr
Interface for DbAuth class.
A PersistentCitizenScope object causes all Citizen objects created during its lifetime to be marked a...
std::vector< Ptr > PolicyPtrArray
static bool available(std::string const &host, std::string const &port)
Determine whether an authenticator string is available for database access.
Include files required for standard LSST Exception handling.
a container for holding hierarchical configuration data in memory.
static std::string password(std::string const &host, std::string const &port)
Get the password to use to authenticate to a database.
static void setPolicy(lsst::pex::policy::Policy::Ptr policy)
Set the authenticator Policy.
static std::string authString(std::string const &host, std::string const &port)
Get the authenticator string for a database.
static void resetPolicy()
Set the authenticator Policy back to null.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
static std::string username(std::string const &host, std::string const &port)
Get the username to use to authenticate to a database.