27 const std::string &GetSecName()
const {
return m_sec_name;}
28 const std::string &GetErrorMessage()
const {
return m_emsg;}
30 static int verify_before_s(
void *authz_ptr,
31 const unsigned char *pred,
34 static int verify_activity_s(
void *authz_ptr,
35 const unsigned char *pred,
38 static int verify_path_s(
void *authz_ptr,
39 const unsigned char *pred,
42 static int verify_name_s(
void *authz_ptr,
43 const unsigned char *pred,
47 int verify_before(
const unsigned char *pred,
size_t pred_sz);
48 int verify_activity(
const unsigned char *pred,
size_t pred_sz);
49 int verify_path(
const unsigned char *pred,
size_t pred_sz);
50 int verify_name(
const unsigned char *pred,
size_t pred_sz);
52 ssize_t m_max_duration;
55 const std::string m_path;
56 std::string m_desired_activity;
57 std::string m_sec_name;
65 int new_privs = privs;
113 int validate_verify_empty(
void *emsg_ptr,
114 const unsigned char *pred,
117 if ((pred_sz >= 5) && (!memcmp(
reinterpret_cast<const char *
>(pred),
"path:", 5) ||
118 !memcmp(
reinterpret_cast<const char *
>(pred),
"name:", 5)))
122 if ((pred_sz >= 9) && (!memcmp(
reinterpret_cast<const char *
>(pred),
"activity:", 9)))
133 : m_max_duration(86400),
135 m_log(log,
"macarons_"),
136 m_authz_behavior(static_cast<int>(
Handler::AuthzBehavior::PASSTHROUGH))
140 if (!
Handler::Config(config, &env, &m_log, m_location, m_secret, m_max_duration, behavior))
142 throw std::runtime_error(
"Macaroon authorization config failed.");
144 m_authz_behavior =
static_cast<int>(behavior);
149 Authz::OnMissing(
const XrdSecEntity *Entity,
const char *path,
152 switch (m_authz_behavior) {
153 case Handler::AuthzBehavior::PASSTHROUGH:
155 case Handler::AuthzBehavior::ALLOW:
157 case Handler::AuthzBehavior::DENY:
175 const char *authz = env ? env->
Get(
"authz") :
nullptr;
176 if (authz && !strncmp(authz,
"Bearer%20", 9))
182 if (!authz && Entity && !strcmp(
"ztn", Entity->
prot) && Entity->
creds &&
185 authz = Entity->
creds;
189 return OnMissing(Entity, path, oper, env);
192 macaroon_returncode mac_err = MACAROON_SUCCESS;
193 struct macaroon* macaroon = macaroon_deserialize(
200 return OnMissing(Entity, path, oper, env);
203 struct macaroon_verifier *verifier = macaroon_verifier_create();
206 m_log.
Emsg(
"Access",
"Failed to create a new macaroon verifier");
211 m_log.
Emsg(
"Access",
"Request with no provided path.");
212 macaroon_verifier_destroy(verifier);
216 AuthzCheck check_helper(path, oper, m_max_duration, m_log);
218 if (macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_before_s, &check_helper, &mac_err) ||
219 macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_activity_s, &check_helper, &mac_err) ||
220 macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_name_s, &check_helper, &mac_err) ||
221 macaroon_verifier_satisfy_general(verifier, AuthzCheck::verify_path_s, &check_helper, &mac_err))
223 m_log.
Emsg(
"Access",
"Failed to configure caveat verifier:");
224 macaroon_verifier_destroy(verifier);
228 const unsigned char *macaroon_loc;
230 macaroon_location(macaroon, &macaroon_loc, &location_sz);
231 if (strncmp(
reinterpret_cast<const char *
>(macaroon_loc), m_location.c_str(), location_sz))
233 std::string location_str(
reinterpret_cast<const char *
>(macaroon_loc), location_sz);
234 m_log.
Emsg(
"Access",
"Macaroon is for incorrect location", location_str.c_str());
235 macaroon_verifier_destroy(verifier);
236 macaroon_destroy(macaroon);
240 if (macaroon_verify(verifier, macaroon,
241 reinterpret_cast<const unsigned char *
>(m_secret.c_str()),
247 macaroon_verifier_destroy(verifier);
248 macaroon_destroy(macaroon);
251 macaroon_verifier_destroy(verifier);
253 const unsigned char *macaroon_id;
255 macaroon_identifier(macaroon, &macaroon_id, &id_sz);
257 std::string macaroon_id_str(
reinterpret_cast<const char *
>(macaroon_id), id_sz);
258 m_log.
Log(
LogMask::Info,
"Access",
"Macaroon verification successful; ID", macaroon_id_str.c_str());
259 macaroon_destroy(macaroon);
262 if (Entity && check_helper.GetSecName().size()) {
263 const std::string &username = check_helper.GetSecName();
264 m_log.
Log(
LogMask::Debug,
"Access",
"Setting the request name to", username.c_str());
265 Entity->
eaAPI->
Add(
"request.name", username,
true);
277 macaroon_returncode mac_err = MACAROON_SUCCESS;
278 std::unique_ptr<
struct macaroon, decltype(&macaroon_destroy)> macaroon(
279 macaroon_deserialize(token, &mac_err),
284 emsg =
"Failed to deserialize the token as a macaroon";
291 std::unique_ptr<
struct macaroon_verifier, decltype(&macaroon_verifier_destroy)> verifier(
292 macaroon_verifier_create(), &macaroon_verifier_destroy);
295 emsg =
"Internal error: failed to create a verifier.";
301 AuthzCheck check_helper(
"/",
AOP_Read, m_max_duration, m_log);
303 if (macaroon_verifier_satisfy_general(verifier.get(), AuthzCheck::verify_before_s, &check_helper, &mac_err) ||
304 macaroon_verifier_satisfy_general(verifier.get(), validate_verify_empty,
nullptr, &mac_err))
306 emsg =
"Failed to configure the verifier";
311 const unsigned char *macaroon_loc;
313 macaroon_location(macaroon.get(), &macaroon_loc, &location_sz);
314 if (strncmp(
reinterpret_cast<const char *
>(macaroon_loc), m_location.c_str(), location_sz))
316 emsg =
"Macaroon contains incorrect location: " +
317 std::string(
reinterpret_cast<const char *
>(macaroon_loc), location_sz);
322 if (macaroon_verify(verifier.get(), macaroon.get(),
323 reinterpret_cast<const unsigned char *
>(m_secret.c_str()),
328 emsg =
"Macaroon verification error" + (check_helper.GetErrorMessage().size() ?
329 (
", " + check_helper.GetErrorMessage()) :
"");
334 const unsigned char *macaroon_id;
336 macaroon_identifier(macaroon.get(), &macaroon_id, &id_sz);
337 m_log.
Log(
LogMask::Info,
"Validate", (
"Macaroon verification successful; ID " +
338 std::string(
reinterpret_cast<const char *
>(macaroon_id), id_sz)).c_str());
345 : m_max_duration(max_duration),
357 m_desired_activity =
"UPDATE_METADATA";
364 m_desired_activity =
"MANAGE";
369 m_desired_activity =
"UPLOAD";
372 m_desired_activity =
"DELETE";
375 m_desired_activity =
"DOWNLOAD";
378 m_desired_activity =
"LIST";
381 m_desired_activity =
"READ_METADATA";
387 AuthzCheck::verify_before_s(
void *authz_ptr,
388 const unsigned char *pred,
391 return static_cast<AuthzCheck*
>(authz_ptr)->verify_before(pred, pred_sz);
396 AuthzCheck::verify_activity_s(
void *authz_ptr,
397 const unsigned char *pred,
400 return static_cast<AuthzCheck*
>(authz_ptr)->verify_activity(pred, pred_sz);
405 AuthzCheck::verify_path_s(
void *authz_ptr,
406 const unsigned char *pred,
409 return static_cast<AuthzCheck*
>(authz_ptr)->verify_path(pred, pred_sz);
414 AuthzCheck::verify_name_s(
void *authz_ptr,
415 const unsigned char *pred,
418 return static_cast<AuthzCheck*
>(authz_ptr)->verify_name(pred, pred_sz);
423 AuthzCheck::verify_before(
const unsigned char * pred,
size_t pred_sz)
425 std::string pred_str(
reinterpret_cast<const char *
>(pred), pred_sz);
426 if (strncmp(
"before:", pred_str.c_str(), 7))
430 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"Checking macaroon for expiration; caveat:", pred_str.c_str());
433 if (strptime(&pred_str[7],
"%Y-%m-%dT%H:%M:%SZ", &caveat_tm) ==
nullptr)
435 m_emsg =
"Failed to parse time string: " + pred_str.substr(7);
439 caveat_tm.tm_isdst = -1;
441 time_t caveat_time = timegm(&caveat_tm);
442 if (-1 == caveat_time)
444 m_emsg =
"Failed to generate unix time: " + pred_str.substr(7);
448 if ((m_max_duration > 0) && (caveat_time > m_now + m_max_duration))
450 m_emsg =
"Max token age is greater than configured max duration; rejecting";
455 int result = (m_now >= caveat_time);
458 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"Macaroon has not expired.");
462 m_emsg =
"Macaroon expired at " + pred_str.substr(7);
470 AuthzCheck::verify_activity(
const unsigned char * pred,
size_t pred_sz)
472 if (!m_desired_activity.size()) {
return 1;}
473 std::string pred_str(
reinterpret_cast<const char *
>(pred), pred_sz);
474 if (strncmp(
"activity:", pred_str.c_str(), 9)) {
return 1;}
475 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"running verify activity", pred_str.c_str());
477 std::stringstream ss(pred_str.substr(9));
478 for (std::string activity;
std::getline(ss, activity,
','); )
481 if (m_desired_activity ==
"READ_METADATA") {
return 0;}
482 if ((activity == m_desired_activity) || ((m_desired_activity ==
"UPLOAD") && (activity ==
"MANAGE")))
484 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"macaroon has desired activity", activity.c_str());
488 m_log.Log(
LogMask::Info,
"AuthzCheck",
"macaroon does NOT have desired activity", m_desired_activity.c_str());
494 AuthzCheck::verify_path(
const unsigned char * pred,
size_t pred_sz)
496 std::string pred_str_raw(
reinterpret_cast<const char *
>(pred), pred_sz);
497 if (strncmp(
"path:", pred_str_raw.c_str(), 5)) {
return 1;}
499 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"running verify path", pred_str.c_str());
501 if ((m_path.find(
"/./") != std::string::npos) ||
502 (m_path.find(
"/../") != std::string::npos))
504 m_log.Log(
LogMask::Info,
"AuthzCheck",
"invalid requested path", m_path.c_str());
515 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"path request verified for", m_path.c_str());
524 const char *opName = (m_oper ==
AOP_Stat) ?
"READ_METADATA" :
"MKDIR";
526 (std::string(opName) + (is_subdir?
" Path request verified for" :
" Path request NOT allowed for")).c_str(),
531 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"path request NOT allowed", m_path.c_str());
539 AuthzCheck::verify_name(
const unsigned char * pred,
size_t pred_sz)
541 std::string pred_str(
reinterpret_cast<const char *
>(pred), pred_sz);
542 if (strncmp(
"name:", pred_str.c_str(), 5)) {
return 1;}
543 if (pred_str.size() < 6) {
return 1;}
544 m_log.Log(
LogMask::Debug,
"AuthzCheck",
"Verifying macaroon with", pred_str.c_str());
547 m_sec_name = pred_str.substr(5);
Access_Operation
The following are supported operations.
@ AOP_Delete
rm() or rmdir()
@ AOP_Update
open() r/w or append
@ AOP_Create
open() with create
@ AOP_Any
Special for getting privs.
@ AOP_Stat
exists(), stat()
@ AOP_Rename
mv() for source
@ AOP_Read
open() r/o, prepare()
@ AOP_Excl_Create
open() with O_EXCL|O_CREAT
@ AOP_Insert
mv() for target
@ AOP_Excl_Insert
mv() where destination doesn't exist.
static bool is_subdirectory(const std::string &dir, const std::string &subdir)
void getline(uchar *buff, int blen)
int emsg(int rc, char *msg)
virtual bool Validate(const char *token, std::string &emsg, long long *expT, XrdSecEntity *entP) override
Authz(XrdSysLogger *lp, const char *parms, XrdAccAuthorize *chain)
virtual XrdAccPrivs Access(const XrdSecEntity *Entity, const char *path, const Access_Operation oper, XrdOucEnv *env) override
static bool Config(const char *config, XrdOucEnv *env, XrdSysError *log, std::string &location, std::string &secret, ssize_t &max_duration, AuthzBehavior &behavior)
virtual XrdAccPrivs Access(const XrdSecEntity *Entity, const char *path, const Access_Operation oper, XrdOucEnv *Env=0)=0
char * Get(const char *varname)
bool Add(XrdSecAttr &attr)
int credslen
Length of the 'creds' data.
XrdSecEntityAttr * eaAPI
non-const API to attributes
char prot[XrdSecPROTOIDSIZE]
Auth protocol used (e.g. krb5)
char * creds
Raw entity credentials or cert.
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)
std::string NormalizeSlashes(const std::string &)