ipa_subdomains.c revision 7197ce636c2b92152f5f6180bef6bda3752d148d
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose/*
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose SSSD
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose IPA Subdomains Module
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose Authors:
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose Sumit Bose <sbose@redhat.com>
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose Copyright (C) 2011 Red Hat
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose This program is free software; you can redistribute it and/or modify
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose it under the terms of the GNU General Public License as published by
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose the Free Software Foundation; either version 3 of the License, or
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose (at your option) any later version.
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose This program is distributed in the hope that it will be useful,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose GNU General Public License for more details.
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose You should have received a copy of the GNU General Public License
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose*/
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose#include "providers/ldap/sdap_async.h"
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose#include "providers/ipa/ipa_subdomains.h"
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose#include "providers/ipa/ipa_common.h"
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce#include <ctype.h>
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose#define SUBDOMAINS_FILTER "objectclass=ipaNTTrustedDomain"
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny#define MASTER_DOMAIN_FILTER "objectclass=ipaNTDomainAttrs"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define RANGE_FILTER "objectclass=ipaIDRange"
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose#define IPA_CN "cn"
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose#define IPA_FLATNAME "ipaNTFlatName"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define IPA_SID "ipaNTSecurityIdentifier"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define IPA_TRUSTED_DOMAIN_SID "ipaNTTrustedDomainSID"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define IPA_BASE_ID "ipaBaseID"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define IPA_ID_RANGE_SIZE "ipaIDRangeSize"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define IPA_BASE_RID "ipaBaseRID"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define IPA_SECONDARY_BASE_RID "ipaSecondaryBaseRID"
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose#define OBJECTCLASS "objectClass"
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce/* do not refresh more often than every 5 seconds for now */
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce#define IPA_SUBDOMAIN_REFRESH_LIMIT 5
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce/* refresh automatically every 4 hours */
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce#define IPA_SUBDOMAIN_REFRESH_PERIOD (3600 * 4)
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek/* the directory domain - realm mappings are written to */
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek#define IPA_SUBDOMAIN_MAPPING_DIR PUBCONF_PATH"/krb5.include.d"
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenyenum ipa_subdomains_req_type {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny IPA_SUBDOMAINS_MASTER,
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny IPA_SUBDOMAINS_SLAVE,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose IPA_SUBDOMAINS_RANGES,
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny IPA_SUBDOMAINS_MAX /* Counter */
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny};
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystruct ipa_subdomains_req_params {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny const char *filter;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny tevent_req_fn cb;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose const char *attrs[8];
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny};
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorcestruct ipa_subdomains_ctx {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce struct be_ctx *be_ctx;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct sdap_id_ctx *sdap_id_ctx;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct sdap_search_base **search_bases;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct sdap_search_base **master_search_bases;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct sdap_search_base **ranges_search_bases;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce time_t last_refreshed;
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce struct tevent_timer *timer_event;
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce /* subdomain map cache */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce int num_subdoms;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce struct sysdb_subdom *subdoms;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce};
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosestatic void ipa_subdomains_reply(struct be_req *be_req, int dp_err, int result)
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose{
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce if (be_req) {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce be_req->fn(be_req, dp_err, result, NULL);
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose}
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosestatic errno_t ipa_ranges_parse_results(TALLOC_CTX *mem_ctx,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t count,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct sysdb_attrs **reply,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info ***_range_list)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose{
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info **range_list = NULL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose const char *value;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t c;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose int ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose range_list = talloc_array(mem_ctx, struct range_info *, count + 1);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (range_list == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("talloc_array failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose for (c = 0; c < count; c++) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose range_list[c] = talloc_zero(range_list, struct range_info);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (range_list[c] == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_attrs_get_string(reply[c], IPA_CN, &value);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose range_list[c]->name = talloc_strdup(range_list[c], value);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (range_list[c]->name == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("talloc_strdup failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_attrs_get_string(reply[c], IPA_TRUSTED_DOMAIN_SID, &value);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret == EOK) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose range_list[c]->trusted_dom_sid = talloc_strdup(range_list[c],
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose value);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (range_list[c]->trusted_dom_sid == NULL) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("talloc_strdup failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ENOMEM;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose } else if (ret != ENOENT) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_attrs_get_uint32_t(reply[c], IPA_BASE_ID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &range_list[c]->base_id);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_attrs_get_uint32_t(reply[c], IPA_ID_RANGE_SIZE,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &range_list[c]->id_range_size);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_attrs_get_uint32_t(reply[c], IPA_BASE_RID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &range_list[c]->base_rid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_attrs_get_uint32_t(reply[c], IPA_SECONDARY_BASE_RID,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose &range_list[c]->secondary_base_rid);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK && ret != ENOENT) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose range_list[c] = NULL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose *_range_list = range_list;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = EOK;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosedone:
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_free(range_list);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return ret;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose}
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorcestatic char *name_to_realm(TALLOC_CTX *memctx, const char *name)
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce{
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce char *realm;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce char *p;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce realm = talloc_strdup(memctx, name);
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce if (!realm) {
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce return NULL;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce }
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce for (p = realm; *p; p++) {
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce *p = toupper(*p);
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce }
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce return realm;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce}
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorcestatic errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce struct sysdb_attrs *attrs,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce struct sysdb_subdom *subdom)
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose{
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose const char *value;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose int ret;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = sysdb_attrs_get_string(attrs, IPA_CN, &value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret != EOK) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ret;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->name == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce subdom->name = talloc_strdup(memctx, value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->name == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ENOMEM;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce } else if (strcmp(subdom->name, value) != 0) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("subdomain name mismatch!\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return EINVAL;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->realm == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* Add Realm as upper(domain name), this is generally always correct
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce * with AD domains */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce subdom->realm = name_to_realm(memctx, subdom->name);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (!subdom->realm) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ENOMEM;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = sysdb_attrs_get_string(attrs, IPA_FLATNAME, &value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ret;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* in theory this may change, it should never happen, so we will log a
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce * warning if it does, but we will allow it for now */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->flat_name != NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (strcmp(subdom->flat_name, value) != 0) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_TRACE_INTERNAL,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ("Flat name for subdomain changed!\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce talloc_free(discard_const(subdom->flat_name));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce subdom->flat_name = (const char *)NULL;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->flat_name == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce subdom->flat_name = talloc_strdup(memctx, value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->flat_name == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ENOMEM;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = sysdb_attrs_get_string(attrs, IPA_TRUSTED_DOMAIN_SID, &value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ret;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* in theory this may change, it should never happen, so we will log a
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce * warning if it does, but we will allow it for now */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->id != NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (strcmp(subdom->id, value) != 0) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_TRACE_INTERNAL,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ("ID for subdomain changed!\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce talloc_free(discard_const(subdom->id));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce subdom->flat_name = (const char *)NULL;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->id == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce subdom->id = talloc_strdup(memctx, value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (subdom->id == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return ENOMEM;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce return EOK;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce}
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozekstatic errno_t
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozekipa_subdomains_write_mappings(struct sss_domain_info *domain,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek size_t num_subdoms,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek struct sysdb_subdom *subdoms)
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek{
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek errno_t ret;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek errno_t err;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek TALLOC_CTX *tmp_ctx;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek const char *mapping_file;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek char *tmp_file = NULL;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek int fd = -1;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek FILE *fstream = NULL;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek size_t i;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek tmp_ctx = talloc_new(NULL);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (!tmp_ctx) return ENOMEM;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek mapping_file = talloc_asprintf(tmp_ctx, "%s/domain_realm_%s",
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek IPA_SUBDOMAIN_MAPPING_DIR, domain->name);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (!mapping_file) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = ENOMEM;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek tmp_file = talloc_asprintf(tmp_ctx, "%sXXXXXX", mapping_file);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (tmp_file == NULL) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = ENOMEM;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek fd = mkstemp(tmp_file);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (fd < 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_OP_FAILURE, ("creating the temp file [%s] for domain-realm "
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek "mappings failed.", tmp_file));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = EIO;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek talloc_zfree(tmp_ctx);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek fstream = fdopen(fd, "a");
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (!fstream) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_OP_FAILURE, ("fdopen failed [%d]: %s\n",
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret, strerror(ret)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = close(fd);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret != 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("fclose failed [%d][%s].\n", ret, strerror(ret)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek /* Nothing to do here, just report the failure */
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = EIO;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = fprintf(fstream, "[domain_realm]\n");
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret < 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_OP_FAILURE, ("fprintf failed\n"));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = EIO;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek for (i = 0; i < num_subdoms; i++) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = fprintf(fstream, ".%s = %s\n%s = %s\n",
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek subdoms[i].name, subdoms[i].realm,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek subdoms[i].name, subdoms[i].realm);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret < 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, ("fprintf failed\n"));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = fclose(fstream);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret != 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("fclose failed [%d][%s].\n", ret, strerror(ret)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek fstream = NULL;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = rename(tmp_file, mapping_file);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret == -1) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("rename failed [%d][%s].\n", ret, strerror(ret)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek talloc_zfree(tmp_file);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = chmod(mapping_file, 0644);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret == -1) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("fchmod failed [%d][%s].\n", ret, strerror(ret)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek goto done;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = EOK;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozekdone:
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (fstream) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek err = fclose(fstream);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (err != 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek err = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("fclose failed [%d][%s].\n", err, strerror(err)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek /* Nothing to do here, just report the failure */
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (tmp_file) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek err = unlink(tmp_file);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (err < 0) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek err = errno;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("Could not remove file [%s]: [%d]: %s",
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek tmp_file, err, strerror(err)));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek talloc_free(tmp_ctx);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek return ret;
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek}
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorcestatic errno_t ipa_subdomains_refresh(struct ipa_subdomains_ctx *ctx,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce int count, struct sysdb_attrs **reply,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce bool *changes)
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce{
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce bool handled[count];
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce const char *value;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce int c, h;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce int ret;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce int i, j;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce memset(handled, 0, sizeof(bool) * count);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* check existing subdoms in cache */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce for (i = 0, h = 0; i < ctx->num_subdoms; i++) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce for (c = 0; c < count; c++) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (handled[c]) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce continue;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = sysdb_attrs_get_string(reply[c], IPA_CN, &value);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret != EOK) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_get_string failed.\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (strcmp(value, ctx->subdoms[i].name) == 0) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce break;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (c >= count) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* ok this subdomain does not exist anymore, let's clean up */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce for (j = i; j < ctx->num_subdoms - 1; j++) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->subdoms[j] = ctx->subdoms[j + 1];
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->num_subdoms--;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce i--;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce } else {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* ok let's try to update it */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = ipa_subdom_parse(ctx->subdoms, reply[c], &ctx->subdoms[i]);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Failed to parse subdom data\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce handled[c] = true;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce h++;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (count == h) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* all domains were already accounted for and have been updated */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = EOK;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce goto done;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* if we get here it means we have changes to the subdomains list */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce *changes = true;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce /* add space for unhandled domains */
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce c = count - h;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->subdoms = talloc_realloc(ctx, ctx->subdoms,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce struct sysdb_subdom,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->num_subdoms + c);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ctx->subdoms == NULL) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = ENOMEM;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce goto done;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce for (c = 0; c < count; c++) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (handled[c]) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce continue;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce i = ctx->num_subdoms;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce memset(&ctx->subdoms[i], 0, sizeof(struct sysdb_subdom));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = ipa_subdom_parse(ctx->subdoms, reply[c], &ctx->subdoms[i]);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Failed to parse subdom data\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->num_subdoms++;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret = EOK;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosedone:
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret != EOK) {
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce ctx->last_refreshed = 0;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->num_subdoms = 0;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce talloc_zfree(ctx->subdoms);
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce } else {
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce ctx->last_refreshed = time(NULL);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return ret;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose}
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosestruct ipa_subdomains_req_ctx {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct be_req *be_req;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct ipa_subdomains_ctx *sd_ctx;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct sdap_id_op *sdap_op;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose char *current_filter;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct sdap_search_base **search_bases;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose int search_base_iter;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose size_t reply_count;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct sysdb_attrs **reply;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose};
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosestatic void ipa_subdomains_get_conn_done(struct tevent_req *req);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystatic errno_t
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenyipa_subdomains_handler_get(struct ipa_subdomains_req_ctx *ctx,
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny enum ipa_subdomains_req_type type);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosestatic void ipa_subdomains_handler_done(struct tevent_req *req);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystatic void ipa_subdomains_handler_master_done(struct tevent_req *req);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosestatic void ipa_subdomains_handler_ranges_done(struct tevent_req *req);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystatic struct ipa_subdomains_req_params subdomain_requests[] = {
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce { MASTER_DOMAIN_FILTER,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce ipa_subdomains_handler_master_done,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce { IPA_CN, IPA_FLATNAME, IPA_SID, NULL }
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce },
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce { SUBDOMAINS_FILTER,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce ipa_subdomains_handler_done,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce { IPA_CN, IPA_FLATNAME, IPA_TRUSTED_DOMAIN_SID, NULL }
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce },
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce { RANGE_FILTER,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce ipa_subdomains_handler_ranges_done,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce { OBJECTCLASS, IPA_CN,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce IPA_BASE_ID, IPA_BASE_RID, IPA_SECONDARY_BASE_RID,
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce IPA_ID_RANGE_SIZE, IPA_TRUSTED_DOMAIN_SID, NULL
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce }
5ea449b18d2597f2581627de80bcaf2bc70b0fd3Simo Sorce }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny};
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce/* NOTE: be_req can be NULL, this is used by the online callback to refresh
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce * subdomains without any request coming from a frontend */
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorcestatic void ipa_subdomains_retrieve(struct ipa_subdomains_ctx *ctx, struct be_req *be_req)
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose{
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce struct ipa_subdomains_req_ctx *req_ctx = NULL;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce struct tevent_req *req;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce int dp_error = DP_ERR_FATAL;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose int ret;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx = talloc(be_req, struct ipa_subdomains_req_ctx);
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce if (req_ctx == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret = ENOMEM;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->be_req = be_req;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->sd_ctx = ctx;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->search_base_iter = 0;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->search_bases = ctx->search_bases;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->current_filter = NULL;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->reply_count = 0;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->reply = NULL;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req_ctx->sdap_op = sdap_id_op_create(req_ctx,
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce ctx->sdap_id_ctx->conn_cache);
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce if (req_ctx->sdap_op == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sdap_id_op_create failed.\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret = ENOMEM;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce req = sdap_id_op_connect_send(req_ctx->sdap_op, req_ctx, &ret);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (req == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sdap_id_op_connect_send failed: %d(%s).\n",
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret, strerror(ret)));
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce tevent_req_set_callback(req, ipa_subdomains_get_conn_done, req_ctx);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorcedone:
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce talloc_free(req_ctx);
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce if (ret == EOK) {
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce dp_error = DP_ERR_OK;
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce }
4c20fe34346919cf676c3e1b54b7701069e2aac6Simo Sorce ipa_subdomains_reply(be_req, dp_error, ret);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose}
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosestatic void ipa_subdomains_get_conn_done(struct tevent_req *req)
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose{
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose int ret;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose int dp_error = DP_ERR_FATAL;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct be_req *be_req;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct ipa_subdomains_req_ctx *ctx;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ctx = tevent_req_callback_data(req, struct ipa_subdomains_req_ctx);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret = sdap_id_op_connect_recv(req, &dp_error);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose talloc_zfree(req);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ret) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (dp_error == DP_ERR_OFFLINE) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose DEBUG(SSSDBG_MINOR_FAILURE,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ("No IPA server is available, cannot get the "
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose "subdomain list while offline\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose/* FIXME: return saved results ?? */
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose } else {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose DEBUG(SSSDBG_OP_FAILURE,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ("Failed to connect to IPA server: [%d](%s)\n",
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret, strerror(ret)));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto fail;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ipa_subdomains_handler_get(ctx, IPA_SUBDOMAINS_SLAVE);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ret != EOK && ret != EAGAIN) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto fail;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosefail:
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose be_req = ctx->be_req;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose talloc_free(ctx);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ipa_subdomains_reply(be_req, dp_error, ret);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose}
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystatic errno_t
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenyipa_subdomains_handler_get(struct ipa_subdomains_req_ctx *ctx,
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny enum ipa_subdomains_req_type type)
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose{
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct tevent_req *req;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct sdap_search_base *base;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny struct ipa_subdomains_req_params *params;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (type >= IPA_SUBDOMAINS_MAX) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return EINVAL;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny params = &subdomain_requests[type];
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose base = ctx->search_bases[ctx->search_base_iter];
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (base == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return EOK;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose talloc_free(ctx->current_filter);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ctx->current_filter = sdap_get_id_specific_filter(ctx, params->filter,
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce base->filter);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ctx->current_filter == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return ENOMEM;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce req = sdap_get_generic_send(ctx, ctx->sd_ctx->be_ctx->ev,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ctx->sd_ctx->sdap_id_ctx->opts,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose sdap_id_op_handle(ctx->sdap_op),
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose base->basedn, base->scope,
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ctx->current_filter, params->attrs, NULL, 0,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose dp_opt_get_int(ctx->sd_ctx->sdap_id_ctx->opts->basic,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose SDAP_SEARCH_TIMEOUT), false);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (req == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sdap_get_generic_send failed.\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return ENOMEM;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny tevent_req_set_callback(req, params->cb, ctx);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return EAGAIN;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose}
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosestatic void ipa_subdomains_handler_done(struct tevent_req *req)
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose{
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose int ret;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose size_t reply_count;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose struct sysdb_attrs **reply = NULL;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct ipa_subdomains_req_ctx *ctx;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct be_req *be_req;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny struct sysdb_ctx *sysdb;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce bool refresh_has_changes = false;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ctx = tevent_req_callback_data(req, struct ipa_subdomains_req_ctx);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce be_req = ctx->be_req;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce if (be_req && be_req->sysdb) {
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce sysdb = be_req->sysdb;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce } else {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce sysdb = ctx->sd_ctx->be_ctx->sysdb;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret = sdap_get_generic_recv(req, ctx, &reply_count, &reply);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose talloc_zfree(req);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ret != EOK) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sdap_get_generic_send request failed.\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (reply_count) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ctx->reply = talloc_realloc(ctx, ctx->reply, struct sysdb_attrs *,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ctx->reply_count + reply_count);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ctx->reply == NULL) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ret = ENOMEM;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose memcpy(ctx->reply+ctx->reply_count, reply,
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose reply_count * sizeof(struct sysdb_attrs *));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ctx->reply_count += reply_count;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose ctx->search_base_iter++;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ipa_subdomains_handler_get(ctx, IPA_SUBDOMAINS_SLAVE);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ret == EAGAIN) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose return;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose } else if (ret != EOK) {
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = ipa_subdomains_refresh(ctx->sd_ctx, ctx->reply_count, ctx->reply,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce &refresh_has_changes);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose if (ret != EOK) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Failed to refresh subdomains.\n"));
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose goto done;
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (refresh_has_changes) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ret = sysdb_update_subdomains(sysdb, ctx->sd_ctx->num_subdoms,
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce ctx->sd_ctx->subdoms);
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce if (ret != EOK) {
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce DEBUG(SSSDBG_OP_FAILURE, ("sysdb_update_subdomains failed.\n"));
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce goto done;
efea50efda58be66638e5d38c8e57fdf9992f204Simo Sorce }
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ret = ipa_subdomains_write_mappings(sysdb_ctx_get_domain(sysdb),
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ctx->sd_ctx->num_subdoms,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ctx->sd_ctx->subdoms);
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek if (ret != EOK) {
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek ("ipa_subdomains_write_mappings failed.\n"));
7197ce636c2b92152f5f6180bef6bda3752d148dJakub Hrozek }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ctx->search_base_iter = 0;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ctx->search_bases = ctx->sd_ctx->ranges_search_bases;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ipa_subdomains_handler_get(ctx, IPA_SUBDOMAINS_RANGES);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret == EAGAIN) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose return;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose } else if (ret != EOK) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("No search base for ranges available.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = EINVAL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosedone:
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_free(ctx);
a56156c13c71a96166b0a8f3921e67f36470f8d7Sumit Bose ipa_subdomains_reply(be_req, DP_ERR_FATAL, ret);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose}
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bosestatic void ipa_subdomains_handler_ranges_done(struct tevent_req *req)
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose{
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose errno_t ret;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce int dp_error = DP_ERR_FATAL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose size_t reply_count;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct sysdb_attrs **reply = NULL;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct ipa_subdomains_req_ctx *ctx;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct be_req *be_req;
b58460076fe843c11d736ae244c1ac979a6473a4Simo Sorce struct sysdb_subdom *domain_info;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct range_info **range_list = NULL;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose struct sysdb_ctx *sysdb;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ctx = tevent_req_callback_data(req, struct ipa_subdomains_req_ctx);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce be_req = ctx->be_req;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce if (be_req && be_req->sysdb) {
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce sysdb = be_req->sysdb;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce } else {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce sysdb = ctx->sd_ctx->be_ctx->sysdb;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sdap_get_generic_recv(req, ctx, &reply_count, &reply);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_zfree(req);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sdap_get_generic_send request failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = ipa_ranges_parse_results(ctx, reply_count, reply, &range_list);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce DEBUG(SSSDBG_OP_FAILURE,
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ("ipa_ranges_parse_results request failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose ret = sysdb_update_ranges(sysdb, range_list);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose talloc_free(range_list);
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose if (ret != EOK) {
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose DEBUG(SSSDBG_OP_FAILURE, ("sysdb_update_ranges failed.\n"));
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose goto done;
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose }
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
386a66b1aa18a176e6a06fa126556c9590c373b6Sumit Bose
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = sysdb_master_domain_get_info(ctx, sysdb, &domain_info);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (ret != EOK) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (domain_info->flat_name == NULL ||
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny domain_info->id == NULL ||
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny domain_info->name == NULL) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ctx->search_base_iter = 0;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ctx->search_bases = ctx->sd_ctx->master_search_bases;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ipa_subdomains_handler_get(ctx, IPA_SUBDOMAINS_MASTER);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (ret == EAGAIN) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny } else if (ret != EOK) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny } else {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = EOK;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenydone:
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny talloc_free(ctx);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce if (ret == EOK) {
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce dp_error = DP_ERR_OK;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce }
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ipa_subdomains_reply(be_req, dp_error, ret);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny}
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystatic void ipa_subdomains_handler_master_done(struct tevent_req *req)
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny{
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny errno_t ret;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce int dp_error = DP_ERR_FATAL;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny size_t reply_count;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny struct sysdb_attrs **reply = NULL;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct ipa_subdomains_req_ctx *ctx;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce struct be_req *be_req;
b58460076fe843c11d736ae244c1ac979a6473a4Simo Sorce struct sysdb_subdom *domain_info;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny const char *tmp_str;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ctx = tevent_req_callback_data(req, struct ipa_subdomains_req_ctx);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce be_req = ctx->be_req;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = sdap_get_generic_recv(req, ctx, &reply_count, &reply);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny talloc_zfree(req);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (ret != EOK) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny DEBUG(SSSDBG_OP_FAILURE, ("sdap_get_generic_send request failed.\n"));
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (reply_count) {
b58460076fe843c11d736ae244c1ac979a6473a4Simo Sorce domain_info = talloc_zero(ctx, struct sysdb_subdom);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (domain_info == NULL) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ENOMEM;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = sysdb_attrs_get_string(reply[0], IPA_FLATNAME, &tmp_str);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce if (ret != EOK) {
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce goto done;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny domain_info->flat_name = talloc_strdup(domain_info, tmp_str);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (domain_info->flat_name == NULL) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ENOMEM;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = sysdb_attrs_get_string(reply[0], IPA_SID, &tmp_str);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (ret != EOK) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny domain_info->id = talloc_strdup(domain_info, tmp_str);
c929c213c91b2f9d55f96d6964b9390636178991Simo Sorce if (domain_info->id == NULL) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ENOMEM;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ret = sysdb_master_domain_add_info(ctx->sd_ctx->be_ctx->sysdb, domain_info);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny } else {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ctx->search_base_iter++;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = ipa_subdomains_handler_get(ctx, IPA_SUBDOMAINS_MASTER);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (ret == EAGAIN) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny } else if (ret != EOK) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny /* Right now we know there has been an error
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * and we don't have the master domain record
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny DEBUG(SSSDBG_CRIT_FAILURE, ("Master domain record not found!\n"));
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ret = EIO;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny goto done;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose
81165faf5d951aca69f410713730c26ff048ec44Sumit Bosedone:
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose talloc_free(ctx);
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce if (ret == EOK) {
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce dp_error = DP_ERR_OK;
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce }
21f19d573047e70ee8ec0119ec00c1ed1af9ec04Simo Sorce ipa_subdomains_reply(be_req, dp_error, ret);
81165faf5d951aca69f410713730c26ff048ec44Sumit Bose}
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorcestatic void ipa_subdom_online_cb(void *pvt);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorcestatic void ipa_subdom_timer_refresh(struct tevent_context *ev,
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce struct tevent_timer *te,
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce struct timeval current_time,
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce void *pvt)
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce{
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce ipa_subdom_online_cb(pvt);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce}
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorcestatic void ipa_subdom_online_cb(void *pvt)
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce{
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce struct ipa_subdomains_ctx *ctx;
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce struct timeval tv;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ctx = talloc_get_type(pvt, struct ipa_subdomains_ctx);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce if (!ctx) {
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce DEBUG(SSSDBG_CRIT_FAILURE, ("Bad private pointer\n"));
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce return;
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce }
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ipa_subdomains_retrieve(ctx, NULL);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce tv = tevent_timeval_current_ofs(IPA_SUBDOMAIN_REFRESH_PERIOD, 0);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce ctx->timer_event = tevent_add_timer(ctx->be_ctx->ev, ctx, tv,
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce ipa_subdom_timer_refresh, ctx);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce if (!ctx->timer_event) {
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to add subdom timer event\n"));
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce }
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce}
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorcestatic void ipa_subdom_offline_cb(void *pvt)
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce{
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce struct ipa_subdomains_ctx *ctx;
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce ctx = talloc_get_type(pvt, struct ipa_subdomains_ctx);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce if (ctx) {
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce talloc_zfree(ctx->timer_event);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce }
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce}
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorcevoid ipa_subdomains_handler(struct be_req *be_req)
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce{
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce struct ipa_subdomains_ctx *ctx;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ctx = talloc_get_type(be_req->be_ctx->bet_info[BET_SUBDOMAINS].pvt_bet_data,
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce struct ipa_subdomains_ctx);
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce if (!ctx) {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ipa_subdomains_reply(be_req, DP_ERR_FATAL, EINVAL);
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce return;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce }
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce if (ctx->last_refreshed > time(NULL) - IPA_SUBDOMAIN_REFRESH_LIMIT) {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ipa_subdomains_reply(be_req, DP_ERR_OK, EOK);
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce return;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce }
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ipa_subdomains_retrieve(ctx, be_req);
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce}
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorcestruct bet_ops ipa_subdomains_ops = {
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce .handler = ipa_subdomains_handler,
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce .finalize = NULL
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce};
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorceint ipa_subdom_init(struct be_ctx *be_ctx,
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct ipa_id_ctx *id_ctx,
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct bet_ops **ops,
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce void **pvt_data)
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce{
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce struct ipa_subdomains_ctx *ctx;
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce int ret;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce ctx = talloc_zero(id_ctx, struct ipa_subdomains_ctx);
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce if (ctx == NULL) {
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_zero failed.\n"));
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce return ENOMEM;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce }
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ctx->be_ctx = be_ctx;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce ctx->sdap_id_ctx = id_ctx->sdap_id_ctx;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce ctx->search_bases = id_ctx->ipa_options->subdomains_search_bases;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce ctx->master_search_bases = id_ctx->ipa_options->master_domain_search_bases;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce ctx->ranges_search_bases = id_ctx->ipa_options->ranges_search_bases;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce *ops = &ipa_subdomains_ops;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce *pvt_data = ctx;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce ret = be_add_online_cb(ctx, be_ctx, ipa_subdom_online_cb, ctx, NULL);
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce if (ret != EOK) {
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to add subdom online callback"));
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce }
6a81cb8c3424dbe9f764af3738299cbbe5874a15Simo Sorce
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce ret = be_add_offline_cb(ctx, be_ctx, ipa_subdom_offline_cb, ctx, NULL);
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce if (ret != EOK) {
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to add subdom offline callback"));
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce }
3b533d57a737e2de1b3e85b073b14d3bfb49dafcSimo Sorce
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce return EOK;
87ed72b47859e673b636c85f35b85f1546c7ed3dSimo Sorce}