ce0cc89899c0113c99972ada73690e75ebc1ab89
[samba.git] / source3 / libsmb / dsgetdcname.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    dsgetdcname
5
6    Copyright (C) Gerald Carter 2006
7    Copyright (C) Guenther Deschner 2007-2008
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libads/sitename_cache.h"
25 #include "../librpc/gen_ndr/ndr_netlogon.h"
26 #include "libads/cldap.h"
27 #include "../lib/addns/dnsquery.h"
28 #include "libsmb/clidgram.h"
29
30 /* 15 minutes */
31 #define DSGETDCNAME_CACHE_TTL   60*15
32
33 struct ip_service_name {
34         struct sockaddr_storage ss;
35         const char *hostname;
36 };
37
38 static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
39                                               uint32_t flags,
40                                               struct sockaddr_storage *ss,
41                                               struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
42                                               struct netr_DsRGetDCNameInfo **info);
43
44 /****************************************************************
45 ****************************************************************/
46
47 static void debug_dsdcinfo_flags(int lvl, uint32_t flags)
48 {
49         DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags));
50
51         if (flags & DS_FORCE_REDISCOVERY)
52                 DEBUGADD(lvl,("DS_FORCE_REDISCOVERY "));
53         if (flags & 0x000000002)
54                 DEBUGADD(lvl,("0x00000002 "));
55         if (flags & 0x000000004)
56                 DEBUGADD(lvl,("0x00000004 "));
57         if (flags & 0x000000008)
58                 DEBUGADD(lvl,("0x00000008 "));
59         if (flags & DS_DIRECTORY_SERVICE_REQUIRED)
60                 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_REQUIRED "));
61         if (flags & DS_DIRECTORY_SERVICE_PREFERRED)
62                 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_PREFERRED "));
63         if (flags & DS_GC_SERVER_REQUIRED)
64                 DEBUGADD(lvl,("DS_GC_SERVER_REQUIRED "));
65         if (flags & DS_PDC_REQUIRED)
66                 DEBUGADD(lvl,("DS_PDC_REQUIRED "));
67         if (flags & DS_BACKGROUND_ONLY)
68                 DEBUGADD(lvl,("DS_BACKGROUND_ONLY "));
69         if (flags & DS_IP_REQUIRED)
70                 DEBUGADD(lvl,("DS_IP_REQUIRED "));
71         if (flags & DS_KDC_REQUIRED)
72                 DEBUGADD(lvl,("DS_KDC_REQUIRED "));
73         if (flags & DS_TIMESERV_REQUIRED)
74                 DEBUGADD(lvl,("DS_TIMESERV_REQUIRED "));
75         if (flags & DS_WRITABLE_REQUIRED)
76                 DEBUGADD(lvl,("DS_WRITABLE_REQUIRED "));
77         if (flags & DS_GOOD_TIMESERV_PREFERRED)
78                 DEBUGADD(lvl,("DS_GOOD_TIMESERV_PREFERRED "));
79         if (flags & DS_AVOID_SELF)
80                 DEBUGADD(lvl,("DS_AVOID_SELF "));
81         if (flags & DS_ONLY_LDAP_NEEDED)
82                 DEBUGADD(lvl,("DS_ONLY_LDAP_NEEDED "));
83         if (flags & DS_IS_FLAT_NAME)
84                 DEBUGADD(lvl,("DS_IS_FLAT_NAME "));
85         if (flags & DS_IS_DNS_NAME)
86                 DEBUGADD(lvl,("DS_IS_DNS_NAME "));
87         if (flags & 0x00040000)
88                 DEBUGADD(lvl,("0x00040000 "));
89         if (flags & 0x00080000)
90                 DEBUGADD(lvl,("0x00080000 "));
91         if (flags & 0x00100000)
92                 DEBUGADD(lvl,("0x00100000 "));
93         if (flags & 0x00200000)
94                 DEBUGADD(lvl,("0x00200000 "));
95         if (flags & 0x00400000)
96                 DEBUGADD(lvl,("0x00400000 "));
97         if (flags & 0x00800000)
98                 DEBUGADD(lvl,("0x00800000 "));
99         if (flags & 0x01000000)
100                 DEBUGADD(lvl,("0x01000000 "));
101         if (flags & 0x02000000)
102                 DEBUGADD(lvl,("0x02000000 "));
103         if (flags & 0x04000000)
104                 DEBUGADD(lvl,("0x04000000 "));
105         if (flags & 0x08000000)
106                 DEBUGADD(lvl,("0x08000000 "));
107         if (flags & 0x10000000)
108                 DEBUGADD(lvl,("0x10000000 "));
109         if (flags & 0x20000000)
110                 DEBUGADD(lvl,("0x20000000 "));
111         if (flags & DS_RETURN_DNS_NAME)
112                 DEBUGADD(lvl,("DS_RETURN_DNS_NAME "));
113         if (flags & DS_RETURN_FLAT_NAME)
114                 DEBUGADD(lvl,("DS_RETURN_FLAT_NAME "));
115         if (flags)
116                 DEBUGADD(lvl,("\n"));
117 }
118
119 /****************************************************************
120 ****************************************************************/
121
122 static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
123 {
124         if (!domain) {
125                 return NULL;
126         }
127
128         return talloc_asprintf_strupper_m(mem_ctx, "DSGETDCNAME/DOMAIN/%s",
129                                           domain);
130 }
131
132 /****************************************************************
133 ****************************************************************/
134
135 static NTSTATUS dsgetdcname_cache_delete(TALLOC_CTX *mem_ctx,
136                                         const char *domain_name)
137 {
138         char *key;
139
140         key = dsgetdcname_cache_key(mem_ctx, domain_name);
141         if (!key) {
142                 return NT_STATUS_NO_MEMORY;
143         }
144
145         if (!gencache_del(key)) {
146                 return NT_STATUS_UNSUCCESSFUL;
147         }
148
149         return NT_STATUS_OK;
150 }
151
152 /****************************************************************
153 ****************************************************************/
154
155 static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
156                                         const char *domain_name,
157                                         DATA_BLOB blob)
158 {
159         time_t expire_time;
160         char *key;
161         bool ret = false;
162
163         key = dsgetdcname_cache_key(mem_ctx, domain_name);
164         if (!key) {
165                 return NT_STATUS_NO_MEMORY;
166         }
167
168         expire_time = time(NULL) + DSGETDCNAME_CACHE_TTL;
169
170         ret = gencache_set_data_blob(key, blob, expire_time);
171
172         return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
173 }
174
175 /****************************************************************
176 ****************************************************************/
177
178 static NTSTATUS store_cldap_reply(TALLOC_CTX *mem_ctx,
179                                   uint32_t flags,
180                                   struct sockaddr_storage *ss,
181                                   uint32_t nt_version,
182                                   struct NETLOGON_SAM_LOGON_RESPONSE_EX *r)
183 {
184         DATA_BLOB blob;
185         enum ndr_err_code ndr_err;
186         NTSTATUS status;
187         char addr[INET6_ADDRSTRLEN];
188
189        print_sockaddr(addr, sizeof(addr), ss);
190
191         /* FIXME */
192         r->sockaddr_size = 0x10; /* the w32 winsock addr size */
193         r->sockaddr.sockaddr_family = 2; /* AF_INET */
194         r->sockaddr.pdc_ip = talloc_strdup(mem_ctx, addr);
195
196         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
197                        (ndr_push_flags_fn_t)ndr_push_NETLOGON_SAM_LOGON_RESPONSE_EX);
198         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
199                 return ndr_map_error2ntstatus(ndr_err);
200         }
201
202         if (r->domain_name) {
203                 status = dsgetdcname_cache_store(mem_ctx, r->domain_name,
204                                                  blob);
205                 if (!NT_STATUS_IS_OK(status)) {
206                         goto done;
207                 }
208                 if (r->client_site) {
209                         sitename_store(r->domain_name, r->client_site);
210                 }
211         }
212         if (r->dns_domain) {
213                 status = dsgetdcname_cache_store(mem_ctx, r->dns_domain, blob);
214                 if (!NT_STATUS_IS_OK(status)) {
215                         goto done;
216                 }
217                 if (r->client_site) {
218                         sitename_store(r->dns_domain, r->client_site);
219                 }
220         }
221
222         status = NT_STATUS_OK;
223
224  done:
225         data_blob_free(&blob);
226
227         return status;
228 }
229
230 /****************************************************************
231 ****************************************************************/
232
233 static uint32_t get_cldap_reply_server_flags(struct netlogon_samlogon_response *r,
234                                              uint32_t nt_version)
235 {
236         switch (nt_version & 0x0000001f) {
237                 case 0:
238                 case 1:
239                 case 16:
240                 case 17:
241                         return 0;
242                 case 2:
243                 case 3:
244                 case 18:
245                 case 19:
246                         return r->data.nt5.server_type;
247                 case 4:
248                 case 5:
249                 case 6:
250                 case 7:
251                         return r->data.nt5_ex.server_type;
252                 case 8:
253                 case 9:
254                 case 10:
255                 case 11:
256                 case 12:
257                 case 13:
258                 case 14:
259                 case 15:
260                         return r->data.nt5_ex.server_type;
261                 case 20:
262                 case 21:
263                 case 22:
264                 case 23:
265                 case 24:
266                 case 25:
267                 case 26:
268                 case 27:
269                 case 28:
270                         return r->data.nt5_ex.server_type;
271                 case 29:
272                 case 30:
273                 case 31:
274                         return r->data.nt5_ex.server_type;
275                 default:
276                         return 0;
277         }
278 }
279
280 /****************************************************************
281 ****************************************************************/
282
283 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
284                                         const char *domain_name,
285                                         const struct GUID *domain_guid,
286                                         uint32_t flags,
287                                         struct netr_DsRGetDCNameInfo **info_p)
288 {
289         char *key;
290         DATA_BLOB blob;
291         enum ndr_err_code ndr_err;
292         struct netr_DsRGetDCNameInfo *info;
293         struct NETLOGON_SAM_LOGON_RESPONSE_EX r;
294         NTSTATUS status;
295
296         key = dsgetdcname_cache_key(mem_ctx, domain_name);
297         if (!key) {
298                 return NT_STATUS_NO_MEMORY;
299         }
300
301         if (!gencache_get_data_blob(key, NULL, &blob, NULL, NULL)) {
302                 return NT_STATUS_NOT_FOUND;
303         }
304
305         info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
306         if (!info) {
307                 data_blob_free(&blob);
308                 return NT_STATUS_NO_MEMORY;
309         }
310
311         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
312                       (ndr_pull_flags_fn_t)ndr_pull_NETLOGON_SAM_LOGON_RESPONSE_EX);
313
314         data_blob_free(&blob);
315         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
316                 dsgetdcname_cache_delete(mem_ctx, domain_name);
317                 return ndr_map_error2ntstatus(ndr_err);
318         }
319
320         status = make_dc_info_from_cldap_reply(mem_ctx, flags, NULL,
321                                                &r, &info);
322         if (!NT_STATUS_IS_OK(status)) {
323                 return status;
324         }
325
326         if (DEBUGLEVEL >= 10) {
327                 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
328         }
329
330         /* check flags */
331         if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
332                 DEBUG(10,("invalid flags\n"));
333                 return NT_STATUS_INVALID_PARAMETER;
334         }
335
336         if ((flags & DS_IP_REQUIRED) &&
337             (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
338                 return NT_STATUS_INVALID_PARAMETER_MIX;
339         }
340
341         *info_p = info;
342
343         return NT_STATUS_OK;
344 }
345
346 /****************************************************************
347 ****************************************************************/
348
349 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
350                                    struct messaging_context *msg_ctx,
351                                    const char *domain_name,
352                                    const struct GUID *domain_guid,
353                                    uint32_t flags,
354                                    const char *site_name,
355                                    struct netr_DsRGetDCNameInfo **info)
356 {
357         NTSTATUS status;
358
359         status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
360                                          flags, info);
361         if (!NT_STATUS_IS_OK(status)
362             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
363                 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
364                         nt_errstr(status)));
365                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
366         }
367
368         if (flags & DS_BACKGROUND_ONLY) {
369                 return status;
370         }
371
372         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
373                 struct netr_DsRGetDCNameInfo *dc_info;
374
375                 status = dsgetdcname(mem_ctx, msg_ctx, domain_name,
376                                      domain_guid, site_name,
377                                      flags | DS_FORCE_REDISCOVERY,
378                                      &dc_info);
379
380                 if (!NT_STATUS_IS_OK(status)) {
381                         return status;
382                 }
383
384                 *info = dc_info;
385         }
386
387         return status;
388 }
389
390 /****************************************************************
391 ****************************************************************/
392
393 static bool check_allowed_required_flags(uint32_t flags,
394                                          const char *site_name)
395 {
396         uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
397         uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
398         uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
399
400         /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
401          * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
402
403         debug_dsdcinfo_flags(10, flags);
404
405         if ((flags & DS_TRY_NEXTCLOSEST_SITE) && site_name) {
406                 return false;
407         }
408
409         if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
410                 return false;
411         }
412
413         if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
414                 return false;
415         }
416
417         if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
418                 return false;
419         }
420
421 #if 0
422         if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
423                 printf("gd: here5 \n");
424                 return false;
425         }
426 #endif
427         return true;
428 }
429
430 /****************************************************************
431 ****************************************************************/
432
433 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
434                                     const char *domain_name,
435                                     uint32_t flags,
436                                     struct ip_service_name **returned_dclist,
437                                     int *returned_count)
438 {
439         NTSTATUS status;
440         enum nbt_name_type name_type = NBT_NAME_LOGON;
441         struct ip_service *iplist;
442         int i;
443         struct ip_service_name *dclist = NULL;
444         int count;
445         static const char *resolve_order[] = { "lmhosts", "wins", "bcast", NULL };
446
447         *returned_dclist = NULL;
448         *returned_count = 0;
449
450         if (flags & DS_PDC_REQUIRED) {
451                 name_type = NBT_NAME_PDC;
452         }
453
454         status = internal_resolve_name(domain_name, name_type, NULL,
455                                        &iplist, &count,
456                                        resolve_order);
457         if (!NT_STATUS_IS_OK(status)) {
458                 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
459                 return status;
460         }
461
462         dclist = talloc_zero_array(mem_ctx, struct ip_service_name, count);
463         if (!dclist) {
464                 SAFE_FREE(iplist);
465                 return NT_STATUS_NO_MEMORY;
466         }
467
468         for (i=0; i<count; i++) {
469
470                 char addr[INET6_ADDRSTRLEN];
471                 struct ip_service_name *r = &dclist[i];
472
473                 print_sockaddr(addr, sizeof(addr),
474                                &iplist[i].ss);
475
476                 r->ss   = iplist[i].ss;
477                 r->hostname = talloc_strdup(mem_ctx, addr);
478                 if (!r->hostname) {
479                         SAFE_FREE(iplist);
480                         return NT_STATUS_NO_MEMORY;
481                 }
482
483         }
484
485         *returned_dclist = dclist;
486         *returned_count = count;
487         SAFE_FREE(iplist);
488
489         return NT_STATUS_OK;
490 }
491
492 /****************************************************************
493 ****************************************************************/
494
495 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
496                                 const char *domain_name,
497                                 const struct GUID *domain_guid,
498                                 uint32_t flags,
499                                 const char *site_name,
500                                 struct ip_service_name **returned_dclist,
501                                 int *return_count)
502 {
503         int i, j;
504         NTSTATUS status;
505         struct dns_rr_srv *dcs = NULL;
506         int numdcs = 0;
507         int numaddrs = 0;
508         struct ip_service_name *dclist = NULL;
509         int count = 0;
510
511         if (flags & DS_PDC_REQUIRED) {
512                 status = ads_dns_query_pdc(mem_ctx,
513                                            domain_name,
514                                            &dcs,
515                                            &numdcs);
516         } else if (flags & DS_GC_SERVER_REQUIRED) {
517                 status = ads_dns_query_gcs(mem_ctx,
518                                            domain_name,
519                                            site_name,
520                                            &dcs,
521                                            &numdcs);
522         } else if (flags & DS_KDC_REQUIRED) {
523                 status = ads_dns_query_kdcs(mem_ctx,
524                                             domain_name,
525                                             site_name,
526                                             &dcs,
527                                             &numdcs);
528         } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
529                 status = ads_dns_query_dcs(mem_ctx,
530                                            domain_name,
531                                            site_name,
532                                            &dcs,
533                                            &numdcs);
534         } else if (domain_guid) {
535                 struct GUID_txt_buf buf;
536                 GUID_buf_string(domain_guid, &buf);
537
538                 status = ads_dns_query_dcs_guid(mem_ctx,
539                                                 domain_name,
540                                                 buf.buf,
541                                                 &dcs,
542                                                 &numdcs);
543         } else {
544                 status = ads_dns_query_dcs(mem_ctx,
545                                            domain_name,
546                                            site_name,
547                                            &dcs,
548                                            &numdcs);
549         }
550
551         if (!NT_STATUS_IS_OK(status)) {
552                 return status;
553         }
554
555         if (numdcs == 0) {
556                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
557         }
558
559         for (i=0;i<numdcs;i++) {
560                 numaddrs += MAX(dcs[i].num_ips,1);
561         }
562
563         dclist = talloc_zero_array(mem_ctx,
564                                    struct ip_service_name,
565                                    numaddrs);
566         if (!dclist) {
567                 return NT_STATUS_NO_MEMORY;
568         }
569
570         /* now unroll the list of IP addresses */
571
572         *return_count = 0;
573         i = 0;
574         j = 0;
575
576         while ((i < numdcs) && (count < numaddrs)) {
577
578                 struct ip_service_name *r = &dclist[count];
579
580                 r->hostname = dcs[i].hostname;
581
582                 /* If we don't have an IP list for a name, lookup it up */
583
584                 if (!dcs[i].ss_s) {
585                         interpret_string_addr_prefer_ipv4(&r->ss,
586                                                 dcs[i].hostname, 0);
587                         i++;
588                         j = 0;
589                 } else {
590                         /* use the IP addresses from the SRV response */
591
592                         if (j >= dcs[i].num_ips) {
593                                 i++;
594                                 j = 0;
595                                 continue;
596                         }
597
598                         r->ss = dcs[i].ss_s[j];
599                         j++;
600                 }
601
602                 /* make sure it is a valid IP.  I considered checking the
603                  * negative connection cache, but this is the wrong place for
604                  * it.  Maybe only as a hack. After think about it, if all of
605                  * the IP addresses returned from DNS are dead, what hope does a
606                  * netbios name lookup have?  The standard reason for falling
607                  * back to netbios lookups is that our DNS server doesn't know
608                  * anything about the DC's   -- jerry */
609
610                 if (!is_zero_addr(&r->ss)) {
611                         count++;
612                         continue;
613                 }
614         }
615
616         *returned_dclist = dclist;
617         *return_count = count;
618
619         if (count > 0) {
620                 return NT_STATUS_OK;
621         }
622
623         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
624 }
625
626 /****************************************************************
627 ****************************************************************/
628
629 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
630                                             const char *dc_unc,
631                                             const char *dc_address,
632                                             uint32_t dc_address_type,
633                                             const struct GUID *domain_guid,
634                                             const char *domain_name,
635                                             const char *forest_name,
636                                             uint32_t flags,
637                                             const char *dc_site_name,
638                                             const char *client_site_name,
639                                             struct netr_DsRGetDCNameInfo **info_out)
640 {
641         struct netr_DsRGetDCNameInfo *info;
642
643         info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
644         NT_STATUS_HAVE_NO_MEMORY(info);
645
646         if (dc_unc) {
647                 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
648                 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
649         }
650
651         if (dc_address) {
652                 if (!(dc_address[0] == '\\' && dc_address[1] == '\\')) {
653                         info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
654                                                            dc_address);
655                 } else {
656                         info->dc_address = talloc_strdup(mem_ctx, dc_address);
657                 }
658                 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
659         }
660
661         info->dc_address_type = dc_address_type;
662
663         if (domain_guid) {
664                 info->domain_guid = *domain_guid;
665         }
666
667         if (domain_name) {
668                 info->domain_name = talloc_strdup(mem_ctx, domain_name);
669                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
670         }
671
672         if (forest_name && *forest_name) {
673                 info->forest_name = talloc_strdup(mem_ctx, forest_name);
674                 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
675                 flags |= DS_DNS_FOREST_ROOT;
676         }
677
678         info->dc_flags = flags;
679
680         if (dc_site_name) {
681                 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
682                 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
683         }
684
685         if (client_site_name) {
686                 info->client_site_name = talloc_strdup(mem_ctx,
687                                                        client_site_name);
688                 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
689         }
690
691         *info_out = info;
692
693         return NT_STATUS_OK;
694 }
695
696 /****************************************************************
697 ****************************************************************/
698
699 static void map_dc_and_domain_names(uint32_t flags,
700                                     const char *dc_name,
701                                     const char *domain_name,
702                                     const char *dns_dc_name,
703                                     const char *dns_domain_name,
704                                     uint32_t *dc_flags,
705                                     const char **hostname_p,
706                                     const char **domain_p)
707 {
708         switch (flags & 0xf0000000) {
709                 case DS_RETURN_FLAT_NAME:
710                         if (dc_name && domain_name &&
711                             *dc_name && *domain_name) {
712                                 *hostname_p = dc_name;
713                                 *domain_p = domain_name;
714                                 break;
715                         }
716                 case DS_RETURN_DNS_NAME:
717                 default:
718                         if (dns_dc_name && dns_domain_name &&
719                             *dns_dc_name && *dns_domain_name) {
720                                 *hostname_p = dns_dc_name;
721                                 *domain_p = dns_domain_name;
722                                 *dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
723                                 break;
724                         }
725                         if (dc_name && domain_name &&
726                             *dc_name && *domain_name) {
727                                 *hostname_p = dc_name;
728                                 *domain_p = domain_name;
729                                 break;
730                         }
731         }
732 }
733
734 /****************************************************************
735 ****************************************************************/
736
737 static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
738                                               uint32_t flags,
739                                               struct sockaddr_storage *ss,
740                                               struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
741                                               struct netr_DsRGetDCNameInfo **info)
742 {
743         const char *dc_hostname = NULL;
744         const char *dc_domain_name = NULL;
745         const char *dc_address = NULL;
746         const char *dc_forest = NULL;
747         uint32_t dc_address_type = 0;
748         uint32_t dc_flags = 0;
749         struct GUID *dc_domain_guid = NULL;
750         const char *dc_server_site = NULL;
751         const char *dc_client_site = NULL;
752
753         char addr[INET6_ADDRSTRLEN];
754
755         if (ss) {
756                 print_sockaddr(addr, sizeof(addr), ss);
757                 dc_address = addr;
758                 dc_address_type = DS_ADDRESS_TYPE_INET;
759         } else {
760                 if (r->sockaddr.pdc_ip) {
761                         dc_address      = r->sockaddr.pdc_ip;
762                         dc_address_type = DS_ADDRESS_TYPE_INET;
763                 } else {
764                         dc_address      = r->pdc_name;
765                         dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
766                 }
767         }
768
769         map_dc_and_domain_names(flags,
770                                 r->pdc_name,
771                                 r->domain_name,
772                                 r->pdc_dns_name,
773                                 r->dns_domain,
774                                 &dc_flags,
775                                 &dc_hostname,
776                                 &dc_domain_name);
777
778         dc_flags        |= r->server_type;
779         dc_forest       = r->forest;
780         dc_domain_guid  = &r->domain_uuid;
781         dc_server_site  = r->server_site;
782         dc_client_site  = r->client_site;
783
784         return make_domain_controller_info(mem_ctx,
785                                            dc_hostname,
786                                            dc_address,
787                                            dc_address_type,
788                                            dc_domain_guid,
789                                            dc_domain_name,
790                                            dc_forest,
791                                            dc_flags,
792                                            dc_server_site,
793                                            dc_client_site,
794                                            info);
795 }
796
797 /****************************************************************
798 ****************************************************************/
799
800 static uint32_t map_ds_flags_to_nt_version(uint32_t flags)
801 {
802         uint32_t nt_version = 0;
803
804         if (flags & DS_PDC_REQUIRED) {
805                 nt_version |= NETLOGON_NT_VERSION_PDC;
806         }
807
808         if (flags & DS_GC_SERVER_REQUIRED) {
809                 nt_version |= NETLOGON_NT_VERSION_GC;
810         }
811
812         if (flags & DS_TRY_NEXTCLOSEST_SITE) {
813                 nt_version |= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE;
814         }
815
816         if (flags & DS_IP_REQUIRED) {
817                 nt_version |= NETLOGON_NT_VERSION_IP;
818         }
819
820         return nt_version;
821 }
822
823 /****************************************************************
824 ****************************************************************/
825
826 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
827                                const char *domain_name,
828                                uint32_t flags,
829                                struct ip_service_name *dclist,
830                                int num_dcs,
831                                struct netr_DsRGetDCNameInfo **info)
832 {
833         int i = 0;
834         bool valid_dc = false;
835         struct netlogon_samlogon_response *r = NULL;
836         uint32_t nt_version = NETLOGON_NT_VERSION_5 |
837                               NETLOGON_NT_VERSION_5EX;
838         uint32_t ret_flags = 0;
839         NTSTATUS status;
840
841         nt_version |= map_ds_flags_to_nt_version(flags);
842
843         for (i=0; i<num_dcs; i++) {
844
845                 char addr[INET6_ADDRSTRLEN];
846                 print_sockaddr(addr, sizeof(addr), &dclist[i].ss);
847
848                 DEBUG(10,("LDAP ping to %s (%s)\n", dclist[i].hostname, addr));
849
850                 if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
851                                         domain_name,
852                                         nt_version,
853                                         &r))
854                 {
855                         nt_version = r->ntver;
856                         ret_flags = get_cldap_reply_server_flags(r, nt_version);
857
858                         if (check_cldap_reply_required_flags(ret_flags, flags)) {
859                                 valid_dc = true;
860                                 break;
861                         }
862                 }
863
864                 continue;
865         }
866
867         if (!valid_dc) {
868                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
869         }
870
871         status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
872                                                &r->data.nt5_ex, info);
873         if (NT_STATUS_IS_OK(status)) {
874                 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
875                                          nt_version, &r->data.nt5_ex);
876         }
877
878         return status;
879 }
880
881 /****************************************************************
882 ****************************************************************/
883
884 /****************************************************************
885 ****************************************************************/
886
887 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
888                                    struct messaging_context *msg_ctx,
889                                    const char *domain_name,
890                                    uint32_t flags,
891                                    struct ip_service_name *dclist,
892                                    int num_dcs,
893                                    struct netr_DsRGetDCNameInfo **info)
894 {
895         struct sockaddr_storage ss;
896         struct ip_service ip_list;
897         enum nbt_name_type name_type = NBT_NAME_LOGON;
898         NTSTATUS status;
899         int i;
900         const char *dc_name = NULL;
901         fstring tmp_dc_name;
902         struct netlogon_samlogon_response *r = NULL;
903         bool store_cache = false;
904         uint32_t nt_version = NETLOGON_NT_VERSION_1 |
905                               NETLOGON_NT_VERSION_5 |
906                               NETLOGON_NT_VERSION_5EX_WITH_IP;
907
908         if (msg_ctx == NULL) {
909                 return NT_STATUS_INVALID_PARAMETER;
910         }
911
912         if (flags & DS_PDC_REQUIRED) {
913                 name_type = NBT_NAME_PDC;
914         }
915
916         nt_version |= map_ds_flags_to_nt_version(flags);
917
918         DEBUG(10,("process_dc_netbios\n"));
919
920         for (i=0; i<num_dcs; i++) {
921                 uint16_t val;
922
923                 generate_random_buffer((uint8_t *)&val, 2);
924
925                 ip_list.ss = dclist[i].ss;
926                 ip_list.port = 0;
927
928                 if (!interpret_string_addr_prefer_ipv4(&ss, dclist[i].hostname, AI_NUMERICHOST)) {
929                         return NT_STATUS_UNSUCCESSFUL;
930                 }
931
932                 status = nbt_getdc(msg_ctx, 10, &dclist[i].ss, domain_name,
933                                    NULL, nt_version,
934                                    mem_ctx, &nt_version, &dc_name, &r);
935                 if (NT_STATUS_IS_OK(status)) {
936                         store_cache = true;
937                         namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
938                         goto make_reply;
939                 }
940
941                 if (name_status_find(domain_name,
942                                      name_type,
943                                      NBT_NAME_SERVER,
944                                      &dclist[i].ss,
945                                      tmp_dc_name))
946                 {
947                         struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
948
949                         r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
950                         NT_STATUS_HAVE_NO_MEMORY(r);
951
952                         ZERO_STRUCT(logon1);
953
954                         nt_version = NETLOGON_NT_VERSION_1;
955
956                         logon1.nt_version = nt_version;
957                         logon1.pdc_name = tmp_dc_name;
958                         logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
959                         NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
960
961                         r->data.nt4 = logon1;
962                         r->ntver = nt_version;
963
964                         map_netlogon_samlogon_response(r);
965
966                         namecache_store(tmp_dc_name, NBT_NAME_SERVER, 1, &ip_list);
967
968                         goto make_reply;
969                 }
970         }
971
972         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
973
974  make_reply:
975
976         status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
977                                                &r->data.nt5_ex, info);
978         if (NT_STATUS_IS_OK(status) && store_cache) {
979                 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
980                                          nt_version, &r->data.nt5_ex);
981         }
982
983         return status;
984 }
985
986 /****************************************************************
987 ****************************************************************/
988
989 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
990                                        struct messaging_context *msg_ctx,
991                                        const char *domain_name,
992                                        const struct GUID *domain_guid,
993                                        uint32_t flags,
994                                        const char *site_name,
995                                        struct netr_DsRGetDCNameInfo **info)
996 {
997         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
998         struct ip_service_name *dclist = NULL;
999         int num_dcs;
1000
1001         DEBUG(10,("dsgetdcname_rediscover\n"));
1002
1003         if (flags & DS_IS_FLAT_NAME) {
1004
1005                 if (lp_disable_netbios()) {
1006                         return NT_STATUS_NOT_SUPPORTED;
1007                 }
1008
1009                 status = discover_dc_netbios(mem_ctx, domain_name, flags,
1010                                              &dclist, &num_dcs);
1011                 NT_STATUS_NOT_OK_RETURN(status);
1012
1013                 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1014                                           dclist, num_dcs, info);
1015         }
1016
1017         if (flags & DS_IS_DNS_NAME) {
1018
1019                 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
1020                                          flags, site_name, &dclist, &num_dcs);
1021                 NT_STATUS_NOT_OK_RETURN(status);
1022
1023                 return process_dc_dns(mem_ctx, domain_name, flags,
1024                                       dclist, num_dcs, info);
1025         }
1026
1027         status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
1028                                  site_name, &dclist, &num_dcs);
1029
1030         if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
1031
1032                 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
1033                                         num_dcs, info);
1034                 if (NT_STATUS_IS_OK(status)) {
1035                         return status;
1036                 }
1037         }
1038
1039         if (lp_disable_netbios()) {
1040                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1041         }
1042
1043         status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
1044                                      &num_dcs);
1045         NT_STATUS_NOT_OK_RETURN(status);
1046
1047         return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags, dclist,
1048                                   num_dcs, info);
1049 }
1050
1051 static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
1052 {
1053         if (info->dc_flags & DS_SERVER_CLOSEST) {
1054                 return true;
1055         }
1056
1057         if (!info->client_site_name) {
1058                 return true;
1059         }
1060
1061         if (!info->dc_site_name) {
1062                 return false;
1063         }
1064
1065         if (strcmp(info->client_site_name, info->dc_site_name) == 0) {
1066                 return true;
1067         }
1068
1069         return false;
1070 }
1071
1072 /********************************************************************
1073  Internal dsgetdcname.
1074 ********************************************************************/
1075
1076 static NTSTATUS dsgetdcname_internal(TALLOC_CTX *mem_ctx,
1077                      struct messaging_context *msg_ctx,
1078                      const char *domain_name,
1079                      const struct GUID *domain_guid,
1080                      const char *site_name,
1081                      uint32_t flags,
1082                      struct netr_DsRGetDCNameInfo **info)
1083 {
1084         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1085         struct netr_DsRGetDCNameInfo *myinfo = NULL;
1086         bool first = true;
1087         struct netr_DsRGetDCNameInfo *first_info = NULL;
1088
1089         DEBUG(10,("dsgetdcname_internal: domain_name: %s, "
1090                   "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1091                   domain_name,
1092                   domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
1093                   site_name ? site_name : "(null)", flags));
1094
1095         *info = NULL;
1096
1097         if (!check_allowed_required_flags(flags, site_name)) {
1098                 DEBUG(0,("invalid flags specified\n"));
1099                 return NT_STATUS_INVALID_PARAMETER;
1100         }
1101
1102         if (flags & DS_FORCE_REDISCOVERY) {
1103                 goto rediscover;
1104         }
1105
1106         status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid,
1107                                     flags, site_name, &myinfo);
1108         if (NT_STATUS_IS_OK(status)) {
1109                 goto done;
1110         }
1111
1112         if (flags & DS_BACKGROUND_ONLY) {
1113                 goto done;
1114         }
1115
1116  rediscover:
1117         status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name,
1118                                         domain_guid, flags, site_name,
1119                                         &myinfo);
1120
1121  done:
1122         if (!NT_STATUS_IS_OK(status)) {
1123                 if (!first) {
1124                         *info = first_info;
1125                         return NT_STATUS_OK;
1126                 }
1127                 return status;
1128         }
1129
1130         if (!first) {
1131                 TALLOC_FREE(first_info);
1132         } else if (!is_closest_site(myinfo)) {
1133                 first = false;
1134                 first_info = myinfo;
1135                 /* TODO: may use the next_closest_site here */
1136                 site_name = myinfo->client_site_name;
1137                 goto rediscover;
1138         }
1139
1140         *info = myinfo;
1141         return NT_STATUS_OK;
1142 }
1143
1144 /********************************************************************
1145  dsgetdcname.
1146
1147  This will be the only public function here.
1148 ********************************************************************/
1149
1150 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
1151                      struct messaging_context *msg_ctx,
1152                      const char *domain_name,
1153                      const struct GUID *domain_guid,
1154                      const char *site_name,
1155                      uint32_t flags,
1156                      struct netr_DsRGetDCNameInfo **info)
1157 {
1158         NTSTATUS status;
1159         const char *query_site = NULL;
1160         char *ptr_to_free = NULL;
1161         bool retry_query_with_null = false;
1162
1163         if ((site_name == NULL) || (site_name[0] == '\0')) {
1164                 ptr_to_free = sitename_fetch(mem_ctx, domain_name);
1165                 if (ptr_to_free != NULL) {
1166                         retry_query_with_null = true;
1167                 }
1168                 query_site = ptr_to_free;
1169         } else {
1170                 query_site = site_name;
1171         }
1172
1173         status = dsgetdcname_internal(mem_ctx,
1174                                 msg_ctx,
1175                                 domain_name,
1176                                 domain_guid,
1177                                 query_site,
1178                                 flags,
1179                                 info);
1180
1181         TALLOC_FREE(ptr_to_free);
1182
1183         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1184                 return status;
1185         }
1186
1187         /* Should we try again with site_name == NULL ? */
1188         if (retry_query_with_null) {
1189                 status = dsgetdcname_internal(mem_ctx,
1190                                         msg_ctx,
1191                                         domain_name,
1192                                         domain_guid,
1193                                         NULL,
1194                                         flags,
1195                                         info);
1196         }
1197
1198         return status;
1199 }