796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher Copyright (C) 2012 Red Hat
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 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 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#include "providers/ldap/sdap_async_private.h"
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_next_base(struct tevent_req *req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_process(struct tevent_req *subreq);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher req = tevent_req_create(memctx, &state, struct sdap_get_services_state);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Services lookup request without a search base\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_next_base(struct tevent_req *req)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state = tevent_req_data(req, struct sdap_get_services_state);
92ec40e6aa25f75903ffdb166a8ec56b67bfd77dPavel Březina state->filter = sdap_combine_filters(state, state->base_filter,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases[state->base_iter]->filter);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Searching for services with base [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov state->search_bases[state->base_iter]->basedn);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases[state->base_iter]->basedn,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->search_bases[state->base_iter]->scope,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher state->opts->service_map, SDAP_OPTS_SERVICES,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher state->enumeration); /* If we're enumerating, we need paging */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_set_callback(subreq, sdap_get_services_process, req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_process(struct tevent_req *subreq)
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_callback_data(subreq, struct tevent_req);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_data(req, struct sdap_get_services_state);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_get_generic_recv(subreq, state,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Search for services, returned %zu results.\n",
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* No services found in this search or enumerating */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Add this batch of sevices to the list */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Copy the new services into the list
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher for (i = 0; i < count; i++) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (state->search_bases[state->base_iter]) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* There are more search bases to try */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* No more search bases
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher * Return ENOENT if no services were found
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_save_services(state, state->sysdb,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to store services.\n");
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Saving %zu services - Done\n", state->count);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Nothing to do */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher for (i = 0; i < num_services; i++) {
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sdap_save_service(tmp_ctx, sysdb, opts, dom,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Do not fail completely on errors.
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher * Just report the failure to save and go on */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to store service %zu. Ignoring.\n", i);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Service [%zu/%zu] processed!\n", i, num_services);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if ((strlen(usn_value) > strlen(higher_usn)) ||
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to commit transaction!\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher *_usn_value = talloc_steal(mem_ctx, higher_usn);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to cancel transaction!\n");
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "Saving service\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Identify the primary name of this services */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher opts->service_map[SDAP_AT_SERVICE_NAME].name,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Could not determine the primary name of the service\n");
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Primary name: [%s]\n", name);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Handle any available aliases */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_aliases(tmp_ctx, attrs, name,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify service aliases\n");
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Get the port number */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_uint16_t(attrs, SYSDB_SVC_PORT, &port);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify service port: [%s]\n",
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Get the protocols this service offers on that port */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = sysdb_attrs_get_string_array(attrs, SYSDB_SVC_PROTO,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify service protocols: [%s]\n",
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek /* Don't perform the extra mallocs if not necessary */
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek ret = sss_get_cased_name_list(tmp_ctx, protocols,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to get case_sensitive protocols names: [%s]\n",
ef6dbf7b5a7a0550628b4a1aa4d60cddef556158Jakub Hrozek store_protocols = dom->case_sensitive ? protocols : cased_protocols;
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Get the USN value, if available */
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher opts->service_map[SDAP_AT_SERVICE_USN].sys_name, &el);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to retrieve USN value: [%s]\n",
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher if (ret == ENOENT || el->num_values == 0) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Original USN value is not available for [%s].\n",
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher opts->service_map[SDAP_AT_SERVICE_USN].sys_name,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to add USN value: [%s]\n",
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher usn_value = talloc_strdup(tmp_ctx, (const char*)el->values[0].data);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher /* Make sure to remove any extra attributes from the sysdb
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher * that have been removed from LDAP
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher ret = list_missing_attrs(svc_attrs, opts->service_map, SDAP_OPTS_SERVICES,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to identify removed attributes: [%s]\n",
4fcc50e133f90cd4c5931a3ac48c84cb628b16fcMichal Zidek ret = sysdb_store_service(dom, name, port, aliases, store_protocols,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Failed to store service in the sysdb: [%s]\n",
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher *_usn_value = talloc_steal(mem_ctx, usn_value);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallaghersdap_get_services_recv(TALLOC_CTX *mem_ctx,
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher tevent_req_data(req, struct sdap_get_services_state);
796463906a54e259bd5b582ce84af4297a58eafcStephen Gallagher *usn_value = talloc_steal(mem_ctx, state->higher_usn);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher/* Enumeration routines */
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherenum_services_op_done(struct tevent_req *subreq);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher req = tevent_req_create(memctx, &state, struct enum_services_state);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher if (id_ctx->srv_opts && id_ctx->srv_opts->max_service_value && !purge) {
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->opts->service_map[SDAP_AT_SERVICE_USN].name,
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);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_MINOR_FAILURE, "Failed to build base filter\n");
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher ret = build_attrs_from_map(state, id_ctx->opts->service_map,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher subreq = sdap_get_services_send(state, state->ev,
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek state->id_ctx->opts->sdom->service_search_bases,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher dp_opt_get_int(state->id_ctx->opts->basic,
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_set_callback(subreq, enum_services_op_done, req);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagherenum_services_op_done(struct tevent_req *subreq)
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_callback_data(subreq, struct tevent_req);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher tevent_req_data(req, struct enum_services_state);
e2925c2d7d10cbb51098402233784044168f1a77Stephen Gallagher ret = sdap_get_services_recv(state, subreq, &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 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;
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_FUNC_DATA, "Services higher USN value: [%s]\n",