2 Unix SMB/CIFS implementation.
4 Some Helpful wrappers on LDAP
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Guenther Deschner 2006,2007
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.
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.
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/>.
25 #include "lib/param/loadparm.h"
29 static ADS_STATUS
ads_ranged_search_internal(ADS_STRUCT
*ads
,
36 const char *range_attr
,
44 a wrapper around ldap_search_s that retries depending on the error code
45 this is supposed to catch dropped connections and auto-reconnect
47 static ADS_STATUS
ads_do_search_retry_internal(ADS_STRUCT
*ads
, const char *bind_path
, int scope
,
49 const char **attrs
, void *args
,
52 ADS_STATUS status
= ADS_SUCCESS
;
59 time_mono(NULL
) - ads
->ldap
.last_attempt
< ADS_RECONNECT_TIME
) {
60 return ADS_ERROR(LDAP_SERVER_DOWN
);
63 bp
= SMB_STRDUP(bind_path
);
66 return ADS_ERROR(LDAP_NO_MEMORY
);
71 /* when binding anonymously, we cannot use the paged search LDAP
72 * control - Guenther */
74 if (ads
->auth
.flags
& ADS_AUTH_ANON_BIND
) {
75 status
= ads_do_search(ads
, bp
, scope
, expr
, attrs
, res
);
77 status
= ads_do_search_all_args(ads
, bp
, scope
, expr
, attrs
, args
, res
);
79 if (ADS_ERR_OK(status
)) {
80 DEBUG(5,("Search for %s in <%s> gave %d replies\n",
81 expr
, bp
, ads_count_replies(ads
, *res
)));
88 if (NT_STATUS_EQUAL(ads_ntstatus(status
), NT_STATUS_IO_TIMEOUT
) &&
89 ads
->config
.ldap_page_size
>= (lp_ldap_page_size() / 4) &&
90 lp_ldap_page_size() > 4) {
91 int new_page_size
= (ads
->config
.ldap_page_size
/ 2);
92 DEBUG(1, ("Reducing LDAP page size from %d to %d due to IO_TIMEOUT\n",
93 ads
->config
.ldap_page_size
, new_page_size
));
94 ads
->config
.ldap_page_size
= new_page_size
;
98 ads_msgfree(ads
, *res
);
101 DEBUG(3,("Reopening ads connection to realm '%s' after error %s\n",
102 ads
->config
.realm
, ads_errstr(status
)));
105 status
= ads_connect(ads
);
107 if (!ADS_ERR_OK(status
)) {
108 DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
109 ads_errstr(status
)));
117 /* when binding anonymously, we cannot use the paged search LDAP
118 * control - Guenther */
120 if (ads
->auth
.flags
& ADS_AUTH_ANON_BIND
) {
121 status
= ads_do_search(ads
, bp
, scope
, expr
, attrs
, res
);
123 status
= ads_do_search_all_args(ads
, bp
, scope
, expr
, attrs
, args
, res
);
126 if (ADS_ERR_OK(status
)) {
127 DEBUG(5,("Search for filter: %s, base: %s gave %d replies\n",
128 expr
, bp
, ads_count_replies(ads
, *res
)));
135 if (!ADS_ERR_OK(status
)) {
136 DEBUG(1,("ads reopen failed after error %s\n",
137 ads_errstr(status
)));
142 ADS_STATUS
ads_do_search_retry(ADS_STRUCT
*ads
, const char *bind_path
,
143 int scope
, const char *expr
,
144 const char **attrs
, LDAPMessage
**res
)
146 return ads_do_search_retry_internal(ads
, bind_path
, scope
, expr
, attrs
, NULL
, res
);
149 static ADS_STATUS
ads_do_search_retry_args(ADS_STRUCT
*ads
, const char *bind_path
,
150 int scope
, const char *expr
,
151 const char **attrs
, void *args
,
154 return ads_do_search_retry_internal(ads
, bind_path
, scope
, expr
, attrs
, args
, res
);
158 ADS_STATUS
ads_search_retry(ADS_STRUCT
*ads
, LDAPMessage
**res
,
159 const char *expr
, const char **attrs
)
161 return ads_do_search_retry(ads
, ads
->config
.bind_path
, LDAP_SCOPE_SUBTREE
,
165 ADS_STATUS
ads_search_retry_dn(ADS_STRUCT
*ads
, LDAPMessage
**res
,
169 return ads_do_search_retry(ads
, dn
, LDAP_SCOPE_BASE
,
170 "(objectclass=*)", attrs
, res
);
173 ADS_STATUS
ads_search_retry_dn_sd_flags(ADS_STRUCT
*ads
, LDAPMessage
**res
,
180 args
.control
= ADS_SD_FLAGS_OID
;
182 args
.critical
= True
;
184 return ads_do_search_retry_args(ads
, dn
, LDAP_SCOPE_BASE
,
185 "(objectclass=*)", attrs
, &args
, res
);
188 ADS_STATUS
ads_search_retry_extended_dn_ranged(ADS_STRUCT
*ads
, TALLOC_CTX
*mem_ctx
,
191 enum ads_extended_dn_flags flags
,
197 args
.control
= ADS_EXTENDED_DN_OID
;
199 args
.critical
= True
;
201 /* we can only range process one attribute */
202 if (!attrs
|| !attrs
[0] || attrs
[1]) {
203 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
206 return ads_ranged_search(ads
, mem_ctx
, LDAP_SCOPE_BASE
, dn
,
207 "(objectclass=*)", &args
, attrs
[0],
208 strings
, num_strings
);
212 ADS_STATUS
ads_search_retry_sid(ADS_STRUCT
*ads
, LDAPMessage
**res
,
213 const struct dom_sid
*sid
,
216 char *dn
, *sid_string
;
219 sid_string
= sid_binstring_hex_talloc(talloc_tos(), sid
);
220 if (sid_string
== NULL
) {
221 return ADS_ERROR(LDAP_NO_MEMORY
);
224 if (!asprintf(&dn
, "<SID=%s>", sid_string
)) {
225 TALLOC_FREE(sid_string
);
226 return ADS_ERROR(LDAP_NO_MEMORY
);
229 status
= ads_do_search_retry(ads
, dn
, LDAP_SCOPE_BASE
,
230 "(objectclass=*)", attrs
, res
);
232 TALLOC_FREE(sid_string
);
236 ADS_STATUS
ads_ranged_search(ADS_STRUCT
*ads
,
242 const char *range_attr
,
250 bool more_values
= False
;
255 attrs
= talloc_array(mem_ctx
, const char *, 3);
256 ADS_ERROR_HAVE_NO_MEMORY(attrs
);
258 attrs
[0] = talloc_strdup(mem_ctx
, range_attr
);
259 attrs
[1] = talloc_strdup(mem_ctx
, "usnChanged");
262 ADS_ERROR_HAVE_NO_MEMORY(attrs
[0]);
263 ADS_ERROR_HAVE_NO_MEMORY(attrs
[1]);
266 status
= ads_ranged_search_internal(ads
, mem_ctx
,
268 attrs
, args
, range_attr
,
269 strings
, num_strings
,
270 &first_usn
, &num_retries
,
273 if (NT_STATUS_EQUAL(STATUS_MORE_ENTRIES
, ads_ntstatus(status
))) {
277 if (!ADS_ERR_OK(status
)) {
283 } while (more_values
);
286 DEBUG(10,("returning with %d strings\n", (int)*num_strings
));
291 static ADS_STATUS
ads_ranged_search_internal(ADS_STRUCT
*ads
,
298 const char *range_attr
,
305 LDAPMessage
*res
= NULL
;
308 uint32_t current_usn
;
310 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs
[0], attrs
[1]));
312 *more_values
= False
;
314 status
= ads_do_search_retry_internal(ads
, base
, scope
, filter
, attrs
, args
, &res
);
316 if (!ADS_ERR_OK(status
)) {
317 DEBUG(1,("ads_search: %s\n",
318 ads_errstr(status
)));
323 return ADS_ERROR(LDAP_NO_MEMORY
);
326 count
= ads_count_replies(ads
, res
);
328 ads_msgfree(ads
, res
);
329 return ADS_ERROR(LDAP_SUCCESS
);
332 if (*num_strings
== 0) {
333 if (!ads_pull_uint32(ads
, res
, "usnChanged", first_usn
)) {
334 DEBUG(1, ("could not pull first usnChanged!\n"));
335 ads_msgfree(ads
, res
);
336 return ADS_ERROR(LDAP_NO_MEMORY
);
340 if (!ads_pull_uint32(ads
, res
, "usnChanged", ¤t_usn
)) {
341 DEBUG(1, ("could not pull current usnChanged!\n"));
342 ads_msgfree(ads
, res
);
343 return ADS_ERROR(LDAP_NO_MEMORY
);
346 if (*first_usn
!= current_usn
) {
347 DEBUG(5, ("USN on this record changed"
348 " - restarting search\n"));
349 if (*num_retries
< 5) {
352 ads_msgfree(ads
, res
);
353 return ADS_ERROR_NT(STATUS_MORE_ENTRIES
);
355 DEBUG(5, ("USN on this record changed"
356 " - restarted search too many times, aborting!\n"));
357 ads_msgfree(ads
, res
);
358 return ADS_ERROR(LDAP_NO_MEMORY
);
362 *strings
= ads_pull_strings_range(ads
, mem_ctx
, res
,
369 ads_msgfree(ads
, res
);
371 /* paranoia checks */
372 if (*strings
== NULL
&& *more_values
) {
373 DEBUG(0,("no strings found but more values???\n"));
374 return ADS_ERROR(LDAP_NO_MEMORY
);
376 if (*num_strings
== 0 && *more_values
) {
377 DEBUG(0,("no strings found but more values???\n"));
378 return ADS_ERROR(LDAP_NO_MEMORY
);
381 return (*more_values
) ? ADS_ERROR_NT(STATUS_MORE_ENTRIES
) : ADS_ERROR(LDAP_SUCCESS
);