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