From 48d1565a58de4ca58b36aef2ae65392ac97d16e8 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 ddaf9b686..c7ed04b15 100644 --- a/src/util/sss_ldap.c +++ b/src/util/sss_ldap.c @@ -301,12 +301,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 }; @@ -314,7 +321,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; @@ -331,6 +338,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) { @@ -355,6 +363,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; @@ -385,6 +405,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, @@ -394,6 +426,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 599559604..b60d05cdb 100644 --- a/src/util/sss_ldap.h +++ b/src/util/sss_ldap.h @@ -43,7 +43,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