r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / 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    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 */
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,
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->ld &&
42             time(NULL) - ads->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                 if (ads->ld) {
79                         ldap_unbind(ads->ld); 
80                 }
81                 
82                 ads->ld = NULL;
83                 status = ads_connect(ads);
84                 
85                 if (!ADS_ERR_OK(status)) {
86                         DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
87                                  ads_errstr(status)));
88                         ads_destroy(&ads);
89                         SAFE_FREE(bp);
90                         return status;
91                 }
92
93                 *res = NULL;
94
95                 /* when binding anonymously, we cannot use the paged search LDAP
96                  * control - Guenther */
97
98                 if (ads->auth.flags & ADS_AUTH_ANON_BIND) {
99                         status = ads_do_search(ads, bp, scope, expr, attrs, res);
100                 } else {
101                         status = ads_do_search_all_args(ads, bp, scope, expr, attrs, args, res);
102                 }
103
104                 if (ADS_ERR_OK(status)) {
105                         DEBUG(5,("Search for filter: %s, base: %s gave %d replies\n",
106                                  expr, bp, ads_count_replies(ads, *res)));
107                         SAFE_FREE(bp);
108                         return status;
109                 }
110         }
111         SAFE_FREE(bp);
112
113         if (!ADS_ERR_OK(status)) {
114                 DEBUG(1,("ads reopen failed after error %s\n", 
115                          ads_errstr(status)));
116         }
117         return status;
118 }
119
120  ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path,
121                                 int scope, const char *expr,
122                                 const char **attrs, LDAPMessage **res)
123 {
124         return ads_do_search_retry_internal(ads, bind_path, scope, expr, attrs, NULL, res);
125 }
126
127  ADS_STATUS ads_do_search_retry_args(ADS_STRUCT *ads, const char *bind_path,
128                                      int scope, const char *expr,
129                                      const char **attrs, void *args,
130                                      LDAPMessage **res)
131 {
132         return ads_do_search_retry_internal(ads, bind_path, scope, expr, attrs, args, res);
133 }
134
135
136  ADS_STATUS ads_search_retry(ADS_STRUCT *ads, LDAPMessage **res, 
137                              const char *expr, const char **attrs)
138 {
139         return ads_do_search_retry(ads, ads->config.bind_path, LDAP_SCOPE_SUBTREE,
140                                    expr, attrs, res);
141 }
142
143  ADS_STATUS ads_search_retry_dn(ADS_STRUCT *ads, LDAPMessage **res, 
144                                 const char *dn, 
145                                 const char **attrs)
146 {
147         return ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
148                                    "(objectclass=*)", attrs, res);
149 }
150
151  ADS_STATUS ads_search_retry_extended_dn(ADS_STRUCT *ads, LDAPMessage **res, 
152                                          const char *dn, 
153                                          const char **attrs,
154                                          enum ads_extended_dn_flags flags)
155 {
156         ads_control args;
157
158         args.control = ADS_EXTENDED_DN_OID;
159         args.val = flags;
160         args.critical = True;
161
162         return ads_do_search_retry_args(ads, dn, LDAP_SCOPE_BASE,
163                                         "(objectclass=*)", attrs, &args, res);
164 }
165
166  ADS_STATUS ads_search_retry_extended_dn_ranged(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, 
167                                                 const char *dn, 
168                                                 const char **attrs,
169                                                 enum ads_extended_dn_flags flags,
170                                                 char ***strings,
171                                                 size_t *num_strings)
172 {
173         ads_control args;
174
175         args.control = ADS_EXTENDED_DN_OID;
176         args.val = flags;
177         args.critical = True;
178
179         /* we can only range process one attribute */
180         if (!attrs || !attrs[0] || attrs[1]) {
181                 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
182         }
183
184         return ads_ranged_search(ads, mem_ctx, LDAP_SCOPE_BASE, dn, 
185                                  "(objectclass=*)", &args, attrs[0],
186                                  strings, num_strings);
187
188 }
189
190  ADS_STATUS ads_search_retry_dn_sd_flags(ADS_STRUCT *ads, LDAPMessage **res, 
191                                          uint32 sd_flags,
192                                          const char *dn, 
193                                          const char **attrs)
194 {
195         ads_control args;
196
197         args.control = ADS_SD_FLAGS_OID;
198         args.val = sd_flags;
199         args.critical = True;
200
201         return ads_do_search_retry_args(ads, dn, LDAP_SCOPE_BASE,
202                                         "(objectclass=*)", attrs, &args, res);
203 }
204
205  ADS_STATUS ads_search_retry_sid(ADS_STRUCT *ads, LDAPMessage **res, 
206                                  const DOM_SID *sid,
207                                  const char **attrs)
208 {
209         char *dn, *sid_string;
210         ADS_STATUS status;
211         
212         sid_string = sid_binstring_hex(sid);
213         if (sid_string == NULL) {
214                 return ADS_ERROR(LDAP_NO_MEMORY);
215         }
216
217         if (!asprintf(&dn, "<SID=%s>", sid_string)) {
218                 SAFE_FREE(sid_string);
219                 return ADS_ERROR(LDAP_NO_MEMORY);
220         }
221
222         status = ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
223                                    "(objectclass=*)", attrs, res);
224         SAFE_FREE(dn);
225         SAFE_FREE(sid_string);
226         return status;
227 }
228
229 ADS_STATUS ads_ranged_search(ADS_STRUCT *ads, 
230                              TALLOC_CTX *mem_ctx,
231                              int scope,
232                              const char *base,
233                              const char *filter,
234                              void *args,
235                              const char *range_attr,
236                              char ***strings,
237                              size_t *num_strings)
238 {
239         ADS_STATUS status;
240         uint32 first_usn;
241         int num_retries = 0;
242         const char **attrs;
243         BOOL more_values = False;
244
245         *num_strings = 0;
246         *strings = NULL;
247
248         attrs = TALLOC_ARRAY(mem_ctx, const char *, 3);
249         ADS_ERROR_HAVE_NO_MEMORY(attrs);
250
251         attrs[0] = talloc_strdup(mem_ctx, range_attr);
252         attrs[1] = talloc_strdup(mem_ctx, "usnChanged");
253         attrs[2] = NULL;
254
255         ADS_ERROR_HAVE_NO_MEMORY(attrs[0]);
256         ADS_ERROR_HAVE_NO_MEMORY(attrs[1]);
257
258         do {
259                 status = ads_ranged_search_internal(ads, mem_ctx, 
260                                                     scope, base, filter, 
261                                                     attrs, args, range_attr, 
262                                                     strings, num_strings,
263                                                     &first_usn, &num_retries, 
264                                                     &more_values);
265
266                 if (NT_STATUS_EQUAL(STATUS_MORE_ENTRIES, ads_ntstatus(status))) {
267                         continue;
268                 }
269
270                 if (!ADS_ERR_OK(status)) {
271                         *num_strings = 0;
272                         strings = NULL;
273                         goto done;
274                 }
275
276         } while (more_values);
277
278  done:
279         DEBUG(10,("returning with %d strings\n", (int)*num_strings));
280
281         return status;
282 }
283
284 ADS_STATUS ads_ranged_search_internal(ADS_STRUCT *ads, 
285                                       TALLOC_CTX *mem_ctx,
286                                       int scope,
287                                       const char *base,
288                                       const char *filter,
289                                       const char **attrs,
290                                       void *args,
291                                       const char *range_attr,
292                                       char ***strings,
293                                       size_t *num_strings,
294                                       uint32 *first_usn,
295                                       int *num_retries,
296                                       BOOL *more_values)
297 {
298         LDAPMessage *res = NULL;
299         ADS_STATUS status;
300         int count;
301         uint32 current_usn;
302
303         DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs[0], attrs[1]));
304
305         *more_values = False;
306
307         status = ads_do_search_retry_internal(ads, base, scope, filter, attrs, args, &res);
308
309         if (!ADS_ERR_OK(status)) {
310                 DEBUG(1,("ads_search: %s\n",
311                          ads_errstr(status)));
312                 return status;
313         }
314         
315         if (!res) {
316                 return ADS_ERROR(LDAP_NO_MEMORY);
317         }
318
319         count = ads_count_replies(ads, res);
320         if (count == 0) {
321                 ads_msgfree(ads, res);
322                 return ADS_ERROR(LDAP_SUCCESS);
323         }
324
325         if (*num_strings == 0) {
326                 if (!ads_pull_uint32(ads, res, "usnChanged", first_usn)) {
327                         DEBUG(1, ("could not pull first usnChanged!\n"));
328                         ads_msgfree(ads, res);
329                         return ADS_ERROR(LDAP_NO_MEMORY);
330                 }
331         }
332
333         if (!ads_pull_uint32(ads, res, "usnChanged", &current_usn)) {
334                 DEBUG(1, ("could not pull current usnChanged!\n"));
335                 ads_msgfree(ads, res);
336                 return ADS_ERROR(LDAP_NO_MEMORY);
337         }
338
339         if (*first_usn != current_usn) {
340                 DEBUG(5, ("USN on this record changed"
341                           " - restarting search\n"));
342                 if (*num_retries < 5) {
343                         (*num_retries)++;
344                         *num_strings = 0;
345                         ads_msgfree(ads, res);
346                         return ADS_ERROR_NT(STATUS_MORE_ENTRIES);
347                 } else {
348                         DEBUG(5, ("USN on this record changed"
349                                   " - restarted search too many times, aborting!\n"));
350                         ads_msgfree(ads, res);
351                         return ADS_ERROR(LDAP_NO_MEMORY);
352                 }
353         }
354
355         *strings = ads_pull_strings_range(ads, mem_ctx, res,
356                                          range_attr,
357                                          *strings,
358                                          &attrs[0],
359                                          num_strings,
360                                          more_values);
361
362         ads_msgfree(ads, res);
363
364         /* paranoia checks */
365         if (*strings == NULL && *more_values) {
366                 DEBUG(0,("no strings found but more values???\n"));
367                 return ADS_ERROR(LDAP_NO_MEMORY);
368         }
369         if (*num_strings == 0 && *more_values) {
370                 DEBUG(0,("no strings found but more values???\n"));
371                 return ADS_ERROR(LDAP_NO_MEMORY);
372         }
373
374         return (*more_values) ? ADS_ERROR_NT(STATUS_MORE_ENTRIES) : ADS_ERROR(LDAP_SUCCESS);
375 }
376
377 #endif