796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher/*
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher SSSD
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher Authors:
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher Copyright (C) 2012 Red Hat
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher This program is free software; you can redistribute it and/or modify
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher it under the terms of the GNU General Public License as published by
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher the Free Software Foundation; either version 3 of the License, or
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher (at your option) any later version.
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher This program is distributed in the hope that it will be useful,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher GNU General Public License for more details.
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher You should have received a copy of the GNU General Public License
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher*/
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher#include "util/util.h"
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher#include "db/sysdb.h"
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher#include "db/sysdb_services.h"
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher#include "providers/ldap/sdap_async_private.h"
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher#include "providers/ldap/ldap_common.h"
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstruct sdap_get_services_state {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct tevent_context *ev;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_options *opts;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_handle *sh;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sss_domain_info *dom;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_ctx *sysdb;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char **attrs;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char *base_filter;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char *filter;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher int timeout;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher bool enumeration;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char *higher_usn;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs **services;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher size_t count;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher size_t base_iter;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_search_base **search_bases;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher};
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic errno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_next_base(struct tevent_req *req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic void
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_process(struct tevent_req *subreq);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic errno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_save_services(TALLOC_CTX *memctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_ctx *sysdb,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sss_domain_info *dom,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_options *opts,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs **services,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher size_t num_services,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char **_usn_value);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic errno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_save_service(TALLOC_CTX *mem_ctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_ctx *sysdb,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_options *opts,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sss_domain_info *dom,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs *attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char **_usn_value,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher time_t now);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstruct tevent_req *
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_send(TALLOC_CTX *memctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct tevent_context *ev,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sss_domain_info *dom,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_ctx *sysdb,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_options *opts,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_search_base **search_bases,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_handle *sh,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char **attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char *filter,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher int timeout,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher bool enumeration)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher{
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher errno_t ret;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct tevent_req *req;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_get_services_state *state;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher req = tevent_req_create(memctx, &state, struct sdap_get_services_state);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!req) return NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->ev = ev;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->opts = opts;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->dom = dom;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->sh = sh;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->sysdb = sysdb;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->attrs = attrs;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->higher_usn = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->services = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->count = 0;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->timeout = timeout;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->base_filter = filter;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->base_iter = 0;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases = search_bases;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->enumeration = enumeration;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher if (!state->search_bases) {
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Services lookup request without a search base\n");
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher ret = EINVAL;
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher goto done;
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher }
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_get_services_next_base(req);
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagherdone:
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_error(req, ret);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_post(req, state->ev);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return req;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher}
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic errno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_next_base(struct tevent_req *req)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher{
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct tevent_req *subreq;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_get_services_state *state;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state = tevent_req_data(req, struct sdap_get_services_state);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_zfree(state->filter);
92ec40e6aa25f75903ffdb166a8ec56b67bfd77dPavel Březina state->filter = sdap_combine_filters(state, state->base_filter,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases[state->base_iter]->filter);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!state->filter) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return ENOMEM;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_TRACE_FUNC,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Searching for services with base [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov state->search_bases[state->base_iter]->basedn);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher subreq = sdap_get_generic_send(
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state, state->ev, state->opts, state->sh,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases[state->base_iter]->basedn,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases[state->base_iter]->scope,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->filter, state->attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->opts->service_map, SDAP_OPTS_SERVICES,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher state->timeout,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher state->enumeration); /* If we're enumerating, we need paging */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!subreq) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return ENOMEM;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_set_callback(subreq, sdap_get_services_process, req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return EOK;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher}
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic void
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_process(struct tevent_req *subreq)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher{
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct tevent_req *req =
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_callback_data(subreq, struct tevent_req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_get_services_state *state =
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_data(req, struct sdap_get_services_state);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher int ret;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher size_t count, i;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs **services;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher bool next_base = false;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_get_generic_recv(subreq, state,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher &count, &services);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_zfree(subreq);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_error(req, ret);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_TRACE_FUNC,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Search for services, returned %zu results.\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov count);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (state->enumeration || count == 0) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* No services found in this search or enumerating */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher next_base = true;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Add this batch of sevices to the list */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (count > 0) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->services =
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_realloc(state,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->services,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs *,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->count + count + 1);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!state->services) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_error(req, ENOMEM);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Copy the new services into the list
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher for (i = 0; i < count; i++) {
3a0522759a7e71b40cc88dba0f5c545b049cf9bfJakub Hrozek state->services[state->count + i] =
3a0522759a7e71b40cc88dba0f5c545b049cf9bfJakub Hrozek talloc_steal(state->services, services[i]);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->count += count;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->services[state->count] = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (next_base) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->base_iter++;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (state->search_bases[state->base_iter]) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* There are more search bases to try */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_get_services_next_base(req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_error(req, ret);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* No more search bases
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher * Return ENOENT if no services were found
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (state->count == 0) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_error(req, ENOENT);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_save_services(state, state->sysdb,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->dom, state->opts,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->services, state->count,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher &state->higher_usn);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to store services.\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_error(req, ret);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Saving %zu services - Done\n", state->count);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_done(req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher}
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic errno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_save_services(TALLOC_CTX *mem_ctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_ctx *sysdb,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sss_domain_info *dom,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_options *opts,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs **services,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher size_t num_services,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char **_usn_value)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher{
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher errno_t ret, sret;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher time_t now;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher size_t i;
ae765daed93b64b9b4177466a2191561b6734e63Stephen Gallagher bool in_transaction = false;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char *higher_usn = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char *usn_value;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher TALLOC_CTX *tmp_ctx;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (num_services == 0) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Nothing to do */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return ENOENT;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tmp_ctx = talloc_new(NULL);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!tmp_ctx) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return ENOMEM;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_transaction_start(sysdb);
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek goto done;
21d485184df986e1a123f70c689517386e51a5ceMichal Zidek }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher in_transaction = true;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher now = time(NULL);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher for (i = 0; i < num_services; i++) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher usn_value = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_save_service(tmp_ctx, sysdb, opts, dom,
3ff729e6c8a371e7a52914772816c39ca73c50a9Jan Zeleny services[i],
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher &usn_value, now);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Do not fail completely on errors.
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher * Just report the failure to save and go on */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to store service %zu. Ignoring.\n", i);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher } else {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Service [%zu/%zu] processed!\n", i, num_services);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (usn_value) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (higher_usn) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if ((strlen(usn_value) > strlen(higher_usn)) ||
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher (strcmp(usn_value, higher_usn) > 0)) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_zfree(higher_usn);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher higher_usn = usn_value;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher } else {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_zfree(usn_value);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher } else {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher higher_usn = usn_value;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_transaction_commit(sysdb);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to commit transaction!\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher in_transaction = false;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (_usn_value) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher *_usn_value = talloc_steal(mem_ctx, higher_usn);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherdone:
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (in_transaction) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher sret = sysdb_transaction_cancel(sysdb);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (sret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to cancel transaction!\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_free(tmp_ctx);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return ret;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher}
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherstatic errno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_save_service(TALLOC_CTX *mem_ctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_ctx *sysdb,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_options *opts,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sss_domain_info *dom,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs *attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char **_usn_value,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher time_t now)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher{
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher errno_t ret;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher TALLOC_CTX *tmp_ctx = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sysdb_attrs *svc_attrs;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct ldb_message_element *el;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char *usn_value = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char *name = NULL;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char **aliases;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher const char **protocols;
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek const char **cased_protocols;
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek const char **store_protocols;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char **missing;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher uint16_t port;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher uint64_t cache_timeout;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "Saving service\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tmp_ctx = talloc_new(NULL);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!tmp_ctx) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = ENOMEM;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher svc_attrs = sysdb_new_attrs(tmp_ctx);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!svc_attrs) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = ENOMEM;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Identify the primary name of this services */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_primary_name(
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher sysdb, attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher opts->service_map[SDAP_AT_SERVICE_NAME].name,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher &name);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not determine the primary name of the service\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Primary name: [%s]\n", name);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Handle any available aliases */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_aliases(tmp_ctx, attrs, name,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher !dom->case_sensitive,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher &aliases);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify service aliases\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Get the port number */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_uint16_t(attrs, SYSDB_SVC_PORT, &port);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify service port: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Get the protocols this service offers on that port */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_string_array(attrs, SYSDB_SVC_PROTO,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tmp_ctx, &protocols);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify service protocols: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek if (dom->case_sensitive == false) {
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek /* Don't perform the extra mallocs if not necessary */
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek ret = sss_get_cased_name_list(tmp_ctx, protocols,
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek dom->case_sensitive, &cased_protocols);
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek if (ret != EOK) {
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to get case_sensitive protocols names: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek goto done;
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek }
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek }
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek store_protocols = dom->case_sensitive ? protocols : cased_protocols;
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Get the USN value, if available */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_el(attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher opts->service_map[SDAP_AT_SERVICE_USN].sys_name, &el);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret && ret != ENOENT) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to retrieve USN value: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret == ENOENT || el->num_values == 0) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_TRACE_LIBS,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Original USN value is not available for [%s].\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov name);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher } else {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_add_string(svc_attrs,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher opts->service_map[SDAP_AT_SERVICE_USN].sys_name,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher (const char*)el->values[0].data);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to add USN value: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher usn_value = talloc_strdup(tmp_ctx, (const char*)el->values[0].data);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (!usn_value) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = ENOMEM;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Make sure to remove any extra attributes from the sysdb
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher * that have been removed from LDAP
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = list_missing_attrs(svc_attrs, opts->service_map, SDAP_OPTS_SERVICES,
3ff729e6c8a371e7a52914772816c39ca73c50a9Jan Zeleny attrs, &missing);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify removed attributes: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher cache_timeout = dom->service_timeout;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_store_service(dom, name, port, aliases, store_protocols,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher svc_attrs, missing, cache_timeout, now);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret != EOK) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to store service in the sysdb: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov strerror(ret));
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher goto done;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher *_usn_value = talloc_steal(mem_ctx, usn_value);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagherdone:
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher talloc_free(tmp_ctx);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return ret;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher}
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghererrno_t
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_recv(TALLOC_CTX *mem_ctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct tevent_req *req,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher char **usn_value)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher{
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher struct sdap_get_services_state *state =
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_data(req, struct sdap_get_services_state);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher TEVENT_REQ_RETURN_ON_ERROR(req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (usn_value) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher *usn_value = talloc_steal(mem_ctx, state->higher_usn);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher }
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher return EOK;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher}
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher/* Enumeration routines */
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherstruct enum_services_state {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct tevent_context *ev;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct sdap_id_ctx *id_ctx;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct sdap_id_op *op;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct sss_domain_info *domain;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct sysdb_ctx *sysdb;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher char *filter;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher const char **attrs;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher};
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherstatic void
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherenum_services_op_done(struct tevent_req *subreq);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherstruct tevent_req *
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherenum_services_send(TALLOC_CTX *memctx,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct tevent_context *ev,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct sdap_id_ctx *id_ctx,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct sdap_id_op *op,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher bool purge)
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher{
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher errno_t ret;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct tevent_req *req;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct tevent_req *subreq;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct enum_services_state *state;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher req = tevent_req_create(memctx, &state, struct enum_services_state);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (!req) return NULL;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->ev = ev;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->id_ctx = id_ctx;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->domain = id_ctx->be->domain;
df0596ec12bc5091608371e2977f3111241e8cafSimo Sorce state->sysdb = id_ctx->be->domain->sysdb;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->op = op;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (id_ctx->srv_opts && id_ctx->srv_opts->max_service_value && !purge) {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->filter = talloc_asprintf(
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher "(&(objectclass=%s)(%s=*)(%s=*)(%s=*)(%s>=%s)(!(%s=%s)))",
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_OC_SERVICE].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_NAME].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_PORT].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_PROTOCOL].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_USN].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->srv_opts->max_service_value,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_USN].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->srv_opts->max_service_value);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher } else {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->filter = talloc_asprintf(
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher "(&(objectclass=%s)(%s=*)(%s=*)(%s=*))",
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_OC_SERVICE].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_NAME].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_PORT].name,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher id_ctx->opts->service_map[SDAP_AT_SERVICE_PROTOCOL].name);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher }
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (!state->filter) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "Failed to build base filter\n");
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher ret = ENOMEM;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher goto fail;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher }
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher ret = build_attrs_from_map(state, id_ctx->opts->service_map,
64ddff90c7fcc02ccb06824ac93af7d5f361a88fJan Zeleny SDAP_OPTS_SERVICES, NULL,
64ddff90c7fcc02ccb06824ac93af7d5f361a88fJan Zeleny &state->attrs, NULL);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (ret != EOK) goto fail;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher subreq = sdap_get_services_send(state, state->ev,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->domain, state->sysdb,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->id_ctx->opts,
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek state->id_ctx->opts->sdom->service_search_bases,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher sdap_id_op_handle(state->op),
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->attrs, state->filter,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher dp_opt_get_int(state->id_ctx->opts->basic,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher SDAP_SEARCH_TIMEOUT),
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher true);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (!subreq) {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher ret = ENOMEM;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher goto fail;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher }
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_set_callback(subreq, enum_services_op_done, req);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher return req;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherfail:
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_error(req, ret);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_post(req, ev);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher return req;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher}
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherstatic void
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherenum_services_op_done(struct tevent_req *subreq)
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher{
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct tevent_req *req =
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_callback_data(subreq, struct tevent_req);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher struct enum_services_state *state =
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_data(req, struct enum_services_state);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher char *usn_value;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher char *endptr = NULL;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher unsigned usn_number;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher int ret;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher ret = sdap_get_services_recv(state, subreq, &usn_value);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher talloc_zfree(subreq);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (ret != EOK) {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_error(req, ret);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher return;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher }
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (usn_value) {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher talloc_zfree(state->id_ctx->srv_opts->max_service_value);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->id_ctx->srv_opts->max_service_value =
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher talloc_steal(state->id_ctx, usn_value);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher usn_number = strtoul(usn_value, &endptr, 10);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if ((endptr == NULL || (*endptr == '\0' && endptr != usn_value))
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher && (usn_number > state->id_ctx->srv_opts->last_usn)) {
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher state->id_ctx->srv_opts->last_usn = usn_number;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher }
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher }
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_FUNC_DATA, "Services higher USN value: [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov state->id_ctx->srv_opts->max_service_value);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_done(req);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher}
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallaghererrno_t
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherenum_services_recv(struct tevent_req *req)
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher{
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher TEVENT_REQ_RETURN_ON_ERROR(req);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher return EOK;
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher}