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
)
47 for (i
= list
->count
; i
> 1; i
--) {
53 tmp
= list
->names
[i
- 1];
54 list
->names
[i
- 1] = list
->names
[r
];
60 fill a referral type structure
62 static NTSTATUS
fill_normal_dfs_referraltype(TALLOC_CTX
*mem_ctx
,
63 struct dfs_referral_type
*ref
,
66 const char *server_path
, int isfirstoffset
)
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;
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
;
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
;
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
,
134 DEBUG(8, ("Called fill_domain_dfs_referraltype\n"));
135 ref
->version
= version
;
136 ref
->referral
.v3
.server_type
= DFS_SERVER_NON_ROOT
;
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
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
151 /* Windows return without the guid when returning domain list
153 ref
->referral
.v3
.size
= 18;
155 ref
->referral
.v3
.size
= 34;
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
,
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 */
171 const char **names2
= talloc_array(mem_ctx
, const char *,
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
;
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
,
193 static const char *attrs
[] = { "serverReference", NULL
};
194 static const char *attrs2
[] = { "dNSHostName", "sAMAccountName", NULL
};
195 struct ldb_result
*r
;
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
;
209 /* none in this site */
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
) {
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
) {
228 return NT_STATUS_NO_MEMORY
;
231 for (i
= 0; i
<r
->count
; i
++) {
233 struct ldb_message
*msg
;
235 dn
= ldb_msg_find_attr_as_dn(ldb
, ctx
, r
->msgs
[i
], "serverReference");
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
;
248 const char *dns
= ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
);
250 DEBUG(2,(__location__
": dNSHostName missing on %s\n",
251 ldb_dn_get_linearized(dn
)));
253 return NT_STATUS_INTERNAL_ERROR
;
256 list
->names
[list
->count
] = talloc_strdup(list
->names
, dns
);
257 if (list
->names
[list
->count
] == NULL
) {
259 return NT_STATUS_NO_MEMORY
;
263 const char *aname
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
265 DEBUG(2,(__location__
": sAMAccountName missing on %s\n",
266 ldb_dn_get_linearized(dn
)));
268 return NT_STATUS_INTERNAL_ERROR
;
271 tmp
= talloc_strdup(list
->names
, aname
);
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
;
286 shuffle_dc_set(list
);
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
;
311 uint32_t current_pos
= 0;
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
)));
329 return NT_STATUS_INTERNAL_ERROR
;
332 DEBUG(2,(__location__
": Expected 1 sitesContainer - found %u within %s\n",
333 r
->count
, ldb_dn_get_linearized(configdn
)));
335 return NT_STATUS_INTERNAL_ERROR
;
338 sitescontainerdn
= talloc_steal(subctx
, r
->msgs
[0]->dn
);
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
)));
351 return NT_STATUS_INTERNAL_ERROR
;
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
) {
360 return NT_STATUS_FOOBAR
;
361 } else if (r
->count
!= 1) {
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
) {
372 return NT_STATUS_NO_MEMORY
;
375 set_list
[current_pos
] = talloc(set_list
, struct dc_set
);
376 if (set_list
[current_pos
] == NULL
) {
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
)));
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
)));
401 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
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
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
*,
425 if (set_list
== NULL
) {
427 return NT_STATUS_NO_MEMORY
;
430 set_list
[current_pos
] = talloc(ctx
, struct dc_set
);
431 if (set_list
[current_pos
] == NULL
) {
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
)));
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 ...)
462 status
= get_dcs_insite(subctx
, ldb
, dn
,
463 set_list
[current_pos
],
465 if (!NT_STATUS_IS_OK(status
)) {
472 set_list
[current_pos
] = NULL
;
474 *pset_list
= talloc_move(ctx
, &set_list
);
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
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
,
532 if (!NT_STATUS_IS_OK(status
)) {
533 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
534 __location__
, nt_errstr(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
,
547 if (!NT_STATUS_IS_OK(status
)) {
548 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
549 __location__
, nt_errstr(status
)));
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
)
567 const char *site_name
= NULL
; /* Name of the site where the client is */
568 bool need_fqdn
= false;
570 const char **dc_list
= NULL
;
571 uint32_t num_dcs
= 0;
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
,'.')) {
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",
610 for(i
=0; set
[i
]; i
++) {
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
,
623 num_dcs
= num_dcs
+ set
[i
]->count
;
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",
649 referral_str
= talloc_asprintf(referrals
, "\\%s",
652 if (referral_str
== NULL
) {
653 return NT_STATUS_NO_MEMORY
;
656 status
= fill_domain_dfs_referraltype(referrals
,
660 if (!NT_STATUS_IS_OK(status
)) {
661 DEBUG(2,("%s: Unable to fill domain referral structure - %s\n",
662 __location__
, nt_errstr(status
)));
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;
684 char *client_str
= NULL
;
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
,'.')) {
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",
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
;
745 for(i
=0; set
[i
]; i
++) {
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 "
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
,
769 if (!NT_STATUS_IS_OK(status
)) {
770 DEBUG(2,("%s: Unable to fill domain referral "
772 __location__
, nt_errstr(status
)));
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
== '\\') {
830 dfs_name
= strchr(server_name
, '\\');
831 if (dfs_name
!= NULL
) {
835 link_path
= strchr(dfs_name
, '\\');
836 if (link_path
!= NULL
) {
842 if (link_path
!= NULL
) {
844 * If it is a DFS Link we do not
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
864 return NT_STATUS_NOT_FOUND
;
867 if (is_ipaddress(server_name
)) {
869 * If it is not domain related do not
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
;
881 cmp
= strcasecmp_m(server_name
, netbios_alias
);
884 * If it is not domain related do not
887 return NT_STATUS_NOT_FOUND
;
890 dns_alias
= talloc_asprintf(r
, "%s.%s",
893 if (dns_alias
== NULL
) {
894 return NT_STATUS_NO_MEMORY
;
897 cmp
= strcasecmp_m(server_name
, dns_alias
);
898 talloc_free(dns_alias
);
901 * If it is not domain related do not
904 return NT_STATUS_NOT_FOUND
;
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
;