dsgetdcname: remove invalid assumptions when using DNS for the DC query.
[nivanova/samba-autobuild/.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
25 #define DSGETDCNAME_FMT "DSGETDCNAME/DOMAIN/%s"
26 /* 15 minutes */
27 #define DSGETDCNAME_CACHE_TTL   60*15
28
29 struct ip_service_name {
30         struct sockaddr_storage ss;
31         unsigned port;
32         const char *hostname;
33 };
34
35 /****************************************************************
36 ****************************************************************/
37
38 void debug_dsdcinfo_flags(int lvl, uint32_t flags)
39 {
40         DEBUG(lvl,("debug_dsdcinfo_flags: 0x%08x\n\t", flags));
41
42         if (flags & DS_FORCE_REDISCOVERY)
43                 DEBUGADD(lvl,("DS_FORCE_REDISCOVERY "));
44         if (flags & 0x000000002)
45                 DEBUGADD(lvl,("0x00000002 "));
46         if (flags & 0x000000004)
47                 DEBUGADD(lvl,("0x00000004 "));
48         if (flags & 0x000000008)
49                 DEBUGADD(lvl,("0x00000008 "));
50         if (flags & DS_DIRECTORY_SERVICE_REQUIRED)
51                 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_REQUIRED "));
52         if (flags & DS_DIRECTORY_SERVICE_PREFERRED)
53                 DEBUGADD(lvl,("DS_DIRECTORY_SERVICE_PREFERRED "));
54         if (flags & DS_GC_SERVER_REQUIRED)
55                 DEBUGADD(lvl,("DS_GC_SERVER_REQUIRED "));
56         if (flags & DS_PDC_REQUIRED)
57                 DEBUGADD(lvl,("DS_PDC_REQUIRED "));
58         if (flags & DS_BACKGROUND_ONLY)
59                 DEBUGADD(lvl,("DS_BACKGROUND_ONLY "));
60         if (flags & DS_IP_REQUIRED)
61                 DEBUGADD(lvl,("DS_IP_REQUIRED "));
62         if (flags & DS_KDC_REQUIRED)
63                 DEBUGADD(lvl,("DS_KDC_REQUIRED "));
64         if (flags & DS_TIMESERV_REQUIRED)
65                 DEBUGADD(lvl,("DS_TIMESERV_REQUIRED "));
66         if (flags & DS_WRITABLE_REQUIRED)
67                 DEBUGADD(lvl,("DS_WRITABLE_REQUIRED "));
68         if (flags & DS_GOOD_TIMESERV_PREFERRED)
69                 DEBUGADD(lvl,("DS_GOOD_TIMESERV_PREFERRED "));
70         if (flags & DS_AVOID_SELF)
71                 DEBUGADD(lvl,("DS_AVOID_SELF "));
72         if (flags & DS_ONLY_LDAP_NEEDED)
73                 DEBUGADD(lvl,("DS_ONLY_LDAP_NEEDED "));
74         if (flags & DS_IS_FLAT_NAME)
75                 DEBUGADD(lvl,("DS_IS_FLAT_NAME "));
76         if (flags & DS_IS_DNS_NAME)
77                 DEBUGADD(lvl,("DS_IS_DNS_NAME "));
78         if (flags & 0x00040000)
79                 DEBUGADD(lvl,("0x00040000 "));
80         if (flags & 0x00080000)
81                 DEBUGADD(lvl,("0x00080000 "));
82         if (flags & 0x00100000)
83                 DEBUGADD(lvl,("0x00100000 "));
84         if (flags & 0x00200000)
85                 DEBUGADD(lvl,("0x00200000 "));
86         if (flags & 0x00400000)
87                 DEBUGADD(lvl,("0x00400000 "));
88         if (flags & 0x00800000)
89                 DEBUGADD(lvl,("0x00800000 "));
90         if (flags & 0x01000000)
91                 DEBUGADD(lvl,("0x01000000 "));
92         if (flags & 0x02000000)
93                 DEBUGADD(lvl,("0x02000000 "));
94         if (flags & 0x04000000)
95                 DEBUGADD(lvl,("0x04000000 "));
96         if (flags & 0x08000000)
97                 DEBUGADD(lvl,("0x08000000 "));
98         if (flags & 0x10000000)
99                 DEBUGADD(lvl,("0x10000000 "));
100         if (flags & 0x20000000)
101                 DEBUGADD(lvl,("0x20000000 "));
102         if (flags & DS_RETURN_DNS_NAME)
103                 DEBUGADD(lvl,("DS_RETURN_DNS_NAME "));
104         if (flags & DS_RETURN_FLAT_NAME)
105                 DEBUGADD(lvl,("DS_RETURN_FLAT_NAME "));
106         if (flags)
107                 DEBUGADD(lvl,("\n"));
108 }
109
110 /****************************************************************
111 ****************************************************************/
112
113 static char *dsgetdcname_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
114 {
115         if (!mem_ctx || !domain) {
116                 return NULL;
117         }
118
119         return talloc_asprintf_strupper_m(mem_ctx, DSGETDCNAME_FMT, domain);
120 }
121
122 /****************************************************************
123 ****************************************************************/
124
125 static NTSTATUS dsgetdcname_cache_delete(TALLOC_CTX *mem_ctx,
126                                         const char *domain_name)
127 {
128         char *key;
129
130         if (!gencache_init()) {
131                 return NT_STATUS_INTERNAL_DB_ERROR;
132         }
133
134         key = dsgetdcname_cache_key(mem_ctx, domain_name);
135         if (!key) {
136                 return NT_STATUS_NO_MEMORY;
137         }
138
139         if (!gencache_del(key)) {
140                 return NT_STATUS_UNSUCCESSFUL;
141         }
142
143         return NT_STATUS_OK;
144 }
145
146 /****************************************************************
147 ****************************************************************/
148
149 static NTSTATUS dsgetdcname_cache_store(TALLOC_CTX *mem_ctx,
150                                         const char *domain_name,
151                                         struct netr_DsRGetDCNameInfo *info)
152 {
153         time_t expire_time;
154         char *key;
155         bool ret = false;
156         DATA_BLOB blob;
157         enum ndr_err_code ndr_err;
158
159         if (!gencache_init()) {
160                 return NT_STATUS_INTERNAL_DB_ERROR;
161         }
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         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info,
171                        (ndr_push_flags_fn_t)ndr_push_netr_DsRGetDCNameInfo);
172         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
173                 return ndr_map_error2ntstatus(ndr_err);
174         }
175
176         if (gencache_lock_entry(key) != 0) {
177                 data_blob_free(&blob);
178                 return NT_STATUS_LOCK_NOT_GRANTED;
179         }
180
181         ret = gencache_set_data_blob(key, &blob, expire_time);
182         data_blob_free(&blob);
183
184         gencache_unlock_entry(key);
185
186         return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
187 }
188
189 /****************************************************************
190 ****************************************************************/
191
192 static NTSTATUS dsgetdcname_cache_refresh(TALLOC_CTX *mem_ctx,
193                                           const char *domain_name,
194                                           struct GUID *domain_guid,
195                                           uint32_t flags,
196                                           const char *site_name,
197                                           struct netr_DsRGetDCNameInfo *info)
198 {
199         struct nbt_cldap_netlogon_5 r;
200
201         /* check if matching entry is older then 15 minutes, if yes, send
202          * CLDAP/MAILSLOT ping again and store the cached data */
203
204         ZERO_STRUCT(r);
205
206         if (ads_cldap_netlogon(mem_ctx, info->dc_unc,
207                                info->domain_name, &r)) {
208
209                 dsgetdcname_cache_delete(mem_ctx, domain_name);
210
211                 return dsgetdcname_cache_store(mem_ctx,
212                                                info->domain_name,
213                                                info);
214         }
215
216         return NT_STATUS_INVALID_NETWORK_RESPONSE;
217 }
218
219 /****************************************************************
220 ****************************************************************/
221
222 #define RETURN_ON_FALSE(x) if (!x) return false;
223
224 static bool check_cldap_reply_required_flags(uint32_t ret_flags,
225                                              uint32_t req_flags)
226 {
227         if (req_flags & DS_PDC_REQUIRED)
228                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_PDC);
229
230         if (req_flags & DS_GC_SERVER_REQUIRED)
231                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_GC);
232
233         if (req_flags & DS_ONLY_LDAP_NEEDED)
234                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_LDAP);
235
236         if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) ||
237             (req_flags & DS_DIRECTORY_SERVICE_PREFERRED))
238                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_DS);
239
240         if (req_flags & DS_KDC_REQUIRED)
241                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_KDC);
242
243         if (req_flags & DS_TIMESERV_REQUIRED)
244                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_TIMESERV);
245
246         if (req_flags & DS_WRITABLE_REQUIRED)
247                 RETURN_ON_FALSE(ret_flags & NBT_SERVER_WRITABLE);
248
249         return true;
250 }
251
252 /****************************************************************
253 ****************************************************************/
254
255 static NTSTATUS dsgetdcname_cache_fetch(TALLOC_CTX *mem_ctx,
256                                         const char *domain_name,
257                                         struct GUID *domain_guid,
258                                         uint32_t flags,
259                                         const char *site_name,
260                                         struct netr_DsRGetDCNameInfo **info_p,
261                                         bool *expired)
262 {
263         char *key;
264         DATA_BLOB blob;
265         enum ndr_err_code ndr_err;
266         struct netr_DsRGetDCNameInfo *info;
267
268         if (!gencache_init()) {
269                 return NT_STATUS_INTERNAL_DB_ERROR;
270         }
271
272         key = dsgetdcname_cache_key(mem_ctx, domain_name);
273         if (!key) {
274                 return NT_STATUS_NO_MEMORY;
275         }
276
277         if (!gencache_get_data_blob(key, &blob, expired)) {
278                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
279         }
280
281         info = TALLOC_ZERO_P(mem_ctx, struct netr_DsRGetDCNameInfo);
282         if (!info) {
283                 return NT_STATUS_NO_MEMORY;
284         }
285
286         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, info,
287                       (ndr_pull_flags_fn_t)ndr_pull_netr_DsRGetDCNameInfo);
288
289         data_blob_free(&blob);
290         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
291                 dsgetdcname_cache_delete(mem_ctx, domain_name);
292                 return ndr_map_error2ntstatus(ndr_err);
293         }
294
295         if (DEBUGLEVEL >= 10) {
296                 NDR_PRINT_DEBUG(netr_DsRGetDCNameInfo, info);
297         }
298
299         /* check flags */
300         if (!check_cldap_reply_required_flags(info->dc_flags, flags)) {
301                 DEBUG(10,("invalid flags\n"));
302                 return NT_STATUS_INVALID_PARAMETER;
303         }
304
305         if ((flags & DS_IP_REQUIRED) &&
306             (info->dc_address_type != DS_ADDRESS_TYPE_INET)) {
307                 return NT_STATUS_INVALID_PARAMETER_MIX;
308         }
309
310         *info_p = info;
311
312         return NT_STATUS_OK;
313 }
314
315 /****************************************************************
316 ****************************************************************/
317
318 static NTSTATUS dsgetdcname_cached(TALLOC_CTX *mem_ctx,
319                                    const char *domain_name,
320                                    struct GUID *domain_guid,
321                                    uint32_t flags,
322                                    const char *site_name,
323                                    struct netr_DsRGetDCNameInfo **info)
324 {
325         NTSTATUS status;
326         bool expired = false;
327
328         status = dsgetdcname_cache_fetch(mem_ctx, domain_name, domain_guid,
329                                          flags, site_name, info, &expired);
330         if (!NT_STATUS_IS_OK(status)) {
331                 DEBUG(10,("dsgetdcname_cached: cache fetch failed with: %s\n",
332                         nt_errstr(status)));
333                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
334         }
335
336         if (flags & DS_BACKGROUND_ONLY) {
337                 return status;
338         }
339
340         if (expired) {
341                 status = dsgetdcname_cache_refresh(mem_ctx, domain_name,
342                                                    domain_guid, flags,
343                                                    site_name, *info);
344                 if (!NT_STATUS_IS_OK(status)) {
345                         return status;
346                 }
347         }
348
349         return status;
350 }
351
352 /****************************************************************
353 ****************************************************************/
354
355 static bool check_allowed_required_flags(uint32_t flags)
356 {
357         uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
358         uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
359         uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
360
361         /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
362          * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
363
364         debug_dsdcinfo_flags(10, flags);
365
366         if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
367                 return false;
368         }
369
370         if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
371                 return false;
372         }
373
374         if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
375                 return false;
376         }
377
378 #if 0
379         if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
380                 printf("gd: here5 \n");
381                 return false;
382         }
383 #endif
384         return true;
385 }
386
387 /****************************************************************
388 ****************************************************************/
389
390 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
391                                     const char *domain_name,
392                                     uint32_t flags,
393                                     struct ip_service_name **returned_dclist,
394                                     int *returned_count)
395 {
396         NTSTATUS status;
397         enum nbt_name_type name_type = NBT_NAME_LOGON;
398         struct ip_service *iplist;
399         int i;
400         struct ip_service_name *dclist = NULL;
401         int count;
402
403         *returned_dclist = NULL;
404         *returned_count = 0;
405
406         if (lp_disable_netbios()) {
407                 return NT_STATUS_NOT_SUPPORTED;
408         }
409
410         if (flags & DS_PDC_REQUIRED) {
411                 name_type = NBT_NAME_PDC;
412         }
413
414         status = internal_resolve_name(domain_name, name_type, NULL,
415                                        &iplist, &count,
416                                        "lmhosts wins bcast");
417         if (!NT_STATUS_IS_OK(status)) {
418                 DEBUG(10,("discover_dc_netbios: failed to find DC\n"));
419                 return status;
420         }
421
422         dclist = TALLOC_ZERO_ARRAY(mem_ctx, struct ip_service_name, count);
423         if (!dclist) {
424                 return NT_STATUS_NO_MEMORY;
425         }
426
427         for (i=0; i<count; i++) {
428
429                 char addr[INET6_ADDRSTRLEN];
430                 struct ip_service_name *r = &dclist[i];
431
432                 print_sockaddr(addr, sizeof(addr),
433                                &iplist[i].ss);
434
435                 r->ss   = iplist[i].ss;
436                 r->port = iplist[i].port;
437                 r->hostname = talloc_strdup(mem_ctx, addr);
438                 if (!r->hostname) {
439                         return NT_STATUS_NO_MEMORY;
440                 }
441
442         }
443
444         *returned_dclist = dclist;
445         *returned_count = count;
446
447         return NT_STATUS_OK;
448 }
449
450 /****************************************************************
451 ****************************************************************/
452
453 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
454                                 const char *domain_name,
455                                 struct GUID *domain_guid,
456                                 uint32_t flags,
457                                 const char *site_name,
458                                 struct ip_service_name **returned_dclist,
459                                 int *return_count)
460 {
461         int i, j;
462         NTSTATUS status;
463         struct dns_rr_srv *dcs = NULL;
464         int numdcs = 0;
465         int numaddrs = 0;
466         struct ip_service_name *dclist = NULL;
467         int count = 0;
468
469         if (flags & DS_PDC_REQUIRED) {
470                 status = ads_dns_query_pdc(mem_ctx, domain_name,
471                                            &dcs, &numdcs);
472         } else if (flags & DS_GC_SERVER_REQUIRED) {
473                 status = ads_dns_query_gcs(mem_ctx, domain_name, site_name,
474                                            &dcs, &numdcs);
475         } else if (flags & DS_KDC_REQUIRED) {
476                 status = ads_dns_query_kdcs(mem_ctx, domain_name, site_name,
477                                             &dcs, &numdcs);
478         } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
479                 status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
480                                            &dcs, &numdcs);
481         } else if (domain_guid) {
482                 status = ads_dns_query_dcs_guid(mem_ctx, domain_name,
483                                                 domain_guid, &dcs, &numdcs);
484         } else {
485                 status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
486                                            &dcs, &numdcs);
487         }
488
489         if (!NT_STATUS_IS_OK(status)) {
490                 return status;
491         }
492
493         if (numdcs == 0) {
494                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
495         }
496
497         for (i=0;i<numdcs;i++) {
498                 numaddrs += MAX(dcs[i].num_ips,1);
499         }
500
501         dclist = TALLOC_ZERO_ARRAY(mem_ctx,
502                                    struct ip_service_name,
503                                    numaddrs);
504         if (!dclist) {
505                 return NT_STATUS_NO_MEMORY;
506         }
507
508         /* now unroll the list of IP addresses */
509
510         *return_count = 0;
511         i = 0;
512         j = 0;
513
514         while ((i < numdcs) && (count < numaddrs)) {
515
516                 struct ip_service_name *r = &dclist[count];
517
518                 r->port = dcs[count].port;
519                 r->hostname = dcs[count].hostname;
520
521                 if (!(flags & DS_IP_REQUIRED)) {
522                         count++;
523                         continue;
524                 }
525
526                 /* If we don't have an IP list for a name, lookup it up */
527
528                 if (!dcs[i].ss_s) {
529                         interpret_string_addr(&r->ss, dcs[i].hostname, 0);
530                         i++;
531                         j = 0;
532                 } else {
533                         /* use the IP addresses from the SRV sresponse */
534
535                         if (j >= dcs[i].num_ips) {
536                                 i++;
537                                 j = 0;
538                                 continue;
539                         }
540
541                         r->ss = dcs[i].ss_s[j];
542                         j++;
543                 }
544
545                 /* make sure it is a valid IP.  I considered checking the
546                  * negative connection cache, but this is the wrong place for
547                  * it.  Maybe only as a hac.  After think about it, if all of
548                  * the IP addresses retuend from DNS are dead, what hope does a
549                  * netbios name lookup have?  The standard reason for falling
550                  * back to netbios lookups is that our DNS server doesn't know
551                  * anything about the DC's   -- jerry */
552
553                 if (!is_zero_addr(&r->ss)) {
554                         count++;
555                         continue;
556                 }
557         }
558
559         *returned_dclist = dclist;
560         *return_count = count;
561
562         if (count > 0) {
563                 return NT_STATUS_OK;
564         }
565
566         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
567 }
568
569 /****************************************************************
570 ****************************************************************/
571
572 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
573                                             const char *dc_unc,
574                                             const char *dc_address,
575                                             uint32_t dc_address_type,
576                                             const struct GUID *domain_guid,
577                                             const char *domain_name,
578                                             const char *forest_name,
579                                             uint32_t flags,
580                                             const char *dc_site_name,
581                                             const char *client_site_name,
582                                             struct netr_DsRGetDCNameInfo **info_out)
583 {
584         struct netr_DsRGetDCNameInfo *info;
585
586         info = TALLOC_ZERO_P(mem_ctx, struct netr_DsRGetDCNameInfo);
587         NT_STATUS_HAVE_NO_MEMORY(info);
588
589         if (dc_unc) {
590                 info->dc_unc = talloc_strdup(mem_ctx, dc_unc);
591                 NT_STATUS_HAVE_NO_MEMORY(info->dc_unc);
592         }
593
594         if (dc_address) {
595                 info->dc_address = talloc_strdup(mem_ctx, dc_address);
596                 NT_STATUS_HAVE_NO_MEMORY(info->dc_address);
597         }
598
599         info->dc_address_type = dc_address_type;
600
601         if (domain_guid) {
602                 info->domain_guid = *domain_guid;
603         }
604
605         if (domain_name) {
606                 info->domain_name = talloc_strdup(mem_ctx, domain_name);
607                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
608         }
609
610         if (forest_name) {
611                 info->forest_name = talloc_strdup(mem_ctx, forest_name);
612                 NT_STATUS_HAVE_NO_MEMORY(info->forest_name);
613         }
614
615         info->dc_flags = flags;
616
617         if (dc_site_name) {
618                 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
619                 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
620         }
621
622         if (client_site_name) {
623                 info->client_site_name = talloc_strdup(mem_ctx,
624                                                        client_site_name);
625                 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
626         }
627
628         *info_out = info;
629
630         return NT_STATUS_OK;
631 }
632
633 /****************************************************************
634 ****************************************************************/
635
636 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
637                                const char *domain_name,
638                                uint32_t flags,
639                                struct ip_service_name *dclist,
640                                int num_dcs,
641                                struct netr_DsRGetDCNameInfo **info)
642 {
643         int i = 0;
644         bool valid_dc = false;
645         struct nbt_cldap_netlogon_5 r;
646         const char *dc_hostname, *dc_domain_name;
647         const char *dc_address;
648         uint32_t dc_address_type;
649         uint32_t dc_flags;
650
651         for (i=0; i<num_dcs; i++) {
652
653                 ZERO_STRUCT(r);
654
655                 DEBUG(10,("LDAP ping to %s\n", dclist[i].hostname));
656
657                 if ((ads_cldap_netlogon(mem_ctx, dclist[i].hostname,
658                                         domain_name, &r)) &&
659                     (check_cldap_reply_required_flags(r.server_type, flags))) {
660                         valid_dc = true;
661                         break;
662                 }
663
664                 continue;
665         }
666
667         if (!valid_dc) {
668                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
669         }
670
671         dc_flags = r.server_type;
672
673         if (flags & DS_RETURN_FLAT_NAME) {
674                 if (!strlen(r.pdc_name) || !strlen(r.domain)) {
675                         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
676                 }
677                 dc_hostname = r.pdc_name;
678                 dc_domain_name = r.domain;
679         } else if (flags & DS_RETURN_DNS_NAME) {
680                 if (!strlen(r.pdc_dns_name) || !strlen(r.dns_domain)) {
681                         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
682                 }
683                 dc_hostname = r.pdc_dns_name;
684                 dc_domain_name = r.dns_domain;
685                 dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
686         } else {
687                 /* FIXME */
688                 dc_hostname = r.pdc_dns_name;
689                 dc_domain_name = r.dns_domain;
690                 dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
691         }
692
693         if (flags & DS_IP_REQUIRED) {
694                 char addr[INET6_ADDRSTRLEN];
695                 print_sockaddr(addr, sizeof(addr), &dclist[i].ss);
696                 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
697                                                 addr);
698                 dc_address_type = DS_ADDRESS_TYPE_INET;
699         } else {
700                 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
701                                              r.pdc_name);
702                 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
703         }
704         NT_STATUS_HAVE_NO_MEMORY(dc_address);
705
706         if (r.forest) {
707                 dc_flags |= DS_DNS_FOREST;
708         }
709
710         return make_domain_controller_info(mem_ctx,
711                                            dc_hostname,
712                                            dc_address,
713                                            dc_address_type,
714                                            &r.domain_uuid,
715                                            dc_domain_name,
716                                            r.forest,
717                                            dc_flags,
718                                            r.server_site,
719                                            r.client_site,
720                                            info);
721
722 }
723
724 /****************************************************************
725 ****************************************************************/
726
727 static struct event_context *ev_context(void)
728 {
729         static struct event_context *ctx;
730
731         if (!ctx && !(ctx = event_context_init(NULL))) {
732                 smb_panic("Could not init event context");
733         }
734         return ctx;
735 }
736
737 /****************************************************************
738 ****************************************************************/
739
740 static struct messaging_context *msg_context(TALLOC_CTX *mem_ctx)
741 {
742         static struct messaging_context *ctx;
743
744         if (!ctx && !(ctx = messaging_init(mem_ctx, server_id_self(),
745                                            ev_context()))) {
746                 smb_panic("Could not init messaging context");
747         }
748         return ctx;
749 }
750
751 /****************************************************************
752 ****************************************************************/
753
754 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
755                                    const char *domain_name,
756                                    uint32_t flags,
757                                    struct ip_service_name *dclist,
758                                    int num_dcs,
759                                    struct netr_DsRGetDCNameInfo **info)
760 {
761         struct sockaddr_storage ss;
762         struct ip_service ip_list;
763         enum nbt_name_type name_type = NBT_NAME_LOGON;
764
765         int i;
766         const char *dc_hostname, *dc_domain_name;
767         const char *dc_address;
768         uint32_t dc_address_type;
769         uint32_t dc_flags = 0;
770         const char *dc_name = NULL;
771         const char *dc_forest = NULL;
772         const char *dc_server_site = NULL;
773         const char *dc_client_site = NULL;
774         struct GUID *dc_domain_guid = NULL;
775         fstring tmp_dc_name;
776         struct messaging_context *msg_ctx = msg_context(mem_ctx);
777         struct nbt_ntlogon_packet *reply = NULL;
778         uint32_t nt_version = NETLOGON_VERSION_1 |
779                               NETLOGON_VERSION_5 |
780                               NETLOGON_VERSION_5EX_WITH_IP;
781
782         if (flags & DS_PDC_REQUIRED) {
783                 name_type = NBT_NAME_PDC;
784         }
785
786         DEBUG(10,("process_dc_netbios\n"));
787
788         for (i=0; i<num_dcs; i++) {
789
790                 ip_list.ss = dclist[i].ss;
791                 ip_list.port = 0;
792
793                 if (!interpret_string_addr(&ss, dclist[i].hostname, AI_NUMERICHOST)) {
794                         return NT_STATUS_UNSUCCESSFUL;
795                 }
796
797                 if (send_getdc_request(mem_ctx, msg_ctx,
798                                        &dclist[i].ss, domain_name,
799                                        NULL, nt_version))
800                 {
801                         int k;
802                         smb_msleep(100);
803                         for (k=0; k<5; k++) {
804                                 if (receive_getdc_response(mem_ctx,
805                                                            &dclist[i].ss,
806                                                            domain_name,
807                                                            &dc_name,
808                                                            &reply)) {
809                                         namecache_store(dc_name, NBT_NAME_SERVER, 1, &ip_list);
810                                         dc_hostname = dc_name;
811                                         dc_domain_name = talloc_strdup_upper(mem_ctx, domain_name);
812                                         NT_STATUS_HAVE_NO_MEMORY(dc_domain_name);
813                                         goto make_reply;
814                                 }
815                                 smb_msleep(500);
816                         }
817                 }
818
819                 if (name_status_find(domain_name,
820                                      name_type,
821                                      NBT_NAME_SERVER,
822                                      &dclist[i].ss,
823                                      tmp_dc_name))
824                 {
825                         dc_hostname = tmp_dc_name;
826                         dc_domain_name = talloc_strdup_upper(mem_ctx, domain_name);
827                         namecache_store(tmp_dc_name, NBT_NAME_SERVER, 1, &ip_list);
828                         goto make_reply;
829                 }
830         }
831
832         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
833
834  make_reply:
835
836         if (reply && reply->command == NTLOGON_RESPONSE_FROM_PDC2) {
837
838                 dc_flags |= reply->req.reply2.server_type;
839                 dc_forest = reply->req.reply2.forest;
840                 dc_server_site = reply->req.reply2.server_site;
841                 dc_client_site = reply->req.reply2.client_site;
842
843                 dc_domain_guid = &reply->req.reply2.domain_uuid;
844
845                 if (flags & DS_RETURN_DNS_NAME) {
846                         dc_domain_name = reply->req.reply2.dns_domain;
847                         dc_hostname = reply->req.reply2.pdc_dns_name;
848                         dc_flags |= DS_DNS_DOMAIN | DS_DNS_CONTROLLER;
849                 } else if (flags & DS_RETURN_FLAT_NAME) {
850                         dc_domain_name = reply->req.reply2.domain;
851                         dc_hostname = reply->req.reply2.pdc_name;
852                 }
853         }
854
855         if (flags & DS_IP_REQUIRED) {
856                 char addr[INET6_ADDRSTRLEN];
857                 print_sockaddr(addr, sizeof(addr), &dclist[i].ss);
858                 dc_address = talloc_asprintf(mem_ctx, "\\\\%s", addr);
859                 dc_address_type = DS_ADDRESS_TYPE_INET;
860         } else {
861                 dc_address = talloc_asprintf(mem_ctx, "\\\\%s", dc_hostname);
862                 dc_address_type = DS_ADDRESS_TYPE_NETBIOS;
863         }
864
865         if (flags & DS_PDC_REQUIRED) {
866                 dc_flags |= NBT_SERVER_PDC | NBT_SERVER_WRITABLE;
867         }
868
869         if (dc_forest) {
870                 dc_flags |= DS_DNS_FOREST;
871         }
872
873         return make_domain_controller_info(mem_ctx,
874                                            dc_hostname,
875                                            dc_address,
876                                            dc_address_type,
877                                            dc_domain_guid,
878                                            dc_domain_name,
879                                            dc_forest,
880                                            dc_flags,
881                                            dc_server_site,
882                                            dc_client_site,
883                                            info);
884 }
885
886 /****************************************************************
887 ****************************************************************/
888
889 static NTSTATUS dsgetdcname_rediscover(TALLOC_CTX *mem_ctx,
890                                        const char *domain_name,
891                                        struct GUID *domain_guid,
892                                        uint32_t flags,
893                                        const char *site_name,
894                                        struct netr_DsRGetDCNameInfo **info)
895 {
896         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
897         struct ip_service_name *dclist = NULL;
898         int num_dcs;
899
900         DEBUG(10,("dsgetdcname_rediscover\n"));
901
902         if (flags & DS_IS_FLAT_NAME) {
903
904                 status = discover_dc_netbios(mem_ctx, domain_name, flags,
905                                              &dclist, &num_dcs);
906                 NT_STATUS_NOT_OK_RETURN(status);
907
908                 return process_dc_netbios(mem_ctx, domain_name, flags,
909                                           dclist, num_dcs, info);
910         }
911
912         if (flags & DS_IS_DNS_NAME) {
913
914                 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
915                                          flags, site_name, &dclist, &num_dcs);
916                 NT_STATUS_NOT_OK_RETURN(status);
917
918                 return process_dc_dns(mem_ctx, domain_name, flags,
919                                       dclist, num_dcs, info);
920         }
921
922         status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
923                                  site_name, &dclist, &num_dcs);
924
925         if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
926
927                 status = process_dc_dns(mem_ctx, domain_name, flags, dclist,
928                                         num_dcs, info);
929                 if (NT_STATUS_IS_OK(status)) {
930                         return status;
931                 }
932         }
933
934         status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
935                                      &num_dcs);
936         NT_STATUS_NOT_OK_RETURN(status);
937
938         return process_dc_netbios(mem_ctx, domain_name, flags, dclist,
939                                   num_dcs, info);
940 }
941
942 /********************************************************************
943  dsgetdcname.
944
945  This will be the only public function here.
946 ********************************************************************/
947
948 NTSTATUS dsgetdcname(TALLOC_CTX *mem_ctx,
949                      const char *domain_name,
950                      struct GUID *domain_guid,
951                      const char *site_name,
952                      uint32_t flags,
953                      struct netr_DsRGetDCNameInfo **info)
954 {
955         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
956         struct netr_DsRGetDCNameInfo *myinfo = NULL;
957
958         DEBUG(10,("dsgetdcname: domain_name: %s, "
959                   "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
960                   domain_name,
961                   domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
962                   site_name, flags));
963
964         *info = NULL;
965
966         if (!check_allowed_required_flags(flags)) {
967                 DEBUG(0,("invalid flags specified\n"));
968                 return NT_STATUS_INVALID_PARAMETER;
969         }
970
971         if (flags & DS_FORCE_REDISCOVERY) {
972                 goto rediscover;
973         }
974
975         status = dsgetdcname_cached(mem_ctx, domain_name, domain_guid,
976                                     flags, site_name, &myinfo);
977         if (NT_STATUS_IS_OK(status)) {
978                 *info = myinfo;
979                 return status;
980         }
981
982         if (flags & DS_BACKGROUND_ONLY) {
983                 return status;
984         }
985
986  rediscover:
987         status = dsgetdcname_rediscover(mem_ctx, domain_name,
988                                         domain_guid, flags, site_name,
989                                         &myinfo);
990
991         if (NT_STATUS_IS_OK(status)) {
992                 dsgetdcname_cache_store(mem_ctx, domain_name, myinfo);
993                 *info = myinfo;
994         }
995
996         return status;
997 }