LSSTApplications  18.1.0
LSSTDataManagementBasePackage
DbAuth.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010, 2016 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) = "$Id$";
42 
43 #include "lsst/daf/base/Citizen.h"
45 
46 #include <cstdlib>
47 #include <fstream>
48 #include <memory>
49 
50 extern "C" {
51  #include <pwd.h>
52  #include <sys/types.h>
53  #include <sys/stat.h>
54  #include <unistd.h>
55 }
56 
57 #include "lsst/pex/exceptions.h"
58 
59 namespace dafBase = lsst::daf::base;
61 namespace pexPolicy = lsst::pex::policy;
62 
63 static pexPolicy::Policy::Ptr authPolicy(static_cast<pexPolicy::Policy*>(0));
64 
66 search(std::string const& host, std::string const& port) {
67  if (authPolicy == 0) {
68  passwd pwd;
69  passwd *pw;
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");
76  }
77  std::string dir = std::string(pw->pw_dir) + "/.lsst";
78  std::string filename = dir + "/db-auth.paf";
79  struct stat st;
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");
84  }
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");
89  }
90  {
92  authPolicy = pexPolicy::Policy::Ptr(new pexPolicy::Policy(filename));
93  }
94  }
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);
107  }
108  return std::pair<std::string, std::string>(username, password);
109  }
110  }
112  "No credentials found for host/port: " + host + ":" + port);
113  return std::pair<std::string, std::string>("", ""); // not reached
114 }
115 
121  authPolicy = pexPolicy::Policy::Ptr(new pexPolicy::Policy(*policy, true));
122 }
123 
128  authPolicy = nullptr;
129 }
130 
138  std::string const& port) {
139  try {
140  std::pair<std::string, std::string> result = search(host, port);
141  return true;
142  }
143  catch (...) {
144  return false;
145  }
146  return false; // not reached
147 }
148 
155  std::string const& port) {
156  std::pair<std::string, std::string> result = search(host, port);
157  return result.first + ":" + result.second;
158 }
159 
166  std::string const& port) {
167  std::pair<std::string, std::string> result = search(host, port);
168  return result.first;
169 }
170 
177  std::string const& port) {
178  std::pair<std::string, std::string> result = search(host, port);
179  return result.second;
180 }
T empty(T... args)
Interface for DbAuth class.
#define __attribute__(x)
Definition: DbAuth.cc:39
A PersistentCitizenScope object causes all Citizen objects created during its lifetime to be marked a...
Definition: Citizen.h:131
static bool available(std::string const &host, std::string const &port)
Determine whether an authenticator string is available for database access.
Definition: DbAuth.cc:137
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
py::object result
Definition: schema.cc:418
std::vector< Ptr > PolicyPtrArray
Definition: Policy.h:182
static void setPolicy(lsst::pex::policy::Policy::Ptr policy)
Set the authenticator Policy.
Definition: DbAuth.cc:119
STL class.
static void resetPolicy()
Set the authenticator Policy back to null.
Definition: DbAuth.cc:126
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
T get(T... args)
static std::string authString(std::string const &host, std::string const &port)
Get the authenticator string for a database.
Definition: DbAuth.cc:154
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
STL class.
T c_str(T... args)
static std::string username(std::string const &host, std::string const &port)
Get the username to use to authenticate to a database.
Definition: DbAuth.cc:165
static std::string password(std::string const &host, std::string const &port)
Get the password to use to authenticate to a database.
Definition: DbAuth.cc:176
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104