XRootD
XrdMacaroons.cc
Go to the documentation of this file.
1 #include "XrdMacaroonsAuthz.hh"
2 #include "XrdMacaroonsHandler.hh"
3 
4 #include "XrdOuc/XrdOucEnv.hh"
5 #include "XrdOuc/XrdOucString.hh"
7 #include "XrdOuc/XrdOucEnv.hh"
8 #include "XrdSys/XrdSysError.hh"
9 #include "XrdSys/XrdSysLogger.hh"
12 #include "XrdVersion.hh"
13 
14 #include <stdexcept>
15 #include <dlfcn.h>
16 
20 
21 // Trick to access compiled version and directly call for the default object
22 // is taken from xrootd-scitokens.
23 static XrdVERSIONINFODEF(compiledVer, XrdAccTest, XrdVNUMBER, XrdVERSION);
25  const char *cfn,
26  const char *parm,
27  XrdVersionInfo &myVer);
28 
30 
31 extern "C" {
32 
34  const char *config,
35  const char *params,
36  XrdOucEnv * /*not used*/,
37  XrdAccAuthorize * chain_authz)
38 {
39  try
40  {
41  auto new_authz = new Macaroons::Authz(log, config, chain_authz);
42  SciTokensHelper = new_authz;
43  return new_authz;
44  }
45  catch (std::runtime_error &e)
46  {
47  XrdSysError err(log, "macaroons");
48  err.Emsg("Config", "Configuration of Macaroon authorization handler failed", e.what());
49  return nullptr;
50  }
51 }
52 
54  const char *config,
55  const char *parms)
56 {
57  XrdAccAuthorize *chain_authz = nullptr;
58  XrdSysError err(log, "macaroons");
59 
60  if (parms && parms[0]) {
61  XrdOucString parms_str(parms);
62  XrdOucString chained_lib;
63  int from = parms_str.tokenize(chained_lib, 0, ' ');
64  const char *chained_parms = nullptr;
65  err.Emsg("Config", "Will chain library", chained_lib.c_str());
66  if (from > 0)
67  {
68  parms_str.erasefromstart(from);
69  if (parms_str.length())
70  {
71  err.Emsg("Config", "Will chain parameters", parms_str.c_str());
72  chained_parms = parms_str.c_str();
73  }
74  }
75  char resolvePath[2048];
76  bool usedAltPath{true};
77  if (!XrdOucPinPath(chained_lib.c_str(), usedAltPath, resolvePath, 2048)) {
78  err.Emsg("Config", "Failed to locate appropriately versioned chained auth library:", parms);
79  return nullptr;
80  }
81  void *handle_base = dlopen(resolvePath, RTLD_LOCAL|RTLD_NOW);
82  if (handle_base == nullptr) {
83  err.Emsg("Config", "Failed to base plugin ", resolvePath, dlerror());
84  return nullptr;
85  }
86 
87  XrdAccAuthorize *(*ep)(XrdSysLogger *, const char *, const char *);
88  ep = (XrdAccAuthorize *(*)(XrdSysLogger *, const char *, const char *))
89  (dlsym(handle_base, "XrdAccAuthorizeObject"));
90  if (!ep)
91  {
92  dlclose(handle_base);
93  err.Emsg("Config", "Unable to chain second authlib after macaroons", parms);
94  return nullptr;
95  }
96 
97  chain_authz = (*ep)(log, config, chained_parms);
98 
99  if (chain_authz == nullptr) {
100  dlclose(handle_base);
101  err.Emsg("Config", "Unable to chain second authlib after macaroons "
102  "which returned nullptr");
103  return nullptr;
104  }
105  }
106  else
107  {
108  chain_authz = XrdAccDefaultAuthorizeObject(log, config, parms, compiledVer);
109  }
110  try
111  {
112  auto new_authz = new Macaroons::Authz(log, config, chain_authz);
113  SciTokensHelper = new_authz;
114  return new_authz;
115  }
116  catch (const std::runtime_error &e)
117  {
118  err.Emsg("Config", "Configuration of Macaroon authorization handler failed", e.what());
119  return nullptr;
120  }
121 }
122 
123 
125  XrdSysError *log, const char * config,
126  const char * parms, XrdOucEnv *env)
127 {
128  void *authz_raw = env->GetPtr("XrdAccAuthorize*");
129  XrdAccAuthorize *def_authz = static_cast<XrdAccAuthorize *>(authz_raw);
130 
131  log->Emsg("Initialize", "Creating new Macaroon handler object");
132  try
133  {
134  return new Macaroons::Handler(log, config, env, def_authz);
135  }
136  catch (std::runtime_error &e)
137  {
138  log->Emsg("Config", "Generation of Macaroon handler failed", e.what());
139  return nullptr;
140  }
141 }
142 
143 
144 }
XrdHttpExtHandler * XrdHttpGetExtHandler(XrdSysError *log, const char *config, const char *parms, XrdOucEnv *env)
XrdAccAuthorize * XrdAccAuthorizeObjAdd(XrdSysLogger *log, const char *config, const char *params, XrdOucEnv *, XrdAccAuthorize *chain_authz)
Definition: XrdMacaroons.cc:33
XrdSciTokensHelper * SciTokensHelper
Definition: XrdMacaroons.cc:29
static XrdVERSIONINFODEF(compiledVer, XrdAccTest, XrdVNUMBER, XrdVERSION)
XrdAccAuthorize * XrdAccAuthorizeObject(XrdSysLogger *log, const char *config, const char *parms)
Definition: XrdMacaroons.cc:53
XrdVERSIONINFO(XrdAccAuthorizeObject, XrdMacaroons)
XrdAccAuthorize * XrdAccDefaultAuthorizeObject(XrdSysLogger *lp, const char *cfn, const char *parm, XrdVersionInfo &myVer)
Definition: XrdAccAccess.cc:64
int XrdOucPinPath(const char *piPath, bool &noAltP, char *buff, int blen)
void * GetPtr(const char *varname)
Definition: XrdOucEnv.cc:281
const char * c_str() const
int erasefromstart(int sz=0)
int length() const
int tokenize(XrdOucString &tok, int from, char del=':')
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95