s3:lib/events: make use of samba_tevent_set_debug()
[Samba/gebeck_regimport.git] / dfs_server / dfs_server_ad.c
blob4d3891a774a9042749d1a6f0b1be685b5fa39225
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright Matthieu Patou <mat@matws.net> 2010-2011
5 Copyright Stefan Metzmacher 2011
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.
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.
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/>.
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"
31 #define MAX_DFS_RESPONSE 56*1024 /* 56 Kb */
33 /* A DC set is a group of DC, they might have been grouped together
34 because they belong to the same site, or to site with same cost ...
36 struct dc_set {
37 const char **names;
38 uint32_t count;
42 fill a referral type structure
44 static NTSTATUS fill_normal_dfs_referraltype(TALLOC_CTX *mem_ctx,
45 struct dfs_referral_type *ref,
46 uint16_t version,
47 const char *dfs_path,
48 const char *server_path, int isfirstoffset)
50 ZERO_STRUCTP(ref);
51 switch (version) {
52 case 4:
53 ref->version = version;
54 /* For the moment there is a bug with XP that don't seems to appriciate much
55 * level4 so we return just level 3 for everyone
57 ref->referral.v4.server_type = DFS_SERVER_NON_ROOT;
58 /* "normal" referral seems to always include the GUID */
59 ref->referral.v4.size = 34;
61 if (isfirstoffset) {
62 ref->referral.v4.entry_flags = DFS_HEADER_FLAG_TARGET_BCK;
64 ref->referral.v4.ttl = 900; /* As w2k8r2 */
65 ref->referral.v4.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
66 if (ref->referral.v4.referrals.r1.DFS_path == NULL) {
67 return NT_STATUS_NO_MEMORY;
69 ref->referral.v4.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
70 if (ref->referral.v4.referrals.r1.DFS_alt_path == NULL) {
71 return NT_STATUS_NO_MEMORY;
73 ref->referral.v4.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
74 if (ref->referral.v4.referrals.r1.netw_address == NULL) {
75 return NT_STATUS_NO_MEMORY;
77 return NT_STATUS_OK;
78 case 3:
79 ref->version = version;
80 ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
81 /* "normal" referral seems to always include the GUID */
82 ref->referral.v3.size = 34;
84 ref->referral.v3.entry_flags = 0;
85 ref->referral.v3.ttl = 600; /* As w2k3 */
86 ref->referral.v3.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
87 if (ref->referral.v3.referrals.r1.DFS_path == NULL) {
88 return NT_STATUS_NO_MEMORY;
90 ref->referral.v3.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
91 if (ref->referral.v3.referrals.r1.DFS_alt_path == NULL) {
92 return NT_STATUS_NO_MEMORY;
94 ref->referral.v3.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
95 if (ref->referral.v3.referrals.r1.netw_address == NULL) {
96 return NT_STATUS_NO_MEMORY;
98 return NT_STATUS_OK;
100 return NT_STATUS_INVALID_LEVEL;
104 fill a domain refererral
106 static NTSTATUS fill_domain_dfs_referraltype(TALLOC_CTX *mem_ctx,
107 struct dfs_referral_type *ref,
108 uint16_t version,
109 const char *domain,
110 const char **names,
111 uint16_t numnames)
113 switch (version) {
114 case 3:
115 ZERO_STRUCTP(ref);
116 DEBUG(8, ("Called fill_domain_dfs_referraltype\n"));
117 ref->version = version;
118 ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
119 #if 0
120 /* We use to have variable size, on Windows 2008R2 it's the same
121 * and it seems that it gives better results so ... let's use the same
122 * size.
124 * Additional note: XP SP2 will ask for version 3 and SP3 for version 4.
127 * It's hard coded ... don't think it's a good way but the
128 * sizeof return not the correct values
130 * We have 18 if the GUID is not included 34 otherwise
132 if (numnames == 0) {
133 /* Windows return without the guid when returning domain list
135 ref->referral.v3.size = 18;
136 } else {
137 ref->referral.v3.size = 34;
139 #endif
140 /* As seen in w2k8r2 it always return the null GUID */
141 ref->referral.v3.size = 34;
142 ref->referral.v3.entry_flags = DFS_FLAG_REFERRAL_DOMAIN_RESP;
143 ref->referral.v3.ttl = 600; /* As w2k3 and w2k8r2*/
144 ref->referral.v3.referrals.r2.special_name = talloc_strdup(mem_ctx,
145 domain);
146 if (ref->referral.v3.referrals.r2.special_name == NULL) {
147 return NT_STATUS_NO_MEMORY;
149 ref->referral.v3.referrals.r2.nb_expanded_names = numnames;
150 /* Put the final terminator */
151 if (names) {
152 int i;
153 const char **names2 = talloc_array(mem_ctx, const char *,
154 numnames+1);
155 NT_STATUS_HAVE_NO_MEMORY(names2);
156 for (i = 0; i<numnames; i++) {
157 names2[i] = talloc_asprintf(names2, "\\%s", names[i]);
158 NT_STATUS_HAVE_NO_MEMORY(names2[i]);
160 names2[numnames] = NULL;
161 ref->referral.v3.referrals.r2.expanded_names = names2;
163 return NT_STATUS_OK;
165 return NT_STATUS_INVALID_LEVEL;
169 get the DCs list within a site
171 static NTSTATUS get_dcs_insite(TALLOC_CTX *ctx, struct ldb_context *ldb,
172 struct ldb_dn *sitedn, struct dc_set *list,
173 bool dofqdn)
175 static const char *attrs[] = { "serverReference", NULL };
176 static const char *attrs2[] = { "dNSHostName", "sAMAccountName", NULL };
177 struct ldb_result *r;
178 unsigned int i;
179 int ret;
180 const char **dc_list;
182 ret = ldb_search(ldb, ctx, &r, sitedn, LDB_SCOPE_SUBTREE, attrs,
183 "(&(objectClass=server)(serverReference=*))");
184 if (ret != LDB_SUCCESS) {
185 DEBUG(2,(__location__ ": Failed to get list of servers - %s\n",
186 ldb_errstring(ldb)));
187 return NT_STATUS_INTERNAL_ERROR;
190 if (r->count == 0) {
191 /* none in this site */
192 talloc_free(r);
193 return NT_STATUS_OK;
197 * need to search for all server object to know the size of the array.
198 * Search all the object of class server in this site
200 dc_list = talloc_array(r, const char *, r->count);
201 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(dc_list, r);
203 /* TODO put some random here in the order */
204 list->names = talloc_realloc(list, list->names, const char *, list->count + r->count);
205 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(list->names, r);
207 for (i = 0; i<r->count; i++) {
208 struct ldb_dn *dn;
209 struct ldb_result *r2;
211 dn = ldb_msg_find_attr_as_dn(ldb, ctx, r->msgs[i], "serverReference");
212 if (!dn) {
213 return NT_STATUS_INTERNAL_ERROR;
216 ret = ldb_search(ldb, r, &r2, dn, LDB_SCOPE_BASE, attrs2, "(objectClass=computer)");
217 if (ret != LDB_SUCCESS) {
218 DEBUG(2,(__location__ ": Search for computer on %s failed - %s\n",
219 ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
220 return NT_STATUS_INTERNAL_ERROR;
223 if (dofqdn) {
224 const char *dns = ldb_msg_find_attr_as_string(r2->msgs[0], "dNSHostName", NULL);
225 if (dns == NULL) {
226 DEBUG(2,(__location__ ": dNSHostName missing on %s\n",
227 ldb_dn_get_linearized(dn)));
228 talloc_free(r);
229 return NT_STATUS_INTERNAL_ERROR;
232 list->names[list->count] = talloc_strdup(list->names, dns);
233 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(list->names[list->count], r);
234 } else {
235 char *tmp;
236 const char *aname = ldb_msg_find_attr_as_string(r2->msgs[0], "sAMAccountName", NULL);
237 if (aname == NULL) {
238 DEBUG(2,(__location__ ": sAMAccountName missing on %s\n",
239 ldb_dn_get_linearized(dn)));
240 talloc_free(r);
241 return NT_STATUS_INTERNAL_ERROR;
244 tmp = talloc_strdup(list->names, aname);
245 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(tmp, r);
247 /* Netbios name is also the sAMAccountName for
248 computer but without the final $ */
249 tmp[strlen(tmp) - 1] = '\0';
250 list->names[list->count] = tmp;
252 list->count++;
253 talloc_free(r2);
256 talloc_free(r);
257 return NT_STATUS_OK;
262 get all DCs
264 static NTSTATUS get_dcs(TALLOC_CTX *ctx, struct ldb_context *ldb,
265 const char *searched_site, bool need_fqdn,
266 struct dc_set ***pset_list, uint32_t flags)
269 * Flags will be used later to indicate things like least-expensive
270 * or same-site options
272 const char *attrs_none[] = { NULL };
273 const char *attrs3[] = { "name", NULL };
274 struct ldb_dn *configdn, *sitedn, *dn, *sitescontainerdn;
275 struct ldb_result *r;
276 struct dc_set **set_list = NULL;
277 uint32_t i;
278 int ret;
279 uint32_t current_pos = 0;
280 NTSTATUS status;
281 TALLOC_CTX *subctx;
283 *pset_list = set_list = NULL;
285 subctx = talloc_new(ctx);
286 NT_STATUS_HAVE_NO_MEMORY(subctx);
288 configdn = ldb_get_config_basedn(ldb);
290 /* Let's search for the Site container */
291 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs_none,
292 "(objectClass=sitesContainer)");
293 if (ret != LDB_SUCCESS) {
294 DEBUG(2,(__location__ ": Failed to find sitesContainer within %s - %s\n",
295 ldb_dn_get_linearized(configdn), ldb_errstring(ldb)));
296 talloc_free(subctx);
297 return NT_STATUS_INTERNAL_ERROR;
299 if (r->count > 1) {
300 DEBUG(2,(__location__ ": Expected 1 sitesContainer - found %u within %s\n",
301 r->count, ldb_dn_get_linearized(configdn)));
302 talloc_free(subctx);
303 return NT_STATUS_INTERNAL_ERROR;
306 sitescontainerdn = talloc_steal(subctx, r->msgs[0]->dn);
307 talloc_free(r);
310 * TODO: Here we should have a more subtle handling
311 * for the case "same-site"
313 ret = ldb_search(ldb, subctx, &r, sitescontainerdn, LDB_SCOPE_SUBTREE,
314 attrs_none, "(objectClass=server)");
315 if (ret != LDB_SUCCESS) {
316 DEBUG(2,(__location__ ": Failed to find servers within %s - %s\n",
317 ldb_dn_get_linearized(sitescontainerdn), ldb_errstring(ldb)));
318 talloc_free(subctx);
319 return NT_STATUS_INTERNAL_ERROR;
321 talloc_free(r);
323 if (searched_site != NULL && searched_site[0] != '\0') {
324 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE,
325 attrs_none, "(&(name=%s)(objectClass=site))", searched_site);
326 if (ret != LDB_SUCCESS) {
327 talloc_free(subctx);
328 return NT_STATUS_FOOBAR;
329 } else if (r->count != 1) {
330 talloc_free(subctx);
331 return NT_STATUS_FOOBAR;
334 /* All of this was to get the DN of the searched_site */
335 sitedn = r->msgs[0]->dn;
337 set_list = talloc_realloc(subctx, set_list, struct dc_set *, current_pos+1);
338 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(set_list, subctx);
340 set_list[current_pos] = talloc(set_list, struct dc_set);
341 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(set_list[current_pos], subctx);
343 set_list[current_pos]->names = NULL;
344 set_list[current_pos]->count = 0;
345 status = get_dcs_insite(subctx, ldb, sitedn,
346 set_list[current_pos], need_fqdn);
347 if (!NT_STATUS_IS_OK(status)) {
348 DEBUG(2,(__location__ ": Failed to get DC from site %s - %s\n",
349 ldb_dn_get_linearized(sitedn), nt_errstr(status)));
350 talloc_free(subctx);
351 return status;
353 talloc_free(r);
354 current_pos++;
357 /* Let's find all the sites */
358 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs3, "(objectClass=site)");
359 if (ret != LDB_SUCCESS) {
360 DEBUG(2,(__location__ ": Failed to find any site containers in %s\n",
361 ldb_dn_get_linearized(configdn)));
362 talloc_free(subctx);
363 return NT_STATUS_INTERNAL_DB_CORRUPTION;
367 * TODO:
368 * We should randomize the order in the main site,
369 * it's mostly needed for sysvol/netlogon referral.
370 * Depending of flag we either randomize order of the
371 * not "in the same site DCs"
372 * or we randomize by group of site that have the same cost
373 * In the long run we want to manipulate an array of site_set
374 * All the site in one set have the same cost (if least-expansive options is selected)
375 * and we will put all the dc related to 1 site set into 1 DCs set.
376 * Within a site set, site order has to be randomized
378 * But for the moment we just return the list of sites
380 if (r->count) {
382 * We will realloc + 2 because we will need one additional place
383 * for element at current_pos + 1 for the NULL element
385 set_list = talloc_realloc(subctx, set_list, struct dc_set *,
386 current_pos+2);
387 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(set_list, subctx);
389 set_list[current_pos] = talloc(ctx, struct dc_set);
390 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(set_list[current_pos], subctx);
392 set_list[current_pos]->names = NULL;
393 set_list[current_pos]->count = 0;
395 set_list[current_pos+1] = NULL;
398 for (i=0; i<r->count; i++) {
399 const char *site_name = ldb_msg_find_attr_as_string(r->msgs[i], "name", NULL);
400 if (site_name == NULL) {
401 DEBUG(2,(__location__ ": Failed to find name attribute in %s\n",
402 ldb_dn_get_linearized(r->msgs[i]->dn)));
403 talloc_free(subctx);
404 return NT_STATUS_INTERNAL_DB_CORRUPTION;
407 if (searched_site == NULL ||
408 strcmp(searched_site, site_name) != 0) {
409 DEBUG(2,(__location__ ": Site: %s %s\n",
410 searched_site, site_name));
413 * Do all the site but the one of the client
414 * (because it has already been done ...)
416 dn = r->msgs[i]->dn;
418 status = get_dcs_insite(subctx, ldb, dn,
419 set_list[current_pos],
420 need_fqdn);
421 if (!NT_STATUS_IS_OK(status)) {
422 talloc_free(subctx);
423 return status;
427 current_pos++;
428 set_list[current_pos] = NULL;
430 *pset_list = talloc_move(ctx, &set_list);
431 talloc_free(subctx);
432 return NT_STATUS_OK;
435 static NTSTATUS dodomain_referral(struct loadparm_context *lp_ctx,
436 struct ldb_context *sam_ctx,
437 const struct tsocket_address *client,
438 struct dfs_GetDFSReferral *r)
441 * TODO for the moment we just return the local domain
443 NTSTATUS status;
444 const char *dns_domain = lpcfg_dnsdomain(lp_ctx);
445 const char *netbios_domain = lpcfg_workgroup(lp_ctx);
446 struct dfs_referral_type *referrals;
447 const char *referral_str;
448 /* In the future this needs to be fetched from the ldb */
449 uint32_t found_domain = 2;
451 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
452 DEBUG(10 ,("Received a domain referral request on a non DC\n"));
453 return NT_STATUS_INVALID_PARAMETER;
456 if (r->in.req.max_referral_level < 3) {
457 DEBUG(2,("invalid max_referral_level %u\n",
458 r->in.req.max_referral_level));
459 return NT_STATUS_UNSUCCESSFUL;
462 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
463 if (r->out.resp == NULL) {
464 return NT_STATUS_NO_MEMORY;
467 r->out.resp->path_consumed = 0;
468 r->out.resp->header_flags = 0; /* Do like w2k3 */
469 r->out.resp->nb_referrals = found_domain; /* the fqdn one + the NT domain */
471 referrals = talloc_zero_array(r->out.resp,
472 struct dfs_referral_type,
473 r->out.resp->nb_referrals);
474 if (referrals == NULL) {
475 return NT_STATUS_NO_MEMORY;
477 r->out.resp->referral_entries = referrals;
479 referral_str = talloc_asprintf(r, "\\%s", netbios_domain);
480 if (referral_str == NULL) {
481 return NT_STATUS_NO_MEMORY;
484 status = fill_domain_dfs_referraltype(referrals,
485 &referrals[0], 3,
486 referral_str,
487 NULL, 0);
488 if (!NT_STATUS_IS_OK(status)) {
489 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
490 __location__, nt_errstr(status)));
491 return status;
494 referral_str = talloc_asprintf(r, "\\%s", dns_domain);
495 if (referral_str == NULL) {
496 return NT_STATUS_NO_MEMORY;
499 status = fill_domain_dfs_referraltype(referrals,
500 &referrals[1], 3,
501 referral_str,
502 NULL, 0);
503 if (!NT_STATUS_IS_OK(status)) {
504 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
505 __location__, nt_errstr(status)));
506 return status;
509 return NT_STATUS_OK;
513 * Handle the logic for dfs referral request like
514 * \\dns_domain or \\netbios_domain.
516 static NTSTATUS dodc_referral(struct loadparm_context *lp_ctx,
517 struct ldb_context *sam_ctx,
518 const struct tsocket_address *client,
519 struct dfs_GetDFSReferral *r,
520 const char *domain_name)
522 NTSTATUS status;
523 const char *site_name = NULL; /* Name of the site where the client is */
524 bool need_fqdn = false;
525 unsigned int i;
526 const char **dc_list = NULL;
527 uint32_t num_dcs = 0;
528 struct dc_set **set;
529 char *client_str = NULL;
530 struct dfs_referral_type *referrals;
531 const char *referral_str;
533 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
534 return NT_STATUS_INVALID_PARAMETER;
537 if (r->in.req.max_referral_level < 3) {
538 DEBUG(2,("invalid max_referral_level %u\n",
539 r->in.req.max_referral_level));
540 return NT_STATUS_UNSUCCESSFUL;
543 DEBUG(10, ("in this we have request for %s requested is %s\n",
544 domain_name, r->in.req.servername));
546 if (strchr(domain_name,'.')) {
547 need_fqdn = 1;
550 if (tsocket_address_is_inet(client, "ip")) {
551 client_str = tsocket_address_inet_addr_string(client, r);
552 if (client_str == NULL) {
553 return NT_STATUS_NO_MEMORY;
557 site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL);
559 status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
560 if (!NT_STATUS_IS_OK(status)) {
561 DEBUG(3,("Unable to get list of DCs - %s\n",
562 nt_errstr(status)));
563 return status;
566 for(i=0; set[i]; i++) {
567 uint32_t j;
569 dc_list = talloc_realloc(r, dc_list, const char*,
570 num_dcs + set[i]->count + 1);
571 if (dc_list == NULL) {
572 return NT_STATUS_NO_MEMORY;
575 for(j=0; j<set[i]->count; j++) {
576 dc_list[num_dcs + j] = talloc_move(dc_list,
577 &set[i]->names[j]);
579 num_dcs = num_dcs + set[i]->count;
580 TALLOC_FREE(set[i]);
581 dc_list[num_dcs] = NULL;
584 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
585 if (r->out.resp == NULL) {
586 return NT_STATUS_NO_MEMORY;
589 r->out.resp->path_consumed = 0;
590 r->out.resp->header_flags = 0; /* Do like w2k3 */
591 r->out.resp->nb_referrals = 1;
593 referrals = talloc_zero_array(r->out.resp,
594 struct dfs_referral_type,
595 r->out.resp->nb_referrals);
596 if (referrals == NULL) {
597 return NT_STATUS_NO_MEMORY;
599 r->out.resp->referral_entries = referrals;
601 if (r->in.req.servername[0] == '\\') {
602 referral_str = talloc_asprintf(referrals, "%s",
603 domain_name);
604 } else {
605 referral_str = talloc_asprintf(referrals, "\\%s",
606 domain_name);
608 if (referral_str == NULL) {
609 return NT_STATUS_NO_MEMORY;
612 status = fill_domain_dfs_referraltype(referrals,
613 &referrals[0], 3,
614 referral_str,
615 dc_list, num_dcs);
616 if (!NT_STATUS_IS_OK(status)) {
617 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
618 __location__, nt_errstr(status)));
619 return status;
622 return NT_STATUS_OK;
626 * Handle the logic for dfs referral request like
627 * \\domain\sysvol or \\domain\netlogon
629 static NTSTATUS dosysvol_referral(struct loadparm_context *lp_ctx,
630 struct ldb_context *sam_ctx,
631 const struct tsocket_address *client,
632 struct dfs_GetDFSReferral *r,
633 const char *domain_name,
634 const char *dfs_name)
636 const char *site_name = NULL; /* Name of the site where the client is */
637 bool need_fqdn = false;
638 unsigned int i, c = 0, nb_entries = 0;
639 struct dc_set **set;
640 char *client_str = NULL;
641 NTSTATUS status;
642 struct dfs_referral_type *referrals;
644 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
645 return NT_STATUS_INVALID_PARAMETER;
648 if (r->in.req.max_referral_level < 3) {
649 DEBUG(2,("invalid max_referral_level %u\n",
650 r->in.req.max_referral_level));
651 return NT_STATUS_UNSUCCESSFUL;
654 DEBUG(10, ("in this we have request for %s and share %s requested is %s\n",
655 domain_name, dfs_name, r->in.req.servername));
657 if (strchr(domain_name,'.')) {
658 need_fqdn = 1;
661 if (tsocket_address_is_inet(client, "ip")) {
662 client_str = tsocket_address_inet_addr_string(client, r);
663 if (client_str == NULL) {
664 return NT_STATUS_NO_MEMORY;
668 site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL);
670 status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
671 if (!NT_STATUS_IS_OK(status)) {
672 DEBUG(3,("Unable to get list of DCs - %s\n",
673 nt_errstr(status)));
674 return status;
677 for(i=0; set[i]; i++) {
678 nb_entries = nb_entries + set[i]->count;
681 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
682 if (r->out.resp == NULL) {
683 return NT_STATUS_NO_MEMORY;
686 /* The length is expected in bytes */
687 r->out.resp->path_consumed = strlen_m(r->in.req.servername) * 2;
688 /* Do like w2k3 and like in 3.3.5.3 of MS-DFSC*/
689 r->out.resp->header_flags = DFS_HEADER_FLAG_STORAGE_SVR;
690 r->out.resp->nb_referrals = nb_entries;
692 referrals = talloc_zero_array(r->out.resp,
693 struct dfs_referral_type,
694 r->out.resp->nb_referrals);
695 if (referrals == NULL) {
696 return NT_STATUS_NO_MEMORY;
698 r->out.resp->referral_entries = referrals;
700 c = 0;
701 for(i=0; set[i]; i++) {
702 uint32_t j;
704 for(j=0; j< set[i]->count; j++) {
705 struct dfs_referral_type *ref = &referrals[c];
706 const char *referral_str;
708 referral_str = talloc_asprintf(referrals, "\\%s\\%s",
709 set[i]->names[j], dfs_name);
710 if (referral_str == NULL) {
711 return NT_STATUS_NO_MEMORY;
714 DEBUG(8,("Doing a dfs referral for %s with this value "
715 "%s requested %s\n",
716 set[i]->names[j], referral_str,
717 r->in.req.servername));
719 status = fill_normal_dfs_referraltype(referrals, ref,
720 r->in.req.max_referral_level,
721 r->in.req.servername,
722 referral_str, c==0);
725 if (!NT_STATUS_IS_OK(status)) {
726 DEBUG(2,("%s: Unable to fill domain referral "
727 "structure - %s\n",
728 __location__, nt_errstr(status)));
729 return status;
732 c++;
736 return NT_STATUS_OK;
740 trans2 getdfsreferral implementation
742 NTSTATUS dfs_server_ad_get_referrals(struct loadparm_context *lp_ctx,
743 struct ldb_context *sam_ctx,
744 const struct tsocket_address *client,
745 struct dfs_GetDFSReferral *r)
747 char *server_name = NULL;
748 char *dfs_name = NULL;
749 char *link_path = NULL;
750 const char *netbios_domain;
751 const char *dns_domain;
752 const char *netbios_name;
753 const char *dns_name;
755 if (!lpcfg_host_msdfs(lp_ctx)) {
756 return NT_STATUS_FS_DRIVER_REQUIRED;
759 if (r->in.req.servername == NULL) {
760 return NT_STATUS_INVALID_PARAMETER;
763 DEBUG(8, ("Requested DFS name: %s length: %u\n",
764 r->in.req.servername,
765 (unsigned int)strlen_m(r->in.req.servername)*2));
768 * If the servername is "" then we are in a case of domain dfs
769 * and the client just searches for the list of local domain
770 * it is attached and also trusted ones.
772 if (strlen(r->in.req.servername) == 0) {
773 return dodomain_referral(lp_ctx, sam_ctx, client, r);
776 server_name = talloc_strdup(r, r->in.req.servername);
777 if (server_name == NULL) {
778 return NT_STATUS_NO_MEMORY;
781 while(*server_name && *server_name == '\\') {
782 server_name++;
785 dfs_name = strchr(server_name, '\\');
786 if (dfs_name != NULL) {
787 dfs_name[0] = '\0';
788 dfs_name++;
790 link_path = strchr(dfs_name, '\\');
791 if (link_path != NULL) {
792 link_path[0] = '\0';
793 link_path++;
797 if (link_path != NULL) {
799 * If it is a DFS Link we do not
800 * handle it here.
802 return NT_STATUS_NOT_FOUND;
805 netbios_domain = lpcfg_workgroup(lp_ctx);
806 dns_domain = lpcfg_dnsdomain(lp_ctx);
807 netbios_name = lpcfg_netbios_name(lp_ctx);
808 dns_name = talloc_asprintf(r, "%s.%s", netbios_name, dns_domain);
809 if (dns_name == NULL) {
810 return NT_STATUS_NO_MEMORY;
813 if ((strcasecmp_m(server_name, netbios_name) == 0) ||
814 (strcasecmp_m(server_name, dns_name) == 0)) {
816 * If it is not domain related do not
817 * handle it here.
819 return NT_STATUS_NOT_FOUND;
822 if (is_ipaddress(server_name)) {
824 * If it is not domain related do not
825 * handle it here.
827 return NT_STATUS_NOT_FOUND;
830 if ((strcasecmp_m(server_name, netbios_domain) != 0) &&
831 (strcasecmp_m(server_name, dns_domain) != 0)) {
833 * Not a domain we handle.
835 return NT_STATUS_INVALID_PARAMETER;
839 * Here we have filtered the thing the requested name don't contain our DNS name.
840 * So if the share == NULL or if share in ("sysvol", "netlogon")
841 * then we proceed. In the first case it will be a dc refereal in the second it will
842 * be just a sysvol/netlogon referral.
844 if (dfs_name == NULL) {
845 return dodc_referral(lp_ctx, sam_ctx,
846 client, r, server_name);
850 * Here we have filtered the thing the requested name don't contain our DNS name.
851 * So if the share == NULL or if share in ("sysvol", "netlogon")
852 * then we proceed. In the first case it will be a dc refereal in the second it will
853 * be just a sysvol/netlogon referral.
855 if (strcasecmp(dfs_name, "sysvol") == 0 ||
856 strcasecmp(dfs_name, "netlogon") == 0) {
857 return dosysvol_referral(lp_ctx, sam_ctx, client, r,
858 server_name, dfs_name);
861 /* By default until all the case are handled */
862 return NT_STATUS_NOT_FOUND;