505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher Copyright (C) 2012 Red Hat
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher This program is free software; you can redistribute it and/or modify
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher it under the terms of the GNU General Public License as published by
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher the Free Software Foundation; either version 3 of the License, or
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher (at your option) any later version.
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher This program is distributed in the hope that it will be useful,
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher GNU General Public License for more details.
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher You should have received a copy of the GNU General Public License
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
3d9bafcbb5c0fbf23351004ded4dea6aa13127fcSumit Bosesdap_idmap_get_configured_external_range(struct sdap_idmap_ctx *idmap_ctx,
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose int_id = dp_opt_get_int(id_ctx->opts->basic, SDAP_MIN_ID);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CONF_SETTINGS, "ldap_min_id must be greater than 0.\n");
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose int_id = dp_opt_get_int(id_ctx->opts->basic, SDAP_MAX_ID);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CONF_SETTINGS, "ldap_max_id must be greater than 0.\n");
3d9bafcbb5c0fbf23351004ded4dea6aa13127fcSumit Bose if ((min == 0 && max != 0) || (min != 0 && max == 0)) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CONF_SETTINGS, "Both ldap_min_id and ldap_max_id " \
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose "either must be 0 (not set) " \
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "or positive integers.\n");
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose /* ldap_min_id and ldap_max_id not set, using min_id and max_id */
3d9bafcbb5c0fbf23351004ded4dea6aa13127fcSumit Bosesdap_idmap_add_configured_external_range(struct sdap_idmap_ctx *idmap_ctx)
3d9bafcbb5c0fbf23351004ded4dea6aa13127fcSumit Bose ret = sdap_idmap_get_configured_external_range(idmap_ctx, &range);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "sdap_idmap_get_configured_external_range failed.\n");
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = sss_idmap_add_auto_domain_ex(idmap_ctx->map,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add domain [%s] to the map: [%d]\n",
bfb40893be20b45279a40188cf16ef0eec1f9423Sumit Boseerrno_t sdap_idmap_find_new_domain(struct sdap_idmap_ctx *idmap_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add new domain [%s]\n", dom_name);
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher idmap_ctx = talloc_zero(tmp_ctx, struct sdap_idmap_ctx);
bfb40893be20b45279a40188cf16ef0eec1f9423Sumit Bose idmap_ctx->find_new_domain = sdap_idmap_find_new_domain;
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek idmap_lower = dp_opt_get_int(idmap_ctx->id_ctx->opts->basic,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek idmap_upper = dp_opt_get_int(idmap_ctx->id_ctx->opts->basic,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek rangesize = dp_opt_get_int(idmap_ctx->id_ctx->opts->basic,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek autorid_mode = dp_opt_get_bool(idmap_ctx->id_ctx->opts->basic,
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl extra_slice_init = dp_opt_get_int(idmap_ctx->id_ctx->opts->basic,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* Validate that the values make sense */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Invalid settings for range selection: "
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek if (((idmap_upper - idmap_lower) % rangesize) != 0) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Range size does not divide evenly. Uppermost range will "
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "not be used\n");
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /* Initialize the map */
a473fb88e6015cf0ccbd2e9005c7e6acca18f452Pavel Březina err = sss_idmap_init(sss_idmap_talloc, idmap_ctx,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not initialize the ID map: [%s]\n",
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek err = sss_idmap_ctx_set_autorid(idmap_ctx->map, autorid_mode);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek err |= sss_idmap_ctx_set_lower(idmap_ctx->map, idmap_lower);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek err |= sss_idmap_ctx_set_upper(idmap_ctx->map, idmap_upper);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek err |= sss_idmap_ctx_set_rangesize(idmap_ctx->map, rangesize);
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err |= sss_idmap_ctx_set_extra_slice_init(idmap_ctx->map, extra_slice_init);
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek /* This should never happen */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "sss_idmap_ctx corrupted\n");
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose /* Setup range for externally managed IDs, i.e. IDs are read from the
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose * ldap_user_uid_number and ldap_group_gid_number attributes. */
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose if (!dp_opt_get_bool(idmap_ctx->id_ctx->opts->basic, SDAP_ID_MAPPING)) {
949fbc93defad394648b2651b43a7bbfa5bff42bSumit Bose ret = sdap_idmap_add_configured_external_range(idmap_ctx);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "sdap_idmap_add_configured_external_range failed.\n");
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /* Read in any existing mappings from the cache */
7d056853e4a5fe6daa5743e38d21b4493f4fca27Jakub Hrozek ret = sysdb_idmap_get_mappings(tmp_ctx, id_ctx->be->domain, &res);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not read ID mappings from the cache: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Initializing [%d] domains for ID-mapping\n", res->count);
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher dom_name = ldb_msg_find_attr_as_string(res->msgs[i],
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /* This should never happen */
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher sid_str = ldb_msg_find_attr_as_string(res->msgs[i],
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /* This should never happen */
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher slice_num = ldb_msg_find_attr_as_int(res->msgs[i],
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /* This should never happen */
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher ret = sdap_idmap_add_domain(idmap_ctx, dom_name,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add domain [%s][%s][%"SPRIid"] "
af58b15fa7f20e33736d79c6a4b3becb568517caLukas Slebodnik "to ID map: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov dom_name, sid_str, slice_num, strerror(ret));
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher /* This is the first time we're setting up id-mapping
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher * Store the default domain as slice 0
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher dom_name = dp_opt_get_string(idmap_ctx->id_ctx->opts->basic, SDAP_IDMAP_DEFAULT_DOMAIN);
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher /* If it's not explicitly specified, use the SSSD domain name */
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher dom_name = idmap_ctx->id_ctx->be->domain->name;
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher ret = dp_opt_set_string(idmap_ctx->id_ctx->opts->basic,
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher sid_str = dp_opt_get_string(idmap_ctx->id_ctx->opts->basic, SDAP_IDMAP_DEFAULT_DOMAIN_SID);
21687d1d553579e81aa43bfa20f2e70fb39e8461Lukas Slebodnik struct sss_domain_info *domain = idmap_ctx->id_ctx->be->domain;
21687d1d553579e81aa43bfa20f2e70fb39e8461Lukas Slebodnik domain->domain_id = talloc_strdup(domain, sid_str);
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher /* Set the default domain as slice 0 */
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher ret = sdap_idmap_add_domain(idmap_ctx, dom_name,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add domain [%s][%s][%u] to ID map: [%s]\n",
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher if (dp_opt_get_bool(idmap_ctx->id_ctx->opts->basic, SDAP_IDMAP_AUTORID_COMPAT)) {
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher /* In autorid compatibility mode, we MUST have a slice 0 */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "WARNING: Autorid compatibility mode selected, "
5dedd73d90f0c1f23299f0c613f384ef902c3653Stephen Gallagher "but %s is not set. UID/GID values may differ "
5dedd73d90f0c1f23299f0c613f384ef902c3653Stephen Gallagher "between clients.\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov idmap_ctx->id_ctx->opts->basic[SDAP_IDMAP_DEFAULT_DOMAIN_SID].opt_name);
4f3fd1fb264a7eaf3a9d062d49e071b0d17e4debStephen Gallagher /* Otherwise, we'll just fall back to hash values as they are seen */
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher *_idmap_ctx = talloc_steal(mem_ctx, idmap_ctx);
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallaghersdap_idmap_add_domain(struct sdap_idmap_ctx *idmap_ctx,
46222e5191473f9a46aec581273eb2eef22e23beMichal Zidek ret = sss_idmap_ctx_get_upper(idmap_ctx->map, &idmap_upper);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to get upper bound of available ID range.\n");
cb446b6149d28c204954ae75143b89aef14115dcSumit Bose if (dp_opt_get_bool(idmap_ctx->id_ctx->opts->basic, SDAP_ID_MAPPING)) {
cb446b6149d28c204954ae75143b89aef14115dcSumit Bose ret = sss_idmap_calculate_range(idmap_ctx->map, dom_sid, &slice, &range);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to calculate range for domain [%s]: [%d]\n", dom_name,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Adding domain [%s] as slice [%"SPRIid"]\n", dom_sid, slice);
cb446b6149d28c204954ae75143b89aef14115dcSumit Bose /* This should never happen */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "BUG: Range maximum exceeds the global maximum: "
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "%u > %"SPRIid"\n", range.max, idmap_upper);
cb446b6149d28c204954ae75143b89aef14115dcSumit Bose ret = sdap_idmap_get_configured_external_range(idmap_ctx, &range);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "sdap_idmap_get_configured_external_range failed.\n");
505e75ba28b42bb3de7a6d55de825091b70cc2b2Stephen Gallagher /* Add this domain to the map */
8babbeee01e67893af4828ddfc922ecac0be4197Pavel Reichl err = sss_idmap_add_auto_domain_ex(idmap_ctx->map, dom_name, dom_sid,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add domain [%s] to the map: [%d]\n",
cb446b6149d28c204954ae75143b89aef14115dcSumit Bose /* If algorithmic mapping is used add this domain to the SYSDB cache so it
cb446b6149d28c204954ae75143b89aef14115dcSumit Bose * will survive reboot */
7d056853e4a5fe6daa5743e38d21b4493f4fca27Jakub Hrozek ret = sysdb_idmap_store_mapping(idmap_ctx->id_ctx->be->domain,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "sysdb_idmap_store_mapping failed.\n");
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallaghersdap_idmap_get_dom_sid_from_object(TALLOC_CTX *mem_ctx,
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher const char *p;
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher || strncmp(object_sid, DOM_SID_PREFIX, DOM_SID_PREFIX_LEN) != 0) {
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher } while(c < 3);
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher /* If we made it here, we are now one character past
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher * the last hyphen in the object-sid.
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher * Copy the dom-sid substring.
45f75fc8e98092fa48faa3d180fd42f7efd51486Stephen Gallagher *dom_sid_str = talloc_strndup(mem_ctx, object_sid,
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallaghersdap_idmap_sid_to_unix(struct sdap_idmap_ctx *idmap_ctx,
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher /* Convert the SID into a UNIX ID */
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher err = sss_idmap_sid_to_unix(idmap_ctx->map,
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher /* This is the first time we've seen this domain
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher * Create a new domain for it. We'll use the dom-sid
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher * as the domain name for now, since we don't have
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher * any way to get the real name.
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher ret = sdap_idmap_get_dom_sid_from_object(NULL, sid_str,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not parse domain SID from [%s]\n", sid_str);
bfb40893be20b45279a40188cf16ef0eec1f9423Sumit Bose ret = idmap_ctx->find_new_domain(idmap_ctx, dom_sid_str, dom_sid_str);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add new domain for sid [%s]\n", sid_str);
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher /* Now try converting to a UNIX ID again */
8be5e4497e5008f7807178acdfcbf97365ec4e73Stephen Gallagher err = sss_idmap_sid_to_unix(idmap_ctx->map,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not convert objectSID [%s] to a UNIX ID\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Object SID [%s] is a built-in one.\n", sid_str);
d6f283302268520c1506fb3da4f2a22f5a741be5Michal Zidek /* ENOTSUP indicates built-in SID */
a47102e74050d8ab14a9ea835ab2640c9aa65856Stephen Gallagher "Object SID [%s] has a RID that is larger than the "
a47102e74050d8ab14a9ea835ab2640c9aa65856Stephen Gallagher "ldap_idmap_range_size. See the \"ID MAPPING\" section of "
de1131abe5ba7aaeb59f81fc3a9cd2a71c0b52ddLukas Slebodnik "sssd-ad(5) for an explanation of how to resolve this issue.\n",
a47102e74050d8ab14a9ea835ab2640c9aa65856Stephen Gallagher /* Fall through intentionally */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not convert objectSID [%s] to a UNIX ID\n",
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bosebool sdap_idmap_domain_has_algorithmic_mapping(struct sdap_idmap_ctx *ctx,
1e4a582e29c119e2c0e58a02dcb41b829e6b5e39Lukas Slebodnik if (dp_opt_get_bool(ctx->id_ctx->opts->basic, SDAP_ID_MAPPING)
dea636af4d1902a081ee891f1b19ee2f8729d759Pavel Březina && dp_target_enabled(ctx->id_ctx->be->provider, "ldap", DPT_ID)) {
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose err = sss_idmap_domain_has_algorithmic_mapping(ctx->map, dom_sid,
cdcca90249aadb72bf2978a63c202c5b68642224Lukas Slebodnik /* continue with idmap_domain_by_name */
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose return false;
fdda4b659fa3be3027df91a2b053835186ec2c59Sumit Bose err = sss_idmap_domain_by_name_has_algorithmic_mapping(ctx->map,
fdda4b659fa3be3027df91a2b053835186ec2c59Sumit Bose } else if (err != IDMAP_NAME_UNKNOWN && err != IDMAP_NO_DOMAIN) {
fdda4b659fa3be3027df91a2b053835186ec2c59Sumit Bose return false;
1e6ad2b73851049197c7756787d14c78f64e1128Sumit Bose /* If there is no SID, e.g. IPA without enabled trust support, we cannot
1e6ad2b73851049197c7756787d14c78f64e1128Sumit Bose * have algorithmic mapping */
1e6ad2b73851049197c7756787d14c78f64e1128Sumit Bose return false;
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose /* This is the first time we've seen this domain
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose * Create a new domain for it. We'll use the dom-sid
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose * as the domain name for now, since we don't have
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose * any way to get the real name.
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose return false;
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose ret = sdap_idmap_get_dom_sid_from_object(tmp_ctx, dom_sid,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not parse domain SID from [%s]\n", dom_sid);
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose return false;
fdda4b659fa3be3027df91a2b053835186ec2c59Sumit Bose ret = ctx->find_new_domain(ctx, dom_name, new_dom_sid);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not add new domain for sid [%s]\n", dom_sid);
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose return false;
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose err = sss_idmap_domain_has_algorithmic_mapping(ctx->map, dom_sid,
b2c7b6fe7a6b9ef3af8d4d3037fe83d6e9bfd6a5Sumit Bose return false;