From 5bf2314b9f64099cd4e88b8f3498d986d97e1ac6 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 15 Aug 2011 17:46:04 +0200 Subject: Handle timeout during sss_ldap_init_send In some cases, where there would be no response from the LDAP server, there would be no R/W events on the LDAP fd, so sdap_async_sys_connect_done would never be called. This patch adds a tevent timer that cancels the connection after SDAP_NETWORK_TIMEOUT seconds. --- src/util/sss_ldap.c | 36 +++++++++++++++++++++++++++++++++++- src/util/sss_ldap.h | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src/util') diff --git a/src/util/sss_ldap.c b/src/util/sss_ldap.c index bff7c8dc..de4bebf8 100644 --- a/src/util/sss_ldap.c +++ b/src/util/sss_ldap.c @@ -285,12 +285,19 @@ static errno_t set_fd_flags_and_opts(int fd) extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url, LDAP **ld); static void sss_ldap_init_sys_connect_done(struct tevent_req *subreq); +static void sdap_async_sys_connect_timeout(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval tv, void *pvt); #endif struct sss_ldap_init_state { LDAP *ldap; int sd; const char *uri; + +#ifdef HAVE_LDAP_INIT_FD + struct tevent_timer *connect_timeout; +#endif }; @@ -298,7 +305,7 @@ struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, const char *uri, struct sockaddr_storage *addr, - int addr_len) + int addr_len, int timeout) { int ret = EOK; struct tevent_req *req; @@ -315,6 +322,7 @@ struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx, #ifdef HAVE_LDAP_INIT_FD struct tevent_req *subreq; + struct timeval tv; state->sd = socket(addr->ss_family, SOCK_STREAM, 0); if (state->sd == -1) { @@ -339,6 +347,18 @@ struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx, goto fail; } + DEBUG(6, ("Setting %d seconds timeout for connecting\n", timeout)); + tv = tevent_timeval_current_ofs(timeout, 0); + + state->connect_timeout = tevent_add_timer(ev, subreq, tv, + sdap_async_sys_connect_timeout, + subreq); + if (state->connect_timeout == NULL) { + DEBUG(1, ("tevent_add_timer failed.\n")); + ret = ENOMEM; + goto fail; + } + tevent_req_set_callback(subreq, sss_ldap_init_sys_connect_done, req); return req; @@ -369,6 +389,18 @@ fail: } #ifdef HAVE_LDAP_INIT_FD +static void sdap_async_sys_connect_timeout(struct tevent_context *ev, + struct tevent_timer *te, + struct timeval tv, void *pvt) +{ + struct tevent_req *connection_request; + + DEBUG(4, ("The LDAP connection timed out\n")); + + connection_request = talloc_get_type(pvt, struct tevent_req); + tevent_req_error(connection_request, ETIMEDOUT); +} + static void sss_ldap_init_sys_connect_done(struct tevent_req *subreq) { struct tevent_req *req = tevent_req_callback_data(subreq, @@ -378,6 +410,8 @@ static void sss_ldap_init_sys_connect_done(struct tevent_req *subreq) int ret; int lret; + talloc_zfree(state->connect_timeout); + ret = sdap_async_sys_connect_recv(subreq); talloc_zfree(subreq); if (ret != EOK) { diff --git a/src/util/sss_ldap.h b/src/util/sss_ldap.h index ab890293..4b4d9c1b 100644 --- a/src/util/sss_ldap.h +++ b/src/util/sss_ldap.h @@ -53,7 +53,7 @@ struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, const char *uri, struct sockaddr_storage *addr, - int addr_len); + int addr_len, int timeout); int sss_ldap_init_recv(struct tevent_req *req, LDAP **ldap, int *sd); #endif /* __SSS_LDAP_H__ */ -- cgit