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