Check whether or not the client is permitted specified access to a path.
505 {
506 const char *authz = env ? env->Get("authz") : nullptr;
507
508
509
510
511 if (authz && !strncmp(authz, "Bearer%20", 9)) {
512 authz += 9;
513 }
514
515
516 if (!authz && Entity && !strcmp(
"ztn", Entity->
prot) && Entity->
creds &&
518 {
519 authz = Entity->
creds;
520 }
521 if (authz == nullptr) {
522 return OnMissing(Entity, path, oper, env);
523 }
524 m_log.Log(LogMask::Debug, "Access", "Trying token-based access control");
525 std::shared_ptr<XrdAccRules> access_rules;
526 uint64_t now = monotonic_time();
527 Check(now);
528 {
529 std::lock_guard<std::mutex> guard(m_mutex);
530 const auto iter = m_map.find(authz);
531 if (iter != m_map.end() && !iter->second->expired()) {
532 access_rules = iter->second;
533 }
534 }
535 if (!access_rules) {
536 m_log.Log(LogMask::Debug, "Access", "Token not found in recent cache; parsing.");
537 try {
538 uint64_t cache_expiry;
539 AccessRulesRaw rules;
540 std::string username;
541 std::string token_subject;
542 std::string issuer;
543 std::vector<MapRule> map_rules;
544 std::vector<std::string> groups;
545 uint32_t authz_strategy;
546 if (GenerateAcls(authz, cache_expiry, rules, username, token_subject, issuer, map_rules, groups, authz_strategy)) {
547 access_rules.reset(new XrdAccRules(now + cache_expiry, username, token_subject, issuer, map_rules, groups, authz_strategy));
548 access_rules->parse(rules);
549 } else {
550 m_log.Log(LogMask::Warning, "Access", "Failed to generate ACLs for token");
551 return OnMissing(Entity, path, oper, env);
552 }
553 if (m_log.getMsgMask() & LogMask::Debug) {
554 m_log.Log(LogMask::Debug, "Access", "New valid token", access_rules->str().c_str());
555 }
556 } catch (std::exception &exc) {
557 m_log.Log(LogMask::Warning, "Access", "Error generating ACLs for authorization", exc.what());
558 return OnMissing(Entity, path, oper, env);
559 }
560 std::lock_guard<std::mutex> guard(m_mutex);
561 m_map[authz] = access_rules;
562 } else if (m_log.getMsgMask() & LogMask::Debug) {
563 m_log.Log(LogMask::Debug, "Access", "Cached token", access_rules->str().c_str());
564 }
565
566
567
568
569
570
571
572
573
574
575
576
577 XrdSecEntity new_secentity;
578 new_secentity.
vorg =
nullptr;
579 new_secentity.
grps =
nullptr;
580 new_secentity.
role =
nullptr;
583 const auto &issuer = access_rules->get_issuer();
584 if (!issuer.empty()) {
585 new_secentity.
vorg = strdup(issuer.c_str());
586 }
587 bool group_success = false;
588 if ((access_rules->get_authz_strategy() & IssuerAuthz::Group) && access_rules->groups().size()) {
589 std::stringstream ss;
590 for (const auto &grp : access_rules->groups()) {
591 ss << grp << " ";
592 }
593 const auto &groups_str = ss.str();
594 new_secentity.
grps =
static_cast<char*
>(malloc(groups_str.size() + 1));
595 if (new_secentity.
grps) {
596 memcpy(new_secentity.
grps, groups_str.c_str(), groups_str.size());
597 new_secentity.
grps[groups_str.size()] =
'\0';
598 group_success = true;
599 }
600 }
601
602 std::string username;
603 bool mapping_success = false;
604 bool scope_success = false;
605 username = access_rules->get_username(path);
606
607 mapping_success = (access_rules->get_authz_strategy() & IssuerAuthz::Mapping) && !username.empty();
608 scope_success = (access_rules->get_authz_strategy() & IssuerAuthz::Capability) && access_rules->apply(oper, path);
609 if (scope_success && (m_log.getMsgMask() & LogMask::Debug)) {
610 std::stringstream ss;
611 ss << "Grant authorization based on scopes for operation=" << OpToName(oper) << ", path=" << path;
612 m_log.Log(LogMask::Debug, "Access", ss.str().c_str());
613 }
614
615 if (!scope_success && !mapping_success && !group_success) {
616 auto returned_accs = OnMissing(&new_secentity, path, oper, env);
617
618 if (new_secentity.
vorg !=
nullptr) free(new_secentity.
vorg);
619 if (new_secentity.
grps !=
nullptr) free(new_secentity.
grps);
620 if (new_secentity.
role !=
nullptr) free(new_secentity.
role);
621
622 return returned_accs;
623 }
624
625
626 if (scope_success && username.empty()) {
627 username = access_rules->get_default_username();
628 }
629
630
631
632 if (scope_success || mapping_success) {
633
634 Entity->
eaAPI->
Add(
"request.name", username,
true);
635 new_secentity.
eaAPI->
Add(
"request.name", username,
true);
636 m_log.Log(LogMask::Debug, "Access", "Request username", username.c_str());
637 }
638
639
640
641
642
643
644 const auto &token_subject = access_rules->get_token_subject();
645 if (!token_subject.empty()) {
646 Entity->
eaAPI->
Add(
"token.subject", token_subject,
true);
647 }
648
649
651
652
653
654
655 if (Entity->
secMon && scope_success && returned_op &&
Mon_isIO(oper))
656 Mon_Report(new_secentity, token_subject, username);
657
658
659 if (new_secentity.
vorg !=
nullptr) free(new_secentity.
vorg);
660 if (new_secentity.
grps !=
nullptr) free(new_secentity.
grps);
661 if (new_secentity.
role !=
nullptr) free(new_secentity.
role);
662
663 return returned_op;
664 }
bool Mon_isIO(const Access_Operation oper)
void Mon_Report(const XrdSecEntity &Entity, const std::string &subject, const std::string &username)
bool Add(XrdSecAttr &attr)
char * vorg
Entity's virtual organization(s)
int credslen
Length of the 'creds' data.
XrdNetAddrInfo * addrInfo
Entity's connection details.
XrdSecEntityAttr * eaAPI
non-const API to attributes
char prot[XrdSecPROTOIDSIZE]
Auth protocol used (e.g. krb5)
char * creds
Raw entity credentials or cert.
XrdSecMonitor * secMon
If !0 security monitoring enabled.
char * grps
Entity's group name(s)
char * role
Entity's role(s)