From 06e1a20b9fc565023e31623970624db26f790e69 Mon Sep 17 00:00:00 2001
From: Jan Zeleny <jzeleny@redhat.com>
Date: Wed, 12 Oct 2011 09:30:53 -0400
Subject: [PATCH 5/5] Added IPA account info handler

Currently it is only handling netgroups by itself, other requests are
forwarded to LDAP provider.
---
 Makefile.am                  |    1 +
 src/providers/ipa/ipa_id.c   |  323 ++++++++++++++++++++++++++++++++++++++++++
 src/providers/ipa/ipa_id.h   |   47 ++++++
 src/providers/ipa/ipa_init.c |    3 +-
 4 files changed, 373 insertions(+), 1 deletions(-)
 create mode 100644 src/providers/ipa/ipa_id.c
 create mode 100644 src/providers/ipa/ipa_id.h

diff --git a/Makefile.am b/Makefile.am
index 25f17d362c57e9c2580cc88ca5a7bbb10d0703bc..5e2abbf95ab77054aaf63ba7cb4e5e9e35a2d20d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -908,6 +908,7 @@ libsss_ipa_la_SOURCES = \
     src/providers/ipa/ipa_init.c \
     src/providers/ipa/ipa_common.c \
     src/providers/ipa/ipa_utils.c \
+    src/providers/ipa/ipa_id.c \
     src/providers/ipa/ipa_netgroups.c \
     src/providers/ipa/ipa_auth.c \
     src/providers/ipa/ipa_access.c \
diff --git a/src/providers/ipa/ipa_id.c b/src/providers/ipa/ipa_id.c
new file mode 100644
index 0000000000000000000000000000000000000000..14a344f2a49a6ff63a975068689929620cdc6700
--- /dev/null
+++ b/src/providers/ipa/ipa_id.c
@@ -0,0 +1,323 @@
+/*
+    SSSD
+
+    IPA Identity Backend Module
+
+    Authors:
+        Jan Zeleny <jzeleny@redhat.com>
+
+    Copyright (C) 2011 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <errno.h>
+
+#include "util/util.h"
+#include "db/sysdb.h"
+#include "providers/ldap/ldap_common.h"
+#include "providers/ldap/sdap_async.h"
+#include "providers/ipa/ipa_id.h"
+
+struct ipa_netgroup_get_state {
+    struct tevent_context *ev;
+    struct ipa_id_ctx *ctx;
+    struct sdap_id_op *op;
+    struct sysdb_ctx *sysdb;
+    struct sss_domain_info *domain;
+
+    const char *name;
+    int timeout;
+
+    char *filter;
+    const char **attrs;
+
+    size_t count;
+    struct sysdb_attrs **netgroups;
+
+    int dp_error;
+};
+
+struct tevent_req *ipa_netgroup_get_send(TALLOC_CTX *memctx,
+                                     struct tevent_context *ev,
+                                     struct ipa_id_ctx *ctx,
+                                     const char *name);
+static int ipa_netgroup_get_retry(struct tevent_req *req);
+static void ipa_netgroup_get_connect_done(struct tevent_req *subreq);
+static void ipa_netgroup_get_done(struct tevent_req *subreq);
+static void ipa_account_info_netgroups_done(struct tevent_req *req);
+
+void ipa_account_info_handler(struct be_req *breq)
+{
+    struct ipa_id_ctx *ipa_ctx;
+    struct sdap_id_ctx *ctx;
+    struct be_acct_req *ar;
+    struct tevent_req *req;
+    const char *err = "Unknown Error";
+    int ret = EOK;
+
+    ipa_ctx = talloc_get_type(breq->be_ctx->bet_info[BET_ID].pvt_bet_data, struct ipa_id_ctx);
+    ctx = ipa_ctx->sdap_id_ctx;
+
+    if (be_is_offline(ctx->be)) {
+        return sdap_handler_done(breq, DP_ERR_OFFLINE, EAGAIN, "Offline");
+    }
+
+    ar = talloc_get_type(breq->req_data, struct be_acct_req);
+
+    switch (ar->entry_type & 0xFFF) {
+    case BE_REQ_USER: /* user */
+    case BE_REQ_GROUP: /* group */
+    case BE_REQ_INITGROUPS: /* init groups for user */
+        return sdap_account_info_handler(breq);
+
+    case BE_REQ_NETGROUP:
+        if (ar->filter_type != BE_FILTER_NAME) {
+            ret = EINVAL;
+            err = "Invalid filter type";
+            break;
+        }
+
+        req = ipa_netgroup_get_send(breq, breq->be_ctx->ev, ipa_ctx, ar->filter_value);
+        if (!req) {
+            return sdap_handler_done(breq, DP_ERR_FATAL, ENOMEM, "Out of memory");
+        }
+
+        tevent_req_set_callback(req, ipa_account_info_netgroups_done, breq);
+        break;
+
+    default: /*fail*/
+        ret = EINVAL;
+        err = "Invalid request type";
+    }
+
+    if (ret != EOK) return sdap_handler_done(breq, DP_ERR_FATAL, ret, err);
+}
+
+static void ipa_account_info_complete(struct be_req *breq, int dp_error,
+                                       int ret, const char *default_error_text)
+{
+    const char* error_text;
+
+    if (dp_error == DP_ERR_OK) {
+        if (ret == EOK) {
+            error_text = NULL;
+        } else {
+            DEBUG(1, ("Bug: dp_error is OK on failed request"));
+            dp_error = DP_ERR_FATAL;
+            error_text = default_error_text;
+        }
+    } else if (dp_error == DP_ERR_OFFLINE) {
+        error_text = "Offline";
+    } else if (dp_error == DP_ERR_FATAL && ret == ENOMEM) {
+        error_text = "Out of memory";
+    } else {
+        error_text = default_error_text;
+    }
+
+    sdap_handler_done(breq, dp_error, ret, error_text);
+}
+
+/* Request for netgroups
+ * - first start here and then go to ipa_netgroups.c
+ */
+struct tevent_req *ipa_netgroup_get_send(TALLOC_CTX *memctx,
+                                     struct tevent_context *ev,
+                                     struct ipa_id_ctx *ipa_ctx,
+                                     const char *name)
+{
+    struct tevent_req *req;
+    struct ipa_netgroup_get_state *state;
+    struct sdap_id_ctx *ctx;
+    char *clean_name;
+    int ret;
+
+    ctx = ipa_ctx->sdap_id_ctx;
+
+    req = tevent_req_create(memctx, &state, struct ipa_netgroup_get_state);
+    if (!req) return NULL;
+
+    state->ev = ev;
+    state->ctx = ipa_ctx;
+    state->dp_error = DP_ERR_FATAL;
+
+    state->op = sdap_id_op_create(state, ctx->conn_cache);
+    if (!state->op) {
+        DEBUG(2, ("sdap_id_op_create failed\n"));
+        ret = ENOMEM;
+        goto fail;
+    }
+
+    state->sysdb = ctx->be->sysdb;
+    state->domain = ctx->be->domain;
+    state->name = name;
+    state->timeout = dp_opt_get_int(ctx->opts->basic, SDAP_SEARCH_TIMEOUT);
+
+    ret = sss_filter_sanitize(state, name, &clean_name);
+    if (ret != EOK) {
+        goto fail;
+    }
+
+    state->filter = talloc_asprintf(state, "(&(%s=%s)(objectclass=%s))",
+                            ctx->opts->netgroup_map[IPA_AT_NETGROUP_NAME].name,
+                            clean_name,
+                            ctx->opts->netgroup_map[IPA_OC_NETGROUP].name);
+    if (!state->filter) {
+        DEBUG(2, ("Failed to build filter\n"));
+        ret = ENOMEM;
+        goto fail;
+    }
+    talloc_zfree(clean_name);
+
+    ret = build_attrs_from_map(state, ctx->opts->netgroup_map,
+                               IPA_OPTS_NETGROUP, &state->attrs);
+    if (ret != EOK) goto fail;
+
+    ret = ipa_netgroup_get_retry(req);
+    if (ret != EOK) {
+        goto fail;
+    }
+
+    return req;
+
+fail:
+    tevent_req_error(req, ret);
+    tevent_req_post(req, ev);
+    return req;
+}
+
+static int ipa_netgroup_get_retry(struct tevent_req *req)
+{
+    struct ipa_netgroup_get_state *state = tevent_req_data(req,
+                                                    struct ipa_netgroup_get_state);
+    struct tevent_req *subreq;
+    int ret = EOK;
+
+    subreq = sdap_id_op_connect_send(state->op, state, &ret);
+    if (!subreq) {
+        return ret;
+    }
+
+    tevent_req_set_callback(subreq, ipa_netgroup_get_connect_done, req);
+    return EOK;
+}
+
+static void ipa_netgroup_get_connect_done(struct tevent_req *subreq)
+{
+    struct tevent_req *req = tevent_req_callback_data(subreq,
+                                                      struct tevent_req);
+    struct ipa_netgroup_get_state *state = tevent_req_data(req,
+                                                    struct ipa_netgroup_get_state);
+    int dp_error = DP_ERR_FATAL;
+    int ret;
+    struct sdap_id_ctx *sdap_ctx = state->ctx->sdap_id_ctx;
+
+    ret = sdap_id_op_connect_recv(subreq, &dp_error);
+    talloc_zfree(subreq);
+
+    if (ret != EOK) {
+        state->dp_error = dp_error;
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    subreq = ipa_get_netgroups_send(state, state->ev,
+                                    state->sysdb, sdap_ctx->opts,
+                                    state->ctx->ipa_options,
+                                    sdap_id_op_handle(state->op),
+                                    state->attrs, state->filter,
+                                    state->timeout);
+    if (!subreq) {
+        tevent_req_error(req, ENOMEM);
+        return;
+    }
+    tevent_req_set_callback(subreq, ipa_netgroup_get_done, req);
+
+    return;
+}
+
+static void ipa_netgroup_get_done(struct tevent_req *subreq)
+{
+    struct tevent_req *req = tevent_req_callback_data(subreq,
+                                                      struct tevent_req);
+    struct ipa_netgroup_get_state *state = tevent_req_data(req,
+                                                    struct ipa_netgroup_get_state);
+    int dp_error = DP_ERR_FATAL;
+    int ret;
+
+    ret = ipa_get_netgroups_recv(subreq, state, &state->count, &state->netgroups);
+    talloc_zfree(subreq);
+    ret = sdap_id_op_done(state->op, ret, &dp_error);
+
+    if (dp_error == DP_ERR_OK && ret != EOK) {
+        /* retry */
+        ret = ipa_netgroup_get_retry(req);
+        if (ret != EOK) {
+            tevent_req_error(req, ret);
+            return;
+        }
+
+        return;
+    }
+
+    if (ret && ret != ENOENT) {
+        state->dp_error = dp_error;
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    if (ret == EOK && state->count > 1) {
+        DEBUG(1, ("Found more than one netgroup with the name [%s].\n",
+                  state->name));
+        tevent_req_error(req, EINVAL);
+        return;
+    }
+
+    if (ret == ENOENT) {
+        ret = sysdb_delete_netgroup(state->sysdb, state->name);
+        if (ret != EOK && ret != ENOENT) {
+            tevent_req_error(req, ret);
+            return;
+        }
+    }
+
+    state->dp_error = DP_ERR_OK;
+    tevent_req_done(req);
+    return;
+}
+
+int ipa_netgroup_get_recv(struct tevent_req *req, int *dp_error_out)
+{
+    struct ipa_netgroup_get_state *state = tevent_req_data(req,
+                                                    struct ipa_netgroup_get_state);
+
+    if (dp_error_out) {
+        *dp_error_out = state->dp_error;
+    }
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    return EOK;
+}
+
+static void ipa_account_info_netgroups_done(struct tevent_req *req)
+{
+    struct be_req *breq = tevent_req_callback_data(req, struct be_req);
+    int ret, dp_error;
+
+    ret = ipa_netgroup_get_recv(req, &dp_error);
+    talloc_zfree(req);
+
+    ipa_account_info_complete(breq, dp_error, ret, "Netgroup lookup failed");
+}
diff --git a/src/providers/ipa/ipa_id.h b/src/providers/ipa/ipa_id.h
new file mode 100644
index 0000000000000000000000000000000000000000..139038f7e9360942df3552ddffc4899e8f8f8cc2
--- /dev/null
+++ b/src/providers/ipa/ipa_id.h
@@ -0,0 +1,47 @@
+/*
+    SSSD
+
+    IPA Identity Backend Module
+
+    Authors:
+        Jan Zeleny <jzeleny@redhat.com>
+
+    Copyright (C) 2011 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#ifndef _IPA_ID_H_
+#define _IPA_ID_H_
+
+#include "providers/ldap/ldap_common.h"
+#include "providers/ipa/ipa_common.h"
+
+void ipa_account_info_handler(struct be_req *breq);
+struct tevent_req *ipa_get_netgroups_send(TALLOC_CTX *memctx,
+                                          struct tevent_context *ev,
+                                          struct sysdb_ctx *sysdb,
+                                          struct sdap_options *opts,
+                                          struct ipa_options *ipa_options,
+                                          struct sdap_handle *sh,
+                                          const char **attrs,
+                                          const char *filter,
+                                          int timeout);
+
+int ipa_get_netgroups_recv(struct tevent_req *req,
+                           TALLOC_CTX *mem_ctx,
+                           size_t *reply_count,
+                           struct sysdb_attrs ***reply);
+#endif
diff --git a/src/providers/ipa/ipa_init.c b/src/providers/ipa/ipa_init.c
index 713b9a43e1311affdb8df702f8b37b257096d2c8..9ae0e730771554229c8e2d30c916c3c3e1b36a53 100644
--- a/src/providers/ipa/ipa_init.c
+++ b/src/providers/ipa/ipa_init.c
@@ -30,6 +30,7 @@
 #include "providers/child_common.h"
 #include "providers/ipa/ipa_common.h"
 #include "providers/krb5/krb5_auth.h"
+#include "providers/ipa/ipa_id.h"
 #include "providers/ipa/ipa_auth.h"
 #include "providers/ipa/ipa_access.h"
 #include "providers/ipa/ipa_dyndns.h"
@@ -38,7 +39,7 @@ struct ipa_options *ipa_options = NULL;
 
 /* Id Handler */
 struct bet_ops ipa_id_ops = {
-    .handler = sdap_account_info_handler,
+    .handler = ipa_account_info_handler,
     .finalize = NULL,
     .check_online = sdap_check_online
 };
-- 
1.7.6.4

