5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose/*
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose SSSD
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose Helper routines for file descriptor events
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose Authors:
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose Sumit Bose <sbose@redhat.com>
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose Copyright (C) 2010 Red Hat
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose This program is free software; you can redistribute it and/or modify
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose it under the terms of the GNU General Public License as published by
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose the Free Software Foundation; either version 3 of the License, or
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose (at your option) any later version.
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose This program is distributed in the hope that it will be useful,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose GNU General Public License for more details.
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose You should have received a copy of the GNU General Public License
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose*/
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#include "util/util.h"
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#include "providers/ldap/sdap_async_private.h"
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bosestruct sdap_fd_events {
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#ifdef HAVE_LDAP_CONNCB
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_conncb *conncb;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#else
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct tevent_fd *fde;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#endif
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose};
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagherint get_fd_from_ldap(LDAP *ldap, int *fd)
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher{
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher int ret;
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher ret = ldap_get_option(ldap, LDAP_OPT_DESC, fd);
fb0df854f9be51892173ba299cab88bf628d2b30Stephen Gallagher if (ret != LDAP_OPT_SUCCESS || *fd < 0) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to get fd from ldap!!\n");
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher *fd = -1;
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher return EIO;
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher }
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher return EOK;
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher}
48a038d077ed2de18a5211e010c18ab680107293Stephen Gallagher
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagherint remove_ldap_connection_callbacks(struct sdap_handle *sh)
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagher{
63fb0857378c450d9806b1a3c6bb5657f00a8ba1Jakub Hrozek /* sdap_fd_events might be NULL here if the back end was marked offline
63fb0857378c450d9806b1a3c6bb5657f00a8ba1Jakub Hrozek * before a connection was established.
01248645166911f3b3c19723f44a84c5a0599e34Stephen Gallagher */
01248645166911f3b3c19723f44a84c5a0599e34Stephen Gallagher if (sh->sdap_fd_events) {
3662413d65e9ba2a899e8ce98ec9c3a549be20c8Jan Zeleny#ifdef HAVE_LDAP_CONNCB
01248645166911f3b3c19723f44a84c5a0599e34Stephen Gallagher talloc_zfree(sh->sdap_fd_events->conncb);
3662413d65e9ba2a899e8ce98ec9c3a549be20c8Jan Zeleny#else
3662413d65e9ba2a899e8ce98ec9c3a549be20c8Jan Zeleny talloc_zfree(sh->sdap_fd_events->fde);
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagher#endif
3662413d65e9ba2a899e8ce98ec9c3a549be20c8Jan Zeleny }
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagher return EOK;
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagher}
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagher
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#ifdef HAVE_LDAP_CONNCB
7bfc287b693d3696bd5b3c60bdb7e543eb230f9bStephen Gallagher
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bosestatic int remove_connection_callback(TALLOC_CTX *mem_ctx)
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose{
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int lret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_conncb *conncb = talloc_get_type(mem_ctx, struct ldap_conncb);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data *cb_data = talloc_get_type(conncb->lc_arg,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose lret = ldap_get_option(cb_data->sh->ldap, LDAP_OPT_CONNECT_CB, conncb);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (lret != LDAP_OPT_SUCCESS) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to remove connection callback.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose } else {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "Successfully removed connection callback.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
b9303e06737e6a024239e9c9a6f05fb9ed0a977eStephen Gallagher return EOK;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose}
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bosestatic int sdap_ldap_connect_callback_add(LDAP *ld, Sockbuf *sb,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose LDAPURLDesc *srv,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct sockaddr *addr,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_conncb *ctx)
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose{
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int ret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ber_socket_t ber_fd;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct fd_event_item *fd_event_item;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data *cb_data = talloc_get_type(ctx->lc_arg,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (cb_data == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "sdap_ldap_connect_callback_add called without "
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "callback data.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return EINVAL;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = ber_sockbuf_ctrl(sb, LBER_SB_OPT_GET_FD, &ber_fd);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (ret == -1) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "ber_sockbuf_ctrl failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return EINVAL;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
b36dfa237c0d9ad3a1c9d59790d6aab3b1e2e82dJakub Hrozek
99dd40a885ed3d42af4bbbde7ee2fc98830544d0Pavel Březina if (DEBUG_IS_SET(SSSDBG_TRACE_LIBS)) {
b36dfa237c0d9ad3a1c9d59790d6aab3b1e2e82dJakub Hrozek char *uri = ldap_url_desc2str(srv);
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_LIBS, "New LDAP connection to [%s] with fd [%d].\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov uri, ber_fd);
b36dfa237c0d9ad3a1c9d59790d6aab3b1e2e82dJakub Hrozek free(uri);
b36dfa237c0d9ad3a1c9d59790d6aab3b1e2e82dJakub Hrozek }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose fd_event_item = talloc_zero(cb_data, struct fd_event_item);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (fd_event_item == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "talloc failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose fd_event_item->fde = tevent_add_fd(cb_data->ev, fd_event_item, ber_fd,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose TEVENT_FD_READ, sdap_ldap_result,
6480abbd1bba71efa8a834fada6505d1767fabfcJakub Hrozek cb_data->sh);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (fd_event_item->fde == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "tevent_add_fd failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose talloc_free(fd_event_item);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose fd_event_item->fd = ber_fd;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose DLIST_ADD(cb_data->fd_list, fd_event_item);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return LDAP_SUCCESS;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose}
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bosestatic void sdap_ldap_connect_callback_del(LDAP *ld, Sockbuf *sb,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_conncb *ctx)
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose{
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int ret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ber_socket_t ber_fd;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct fd_event_item *fd_event_item;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data *cb_data = talloc_get_type(ctx->lc_arg,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (sb == NULL || cb_data == NULL) {
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = ber_sockbuf_ctrl(sb, LBER_SB_OPT_GET_FD, &ber_fd);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (ret == -1) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "ber_sockbuf_ctrl failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "Closing LDAP connection with fd [%d].\n", ber_fd);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose DLIST_FOR_EACH(fd_event_item, cb_data->fd_list) {
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (fd_event_item->fd == ber_fd) {
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose break;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (fd_event_item == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "No event for fd [%d] found.\n", ber_fd);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose DLIST_REMOVE(cb_data->fd_list, fd_event_item);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose talloc_zfree(fd_event_item);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose}
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
6480abbd1bba71efa8a834fada6505d1767fabfcJakub Hrozek#else
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bosestatic int sdap_install_ldap_callbacks(struct sdap_handle *sh,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct tevent_context *ev)
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose{
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int fd;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int ret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (sh->sdap_fd_events) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "sdap_install_ldap_callbacks is called with already "
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "initialized sdap_fd_events.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return EINVAL;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events = talloc_zero(sh, struct sdap_fd_events);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (!sh->sdap_fd_events) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = get_fd_from_ldap(sh->ldap, &fd);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (ret) return ret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events->fde = tevent_add_fd(ev, sh->sdap_fd_events, fd,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose TEVENT_FD_READ, sdap_ldap_result,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (!sh->sdap_fd_events->fde) {
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose talloc_zfree(sh->sdap_fd_events);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_INTERNAL,
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov "Trace: sh[%p], connected[%d], ops[%p], fde[%p], ldap[%p]\n",
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh, (int)sh->connected, sh->ops, sh->sdap_fd_events->fde,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov sh->ldap);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return EOK;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose}
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
6480abbd1bba71efa8a834fada6505d1767fabfcJakub Hrozek#endif
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Boseerrno_t setup_ldap_connection_callbacks(struct sdap_handle *sh,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct tevent_context *ev)
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose{
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#ifdef HAVE_LDAP_CONNCB
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int ret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_cb_data *cb_data;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events = talloc_zero(sh, struct sdap_fd_events);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (sh->sdap_fd_events == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose goto fail;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events->conncb = talloc_zero(sh->sdap_fd_events,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose struct ldap_conncb);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (sh->sdap_fd_events->conncb == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose goto fail;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose cb_data = talloc_zero(sh->sdap_fd_events->conncb, struct ldap_cb_data);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (cb_data == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = ENOMEM;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose goto fail;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose cb_data->sh = sh;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose cb_data->ev = ev;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events->conncb->lc_add = sdap_ldap_connect_callback_add;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events->conncb->lc_del = sdap_ldap_connect_callback_del;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events->conncb->lc_arg = cb_data;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = ldap_set_option(sh->ldap, LDAP_OPT_CONNECT_CB,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->sdap_fd_events->conncb);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose if (ret != LDAP_OPT_SUCCESS) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "Failed to set connection callback\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = EFAULT;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose goto fail;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose }
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose talloc_set_destructor((TALLOC_CTX *) sh->sdap_fd_events->conncb,
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose remove_connection_callback);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return EOK;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bosefail:
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose talloc_zfree(sh->sdap_fd_events);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return ret;
6480abbd1bba71efa8a834fada6505d1767fabfcJakub Hrozek#else
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "LDAP connection callbacks are not supported.\n");
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return EOK;
6480abbd1bba71efa8a834fada6505d1767fabfcJakub Hrozek#endif
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose}
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Boseerrno_t sdap_set_connected(struct sdap_handle *sh, struct tevent_context *ev)
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose{
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose int ret = EOK;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose sh->connected = true;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#ifndef HAVE_LDAP_CONNCB
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose ret = sdap_install_ldap_callbacks(sh, ev);
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose#endif
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose return ret;
5b680ac8ef46fc1714f2ab59a07f68ac386ad89bSumit Bose}
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Boseerrno_t sdap_call_conn_cb(const char *uri,int fd, struct sdap_handle *sh)
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose{
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose#ifdef HAVE_LDAP_CONNCB
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose int ret;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose Sockbuf *sb;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose LDAPURLDesc *lud;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose sb = ber_sockbuf_alloc();
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose if (sb == NULL) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "ber_sockbuf_alloc failed.\n");
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose return ENOMEM;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose }
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose ret = ber_sockbuf_ctrl(sb, LBER_SB_OPT_SET_FD, &fd);
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose if (ret != 1) {
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "ber_sockbuf_ctrl failed.\n");
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose return EFAULT;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose }
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose ret = ldap_url_parse(uri, &lud);
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose if (ret != 0) {
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose ber_sockbuf_free(sb);
795315c21d865a3060fc50bea79a758a7de7a956Lukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "ldap_url_parse failed to validate [%s] on fd [%d].\n",
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov uri, fd);
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose return EFAULT;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose }
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose ret = sdap_ldap_connect_callback_add(NULL, sb, lud, NULL,
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose sh->sdap_fd_events->conncb);
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose ldap_free_urldesc(lud);
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose ber_sockbuf_free(sb);
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose return ret;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose#else
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov DEBUG(SSSDBG_TRACE_ALL, "LDAP connection callbacks are not supported.\n");
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose return EOK;
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose#endif
0eb8bf8c05a5535ed44c71342f4bed5b821cfa06Sumit Bose}