469 enum class AuthzBehavior {
478 m_parms(parms ? parms :
""),
479 m_next_clean(monotonic_time() + m_expiry_secs),
480 m_log(lp,
"scitokens_")
482 pthread_rwlock_init(&m_config_lock,
nullptr);
483 m_config_lock_initialized =
true;
484 m_log.Say(
"++++++ XrdAccSciTokens: Initialized SciTokens-based authorization.");
486 throw std::runtime_error(
"Failed to configure SciTokens authorization.");
491 if (m_config_lock_initialized) {
492 pthread_rwlock_destroy(&m_config_lock);
501 const char *authz = env ? env->
Get(
"authz") :
nullptr;
506 if (authz && !strncmp(authz,
"Bearer%20", 9)) {
511 if (!authz && Entity && !strcmp(
"ztn", Entity->
prot) && Entity->
creds &&
514 authz = Entity->
creds;
516 if (authz ==
nullptr) {
517 return OnMissing(Entity, path, oper, env);
519 m_log.Log(LogMask::Debug,
"Access",
"Trying token-based access control");
520 std::shared_ptr<XrdAccRules> access_rules;
521 uint64_t now = monotonic_time();
524 std::lock_guard<std::mutex> guard(m_map_mutex);
525 const auto iter = m_map.find(authz);
526 if (iter != m_map.end() && !iter->second->expired()) {
527 access_rules = iter->second;
531 m_log.Log(LogMask::Debug,
"Access",
"Token not found in recent cache; parsing.");
533 uint64_t cache_expiry;
534 AccessRulesRaw rules;
535 std::string username;
536 std::string token_subject;
538 std::vector<MapRule> map_rules;
539 std::vector<std::string> groups;
540 uint32_t authz_strategy;
541 if (GenerateAcls(authz, cache_expiry, rules, username, token_subject, issuer, map_rules, groups, authz_strategy)) {
542 access_rules.reset(
new XrdAccRules(now + cache_expiry, username, token_subject, issuer, map_rules, groups, authz_strategy));
543 access_rules->parse(rules);
545 m_log.Log(LogMask::Warning,
"Access",
"Failed to generate ACLs for token");
546 return OnMissing(Entity, path, oper, env);
548 if (m_log.getMsgMask() & LogMask::Debug) {
549 m_log.Log(LogMask::Debug,
"Access",
"New valid token", access_rules->str().c_str());
551 }
catch (std::exception &exc) {
552 m_log.Log(LogMask::Warning,
"Access",
"Error generating ACLs for authorization", exc.what());
553 return OnMissing(Entity, path, oper, env);
555 std::lock_guard<std::mutex> guard(m_map_mutex);
556 m_map[authz] = access_rules;
557 }
else if (m_log.getMsgMask() & LogMask::Debug) {
558 m_log.Log(LogMask::Debug,
"Access",
"Cached token", access_rules->str().c_str());
573 new_secentity.
vorg =
nullptr;
574 new_secentity.
grps =
nullptr;
575 new_secentity.
role =
nullptr;
578 const auto &issuer = access_rules->get_issuer();
579 if (!issuer.empty()) {
580 new_secentity.
vorg = strdup(issuer.c_str());
582 bool group_success =
false;
583 if ((access_rules->get_authz_strategy() & IssuerAuthz::Group) && access_rules->groups().size()) {
584 std::stringstream ss;
585 for (
const auto &grp : access_rules->groups()) {
588 const auto &groups_str = ss.str();
589 new_secentity.
grps =
static_cast<char*
>(malloc(groups_str.size() + 1));
590 if (new_secentity.
grps) {
591 memcpy(new_secentity.
grps, groups_str.c_str(), groups_str.size());
592 new_secentity.
grps[groups_str.size()] =
'\0';
594 group_success =
true;
597 std::string username;
598 bool mapping_success =
false;
599 bool scope_success =
false;
600 username = access_rules->get_username(path);
602 mapping_success = (access_rules->get_authz_strategy() & IssuerAuthz::Mapping) && !username.empty();
603 scope_success = (access_rules->get_authz_strategy() & IssuerAuthz::Capability) && access_rules->apply(oper, path);
604 if (scope_success && (m_log.getMsgMask() & LogMask::Debug)) {
605 std::stringstream ss;
606 ss <<
"Grant authorization based on scopes for operation=" << OpToName(oper) <<
", path=" << path;
607 m_log.Log(LogMask::Debug,
"Access", ss.str().c_str());
610 if (!scope_success && !mapping_success && !group_success) {
611 auto returned_accs = OnMissing(&new_secentity, path, oper, env);
613 if (new_secentity.
vorg !=
nullptr) free(new_secentity.
vorg);
614 if (new_secentity.
grps !=
nullptr) free(new_secentity.
grps);
615 if (new_secentity.
role !=
nullptr) free(new_secentity.
role);
617 return returned_accs;
621 if (scope_success && username.empty()) {
622 username = access_rules->get_default_username();
627 if (scope_success || mapping_success) {
629 Entity->
eaAPI->
Add(
"request.name", username,
true);
630 new_secentity.
eaAPI->
Add(
"request.name", username,
true);
631 m_log.Log(LogMask::Debug,
"Access",
"Request username", username.c_str());
639 const auto &token_subject = access_rules->get_token_subject();
640 if (!token_subject.empty()) {
641 Entity->
eaAPI->
Add(
"token.subject", token_subject,
true);
650 if (Entity->
secMon && scope_success && returned_op &&
Mon_isIO(oper))
651 Mon_Report(new_secentity, token_subject, username);
654 if (new_secentity.
vorg !=
nullptr) free(new_secentity.
vorg);
655 if (new_secentity.
grps !=
nullptr) free(new_secentity.
grps);
656 if (new_secentity.
role !=
nullptr) free(new_secentity.
role);
672 for (
auto it: m_issuers) {
676 issuers.push_back(issuer_info);
682 virtual bool Validate(
const char *token, std::string &
emsg,
long long *expT,
690 if (!strncmp(token,
"Bearer%20", 9)) token += 9;
691 pthread_rwlock_rdlock(&m_config_lock);
692 auto retval = scitoken_deserialize(token, &scitoken, &m_valid_issuers_array[0], &err_msg);
693 pthread_rwlock_unlock(&m_config_lock);
696 m_log.Log(LogMask::Warning,
"Validate",
"Failed to deserialize SciToken:", err_msg);
707 {
char *value =
nullptr;
708 if (!scitoken_get_claim_string(scitoken,
"sub", &value, &err_msg))
709 Entity->
name = strdup(value);
714 if (expT && scitoken_get_expiration(scitoken, expT, &err_msg)) {
722 scitoken_destroy(scitoken);
740 return (m_chain ? m_chain->Test(priv, oper) : 0);
751 switch (m_authz_behavior) {
752 case AuthzBehavior::PASSTHROUGH:
754 case AuthzBehavior::ALLOW:
756 case AuthzBehavior::DENY:
763 bool GenerateAcls(
const std::string &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) {
766 bool looks_good =
true;
767 int separator_count = 0;
768 for (
auto cur_char = authz.c_str(); *cur_char; cur_char++) {
769 if (*cur_char ==
'.') {
771 if (separator_count > 2) {
775 if (!(*cur_char >= 65 && *cur_char <= 90) &&
776 !(*cur_char >= 97 && *cur_char <= 122) &&
777 !(*cur_char >= 48 && *cur_char <= 57) &&
778 (*cur_char != 43) && (*cur_char != 47) &&
779 (*cur_char != 45) && (*cur_char != 95))
785 if ((separator_count != 2) || (!looks_good)) {
786 m_log.Log(LogMask::Debug,
"Parse",
"Token does not appear to be a valid JWT; skipping.");
791 SciToken token =
nullptr;
792 pthread_rwlock_rdlock(&m_config_lock);
793 auto retval = scitoken_deserialize(authz.c_str(), &token, &m_valid_issuers_array[0], &err_msg);
794 pthread_rwlock_unlock(&m_config_lock);
797 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to deserialize SciToken:", err_msg);
803 if (scitoken_get_expiration(token, &expiry, &err_msg)) {
804 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Unable to determine token expiration:", err_msg);
806 scitoken_destroy(token);
810 expiry = std::max(
static_cast<int64_t
>(monotonic_time() - expiry),
811 static_cast<int64_t
>(60));
816 char *value =
nullptr;
817 if (scitoken_get_claim_string(token,
"iss", &value, &err_msg)) {
818 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get issuer:", err_msg);
819 scitoken_destroy(token);
823 std::string token_issuer(value);
826 pthread_rwlock_rdlock(&m_config_lock);
827 auto enf = enforcer_create(token_issuer.c_str(), &m_audiences_array[0], &err_msg);
828 pthread_rwlock_unlock(&m_config_lock);
830 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to create an enforcer:", err_msg);
831 scitoken_destroy(token);
837 if (enforcer_generate_acls(enf, token, &acls, &err_msg)) {
838 scitoken_destroy(token);
839 enforcer_destroy(enf);
840 m_log.Log(LogMask::Warning,
"GenerateAcls",
"ACL generation from SciToken failed:", err_msg);
844 enforcer_destroy(enf);
846 pthread_rwlock_rdlock(&m_config_lock);
847 auto iter = m_issuers.find(token_issuer);
848 if (iter == m_issuers.end()) {
849 pthread_rwlock_unlock(&m_config_lock);
850 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Authorized issuer without a config.");
851 scitoken_destroy(token);
854 const auto config = iter->second;
855 pthread_rwlock_unlock(&m_config_lock);
859 std::vector<std::string> groups_parsed;
860 if (scitoken_get_claim_string_list(token, config.m_groups_claim.c_str(), &group_list, &err_msg) == 0) {
861 for (
int idx=0; group_list[idx]; idx++) {
862 groups_parsed.emplace_back(group_list[idx]);
864 scitoken_free_string_list(group_list);
867 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get token groups:", err_msg);
871 if (scitoken_get_claim_string(token,
"sub", &value, &err_msg)) {
872 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get token subject:", err_msg);
874 scitoken_destroy(token);
877 token_subject = std::string(value);
880 auto tmp_username = token_subject;
881 if (!config.m_username_claim.empty()) {
882 if (scitoken_get_claim_string(token, config.m_username_claim.c_str(), &value, &err_msg)) {
883 m_log.Log(LogMask::Warning,
"GenerateAcls",
"Failed to get token username:", err_msg);
885 scitoken_destroy(token);
888 tmp_username = std::string(value);
890 }
else if (!config.m_map_subject) {
891 tmp_username = config.m_default_user;
894 for (
auto rule : config.m_map_rules) {
895 for (
auto path : config.m_base_paths) {
896 auto path_rule = rule;
897 path_rule.m_path_prefix = path + rule.m_path_prefix;
898 auto pos = path_rule.m_path_prefix.find(
"//");
899 if (pos != std::string::npos) {
900 path_rule.m_path_prefix.erase(pos + 1, 1);
902 map_rules.emplace_back(path_rule);
906 AccessRulesRaw xrd_rules;
908 std::set<std::string> paths_write_seen;
909 std::set<std::string> paths_create_or_modify_seen;
910 std::vector<std::string> acl_paths;
911 acl_paths.reserve(config.m_restricted_paths.size() + 1);
912 while (acls[idx].resource && acls[idx++].authz) {
914 const auto &acl_path = acls[idx-1].resource;
915 const auto &acl_authz = acls[idx-1].authz;
916 if (config.m_restricted_paths.empty()) {
917 acl_paths.push_back(acl_path);
919 auto acl_path_size = strlen(acl_path);
920 for (
const auto &restricted_path : config.m_restricted_paths) {
923 if (!strncmp(acl_path, restricted_path.c_str(), restricted_path.size())) {
926 if (acl_path_size > restricted_path.size() && acl_path[restricted_path.size()] !=
'/') {
929 acl_paths.push_back(acl_path);
935 if (!strncmp(acl_path, restricted_path.c_str(), acl_path_size)) {
942 if ((restricted_path.size() > acl_path_size && restricted_path[acl_path_size] !=
'/') && (acl_path_size != 1)) {
945 acl_paths.push_back(restricted_path);
949 for (
const auto &acl_path : acl_paths) {
950 for (
const auto &base_path : config.m_base_paths) {
951 if (!acl_path[0] || acl_path[0] !=
'/') {
continue;}
953 MakeCanonical(base_path + acl_path, path);
954 if (!strcmp(acl_authz,
"read")) {
955 xrd_rules.emplace_back(
AOP_Read, path);
957 xrd_rules.emplace_back(
AOP_Stat, path);
958 }
else if (!strcmp(acl_authz,
"create")) {
959 paths_create_or_modify_seen.insert(path);
964 xrd_rules.emplace_back(
AOP_Stat, path);
965 }
else if (!strcmp(acl_authz,
"modify")) {
966 paths_create_or_modify_seen.insert(path);
973 xrd_rules.emplace_back(
AOP_Stat, path);
975 }
else if (!strcmp(acl_authz,
"storage.stage")) {
977 xrd_rules.emplace_back(
AOP_Poll, path);
978 }
else if (!strcmp(acl_authz,
"storage.poll")) {
979 xrd_rules.emplace_back(
AOP_Poll, path);
980 }
else if (!strcmp(acl_authz,
"write")) {
981 paths_write_seen.insert(path);
986 for (
const auto &write_path : paths_write_seen) {
987 if (paths_create_or_modify_seen.find(write_path) == paths_create_or_modify_seen.end()) {
989 xrd_rules.emplace_back(
AOP_Create, write_path);
990 xrd_rules.emplace_back(
AOP_Mkdir, write_path);
991 xrd_rules.emplace_back(
AOP_Rename, write_path);
992 xrd_rules.emplace_back(
AOP_Insert, write_path);
993 xrd_rules.emplace_back(
AOP_Update, write_path);
994 xrd_rules.emplace_back(
AOP_Stat, write_path);
995 xrd_rules.emplace_back(
AOP_Chmod, write_path);
996 xrd_rules.emplace_back(
AOP_Delete, write_path);
999 authz_strategy = config.m_authz_strategy;
1001 cache_expiry = expiry;
1002 rules = std::move(xrd_rules);
1003 username = std::move(tmp_username);
1004 issuer = std::move(token_issuer);
1005 groups = std::move(groups_parsed);
1011 bool Config(XrdOucEnv *envP) {
1013 m_log.setMsgMask(LogMask::Error | LogMask::Warning);
1015 char *config_filename =
nullptr;
1019 XrdOucGatherConf scitokens_conf(
"scitokens.trace", &m_log);
1022 m_log.Emsg(
"Config", -result,
"parsing config file", config_filename);
1027 std::string map_filename;
1028 while (scitokens_conf.GetLine()) {
1029 m_log.setMsgMask(0);
1030 scitokens_conf.GetToken();
1031 if (!(val = scitokens_conf.GetToken())) {
1032 m_log.Emsg(
"Config",
"scitokens.trace requires an argument. Usage: scitokens.trace [all|error|warning|info|debug|none]");
1036 if (!strcmp(val,
"all")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::All);}
1037 else if (!strcmp(val,
"error")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Error);}
1038 else if (!strcmp(val,
"warning")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Warning);}
1039 else if (!strcmp(val,
"info")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Info);}
1040 else if (!strcmp(val,
"debug")) {m_log.setMsgMask(m_log.getMsgMask() | LogMask::Debug);}
1041 else if (!strcmp(val,
"none")) {m_log.setMsgMask(0);}
1042 else {m_log.Emsg(
"Config",
"scitokens.trace encountered an unknown directive:", val);
return false;}
1043 }
while ((val = scitokens_conf.GetToken()));
1045 m_log.Emsg(
"Config",
"Logging levels enabled -",
LogMaskToString(m_log.getMsgMask()).c_str());
1047 auto xrdEnv =
static_cast<XrdOucEnv*
>(
envP ?
envP->
GetPtr(
"xrdEnv*") :
nullptr);
1048 auto tlsCtx =
static_cast<XrdTlsContext*
>(xrdEnv ? xrdEnv->GetPtr(
"XrdTlsContext*") :
nullptr);
1051 if (params && !params->cafile.empty()) {
1052#ifdef HAVE_SCITOKEN_CONFIG_SET_STR
1053 scitoken_config_set_str(
"tls.ca_file", params->cafile.c_str(),
nullptr);
1055 m_log.Log(LogMask::Warning,
"Config",
"tls.ca_file is set but the platform's libscitokens.so does not support setting config parameters");
1061 if (
const char* xdg_cache_home = getenv(
"XDG_CACHE_HOME")) {
1062 m_log.Log(LogMask::Info,
"Config",
"Scitokens cache file location env var is set to : ", xdg_cache_home);
1065 const char* adminpath_env = getenv(
"XRDADMINPATH");
1066 if (!adminpath_env || !*adminpath_env) {
1067 m_log.Log(LogMask::Warning,
"Config",
"XRDADMINPATH is not defined; leaving cache location unset");
1069 std::string adminpath = adminpath_env;
1070 while (adminpath.size() > 1 && adminpath.back() ==
'/') adminpath.pop_back();
1071 std::string xdg_cache_home_str = adminpath +
"/.cache";
1072 m_log.Log(LogMask::Info,
"Config",
"Scitokens cache file location env var is not set; using : ", xdg_cache_home_str.c_str());
1074#ifdef HAVE_SCITOKEN_CONFIG_SET_STR
1075 scitoken_config_set_str(
"keycache.cache_home", xdg_cache_home_str.c_str(),
nullptr);
1077 setenv(
"XDG_CACHE_HOME", xdg_cache_home_str.c_str(), 1);
1085 bool ParseMapfile(
const std::string &filename, std::vector<MapRule> &rules)
1087 std::stringstream ss;
1088 std::ifstream mapfile(filename);
1089 if (!mapfile.is_open())
1091 ss <<
"Error opening mapfile (" << filename <<
"): " << strerror(errno);
1092 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1095 picojson::value val;
1096 auto err = picojson::parse(val, mapfile);
1098 ss <<
"Unable to parse mapfile (" << filename <<
") as json: " << err;
1099 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1102 if (!val.is<picojson::array>()) {
1103 ss <<
"Top-level element of the mapfile " << filename <<
" must be a list";
1104 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1107 const auto& rule_list = val.get<picojson::array>();
1108 for (
const auto &rule : rule_list)
1110 if (!rule.is<picojson::object>()) {
1111 ss <<
"Mapfile " << filename <<
" must be a list of JSON objects; found non-object";
1112 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1118 std::string username;
1120 bool ignore =
false;
1121 for (
const auto &entry : rule.get<picojson::object>()) {
1122 if (!entry.second.is<std::string>()) {
1123 if (entry.first !=
"result" && entry.first !=
"group" && entry.first !=
"sub" && entry.first !=
"path") {
continue;}
1124 ss <<
"In mapfile " << filename <<
", rule entry for " << entry.first <<
" has non-string value";
1125 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1128 if (entry.first ==
"result") {
1129 result = entry.second.get<std::string>();
1131 else if (entry.first ==
"group") {
1132 group = entry.second.get<std::string>();
1134 else if (entry.first ==
"sub") {
1135 sub = entry.second.get<std::string>();
1136 }
else if (entry.first ==
"username") {
1137 username = entry.second.get<std::string>();
1138 }
else if (entry.first ==
"path") {
1139 std::string norm_path;
1140 if (!MakeCanonical(entry.second.get<std::string>(), norm_path)) {
1141 ss <<
"In mapfile " << filename <<
" encountered a path " << entry.second.get<std::string>()
1142 <<
" that cannot be normalized";
1143 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1147 }
else if (entry.first ==
"ignore") {
1152 if (ignore)
continue;
1155 ss <<
"In mapfile " << filename <<
" encountered a rule without a 'result' attribute";
1156 m_log.Log(LogMask::Error,
"ParseMapfile", ss.str().c_str());
1159 rules.emplace_back(sub, username, path, group, result);
1168 m_cfg_file =
"/etc/xrootd/scitokens.cfg";
1169 if (!m_parms.empty()) {
1171 std::vector<std::string> arg_list;
1173 while ((m_parms.size() > pos) && (m_parms[pos] ==
' ')) {pos++;}
1174 auto next_pos = m_parms.find_first_of(
", ", pos);
1175 auto next_arg = m_parms.substr(pos, next_pos - pos);
1177 if (!next_arg.empty()) {
1178 arg_list.emplace_back(std::move(next_arg));
1180 }
while (pos != std::string::npos);
1182 for (
const auto &arg : arg_list) {
1183 if (strncmp(arg.c_str(),
"config=", 7)) {
1184 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring unknown configuration argument:", arg.c_str());
1187 m_cfg_file = std::string(arg.c_str() + 7);
1190 m_log.Log(LogMask::Info,
"Reconfig",
"Parsing configuration file:", m_cfg_file.c_str());
1192 OverrideINIReader reader(m_cfg_file);
1193 if (reader.ParseError() < 0) {
1194 std::stringstream ss;
1195 ss <<
"Error opening config file (" << m_cfg_file <<
"): " << strerror(errno);
1196 m_log.Log(LogMask::Error,
"Reconfig", ss.str().c_str());
1198 }
else if (reader.ParseError()) {
1199 std::stringstream ss;
1200 ss <<
"Parse error on line " << reader.ParseError() <<
" of file " << m_cfg_file;
1201 m_log.Log(LogMask::Error,
"Reconfig", ss.str().c_str());
1204 std::vector<std::string> audiences;
1205 std::unordered_map<std::string, IssuerConfig> issuers;
1206 for (
const auto §ion : reader.Sections()) {
1207 std::string section_lower;
1208 std::transform(section.begin(), section.end(), std::back_inserter(section_lower),
1209 [](
unsigned char c){ return std::tolower(c); });
1211 if (section_lower.substr(0, 6) ==
"global") {
1212 auto audience = reader.Get(section,
"audience",
"");
1213 if (!audience.empty()) {
1216 while (audience.size() > pos && (audience[pos] ==
',' || audience[pos] ==
' ')) {pos++;}
1217 auto next_pos = audience.find_first_of(
", ", pos);
1218 auto next_aud = audience.substr(pos, next_pos - pos);
1220 if (!next_aud.empty()) {
1221 audiences.push_back(next_aud);
1223 }
while (pos != std::string::npos);
1225 audience = reader.Get(section,
"audience_json",
"");
1226 if (!audience.empty()) {
1227 picojson::value json_obj;
1228 auto err = picojson::parse(json_obj, audience);
1230 m_log.Log(LogMask::Error,
"Reconfig",
"Unable to parse audience_json:", err.c_str());
1233 if (!json_obj.is<picojson::value::array>()) {
1234 m_log.Log(LogMask::Error,
"Reconfig",
"audience_json must be a list of strings; not a list.");
1237 for (
const auto &val : json_obj.get<picojson::value::array>()) {
1238 if (!val.is<std::string>()) {
1239 m_log.Log(LogMask::Error,
"Reconfig",
"audience must be a list of strings; value is not a string.");
1242 audiences.push_back(val.get<std::string>());
1245 auto onmissing = reader.Get(section,
"onmissing",
"");
1246 if (onmissing ==
"passthrough") {
1247 m_authz_behavior = AuthzBehavior::PASSTHROUGH;
1248 }
else if (onmissing ==
"allow") {
1249 m_authz_behavior = AuthzBehavior::ALLOW;
1250 }
else if (onmissing ==
"deny") {
1251 m_authz_behavior = AuthzBehavior::DENY;
1252 }
else if (!onmissing.empty()) {
1253 m_log.Log(LogMask::Error,
"Reconfig",
"Unknown value for onmissing key:", onmissing.c_str());
1258 if (section_lower.substr(0, 7) !=
"issuer ") {
continue;}
1260 auto issuer = reader.Get(section,
"issuer",
"");
1261 if (issuer.empty()) {
1262 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring section because 'issuer' attribute is not set:",
1266 m_log.Log(LogMask::Debug,
"Reconfig",
"Configuring issuer", issuer.c_str());
1268 std::vector<MapRule> rules;
1269 auto name_mapfile = reader.Get(section,
"name_mapfile",
"");
1270 if (!name_mapfile.empty()) {
1271 if (!ParseMapfile(name_mapfile, rules)) {
1272 m_log.Log(LogMask::Error,
"Reconfig",
"Failed to parse mapfile; failing (re-)configuration", name_mapfile.c_str());
1275 m_log.Log(LogMask::Info,
"Reconfig",
"Successfully parsed SciTokens mapfile:", name_mapfile.c_str());
1279 auto base_path = reader.Get(section,
"base_path",
"");
1280 if (base_path.empty()) {
1281 m_log.Log(LogMask::Error,
"Reconfig",
"Ignoring section because 'base_path' attribute is not set:",
1287 while (section.size() > pos && std::isspace(section[pos])) {pos++;}
1289 auto name = section.substr(pos);
1291 m_log.Log(LogMask::Error,
"Reconfig",
"Invalid section name:", section.c_str());
1295 std::vector<std::string> base_paths;
1296 ParseCanonicalPaths(base_path, base_paths);
1298 auto restricted_path = reader.Get(section,
"restricted_path",
"");
1299 std::vector<std::string> restricted_paths;
1300 if (!restricted_path.empty()) {
1301 ParseCanonicalPaths(restricted_path, restricted_paths);
1304 auto default_user = reader.Get(section,
"default_user",
"");
1305 auto map_subject = reader.GetBoolean(section,
"map_subject",
false);
1306 auto username_claim = reader.Get(section,
"username_claim",
"");
1307 auto groups_claim = reader.Get(section,
"groups_claim",
"wlcg.groups");
1309 auto authz_strategy_str = reader.Get(section,
"authorization_strategy",
"");
1310 uint32_t authz_strategy = 0;
1311 if (authz_strategy_str.empty()) {
1312 authz_strategy = IssuerAuthz::Default;
1314 std::istringstream authz_strategy_stream(authz_strategy_str);
1315 std::string authz_str;
1316 while (std::getline(authz_strategy_stream, authz_str,
' ')) {
1317 if (!strcasecmp(authz_str.c_str(),
"capability")) {
1318 authz_strategy |= IssuerAuthz::Capability;
1319 }
else if (!strcasecmp(authz_str.c_str(),
"group")) {
1320 authz_strategy |= IssuerAuthz::Group;
1321 }
else if (!strcasecmp(authz_str.c_str(),
"mapping")) {
1322 authz_strategy |= IssuerAuthz::Mapping;
1324 m_log.Log(LogMask::Error,
"Reconfig",
"Unknown authorization strategy (ignoring):", authz_str.c_str());
1329 issuers.emplace(std::piecewise_construct,
1330 std::forward_as_tuple(issuer),
1331 std::forward_as_tuple(name, issuer, base_paths, restricted_paths,
1332 map_subject, authz_strategy, default_user, username_claim, groups_claim, rules));
1335 if (issuers.empty()) {
1336 m_log.Log(LogMask::Warning,
"Reconfig",
"No issuers configured.");
1339 pthread_rwlock_wrlock(&m_config_lock);
1341 m_audiences = std::move(audiences);
1343 m_audiences_array.resize(m_audiences.size() + 1);
1344 for (
const auto &audience : m_audiences) {
1345 m_audiences_array[idx++] = audience.c_str();
1347 m_audiences_array[idx] =
nullptr;
1349 m_issuers = std::move(issuers);
1350 m_valid_issuers_array.resize(m_issuers.size() + 1);
1352 for (
const auto &issuer : m_issuers) {
1353 m_valid_issuers_array[idx++] = issuer.first.c_str();
1355 m_valid_issuers_array[idx] =
nullptr;
1357 pthread_rwlock_unlock(&m_config_lock);
1360 pthread_rwlock_unlock(&m_config_lock);
1364 void Check(uint64_t now)
1367 std::unique_lock<std::mutex> lock(m_check_mutex, std::try_to_lock);
1368 if (!lock.owns_lock()) {
return;}
1371 if (now <= m_next_clean) {
return;}
1375 std::lock_guard<std::mutex> guard(m_map_mutex);
1376 for (
auto iter = m_map.begin(); iter != m_map.end(); ) {
1377 if (iter->second->expired()) {
1378 iter = m_map.erase(iter);
1386 m_next_clean = monotonic_time() + m_expiry_secs;
1389 bool m_config_lock_initialized{
false};
1390 pthread_rwlock_t m_config_lock;
1391 std::vector<std::string> m_audiences;
1392 std::vector<const char *> m_audiences_array;
1393 std::map<std::string, std::shared_ptr<XrdAccRules>> m_map;
1394 std::mutex m_check_mutex;
1395 std::mutex m_map_mutex;
1397 const std::string m_parms;
1398 std::vector<const char*> m_valid_issuers_array;
1399 std::unordered_map<std::string, IssuerConfig> m_issuers;
1400 uint64_t m_next_clean{0};
1402 AuthzBehavior m_authz_behavior{AuthzBehavior::PASSTHROUGH};
1403 std::string m_cfg_file;
1405 static constexpr uint64_t m_expiry_secs = 60;