r15704: Prefer LDAP error codes in ads_search_retry_sid().
[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 static 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, void **res)
33 {
34         ADS_STATUS status = ADS_SUCCESS;
35         int count = 3;
36         char *bp;
37
38         *res = NULL;
39
40         if (!ads->ld &&
41             time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
42                 return ADS_ERROR(LDAP_SERVER_DOWN);
43         }
44
45         bp = SMB_STRDUP(bind_path);
46
47         if (!bp) {
48                 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
49         }
50
51         *res = NULL;
52         status = ads_do_search_all_args(ads, bp, scope, expr, attrs, args, res);
53         if (ADS_ERR_OK(status)) {
54                 DEBUG(5,("Search for %s gave %d replies\n",
55                          expr, ads_count_replies(ads, *res)));
56                 SAFE_FREE(bp);
57                 return status;
58         }
59
60         while (--count) {
61
62                 if (*res) 
63                         ads_msgfree(ads, *res);
64                 *res = NULL;
65                 
66                 DEBUG(3,("Reopening ads connection to realm '%s' after error %s\n", 
67                          ads->config.realm, ads_errstr(status)));
68                          
69                 if (ads->ld) {
70                         ldap_unbind(ads->ld); 
71                 }
72                 
73                 ads->ld = NULL;
74                 status = ads_connect(ads);
75                 
76                 if (!ADS_ERR_OK(status)) {
77                         DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
78                                  ads_errstr(status)));
79                         ads_destroy(&ads);
80                         SAFE_FREE(bp);
81                         return status;
82                 }
83
84                 *res = NULL;
85                 status = ads_do_search_all_args(ads, bp, scope, expr, attrs, args, res);
86                 if (ADS_ERR_OK(status)) {
87                         DEBUG(5,("Search for %s gave %d replies\n",
88                                  expr, ads_count_replies(ads, *res)));
89                         SAFE_FREE(bp);
90                         return status;
91                 }
92         }
93         SAFE_FREE(bp);
94
95         if (!ADS_ERR_OK(status))
96                 DEBUG(1,("ads reopen failed after error %s\n", 
97                          ads_errstr(status)));
98
99         return status;
100 }
101
102 ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope, 
103                                const char *expr,
104                                const char **attrs, void **res)
105 {
106         return ads_do_search_retry_internal(ads, bind_path, scope, expr, attrs, NULL, res);
107 }
108
109 ADS_STATUS ads_do_search_retry_args(ADS_STRUCT *ads, const char *bind_path, int scope, 
110                                     const char *expr,
111                                     const char **attrs, void *args, void **res)
112 {
113         return ads_do_search_retry_internal(ads, bind_path, scope, expr, attrs, args, res);
114 }
115
116
117 ADS_STATUS ads_search_retry(ADS_STRUCT *ads, void **res, 
118                             const char *expr, 
119                             const char **attrs)
120 {
121         return ads_do_search_retry(ads, ads->config.bind_path, LDAP_SCOPE_SUBTREE,
122                                    expr, attrs, res);
123 }
124
125 ADS_STATUS ads_search_retry_dn(ADS_STRUCT *ads, void **res, 
126                                const char *dn, 
127                                const char **attrs)
128 {
129         return ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
130                                    "(objectclass=*)", attrs, res);
131 }
132
133 ADS_STATUS ads_search_retry_extended_dn(ADS_STRUCT *ads, void **res, 
134                                         const char *dn, 
135                                         const char **attrs,
136                                         enum ads_extended_dn_flags flags)
137 {
138         ads_control args;
139
140         args.control = ADS_EXTENDED_DN_OID;
141         args.val = flags;
142         args.critical = True;
143
144         return ads_do_search_retry_args(ads, dn, LDAP_SCOPE_BASE,
145                                         "(objectclass=*)", attrs, &args, res);
146 }
147
148 ADS_STATUS ads_search_retry_sid(ADS_STRUCT *ads, void **res, 
149                                 const DOM_SID *sid,
150                                 const char **attrs)
151 {
152         char *dn, *sid_string;
153         ADS_STATUS status;
154         
155         sid_string = sid_binstring_hex(sid);
156         if (sid_string == NULL) {
157                 return ADS_ERROR(LDAP_NO_MEMORY);
158         }
159
160         if (!asprintf(&dn, "<SID=%s>", sid_string)) {
161                 SAFE_FREE(sid_string);
162                 return ADS_ERROR(LDAP_NO_MEMORY);
163         }
164
165         status = ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
166                                    "(objectclass=*)", attrs, res);
167         SAFE_FREE(dn);
168         SAFE_FREE(sid_string);
169         return status;
170 }
171
172 #endif