messaging4: Store a names_db handle in the imessaging_context
[Samba.git] / dfs_server / dfs_server_ad.c
blob5e2634fb7a1a9dcedfb4f5f4b443ec2f6b1795f0
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;
41 static void shuffle_dc_set(struct dc_set *list)
43 uint32_t i;
45 srandom(time(NULL));
47 for (i = list->count; i > 1; i--) {
48 uint32_t r;
49 const char *tmp;
51 r = random() % i;
53 tmp = list->names[i - 1];
54 list->names[i - 1] = list->names[r];
55 list->names[r] = tmp;
60 fill a referral type structure
62 static NTSTATUS fill_normal_dfs_referraltype(TALLOC_CTX *mem_ctx,
63 struct dfs_referral_type *ref,
64 uint16_t version,
65 const char *dfs_path,
66 const char *server_path, int isfirstoffset)
68 ZERO_STRUCTP(ref);
69 switch (version) {
70 case 4:
71 ref->version = version;
72 /* For the moment there is a bug with XP that don't seems to appriciate much
73 * level4 so we return just level 3 for everyone
75 ref->referral.v4.server_type = DFS_SERVER_NON_ROOT;
76 /* "normal" referral seems to always include the GUID */
77 ref->referral.v4.size = 34;
79 if (isfirstoffset) {
80 ref->referral.v4.entry_flags = DFS_HEADER_FLAG_TARGET_BCK;
82 ref->referral.v4.ttl = 900; /* As w2k8r2 */
83 ref->referral.v4.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
84 if (ref->referral.v4.referrals.r1.DFS_path == NULL) {
85 return NT_STATUS_NO_MEMORY;
87 ref->referral.v4.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
88 if (ref->referral.v4.referrals.r1.DFS_alt_path == NULL) {
89 return NT_STATUS_NO_MEMORY;
91 ref->referral.v4.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
92 if (ref->referral.v4.referrals.r1.netw_address == NULL) {
93 return NT_STATUS_NO_MEMORY;
95 return NT_STATUS_OK;
96 case 3:
97 ref->version = version;
98 ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
99 /* "normal" referral seems to always include the GUID */
100 ref->referral.v3.size = 34;
102 ref->referral.v3.entry_flags = 0;
103 ref->referral.v3.ttl = 600; /* As w2k3 */
104 ref->referral.v3.referrals.r1.DFS_path = talloc_strdup(mem_ctx, dfs_path);
105 if (ref->referral.v3.referrals.r1.DFS_path == NULL) {
106 return NT_STATUS_NO_MEMORY;
108 ref->referral.v3.referrals.r1.DFS_alt_path = talloc_strdup(mem_ctx, dfs_path);
109 if (ref->referral.v3.referrals.r1.DFS_alt_path == NULL) {
110 return NT_STATUS_NO_MEMORY;
112 ref->referral.v3.referrals.r1.netw_address = talloc_strdup(mem_ctx, server_path);
113 if (ref->referral.v3.referrals.r1.netw_address == NULL) {
114 return NT_STATUS_NO_MEMORY;
116 return NT_STATUS_OK;
118 return NT_STATUS_INVALID_LEVEL;
122 fill a domain refererral
124 static NTSTATUS fill_domain_dfs_referraltype(TALLOC_CTX *mem_ctx,
125 struct dfs_referral_type *ref,
126 uint16_t version,
127 const char *domain,
128 const char **names,
129 uint16_t numnames)
131 switch (version) {
132 case 3:
133 ZERO_STRUCTP(ref);
134 DEBUG(8, ("Called fill_domain_dfs_referraltype\n"));
135 ref->version = version;
136 ref->referral.v3.server_type = DFS_SERVER_NON_ROOT;
137 #if 0
138 /* We use to have variable size, on Windows 2008R2 it's the same
139 * and it seems that it gives better results so ... let's use the same
140 * size.
142 * Additional note: XP SP2 will ask for version 3 and SP3 for version 4.
145 * It's hard coded ... don't think it's a good way but the
146 * sizeof return not the correct values
148 * We have 18 if the GUID is not included 34 otherwise
150 if (numnames == 0) {
151 /* Windows return without the guid when returning domain list
153 ref->referral.v3.size = 18;
154 } else {
155 ref->referral.v3.size = 34;
157 #endif
158 /* As seen in w2k8r2 it always return the null GUID */
159 ref->referral.v3.size = 34;
160 ref->referral.v3.entry_flags = DFS_FLAG_REFERRAL_DOMAIN_RESP;
161 ref->referral.v3.ttl = 600; /* As w2k3 and w2k8r2*/
162 ref->referral.v3.referrals.r2.special_name = talloc_strdup(mem_ctx,
163 domain);
164 if (ref->referral.v3.referrals.r2.special_name == NULL) {
165 return NT_STATUS_NO_MEMORY;
167 ref->referral.v3.referrals.r2.nb_expanded_names = numnames;
168 /* Put the final terminator */
169 if (names) {
170 int i;
171 const char **names2 = talloc_array(mem_ctx, const char *,
172 numnames+1);
173 NT_STATUS_HAVE_NO_MEMORY(names2);
174 for (i = 0; i<numnames; i++) {
175 names2[i] = talloc_asprintf(names2, "\\%s", names[i]);
176 NT_STATUS_HAVE_NO_MEMORY(names2[i]);
178 names2[numnames] = NULL;
179 ref->referral.v3.referrals.r2.expanded_names = names2;
181 return NT_STATUS_OK;
183 return NT_STATUS_INVALID_LEVEL;
187 get the DCs list within a site
189 static NTSTATUS get_dcs_insite(TALLOC_CTX *ctx, struct ldb_context *ldb,
190 struct ldb_dn *sitedn, struct dc_set *list,
191 bool dofqdn)
193 static const char *attrs[] = { "serverReference", NULL };
194 static const char *attrs2[] = { "dNSHostName", "sAMAccountName", NULL };
195 struct ldb_result *r;
196 unsigned int i;
197 int ret;
198 const char **dc_list;
200 ret = ldb_search(ldb, ctx, &r, sitedn, LDB_SCOPE_SUBTREE, attrs,
201 "(&(objectClass=server)(serverReference=*))");
202 if (ret != LDB_SUCCESS) {
203 DEBUG(2,(__location__ ": Failed to get list of servers - %s\n",
204 ldb_errstring(ldb)));
205 return NT_STATUS_INTERNAL_ERROR;
208 if (r->count == 0) {
209 /* none in this site */
210 talloc_free(r);
211 return NT_STATUS_OK;
215 * need to search for all server object to know the size of the array.
216 * Search all the object of class server in this site
218 dc_list = talloc_array(r, const char *, r->count);
219 if (dc_list == NULL) {
220 TALLOC_FREE(r);
221 return NT_STATUS_NO_MEMORY;
224 /* TODO put some random here in the order */
225 list->names = talloc_realloc(list, list->names, const char *, list->count + r->count);
226 if (list->names == NULL) {
227 TALLOC_FREE(r);
228 return NT_STATUS_NO_MEMORY;
231 for (i = 0; i<r->count; i++) {
232 struct ldb_dn *dn;
233 struct ldb_message *msg;
235 dn = ldb_msg_find_attr_as_dn(ldb, ctx, r->msgs[i], "serverReference");
236 if (!dn) {
237 return NT_STATUS_INTERNAL_ERROR;
240 ret = dsdb_search_one(ldb, r, &msg, dn, LDB_SCOPE_BASE, attrs2, 0, "(objectClass=computer)");
241 if (ret != LDB_SUCCESS) {
242 DEBUG(2,(__location__ ": Search for computer on %s failed - %s\n",
243 ldb_dn_get_linearized(dn), ldb_errstring(ldb)));
244 return NT_STATUS_INTERNAL_ERROR;
247 if (dofqdn) {
248 const char *dns = ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL);
249 if (dns == NULL) {
250 DEBUG(2,(__location__ ": dNSHostName missing on %s\n",
251 ldb_dn_get_linearized(dn)));
252 talloc_free(r);
253 return NT_STATUS_INTERNAL_ERROR;
256 list->names[list->count] = talloc_strdup(list->names, dns);
257 if (list->names[list->count] == NULL) {
258 TALLOC_FREE(r);
259 return NT_STATUS_NO_MEMORY;
261 } else {
262 char *tmp;
263 const char *aname = ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL);
264 if (aname == NULL) {
265 DEBUG(2,(__location__ ": sAMAccountName missing on %s\n",
266 ldb_dn_get_linearized(dn)));
267 talloc_free(r);
268 return NT_STATUS_INTERNAL_ERROR;
271 tmp = talloc_strdup(list->names, aname);
272 if (tmp == NULL) {
273 TALLOC_FREE(r);
274 return NT_STATUS_NO_MEMORY;
277 /* Netbios name is also the sAMAccountName for
278 computer but without the final $ */
279 tmp[strlen(tmp) - 1] = '\0';
280 list->names[list->count] = tmp;
282 list->count++;
283 talloc_free(msg);
286 shuffle_dc_set(list);
288 talloc_free(r);
289 return NT_STATUS_OK;
294 get all DCs
296 static NTSTATUS get_dcs(TALLOC_CTX *ctx, struct ldb_context *ldb,
297 const char *searched_site, bool need_fqdn,
298 struct dc_set ***pset_list, uint32_t flags)
301 * Flags will be used later to indicate things like least-expensive
302 * or same-site options
304 const char *attrs_none[] = { NULL };
305 const char *attrs3[] = { "name", NULL };
306 struct ldb_dn *configdn, *sitedn, *dn, *sitescontainerdn;
307 struct ldb_result *r;
308 struct dc_set **set_list = NULL;
309 uint32_t i;
310 int ret;
311 uint32_t current_pos = 0;
312 NTSTATUS status;
313 TALLOC_CTX *subctx;
315 *pset_list = set_list = NULL;
317 subctx = talloc_new(ctx);
318 NT_STATUS_HAVE_NO_MEMORY(subctx);
320 configdn = ldb_get_config_basedn(ldb);
322 /* Let's search for the Site container */
323 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs_none,
324 "(objectClass=sitesContainer)");
325 if (ret != LDB_SUCCESS) {
326 DEBUG(2,(__location__ ": Failed to find sitesContainer within %s - %s\n",
327 ldb_dn_get_linearized(configdn), ldb_errstring(ldb)));
328 talloc_free(subctx);
329 return NT_STATUS_INTERNAL_ERROR;
331 if (r->count > 1) {
332 DEBUG(2,(__location__ ": Expected 1 sitesContainer - found %u within %s\n",
333 r->count, ldb_dn_get_linearized(configdn)));
334 talloc_free(subctx);
335 return NT_STATUS_INTERNAL_ERROR;
338 sitescontainerdn = talloc_steal(subctx, r->msgs[0]->dn);
339 talloc_free(r);
342 * TODO: Here we should have a more subtle handling
343 * for the case "same-site"
345 ret = ldb_search(ldb, subctx, &r, sitescontainerdn, LDB_SCOPE_SUBTREE,
346 attrs_none, "(objectClass=server)");
347 if (ret != LDB_SUCCESS) {
348 DEBUG(2,(__location__ ": Failed to find servers within %s - %s\n",
349 ldb_dn_get_linearized(sitescontainerdn), ldb_errstring(ldb)));
350 talloc_free(subctx);
351 return NT_STATUS_INTERNAL_ERROR;
353 talloc_free(r);
355 if (searched_site != NULL && searched_site[0] != '\0') {
356 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE,
357 attrs_none, "(&(name=%s)(objectClass=site))", searched_site);
358 if (ret != LDB_SUCCESS) {
359 talloc_free(subctx);
360 return NT_STATUS_FOOBAR;
361 } else if (r->count != 1) {
362 talloc_free(subctx);
363 return NT_STATUS_FOOBAR;
366 /* All of this was to get the DN of the searched_site */
367 sitedn = r->msgs[0]->dn;
369 set_list = talloc_realloc(subctx, set_list, struct dc_set *, current_pos+1);
370 if (set_list == NULL) {
371 TALLOC_FREE(subctx);
372 return NT_STATUS_NO_MEMORY;
375 set_list[current_pos] = talloc(set_list, struct dc_set);
376 if (set_list[current_pos] == NULL) {
377 TALLOC_FREE(subctx);
378 return NT_STATUS_NO_MEMORY;
381 set_list[current_pos]->names = NULL;
382 set_list[current_pos]->count = 0;
383 status = get_dcs_insite(subctx, ldb, sitedn,
384 set_list[current_pos], need_fqdn);
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(2,(__location__ ": Failed to get DC from site %s - %s\n",
387 ldb_dn_get_linearized(sitedn), nt_errstr(status)));
388 talloc_free(subctx);
389 return status;
391 talloc_free(r);
392 current_pos++;
395 /* Let's find all the sites */
396 ret = ldb_search(ldb, subctx, &r, configdn, LDB_SCOPE_SUBTREE, attrs3, "(objectClass=site)");
397 if (ret != LDB_SUCCESS) {
398 DEBUG(2,(__location__ ": Failed to find any site containers in %s\n",
399 ldb_dn_get_linearized(configdn)));
400 talloc_free(subctx);
401 return NT_STATUS_INTERNAL_DB_CORRUPTION;
405 * TODO:
406 * We should randomize the order in the main site,
407 * it's mostly needed for sysvol/netlogon referral.
408 * Depending of flag we either randomize order of the
409 * not "in the same site DCs"
410 * or we randomize by group of site that have the same cost
411 * In the long run we want to manipulate an array of site_set
412 * All the site in one set have the same cost (if least-expansive options is selected)
413 * and we will put all the dc related to 1 site set into 1 DCs set.
414 * Within a site set, site order has to be randomized
416 * But for the moment we just return the list of sites
418 if (r->count) {
420 * We will realloc + 2 because we will need one additional place
421 * for element at current_pos + 1 for the NULL element
423 set_list = talloc_realloc(subctx, set_list, struct dc_set *,
424 current_pos+2);
425 if (set_list == NULL) {
426 TALLOC_FREE(subctx);
427 return NT_STATUS_NO_MEMORY;
430 set_list[current_pos] = talloc(ctx, struct dc_set);
431 if (set_list[current_pos] == NULL) {
432 TALLOC_FREE(subctx);
433 return NT_STATUS_NO_MEMORY;
436 set_list[current_pos]->names = NULL;
437 set_list[current_pos]->count = 0;
439 set_list[current_pos+1] = NULL;
442 for (i=0; i<r->count; i++) {
443 const char *site_name = ldb_msg_find_attr_as_string(r->msgs[i], "name", NULL);
444 if (site_name == NULL) {
445 DEBUG(2,(__location__ ": Failed to find name attribute in %s\n",
446 ldb_dn_get_linearized(r->msgs[i]->dn)));
447 talloc_free(subctx);
448 return NT_STATUS_INTERNAL_DB_CORRUPTION;
451 if (searched_site == NULL ||
452 strcmp(searched_site, site_name) != 0) {
453 DEBUG(2,(__location__ ": Site: %s %s\n",
454 searched_site, site_name));
457 * Do all the site but the one of the client
458 * (because it has already been done ...)
460 dn = r->msgs[i]->dn;
462 status = get_dcs_insite(subctx, ldb, dn,
463 set_list[current_pos],
464 need_fqdn);
465 if (!NT_STATUS_IS_OK(status)) {
466 talloc_free(subctx);
467 return status;
471 current_pos++;
472 set_list[current_pos] = NULL;
474 *pset_list = talloc_move(ctx, &set_list);
475 talloc_free(subctx);
476 return NT_STATUS_OK;
479 static NTSTATUS dodomain_referral(struct loadparm_context *lp_ctx,
480 struct ldb_context *sam_ctx,
481 const struct tsocket_address *client,
482 struct dfs_GetDFSReferral *r)
485 * TODO for the moment we just return the local domain
487 NTSTATUS status;
488 const char *dns_domain = lpcfg_dnsdomain(lp_ctx);
489 const char *netbios_domain = lpcfg_workgroup(lp_ctx);
490 struct dfs_referral_type *referrals;
491 const char *referral_str;
492 /* In the future this needs to be fetched from the ldb */
493 uint32_t found_domain = 2;
495 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
496 DEBUG(10 ,("Received a domain referral request on a non DC\n"));
497 return NT_STATUS_INVALID_PARAMETER;
500 if (r->in.req.max_referral_level < 3) {
501 DEBUG(2,("invalid max_referral_level %u\n",
502 r->in.req.max_referral_level));
503 return NT_STATUS_UNSUCCESSFUL;
506 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
507 if (r->out.resp == NULL) {
508 return NT_STATUS_NO_MEMORY;
511 r->out.resp->path_consumed = 0;
512 r->out.resp->header_flags = 0; /* Do like w2k3 */
513 r->out.resp->nb_referrals = found_domain; /* the fqdn one + the NT domain */
515 referrals = talloc_zero_array(r->out.resp,
516 struct dfs_referral_type,
517 r->out.resp->nb_referrals);
518 if (referrals == NULL) {
519 return NT_STATUS_NO_MEMORY;
521 r->out.resp->referral_entries = referrals;
523 referral_str = talloc_asprintf(r, "\\%s", netbios_domain);
524 if (referral_str == NULL) {
525 return NT_STATUS_NO_MEMORY;
528 status = fill_domain_dfs_referraltype(referrals,
529 &referrals[0], 3,
530 referral_str,
531 NULL, 0);
532 if (!NT_STATUS_IS_OK(status)) {
533 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
534 __location__, nt_errstr(status)));
535 return status;
538 referral_str = talloc_asprintf(r, "\\%s", dns_domain);
539 if (referral_str == NULL) {
540 return NT_STATUS_NO_MEMORY;
543 status = fill_domain_dfs_referraltype(referrals,
544 &referrals[1], 3,
545 referral_str,
546 NULL, 0);
547 if (!NT_STATUS_IS_OK(status)) {
548 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
549 __location__, nt_errstr(status)));
550 return status;
553 return NT_STATUS_OK;
557 * Handle the logic for dfs referral request like
558 * \\dns_domain or \\netbios_domain.
560 static NTSTATUS dodc_referral(struct loadparm_context *lp_ctx,
561 struct ldb_context *sam_ctx,
562 const struct tsocket_address *client,
563 struct dfs_GetDFSReferral *r,
564 const char *domain_name)
566 NTSTATUS status;
567 const char *site_name = NULL; /* Name of the site where the client is */
568 bool need_fqdn = false;
569 unsigned int i;
570 const char **dc_list = NULL;
571 uint32_t num_dcs = 0;
572 struct dc_set **set;
573 char *client_str = NULL;
574 struct dfs_referral_type *referrals;
575 const char *referral_str;
577 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
578 return NT_STATUS_INVALID_PARAMETER;
581 if (r->in.req.max_referral_level < 3) {
582 DEBUG(2,("invalid max_referral_level %u\n",
583 r->in.req.max_referral_level));
584 return NT_STATUS_UNSUCCESSFUL;
587 DEBUG(10, ("in this we have request for %s requested is %s\n",
588 domain_name, r->in.req.servername));
590 if (strchr(domain_name,'.')) {
591 need_fqdn = 1;
594 if (tsocket_address_is_inet(client, "ip")) {
595 client_str = tsocket_address_inet_addr_string(client, r);
596 if (client_str == NULL) {
597 return NT_STATUS_NO_MEMORY;
601 site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL);
603 status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
604 if (!NT_STATUS_IS_OK(status)) {
605 DEBUG(3,("Unable to get list of DCs - %s\n",
606 nt_errstr(status)));
607 return status;
610 for(i=0; set[i]; i++) {
611 uint32_t j;
613 dc_list = talloc_realloc(r, dc_list, const char*,
614 num_dcs + set[i]->count + 1);
615 if (dc_list == NULL) {
616 return NT_STATUS_NO_MEMORY;
619 for(j=0; j<set[i]->count; j++) {
620 dc_list[num_dcs + j] = talloc_move(dc_list,
621 &set[i]->names[j]);
623 num_dcs = num_dcs + set[i]->count;
624 TALLOC_FREE(set[i]);
625 dc_list[num_dcs] = NULL;
628 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
629 if (r->out.resp == NULL) {
630 return NT_STATUS_NO_MEMORY;
633 r->out.resp->path_consumed = 0;
634 r->out.resp->header_flags = 0; /* Do like w2k3 */
635 r->out.resp->nb_referrals = 1;
637 referrals = talloc_zero_array(r->out.resp,
638 struct dfs_referral_type,
639 r->out.resp->nb_referrals);
640 if (referrals == NULL) {
641 return NT_STATUS_NO_MEMORY;
643 r->out.resp->referral_entries = referrals;
645 if (r->in.req.servername[0] == '\\') {
646 referral_str = talloc_asprintf(referrals, "%s",
647 domain_name);
648 } else {
649 referral_str = talloc_asprintf(referrals, "\\%s",
650 domain_name);
652 if (referral_str == NULL) {
653 return NT_STATUS_NO_MEMORY;
656 status = fill_domain_dfs_referraltype(referrals,
657 &referrals[0], 3,
658 referral_str,
659 dc_list, num_dcs);
660 if (!NT_STATUS_IS_OK(status)) {
661 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
662 __location__, nt_errstr(status)));
663 return status;
666 return NT_STATUS_OK;
670 * Handle the logic for dfs referral request like
671 * \\domain\sysvol or \\domain\netlogon
673 static NTSTATUS dosysvol_referral(struct loadparm_context *lp_ctx,
674 struct ldb_context *sam_ctx,
675 const struct tsocket_address *client,
676 struct dfs_GetDFSReferral *r,
677 const char *domain_name,
678 const char *dfs_name)
680 const char *site_name = NULL; /* Name of the site where the client is */
681 bool need_fqdn = false;
682 unsigned int i, c = 0, nb_entries = 0;
683 struct dc_set **set;
684 char *client_str = NULL;
685 NTSTATUS status;
686 struct dfs_referral_type *referrals;
688 if (lpcfg_server_role(lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
689 return NT_STATUS_INVALID_PARAMETER;
692 if (r->in.req.max_referral_level < 3) {
693 DEBUG(2,("invalid max_referral_level %u\n",
694 r->in.req.max_referral_level));
695 return NT_STATUS_UNSUCCESSFUL;
698 DEBUG(10, ("in this we have request for %s and share %s requested is %s\n",
699 domain_name, dfs_name, r->in.req.servername));
701 if (strchr(domain_name,'.')) {
702 need_fqdn = 1;
705 if (tsocket_address_is_inet(client, "ip")) {
706 client_str = tsocket_address_inet_addr_string(client, r);
707 if (client_str == NULL) {
708 return NT_STATUS_NO_MEMORY;
712 site_name = samdb_client_site_name(sam_ctx, r, client_str, NULL);
714 status = get_dcs(r, sam_ctx, site_name, need_fqdn, &set, 0);
715 if (!NT_STATUS_IS_OK(status)) {
716 DEBUG(3,("Unable to get list of DCs - %s\n",
717 nt_errstr(status)));
718 return status;
721 for(i=0; set[i]; i++) {
722 nb_entries = nb_entries + set[i]->count;
725 r->out.resp = talloc_zero(r, struct dfs_referral_resp);
726 if (r->out.resp == NULL) {
727 return NT_STATUS_NO_MEMORY;
730 /* The length is expected in bytes */
731 r->out.resp->path_consumed = strlen_m(r->in.req.servername) * 2;
732 /* Do like w2k3 and like in 3.3.5.3 of MS-DFSC*/
733 r->out.resp->header_flags = DFS_HEADER_FLAG_STORAGE_SVR;
734 r->out.resp->nb_referrals = nb_entries;
736 referrals = talloc_zero_array(r->out.resp,
737 struct dfs_referral_type,
738 r->out.resp->nb_referrals);
739 if (referrals == NULL) {
740 return NT_STATUS_NO_MEMORY;
742 r->out.resp->referral_entries = referrals;
744 c = 0;
745 for(i=0; set[i]; i++) {
746 uint32_t j;
748 for(j=0; j< set[i]->count; j++) {
749 struct dfs_referral_type *ref = &referrals[c];
750 const char *referral_str;
752 referral_str = talloc_asprintf(referrals, "\\%s\\%s",
753 set[i]->names[j], dfs_name);
754 if (referral_str == NULL) {
755 return NT_STATUS_NO_MEMORY;
758 DEBUG(8,("Doing a dfs referral for %s with this value "
759 "%s requested %s\n",
760 set[i]->names[j], referral_str,
761 r->in.req.servername));
763 status = fill_normal_dfs_referraltype(referrals, ref,
764 r->in.req.max_referral_level,
765 r->in.req.servername,
766 referral_str, c==0);
769 if (!NT_STATUS_IS_OK(status)) {
770 DEBUG(2,("%s: Unable to fill domain referral "
771 "structure - %s\n",
772 __location__, nt_errstr(status)));
773 return status;
776 c++;
780 return NT_STATUS_OK;
784 trans2 getdfsreferral implementation
786 NTSTATUS dfs_server_ad_get_referrals(struct loadparm_context *lp_ctx,
787 struct ldb_context *sam_ctx,
788 const struct tsocket_address *client,
789 struct dfs_GetDFSReferral *r)
791 char *server_name = NULL;
792 char *dfs_name = NULL;
793 char *link_path = NULL;
794 const char *netbios_domain;
795 const char *dns_domain;
796 const char *netbios_name;
797 const char *dns_name;
798 const char **netbios_aliases;
800 if (!lpcfg_host_msdfs(lp_ctx)) {
801 return NT_STATUS_FS_DRIVER_REQUIRED;
804 if (r->in.req.servername == NULL) {
805 return NT_STATUS_INVALID_PARAMETER;
808 DEBUG(8, ("Requested DFS name: %s length: %u\n",
809 r->in.req.servername,
810 (unsigned int)strlen_m(r->in.req.servername)*2));
813 * If the servername is "" then we are in a case of domain dfs
814 * and the client just searches for the list of local domain
815 * it is attached and also trusted ones.
817 if (strlen(r->in.req.servername) == 0) {
818 return dodomain_referral(lp_ctx, sam_ctx, client, r);
821 server_name = talloc_strdup(r, r->in.req.servername);
822 if (server_name == NULL) {
823 return NT_STATUS_NO_MEMORY;
826 while(*server_name && *server_name == '\\') {
827 server_name++;
830 dfs_name = strchr(server_name, '\\');
831 if (dfs_name != NULL) {
832 dfs_name[0] = '\0';
833 dfs_name++;
835 link_path = strchr(dfs_name, '\\');
836 if (link_path != NULL) {
837 link_path[0] = '\0';
838 link_path++;
842 if (link_path != NULL) {
844 * If it is a DFS Link we do not
845 * handle it here.
847 return NT_STATUS_NOT_FOUND;
850 netbios_domain = lpcfg_workgroup(lp_ctx);
851 dns_domain = lpcfg_dnsdomain(lp_ctx);
852 netbios_name = lpcfg_netbios_name(lp_ctx);
853 dns_name = talloc_asprintf(r, "%s.%s", netbios_name, dns_domain);
854 if (dns_name == NULL) {
855 return NT_STATUS_NO_MEMORY;
858 if ((strcasecmp_m(server_name, netbios_name) == 0) ||
859 (strcasecmp_m(server_name, dns_name) == 0)) {
861 * If it is not domain related do not
862 * handle it here.
864 return NT_STATUS_NOT_FOUND;
867 if (is_ipaddress(server_name)) {
869 * If it is not domain related do not
870 * handle it here.
872 return NT_STATUS_NOT_FOUND;
875 netbios_aliases = lpcfg_netbios_aliases(lp_ctx);
876 while (netbios_aliases && *netbios_aliases) {
877 const char *netbios_alias = *netbios_aliases;
878 char *dns_alias;
879 int cmp;
881 cmp = strcasecmp_m(server_name, netbios_alias);
882 if (cmp == 0) {
884 * If it is not domain related do not
885 * handle it here.
887 return NT_STATUS_NOT_FOUND;
890 dns_alias = talloc_asprintf(r, "%s.%s",
891 netbios_alias,
892 dns_domain);
893 if (dns_alias == NULL) {
894 return NT_STATUS_NO_MEMORY;
897 cmp = strcasecmp_m(server_name, dns_alias);
898 talloc_free(dns_alias);
899 if (cmp == 0) {
901 * If it is not domain related do not
902 * handle it here.
904 return NT_STATUS_NOT_FOUND;
906 netbios_aliases++;
909 if ((strcasecmp_m(server_name, netbios_domain) != 0) &&
910 (strcasecmp_m(server_name, dns_domain) != 0)) {
912 * Not a domain we handle.
914 return NT_STATUS_INVALID_PARAMETER;
918 * Here we have filtered the thing the requested name don't contain our DNS name.
919 * So if the share == NULL or if share in ("sysvol", "netlogon")
920 * then we proceed. In the first case it will be a dc refereal in the second it will
921 * be just a sysvol/netlogon referral.
923 if (dfs_name == NULL) {
924 return dodc_referral(lp_ctx, sam_ctx,
925 client, r, server_name);
929 * Here we have filtered the thing the requested name don't contain our DNS name.
930 * So if the share == NULL or if share in ("sysvol", "netlogon")
931 * then we proceed. In the first case it will be a dc refereal in the second it will
932 * be just a sysvol/netlogon referral.
934 if (strcasecmp(dfs_name, "sysvol") == 0 ||
935 strcasecmp(dfs_name, "netlogon") == 0) {
936 return dosysvol_referral(lp_ctx, sam_ctx, client, r,
937 server_name, dfs_name);
940 /* By default until all the case are handled */
941 return NT_STATUS_NOT_FOUND;