Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Copyright Matthieu Patou <mat@matws.net> 2010-2011
5 : Copyright Stefan Metzmacher 2011
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "librpc/gen_ndr/dfsblobs.h"
23 : #include "librpc/gen_ndr/ndr_dfsblobs.h"
24 : #include "dsdb/samdb/samdb.h"
25 : #include "auth/session.h"
26 : #include "param/param.h"
27 : #include "lib/tsocket/tsocket.h"
28 : #include "dfs_server/dfs_server_ad.h"
29 : #include "lib/util/util_net.h"
30 : #include "libds/common/roles.h"
31 :
32 : #define MAX_DFS_RESPONSE 56*1024 /* 56 Kb */
33 :
34 : #undef strcasecmp
35 :
36 : /* A DC set is a group of DC, they might have been grouped together
37 : because they belong to the same site, or to site with same cost ...
38 : */
39 : struct dc_set {
40 : const char **names;
41 : uint32_t count;
42 : };
43 :
44 8 : static void shuffle_dc_set(struct dc_set *list)
45 : {
46 : uint32_t i;
47 :
48 8 : for (i = list->count; i > 1; i--) {
49 : uint32_t r;
50 : const char *tmp;
51 :
52 0 : r = generate_random() % i;
53 :
54 0 : tmp = list->names[i - 1];
55 0 : list->names[i - 1] = list->names[r];
56 0 : list->names[r] = tmp;
57 : }
58 8 : }
59 :
60 : /*
61 : fill a referral type structure
62 : */
63 2 : static NTSTATUS fill_normal_dfs_referraltype(TALLOC_CTX *mem_ctx,
64 : struct dfs_referral_type *ref,
65 : uint16_t version,
66 : const char *dfs_path,
67 : const char *server_path, int isfirstoffset)
68 : {
69 2 : ZERO_STRUCTP(ref);
70 2 : switch (version) {
71 1 : case 4:
72 1 : ref->version = version;
73 : /* For the moment there is a bug with XP that don't seems to appriciate much
74 : * level4 so we return just level 3 for everyone
75 : */
76 1 : ref->referral.v4.server_type = DFS_SERVER_NON_ROOT;
77 : /* "normal" referral seems to always include the GUID */
78 1 : ref->referral.v4.size = 34;
79 :
80 1 : if (isfirstoffset) {
81 1 : ref->referral.v4.entry_flags = DFS_HEADER_FLAG_TARGET_BCK;
82 : }
83 1 : ref->referral.v4.ttl = 900; /* As w2k8r2 */
84 1 : ref->referral.v4.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
85 1 : if (ref->referral.v4.referrals.r1.DFS_path == NULL) {
86 0 : return NT_STATUS_NO_MEMORY;
87 : }
88 1 : ref->referral.v4.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
89 1 : if (ref->referral.v4.referrals.r1.DFS_alt_path == NULL) {
90 0 : return NT_STATUS_NO_MEMORY;
91 : }
92 1 : ref->referral.v4.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
93 1 : if (ref->referral.v4.referrals.r1.netw_address == NULL) {
94 0 : return NT_STATUS_NO_MEMORY;
95 : }
96 1 : return NT_STATUS_OK;
97 1 : case 3:
98 1 : ref->version = version;
99 1 : ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
100 : /* "normal" referral seems to always include the GUID */
101 1 : ref->referral.v3.size = 34;
102 :
103 1 : ref->referral.v3.entry_flags = 0;
104 1 : ref->referral.v3.ttl = 600; /* As w2k3 */
105 1 : ref->referral.v3.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
106 1 : if (ref->referral.v3.referrals.r1.DFS_path == NULL) {
107 0 : return NT_STATUS_NO_MEMORY;
108 : }
109 1 : ref->referral.v3.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
110 1 : if (ref->referral.v3.referrals.r1.DFS_alt_path == NULL) {
111 0 : return NT_STATUS_NO_MEMORY;
112 : }
113 1 : ref->referral.v3.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
114 1 : if (ref->referral.v3.referrals.r1.netw_address == NULL) {
115 0 : return NT_STATUS_NO_MEMORY;
116 : }
117 1 : return NT_STATUS_OK;
118 : }
119 0 : return NT_STATUS_INVALID_LEVEL;
120 : }
121 :
122 : /*
123 : fill a domain refererral
124 : */
125 18 : static NTSTATUS fill_domain_dfs_referraltype(TALLOC_CTX *mem_ctx,
126 : struct dfs_referral_type *ref,
127 : uint16_t version,
128 : const char *domain,
129 : const char **names,
130 : uint16_t numnames)
131 : {
132 18 : switch (version) {
133 18 : case 3:
134 18 : ZERO_STRUCTP(ref);
135 18 : DEBUG(8, ("Called fill_domain_dfs_referraltype\n"));
136 18 : ref->version = version;
137 18 : ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
138 : #if 0
139 : /* We use to have variable size, on Windows 2008R2 it's the same
140 : * and it seems that it gives better results so ... let's use the same
141 : * size.
142 : *
143 : * Additional note: XP SP2 will ask for version 3 and SP3 for version 4.
144 : */
145 : /*
146 : * It's hard coded ... don't think it's a good way but the
147 : * sizeof return not the correct values
148 : *
149 : * We have 18 if the GUID is not included 34 otherwise
150 : */
151 : if (numnames == 0) {
152 : /* Windows return without the guid when returning domain list
153 : */
154 : ref->referral.v3.size = 18;
155 : } else {
156 : ref->referral.v3.size = 34;
157 : }
158 : #endif
159 : /* As seen in w2k8r2 it always return the null GUID */
160 18 : ref->referral.v3.size = 34;
161 18 : ref->referral.v3.entry_flags = DFS_FLAG_REFERRAL_DOMAIN_RESP;
162 18 : ref->referral.v3.ttl = 600; /* As w2k3 and w2k8r2*/
163 18 : ref->referral.v3.referrals.r2.special_name = talloc_strdup(mem_ctx,
164 : domain);
165 18 : if (ref->referral.v3.referrals.r2.special_name == NULL) {
166 0 : return NT_STATUS_NO_MEMORY;
167 : }
168 18 : ref->referral.v3.referrals.r2.nb_expanded_names = numnames;
169 : /* Put the final terminator */
170 18 : if (names) {
171 : int i;
172 6 : const char **names2 = talloc_array(mem_ctx, const char *,
173 : numnames+1);
174 6 : NT_STATUS_HAVE_NO_MEMORY(names2);
175 12 : for (i = 0; i<numnames; i++) {
176 6 : names2[i] = talloc_asprintf(names2, "\\%s", names[i]);
177 6 : NT_STATUS_HAVE_NO_MEMORY(names2[i]);
178 : }
179 6 : names2[numnames] = NULL;
180 6 : ref->referral.v3.referrals.r2.expanded_names = names2;
181 : }
182 18 : return NT_STATUS_OK;
183 : }
184 0 : return NT_STATUS_INVALID_LEVEL;
185 : }
186 :
187 : /*
188 : get the DCs list within a site
189 : */
190 8 : static NTSTATUS get_dcs_insite(TALLOC_CTX *ctx, struct ldb_context *ldb,
191 : struct ldb_dn *sitedn, struct dc_set *list,
192 : bool dofqdn)
193 : {
194 : static const char *attrs[] = { "serverReference", NULL };
195 : static const char *attrs2[] = { "dNSHostName", "sAMAccountName", NULL };
196 : struct ldb_result *r;
197 : unsigned int i;
198 : int ret;
199 : const char **dc_list;
200 :
201 8 : ret = ldb_search(ldb, ctx, &r, sitedn, LDB_SCOPE_SUBTREE, attrs,
202 : "(&(objectClass=server)(serverReference=*))");
203 8 : if (ret != LDB_SUCCESS) {
204 0 : DEBUG(2,(__location__ ": Failed to get list of servers - %s\n",
205 : ldb_errstring(ldb)));
206 0 : return NT_STATUS_INTERNAL_ERROR;
207 : }
208 :
209 8 : if (r->count == 0) {
210 : /* none in this site */
211 0 : talloc_free(r);
212 0 : return NT_STATUS_OK;
213 : }
214 :
215 : /*
216 : * need to search for all server object to know the size of the array.
217 : * Search all the object of class server in this site
218 : */
219 8 : dc_list = talloc_array(r, const char *, r->count);
220 8 : if (dc_list == NULL) {
221 0 : TALLOC_FREE(r);
222 0 : return NT_STATUS_NO_MEMORY;
223 : }
224 :
225 : /* TODO put some random here in the order */
226 8 : list->names = talloc_realloc(list, list->names, const char *, list->count + r->count);
227 8 : if (list->names == NULL) {
228 0 : TALLOC_FREE(r);
229 0 : return NT_STATUS_NO_MEMORY;
230 : }
231 :
232 32 : for (i = 0; i<r->count; i++) {
233 : struct ldb_dn *dn;
234 : struct ldb_message *msg;
235 :
236 8 : dn = ldb_msg_find_attr_as_dn(ldb, ctx, r->msgs[i], "serverReference");
237 8 : if (!dn) {
238 0 : return NT_STATUS_INTERNAL_ERROR;
239 : }
240 :
241 8 : ret = dsdb_search_one(ldb, r, &msg, dn, LDB_SCOPE_BASE, attrs2, 0, "(objectClass=computer)");
242 8 : if (ret != LDB_SUCCESS) {
243 0 : DEBUG(2,(__location__ ": Search for computer on %s failed - %s\n",
244 : ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
245 0 : return NT_STATUS_INTERNAL_ERROR;
246 : }
247 :
248 8 : if (dofqdn) {
249 5 : const char *dns = ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL);
250 5 : if (dns == NULL) {
251 0 : DEBUG(2,(__location__ ": dNSHostName missing on %s\n",
252 : ldb_dn_get_linearized(dn)));
253 0 : talloc_free(r);
254 0 : return NT_STATUS_INTERNAL_ERROR;
255 : }
256 :
257 5 : list->names[list->count] = talloc_strdup(list->names, dns);
258 5 : if (list->names[list->count] == NULL) {
259 0 : TALLOC_FREE(r);
260 0 : return NT_STATUS_NO_MEMORY;
261 : }
262 : } else {
263 : char *tmp;
264 3 : const char *aname = ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL);
265 3 : if (aname == NULL) {
266 0 : DEBUG(2,(__location__ ": sAMAccountName missing on %s\n",
267 : ldb_dn_get_linearized(dn)));
268 0 : talloc_free(r);
269 0 : return NT_STATUS_INTERNAL_ERROR;
270 : }
271 :
272 3 : tmp = talloc_strdup(list->names, aname);
273 3 : if (tmp == NULL) {
274 0 : TALLOC_FREE(r);
275 0 : return NT_STATUS_NO_MEMORY;
276 : }
277 :
278 : /* Netbios name is also the sAMAccountName for
279 : computer but without the final $ */
280 3 : tmp[strlen(tmp) - 1] = '\0';
281 3 : list->names[list->count] = tmp;
282 : }
283 8 : list->count++;
284 8 : talloc_free(msg);
285 : }
286 :
287 8 : shuffle_dc_set(list);
288 :
289 8 : talloc_free(r);
290 8 : return NT_STATUS_OK;
291 : }
292 :
293 :
294 : /*
295 : get all DCs
296 : */
297 8 : static NTSTATUS get_dcs(TALLOC_CTX *ctx, struct ldb_context *ldb,
298 : const char *searched_site, bool need_fqdn,
299 : struct dc_set ***pset_list, uint32_t flags)
300 : {
301 : /*
302 : * Flags will be used later to indicate things like least-expensive
303 : * or same-site options
304 : */
305 8 : const char *attrs_none[] = { NULL };
306 8 : const char *attrs3[] = { "name", NULL };
307 : struct ldb_dn *configdn, *sitedn, *dn, *sitescontainerdn;
308 : struct ldb_result *r;
309 8 : struct dc_set **set_list = NULL;
310 : uint32_t i;
311 : int ret;
312 8 : uint32_t current_pos = 0;
313 : NTSTATUS status;
314 : TALLOC_CTX *subctx;
315 :
316 8 : *pset_list = set_list = NULL;
317 :
318 8 : subctx = talloc_new(ctx);
319 8 : NT_STATUS_HAVE_NO_MEMORY(subctx);
320 :
321 8 : configdn = ldb_get_config_basedn(ldb);
322 :
323 : /* Let's search for the Site container */
324 8 : ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs_none,
325 : "(objectClass=sitesContainer)");
326 8 : if (ret != LDB_SUCCESS) {
327 0 : DEBUG(2,(__location__ ": Failed to find sitesContainer within %s - %s\n",
328 : ldb_dn_get_linearized(configdn), ldb_errstring(ldb)));
329 0 : talloc_free(subctx);
330 0 : return NT_STATUS_INTERNAL_ERROR;
331 : }
332 8 : if (r->count > 1) {
333 0 : DEBUG(2,(__location__ ": Expected 1 sitesContainer - found %u within %s\n",
334 : r->count, ldb_dn_get_linearized(configdn)));
335 0 : talloc_free(subctx);
336 0 : return NT_STATUS_INTERNAL_ERROR;
337 : }
338 :
339 8 : sitescontainerdn = talloc_steal(subctx, r->msgs[0]->dn);
340 8 : talloc_free(r);
341 :
342 : /*
343 : * TODO: Here we should have a more subtle handling
344 : * for the case "same-site"
345 : */
346 8 : ret = ldb_search(ldb, subctx, &r, sitescontainerdn, LDB_SCOPE_SUBTREE,
347 : attrs_none, "(objectClass=server)");
348 8 : if (ret != LDB_SUCCESS) {
349 0 : DEBUG(2,(__location__ ": Failed to find servers within %s - %s\n",
350 : ldb_dn_get_linearized(sitescontainerdn), ldb_errstring(ldb)));
351 0 : talloc_free(subctx);
352 0 : return NT_STATUS_INTERNAL_ERROR;
353 : }
354 8 : talloc_free(r);
355 :
356 8 : if (searched_site != NULL && searched_site[0] != '\0') {
357 8 : ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE,
358 : attrs_none, "(&(name=%s)(objectClass=site))", searched_site);
359 8 : if (ret != LDB_SUCCESS) {
360 0 : talloc_free(subctx);
361 0 : return NT_STATUS_FOOBAR;
362 8 : } else if (r->count != 1) {
363 0 : talloc_free(subctx);
364 0 : return NT_STATUS_FOOBAR;
365 : }
366 :
367 : /* All of this was to get the DN of the searched_site */
368 8 : sitedn = r->msgs[0]->dn;
369 :
370 : /*
371 : * We will realloc + 2 because we will need one additional place
372 : * for element at current_pos + 1 for the NULL element
373 : */
374 8 : set_list = talloc_realloc(subctx, set_list, struct dc_set *, current_pos+2);
375 8 : if (set_list == NULL) {
376 0 : TALLOC_FREE(subctx);
377 0 : return NT_STATUS_NO_MEMORY;
378 : }
379 :
380 8 : set_list[current_pos] = talloc(set_list, struct dc_set);
381 8 : if (set_list[current_pos] == NULL) {
382 0 : TALLOC_FREE(subctx);
383 0 : return NT_STATUS_NO_MEMORY;
384 : }
385 :
386 8 : set_list[current_pos]->names = NULL;
387 8 : set_list[current_pos]->count = 0;
388 :
389 8 : set_list[current_pos+1] = NULL;
390 :
391 16 : status = get_dcs_insite(subctx, ldb, sitedn,
392 8 : set_list[current_pos], need_fqdn);
393 8 : if (!NT_STATUS_IS_OK(status)) {
394 0 : DEBUG(2,(__location__ ": Failed to get DC from site %s - %s\n",
395 : ldb_dn_get_linearized(sitedn), nt_errstr(status)));
396 0 : talloc_free(subctx);
397 0 : return status;
398 : }
399 8 : talloc_free(r);
400 8 : current_pos++;
401 : }
402 :
403 : /* Let's find all the sites */
404 8 : ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs3, "(objectClass=site)");
405 8 : if (ret != LDB_SUCCESS) {
406 0 : DEBUG(2,(__location__ ": Failed to find any site containers in %s\n",
407 : ldb_dn_get_linearized(configdn)));
408 0 : talloc_free(subctx);
409 0 : return NT_STATUS_INTERNAL_DB_CORRUPTION;
410 : }
411 :
412 : /*
413 : * TODO:
414 : * We should randomize the order in the main site,
415 : * it's mostly needed for sysvol/netlogon referral.
416 : * Depending of flag we either randomize order of the
417 : * not "in the same site DCs"
418 : * or we randomize by group of site that have the same cost
419 : * In the long run we want to manipulate an array of site_set
420 : * All the site in one set have the same cost (if least-expansive options is selected)
421 : * and we will put all the dc related to 1 site set into 1 DCs set.
422 : * Within a site set, site order has to be randomized
423 : *
424 : * But for the moment we just return the list of sites
425 : */
426 8 : if (r->count) {
427 : /*
428 : * We will realloc + 2 because we will need one additional place
429 : * for element at current_pos + 1 for the NULL element
430 : */
431 8 : set_list = talloc_realloc(subctx, set_list, struct dc_set *,
432 : current_pos+2);
433 8 : if (set_list == NULL) {
434 0 : TALLOC_FREE(subctx);
435 0 : return NT_STATUS_NO_MEMORY;
436 : }
437 :
438 8 : set_list[current_pos] = talloc(ctx, struct dc_set);
439 8 : if (set_list[current_pos] == NULL) {
440 0 : TALLOC_FREE(subctx);
441 0 : return NT_STATUS_NO_MEMORY;
442 : }
443 :
444 8 : set_list[current_pos]->names = NULL;
445 8 : set_list[current_pos]->count = 0;
446 :
447 8 : set_list[current_pos+1] = NULL;
448 : }
449 :
450 16 : for (i=0; i<r->count; i++) {
451 8 : const char *site_name = ldb_msg_find_attr_as_string(r->msgs[i], "name", NULL);
452 8 : if (site_name == NULL) {
453 0 : DEBUG(2,(__location__ ": Failed to find name attribute in %s\n",
454 : ldb_dn_get_linearized(r->msgs[i]->dn)));
455 0 : talloc_free(subctx);
456 0 : return NT_STATUS_INTERNAL_DB_CORRUPTION;
457 : }
458 :
459 16 : if (searched_site == NULL ||
460 8 : strcmp(searched_site, site_name) != 0) {
461 0 : DEBUG(2,(__location__ ": Site: %s %s\n",
462 : searched_site, site_name));
463 :
464 : /*
465 : * Do all the site but the one of the client
466 : * (because it has already been done ...)
467 : */
468 0 : dn = r->msgs[i]->dn;
469 :
470 0 : status = get_dcs_insite(subctx, ldb, dn,
471 0 : set_list[current_pos],
472 : need_fqdn);
473 0 : if (!NT_STATUS_IS_OK(status)) {
474 0 : talloc_free(subctx);
475 0 : return status;
476 : }
477 : }
478 : }
479 :
480 8 : *pset_list = talloc_move(ctx, &set_list);
481 8 : talloc_free(subctx);
482 8 : return NT_STATUS_OK;
483 : }
484 :
485 7 : static NTSTATUS dodomain_referral(struct loadparm_context *lp_ctx,
486 : struct ldb_context *sam_ctx,
487 : const struct tsocket_address *client,
488 : struct dfs_GetDFSReferral *r)
489 : {
490 : /*
491 : * TODO for the moment we just return the local domain
492 : */
493 : NTSTATUS status;
494 7 : const char *dns_domain = lpcfg_dnsdomain(lp_ctx);
495 7 : const char *netbios_domain = lpcfg_workgroup(lp_ctx);
496 : struct dfs_referral_type *referrals;
497 : const char *referral_str;
498 : /* In the future this needs to be fetched from the ldb */
499 7 : uint32_t found_domain = 2;
500 :
501 7 : if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
502 0 : DEBUG(10 ,("Received a domain referral request on a non DC\n"));
503 0 : return NT_STATUS_INVALID_PARAMETER;
504 : }
505 :
506 7 : if (r->in.req.max_referral_level < 3) {
507 1 : DEBUG(2,("invalid max_referral_level %u\n",
508 : r->in.req.max_referral_level));
509 1 : return NT_STATUS_UNSUCCESSFUL;
510 : }
511 :
512 6 : r->out.resp = talloc_zero(r, struct dfs_referral_resp);
513 6 : if (r->out.resp == NULL) {
514 0 : return NT_STATUS_NO_MEMORY;
515 : }
516 :
517 6 : r->out.resp->path_consumed = 0;
518 6 : r->out.resp->header_flags = 0; /* Do like w2k3 */
519 6 : r->out.resp->nb_referrals = found_domain; /* the fqdn one + the NT domain */
520 :
521 6 : referrals = talloc_zero_array(r->out.resp,
522 : struct dfs_referral_type,
523 : r->out.resp->nb_referrals);
524 6 : if (referrals == NULL) {
525 0 : return NT_STATUS_NO_MEMORY;
526 : }
527 6 : r->out.resp->referral_entries = referrals;
528 :
529 6 : referral_str = talloc_asprintf(r, "\\%s", netbios_domain);
530 6 : if (referral_str == NULL) {
531 0 : return NT_STATUS_NO_MEMORY;
532 : }
533 :
534 6 : status = fill_domain_dfs_referraltype(referrals,
535 : &referrals[0], 3,
536 : referral_str,
537 : NULL, 0);
538 6 : if (!NT_STATUS_IS_OK(status)) {
539 0 : DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
540 : __location__, nt_errstr(status)));
541 0 : return status;
542 : }
543 :
544 6 : referral_str = talloc_asprintf(r, "\\%s", dns_domain);
545 6 : if (referral_str == NULL) {
546 0 : return NT_STATUS_NO_MEMORY;
547 : }
548 :
549 6 : status = fill_domain_dfs_referraltype(referrals,
550 : &referrals[1], 3,
551 : referral_str,
552 : NULL, 0);
553 6 : if (!NT_STATUS_IS_OK(status)) {
554 0 : DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
555 : __location__, nt_errstr(status)));
556 0 : return status;
557 : }
558 :
559 6 : return NT_STATUS_OK;
560 : }
561 :
562 : /*
563 : * Handle the logic for dfs referral request like
564 : * \\dns_domain or \\netbios_domain.
565 : */
566 6 : static NTSTATUS dodc_referral(struct loadparm_context *lp_ctx,
567 : struct ldb_context *sam_ctx,
568 : const struct tsocket_address *client,
569 : struct dfs_GetDFSReferral *r,
570 : const char *domain_name)
571 : {
572 : NTSTATUS status;
573 6 : const char *site_name = NULL; /* Name of the site where the client is */
574 6 : bool need_fqdn = false;
575 : unsigned int i;
576 6 : const char **dc_list = NULL;
577 6 : uint32_t num_dcs = 0;
578 : struct dc_set **set;
579 6 : char *client_str = NULL;
580 : struct dfs_referral_type *referrals;
581 : const char *referral_str;
582 :
583 6 : if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
584 0 : return NT_STATUS_INVALID_PARAMETER;
585 : }
586 :
587 6 : if (r->in.req.max_referral_level < 3) {
588 0 : DEBUG(2,("invalid max_referral_level %u\n",
589 : r->in.req.max_referral_level));
590 0 : return NT_STATUS_UNSUCCESSFUL;
591 : }
592 :
593 6 : DEBUG(10, ("in this we have request for %s requested is %s\n",
594 : domain_name, r->in.req.servername));
595 :
596 6 : if (strchr(domain_name,'.')) {
597 3 : need_fqdn = 1;
598 : }
599 :
600 6 : if (tsocket_address_is_inet(client, "ip")) {
601 6 : client_str = tsocket_address_inet_addr_string(client, r);
602 6 : if (client_str == NULL) {
603 0 : return NT_STATUS_NO_MEMORY;
604 : }
605 : }
606 :
607 6 : site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL, true);
608 :
609 6 : status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
610 6 : if (!NT_STATUS_IS_OK(status)) {
611 0 : DEBUG(3,("Unable to get list of DCs - %s\n",
612 : nt_errstr(status)));
613 0 : return status;
614 : }
615 :
616 18 : for(i=0; set[i]; i++) {
617 : uint32_t j;
618 :
619 12 : dc_list = talloc_realloc(r, dc_list, const char*,
620 : num_dcs + set[i]->count + 1);
621 12 : if (dc_list == NULL) {
622 0 : return NT_STATUS_NO_MEMORY;
623 : }
624 :
625 18 : for(j=0; j<set[i]->count; j++) {
626 6 : dc_list[num_dcs + j] = talloc_move(dc_list,
627 : &set[i]->names[j]);
628 : }
629 12 : num_dcs = num_dcs + set[i]->count;
630 12 : TALLOC_FREE(set[i]);
631 12 : dc_list[num_dcs] = NULL;
632 : }
633 :
634 6 : r->out.resp = talloc_zero(r, struct dfs_referral_resp);
635 6 : if (r->out.resp == NULL) {
636 0 : return NT_STATUS_NO_MEMORY;
637 : }
638 :
639 6 : r->out.resp->path_consumed = 0;
640 6 : r->out.resp->header_flags = 0; /* Do like w2k3 */
641 6 : r->out.resp->nb_referrals = 1;
642 :
643 6 : referrals = talloc_zero_array(r->out.resp,
644 : struct dfs_referral_type,
645 : r->out.resp->nb_referrals);
646 6 : if (referrals == NULL) {
647 0 : return NT_STATUS_NO_MEMORY;
648 : }
649 6 : r->out.resp->referral_entries = referrals;
650 :
651 6 : if (r->in.req.servername[0] == '\\') {
652 4 : referral_str = talloc_asprintf(referrals, "%s",
653 : domain_name);
654 : } else {
655 2 : referral_str = talloc_asprintf(referrals, "\\%s",
656 : domain_name);
657 : }
658 6 : if (referral_str == NULL) {
659 0 : return NT_STATUS_NO_MEMORY;
660 : }
661 :
662 6 : status = fill_domain_dfs_referraltype(referrals,
663 : &referrals[0], 3,
664 : referral_str,
665 : dc_list, num_dcs);
666 6 : if (!NT_STATUS_IS_OK(status)) {
667 0 : DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
668 : __location__, nt_errstr(status)));
669 0 : return status;
670 : }
671 :
672 6 : return NT_STATUS_OK;
673 : }
674 :
675 : /*
676 : * Handle the logic for dfs referral request like
677 : * \\domain\sysvol or \\domain\netlogon
678 : */
679 2 : static NTSTATUS dosysvol_referral(struct loadparm_context *lp_ctx,
680 : struct ldb_context *sam_ctx,
681 : const struct tsocket_address *client,
682 : struct dfs_GetDFSReferral *r,
683 : const char *domain_name,
684 : const char *dfs_name)
685 : {
686 2 : const char *site_name = NULL; /* Name of the site where the client is */
687 2 : bool need_fqdn = false;
688 2 : unsigned int i, c = 0, nb_entries = 0;
689 : struct dc_set **set;
690 2 : char *client_str = NULL;
691 : NTSTATUS status;
692 : struct dfs_referral_type *referrals;
693 :
694 2 : if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
695 0 : return NT_STATUS_INVALID_PARAMETER;
696 : }
697 :
698 2 : if (r->in.req.max_referral_level < 3) {
699 0 : DEBUG(2,("invalid max_referral_level %u\n",
700 : r->in.req.max_referral_level));
701 0 : return NT_STATUS_UNSUCCESSFUL;
702 : }
703 :
704 2 : DEBUG(10, ("in this we have request for %s and share %s requested is %s\n",
705 : domain_name, dfs_name, r->in.req.servername));
706 :
707 2 : if (strchr(domain_name,'.')) {
708 2 : need_fqdn = 1;
709 : }
710 :
711 2 : if (tsocket_address_is_inet(client, "ip")) {
712 2 : client_str = tsocket_address_inet_addr_string(client, r);
713 2 : if (client_str == NULL) {
714 0 : return NT_STATUS_NO_MEMORY;
715 : }
716 : }
717 :
718 2 : site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL, true);
719 :
720 2 : status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
721 2 : if (!NT_STATUS_IS_OK(status)) {
722 0 : DEBUG(3,("Unable to get list of DCs - %s\n",
723 : nt_errstr(status)));
724 0 : return status;
725 : }
726 :
727 6 : for(i=0; set[i]; i++) {
728 4 : nb_entries = nb_entries + set[i]->count;
729 : }
730 :
731 2 : r->out.resp = talloc_zero(r, struct dfs_referral_resp);
732 2 : if (r->out.resp == NULL) {
733 0 : return NT_STATUS_NO_MEMORY;
734 : }
735 :
736 : /* The length is expected in bytes */
737 2 : r->out.resp->path_consumed = strlen_m(r->in.req.servername) * 2;
738 : /* Do like w2k3 and like in 3.3.5.3 of MS-DFSC*/
739 2 : r->out.resp->header_flags = DFS_HEADER_FLAG_STORAGE_SVR;
740 2 : r->out.resp->nb_referrals = nb_entries;
741 :
742 2 : referrals = talloc_zero_array(r->out.resp,
743 : struct dfs_referral_type,
744 : r->out.resp->nb_referrals);
745 2 : if (referrals == NULL) {
746 0 : return NT_STATUS_NO_MEMORY;
747 : }
748 2 : r->out.resp->referral_entries = referrals;
749 :
750 2 : c = 0;
751 6 : for(i=0; set[i]; i++) {
752 : uint32_t j;
753 :
754 6 : for(j=0; j< set[i]->count; j++) {
755 2 : struct dfs_referral_type *ref = &referrals[c];
756 : const char *referral_str;
757 :
758 2 : referral_str = talloc_asprintf(referrals, "\\%s\\%s",
759 2 : set[i]->names[j], dfs_name);
760 2 : if (referral_str == NULL) {
761 0 : return NT_STATUS_NO_MEMORY;
762 : }
763 :
764 2 : DEBUG(8,("Doing a dfs referral for %s with this value "
765 : "%s requested %s\n",
766 : set[i]->names[j], referral_str,
767 : r->in.req.servername));
768 :
769 4 : status = fill_normal_dfs_referraltype(referrals, ref,
770 2 : r->in.req.max_referral_level,
771 : r->in.req.servername,
772 : referral_str, c==0);
773 :
774 :
775 2 : if (!NT_STATUS_IS_OK(status)) {
776 0 : DEBUG(2,("%s: Unable to fill domain referral "
777 : "structure - %s\n",
778 : __location__, nt_errstr(status)));
779 0 : return status;
780 : }
781 :
782 2 : c++;
783 : }
784 : }
785 :
786 2 : return NT_STATUS_OK;
787 : }
788 :
789 : /*
790 : trans2 getdfsreferral implementation
791 : */
792 215 : NTSTATUS dfs_server_ad_get_referrals(struct loadparm_context *lp_ctx,
793 : struct ldb_context *sam_ctx,
794 : const struct tsocket_address *client,
795 : struct dfs_GetDFSReferral *r)
796 : {
797 215 : char *server_name = NULL;
798 215 : char *dfs_name = NULL;
799 215 : char *link_path = NULL;
800 : const char *netbios_domain;
801 : const char *dns_domain;
802 : const char *netbios_name;
803 : const char *dns_name;
804 : const char **netbios_aliases;
805 : char path_separator;
806 :
807 215 : if (!lpcfg_host_msdfs(lp_ctx)) {
808 0 : return NT_STATUS_FS_DRIVER_REQUIRED;
809 : }
810 :
811 215 : if (r->in.req.servername == NULL) {
812 0 : return NT_STATUS_INVALID_PARAMETER;
813 : }
814 :
815 215 : DEBUG(8, ("Requested DFS name: %s length: %u\n",
816 : r->in.req.servername,
817 : (unsigned int)strlen_m(r->in.req.servername)*2));
818 :
819 : /*
820 : * If the servername is "" then we are in a case of domain dfs
821 : * and the client just searches for the list of local domain
822 : * it is attached and also trusted ones.
823 : */
824 215 : if (strlen(r->in.req.servername) == 0) {
825 7 : return dodomain_referral(lp_ctx, sam_ctx, client, r);
826 : }
827 :
828 208 : server_name = talloc_strdup(r, r->in.req.servername);
829 208 : if (server_name == NULL) {
830 0 : return NT_STATUS_NO_MEMORY;
831 : }
832 :
833 208 : path_separator = (*server_name == '/') ? '/' : '\\';
834 :
835 535 : while(*server_name && *server_name == path_separator) {
836 204 : server_name++;
837 : }
838 :
839 208 : dfs_name = strchr_m(server_name, path_separator);
840 208 : if (dfs_name != NULL) {
841 201 : dfs_name[0] = '\0';
842 201 : dfs_name++;
843 :
844 201 : link_path = strchr_m(dfs_name, path_separator);
845 201 : if (link_path != NULL) {
846 1 : link_path[0] = '\0';
847 1 : link_path++;
848 : }
849 : }
850 :
851 208 : if (link_path != NULL) {
852 : /*
853 : * If it is a DFS Link we do not
854 : * handle it here.
855 : */
856 1 : return NT_STATUS_NOT_FOUND;
857 : }
858 :
859 207 : netbios_domain = lpcfg_workgroup(lp_ctx);
860 207 : dns_domain = lpcfg_dnsdomain(lp_ctx);
861 207 : netbios_name = lpcfg_netbios_name(lp_ctx);
862 207 : dns_name = talloc_asprintf(r, "%s.%s", netbios_name, dns_domain);
863 207 : if (dns_name == NULL) {
864 0 : return NT_STATUS_NO_MEMORY;
865 : }
866 :
867 319 : if ((strcasecmp_m(server_name, netbios_name) == 0) ||
868 112 : (strcasecmp_m(server_name, dns_name) == 0)) {
869 : /*
870 : * If it is not domain related do not
871 : * handle it here.
872 : */
873 144 : return NT_STATUS_NOT_FOUND;
874 : }
875 :
876 63 : if (is_ipaddress(server_name)) {
877 : /*
878 : * If it is not domain related do not
879 : * handle it here.
880 : */
881 54 : return NT_STATUS_NOT_FOUND;
882 : }
883 :
884 9 : netbios_aliases = lpcfg_netbios_aliases(lp_ctx);
885 27 : while (netbios_aliases && *netbios_aliases) {
886 9 : const char *netbios_alias = *netbios_aliases;
887 : char *dns_alias;
888 : int cmp;
889 :
890 9 : cmp = strcasecmp_m(server_name, netbios_alias);
891 9 : if (cmp == 0) {
892 : /*
893 : * If it is not domain related do not
894 : * handle it here.
895 : */
896 0 : return NT_STATUS_NOT_FOUND;
897 : }
898 :
899 9 : dns_alias = talloc_asprintf(r, "%s.%s",
900 : netbios_alias,
901 : dns_domain);
902 9 : if (dns_alias == NULL) {
903 0 : return NT_STATUS_NO_MEMORY;
904 : }
905 :
906 9 : cmp = strcasecmp_m(server_name, dns_alias);
907 9 : talloc_free(dns_alias);
908 9 : if (cmp == 0) {
909 : /*
910 : * If it is not domain related do not
911 : * handle it here.
912 : */
913 0 : return NT_STATUS_NOT_FOUND;
914 : }
915 9 : netbios_aliases++;
916 : }
917 :
918 15 : if ((strcasecmp_m(server_name, netbios_domain) != 0) &&
919 6 : (strcasecmp_m(server_name, dns_domain) != 0)) {
920 : /*
921 : * Not a domain we handle.
922 : */
923 1 : return NT_STATUS_INVALID_PARAMETER;
924 : }
925 :
926 : /*
927 : * Here we have filtered the thing the requested name don't contain our DNS name.
928 : * So if the share == NULL or if share in ("sysvol", "netlogon")
929 : * then we proceed. In the first case it will be a dc refereal in the second it will
930 : * be just a sysvol/netlogon referral.
931 : */
932 8 : if (dfs_name == NULL) {
933 6 : return dodc_referral(lp_ctx, sam_ctx,
934 : client, r, server_name);
935 : }
936 :
937 : /*
938 : * Here we have filtered the thing the requested name don't contain our DNS name.
939 : * So if the share == NULL or if share in ("sysvol", "netlogon")
940 : * then we proceed. In the first case it will be a dc refereal in the second it will
941 : * be just a sysvol/netlogon referral.
942 : */
943 2 : if (strcasecmp(dfs_name, "sysvol") == 0 ||
944 0 : strcasecmp(dfs_name, "netlogon") == 0) {
945 2 : return dosysvol_referral(lp_ctx, sam_ctx, client, r,
946 : server_name, dfs_name);
947 : }
948 :
949 : /* By default until all the case are handled */
950 0 : return NT_STATUS_NOT_FOUND;
951 : }
|