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