ldap_id_enum.c revision 9e99e3c5c45b20189e76e4d2546966ff1fb3ce40
2d2eda71267231c2526be701fe655db125852c1ffielding/*
f062ed7bd262a37a909dd77ce5fc23b446818823fielding SSSD
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding LDAP Identity Enumeration
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
2d2eda71267231c2526be701fe655db125852c1ffielding Authors:
2d2eda71267231c2526be701fe655db125852c1ffielding Simo Sorce <ssorce@redhat.com>
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding Copyright (C) 2009 Red Hat
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding This program is free software; you can redistribute it and/or modify
f062ed7bd262a37a909dd77ce5fc23b446818823fielding it under the terms of the GNU General Public License as published by
2d2eda71267231c2526be701fe655db125852c1ffielding the Free Software Foundation; either version 3 of the License, or
2d2eda71267231c2526be701fe655db125852c1ffielding (at your option) any later version.
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding This program is distributed in the hope that it will be useful,
2d2eda71267231c2526be701fe655db125852c1ffielding but WITHOUT ANY WARRANTY; without even the implied warranty of
2d2eda71267231c2526be701fe655db125852c1ffielding MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f062ed7bd262a37a909dd77ce5fc23b446818823fielding GNU General Public License for more details.
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding You should have received a copy of the GNU General Public License
f062ed7bd262a37a909dd77ce5fc23b446818823fielding along with this program. If not, see <http://www.gnu.org/licenses/>.
f062ed7bd262a37a909dd77ce5fc23b446818823fielding*/
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
2d2eda71267231c2526be701fe655db125852c1ffielding#include <errno.h>
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include <time.h>
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include <sys/time.h>
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "util/util.h"
2d2eda71267231c2526be701fe655db125852c1ffielding#include "db/sysdb.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "providers/ldap/ldap_common.h"
f062ed7bd262a37a909dd77ce5fc23b446818823fielding#include "providers/ldap/sdap_async.h"
64185f9824e42f21ca7b9ae6c004484215c031a7rbb
2d2eda71267231c2526be701fe655db125852c1ffieldingextern struct tevent_req *ldap_id_cleanup_send(TALLOC_CTX *memctx,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct tevent_context *ev,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct sdap_id_ctx *ctx);
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fielding/* ==Enumeration-Task===================================================== */
2d2eda71267231c2526be701fe655db125852c1ffielding
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingstatic struct tevent_req *ldap_id_enumerate_send(struct tevent_context *ev,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct sdap_id_ctx *ctx);
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingstatic void ldap_id_enumerate_reschedule(struct tevent_req *req);
f062ed7bd262a37a909dd77ce5fc23b446818823fielding
f062ed7bd262a37a909dd77ce5fc23b446818823fieldingstatic void ldap_id_enumerate_timeout(struct tevent_context *ev,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct tevent_timer *te,
2d2eda71267231c2526be701fe655db125852c1ffielding struct timeval tv, void *pvt);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enumerate_timer(struct tevent_context *ev,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct tevent_timer *tt,
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct timeval tv, void *pvt)
f062ed7bd262a37a909dd77ce5fc23b446818823fielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx = talloc_get_type(pvt, struct sdap_id_ctx);
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct tevent_timer *timeout;
f062ed7bd262a37a909dd77ce5fc23b446818823fielding struct tevent_req *req;
f062ed7bd262a37a909dd77ce5fc23b446818823fielding int delay;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (be_is_offline(ctx->be)) {
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(4, ("Backend is marked offline, retry later!\n"));
2d2eda71267231c2526be701fe655db125852c1ffielding /* schedule starting from now, not the last run */
2d2eda71267231c2526be701fe655db125852c1ffielding delay = dp_opt_get_int(ctx->opts->basic, SDAP_ENUM_REFRESH_TIMEOUT);
2d2eda71267231c2526be701fe655db125852c1ffielding tv = tevent_timeval_current_ofs(delay, 0);
2d2eda71267231c2526be701fe655db125852c1ffielding ldap_id_enumerate_set_timer(ctx, tv);
2d2eda71267231c2526be701fe655db125852c1ffielding return;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding req = ldap_id_enumerate_send(ev, ctx);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!req) {
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(1, ("Failed to schedule enumeration, retrying later!\n"));
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* schedule starting from now, not the last run */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb delay = dp_opt_get_int(ctx->opts->basic, SDAP_ENUM_REFRESH_TIMEOUT);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tv = tevent_timeval_current_ofs(delay, 0);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ldap_id_enumerate_set_timer(ctx, tv);
952908500d5f99f35afc5ed510391b9bdc3833farbb return;
952908500d5f99f35afc5ed510391b9bdc3833farbb }
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_set_callback(req, ldap_id_enumerate_reschedule, ctx);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
2d2eda71267231c2526be701fe655db125852c1ffielding /* if enumeration takes so long, either we try to enumerate too
30c289e6bc6d28d210b21edd800ab2cfc78a8381wrowe * frequently, or something went seriously wrong */
bd53cb2bf4d77574fd502e1c02d8c3c0d5431967stoddard delay = dp_opt_get_int(ctx->opts->basic, SDAP_ENUM_REFRESH_TIMEOUT);
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb tv = tevent_timeval_current_ofs(delay, 0);
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb timeout = tevent_add_timer(ctx->be->ev, req, tv,
44c46ef733836b32585d135d2d90856e7cfd9929rbb ldap_id_enumerate_timeout, req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return;
2d2eda71267231c2526be701fe655db125852c1ffielding}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic void ldap_id_enumerate_timeout(struct tevent_context *ev,
14bea4ba98aabaf554e37165a07123bb05d6736bstoddard struct tevent_timer *te,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct timeval tv, void *pvt)
9731f9232bddd7dbac757c780b2b1a2a6931dce7stoddard{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req *req = talloc_get_type(pvt, struct tevent_req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct sdap_id_ctx *ctx = tevent_req_callback_data(req,
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx);
2d2eda71267231c2526be701fe655db125852c1ffielding int delay;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding delay = dp_opt_get_int(ctx->opts->basic, SDAP_ENUM_REFRESH_TIMEOUT);
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(1, ("Enumeration timed out! Timeout too small? (%ds)!\n", delay));
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding tv = tevent_timeval_current_ofs(delay, 0);
2d2eda71267231c2526be701fe655db125852c1ffielding ldap_id_enumerate_set_timer(ctx, tv);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(req);
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enumerate_reschedule(struct tevent_req *req)
2e123e8beedc9f921448c113e2d6823a92fd5261fielding{
2e123e8beedc9f921448c113e2d6823a92fd5261fielding struct sdap_id_ctx *ctx = tevent_req_callback_data(req,
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx);
2d2eda71267231c2526be701fe655db125852c1ffielding enum tevent_req_state tstate;
2d2eda71267231c2526be701fe655db125852c1ffielding uint64_t err;
2d2eda71267231c2526be701fe655db125852c1ffielding struct timeval tv;
2d2eda71267231c2526be701fe655db125852c1ffielding int delay;
2d2eda71267231c2526be701fe655db125852c1ffielding
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (tevent_req_is_error(req, &tstate, &err)) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* On error schedule starting from now, not the last run */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tv = tevent_timeval_current();
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb } else {
72a4ef8eac1adef882246c5bfb9b8bbd82d613c4coar tv = ctx->last_enum;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb talloc_zfree(req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
2d2eda71267231c2526be701fe655db125852c1ffielding delay = dp_opt_get_int(ctx->opts->basic, SDAP_ENUM_REFRESH_TIMEOUT);
2d2eda71267231c2526be701fe655db125852c1ffielding tv = tevent_timeval_add(&tv, delay, 0);
2d2eda71267231c2526be701fe655db125852c1ffielding ldap_id_enumerate_set_timer(ctx, tv);
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingint ldap_id_enumerate_set_timer(struct sdap_id_ctx *ctx, struct timeval tv)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_timer *enum_task;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(6, ("Scheduling next enumeration at %ld.%ld\n",
57edbe3cb9356a0b599c7b07f3aae0e721ee57e2coar (long)tv.tv_sec, (long)tv.tv_usec));
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding enum_task = tevent_add_timer(ctx->be->ev, ctx,
2d2eda71267231c2526be701fe655db125852c1ffielding tv, ldap_id_enumerate_timer, ctx);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!enum_task) {
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(0, ("FATAL: failed to setup enumeration task!\n"));
2d2eda71267231c2526be701fe655db125852c1ffielding return EFAULT;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return EOK;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#define MAX_ENUM_RESTARTS 3
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
2d2eda71267231c2526be701fe655db125852c1ffieldingstruct global_enum_state {
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_context *ev;
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding bool purge;
2d2eda71267231c2526be701fe655db125852c1ffielding int restarts;
2d2eda71267231c2526be701fe655db125852c1ffielding};
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_context *ev,
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx,
2d2eda71267231c2526be701fe655db125852c1ffielding bool purge);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enum_users_done(struct tevent_req *subreq);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_context *ev,
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx,
2d2eda71267231c2526be701fe655db125852c1ffielding bool purge);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enum_groups_done(struct tevent_req *subreq);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enum_cleanup_done(struct tevent_req *subreq);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int ldap_id_enum_users_restart(struct tevent_req *req);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic int ldap_id_enum_groups_restart(struct tevent_req *req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic struct tevent_req *ldap_id_enumerate_send(struct tevent_context *ev,
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct global_enum_state *state;
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req *req, *subreq;
2d2eda71267231c2526be701fe655db125852c1ffielding int t;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding req = tevent_req_create(ctx, &state, struct global_enum_state);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!req) return NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding state->ev = ev;
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx = ctx;
2d2eda71267231c2526be701fe655db125852c1ffielding state->restarts = 0;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->last_enum = tevent_timeval_current();
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding t = dp_opt_get_int(ctx->opts->basic, SDAP_CACHE_PURGE_TIMEOUT);
2d2eda71267231c2526be701fe655db125852c1ffielding if ((ctx->last_purge.tv_sec + t) < ctx->last_enum.tv_sec) {
2d2eda71267231c2526be701fe655db125852c1ffielding state->purge = true;
2d2eda71267231c2526be701fe655db125852c1ffielding } else {
2d2eda71267231c2526be701fe655db125852c1ffielding state->purge = false;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding subreq = enum_users_send(state, ev, ctx, state->purge);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!subreq) {
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(req);
2d2eda71267231c2526be701fe655db125852c1ffielding return NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_set_callback(subreq, ldap_id_enum_users_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return req;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enum_users_done(struct tevent_req *subreq)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req *req = tevent_req_callback_data(subreq,
e44e11f9fece12c783f18d033923bfc0d6b4289aake struct tevent_req);
2d2eda71267231c2526be701fe655db125852c1ffielding struct global_enum_state *state = tevent_req_data(req,
2d2eda71267231c2526be701fe655db125852c1ffielding struct global_enum_state);
2d2eda71267231c2526be701fe655db125852c1ffielding enum tevent_req_state tstate;
2d2eda71267231c2526be701fe655db125852c1ffielding uint64_t err = 0;
e44e11f9fece12c783f18d033923bfc0d6b4289aake int ret;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (tevent_req_is_error(subreq, &tstate, &err)) {
2d2eda71267231c2526be701fe655db125852c1ffielding if (tstate != TEVENT_REQ_USER_ERROR) {
2d2eda71267231c2526be701fe655db125852c1ffielding err = EIO;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding if (err != ENOENT) {
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(subreq);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding subreq = enum_groups_send(state, state->ev, state->ctx, state->purge);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!subreq) {
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_set_callback(subreq, ldap_id_enum_groups_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingfail:
2d2eda71267231c2526be701fe655db125852c1ffielding if (err) {
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(9, ("User enumeration failed with: (%d)[%s]\n",
2d2eda71267231c2526be701fe655db125852c1ffielding (int)err, strerror(err)));
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (sdap_check_gssapi_reconnect(state->ctx)) {
2d2eda71267231c2526be701fe655db125852c1ffielding if (state->ctx->gsh) {
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx->gsh->connected = false;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding ret = ldap_id_enum_users_restart(req);
2d2eda71267231c2526be701fe655db125852c1ffielding if (ret == EOK) return;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding sdap_mark_offline(state->ctx);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(1, ("Failed to enumerate users, retrying later!\n"));
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_done(req);
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void ldap_id_enum_groups_done(struct tevent_req *subreq)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req *req = tevent_req_callback_data(subreq,
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req);
2d2eda71267231c2526be701fe655db125852c1ffielding struct global_enum_state *state = tevent_req_data(req,
2d2eda71267231c2526be701fe655db125852c1ffielding struct global_enum_state);
2d2eda71267231c2526be701fe655db125852c1ffielding enum tevent_req_state tstate;
2d2eda71267231c2526be701fe655db125852c1ffielding uint64_t err = 0;
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet int ret;
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet if (tevent_req_is_error(subreq, &tstate, &err)) {
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet if (tstate != TEVENT_REQ_USER_ERROR) {
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet err = EIO;
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet }
2efb935ae8fe12d5192a3bf2c52c28461b6c68afdgaudet if (err != ENOENT) {
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(subreq);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (state->purge) {
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding subreq = ldap_id_cleanup_send(state, state->ev, state->ctx);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!subreq) {
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_set_callback(subreq, ldap_id_enum_cleanup_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_done(req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
32644678e889a3253f71bde0b3d6daea6d9dc21awrowefail:
32644678e889a3253f71bde0b3d6daea6d9dc21awrowe /* check if credentials are expired otherwise go offline on failures */
725c954c86847e3c0dd394907a20b273ee8126earbb if (sdap_check_gssapi_reconnect(state->ctx)) {
32644678e889a3253f71bde0b3d6daea6d9dc21awrowe if (state->ctx->gsh) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb state->ctx->gsh->connected = false;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ret = ldap_id_enum_groups_restart(req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (ret == EOK) return;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb sdap_mark_offline(state->ctx);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb DEBUG(1, ("Failed to enumerate groups (%d [%s]), retrying later!\n",
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb (int)err, strerror(err)));
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_done(req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic void ldap_id_enum_cleanup_done(struct tevent_req *subreq)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req *req = tevent_req_callback_data(subreq,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb talloc_zfree(subreq);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_done(req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic void ldap_id_enum_users_immediate(struct tevent_context *ctx,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_immediate *im,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb void *private_data)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req *req = talloc_get_type(private_data,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state *state = tevent_req_data(req,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req *subreq;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb subreq = enum_users_send(state, state->ev, state->ctx, state->purge);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (subreq == NULL) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_error(req, ENOMEM);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_set_callback(subreq, ldap_id_enum_users_done, req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic int ldap_id_enum_users_restart(struct tevent_req *req)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state *state = tevent_req_data(req,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_immediate *im;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb state->restarts++;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (state->restarts < MAX_ENUM_RESTARTS) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return ELOOP;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb im = tevent_create_immediate(req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (!im) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return ENOMEM;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb /* schedule a completely new event to avoid deep recursions */
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_schedule_immediate(im, state->ev,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb ldap_id_enum_users_immediate, req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return EOK;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic void ldap_id_enum_groups_immediate(struct tevent_context *ctx,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_immediate *im,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb void *private_data)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req *req = talloc_get_type(private_data,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state *state = tevent_req_data(req,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct tevent_req *subreq;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb subreq = enum_groups_send(state, state->ev, state->ctx, state->purge);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb if (subreq == NULL) {
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_error(req, ENOMEM);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb return;
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb }
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb tevent_req_set_callback(subreq, ldap_id_enum_groups_done, req);
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb}
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbbstatic int ldap_id_enum_groups_restart(struct tevent_req *req)
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb{
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state *state = tevent_req_data(req,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct global_enum_state);
2e123e8beedc9f921448c113e2d6823a92fd5261fielding struct tevent_immediate *im;
2d2eda71267231c2526be701fe655db125852c1ffielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm state->restarts++;
2d2eda71267231c2526be701fe655db125852c1ffielding if (state->restarts < MAX_ENUM_RESTARTS) {
2d2eda71267231c2526be701fe655db125852c1ffielding return ELOOP;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding im = tevent_create_immediate(req);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!im) {
725c954c86847e3c0dd394907a20b273ee8126earbb return ENOMEM;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* schedule a completely new event to avoid deep recursions */
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_schedule_immediate(im, state->ev,
2d2eda71267231c2526be701fe655db125852c1ffielding ldap_id_enum_groups_immediate, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
7463de0c603f07c9e1820e812d2f1a73661843e6rbb return EOK;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding/* ==User-Enumeration===================================================== */
2d2eda71267231c2526be701fe655db125852c1ffielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstruct enum_users_state {
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_context *ev;
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding char *filter;
2d2eda71267231c2526be701fe655db125852c1ffielding const char **attrs;
2d2eda71267231c2526be701fe655db125852c1ffielding};
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void enum_users_connect_done(struct tevent_req *subreq);
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void enum_users_op_done(struct tevent_req *subreq);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_context *ev,
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx,
2d2eda71267231c2526be701fe655db125852c1ffielding bool purge)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req *req, *subreq;
2d2eda71267231c2526be701fe655db125852c1ffielding struct enum_users_state *state;
2d2eda71267231c2526be701fe655db125852c1ffielding int ret;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding req = tevent_req_create(memctx, &state, struct enum_users_state);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!req) return NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding state->ev = ev;
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx = ctx;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (ctx->max_user_timestamp && !purge) {
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding state->filter = talloc_asprintf(state,
2d2eda71267231c2526be701fe655db125852c1ffielding "(&(%s=*)(objectclass=%s)(%s>=%s)(!(%s=%s)))",
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->opts->user_map[SDAP_AT_USER_NAME].name,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->opts->user_map[SDAP_OC_USER].name,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->opts->user_map[SDAP_AT_USER_MODSTAMP].name,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->max_user_timestamp,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->opts->user_map[SDAP_AT_USER_MODSTAMP].name,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->max_user_timestamp);
2d2eda71267231c2526be701fe655db125852c1ffielding } else {
2d2eda71267231c2526be701fe655db125852c1ffielding state->filter = talloc_asprintf(state,
2d2eda71267231c2526be701fe655db125852c1ffielding "(&(%s=*)(objectclass=%s))",
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->opts->user_map[SDAP_AT_USER_NAME].name,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->opts->user_map[SDAP_OC_USER].name);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding if (!state->filter) {
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(2, ("Failed to build filter\n"));
2d2eda71267231c2526be701fe655db125852c1ffielding ret = ENOMEM;
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* TODO: handle attrs_type */
2d2eda71267231c2526be701fe655db125852c1ffielding ret = build_attrs_from_map(state, ctx->opts->user_map,
2d2eda71267231c2526be701fe655db125852c1ffielding SDAP_OPTS_USER, &state->attrs);
2d2eda71267231c2526be701fe655db125852c1ffielding if (ret != EOK) goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (!sdap_connected(ctx)) {
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding /* FIXME: add option to decide if tls should be used
2d2eda71267231c2526be701fe655db125852c1ffielding * or SASL/GSSAPI, etc ... */
2d2eda71267231c2526be701fe655db125852c1ffielding subreq = sdap_cli_connect_send(state, ev, ctx->opts,
2d2eda71267231c2526be701fe655db125852c1ffielding ctx->be, ctx->service,
2d2eda71267231c2526be701fe655db125852c1ffielding &ctx->rootDSE);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!subreq) {
2d2eda71267231c2526be701fe655db125852c1ffielding ret = ENOMEM;
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_set_callback(subreq, enum_users_connect_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return req;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding subreq = sdap_get_users_send(state, state->ev,
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx->be->domain,
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx->be->sysdb,
763f7b125b6d3dd1e4992a3822005efa2616f983coar state->ctx->opts,
763f7b125b6d3dd1e4992a3822005efa2616f983coar state->ctx->gsh,
2d2eda71267231c2526be701fe655db125852c1ffielding state->attrs, state->filter);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!subreq) {
2d2eda71267231c2526be701fe655db125852c1ffielding ret = ENOMEM;
2d2eda71267231c2526be701fe655db125852c1ffielding goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_set_callback(subreq, enum_users_op_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return req;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingfail:
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_error(req, ret);
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_post(req, ev);
2d2eda71267231c2526be701fe655db125852c1ffielding return req;
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void enum_users_connect_done(struct tevent_req *subreq)
2d2eda71267231c2526be701fe655db125852c1ffielding{
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req *req = tevent_req_callback_data(subreq,
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req);
2d2eda71267231c2526be701fe655db125852c1ffielding struct enum_users_state *state = tevent_req_data(req,
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar struct enum_users_state);
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar int ret;
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar ret = sdap_cli_connect_recv(subreq, state->ctx,
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar &state->ctx->gsh, &state->ctx->rootDSE);
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar talloc_zfree(subreq);
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (ret) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (ret == ENOTSUP) {
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar DEBUG(0, ("Authentication mechanism not Supported by server"));
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_error(req, ret);
2d2eda71267231c2526be701fe655db125852c1ffielding return;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
54e94821097724bf413d2d4cc70711760f7494e1trawick subreq = sdap_get_users_send(state, state->ev,
54e94821097724bf413d2d4cc70711760f7494e1trawick state->ctx->be->domain,
54e94821097724bf413d2d4cc70711760f7494e1trawick state->ctx->be->sysdb,
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx->opts, state->ctx->gsh,
54e94821097724bf413d2d4cc70711760f7494e1trawick state->attrs, state->filter);
54e94821097724bf413d2d4cc70711760f7494e1trawick if (!subreq) {
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_error(req, ENOMEM);
2d2eda71267231c2526be701fe655db125852c1ffielding return;
54e94821097724bf413d2d4cc70711760f7494e1trawick }
54e94821097724bf413d2d4cc70711760f7494e1trawick tevent_req_set_callback(subreq, enum_users_op_done, req);
54e94821097724bf413d2d4cc70711760f7494e1trawick}
54e94821097724bf413d2d4cc70711760f7494e1trawick
54e94821097724bf413d2d4cc70711760f7494e1trawickstatic void enum_users_op_done(struct tevent_req *subreq)
54e94821097724bf413d2d4cc70711760f7494e1trawick{
54e94821097724bf413d2d4cc70711760f7494e1trawick struct tevent_req *req = tevent_req_callback_data(subreq,
54e94821097724bf413d2d4cc70711760f7494e1trawick struct tevent_req);
54e94821097724bf413d2d4cc70711760f7494e1trawick struct enum_users_state *state = tevent_req_data(req,
54e94821097724bf413d2d4cc70711760f7494e1trawick struct enum_users_state);
54e94821097724bf413d2d4cc70711760f7494e1trawick char *timestamp;
54e94821097724bf413d2d4cc70711760f7494e1trawick int ret;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding ret = sdap_get_users_recv(subreq, state, &timestamp);
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(subreq);
2d2eda71267231c2526be701fe655db125852c1ffielding if (ret) {
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_error(req, ret);
2d2eda71267231c2526be701fe655db125852c1ffielding return;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding if (timestamp) {
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(state->ctx->max_user_timestamp);
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx->max_user_timestamp = talloc_steal(state->ctx, timestamp);
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding DEBUG(4, ("Users higher timestamp: [%s]\n",
2d2eda71267231c2526be701fe655db125852c1ffielding state->ctx->max_user_timestamp));
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_done(req);
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding/* =Group-Enumeration===================================================== */
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstruct enum_groups_state {
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_context *ev;
2d2eda71267231c2526be701fe655db125852c1ffielding struct sdap_id_ctx *ctx;
2d2eda71267231c2526be701fe655db125852c1ffielding
952908500d5f99f35afc5ed510391b9bdc3833farbb char *filter;
952908500d5f99f35afc5ed510391b9bdc3833farbb const char **attrs;
2d2eda71267231c2526be701fe655db125852c1ffielding};
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingstatic void enum_groups_connect_done(struct tevent_req *subreq);
952908500d5f99f35afc5ed510391b9bdc3833farbbstatic void enum_groups_op_done(struct tevent_req *subreq);
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbbstatic struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
952908500d5f99f35afc5ed510391b9bdc3833farbb struct tevent_context *ev,
952908500d5f99f35afc5ed510391b9bdc3833farbb struct sdap_id_ctx *ctx,
952908500d5f99f35afc5ed510391b9bdc3833farbb bool purge)
952908500d5f99f35afc5ed510391b9bdc3833farbb{
2d2eda71267231c2526be701fe655db125852c1ffielding struct tevent_req *req, *subreq;
2d2eda71267231c2526be701fe655db125852c1ffielding struct enum_groups_state *state;
2d2eda71267231c2526be701fe655db125852c1ffielding const char *attr_name;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde int ret;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde
3887202241db08986e94b252fbd06a55e55d4b2dbhyde req = tevent_req_create(memctx, &state, struct enum_groups_state);
3887202241db08986e94b252fbd06a55e55d4b2dbhyde if (!req) return NULL;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde
3887202241db08986e94b252fbd06a55e55d4b2dbhyde state->ev = ev;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde state->ctx = ctx;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde
2d2eda71267231c2526be701fe655db125852c1ffielding attr_name = ctx->opts->group_map[SDAP_AT_GROUP_NAME].name;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde
2d2eda71267231c2526be701fe655db125852c1ffielding if (ctx->max_group_timestamp && !purge) {
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding state->filter = talloc_asprintf(state,
2d2eda71267231c2526be701fe655db125852c1ffielding "(&(%s=*)(objectclass=%s)(%s>=%s)(!(%s=%s)))",
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick ctx->opts->group_map[SDAP_AT_GROUP_NAME].name,
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick ctx->opts->group_map[SDAP_OC_GROUP].name,
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick ctx->opts->group_map[SDAP_AT_GROUP_MODSTAMP].name,
952908500d5f99f35afc5ed510391b9bdc3833farbb ctx->max_group_timestamp,
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick ctx->opts->group_map[SDAP_AT_GROUP_MODSTAMP].name,
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick ctx->max_group_timestamp);
952908500d5f99f35afc5ed510391b9bdc3833farbb } else {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm state->filter = talloc_asprintf(state,
952908500d5f99f35afc5ed510391b9bdc3833farbb "(&(%s=*)(objectclass=%s))",
952908500d5f99f35afc5ed510391b9bdc3833farbb ctx->opts->group_map[SDAP_AT_GROUP_NAME].name,
952908500d5f99f35afc5ed510391b9bdc3833farbb ctx->opts->group_map[SDAP_OC_GROUP].name);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick if (!state->filter) {
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick DEBUG(2, ("Failed to build filter\n"));
3a50e4da8a0db4515ab45678e5b39ff7e7594320trawick ret = ENOMEM;
952908500d5f99f35afc5ed510391b9bdc3833farbb goto fail;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde }
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbb /* TODO: handle attrs_type */
952908500d5f99f35afc5ed510391b9bdc3833farbb ret = build_attrs_from_map(state, ctx->opts->group_map,
952908500d5f99f35afc5ed510391b9bdc3833farbb SDAP_OPTS_GROUP, &state->attrs);
952908500d5f99f35afc5ed510391b9bdc3833farbb if (ret != EOK) goto fail;
3887202241db08986e94b252fbd06a55e55d4b2dbhyde
952908500d5f99f35afc5ed510391b9bdc3833farbb if (!sdap_connected(ctx)) {
b28574227fcf956444ac03bde294122a05ad2bderbb
952908500d5f99f35afc5ed510391b9bdc3833farbb /* FIXME: add option to decide if tls should be used
3887202241db08986e94b252fbd06a55e55d4b2dbhyde * or SASL/GSSAPI, etc ... */
3887202241db08986e94b252fbd06a55e55d4b2dbhyde subreq = sdap_cli_connect_send(state, ev, ctx->opts,
3887202241db08986e94b252fbd06a55e55d4b2dbhyde ctx->be, ctx->service,
952908500d5f99f35afc5ed510391b9bdc3833farbb &ctx->rootDSE);
2d2eda71267231c2526be701fe655db125852c1ffielding if (!subreq) {
952908500d5f99f35afc5ed510391b9bdc3833farbb ret = ENOMEM;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm goto fail;
952908500d5f99f35afc5ed510391b9bdc3833farbb }
2d2eda71267231c2526be701fe655db125852c1ffielding
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_set_callback(subreq, enum_groups_connect_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return req;
952908500d5f99f35afc5ed510391b9bdc3833farbb }
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbb subreq = sdap_get_groups_send(state, state->ev,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->be->domain,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->be->sysdb,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->opts, state->ctx->gsh,
2d2eda71267231c2526be701fe655db125852c1ffielding state->attrs, state->filter);
952908500d5f99f35afc5ed510391b9bdc3833farbb if (!subreq) {
952908500d5f99f35afc5ed510391b9bdc3833farbb ret = ENOMEM;
952908500d5f99f35afc5ed510391b9bdc3833farbb goto fail;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_set_callback(subreq, enum_groups_op_done, req);
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return req;
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbbfail:
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_error(req, ret);
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_post(req, ev);
952908500d5f99f35afc5ed510391b9bdc3833farbb return req;
952908500d5f99f35afc5ed510391b9bdc3833farbb}
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbbstatic void enum_groups_connect_done(struct tevent_req *subreq)
952908500d5f99f35afc5ed510391b9bdc3833farbb{
952908500d5f99f35afc5ed510391b9bdc3833farbb struct tevent_req *req = tevent_req_callback_data(subreq,
952908500d5f99f35afc5ed510391b9bdc3833farbb struct tevent_req);
952908500d5f99f35afc5ed510391b9bdc3833farbb struct enum_groups_state *state = tevent_req_data(req,
952908500d5f99f35afc5ed510391b9bdc3833farbb struct enum_groups_state);
952908500d5f99f35afc5ed510391b9bdc3833farbb int ret;
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbb ret = sdap_cli_connect_recv(subreq, state->ctx,
952908500d5f99f35afc5ed510391b9bdc3833farbb &state->ctx->gsh, &state->ctx->rootDSE);
952908500d5f99f35afc5ed510391b9bdc3833farbb talloc_zfree(subreq);
952908500d5f99f35afc5ed510391b9bdc3833farbb if (ret) {
952908500d5f99f35afc5ed510391b9bdc3833farbb if (ret == ENOTSUP) {
952908500d5f99f35afc5ed510391b9bdc3833farbb DEBUG(0, ("Authentication mechanism not Supported by server"));
952908500d5f99f35afc5ed510391b9bdc3833farbb }
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_error(req, ret);
2d2eda71267231c2526be701fe655db125852c1ffielding return;
2d2eda71267231c2526be701fe655db125852c1ffielding }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding subreq = sdap_get_groups_send(state, state->ev,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->be->domain,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->be->sysdb,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->opts, state->ctx->gsh,
952908500d5f99f35afc5ed510391b9bdc3833farbb state->attrs, state->filter);
952908500d5f99f35afc5ed510391b9bdc3833farbb if (!subreq) {
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_error(req, ENOMEM);
952908500d5f99f35afc5ed510391b9bdc3833farbb return;
952908500d5f99f35afc5ed510391b9bdc3833farbb }
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_set_callback(subreq, enum_groups_op_done, req);
952908500d5f99f35afc5ed510391b9bdc3833farbb}
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbbstatic void enum_groups_op_done(struct tevent_req *subreq)
952908500d5f99f35afc5ed510391b9bdc3833farbb{
952908500d5f99f35afc5ed510391b9bdc3833farbb struct tevent_req *req = tevent_req_callback_data(subreq,
952908500d5f99f35afc5ed510391b9bdc3833farbb struct tevent_req);
952908500d5f99f35afc5ed510391b9bdc3833farbb struct enum_groups_state *state = tevent_req_data(req,
952908500d5f99f35afc5ed510391b9bdc3833farbb struct enum_groups_state);
952908500d5f99f35afc5ed510391b9bdc3833farbb char *timestamp;
952908500d5f99f35afc5ed510391b9bdc3833farbb int ret;
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbb ret = sdap_get_groups_recv(subreq, state, &timestamp);
952908500d5f99f35afc5ed510391b9bdc3833farbb talloc_zfree(subreq);
952908500d5f99f35afc5ed510391b9bdc3833farbb if (ret) {
952908500d5f99f35afc5ed510391b9bdc3833farbb tevent_req_error(req, ret);
952908500d5f99f35afc5ed510391b9bdc3833farbb return;
2d2eda71267231c2526be701fe655db125852c1ffielding }
5d54ba1fdf6f8d7167fafcd93bef30df3906b1aecoar
91f0d8da77152d24e4bbb31ce199282b3fd6e3b2coar if (timestamp) {
2d2eda71267231c2526be701fe655db125852c1ffielding talloc_zfree(state->ctx->max_group_timestamp);
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->max_group_timestamp = talloc_steal(state->ctx, timestamp);
952908500d5f99f35afc5ed510391b9bdc3833farbb }
952908500d5f99f35afc5ed510391b9bdc3833farbb
952908500d5f99f35afc5ed510391b9bdc3833farbb DEBUG(4, ("Groups higher timestamp: [%s]\n",
952908500d5f99f35afc5ed510391b9bdc3833farbb state->ctx->max_group_timestamp));
952908500d5f99f35afc5ed510391b9bdc3833farbb
2d2eda71267231c2526be701fe655db125852c1ffielding tevent_req_done(req);
2d2eda71267231c2526be701fe655db125852c1ffielding}
2d2eda71267231c2526be701fe655db125852c1ffielding
952908500d5f99f35afc5ed510391b9bdc3833farbb