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