This needs to be #ifdef HAVE_LDAP.
[samba.git] / source3 / libads / ldap_utils.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Some Helpful wrappers on LDAP 
5
6    Copyright (C) Andrew Tridgell 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 */
30 ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope, 
31                                const char *exp,
32                                const char **attrs, void **res)
33 {
34         ADS_STATUS status;
35         int count = 3;
36         char *bp;
37
38         if (!ads->ld &&
39             time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
40                 return ADS_ERROR(LDAP_SERVER_DOWN);
41         }
42
43         bp = strdup(bind_path);
44
45         while (count--) {
46                 status = ads_do_search_all(ads, bp, scope, exp, attrs, res);
47                 if (ADS_ERR_OK(status)) {
48                         DEBUG(5,("Search for %s gave %d replies\n",
49                                  exp, ads_count_replies(ads, *res)));
50                         free(bp);
51                         return status;
52                 }
53
54                 if (*res) ads_msgfree(ads, *res);
55                 *res = NULL;
56                 DEBUG(3,("Reopening ads connection to realm '%s' after error %s\n", 
57                          ads->config.realm, ads_errstr(status)));
58                 if (ads->ld) {
59                         ldap_unbind(ads->ld); 
60                 }
61                 ads->ld = NULL;
62                 status = ads_connect(ads);
63                 if (!ADS_ERR_OK(status)) {
64                         DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
65                                  ads_errstr(status)));
66                         ads_destroy(&ads);
67                         free(bp);
68                         return status;
69                 }
70         }
71         free(bp);
72
73         DEBUG(1,("ads reopen failed after error %s\n", ads_errstr(status)));
74         return status;
75 }
76
77
78 ADS_STATUS ads_search_retry(ADS_STRUCT *ads, void **res, 
79                             const char *exp, 
80                             const char **attrs)
81 {
82         return ads_do_search_retry(ads, ads->config.bind_path, LDAP_SCOPE_SUBTREE,
83                                    exp, attrs, res);
84 }
85
86 ADS_STATUS ads_search_retry_dn(ADS_STRUCT *ads, void **res, 
87                                const char *dn, 
88                                const char **attrs)
89 {
90         return ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
91                                    "(objectclass=*)", attrs, res);
92 }
93 #endif