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