e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny/*
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny SSSD
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny Async IPA Helper routines for netgroups
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny Authors:
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny Jan Zeleny <jzeleny@redhat.com>
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny Copyright (C) 2011 Red Hat
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny This program is free software; you can redistribute it and/or modify
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny it under the terms of the GNU General Public License as published by
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny the Free Software Foundation; either version 3 of the License, or
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny (at your option) any later version.
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny This program is distributed in the hope that it will be useful,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny but WITHOUT ANY WARRANTY; without even the implied warranty of
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny GNU General Public License for more details.
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny You should have received a copy of the GNU General Public License
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny along with this program. If not, see <http://www.gnu.org/licenses/>.
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny*/
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#include "util/util.h"
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#include "db/sysdb.h"
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#include "providers/ldap/sdap_async_private.h"
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#include "providers/ipa/ipa_id.h"
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#include "db/sysdb.h"
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#include <ctype.h>
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#define ENTITY_NG 1
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#define ENTITY_USER 2
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny#define ENTITY_HOST 4
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystruct ipa_get_netgroups_state {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_context *ev;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_options *opts;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_options *ipa_opts;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_handle *sh;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_ctx *sysdb;
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher struct sss_domain_info *dom;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **attrs;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int timeout;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *base_filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t netgr_base_iter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t host_base_iter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t user_base_iter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* Entities which have been already asked for
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * and are scheduled for inspection */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_table_t *new_netgroups;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_table_t *new_users;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_table_t *new_hosts;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int current_entity;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int entities_found;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs **netgroups;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int netgroups_count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny};
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic errno_t ipa_save_netgroup(TALLOC_CTX *mem_ctx,
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher struct sss_domain_info *dom,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_options *opts,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs *attrs)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ldb_message_element *el;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs *netgroup_attrs;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *name = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t c;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(attrs,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny opts->netgroup_map[IPA_AT_NETGROUP_NAME].sys_name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (el->num_values == 0) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = EINVAL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny name = (const char *)el->values[0].data;
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Storing netgroup %s\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny netgroup_attrs = sysdb_new_attrs(mem_ctx);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (!netgroup_attrs) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(attrs, SYSDB_ORIG_DN, &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (el->num_values == 0) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "Original DN is not available for [%s].\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "Adding original DN [%s] to attributes of [%s].\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov el->values[0].data, name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_add_string(netgroup_attrs, SYSDB_ORIG_DN,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny (const char *)el->values[0].data);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(attrs, SYSDB_NETGROUP_TRIPLE, &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (el->num_values == 0) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "No netgroup triples for netgroup [%s].\n", name);
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny ret = sysdb_attrs_get_el(netgroup_attrs, SYSDB_NETGROUP_TRIPLE, &el);
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny if (ret != EOK) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny goto fail;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for(c = 0; c < el->num_values; c++) {
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech ret = sysdb_attrs_add_string_safe(netgroup_attrs,
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech SYSDB_NETGROUP_TRIPLE,
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech (const char*)el->values[c].data);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(attrs,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny opts->netgroup_map[IPA_AT_NETGROUP_MEMBER].sys_name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (el->num_values == 0) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "No original members for netgroup [%s]\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "Adding original members to netgroup [%s]\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for(c = 0; c < el->num_values; c++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_add_string(netgroup_attrs,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny opts->netgroup_map[IPA_AT_NETGROUP_MEMBER].sys_name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny (const char*)el->values[c].data);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(attrs, SYSDB_NETGROUP_MEMBER, &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (el->num_values == 0) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS, "No members for netgroup [%s]\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS, "Adding members to netgroup [%s]\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for(c = 0; c < el->num_values; c++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_add_string(netgroup_attrs, SYSDB_NETGROUP_MEMBER,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny (const char*)el->values[c].data);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_FUNC, "Storing info for netgroup %s\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
d115f40c7a3999e3cbe705a2ff9cf0fd493f80fbMichal Zidek ret = sysdb_add_netgroup(dom, name, NULL, netgroup_attrs, NULL,
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher dom->netgroup_timeout, 0);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenyfail:
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_OP_FAILURE, "Failed to save netgroup %s\n", name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic errno_t ipa_netgr_next_base(struct tevent_req *req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic void ipa_get_netgroups_process(struct tevent_req *subreq);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_process_all(struct ipa_get_netgroups_state *state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystruct tevent_req *ipa_get_netgroups_send(TALLOC_CTX *memctx,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_context *ev,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_ctx *sysdb,
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher struct sss_domain_info *dom,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_options *opts,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_options *ipa_options,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_handle *sh,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **attrs,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *filter,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int timeout)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state *state;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny req = tevent_req_create(memctx, &state, struct ipa_get_netgroups_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (!req) return NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->ev = ev;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->opts = opts;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->ipa_opts = ipa_options;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->sh = sh;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->sysdb = sysdb;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->attrs = attrs;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->timeout = timeout;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->base_filter = filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgr_base_iter = 0;
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher state->dom = dom;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek if (!ipa_options->id->sdom->netgroup_search_bases) {
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Netgroup lookup request without a search base\n");
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher ret = EINVAL;
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher goto done;
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher }
169fa5bd3edd34aa0db35681832bd7406e423c1bStephen Gallagher
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sss_hash_create(state, 32, &state->new_netgroups);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sss_hash_create(state, 32, &state->new_users);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sss_hash_create(state, 32, &state->new_hosts);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_next_base(req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenydone:
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_error(req, ret);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_post(req, ev);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return req;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic errno_t ipa_netgr_next_base(struct tevent_req *req)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *subreq;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state *state;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_search_base **netgr_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state = tevent_req_data(req, struct ipa_get_netgroups_state);
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek netgr_bases = state->ipa_opts->id->sdom->netgroup_search_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_zfree(state->filter);
92ec40e6aa25f75903ffdb166a8ec56b67bfd77dPavel Bƙezina state->filter = sdap_combine_filters(
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->base_filter,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny netgr_bases[state->netgr_base_iter]->filter);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (!state->filter) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny DEBUG(SSSDBG_TRACE_FUNC,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Searching for netgroups with base [%s]\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov netgr_bases[state->netgr_base_iter]->basedn);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny subreq = sdap_get_generic_send(
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state, state->ev, state->opts, state->sh,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny netgr_bases[state->netgr_base_iter]->basedn,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny netgr_bases[state->netgr_base_iter]->scope,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->filter, state->attrs,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->opts->netgroup_map, IPA_OPTS_NETGROUP,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher state->timeout,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher true);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (!subreq) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_set_callback(subreq, ipa_get_netgroups_process, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_fetch_netgroups(struct ipa_get_netgroups_state *state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_fetch_users(struct ipa_get_netgroups_state *state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_fetch_hosts(struct ipa_get_netgroups_state *state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic void ipa_netgr_members_process(struct tevent_req *subreq);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic void ipa_get_netgroups_process(struct tevent_req *subreq)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req = tevent_req_callback_data(subreq,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state *state = tevent_req_data(req,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int i, ret;
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik struct ldb_message_element *el;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_search_base **netgr_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs **netgroups;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t netgroups_count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *orig_dn;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *dn;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny bool fetch_members = false;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_key_t key;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_value_t value;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek netgr_bases = state->ipa_opts->id->sdom->netgroup_search_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sdap_get_generic_recv(subreq, state, &netgroups_count, &netgroups);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_zfree(subreq);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_FUNC, "Search for netgroups, returned %zu results.\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov netgroups_count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (netgroups_count == 0) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* No netgroups found in this search */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgr_base_iter++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (netgr_bases[state->netgr_base_iter]) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* There are more search bases to try */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_next_base(req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_error(req, ENOENT);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOENT;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny filter = talloc_strdup(state, "(|");
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (filter == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for (i = 0; i < netgroups_count; i++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(netgroups[i], SYSDB_ORIG_NETGROUP_MEMBER,
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik if (el->num_values) state->entities_found |= ENTITY_NG;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
9674f0f018c65a9af6b18dd0a4e515f726803d27Jan Zeleny ret = sysdb_attrs_get_el(netgroups[i], SYSDB_ORIG_MEMBER_USER,
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik if (el->num_values) state->entities_found |= ENTITY_USER;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
9674f0f018c65a9af6b18dd0a4e515f726803d27Jan Zeleny ret = sysdb_attrs_get_el(netgroups[i], SYSDB_ORIG_MEMBER_HOST,
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
c048657aa2fbb246b5dc199ef6101bfd6e5eeaeaLukas Slebodnik if (el->num_values) state->entities_found |= ENTITY_HOST;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_string(netgroups[i], SYSDB_ORIG_DN, &orig_dn);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.type = HASH_KEY_STRING;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny value.type = HASH_VALUE_PTR;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.str = discard_const(orig_dn);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny value.ptr = netgroups[i];
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = hash_enter(state->new_netgroups, &key, &value);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != HASH_SUCCESS) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->entities_found == 0) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny continue;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sss_filter_sanitize(state, orig_dn, &dn);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* Add this to the filter */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny filter = talloc_asprintf_append(filter, "(%s=%s)",
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->opts->netgroup_map[IPA_AT_NETGROUP_MEMBER_OF].name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny dn);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (filter == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny fetch_members = true;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (!fetch_members) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_process_all(state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_error(req, ret);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_done(req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->filter = talloc_asprintf_append(filter, ")");
0368c4b1a1341889bb86a80887d4c91d224f339cJan Zeleny if (state->filter == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->entities_found & ENTITY_NG) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgr_base_iter = 0;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_fetch_netgroups(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else if (state->entities_found & ENTITY_USER) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_fetch_users(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else if (state->entities_found & ENTITY_HOST) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_fetch_hosts(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenydone:
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_error(req, ret);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_fetch_netgroups(struct ipa_get_netgroups_state *state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *base_filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *subreq;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_search_base **bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek bases = state->ipa_opts->id->sdom->netgroup_search_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (bases[state->netgr_base_iter] == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* No more bases to try */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOENT;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny base_filter = bases[state->netgr_base_iter]->filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny filter = talloc_asprintf(state, "(&%s%s(objectclass=%s))",
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->filter,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny base_filter?base_filter:"",
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->opts->netgroup_map[SDAP_OC_NETGROUP].name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (filter == NULL)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny subreq = sdap_get_generic_send(state, state->ev, state->opts, state->sh,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny bases[state->netgr_base_iter]->basedn,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny bases[state->netgr_base_iter]->scope,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny filter, state->attrs, state->opts->netgroup_map,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher IPA_OPTS_NETGROUP, state->timeout, true);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->current_entity = ENTITY_NG;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (subreq == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_set_callback(subreq, ipa_netgr_members_process, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_fetch_users(struct ipa_get_netgroups_state *state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *attrs[] = { state->opts->user_map[SDAP_AT_USER_NAME].name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->opts->user_map[SDAP_AT_USER_MEMBEROF].name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny "objectclass", NULL };
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *base_filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *subreq;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_search_base **bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
749cfb5d3270b5daf389d51a0dbd3fd2aec6e05dJakub Hrozek bases = state->ipa_opts->id->sdom->user_search_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (bases[state->user_base_iter] == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOENT;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny base_filter = bases[state->user_base_iter]->filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny filter = talloc_asprintf(state, "(&%s%s(objectclass=%s))",
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->filter,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny base_filter?base_filter:"",
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->opts->user_map[SDAP_OC_USER].name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (filter == NULL)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny subreq = sdap_get_generic_send(state, state->ev, state->opts, state->sh,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny dp_opt_get_string(state->opts->basic,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny SDAP_USER_SEARCH_BASE),
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny LDAP_SCOPE_SUBTREE,
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher filter, attrs, state->opts->user_map,
4dd38025efda88f123eac672f87d3cda12f050c8Jakub Hrozek state->opts->user_map_cnt,
4dd38025efda88f123eac672f87d3cda12f050c8Jakub Hrozek state->timeout, true);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->current_entity = ENTITY_USER;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (subreq == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_free(attrs);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_set_callback(subreq, ipa_netgr_members_process, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_fetch_hosts(struct ipa_get_netgroups_state *state,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **attrs;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *base_filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *subreq;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sdap_search_base **bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev bases = state->ipa_opts->id->sdom->host_search_bases;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (bases[state->host_base_iter] == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOENT;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny base_filter = bases[state->host_base_iter]->filter;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny filter = talloc_asprintf(state, "(&%s%s(objectclass=%s))",
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->filter,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev base_filter ? base_filter : "",
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->ipa_opts->id->host_map[SDAP_OC_HOST].name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (filter == NULL)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = build_attrs_from_map(state, state->ipa_opts->id->host_map,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev SDAP_OPTS_HOST, NULL, &attrs, NULL);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_free(filter);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny subreq = sdap_get_generic_send(state, state->ev, state->opts, state->sh,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny bases[state->host_base_iter]->basedn,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny bases[state->host_base_iter]->scope,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev filter, attrs, state->ipa_opts->id->host_map,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev SDAP_OPTS_HOST, state->timeout, true);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->current_entity = ENTITY_HOST;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (subreq == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_free(filter);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_set_callback(subreq, ipa_netgr_members_process, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic void ipa_netgr_members_process(struct tevent_req *subreq)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *req = tevent_req_callback_data(subreq,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state *state = tevent_req_data(req,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs **entities;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int ret, i;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *orig_dn;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_table_t *table;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_key_t key;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_value_t value;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int (* next_call)(struct ipa_get_netgroups_state *,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct tevent_req *);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny bool next_batch_scheduled = false;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sdap_get_generic_recv(subreq, state, &count, &entities);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_zfree(subreq);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Found %zu members in current search base\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny next_call = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* While processing a batch of entities from one search base,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * schedule query for another search base if there is one
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny *
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * If there is no other search base, another class of entities
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * will be scheduled for lookup after processing of current
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * batch. The order of lookup is: netgroups -> users -> hosts
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->current_entity == ENTITY_NG) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* We just received a batch of netgroups */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgr_base_iter++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_fetch_netgroups(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny table = state->new_netgroups;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* If there is a member netgroup, we always have to
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * ask for both member users and hosts
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * -> now schedule users
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny next_call = ipa_netgr_fetch_users;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else if (state->current_entity == ENTITY_USER) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* We just received a batch of users */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->user_base_iter++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_fetch_users(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny table = state->new_users;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->entities_found & ENTITY_HOST ||
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->entities_found & ENTITY_NG) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny next_call = ipa_netgr_fetch_hosts;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else if (state->current_entity == ENTITY_HOST) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* We just received a batch of hosts */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->host_base_iter++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_fetch_hosts(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny table = state->new_hosts;
92ebfa89209b57ba1d9a202543ec32486036f6f0Jan Zeleny } else {
92ebfa89209b57ba1d9a202543ec32486036f6f0Jan Zeleny DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "Invalid entity type given for processing: %d\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov state->current_entity);
92ebfa89209b57ba1d9a202543ec32486036f6f0Jan Zeleny ret = EINVAL;
92ebfa89209b57ba1d9a202543ec32486036f6f0Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret == EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* Next search base has been scheduled for inspection,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * don't try to look for other type of entities
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny next_batch_scheduled = true;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else if (ret != ENOENT) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* Process all member entites and store them in the designated hash table */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.type = HASH_KEY_STRING;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny value.type = HASH_VALUE_PTR;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for (i = 0; i < count; i++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_string(entities[i], SYSDB_ORIG_DN, &orig_dn);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
ff565da1011aa4312847e28e7af66e57fccf8b90Michal Ćœidek key.str = talloc_strdup(table, orig_dn);
ff565da1011aa4312847e28e7af66e57fccf8b90Michal Ćœidek if (key.str == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
ff565da1011aa4312847e28e7af66e57fccf8b90Michal Ćœidek
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny value.ptr = entities[i];
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = hash_enter(table, &key, &value);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != HASH_SUCCESS) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (next_batch_scheduled) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* The next search base is already scheduled to be searched */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (next_call) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* There is another class of members that has to be retrieved
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny * - schedule the lookup
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = next_call(state, req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* All members, that could have been fetched, were fetched */
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ipa_netgr_process_all(state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) goto fail;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_done(req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenyfail:
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny tevent_req_error(req, ret);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic bool extract_netgroups(hash_entry_t *entry, void *pvt)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state *state;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state = talloc_get_type(pvt, struct ipa_get_netgroups_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgroups[state->netgroups_count] = talloc_get_type(entry->value.ptr,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgroups_count++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return true;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystruct extract_state {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *group;
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech const char *appropriateMemberOf;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **entries;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int entries_count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny};
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zelenystatic bool extract_entities(hash_entry_t *entry, void *pvt)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech int ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct extract_state *state;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs *member;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ldb_message_element *el;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ldb_message_element *name_el;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state = talloc_get_type(pvt, struct extract_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny member = talloc_get_type(entry->value.ptr, struct sysdb_attrs);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech ret = sysdb_attrs_get_el(member, state->appropriateMemberOf, &el);
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech if (ret != EOK) {
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech return false;
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(member, SYSDB_NAME, &name_el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK || name_el == NULL || name_el->num_values == 0) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return false;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech for (int j = 0; j < el->num_values; j++) {
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech if (strcmp((char *)el->values[j].data, state->group) == 0) {
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech state->entries = talloc_realloc(state, state->entries,
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech const char *,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->entries_count + 1);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->entries == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return false;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->entries[state->entries_count] = (char *)name_el->values[0].data;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->entries_count++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny break;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return true;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int extract_members(TALLOC_CTX *mem_ctx,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs *netgroup,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *member_type,
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech const char *appropriateMemberOf,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_table_t *lookup_table,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char ***_ret_array,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int *_ret_count)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct extract_state *state;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ldb_message_element *el;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs *member;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_key_t key;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_value_t value;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **process = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **ret_array = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int process_count = 0;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int ret_count = 0;
029e6f1727972ffe31f97c7738a98d0efecb3e15Jakub Hrozek int ret, i, pi;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.type = HASH_KEY_STRING;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny value.type = HASH_VALUE_PTR;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state = talloc_zero(mem_ctx, struct extract_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech state->appropriateMemberOf = appropriateMemberOf;
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(netgroup, member_type, &el);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK && ret != ENOENT) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret == EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for (i = 0; i < el->num_values; i++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.str = (char *)el->values[i].data;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = hash_lookup(lookup_table, &key, &value);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != HASH_SUCCESS && ret != HASH_ERROR_KEY_NOT_FOUND) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOENT;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret == HASH_ERROR_KEY_NOT_FOUND) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny process = talloc_realloc(mem_ctx, process, const char *, process_count + 1);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (process == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny process[process_count] = (char *)el->values[i].data;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny process_count++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret_array = talloc_realloc(mem_ctx, ret_array, const char *, ret_count + 1);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret_array == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny member = talloc_get_type(value.ptr, struct sysdb_attrs);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_string(member, SYSDB_NAME, &ret_array[ret_count]);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret_count++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
029e6f1727972ffe31f97c7738a98d0efecb3e15Jakub Hrozek for (pi = 0; pi < process_count; pi++) {
029e6f1727972ffe31f97c7738a98d0efecb3e15Jakub Hrozek state->group = process[pi];
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny hash_iterate(lookup_table, extract_entities, state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->entries_count > 0) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret_array = talloc_realloc(mem_ctx, ret_array, const char *,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret_count + state->entries_count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret_array == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny memcpy(&ret_array[ret_count], state->entries,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->entries_count*sizeof(const char *));
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret_count += state->entries_count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->entries_count = 0;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_zfree(state->entries);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny } else {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret_array = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny *_ret_array = ret_array;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny *_ret_count = ret_count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenydone:
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenystatic int ipa_netgr_process_all(struct ipa_get_netgroups_state *state)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int i, j, k, ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **members;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs *member;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *member_name;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct extract_state *extract_state;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ldb_message_element *external_hosts;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *dash[] = {"-"};
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **uids = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char **hosts = NULL;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int uids_count = 0;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny int hosts_count = 0;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_key_t key;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_value_t value;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny const char *domain;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny char *triple;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->netgroups = talloc_zero_array(state, struct sysdb_attrs *,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_count(state->new_netgroups));
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (state->netgroups == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny extract_state = talloc_zero(state, struct extract_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (extract_state == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.type = HASH_KEY_STRING;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny value.type = HASH_VALUE_PTR;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hash_iterate(state->new_netgroups, extract_netgroups, state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for (i = 0; i < state->netgroups_count; i++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* load all its member netgroups, translate */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Extracting netgroup members of netgroup %d\n", i);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_string_array(state->netgroups[i],
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny SYSDB_ORIG_NETGROUP_MEMBER,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state, &members);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK && ret != ENOENT) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
57170bd05e64559a604c1907c59728e8d9e83cd4Stephen Gallagher j = 0;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret == EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for (j = 0; members[j]; j++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny key.str = discard_const(members[j]);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = hash_lookup(state->new_netgroups, &key, &value);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != HASH_SUCCESS) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOENT;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny member = talloc_get_type(value.ptr, struct sysdb_attrs);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_string(member, SYSDB_NAME, &member_name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_add_string(state->netgroups[i],
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny SYSDB_NETGROUP_MEMBER,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny member_name);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny talloc_zfree(members);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Extracted %d netgroup members\n", j);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny /* Load all UIDs */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "Extracting user members of netgroup %d\n", i);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = extract_members(state, state->netgroups[i],
9674f0f018c65a9af6b18dd0a4e515f726803d27Jan Zeleny SYSDB_ORIG_MEMBER_USER,
e6595222c41af84288d303e8d464ce45b1408ed3Petr Cech state->ipa_opts->id->user_map[SDAP_AT_USER_MEMBEROF].sys_name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->new_users,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny &uids, &uids_count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Extracted %d user members\n", uids_count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "Extracting host members of netgroup %d\n", i);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = extract_members(state, state->netgroups[i],
9674f0f018c65a9af6b18dd0a4e515f726803d27Jan Zeleny SYSDB_ORIG_MEMBER_HOST,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->ipa_opts->id->host_map[SDAP_AT_HOST_MEMBER_OF].sys_name,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny state->new_hosts,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny &hosts, &hosts_count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Extracted %d host members\n", hosts_count);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_el(state->netgroups[i],
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny SYSDB_ORIG_NETGROUP_EXTERNAL_HOST,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny &external_hosts);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (external_hosts->num_values > 0) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hosts = talloc_realloc(state, hosts, const char *,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hosts_count + external_hosts->num_values);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (hosts == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny for (j = 0; j < external_hosts->num_values; j++) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hosts[hosts_count] = talloc_strdup(hosts, (char *)external_hosts->values[j].data);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (hosts[hosts_count] == NULL) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = ENOMEM;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny hosts_count++;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny ret = sysdb_attrs_get_string(state->netgroups[i], SYSDB_NETGROUP_DOMAIN,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny &domain);
67645557dc0301e7ea66bba15c4ff3fb11f9540eJakub Hrozek if (ret == ENOENT) {
67645557dc0301e7ea66bba15c4ff3fb11f9540eJakub Hrozek domain = NULL;
67645557dc0301e7ea66bba15c4ff3fb11f9540eJakub Hrozek } else if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny if (uids_count > 0 || hosts_count > 0) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny if (uids_count == 0) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny uids_count = 1;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny uids = dash;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny if (hosts_count == 0) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny hosts_count = 1;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny hosts = dash;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL, "Putting together triples of "
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "netgroup %d\n", i);
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny for (j = 0; j < uids_count; j++) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny for (k = 0; k < hosts_count; k++) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny triple = talloc_asprintf(state, "(%s,%s,%s)",
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny hosts[k], uids[j],
67645557dc0301e7ea66bba15c4ff3fb11f9540eJakub Hrozek domain ? domain : "");
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny if (triple == NULL) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny ret = ENOMEM;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny goto done;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny ret = sysdb_attrs_add_string(state->netgroups[i],
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny SYSDB_NETGROUP_TRIPLE,
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny triple);
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny if (ret != EOK) {
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny goto done;
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
3e1c814a7ca3a0e4086e0822d6b4df23b8465bc8Jan Zeleny
feffebb989dc5e24a707a7266cc467da23087241Lukas Slebodnik ret = ipa_save_netgroup(state, state->dom,
bd92e8ee315d4da9350b9ef0358c88a7b54aeebeStephen Gallagher state->opts, state->netgroups[i]);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (ret != EOK) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny goto done;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
66b927a4f19f18f2ed07be416747817037b47909Jakub Hrozek ret = EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenydone:
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return ret;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zelenyint ipa_get_netgroups_recv(struct tevent_req *req,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny TALLOC_CTX *mem_ctx,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny size_t *reply_count,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct sysdb_attrs ***reply)
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny{
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state *state = tevent_req_data(req,
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny struct ipa_get_netgroups_state);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny TEVENT_REQ_RETURN_ON_ERROR(req);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (reply_count) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny *reply_count = state->netgroups_count;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny if (reply) {
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny *reply = talloc_steal(mem_ctx, state->netgroups);
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny }
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny return EOK;
e526b608657f229f7486b3aa8c53b0f2c53b42b1Jan Zeleny}