456 enum class AuthzBehavior {
465 m_parms(parms ? parms :
""),
466 m_next_clean(monotonic_time() + m_expiry_secs),
467 m_log(lp,
"scitokens_")
469 pthread_rwlock_init(&m_config_lock,
nullptr);
470 m_config_lock_initialized =
true;
471 m_log.Say(
"++++++ XrdAccSciTokens: Initialized SciTokens-based authorization.");
473 throw std::runtime_error(
"Failed to configure SciTokens authorization.");
478 if (m_config_lock_initialized) {
479 pthread_rwlock_destroy(&m_config_lock);
488 std::vector<std::string_view> authz_list;
489 authz_list.reserve(1);
495 ParseTokenString(
"authz", env, authz_list);
496 ParseTokenString(
"access_token", env, authz_list);
498 if (Entity && !strcmp(
"ztn", Entity->
prot) && Entity->
creds &&
501 authz_list.push_back(Entity->
creds);
504 if (authz_list.empty()) {
505 return OnMissing(Entity, path, oper, env);
510 if (authz_list.size() > 10) {
511 m_log.Log(LogMask::Warning,
"Access",
"Request had more than 10 tokens attached; ignoring");
512 return OnMissing(Entity, path, oper, env);
515 m_log.Log(LogMask::Debug,
"Access",
"Trying token-based access control");
516 std::vector<std::shared_ptr<XrdAccRules>> access_rules_list;
517 uint64_t now = monotonic_time();
519 for (
const auto &authz : authz_list) {
520 std::shared_ptr<XrdAccRules> access_rules;
522 std::lock_guard<std::mutex> guard(m_mutex);
523 const auto iter = m_map.find(authz);
524 if (iter != m_map.end() && !iter->second->expired()) {
525 access_rules = iter->second;
529 m_log.Log(LogMask::Debug,
"Access",
"Token not found in recent cache; parsing.");
531 uint64_t cache_expiry;
533 std::string username;
534 std::string token_subject;
536 std::vector<MapRule> map_rules;
537 std::vector<std::string> groups;
538 uint32_t authz_strategy;
540 if (GenerateAcls(authz, cache_expiry, rules, username, token_subject, issuer, map_rules, groups, authz_strategy, acceptable_authz)) {
541 access_rules.reset(
new XrdAccRules(now + cache_expiry, username, token_subject, issuer, map_rules, groups, authz_strategy, acceptable_authz));
542 access_rules->parse(rules);
544 m_log.Log(LogMask::Warning,
"Access",
"Failed to generate ACLs for token");
547 if (m_log.getMsgMask() & LogMask::Debug) {
548 m_log.Log(LogMask::Debug,
"Access",
"New valid token", access_rules->str().c_str());
550 }
catch (std::exception &exc) {
551 m_log.Log(LogMask::Warning,
"Access",
"Error generating ACLs for authorization", exc.what());
554 std::lock_guard<std::mutex> guard(m_mutex);
555 m_map[std::string(authz)] = access_rules;
556 }
else if (m_log.getMsgMask() & LogMask::Debug) {
557 m_log.Log(LogMask::Debug,
"Access",
"Cached token", access_rules->str().c_str());
559 access_rules_list.push_back(access_rules);
561 if (access_rules_list.empty()) {
562 return OnMissing(Entity, path, oper, env);
564 std::string_view path_view(path, strlen(path));
568 return OnMissing(Entity, path, oper, env);
582 for (
const auto &access_rules : access_rules_list) {
584 if (!access_rules->acceptable_authz(oper)) {
585 m_log.Log(LogMask::Debug,
"Access",
"Issuer is not acceptable for given operation:", access_rules->get_issuer().c_str());
590 new_secentity.
vorg =
nullptr;
591 new_secentity.
grps =
nullptr;
592 new_secentity.
role =
nullptr;
595 const auto &issuer = access_rules->get_issuer();
596 if (!issuer.empty()) {
597 new_secentity.
vorg = strdup(issuer.c_str());
599 bool group_success =
false;
600 if ((access_rules->get_authz_strategy() &
IssuerAuthz::Group) && access_rules->groups().size()) {
601 std::stringstream ss;
602 for (
const auto &grp : access_rules->groups()) {
605 const auto &groups_str = ss.str();
606 new_secentity.
grps =
static_cast<char*
>(malloc(groups_str.size() + 1));
607 if (new_secentity.
grps) {
608 memcpy(new_secentity.
grps, groups_str.c_str(), groups_str.size());
609 new_secentity.
grps[groups_str.size()] =
'\0';
611 group_success =
true;
614 std::string username;
615 bool mapping_success =
false;
616 bool scope_success =
false;
617 username = access_rules->get_username(path_view);
619 mapping_success = (access_rules->get_authz_strategy() &
IssuerAuthz::Mapping) && !username.empty();
620 scope_success = (access_rules->get_authz_strategy() &
IssuerAuthz::Capability) && access_rules->apply(oper, path_view);
621 if (scope_success && (m_log.getMsgMask() & LogMask::Debug)) {
622 std::stringstream ss;
623 ss <<
"Grant authorization based on scopes for operation=" << OpToName(oper) <<
", path=" << path;
624 m_log.Log(LogMask::Debug,
"Access", ss.str().c_str());
627 if (!scope_success && !mapping_success && !group_success) {
628 auto returned_accs = OnMissing(&new_secentity, path, oper, env);
630 if (new_secentity.
vorg !=
nullptr) free(new_secentity.
vorg);
631 if (new_secentity.
grps !=
nullptr) free(new_secentity.
grps);
632 if (new_secentity.
role !=
nullptr) free(new_secentity.
role);
634 return returned_accs;
638 if (scope_success && username.empty()) {
639 username = access_rules->get_default_username();
644 if (scope_success || mapping_success) {
646 Entity->
eaAPI->
Add(
"request.name", username,
true);
647 new_secentity.
eaAPI->
Add(
"request.name", username,
true);
648 m_log.Log(LogMask::Debug,
"Access",
"Request username", username.c_str());
656 const auto &token_subject = access_rules->get_token_subject();
657 if (!token_subject.empty()) {
658 Entity->
eaAPI->
Add(
"token.subject", token_subject,
true);
667 if (Entity->
secMon && scope_success && returned_op &&
Mon_isIO(oper))
668 Mon_Report(new_secentity, token_subject, username);
671 if (new_secentity.
vorg !=
nullptr) free(new_secentity.
vorg);
672 if (new_secentity.
grps !=
nullptr) free(new_secentity.
grps);
673 if (new_secentity.
role !=
nullptr) free(new_secentity.
role);
678 return OnMissing(Entity, path, oper, env);
692 pthread_rwlock_rdlock(&m_config_lock);
694 for (
const auto &it: m_issuers) {
695 issuers.push_back({it.first, it.second.m_url});
698 pthread_rwlock_unlock(&m_config_lock);
701 pthread_rwlock_unlock(&m_config_lock);
706 virtual bool Validate(
const char *token, std::string &
emsg,
long long *expT,
714 if (!strncmp(token,
"Bearer%20", 9)) token += 9;
715 pthread_rwlock_rdlock(&m_config_lock);
716 auto retval = scitoken_deserialize(token, &scitoken, &m_valid_issuers_array[0], &err_msg);
717 pthread_rwlock_unlock(&m_config_lock);
720 m_log.Log(LogMask::Warning,
"Validate",
"Failed to deserialize SciToken:", err_msg);
731 {
char *value =
nullptr;
732 if (!scitoken_get_claim_string(scitoken,
"sub", &value, &err_msg)) {
733 Entity->
name = strdup(value);
742 if (expT && scitoken_get_expiration(scitoken, expT, &err_msg)) {
745 scitoken_destroy(scitoken);
751 scitoken_destroy(scitoken);
769 return (m_chain ? m_chain->Test(priv, oper) : 0);
780 switch (m_authz_behavior) {
781 case AuthzBehavior::PASSTHROUGH:
783 case AuthzBehavior::ALLOW:
785 case AuthzBehavior::DENY:
792 bool GenerateAcls(
const std::string_view &authz, uint64_t &cache_expiry,
AccessRulesRaw &rules, std::string &username, std::string &token_subject, std::string &issuer, std::vector<MapRule> &map_rules, std::vector<std::string> &groups, uint32_t &authz_strategy,
AuthzSetting &acceptable_authz) {
795 bool looks_good =
true;
796 int separator_count = 0;
797 for (
auto cur_char = authz.data(); *cur_char; cur_char++) {
798 if (*cur_char ==
'.') {
800 if (separator_count > 2) {
804 if (!(*cur_char >= 65 && *cur_char <= 90) &&
805 !(*cur_char >= 97 && *cur_char <= 122) &&
806 !(*cur_char >= 48 && *cur_char <= 57) &&
807 (*cur_char != 43) && (*cur_char != 47) &&
808 (*cur_char != 45) && (*cur_char != 95))
814 if ((separator_count != 2) || (!looks_good)) {
815 m_log.Log(LogMask::Debug,
"Parse",
"Token does not appear to be a valid JWT; skipping.");
820 SciToken token =
nullptr;
821 pthread_rwlock_rdlock(&m_config_lock);
822 auto retval = scitoken_deserialize(authz.data(), &token, &m_valid_issuers_array[0], &err_msg);
823 pthread_rwlock_unlock(&m_config_lock);
826 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to deserialize SciToken:", err_msg);
832 if (scitoken_get_expiration(token, &expiry, &err_msg)) {
833 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Unable to determine token expiration:", err_msg);
835 scitoken_destroy(token);
839 const auto now_wall =
static_cast<long long>(std::time(
nullptr));
840 const auto remaining = expiry - now_wall;
841 if (remaining <= 0) {
842 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Token already expired.");
843 scitoken_destroy(token);
846 expiry = std::min(
static_cast<int64_t
>(remaining),
847 static_cast<int64_t
>(m_expiry_secs));
849 expiry = m_expiry_secs;
852 char *value =
nullptr;
853 if (scitoken_get_claim_string(token,
"iss", &value, &err_msg)) {
854 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get issuer:", err_msg);
855 scitoken_destroy(token);
859 std::string token_issuer(value);
862 pthread_rwlock_rdlock(&m_config_lock);
863 auto enf = enforcer_create(token_issuer.c_str(), &m_audiences_array[0], &err_msg);
864 pthread_rwlock_unlock(&m_config_lock);
866 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to create an enforcer:", err_msg);
867 scitoken_destroy(token);
873 if (enforcer_generate_acls(enf, token, &acls, &err_msg)) {
874 scitoken_destroy(token);
875 enforcer_destroy(enf);
876 m_log.Log(LogMask::Warning,
"GenerateAcls",
"ACL generation from SciToken failed:", err_msg);
880 enforcer_destroy(enf);
884 ~AclGuard() {
if (ptr) enforcer_acl_free(ptr); }
887 pthread_rwlock_rdlock(&m_config_lock);
888 auto iter = m_issuers.find(token_issuer);
889 if (iter == m_issuers.end()) {
890 pthread_rwlock_unlock(&m_config_lock);
891 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Authorized issuer without a config.");
892 scitoken_destroy(token);
895 const auto config = iter->second;
896 pthread_rwlock_unlock(&m_config_lock);
900 std::vector<std::string> groups_parsed;
901 if (scitoken_get_claim_string_list(token, config.m_groups_claim.c_str(), &group_list, &err_msg) == 0) {
902 for (
int idx=0; group_list[idx]; idx++) {
903 groups_parsed.emplace_back(group_list[idx]);
905 scitoken_free_string_list(group_list);
908 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get token groups:", err_msg);
912 if (scitoken_get_claim_string(token,
"sub", &value, &err_msg)) {
913 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get token subject:", err_msg);
915 scitoken_destroy(token);
918 token_subject = std::string(value);
921 auto tmp_username = token_subject;
922 if (!config.m_username_claim.empty()) {
923 if (scitoken_get_claim_string(token, config.m_username_claim.c_str(), &value, &err_msg)) {
924 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get token username:", err_msg);
926 scitoken_destroy(token);
929 tmp_username = std::string(value);
931 if (!IsSafeUsername(tmp_username)) {
932 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Token username claim contains unsafe characters; rejecting:", tmp_username.c_str());
933 scitoken_destroy(token);
936 }
else if (!config.m_map_subject) {
937 tmp_username = config.m_default_user;
940 for (
auto rule : config.m_map_rules) {
941 for (
auto path : config.m_base_paths) {
942 auto path_rule = rule;
943 path_rule.m_path_prefix = path + rule.m_path_prefix;
944 auto pos = path_rule.m_path_prefix.find(
"//");
945 if (pos != std::string::npos) {
946 path_rule.m_path_prefix.erase(pos + 1, 1);
948 map_rules.emplace_back(path_rule);
954 std::set<std::string> paths_write_seen;
955 std::set<std::string> paths_create_or_modify_seen;
956 std::vector<std::string> acl_paths;
957 acl_paths.reserve(config.m_restricted_paths.size() + 1);
958 while (acls[idx].resource && acls[idx++].authz) {
960 const auto &acl_path = acls[idx-1].resource;
961 const auto &acl_authz = acls[idx-1].authz;
962 if (config.m_restricted_paths.empty()) {
963 acl_paths.push_back(acl_path);
965 auto acl_path_size = strlen(acl_path);
966 for (
const auto &restricted_path : config.m_restricted_paths) {
969 if (!strncmp(acl_path, restricted_path.c_str(), restricted_path.size())) {
972 if (acl_path_size > restricted_path.size() && acl_path[restricted_path.size()] !=
'/') {
975 acl_paths.push_back(acl_path);
981 if (!strncmp(acl_path, restricted_path.c_str(), acl_path_size)) {
988 if ((restricted_path.size() > acl_path_size && restricted_path[acl_path_size] !=
'/') && (acl_path_size != 1)) {
991 acl_paths.push_back(restricted_path);
995 for (
const auto &acl_path : acl_paths) {
996 for (
const auto &base_path : config.m_base_paths) {
997 if (!acl_path[0] || acl_path[0] !=
'/') {
continue;}
999 MakeCanonical(base_path + acl_path, path);
1000 if (!strcmp(acl_authz,
"read")) {
1001 xrd_rules.emplace_back(
AOP_Read, path);
1003 xrd_rules.emplace_back(
AOP_Stat, path);
1004 }
else if (!strcmp(acl_authz,
"create")) {
1005 paths_create_or_modify_seen.insert(path);
1007 xrd_rules.emplace_back(
AOP_Mkdir, path);
1010 xrd_rules.emplace_back(
AOP_Stat, path);
1011 }
else if (!strcmp(acl_authz,
"modify")) {
1012 paths_create_or_modify_seen.insert(path);
1014 xrd_rules.emplace_back(
AOP_Mkdir, path);
1018 xrd_rules.emplace_back(
AOP_Chmod, path);
1019 xrd_rules.emplace_back(
AOP_Stat, path);
1021 }
else if (!strcmp(acl_authz,
"write")) {
1022 paths_write_seen.insert(path);
1027 for (
const auto &write_path : paths_write_seen) {
1028 if (paths_create_or_modify_seen.find(write_path) == paths_create_or_modify_seen.end()) {
1030 xrd_rules.emplace_back(
AOP_Create, write_path);
1031 xrd_rules.emplace_back(
AOP_Mkdir, write_path);
1032 xrd_rules.emplace_back(
AOP_Rename, write_path);
1033 xrd_rules.emplace_back(
AOP_Insert, write_path);
1034 xrd_rules.emplace_back(
AOP_Update, write_path);
1035 xrd_rules.emplace_back(
AOP_Stat, write_path);
1036 xrd_rules.emplace_back(
AOP_Chmod, write_path);
1037 xrd_rules.emplace_back(
AOP_Delete, write_path);
1040 authz_strategy = config.m_authz_strategy;
1042 cache_expiry = expiry;
1043 rules = std::move(xrd_rules);
1044 username = std::move(tmp_username);
1045 issuer = std::move(token_issuer);
1046 groups = std::move(groups_parsed);
1047 acceptable_authz = config.m_acceptable_authz;
1048 scitoken_destroy(token);
1054 bool Config(XrdOucEnv *envP) {
1056 m_log.setMsgMask(LogMask::Error | LogMask::Warning);
1058 char *config_filename =
nullptr;
1062 XrdOucGatherConf scitokens_conf(
"scitokens.trace", &m_log);
1065 m_log.Emsg(
"Config", -result,
"parsing config file", config_filename);
1070 std::string map_filename;
1071 while (scitokens_conf.GetLine()) {
1072 m_log.setMsgMask(0);
1073 scitokens_conf.GetToken();
1074 if (!(val = scitokens_conf.GetToken())) {
1075 m_log.Emsg(
"Config",
"scitokens.trace requires an argument. Usage: scitokens.trace [all|error|warning|info|debug|none]");
1079 if (!strcmp(val,
"all")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::All);}
1080 else if (!strcmp(val,
"error")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Error);}
1081 else if (!strcmp(val,
"warning")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Warning);}
1082 else if (!strcmp(val,
"info")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Info);}
1083 else if (!strcmp(val,
"debug")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Debug);}
1084 else if (!strcmp(val,
"none")) {m_log.setMsgMask(0);}
1085 else {m_log.Emsg(
"Config",
"scitokens.trace encountered an unknown directive:", val);
return false;}
1086 }
while ((val = scitokens_conf.GetToken()));
1088 m_log.Emsg(
"Config",
"Logging levels enabled -",
LogMaskToString(m_log.getMsgMask()).c_str());
1090 auto xrdEnv =
static_cast<XrdOucEnv*
>(
envP ?
envP->
GetPtr(
"xrdEnv*") :
nullptr);
1091 auto tlsCtx =
static_cast<XrdTlsContext*
>(xrdEnv ? xrdEnv->GetPtr(
"XrdTlsContext*") :
nullptr);
1094 if (params && !params->cafile.empty()) {
1095#ifdef HAVE_SCITOKEN_CONFIG_SET_STR
1096 scitoken_config_set_str(
"tls.ca_file", params->cafile.c_str(),
nullptr);
1098 m_log.Log(LogMask::Warning,
"Config",
"tls.ca_file is set but the platform's libscitokens.so does not support setting config parameters");
1106 bool ParseMapfile(
const std::string &filename, std::vector<MapRule> &rules)
1108 std::stringstream ss;
1109 std::ifstream mapfile(filename);
1110 if (!mapfile.is_open())
1112 ss <<
"Error opening mapfile (" << filename <<
"): " << strerror(errno);
1113 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1116 picojson::value val;
1117 auto err = picojson::parse(val, mapfile);
1119 ss <<
"Unable to parse mapfile (" << filename <<
") as json: " << err;
1120 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1123 if (!val.is<picojson::array>()) {
1124 ss <<
"Top-level element of the mapfile " << filename <<
" must be a list";
1125 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1128 const auto& rule_list = val.get<picojson::array>();
1129 for (
const auto &rule : rule_list)
1131 if (!rule.is<picojson::object>()) {
1132 ss <<
"Mapfile " << filename <<
" must be a list of JSON objects; found non-object";
1133 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1139 std::string username;
1141 bool ignore =
false;
1142 for (
const auto &entry : rule.get<picojson::object>()) {
1143 if (!entry.second.is<std::string>()) {
1144 if (entry.first !=
"result" && entry.first !=
"group" && entry.first !=
"sub" && entry.first !=
"path") {
continue;}
1145 ss <<
"In mapfile " << filename <<
", rule entry for " << entry.first <<
" has non-string value";
1146 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1149 if (entry.first ==
"result") {
1150 result = entry.second.get<std::string>();
1152 else if (entry.first ==
"group") {
1153 group = entry.second.get<std::string>();
1155 else if (entry.first ==
"sub") {
1156 sub = entry.second.get<std::string>();
1157 }
else if (entry.first ==
"username") {
1158 username = entry.second.get<std::string>();
1159 }
else if (entry.first ==
"path") {
1160 std::string norm_path;
1161 if (!MakeCanonical(entry.second.get<std::string>(), norm_path)) {
1162 ss <<
"In mapfile " << filename <<
" encountered a path " << entry.second.get<std::string>()
1163 <<
" that cannot be normalized";
1164 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1168 }
else if (entry.first ==
"ignore") {
1173 if (ignore)
continue;
1176 ss <<
"In mapfile " << filename <<
" encountered a rule without a 'result' attribute";
1177 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1180 rules.emplace_back(sub, username, path, group, result);
1190 bool ParseAuthzSetting(OverrideINIReader &reader,
const std::string §ion,
const std::string &variable,
AuthzSetting &result) {
1191 auto authz_setting_str = reader.Get(section, variable,
"");
1193 if (authz_setting_str ==
"") {
1195 }
else if (authz_setting_str ==
"none") {
1197 }
else if (authz_setting_str ==
"all") {
1199 }
else if (authz_setting_str ==
"read") {
1201 }
else if (authz_setting_str ==
"write") {
1204 std::stringstream ss;
1205 ss <<
"Failed to parse " << variable <<
" in section " << section <<
": unknown authorization setting " << authz_setting_str;
1206 m_log.Log(LogMask::Error,
"Reconfig", ss.str().c_str());
1209 result = authz_setting;
1216 std::string new_cfg_file =
"/etc/xrootd/scitokens.cfg";
1217 if (!m_parms.empty()) {
1219 std::vector<std::string> arg_list;
1221 while ((m_parms.size() > pos) && (m_parms[pos] ==
' ')) {pos++;}
1222 auto next_pos = m_parms.find_first_of(
", ", pos);
1223 auto next_arg = m_parms.substr(pos, next_pos - pos);
1225 if (!next_arg.empty()) {
1226 arg_list.emplace_back(std::move(next_arg));
1228 }
while (pos != std::string::npos);
1230 for (
const auto &arg : arg_list) {
1231 if (strncmp(arg.c_str(),
"config=", 7)) {
1232 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring unknown configuration argument:", arg.c_str());
1235 new_cfg_file = std::string(arg.c_str() + 7);
1238 m_log.Log(LogMask::Info,
"Reconfig",
"Parsing configuration file:", new_cfg_file.c_str());
1240 OverrideINIReader reader(new_cfg_file);
1241 if (reader.ParseError() < 0) {
1242 std::stringstream ss;
1243 ss <<
"Error opening config file (" << m_cfg_file <<
"): " << strerror(errno);
1244 m_log.Log(LogMask::Error,
"Reconfig", ss.str().c_str());
1246 }
else if (reader.ParseError()) {
1247 std::stringstream ss;
1248 ss <<
"Parse error on line " << reader.ParseError() <<
" of file " << m_cfg_file;
1249 m_log.Log(LogMask::Error,
"Reconfig", ss.str().c_str());
1252 std::vector<std::string> audiences;
1253 std::unordered_map<std::string, IssuerConfig> issuers;
1254 AuthzBehavior new_authz_behavior = m_authz_behavior;
1255 for (
const auto §ion : reader.Sections()) {
1256 std::string section_lower;
1257 std::transform(section.begin(), section.end(), std::back_inserter(section_lower),
1258 [](
unsigned char c){ return std::tolower(c); });
1260 if (section_lower.substr(0, 6) ==
"global") {
1261 auto audience = reader.Get(section,
"audience",
"");
1262 if (!audience.empty()) {
1265 while (audience.size() > pos && (audience[pos] ==
',' || audience[pos] ==
' ')) {pos++;}
1266 auto next_pos = audience.find_first_of(
", ", pos);
1267 auto next_aud = audience.substr(pos, next_pos - pos);
1269 if (!next_aud.empty()) {
1270 audiences.push_back(next_aud);
1272 }
while (pos != std::string::npos);
1274 audience = reader.Get(section,
"audience_json",
"");
1275 if (!audience.empty()) {
1276 picojson::value json_obj;
1277 auto err = picojson::parse(json_obj, audience);
1279 m_log.Log(LogMask::Error,
"Reconfig",
"Unable to parse audience_json:", err.c_str());
1282 if (!json_obj.is<picojson::value::array>()) {
1283 m_log.Log(LogMask::Error,
"Reconfig",
"audience_json must be a list of strings; not a list.");
1286 for (
const auto &val : json_obj.get<picojson::value::array>()) {
1287 if (!val.is<std::string>()) {
1288 m_log.Log(LogMask::Error,
"Reconfig",
"audience must be a list of strings; value is not a string.");
1291 audiences.push_back(val.get<std::string>());
1294 auto onmissing = reader.Get(section,
"onmissing",
"");
1295 if (onmissing ==
"passthrough") {
1296 new_authz_behavior = AuthzBehavior::PASSTHROUGH;
1297 }
else if (onmissing ==
"allow") {
1298 new_authz_behavior = AuthzBehavior::ALLOW;
1299 }
else if (onmissing ==
"deny") {
1300 new_authz_behavior = AuthzBehavior::DENY;
1301 }
else if (!onmissing.empty()) {
1302 m_log.Log(LogMask::Error,
"Reconfig",
"Unknown value for onmissing key:", onmissing.c_str());
1307 if (section_lower.substr(0, 7) !=
"issuer ") {
continue;}
1309 auto issuer = reader.Get(section,
"issuer",
"");
1310 if (issuer.empty()) {
1311 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring section because 'issuer' attribute is not set:",
1315 m_log.Log(LogMask::Debug,
"Reconfig",
"Configuring issuer", issuer.c_str());
1317 std::vector<MapRule> rules;
1318 auto name_mapfile = reader.Get(section,
"name_mapfile",
"");
1319 if (!name_mapfile.empty()) {
1320 if (!ParseMapfile(name_mapfile, rules)) {
1321 m_log.Log(LogMask::Error,
"Reconfig",
"Failed to parse mapfile; failing (re-)configuration", name_mapfile.c_str());
1324 m_log.Log(LogMask::Info,
"Reconfig",
"Successfully parsed SciTokens mapfile:", name_mapfile.c_str());
1328 auto base_path = reader.Get(section,
"base_path",
"");
1329 if (base_path.empty()) {
1330 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring section because 'base_path' attribute is not set:",
1336 while (section.size() > pos && std::isspace(section[pos])) {pos++;}
1338 auto name = section.substr(pos);
1340 m_log.Log(LogMask::Error,
"Reconfig",
"Invalid section name:", section.c_str());
1344 std::vector<std::string> base_paths;
1345 ParseCanonicalPaths(base_path, base_paths);
1347 auto restricted_path = reader.Get(section,
"restricted_path",
"");
1348 std::vector<std::string> restricted_paths;
1349 if (!restricted_path.empty()) {
1350 ParseCanonicalPaths(restricted_path, restricted_paths);
1353 auto default_user = reader.Get(section,
"default_user",
"");
1354 auto map_subject = reader.GetBoolean(section,
"map_subject",
false);
1355 auto username_claim = reader.Get(section,
"username_claim",
"");
1356 auto groups_claim = reader.Get(section,
"groups_claim",
"wlcg.groups");
1359 if (!ParseAuthzSetting(reader, section,
"required_authorization", required_authz)) {
1360 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring required_authorization and using default of 'none'");
1362 if (!ParseAuthzSetting(reader, section,
"acceptable_authorization", acceptable_authz)) {
1363 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring acceptable_authorization and using default of 'all'");
1366 auto authz_strategy_str = reader.Get(section,
"authorization_strategy",
"");
1367 uint32_t authz_strategy = 0;
1368 if (authz_strategy_str.empty()) {
1371 std::istringstream authz_strategy_stream(authz_strategy_str);
1372 std::string authz_str;
1373 while (std::getline(authz_strategy_stream, authz_str,
' ')) {
1374 if (!strcasecmp(authz_str.c_str(),
"capability")) {
1376 }
else if (!strcasecmp(authz_str.c_str(),
"group")) {
1378 }
else if (!strcasecmp(authz_str.c_str(),
"mapping")) {
1381 m_log.Log(LogMask::Error,
"Reconfig",
"Unknown authorization strategy (ignoring):", authz_str.c_str());
1386 issuers.emplace(std::piecewise_construct,
1387 std::forward_as_tuple(issuer),
1388 std::forward_as_tuple(name, issuer, base_paths, restricted_paths,
1389 map_subject, authz_strategy, default_user, username_claim, groups_claim, rules,
1390 acceptable_authz, required_authz));
1396 for (
const auto &base_path : base_paths) {
1397 if (restricted_paths.empty()) {
1398 restricted_paths.emplace_back(
"/");
1400 for (
const auto &restricted_path : restricted_paths) {
1401 auto full_path = base_path +
"/" + restricted_path;
1402 std::string cleaned_path;
1403 MakeCanonical(full_path, cleaned_path);
1405 rules.emplace_back(
AOP_Read, cleaned_path);
1406 rules.emplace_back(
AOP_Stat, cleaned_path);
1408 rules.emplace_back(
AOP_Create, cleaned_path);
1409 rules.emplace_back(
AOP_Mkdir, cleaned_path);
1410 rules.emplace_back(
AOP_Stat, cleaned_path);
1414 m_required_issuers.emplace_back(std::make_unique<SubpathMatch>(rules), issuer);
1418 if (issuers.empty()) {
1419 m_log.Log(LogMask::Warning,
"Reconfig",
"No issuers configured.");
1422 pthread_rwlock_wrlock(&m_config_lock);
1424 m_authz_behavior = new_authz_behavior;
1425 m_cfg_file = std::move(new_cfg_file);
1426 m_audiences = std::move(audiences);
1428 m_audiences_array.resize(m_audiences.size() + 1);
1429 for (
const auto &audience : m_audiences) {
1430 m_audiences_array[idx++] = audience.c_str();
1432 m_audiences_array[idx] =
nullptr;
1434 m_issuers = std::move(issuers);
1435 m_valid_issuers_array.resize(m_issuers.size() + 1);
1437 for (
const auto &issuer : m_issuers) {
1438 m_valid_issuers_array[idx++] = issuer.first.c_str();
1440 m_valid_issuers_array[idx] =
nullptr;
1442 pthread_rwlock_unlock(&m_config_lock);
1445 pthread_rwlock_unlock(&m_config_lock);
1449 void Check(uint64_t now)
1451 if (now <= m_next_clean) {
return;}
1452 std::lock_guard<std::mutex> guard(m_mutex);
1454 for (
auto iter = m_map.begin(); iter != m_map.end(); ) {
1455 if (iter->second->expired()) {
1456 iter = m_map.erase(iter);
1463 m_next_clean = monotonic_time() + m_expiry_secs;
1466 bool m_config_lock_initialized{
false};
1468 pthread_rwlock_t m_config_lock;
1469 std::vector<std::string> m_audiences;
1470 std::vector<const char *> m_audiences_array;
1471 std::map<std::string, std::shared_ptr<XrdAccRules>, std::less<>> m_map;
1473 const std::string m_parms;
1474 std::vector<const char*> m_valid_issuers_array;
1477 std::vector<std::pair<std::unique_ptr<SubpathMatch>, std::string>> m_required_issuers;
1478 std::unordered_map<std::string, IssuerConfig> m_issuers;
1479 uint64_t m_next_clean{0};
1481 AuthzBehavior m_authz_behavior{AuthzBehavior::PASSTHROUGH};
1482 std::string m_cfg_file;
1484 static constexpr uint64_t m_expiry_secs = 60;