LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 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 "boost/scoped_array.hpp"
47 #include <cstdlib>
48 #include <fstream>
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;
60 namespace dafPersist = lsst::daf::persistence;
61 namespace pexPolicy = lsst::pex::policy;
62 
63 static pexPolicy::Policy::Ptr authPolicy(static_cast<pexPolicy::Policy*>(0));
64 
65 static std::pair<std::string, std::string>
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  boost::scoped_array<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");
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) {
82  throw LSST_EXCEPT(pexExcept::RuntimeError,
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) {
87  throw LSST_EXCEPT(pexExcept::RuntimeError,
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()) {
105  throw LSST_EXCEPT(pexExcept::RuntimeError,
106  "Empty username for host/port: " + host + ":" + port);
107  }
108  return std::pair<std::string, std::string>(username, password);
109  }
110  }
111  throw LSST_EXCEPT(pexExcept::RuntimeError,
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 
130 bool dafPersist::DbAuth::available(std::string const& host,
131  std::string const& port) {
132  try {
133  std::pair<std::string, std::string> result = search(host, port);
134  return true;
135  }
136  catch (...) {
137  return false;
138  }
139  return false; // not reached
140 }
141 
147 std::string dafPersist::DbAuth::authString(std::string const& host,
148  std::string const& port) {
149  std::pair<std::string, std::string> result = search(host, port);
150  return result.first + ":" + result.second;
151 }
152 
158 std::string dafPersist::DbAuth::username(std::string const& host,
159  std::string const& port) {
160  std::pair<std::string, std::string> result = search(host, port);
161  return result.first;
162 }
163 
169 std::string dafPersist::DbAuth::password(std::string const& host,
170  std::string const& port) {
171  std::pair<std::string, std::string> result = search(host, port);
172  return result.second;
173 }
Interface for DbAuth class.
std::vector< Ptr > PolicyPtrArray
Definition: Policy.h:182
static bool available(std::string const &host, std::string const &port)
Definition: DbAuth.cc:130
Include files required for standard LSST Exception handling.
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
static void setPolicy(lsst::pex::policy::Policy::Ptr policy)
Definition: DbAuth.cc:119
#define __attribute__(x)
Definition: DbAuth.cc:39
static std::string authString(std::string const &host, std::string const &port)
Definition: DbAuth.cc:147
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
static std::string username(std::string const &host, std::string const &port)
Definition: DbAuth.cc:158
static std::string password(std::string const &host, std::string const &port)
Definition: DbAuth.cc:169