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