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"
26 #include "auth/credentials/credentials.h"
30 void ads_set_reconnect_fn(ADS_STRUCT
*ads
,
31 NTSTATUS (*fn
)(struct ads_struct
*ads
,
34 struct cli_credentials
**creds
),
37 ads
->auth
.reconnect_state
->fn
= fn
;
38 ads
->auth
.reconnect_state
->private_data
= private_data
;
41 static ADS_STATUS
ads_ranged_search_internal(ADS_STRUCT
*ads
,
48 const char *range_attr
,
56 a wrapper around ldap_search_s that retries depending on the error code
57 this is supposed to catch dropped connections and auto-reconnect
59 static ADS_STATUS
ads_do_search_retry_internal(ADS_STRUCT
*ads
, const char *bind_path
, int scope
,
61 const char **attrs
, void *args
,
71 time_mono(NULL
) - ads
->ldap
.last_attempt
< ADS_RECONNECT_TIME
) {
72 return ADS_ERROR(LDAP_SERVER_DOWN
);
75 bp
= SMB_STRDUP(bind_path
);
78 return ADS_ERROR(LDAP_NO_MEMORY
);
83 /* when binding anonymously, we cannot use the paged search LDAP
84 * control - Guenther */
86 if (ads
->auth
.flags
& ADS_AUTH_ANON_BIND
) {
87 status
= ads_do_search(ads
, bp
, scope
, expr
, attrs
, res
);
89 status
= ads_do_search_all_args(ads
, bp
, scope
, expr
, attrs
, args
, res
);
91 if (ADS_ERR_OK(status
)) {
92 DEBUG(5,("Search for %s in <%s> gave %d replies\n",
93 expr
, bp
, ads_count_replies(ads
, *res
)));
99 struct cli_credentials
*creds
= NULL
;
100 char *cred_name
= NULL
;
103 if (NT_STATUS_EQUAL(ads_ntstatus(status
), NT_STATUS_IO_TIMEOUT
) &&
104 ads
->config
.ldap_page_size
>= (lp_ldap_page_size() / 4) &&
105 lp_ldap_page_size() > 4) {
106 int new_page_size
= (ads
->config
.ldap_page_size
/ 2);
107 DEBUG(1, ("Reducing LDAP page size from %d to %d due to IO_TIMEOUT\n",
108 ads
->config
.ldap_page_size
, new_page_size
));
109 ads
->config
.ldap_page_size
= new_page_size
;
113 ads_msgfree(ads
, *res
);
118 if (ads
->auth
.reconnect_state
->fn
== NULL
) {
119 DBG_NOTICE("Search for %s in <%s> failed: %s\n",
120 expr
, bp
, ads_errstr(status
));
125 ntstatus
= ads
->auth
.reconnect_state
->fn(ads
,
126 ads
->auth
.reconnect_state
->private_data
,
128 if (!NT_STATUS_IS_OK(ntstatus
)) {
129 DBG_WARNING("Failed to get creds for realm(%s): %s\n",
130 ads
->server
.realm
, nt_errstr(ntstatus
));
131 DBG_WARNING("Search for %s in <%s> failed: %s\n",
132 expr
, bp
, ads_errstr(status
));
137 cred_name
= cli_credentials_get_unparsed_name(creds
, creds
);
138 DBG_NOTICE("Reopening ads connection as %s to "
139 "realm '%s' after error %s\n",
140 cred_name
, ads
->server
.realm
, ads_errstr(status
));
142 status
= ads_connect_creds(ads
, creds
);
143 if (!ADS_ERR_OK(status
)) {
144 DBG_WARNING("Reconnect ads connection as %s to "
145 "realm '%s' failed: %s\n",
146 cred_name
, ads
->server
.realm
,
149 * We need to keep the ads pointer
150 * from being freed here as we don't own it and
151 * callers depend on it being around.
162 /* when binding anonymously, we cannot use the paged search LDAP
163 * control - Guenther */
165 if (ads
->auth
.flags
& ADS_AUTH_ANON_BIND
) {
166 status
= ads_do_search(ads
, bp
, scope
, expr
, attrs
, res
);
168 status
= ads_do_search_all_args(ads
, bp
, scope
, expr
, attrs
, args
, res
);
171 if (ADS_ERR_OK(status
)) {
172 DEBUG(5,("Search for filter: %s, base: %s gave %d replies\n",
173 expr
, bp
, ads_count_replies(ads
, *res
)));
180 if (!ADS_ERR_OK(status
)) {
181 DEBUG(1,("ads reopen failed after error %s\n",
182 ads_errstr(status
)));
187 ADS_STATUS
ads_do_search_retry(ADS_STRUCT
*ads
, const char *bind_path
,
188 int scope
, const char *expr
,
189 const char **attrs
, LDAPMessage
**res
)
191 return ads_do_search_retry_internal(ads
, bind_path
, scope
, expr
, attrs
, NULL
, res
);
194 static ADS_STATUS
ads_do_search_retry_args(ADS_STRUCT
*ads
, const char *bind_path
,
195 int scope
, const char *expr
,
196 const char **attrs
, void *args
,
199 return ads_do_search_retry_internal(ads
, bind_path
, scope
, expr
, attrs
, args
, res
);
203 ADS_STATUS
ads_search_retry(ADS_STRUCT
*ads
, LDAPMessage
**res
,
204 const char *expr
, const char **attrs
)
206 return ads_do_search_retry(ads
, ads
->config
.bind_path
, LDAP_SCOPE_SUBTREE
,
210 ADS_STATUS
ads_search_retry_dn(ADS_STRUCT
*ads
, LDAPMessage
**res
,
214 return ads_do_search_retry(ads
, dn
, LDAP_SCOPE_BASE
,
215 "(objectclass=*)", attrs
, res
);
218 ADS_STATUS
ads_search_retry_dn_sd_flags(ADS_STRUCT
*ads
, LDAPMessage
**res
,
225 args
.control
= ADS_SD_FLAGS_OID
;
227 args
.critical
= True
;
229 return ads_do_search_retry_args(ads
, dn
, LDAP_SCOPE_BASE
,
230 "(objectclass=*)", attrs
, &args
, res
);
233 ADS_STATUS
ads_search_retry_extended_dn_ranged(ADS_STRUCT
*ads
, TALLOC_CTX
*mem_ctx
,
236 enum ads_extended_dn_flags flags
,
242 args
.control
= ADS_EXTENDED_DN_OID
;
244 args
.critical
= True
;
246 /* we can only range process one attribute */
247 if (!attrs
|| !attrs
[0] || attrs
[1]) {
248 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
251 return ads_ranged_search(ads
, mem_ctx
, LDAP_SCOPE_BASE
, dn
,
252 "(objectclass=*)", &args
, attrs
[0],
253 strings
, num_strings
);
257 ADS_STATUS
ads_search_retry_sid(ADS_STRUCT
*ads
, LDAPMessage
**res
,
258 const struct dom_sid
*sid
,
261 char *dn
, *sid_string
;
264 sid_string
= sid_binstring_hex_talloc(talloc_tos(), sid
);
265 if (sid_string
== NULL
) {
266 return ADS_ERROR(LDAP_NO_MEMORY
);
269 if (!asprintf(&dn
, "<SID=%s>", sid_string
)) {
270 TALLOC_FREE(sid_string
);
271 return ADS_ERROR(LDAP_NO_MEMORY
);
274 status
= ads_do_search_retry(ads
, dn
, LDAP_SCOPE_BASE
,
275 "(objectclass=*)", attrs
, res
);
277 TALLOC_FREE(sid_string
);
281 ADS_STATUS
ads_ranged_search(ADS_STRUCT
*ads
,
287 const char *range_attr
,
295 bool more_values
= False
;
300 attrs
= talloc_array(mem_ctx
, const char *, 3);
301 ADS_ERROR_HAVE_NO_MEMORY(attrs
);
303 attrs
[0] = talloc_strdup(mem_ctx
, range_attr
);
304 attrs
[1] = talloc_strdup(mem_ctx
, "usnChanged");
307 ADS_ERROR_HAVE_NO_MEMORY(attrs
[0]);
308 ADS_ERROR_HAVE_NO_MEMORY(attrs
[1]);
311 status
= ads_ranged_search_internal(ads
, mem_ctx
,
313 attrs
, args
, range_attr
,
314 strings
, num_strings
,
315 &first_usn
, &num_retries
,
318 if (NT_STATUS_EQUAL(STATUS_MORE_ENTRIES
, ads_ntstatus(status
))) {
322 if (!ADS_ERR_OK(status
)) {
328 } while (more_values
);
331 DEBUG(10,("returning with %d strings\n", (int)*num_strings
));
336 static ADS_STATUS
ads_ranged_search_internal(ADS_STRUCT
*ads
,
343 const char *range_attr
,
350 LDAPMessage
*res
= NULL
;
353 uint32_t current_usn
;
355 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs
[0], attrs
[1]));
357 *more_values
= False
;
359 status
= ads_do_search_retry_internal(ads
, base
, scope
, filter
, attrs
, args
, &res
);
361 if (!ADS_ERR_OK(status
)) {
362 DEBUG(1,("ads_search: %s\n",
363 ads_errstr(status
)));
368 return ADS_ERROR(LDAP_NO_MEMORY
);
371 count
= ads_count_replies(ads
, res
);
373 ads_msgfree(ads
, res
);
374 return ADS_ERROR(LDAP_SUCCESS
);
377 if (*num_strings
== 0) {
378 if (!ads_pull_uint32(ads
, res
, "usnChanged", first_usn
)) {
379 DEBUG(1, ("could not pull first usnChanged!\n"));
380 ads_msgfree(ads
, res
);
381 return ADS_ERROR(LDAP_NO_MEMORY
);
385 if (!ads_pull_uint32(ads
, res
, "usnChanged", ¤t_usn
)) {
386 DEBUG(1, ("could not pull current usnChanged!\n"));
387 ads_msgfree(ads
, res
);
388 return ADS_ERROR(LDAP_NO_MEMORY
);
391 if (*first_usn
!= current_usn
) {
392 DEBUG(5, ("USN on this record changed"
393 " - restarting search\n"));
394 if (*num_retries
< 5) {
397 ads_msgfree(ads
, res
);
398 return ADS_ERROR_NT(STATUS_MORE_ENTRIES
);
400 DEBUG(5, ("USN on this record changed"
401 " - restarted search too many times, aborting!\n"));
402 ads_msgfree(ads
, res
);
403 return ADS_ERROR(LDAP_NO_MEMORY
);
407 *strings
= ads_pull_strings_range(ads
, mem_ctx
, res
,
414 ads_msgfree(ads
, res
);
416 /* paranoia checks */
417 if (*strings
== NULL
&& *more_values
) {
418 DEBUG(0,("no strings found but more values???\n"));
419 return ADS_ERROR(LDAP_NO_MEMORY
);
421 if (*num_strings
== 0 && *more_values
) {
422 DEBUG(0,("no strings found but more values???\n"));
423 return ADS_ERROR(LDAP_NO_MEMORY
);
426 return (*more_values
) ? ADS_ERROR_NT(STATUS_MORE_ENTRIES
) : ADS_ERROR(LDAP_SUCCESS
);