source: branches/samba-3.3.x/source/libads/ldap_utils.c@ 206

Last change on this file since 206 was 206, checked in by Herwig Bauernfeind, 17 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

File size: 9.2 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Some Helpful wrappers on LDAP
5
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Guenther Deschner 2006,2007
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "includes.h"
24
25#ifdef HAVE_LDAP
26/*
27 a wrapper around ldap_search_s that retries depending on the error code
28 this is supposed to catch dropped connections and auto-reconnect
29*/
30static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind_path, int scope,
31 const char *expr,
32 const char **attrs, void *args,
33 LDAPMessage **res)
34{
35 ADS_STATUS status = ADS_SUCCESS;
36 int count = 3;
37 char *bp;
38
39 *res = NULL;
40
41 if (!ads->ldap.ld &&
42 time(NULL) - ads->ldap.last_attempt < ADS_RECONNECT_TIME) {
43 return ADS_ERROR(LDAP_SERVER_DOWN);
44 }
45
46 bp = SMB_STRDUP(bind_path);
47
48 if (!bp) {
49 return ADS_ERROR(LDAP_NO_MEMORY);
50 }
51
52 *res = NULL;
53
54 /* when binding anonymously, we cannot use the paged search LDAP
55 * control - Guenther */
56
57 if (ads->auth.flags & ADS_AUTH_ANON_BIND) {
58 status = ads_do_search(ads, bp, scope, expr, attrs, res);
59 } else {
60 status = ads_do_search_all_args(ads, bp, scope, expr, attrs, args, res);
61 }
62 if (ADS_ERR_OK(status)) {
63 DEBUG(5,("Search for %s in <%s> gave %d replies\n",
64 expr, bp, ads_count_replies(ads, *res)));
65 SAFE_FREE(bp);
66 return status;
67 }
68
69 while (--count) {
70
71 if (*res)
72 ads_msgfree(ads, *res);
73 *res = NULL;
74
75 DEBUG(3,("Reopening ads connection to realm '%s' after error %s\n",
76 ads->config.realm, ads_errstr(status)));
77
78 ads_disconnect(ads);
79 status = ads_connect(ads);
80
81 if (!ADS_ERR_OK(status)) {
82 DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
83 ads_errstr(status)));
84 ads_destroy(&ads);
85 SAFE_FREE(bp);
86 return status;
87 }
88
89 *res = NULL;
90
91 /* when binding anonymously, we cannot use the paged search LDAP
92 * control - Guenther */
93
94 if (ads->auth.flags & ADS_AUTH_ANON_BIND) {
95 status = ads_do_search(ads, bp, scope, expr, attrs, res);
96 } else {
97 status = ads_do_search_all_args(ads, bp, scope, expr, attrs, args, res);
98 }
99
100 if (ADS_ERR_OK(status)) {
101 DEBUG(5,("Search for filter: %s, base: %s gave %d replies\n",
102 expr, bp, ads_count_replies(ads, *res)));
103 SAFE_FREE(bp);
104 return status;
105 }
106 }
107 SAFE_FREE(bp);
108
109 if (!ADS_ERR_OK(status)) {
110 DEBUG(1,("ads reopen failed after error %s\n",
111 ads_errstr(status)));
112 }
113 return status;
114}