XRootD
Macaroons::Authz Class Referencefinal

#include <XrdMacaroonsAuthz.hh>

+ Inheritance diagram for Macaroons::Authz:
+ Collaboration diagram for Macaroons::Authz:

Public Member Functions

 Authz (XrdSysLogger *lp, const char *parms, XrdAccAuthorize *chain)
 
virtual ~Authz ()
 
virtual XrdAccPrivs Access (const XrdSecEntity *Entity, const char *path, const Access_Operation oper, XrdOucEnv *env) override
 
virtual int Audit (const int accok, const XrdSecEntity *Entity, const char *path, const Access_Operation oper, XrdOucEnv *Env) override
 
virtual Issuers IssuerList () override
 
virtual int Test (const XrdAccPrivs priv, const Access_Operation oper) override
 
virtual bool Validate (const char *token, std::string &emsg, long long *expT, XrdSecEntity *entP) override
 
- Public Member Functions inherited from XrdAccAuthorize
 XrdAccAuthorize ()
 Constructor. More...
 
virtual ~XrdAccAuthorize ()
 Destructor. More...
 
- Public Member Functions inherited from XrdSciTokensHelper
 XrdSciTokensHelper ()
 Constructor and Destructor. More...
 
virtual ~XrdSciTokensHelper ()
 

Additional Inherited Members

- Public Types inherited from XrdSciTokensHelper
typedef std::vector< ValidIssuerIssuers
 

Detailed Description

Definition at line 7 of file XrdMacaroonsAuthz.hh.

Constructor & Destructor Documentation

◆ Authz()

Authz::Authz ( XrdSysLogger lp,
const char *  parms,
XrdAccAuthorize chain 
)

Definition at line 126 of file XrdMacaroonsAuthz.cc.

127  : m_max_duration(86400),
128  m_chain(chain),
129  m_log(log, "macarons_"),
130  m_authz_behavior(static_cast<int>(Handler::AuthzBehavior::PASSTHROUGH))
131 {
132  Handler::AuthzBehavior behavior(Handler::AuthzBehavior::PASSTHROUGH);
133  XrdOucEnv env;
134  if (!Handler::Config(config, &env, &m_log, m_location, m_secret, m_max_duration, behavior))
135  {
136  throw std::runtime_error("Macaroon authorization config failed.");
137  }
138  m_authz_behavior = static_cast<int>(behavior);
139 }
static bool Config(const char *config, XrdOucEnv *env, XrdSysError *log, std::string &location, std::string &secret, ssize_t &max_duration, AuthzBehavior &behavior)

References Macaroons::Handler::Config().

+ Here is the call graph for this function:

◆ ~Authz()

virtual Macaroons::Authz::~Authz ( )
inlinevirtual

Definition at line 12 of file XrdMacaroonsAuthz.hh.

12 {}

Member Function Documentation

◆ Access()

XrdAccPrivs Authz::Access ( const XrdSecEntity Entity,
const char *  path,
const Access_Operation  oper,
XrdOucEnv Env 
)
overridevirtual

Check whether or not the client is permitted specified access to a path.

Parameters
Entity-> Authentication information
path-> The logical path which is the target of oper
oper-> The operation being attempted (see the enum above). If the oper is AOP_Any, then the actual privileges are returned and the caller may make subsequent tests using Test().
Env-> Environmental information at the time of the operation as supplied by the path CGI string. This is optional and the pointer may be zero.
Returns
Permit: a non-zero value (access is permitted) Deny: zero (access is denied)

Implements XrdAccAuthorize.

Definition at line 158 of file XrdMacaroonsAuthz.cc.

160 {
161  // We don't allow any testing to occur in this authz module, preventing
162  // a macaroon to be used to receive further macaroons.
163  if (oper == AOP_Any)
164  {
165  return m_chain ? m_chain->Access(Entity, path, oper, env) : XrdAccPriv_None;
166  }
167 
168  const char *authz = env ? env->Get("authz") : nullptr;
169  if (authz && !strncmp(authz, "Bearer%20", 9))
170  {
171  authz += 9;
172  }
173 
174  // If there's no request-specific token, check for a ZTN session token
175  if (!authz && Entity && !strcmp("ztn", Entity->prot) && Entity->creds &&
176  Entity->credslen && Entity->creds[Entity->credslen] == '\0')
177  {
178  authz = Entity->creds;
179  }
180 
181  if (!authz) {
182  return OnMissing(Entity, path, oper, env);
183  }
184 
185  macaroon_returncode mac_err = MACAROON_SUCCESS;
186  struct macaroon* macaroon = macaroon_deserialize(
187  authz,
188  &mac_err);
189  if (!macaroon)
190  {
191  // Do not log - might be other token type!
192  //m_log.Emsg("Access", "Failed to parse the macaroon");
193  return OnMissing(Entity, path, oper, env);
194  }
195 
196  struct macaroon_verifier *verifier = macaroon_verifier_create();
197  if (!verifier)
198  {
199  m_log.Emsg("Access", "Failed to create a new macaroon verifier");
200  return XrdAccPriv_None;
201  }
202  if (!path)
203  {
204  m_log.Emsg("Access", "Request with no provided path.");
205  macaroon_verifier_destroy(verifier);
206  return XrdAccPriv_None;
207  }
208 
209  AuthzCheck check_helper(path, oper, m_max_duration, m_log);
210 
211  if (macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_before_s, &check_helper, &mac_err) ||
212  macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_activity_s, &check_helper, &mac_err) ||
213  macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_name_s, &check_helper, &mac_err) ||
214  macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_path_s, &check_helper, &mac_err))
215  {
216  m_log.Emsg("Access", "Failed to configure caveat verifier:");
217  macaroon_verifier_destroy(verifier);
218  return XrdAccPriv_None;
219  }
220 
221  const unsigned char *macaroon_loc;
222  size_t location_sz;
223  macaroon_location(macaroon, &macaroon_loc, &location_sz);
224  if (strncmp(reinterpret_cast<const char *>(macaroon_loc), m_location.c_str(), location_sz))
225  {
226  std::string location_str(reinterpret_cast<const char *>(macaroon_loc), location_sz);
227  m_log.Emsg("Access", "Macaroon is for incorrect location", location_str.c_str());
228  macaroon_verifier_destroy(verifier);
229  macaroon_destroy(macaroon);
230  return m_chain ? m_chain->Access(Entity, path, oper, env) : XrdAccPriv_None;
231  }
232 
233  if (macaroon_verify(verifier, macaroon,
234  reinterpret_cast<const unsigned char *>(m_secret.c_str()),
235  m_secret.size(),
236  nullptr, 0, // discharge macaroons
237  &mac_err))
238  {
239  m_log.Log(LogMask::Debug, "Access", "Macaroon verification failed");
240  macaroon_verifier_destroy(verifier);
241  macaroon_destroy(macaroon);
242  // This token is from our server (location matched) but caveats or HMAC
243  // failed. Falling through to the chain would let a path-restricted
244  // macaroon bypass its own restrictions. Deny unconditionally.
245  return XrdAccPriv_None;
246  }
247  macaroon_verifier_destroy(verifier);
248 
249  const unsigned char *macaroon_id;
250  size_t id_sz;
251  macaroon_identifier(macaroon, &macaroon_id, &id_sz);
252 
253  std::string macaroon_id_str(reinterpret_cast<const char *>(macaroon_id), id_sz);
254  m_log.Log(LogMask::Info, "Access", "Macaroon verification successful; ID", macaroon_id_str.c_str());
255  macaroon_destroy(macaroon);
256 
257  // Copy the name, if present into the macaroon, into the credential object.
258  if (Entity && check_helper.GetSecName().size()) {
259  const std::string &username = check_helper.GetSecName();
260  m_log.Log(LogMask::Debug, "Access", "Setting the request name to", username.c_str());
261  Entity->eaAPI->Add("request.name", username,true);
262  }
263 
264  // We passed verification - give the correct privilege.
265  return AddPriv(oper, XrdAccPriv_None);
266 }
@ AOP_Any
Special for getting privs.
@ XrdAccPriv_None
Definition: XrdAccPrivs.hh:53
bool Debug
virtual XrdAccPrivs Access(const XrdSecEntity *Entity, const char *path, const Access_Operation oper, XrdOucEnv *Env=0)=0
bool Add(XrdSecAttr &attr)
int credslen
Length of the 'creds' data.
Definition: XrdSecEntity.hh:78
XrdSecEntityAttr * eaAPI
non-const API to attributes
Definition: XrdSecEntity.hh:92
char prot[XrdSecPROTOIDSIZE]
Auth protocol used (e.g. krb5)
Definition: XrdSecEntity.hh:67
char * creds
Raw entity credentials or cert.
Definition: XrdSecEntity.hh:77
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)
Definition: XrdSysError.hh:133

References XrdAccAuthorize::Access(), XrdSecEntityAttr::Add(), AOP_Any, XrdSecEntity::creds, XrdSecEntity::credslen, Debug, XrdSecEntity::eaAPI, XrdSysError::Emsg(), XrdOucEnv::Get(), TPC::Info, XrdSysError::Log(), XrdSecEntity::prot, and XrdAccPriv_None.

+ Here is the call graph for this function:

◆ Audit()

virtual int Macaroons::Authz::Audit ( const int  accok,
const XrdSecEntity Entity,
const char *  path,
const Access_Operation  oper,
XrdOucEnv Env 
)
inlineoverridevirtual

Route an audit message to the appropriate audit exit routine. See XrdAccAudit.h for more information on how the default implementation works. Currently, this method is not called by the ofs but should be used by the implementation to record denials or grants, as warranted.

Parameters
accok-> True is access was grated; false otherwise.
Entity-> Authentication information
path-> The logical path which is the target of oper
oper-> The operation being attempted (see above)
Env-> Environmental information at the time of the operation as supplied by the path CGI string. This is optional and the pointer may be zero.
Returns
Success: !0 information recorded. Failure: 0 information could not be recorded.

Implements XrdAccAuthorize.

Definition at line 26 of file XrdMacaroonsAuthz.hh.

29  {
30  return 0;
31  }

◆ IssuerList()

virtual Issuers Macaroons::Authz::IssuerList ( )
inlineoverridevirtual

Implements XrdSciTokensHelper.

Definition at line 41 of file XrdMacaroonsAuthz.hh.

41 {return Issuers();}
std::vector< ValidIssuer > Issuers

◆ Test()

virtual int Macaroons::Authz::Test ( const XrdAccPrivs  priv,
const Access_Operation  oper 
)
inlineoverridevirtual

Check whether the specified operation is permitted.

Parameters
priv-> the privileges as returned by Access().
oper-> The operation being attempted (see above)
Returns
Permit: a non-zero value (access is permitted) Deny: zero (access is denied)

Implements XrdAccAuthorize.

Definition at line 33 of file XrdMacaroonsAuthz.hh.

35  {
36  return 0;
37  }

◆ Validate()

bool Authz::Validate ( const char *  token,
std::string &  emsg,
long long *  expT,
XrdSecEntity entP 
)
overridevirtual

Validate a scitoken.

Parameters
token- Pointer to the token to validate.
emsg- Reference to a string to hold the reason for rejection
expT- Pointer to where the expiry value is to be placed. If nill, the value is not returned.
entP- Pointer to the SecEntity object and when not nil requests that it be filled with any identifying information in the token. The caller assumes that all supplied fields may be released by calling free().
Returns
Return true if the token is valid; false otherwise with emsg set.

Implements XrdSciTokensHelper.

Definition at line 268 of file XrdMacaroonsAuthz.cc.

272 {
273  macaroon_returncode mac_err = MACAROON_SUCCESS;
274  std::unique_ptr<struct macaroon, decltype(&macaroon_destroy)> macaroon(
275  macaroon_deserialize(token, &mac_err),
276  &macaroon_destroy);
277 
278  if (!macaroon)
279  {
280  emsg = "Failed to deserialize the token as a macaroon";
281  // Purposely log at debug level in case if this validation is ever
282  // chained so we don't have overly-chatty logs.
283  m_log.Log(LogMask::Debug, "Validate", emsg.c_str());
284  return false;
285  }
286 
287  std::unique_ptr<struct macaroon_verifier, decltype(&macaroon_verifier_destroy)> verifier(
288  macaroon_verifier_create(), &macaroon_verifier_destroy);
289  if (!verifier)
290  {
291  emsg = "Internal error: failed to create a verifier.";
292  m_log.Log(LogMask::Error, "Validate", emsg.c_str());
293  return false;
294  }
295 
296  // Note the path and operation here are ignored as we won't use those validators
297  AuthzCheck check_helper("/", AOP_Read, m_max_duration, m_log);
298 
299  if (macaroon_verifier_satisfy_general(verifier.get(), AuthzCheck::verify_before_s, &check_helper, &mac_err) ||
300  macaroon_verifier_satisfy_general(verifier.get(), validate_verify_empty, nullptr, &mac_err))
301  {
302  emsg = "Failed to configure the verifier";
303  m_log.Log(LogMask::Error, "Validate", emsg.c_str());
304  return false;
305  }
306 
307  const unsigned char *macaroon_loc;
308  size_t location_sz;
309  macaroon_location(macaroon.get(), &macaroon_loc, &location_sz);
310  if (strncmp(reinterpret_cast<const char *>(macaroon_loc), m_location.c_str(), location_sz))
311  {
312  emsg = "Macaroon contains incorrect location: " +
313  std::string(reinterpret_cast<const char *>(macaroon_loc), location_sz);
314  m_log.Log(LogMask::Warning, "Validate", emsg.c_str(), ("all.sitename is " + m_location).c_str());
315  return false;
316  }
317 
318  if (macaroon_verify(verifier.get(), macaroon.get(),
319  reinterpret_cast<const unsigned char *>(m_secret.c_str()),
320  m_secret.size(),
321  nullptr, 0,
322  &mac_err))
323  {
324  emsg = "Macaroon verification error" + (check_helper.GetErrorMessage().size() ?
325  (", " + check_helper.GetErrorMessage()) : "");
326  m_log.Log(LogMask::Warning, "Validate", emsg.c_str());
327  return false;
328  }
329 
330  const unsigned char *macaroon_id;
331  size_t id_sz;
332  macaroon_identifier(macaroon.get(), &macaroon_id, &id_sz);
333  m_log.Log(LogMask::Info, "Validate", ("Macaroon verification successful; ID " +
334  std::string(reinterpret_cast<const char *>(macaroon_id), id_sz)).c_str());
335 
336  return true;
337 }
@ AOP_Read
open() r/o, prepare()
int emsg(int rc, char *msg)
@ Error
@ Warning

References AOP_Read, Debug, emsg(), Error, TPC::Info, XrdSysError::Log(), and TPC::Warning.

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: