dsgetdcname: Fix a signed/unsigned hickup
[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;
504         size_t j;
505         NTSTATUS status;
506         struct dns_rr_srv *dcs = NULL;
507         int numdcs = 0;
508         int numaddrs = 0;
509         struct ip_service_name *dclist = NULL;
510         int count = 0;
511
512         if (flags & DS_PDC_REQUIRED) {
513                 status = ads_dns_query_pdc(mem_ctx,
514                                            domain_name,
515                                            &dcs,
516                                            &numdcs);
517         } else if (flags & DS_GC_SERVER_REQUIRED) {
518                 status = ads_dns_query_gcs(mem_ctx,
519                                            domain_name,
520                                            site_name,
521                                            &dcs,
522                                            &numdcs);
523         } else if (flags & DS_KDC_REQUIRED) {
524                 status = ads_dns_query_kdcs(mem_ctx,
525                                             domain_name,
526                                             site_name,
527                                             &dcs,
528                                             &numdcs);
529         } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
530                 status = ads_dns_query_dcs(mem_ctx,
531                                            domain_name,
532                                            site_name,
533                                            &dcs,
534                                            &numdcs);
535         } else if (domain_guid) {
536                 struct GUID_txt_buf buf;
537                 GUID_buf_string(domain_guid, &buf);
538
539                 status = ads_dns_query_dcs_guid(mem_ctx,
540                                                 domain_name,
541                                                 buf.buf,
542                                                 &dcs,
543                                                 &numdcs);
544         } else {
545                 status = ads_dns_query_dcs(mem_ctx,
546                                            domain_name,
547                                            site_name,
548                                            &dcs,
549                                            &numdcs);
550         }
551
552         if (!NT_STATUS_IS_OK(status)) {
553                 return status;
554         }
555
556         if (numdcs == 0) {
557                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
558         }
559
560         for (i=0;i<numdcs;i++) {
561                 numaddrs += MAX(dcs[i].num_ips,1);
562         }
563
564         dclist = talloc_zero_array(mem_ctx,
565                                    struct ip_service_name,
566                                    numaddrs);
567         if (!dclist) {
568                 return NT_STATUS_NO_MEMORY;
569         }
570
571         /* now unroll the list of IP addresses */
572
573         *return_count = 0;
574         i = 0;
575         j = 0;
576
577         while ((i < numdcs) && (count < numaddrs)) {
578
579                 struct ip_service_name *r = &dclist[count];
580
581                 r->hostname = dcs[i].hostname;
582
583                 /* If we don't have an IP list for a name, lookup it up */
584
585                 if (!dcs[i].ss_s) {
586                         interpret_string_addr_prefer_ipv4(&r->ss,
587                                                 dcs[i].hostname, 0);
588                         i++;
589                         j = 0;
590                 } else {
591                         /* use the IP addresses from the SRV response */
592
593                         if (j >= dcs[i].num_ips) {
594                                 i++;
595                                 j = 0;
596                                 continue;
597                         }
598
599                         r->ss = dcs[i].ss_s[j];
600                         j++;
601                 }
602
603                 /* make sure it is a valid IP.  I considered checking the
604                  * negative connection cache, but this is the wrong place for
605                  * it.  Maybe only as a hack. After think about it, if all of
606                  * the IP addresses returned from DNS are dead, what hope does a
607                  * netbios name lookup have?  The standard reason for falling
608                  * back to netbios lookups is that our DNS server doesn't know
609                  * anything about the DC's   -- jerry */
610
611                 if (!is_zero_addr(&r->ss)) {
612                         count++;
613                         continue;
614                 }
615         }
616
617         *returned_dclist = dclist;
618         *return_count = count;
619
620         if (count > 0) {
621                 return NT_STATUS_OK;
622         }
623
624         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
625 }
626
627 /****************************************************************
628 ****************************************************************/
629
630 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
631                                             const char *dc_unc,
632                                             const char *dc_address,
633                                             uint32_t dc_address_type,
634                                             const struct GUID *domain_guid,
635                                             const char *domain_name,
636                                             const char *forest_name,
637                                             uint32_t flags,
638                                             const char *dc_site_name,
639                                             const char *client_site_name,
640                                             struct netr_DsRGetDCNameInfo **info_out)
641 {
642         struct netr_DsRGetDCNameInfo *info;
643
644         info = talloc_zero(mem_ctx, struct netr_DsRGetDCNameInfo);
645         NT_STATUS_HAVE_NO_MEMORY(info);
646
647         if (dc_unc) {
648                 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
649                 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
650         }
651
652         if (dc_address) {
653                 if (!(dc_address[0] == '\\' && dc_address[1] == '\\')) {
654                         info->dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
655                                                            dc_address);
656                 } else {
657                         info->dc_address = talloc_strdup(mem_ctx, dc_address);
658                 }
659                 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
660         }
661
662         info->dc_address_type = dc_address_type;
663
664         if (domain_guid) {
665                 info->domain_guid = *domain_guid;
666         }
667
668         if (domain_name) {
669                 info->domain_name = talloc_strdup(mem_ctx, domain_name);
670                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
671         }
672
673         if (forest_name && *forest_name) {
674                 info->forest_name = talloc_strdup(mem_ctx, forest_name);
675                 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
676                 flags |= DS_DNS_FOREST_ROOT;
677         }
678
679         info->dc_flags = flags;
680
681         if (dc_site_name) {
682                 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
683                 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
684         }
685
686         if (client_site_name) {
687                 info->client_site_name = talloc_strdup(mem_ctx,
688                                                        client_site_name);
689                 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
690         }
691
692         *info_out = info;
693
694         return NT_STATUS_OK;
695 }
696
697 /****************************************************************
698 ****************************************************************/
699
700 static void map_dc_and_domain_names(uint32_t flags,
701                                     const char *dc_name,
702                                     const char *domain_name,
703                                     const char *dns_dc_name,
704                                     const char *dns_domain_name,
705                                     uint32_t *dc_flags,
706                                     const char **hostname_p,
707                                     const char **domain_p)
708 {
709         switch (flags & 0xf0000000) {
710                 case DS_RETURN_FLAT_NAME:
711                         if (dc_name && domain_name &&
712                             *dc_name && *domain_name) {
713                                 *hostname_p = dc_name;
714                                 *domain_p = domain_name;
715                                 break;
716                         }
717                 case DS_RETURN_DNS_NAME:
718                 default:
719                         if (dns_dc_name && dns_domain_name &&
720                             *dns_dc_name && *dns_domain_name) {
721                                 *hostname_p = dns_dc_name;
722                                 *domain_p = dns_domain_name;
723                                 *dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
724                                 break;
725                         }
726                         if (dc_name && domain_name &&
727                             *dc_name && *domain_name) {
728                                 *hostname_p = dc_name;
729                                 *domain_p = domain_name;
730                                 break;
731                         }
732         }
733 }
734
735 /****************************************************************
736 ****************************************************************/
737
738 static NTSTATUS make_dc_info_from_cldap_reply(TALLOC_CTX *mem_ctx,
739                                               uint32_t flags,
740                                               struct sockaddr_storage *ss,
741                                               struct NETLOGON_SAM_LOGON_RESPONSE_EX *r,
742                                               struct netr_DsRGetDCNameInfo **info)
743 {
744         const char *dc_hostname = NULL;
745         const char *dc_domain_name = NULL;
746         const char *dc_address = NULL;
747         const char *dc_forest = NULL;
748         uint32_t dc_address_type = 0;
749         uint32_t dc_flags = 0;
750         struct GUID *dc_domain_guid = NULL;
751         const char *dc_server_site = NULL;
752         const char *dc_client_site = NULL;
753
754         char addr[INET6_ADDRSTRLEN];
755
756         if (ss) {
757                 print_sockaddr(addr, sizeof(addr), ss);
758                 dc_address = addr;
759                 dc_address_type = DS_ADDRESS_TYPE_INET;
760         } else {
761                 if (r->sockaddr.pdc_ip) {
762                         dc_address      = r->sockaddr.pdc_ip;
763                         dc_address_type = DS_ADDRESS_TYPE_INET;
764                 } else {
765                         dc_address      = r->pdc_name;
766                         dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
767                 }
768         }
769
770         map_dc_and_domain_names(flags,
771                                 r->pdc_name,
772                                 r->domain_name,
773                                 r->pdc_dns_name,
774                                 r->dns_domain,
775                                 &dc_flags,
776                                 &dc_hostname,
777                                 &dc_domain_name);
778
779         dc_flags        |= r->server_type;
780         dc_forest       = r->forest;
781         dc_domain_guid  = &r->domain_uuid;
782         dc_server_site  = r->server_site;
783         dc_client_site  = r->client_site;
784
785         return make_domain_controller_info(mem_ctx,
786                                            dc_hostname,
787                                            dc_address,
788                                            dc_address_type,
789                                            dc_domain_guid,
790                                            dc_domain_name,
791                                            dc_forest,
792                                            dc_flags,
793                                            dc_server_site,
794                                            dc_client_site,
795                                            info);
796 }
797
798 /****************************************************************
799 ****************************************************************/
800
801 static uint32_t map_ds_flags_to_nt_version(uint32_t flags)
802 {
803         uint32_t nt_version = 0;
804
805         if (flags & DS_PDC_REQUIRED) {
806                 nt_version |= NETLOGON_NT_VERSION_PDC;
807         }
808
809         if (flags & DS_GC_SERVER_REQUIRED) {
810                 nt_version |= NETLOGON_NT_VERSION_GC;
811         }
812
813         if (flags & DS_TRY_NEXTCLOSEST_SITE) {
814                 nt_version |= NETLOGON_NT_VERSION_WITH_CLOSEST_SITE;
815         }
816
817         if (flags & DS_IP_REQUIRED) {
818                 nt_version |= NETLOGON_NT_VERSION_IP;
819         }
820
821         return nt_version;
822 }
823
824 /****************************************************************
825 ****************************************************************/
826
827 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
828                                const char *domain_name,
829                                uint32_t flags,
830                                struct ip_service_name *dclist,
831                                int num_dcs,
832                                struct netr_DsRGetDCNameInfo **info)
833 {
834         int i = 0;
835         bool valid_dc = false;
836         struct netlogon_samlogon_response *r = NULL;
837         uint32_t nt_version = NETLOGON_NT_VERSION_5 |
838                               NETLOGON_NT_VERSION_5EX;
839         uint32_t ret_flags = 0;
840         NTSTATUS status;
841
842         nt_version |= map_ds_flags_to_nt_version(flags);
843
844         for (i=0; i<num_dcs; i++) {
845
846                 char addr[INET6_ADDRSTRLEN];
847                 print_sockaddr(addr, sizeof(addr), &dclist[i].ss);
848
849                 DEBUG(10,("LDAP ping to %s (%s)\n", dclist[i].hostname, addr));
850
851                 if (ads_cldap_netlogon(mem_ctx, &dclist[i].ss,
852                                         domain_name,
853                                         nt_version,
854                                         &r))
855                 {
856                         nt_version = r->ntver;
857                         ret_flags = get_cldap_reply_server_flags(r, nt_version);
858
859                         if (check_cldap_reply_required_flags(ret_flags, flags)) {
860                                 valid_dc = true;
861                                 break;
862                         }
863                 }
864
865                 continue;
866         }
867
868         if (!valid_dc) {
869                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
870         }
871
872         status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
873                                                &r->data.nt5_ex, info);
874         if (NT_STATUS_IS_OK(status)) {
875                 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
876                                          nt_version, &r->data.nt5_ex);
877         }
878
879         return status;
880 }
881
882 /****************************************************************
883 ****************************************************************/
884
885 /****************************************************************
886 ****************************************************************/
887
888 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
889                                    struct messaging_context *msg_ctx,
890                                    const char *domain_name,
891                                    uint32_t flags,
892                                    struct ip_service_name *dclist,
893                                    int num_dcs,
894                                    struct netr_DsRGetDCNameInfo **info)
895 {
896         struct sockaddr_storage ss;
897         struct ip_service ip_list;
898         enum nbt_name_type name_type = NBT_NAME_LOGON;
899         NTSTATUS status;
900         int i;
901         const char *dc_name = NULL;
902         fstring tmp_dc_name;
903         struct netlogon_samlogon_response *r = NULL;
904         bool store_cache = false;
905         uint32_t nt_version = NETLOGON_NT_VERSION_1 |
906                               NETLOGON_NT_VERSION_5 |
907                               NETLOGON_NT_VERSION_5EX_WITH_IP;
908
909         if (msg_ctx == NULL) {
910                 return NT_STATUS_INVALID_PARAMETER;
911         }
912
913         if (flags & DS_PDC_REQUIRED) {
914                 name_type = NBT_NAME_PDC;
915         }
916
917         nt_version |= map_ds_flags_to_nt_version(flags);
918
919         DEBUG(10,("process_dc_netbios\n"));
920
921         for (i=0; i<num_dcs; i++) {
922                 uint16_t val;
923
924                 generate_random_buffer((uint8_t *)&val, 2);
925
926                 ip_list.ss = dclist[i].ss;
927                 ip_list.port = 0;
928
929                 if (!interpret_string_addr_prefer_ipv4(&ss, dclist[i].hostname, AI_NUMERICHOST)) {
930                         return NT_STATUS_UNSUCCESSFUL;
931                 }
932
933                 status = nbt_getdc(msg_ctx, 10, &dclist[i].ss, domain_name,
934                                    NULL, nt_version,
935                                    mem_ctx, &nt_version, &dc_name, &r);
936                 if (NT_STATUS_IS_OK(status)) {
937                         store_cache = true;
938                         namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
939                         goto make_reply;
940                 }
941
942                 if (name_status_find(domain_name,
943                                      name_type,
944                                      NBT_NAME_SERVER,
945                                      &dclist[i].ss,
946                                      tmp_dc_name))
947                 {
948                         struct NETLOGON_SAM_LOGON_RESPONSE_NT40 logon1;
949
950                         r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
951                         NT_STATUS_HAVE_NO_MEMORY(r);
952
953                         ZERO_STRUCT(logon1);
954
955                         nt_version = NETLOGON_NT_VERSION_1;
956
957                         logon1.nt_version = nt_version;
958                         logon1.pdc_name = tmp_dc_name;
959                         logon1.domain_name = talloc_strdup_upper(mem_ctx, domain_name);
960                         NT_STATUS_HAVE_NO_MEMORY(logon1.domain_name);
961
962                         r->data.nt4 = logon1;
963                         r->ntver = nt_version;
964
965                         map_netlogon_samlogon_response(r);
966
967                         namecache_store(tmp_dc_name, NBT_NAME_SERVER, 1, &ip_list);
968
969                         goto make_reply;
970                 }
971         }
972
973         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
974
975  make_reply:
976
977         status = make_dc_info_from_cldap_reply(mem_ctx, flags, &dclist[i].ss,
978                                                &r->data.nt5_ex, info);
979         if (NT_STATUS_IS_OK(status) && store_cache) {
980                 return store_cldap_reply(mem_ctx, flags, &dclist[i].ss,
981                                          nt_version, &r->data.nt5_ex);
982         }
983
984         return status;
985 }
986
987 /****************************************************************
988 ****************************************************************/
989
990 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
991                                        struct messaging_context *msg_ctx,
992                                        const char *domain_name,
993                                        const struct GUID *domain_guid,
994                                        uint32_t flags,
995                                        const char *site_name,
996                                        struct netr_DsRGetDCNameInfo **info)
997 {
998         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
999         struct ip_service_name *dclist = NULL;
1000         int num_dcs;
1001
1002         DEBUG(10,("dsgetdcname_rediscover\n"));
1003
1004         if (flags & DS_IS_FLAT_NAME) {
1005
1006                 if (lp_disable_netbios()) {
1007                         return NT_STATUS_NOT_SUPPORTED;
1008                 }
1009
1010                 status = discover_dc_netbios(mem_ctx, domain_name, flags,
1011                                              &dclist, &num_dcs);
1012                 NT_STATUS_NOT_OK_RETURN(status);
1013
1014                 return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags,
1015                                           dclist, num_dcs, info);
1016         }
1017
1018         if (flags & DS_IS_DNS_NAME) {
1019
1020                 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
1021                                          flags, site_name, &dclist, &num_dcs);
1022                 NT_STATUS_NOT_OK_RETURN(status);
1023
1024                 return process_dc_dns(mem_ctx, domain_name, flags,
1025                                       dclist, num_dcs, info);
1026         }
1027
1028         status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
1029                                  site_name, &dclist, &num_dcs);
1030
1031         if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
1032
1033                 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
1034                                         num_dcs, info);
1035                 if (NT_STATUS_IS_OK(status)) {
1036                         return status;
1037                 }
1038         }
1039
1040         if (lp_disable_netbios()) {
1041                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1042         }
1043
1044         status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
1045                                      &num_dcs);
1046         NT_STATUS_NOT_OK_RETURN(status);
1047
1048         return process_dc_netbios(mem_ctx, msg_ctx, domain_name, flags, dclist,
1049                                   num_dcs, info);
1050 }
1051
1052 static bool is_closest_site(struct netr_DsRGetDCNameInfo *info)
1053 {
1054         if (info->dc_flags & DS_SERVER_CLOSEST) {
1055                 return true;
1056         }
1057
1058         if (!info->client_site_name) {
1059                 return true;
1060         }
1061
1062         if (!info->dc_site_name) {
1063                 return false;
1064         }
1065
1066         if (strcmp(info->client_site_name, info->dc_site_name) == 0) {
1067                 return true;
1068         }
1069
1070         return false;
1071 }
1072
1073 /********************************************************************
1074  Internal dsgetdcname.
1075 ********************************************************************/
1076
1077 static NTSTATUS dsgetdcname_internal(TALLOC_CTX *mem_ctx,
1078                      struct messaging_context *msg_ctx,
1079                      const char *domain_name,
1080                      const struct GUID *domain_guid,
1081                      const char *site_name,
1082                      uint32_t flags,
1083                      struct netr_DsRGetDCNameInfo **info)
1084 {
1085         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1086         struct netr_DsRGetDCNameInfo *myinfo = NULL;
1087         bool first = true;
1088         struct netr_DsRGetDCNameInfo *first_info = NULL;
1089
1090         DEBUG(10,("dsgetdcname_internal: domain_name: %s, "
1091                   "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
1092                   domain_name,
1093                   domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
1094                   site_name ? site_name : "(null)", flags));
1095
1096         *info = NULL;
1097
1098         if (!check_allowed_required_flags(flags, site_name)) {
1099                 DEBUG(0,("invalid flags specified\n"));
1100                 return NT_STATUS_INVALID_PARAMETER;
1101         }
1102
1103         if (flags & DS_FORCE_REDISCOVERY) {
1104                 goto rediscover;
1105         }
1106
1107         status = dsgetdcname_cached(mem_ctx, msg_ctx, domain_name, domain_guid,
1108                                     flags, site_name, &myinfo);
1109         if (NT_STATUS_IS_OK(status)) {
1110                 goto done;
1111         }
1112
1113         if (flags & DS_BACKGROUND_ONLY) {
1114                 goto done;
1115         }
1116
1117  rediscover:
1118         status = dsgetdcname_rediscover(mem_ctx, msg_ctx, domain_name,
1119                                         domain_guid, flags, site_name,
1120                                         &myinfo);
1121
1122  done:
1123         if (!NT_STATUS_IS_OK(status)) {
1124                 if (!first) {
1125                         *info = first_info;
1126                         return NT_STATUS_OK;
1127                 }
1128                 return status;
1129         }
1130
1131         if (!first) {
1132                 TALLOC_FREE(first_info);
1133         } else if (!is_closest_site(myinfo)) {
1134                 first = false;
1135                 first_info = myinfo;
1136                 /* TODO: may use the next_closest_site here */
1137                 site_name = myinfo->client_site_name;
1138                 goto rediscover;
1139         }
1140
1141         *info = myinfo;
1142         return NT_STATUS_OK;
1143 }
1144
1145 /********************************************************************
1146  dsgetdcname.
1147
1148  This will be the only public function here.
1149 ********************************************************************/
1150
1151 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
1152                      struct messaging_context *msg_ctx,
1153                      const char *domain_name,
1154                      const struct GUID *domain_guid,
1155                      const char *site_name,
1156                      uint32_t flags,
1157                      struct netr_DsRGetDCNameInfo **info)
1158 {
1159         NTSTATUS status;
1160         const char *query_site = NULL;
1161         char *ptr_to_free = NULL;
1162         bool retry_query_with_null = false;
1163
1164         if ((site_name == NULL) || (site_name[0] == '\0')) {
1165                 ptr_to_free = sitename_fetch(mem_ctx, domain_name);
1166                 if (ptr_to_free != NULL) {
1167                         retry_query_with_null = true;
1168                 }
1169                 query_site = ptr_to_free;
1170         } else {
1171                 query_site = site_name;
1172         }
1173
1174         status = dsgetdcname_internal(mem_ctx,
1175                                 msg_ctx,
1176                                 domain_name,
1177                                 domain_guid,
1178                                 query_site,
1179                                 flags,
1180                                 info);
1181
1182         TALLOC_FREE(ptr_to_free);
1183
1184         if (!NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1185                 return status;
1186         }
1187
1188         /* Should we try again with site_name == NULL ? */
1189         if (retry_query_with_null) {
1190                 status = dsgetdcname_internal(mem_ctx,
1191                                         msg_ctx,
1192                                         domain_name,
1193                                         domain_guid,
1194                                         NULL,
1195                                         flags,
1196                                         info);
1197         }
1198
1199         return status;
1200 }