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