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/>.
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 ...
41 static void shuffle_dc_set(struct dc_set
*list
)
45 for (i
= list
->count
; i
> 1; i
--) {
49 r
= generate_random() % i
;
51 tmp
= list
->names
[i
- 1];
52 list
->names
[i
- 1] = list
->names
[r
];
58 fill a referral type structure
60 static NTSTATUS
fill_normal_dfs_referraltype(TALLOC_CTX
*mem_ctx
,
61 struct dfs_referral_type
*ref
,
64 const char *server_path
, int isfirstoffset
)
69 ref
->version
= version
;
70 /* For the moment there is a bug with XP that don't seems to appriciate much
71 * level4 so we return just level 3 for everyone
73 ref
->referral
.v4
.server_type
= DFS_SERVER_NON_ROOT
;
74 /* "normal" referral seems to always include the GUID */
75 ref
->referral
.v4
.size
= 34;
78 ref
->referral
.v4
.entry_flags
= DFS_HEADER_FLAG_TARGET_BCK
;
80 ref
->referral
.v4
.ttl
= 900; /* As w2k8r2 */
81 ref
->referral
.v4
.referrals
.r1
.DFS_path
= talloc_strdup(mem_ctx
, dfs_path
);
82 if (ref
->referral
.v4
.referrals
.r1
.DFS_path
== NULL
) {
83 return NT_STATUS_NO_MEMORY
;
85 ref
->referral
.v4
.referrals
.r1
.DFS_alt_path
= talloc_strdup(mem_ctx
, dfs_path
);
86 if (ref
->referral
.v4
.referrals
.r1
.DFS_alt_path
== NULL
) {
87 return NT_STATUS_NO_MEMORY
;
89 ref
->referral
.v4
.referrals
.r1
.netw_address
= talloc_strdup(mem_ctx
, server_path
);
90 if (ref
->referral
.v4
.referrals
.r1
.netw_address
== NULL
) {
91 return NT_STATUS_NO_MEMORY
;
95 ref
->version
= version
;
96 ref
->referral
.v3
.server_type
= DFS_SERVER_NON_ROOT
;
97 /* "normal" referral seems to always include the GUID */
98 ref
->referral
.v3
.size
= 34;
100 ref
->referral
.v3
.entry_flags
= 0;
101 ref
->referral
.v3
.ttl
= 600; /* As w2k3 */
102 ref
->referral
.v3
.referrals
.r1
.DFS_path
= talloc_strdup(mem_ctx
, dfs_path
);
103 if (ref
->referral
.v3
.referrals
.r1
.DFS_path
== NULL
) {
104 return NT_STATUS_NO_MEMORY
;
106 ref
->referral
.v3
.referrals
.r1
.DFS_alt_path
= talloc_strdup(mem_ctx
, dfs_path
);
107 if (ref
->referral
.v3
.referrals
.r1
.DFS_alt_path
== NULL
) {
108 return NT_STATUS_NO_MEMORY
;
110 ref
->referral
.v3
.referrals
.r1
.netw_address
= talloc_strdup(mem_ctx
, server_path
);
111 if (ref
->referral
.v3
.referrals
.r1
.netw_address
== NULL
) {
112 return NT_STATUS_NO_MEMORY
;
116 return NT_STATUS_INVALID_LEVEL
;
120 fill a domain refererral
122 static NTSTATUS
fill_domain_dfs_referraltype(TALLOC_CTX
*mem_ctx
,
123 struct dfs_referral_type
*ref
,
132 DEBUG(8, ("Called fill_domain_dfs_referraltype\n"));
133 ref
->version
= version
;
134 ref
->referral
.v3
.server_type
= DFS_SERVER_NON_ROOT
;
136 /* We use to have variable size, on Windows 2008R2 it's the same
137 * and it seems that it gives better results so ... let's use the same
140 * Additional note: XP SP2 will ask for version 3 and SP3 for version 4.
143 * It's hard coded ... don't think it's a good way but the
144 * sizeof return not the correct values
146 * We have 18 if the GUID is not included 34 otherwise
149 /* Windows return without the guid when returning domain list
151 ref
->referral
.v3
.size
= 18;
153 ref
->referral
.v3
.size
= 34;
156 /* As seen in w2k8r2 it always return the null GUID */
157 ref
->referral
.v3
.size
= 34;
158 ref
->referral
.v3
.entry_flags
= DFS_FLAG_REFERRAL_DOMAIN_RESP
;
159 ref
->referral
.v3
.ttl
= 600; /* As w2k3 and w2k8r2*/
160 ref
->referral
.v3
.referrals
.r2
.special_name
= talloc_strdup(mem_ctx
,
162 if (ref
->referral
.v3
.referrals
.r2
.special_name
== NULL
) {
163 return NT_STATUS_NO_MEMORY
;
165 ref
->referral
.v3
.referrals
.r2
.nb_expanded_names
= numnames
;
166 /* Put the final terminator */
169 const char **names2
= talloc_array(mem_ctx
, const char *,
171 NT_STATUS_HAVE_NO_MEMORY(names2
);
172 for (i
= 0; i
<numnames
; i
++) {
173 names2
[i
] = talloc_asprintf(names2
, "\\%s", names
[i
]);
174 NT_STATUS_HAVE_NO_MEMORY(names2
[i
]);
176 names2
[numnames
] = NULL
;
177 ref
->referral
.v3
.referrals
.r2
.expanded_names
= names2
;
181 return NT_STATUS_INVALID_LEVEL
;
185 get the DCs list within a site
187 static NTSTATUS
get_dcs_insite(TALLOC_CTX
*ctx
, struct ldb_context
*ldb
,
188 struct ldb_dn
*sitedn
, struct dc_set
*list
,
191 static const char *attrs
[] = { "serverReference", NULL
};
192 static const char *attrs2
[] = { "dNSHostName", "sAMAccountName", NULL
};
193 struct ldb_result
*r
;
196 const char **dc_list
;
198 ret
= ldb_search(ldb
, ctx
, &r
, sitedn
, LDB_SCOPE_SUBTREE
, attrs
,
199 "(&(objectClass=server)(serverReference=*))");
200 if (ret
!= LDB_SUCCESS
) {
201 DEBUG(2,(__location__
": Failed to get list of servers - %s\n",
202 ldb_errstring(ldb
)));
203 return NT_STATUS_INTERNAL_ERROR
;
207 /* none in this site */
213 * need to search for all server object to know the size of the array.
214 * Search all the object of class server in this site
216 dc_list
= talloc_array(r
, const char *, r
->count
);
217 if (dc_list
== NULL
) {
219 return NT_STATUS_NO_MEMORY
;
222 /* TODO put some random here in the order */
223 list
->names
= talloc_realloc(list
, list
->names
, const char *, list
->count
+ r
->count
);
224 if (list
->names
== NULL
) {
226 return NT_STATUS_NO_MEMORY
;
229 for (i
= 0; i
<r
->count
; i
++) {
231 struct ldb_message
*msg
;
233 dn
= ldb_msg_find_attr_as_dn(ldb
, ctx
, r
->msgs
[i
], "serverReference");
235 return NT_STATUS_INTERNAL_ERROR
;
238 ret
= dsdb_search_one(ldb
, r
, &msg
, dn
, LDB_SCOPE_BASE
, attrs2
, 0, "(objectClass=computer)");
239 if (ret
!= LDB_SUCCESS
) {
240 DEBUG(2,(__location__
": Search for computer on %s failed - %s\n",
241 ldb_dn_get_linearized(dn
), ldb_errstring(ldb
)));
242 return NT_STATUS_INTERNAL_ERROR
;
246 const char *dns
= ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
);
248 DEBUG(2,(__location__
": dNSHostName missing on %s\n",
249 ldb_dn_get_linearized(dn
)));
251 return NT_STATUS_INTERNAL_ERROR
;
254 list
->names
[list
->count
] = talloc_strdup(list
->names
, dns
);
255 if (list
->names
[list
->count
] == NULL
) {
257 return NT_STATUS_NO_MEMORY
;
261 const char *aname
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
263 DEBUG(2,(__location__
": sAMAccountName missing on %s\n",
264 ldb_dn_get_linearized(dn
)));
266 return NT_STATUS_INTERNAL_ERROR
;
269 tmp
= talloc_strdup(list
->names
, aname
);
272 return NT_STATUS_NO_MEMORY
;
275 /* Netbios name is also the sAMAccountName for
276 computer but without the final $ */
277 tmp
[strlen(tmp
) - 1] = '\0';
278 list
->names
[list
->count
] = tmp
;
284 shuffle_dc_set(list
);
294 static NTSTATUS
get_dcs(TALLOC_CTX
*ctx
, struct ldb_context
*ldb
,
295 const char *searched_site
, bool need_fqdn
,
296 struct dc_set
***pset_list
, uint32_t flags
)
299 * Flags will be used later to indicate things like least-expensive
300 * or same-site options
302 const char *attrs_none
[] = { NULL
};
303 const char *attrs3
[] = { "name", NULL
};
304 struct ldb_dn
*configdn
, *sitedn
, *dn
, *sitescontainerdn
;
305 struct ldb_result
*r
;
306 struct dc_set
**set_list
= NULL
;
309 uint32_t current_pos
= 0;
313 *pset_list
= set_list
= NULL
;
315 subctx
= talloc_new(ctx
);
316 NT_STATUS_HAVE_NO_MEMORY(subctx
);
318 configdn
= ldb_get_config_basedn(ldb
);
320 /* Let's search for the Site container */
321 ret
= ldb_search(ldb
, subctx
, &r
, configdn
, LDB_SCOPE_SUBTREE
, attrs_none
,
322 "(objectClass=sitesContainer)");
323 if (ret
!= LDB_SUCCESS
) {
324 DEBUG(2,(__location__
": Failed to find sitesContainer within %s - %s\n",
325 ldb_dn_get_linearized(configdn
), ldb_errstring(ldb
)));
327 return NT_STATUS_INTERNAL_ERROR
;
330 DEBUG(2,(__location__
": Expected 1 sitesContainer - found %u within %s\n",
331 r
->count
, ldb_dn_get_linearized(configdn
)));
333 return NT_STATUS_INTERNAL_ERROR
;
336 sitescontainerdn
= talloc_steal(subctx
, r
->msgs
[0]->dn
);
340 * TODO: Here we should have a more subtle handling
341 * for the case "same-site"
343 ret
= ldb_search(ldb
, subctx
, &r
, sitescontainerdn
, LDB_SCOPE_SUBTREE
,
344 attrs_none
, "(objectClass=server)");
345 if (ret
!= LDB_SUCCESS
) {
346 DEBUG(2,(__location__
": Failed to find servers within %s - %s\n",
347 ldb_dn_get_linearized(sitescontainerdn
), ldb_errstring(ldb
)));
349 return NT_STATUS_INTERNAL_ERROR
;
353 if (searched_site
!= NULL
&& searched_site
[0] != '\0') {
354 ret
= ldb_search(ldb
, subctx
, &r
, configdn
, LDB_SCOPE_SUBTREE
,
355 attrs_none
, "(&(name=%s)(objectClass=site))", searched_site
);
356 if (ret
!= LDB_SUCCESS
) {
358 return NT_STATUS_FOOBAR
;
359 } else if (r
->count
!= 1) {
361 return NT_STATUS_FOOBAR
;
364 /* All of this was to get the DN of the searched_site */
365 sitedn
= r
->msgs
[0]->dn
;
368 * We will realloc + 2 because we will need one additional place
369 * for element at current_pos + 1 for the NULL element
371 set_list
= talloc_realloc(subctx
, set_list
, struct dc_set
*, current_pos
+2);
372 if (set_list
== NULL
) {
374 return NT_STATUS_NO_MEMORY
;
377 set_list
[current_pos
] = talloc(set_list
, struct dc_set
);
378 if (set_list
[current_pos
] == NULL
) {
380 return NT_STATUS_NO_MEMORY
;
383 set_list
[current_pos
]->names
= NULL
;
384 set_list
[current_pos
]->count
= 0;
386 set_list
[current_pos
+1] = NULL
;
388 status
= get_dcs_insite(subctx
, ldb
, sitedn
,
389 set_list
[current_pos
], need_fqdn
);
390 if (!NT_STATUS_IS_OK(status
)) {
391 DEBUG(2,(__location__
": Failed to get DC from site %s - %s\n",
392 ldb_dn_get_linearized(sitedn
), nt_errstr(status
)));
400 /* Let's find all the sites */
401 ret
= ldb_search(ldb
, subctx
, &r
, configdn
, LDB_SCOPE_SUBTREE
, attrs3
, "(objectClass=site)");
402 if (ret
!= LDB_SUCCESS
) {
403 DEBUG(2,(__location__
": Failed to find any site containers in %s\n",
404 ldb_dn_get_linearized(configdn
)));
406 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
411 * We should randomize the order in the main site,
412 * it's mostly needed for sysvol/netlogon referral.
413 * Depending of flag we either randomize order of the
414 * not "in the same site DCs"
415 * or we randomize by group of site that have the same cost
416 * In the long run we want to manipulate an array of site_set
417 * All the site in one set have the same cost (if least-expansive options is selected)
418 * and we will put all the dc related to 1 site set into 1 DCs set.
419 * Within a site set, site order has to be randomized
421 * But for the moment we just return the list of sites
425 * We will realloc + 2 because we will need one additional place
426 * for element at current_pos + 1 for the NULL element
428 set_list
= talloc_realloc(subctx
, set_list
, struct dc_set
*,
430 if (set_list
== NULL
) {
432 return NT_STATUS_NO_MEMORY
;
435 set_list
[current_pos
] = talloc(ctx
, struct dc_set
);
436 if (set_list
[current_pos
] == NULL
) {
438 return NT_STATUS_NO_MEMORY
;
441 set_list
[current_pos
]->names
= NULL
;
442 set_list
[current_pos
]->count
= 0;
444 set_list
[current_pos
+1] = NULL
;
447 for (i
=0; i
<r
->count
; i
++) {
448 const char *site_name
= ldb_msg_find_attr_as_string(r
->msgs
[i
], "name", NULL
);
449 if (site_name
== NULL
) {
450 DEBUG(2,(__location__
": Failed to find name attribute in %s\n",
451 ldb_dn_get_linearized(r
->msgs
[i
]->dn
)));
453 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
456 if (searched_site
== NULL
||
457 strcmp(searched_site
, site_name
) != 0) {
458 DEBUG(2,(__location__
": Site: %s %s\n",
459 searched_site
, site_name
));
462 * Do all the site but the one of the client
463 * (because it has already been done ...)
467 status
= get_dcs_insite(subctx
, ldb
, dn
,
468 set_list
[current_pos
],
470 if (!NT_STATUS_IS_OK(status
)) {
477 *pset_list
= talloc_move(ctx
, &set_list
);
482 static NTSTATUS
dodomain_referral(struct loadparm_context
*lp_ctx
,
483 struct ldb_context
*sam_ctx
,
484 const struct tsocket_address
*client
,
485 struct dfs_GetDFSReferral
*r
)
488 * TODO for the moment we just return the local domain
491 const char *dns_domain
= lpcfg_dnsdomain(lp_ctx
);
492 const char *netbios_domain
= lpcfg_workgroup(lp_ctx
);
493 struct dfs_referral_type
*referrals
;
494 const char *referral_str
;
495 /* In the future this needs to be fetched from the ldb */
496 uint32_t found_domain
= 2;
498 if (lpcfg_server_role(lp_ctx
) != ROLE_ACTIVE_DIRECTORY_DC
) {
499 DEBUG(10 ,("Received a domain referral request on a non DC\n"));
500 return NT_STATUS_INVALID_PARAMETER
;
503 if (r
->in
.req
.max_referral_level
< 3) {
504 DEBUG(2,("invalid max_referral_level %u\n",
505 r
->in
.req
.max_referral_level
));
506 return NT_STATUS_UNSUCCESSFUL
;
509 r
->out
.resp
= talloc_zero(r
, struct dfs_referral_resp
);
510 if (r
->out
.resp
== NULL
) {
511 return NT_STATUS_NO_MEMORY
;
514 r
->out
.resp
->path_consumed
= 0;
515 r
->out
.resp
->header_flags
= 0; /* Do like w2k3 */
516 r
->out
.resp
->nb_referrals
= found_domain
; /* the fqdn one + the NT domain */
518 referrals
= talloc_zero_array(r
->out
.resp
,
519 struct dfs_referral_type
,
520 r
->out
.resp
->nb_referrals
);
521 if (referrals
== NULL
) {
522 return NT_STATUS_NO_MEMORY
;
524 r
->out
.resp
->referral_entries
= referrals
;
526 referral_str
= talloc_asprintf(r
, "\\%s", netbios_domain
);
527 if (referral_str
== NULL
) {
528 return NT_STATUS_NO_MEMORY
;
531 status
= fill_domain_dfs_referraltype(referrals
,
535 if (!NT_STATUS_IS_OK(status
)) {
536 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
537 __location__
, nt_errstr(status
)));
541 referral_str
= talloc_asprintf(r
, "\\%s", dns_domain
);
542 if (referral_str
== NULL
) {
543 return NT_STATUS_NO_MEMORY
;
546 status
= fill_domain_dfs_referraltype(referrals
,
550 if (!NT_STATUS_IS_OK(status
)) {
551 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
552 __location__
, nt_errstr(status
)));
560 * Handle the logic for dfs referral request like
561 * \\dns_domain or \\netbios_domain.
563 static NTSTATUS
dodc_referral(struct loadparm_context
*lp_ctx
,
564 struct ldb_context
*sam_ctx
,
565 const struct tsocket_address
*client
,
566 struct dfs_GetDFSReferral
*r
,
567 const char *domain_name
)
570 const char *site_name
= NULL
; /* Name of the site where the client is */
571 bool need_fqdn
= false;
573 const char **dc_list
= NULL
;
574 uint32_t num_dcs
= 0;
576 char *client_str
= NULL
;
577 struct dfs_referral_type
*referrals
;
578 const char *referral_str
;
580 if (lpcfg_server_role(lp_ctx
) != ROLE_ACTIVE_DIRECTORY_DC
) {
581 return NT_STATUS_INVALID_PARAMETER
;
584 if (r
->in
.req
.max_referral_level
< 3) {
585 DEBUG(2,("invalid max_referral_level %u\n",
586 r
->in
.req
.max_referral_level
));
587 return NT_STATUS_UNSUCCESSFUL
;
590 DEBUG(10, ("in this we have request for %s requested is %s\n",
591 domain_name
, r
->in
.req
.servername
));
593 if (strchr(domain_name
,'.')) {
597 if (tsocket_address_is_inet(client
, "ip")) {
598 client_str
= tsocket_address_inet_addr_string(client
, r
);
599 if (client_str
== NULL
) {
600 return NT_STATUS_NO_MEMORY
;
604 site_name
= samdb_client_site_name(sam_ctx
, r
, client_str
, NULL
);
606 status
= get_dcs(r
, sam_ctx
, site_name
, need_fqdn
, &set
, 0);
607 if (!NT_STATUS_IS_OK(status
)) {
608 DEBUG(3,("Unable to get list of DCs - %s\n",
613 for(i
=0; set
[i
]; i
++) {
616 dc_list
= talloc_realloc(r
, dc_list
, const char*,
617 num_dcs
+ set
[i
]->count
+ 1);
618 if (dc_list
== NULL
) {
619 return NT_STATUS_NO_MEMORY
;
622 for(j
=0; j
<set
[i
]->count
; j
++) {
623 dc_list
[num_dcs
+ j
] = talloc_move(dc_list
,
626 num_dcs
= num_dcs
+ set
[i
]->count
;
628 dc_list
[num_dcs
] = NULL
;
631 r
->out
.resp
= talloc_zero(r
, struct dfs_referral_resp
);
632 if (r
->out
.resp
== NULL
) {
633 return NT_STATUS_NO_MEMORY
;
636 r
->out
.resp
->path_consumed
= 0;
637 r
->out
.resp
->header_flags
= 0; /* Do like w2k3 */
638 r
->out
.resp
->nb_referrals
= 1;
640 referrals
= talloc_zero_array(r
->out
.resp
,
641 struct dfs_referral_type
,
642 r
->out
.resp
->nb_referrals
);
643 if (referrals
== NULL
) {
644 return NT_STATUS_NO_MEMORY
;
646 r
->out
.resp
->referral_entries
= referrals
;
648 if (r
->in
.req
.servername
[0] == '\\') {
649 referral_str
= talloc_asprintf(referrals
, "%s",
652 referral_str
= talloc_asprintf(referrals
, "\\%s",
655 if (referral_str
== NULL
) {
656 return NT_STATUS_NO_MEMORY
;
659 status
= fill_domain_dfs_referraltype(referrals
,
663 if (!NT_STATUS_IS_OK(status
)) {
664 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
665 __location__
, nt_errstr(status
)));
673 * Handle the logic for dfs referral request like
674 * \\domain\sysvol or \\domain\netlogon
676 static NTSTATUS
dosysvol_referral(struct loadparm_context
*lp_ctx
,
677 struct ldb_context
*sam_ctx
,
678 const struct tsocket_address
*client
,
679 struct dfs_GetDFSReferral
*r
,
680 const char *domain_name
,
681 const char *dfs_name
)
683 const char *site_name
= NULL
; /* Name of the site where the client is */
684 bool need_fqdn
= false;
685 unsigned int i
, c
= 0, nb_entries
= 0;
687 char *client_str
= NULL
;
689 struct dfs_referral_type
*referrals
;
691 if (lpcfg_server_role(lp_ctx
) != ROLE_ACTIVE_DIRECTORY_DC
) {
692 return NT_STATUS_INVALID_PARAMETER
;
695 if (r
->in
.req
.max_referral_level
< 3) {
696 DEBUG(2,("invalid max_referral_level %u\n",
697 r
->in
.req
.max_referral_level
));
698 return NT_STATUS_UNSUCCESSFUL
;
701 DEBUG(10, ("in this we have request for %s and share %s requested is %s\n",
702 domain_name
, dfs_name
, r
->in
.req
.servername
));
704 if (strchr(domain_name
,'.')) {
708 if (tsocket_address_is_inet(client
, "ip")) {
709 client_str
= tsocket_address_inet_addr_string(client
, r
);
710 if (client_str
== NULL
) {
711 return NT_STATUS_NO_MEMORY
;
715 site_name
= samdb_client_site_name(sam_ctx
, r
, client_str
, NULL
);
717 status
= get_dcs(r
, sam_ctx
, site_name
, need_fqdn
, &set
, 0);
718 if (!NT_STATUS_IS_OK(status
)) {
719 DEBUG(3,("Unable to get list of DCs - %s\n",
724 for(i
=0; set
[i
]; i
++) {
725 nb_entries
= nb_entries
+ set
[i
]->count
;
728 r
->out
.resp
= talloc_zero(r
, struct dfs_referral_resp
);
729 if (r
->out
.resp
== NULL
) {
730 return NT_STATUS_NO_MEMORY
;
733 /* The length is expected in bytes */
734 r
->out
.resp
->path_consumed
= strlen_m(r
->in
.req
.servername
) * 2;
735 /* Do like w2k3 and like in 3.3.5.3 of MS-DFSC*/
736 r
->out
.resp
->header_flags
= DFS_HEADER_FLAG_STORAGE_SVR
;
737 r
->out
.resp
->nb_referrals
= nb_entries
;
739 referrals
= talloc_zero_array(r
->out
.resp
,
740 struct dfs_referral_type
,
741 r
->out
.resp
->nb_referrals
);
742 if (referrals
== NULL
) {
743 return NT_STATUS_NO_MEMORY
;
745 r
->out
.resp
->referral_entries
= referrals
;
748 for(i
=0; set
[i
]; i
++) {
751 for(j
=0; j
< set
[i
]->count
; j
++) {
752 struct dfs_referral_type
*ref
= &referrals
[c
];
753 const char *referral_str
;
755 referral_str
= talloc_asprintf(referrals
, "\\%s\\%s",
756 set
[i
]->names
[j
], dfs_name
);
757 if (referral_str
== NULL
) {
758 return NT_STATUS_NO_MEMORY
;
761 DEBUG(8,("Doing a dfs referral for %s with this value "
763 set
[i
]->names
[j
], referral_str
,
764 r
->in
.req
.servername
));
766 status
= fill_normal_dfs_referraltype(referrals
, ref
,
767 r
->in
.req
.max_referral_level
,
768 r
->in
.req
.servername
,
772 if (!NT_STATUS_IS_OK(status
)) {
773 DEBUG(2,("%s: Unable to fill domain referral "
775 __location__
, nt_errstr(status
)));
787 trans2 getdfsreferral implementation
789 NTSTATUS
dfs_server_ad_get_referrals(struct loadparm_context
*lp_ctx
,
790 struct ldb_context
*sam_ctx
,
791 const struct tsocket_address
*client
,
792 struct dfs_GetDFSReferral
*r
)
794 char *server_name
= NULL
;
795 char *dfs_name
= NULL
;
796 char *link_path
= NULL
;
797 const char *netbios_domain
;
798 const char *dns_domain
;
799 const char *netbios_name
;
800 const char *dns_name
;
801 const char **netbios_aliases
;
804 if (!lpcfg_host_msdfs(lp_ctx
)) {
805 return NT_STATUS_FS_DRIVER_REQUIRED
;
808 if (r
->in
.req
.servername
== NULL
) {
809 return NT_STATUS_INVALID_PARAMETER
;
812 DEBUG(8, ("Requested DFS name: %s length: %u\n",
813 r
->in
.req
.servername
,
814 (unsigned int)strlen_m(r
->in
.req
.servername
)*2));
817 * If the servername is "" then we are in a case of domain dfs
818 * and the client just searches for the list of local domain
819 * it is attached and also trusted ones.
821 if (strlen(r
->in
.req
.servername
) == 0) {
822 return dodomain_referral(lp_ctx
, sam_ctx
, client
, r
);
825 server_name
= talloc_strdup(r
, r
->in
.req
.servername
);
826 if (server_name
== NULL
) {
827 return NT_STATUS_NO_MEMORY
;
830 path_separator
= (*server_name
== '/') ? '/' : '\\';
832 while(*server_name
&& *server_name
== path_separator
) {
836 dfs_name
= strchr_m(server_name
, path_separator
);
837 if (dfs_name
!= NULL
) {
841 link_path
= strchr_m(dfs_name
, path_separator
);
842 if (link_path
!= NULL
) {
848 if (link_path
!= NULL
) {
850 * If it is a DFS Link we do not
853 return NT_STATUS_NOT_FOUND
;
856 netbios_domain
= lpcfg_workgroup(lp_ctx
);
857 dns_domain
= lpcfg_dnsdomain(lp_ctx
);
858 netbios_name
= lpcfg_netbios_name(lp_ctx
);
859 dns_name
= talloc_asprintf(r
, "%s.%s", netbios_name
, dns_domain
);
860 if (dns_name
== NULL
) {
861 return NT_STATUS_NO_MEMORY
;
864 if ((strcasecmp_m(server_name
, netbios_name
) == 0) ||
865 (strcasecmp_m(server_name
, dns_name
) == 0)) {
867 * If it is not domain related do not
870 return NT_STATUS_NOT_FOUND
;
873 if (is_ipaddress(server_name
)) {
875 * If it is not domain related do not
878 return NT_STATUS_NOT_FOUND
;
881 netbios_aliases
= lpcfg_netbios_aliases(lp_ctx
);
882 while (netbios_aliases
&& *netbios_aliases
) {
883 const char *netbios_alias
= *netbios_aliases
;
887 cmp
= strcasecmp_m(server_name
, netbios_alias
);
890 * If it is not domain related do not
893 return NT_STATUS_NOT_FOUND
;
896 dns_alias
= talloc_asprintf(r
, "%s.%s",
899 if (dns_alias
== NULL
) {
900 return NT_STATUS_NO_MEMORY
;
903 cmp
= strcasecmp_m(server_name
, dns_alias
);
904 talloc_free(dns_alias
);
907 * If it is not domain related do not
910 return NT_STATUS_NOT_FOUND
;
915 if ((strcasecmp_m(server_name
, netbios_domain
) != 0) &&
916 (strcasecmp_m(server_name
, dns_domain
) != 0)) {
918 * Not a domain we handle.
920 return NT_STATUS_INVALID_PARAMETER
;
924 * Here we have filtered the thing the requested name don't contain our DNS name.
925 * So if the share == NULL or if share in ("sysvol", "netlogon")
926 * then we proceed. In the first case it will be a dc refereal in the second it will
927 * be just a sysvol/netlogon referral.
929 if (dfs_name
== NULL
) {
930 return dodc_referral(lp_ctx
, sam_ctx
,
931 client
, r
, server_name
);
935 * Here we have filtered the thing the requested name don't contain our DNS name.
936 * So if the share == NULL or if share in ("sysvol", "netlogon")
937 * then we proceed. In the first case it will be a dc refereal in the second it will
938 * be just a sysvol/netlogon referral.
940 if (strcasecmp(dfs_name
, "sysvol") == 0 ||
941 strcasecmp(dfs_name
, "netlogon") == 0) {
942 return dosysvol_referral(lp_ctx
, sam_ctx
, client
, r
,
943 server_name
, dfs_name
);
946 /* By default until all the case are handled */
947 return NT_STATUS_NOT_FOUND
;