Remove some globals
[gd/samba/.git] / source3 / libsmb / dsgetdcname.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    DsGetDcname
5
6    Copyright (C) Gerald Carter 2006
7    Copyright (C) Guenther Deschner 2007
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 int pack_dsdcinfo(struct DS_DOMAIN_CONTROLLER_INFO *info,
114                          unsigned char **buf)
115 {
116         unsigned char *buffer = NULL;
117         int len = 0;
118         int buflen = 0;
119         UUID_FLAT guid_flat;
120
121         DEBUG(10,("pack_dsdcinfo: Packing dsdcinfo\n"));
122
123         ZERO_STRUCT(guid_flat);
124
125         if (info->domain_guid) {
126                 const struct GUID *guid = info->domain_guid;
127                 smb_uuid_pack(*guid, &guid_flat);
128         }
129
130  again:
131         len = 0;
132
133         if (buflen > 0) {
134                 DEBUG(10,("pack_dsdcinfo: Packing domain %s (%s)\n",
135                           info->domain_name, info->domain_controller_name));
136         }
137
138         len += tdb_pack(buffer+len, buflen-len, "ffdBffdff",
139                         info->domain_controller_name,
140                         info->domain_controller_address,
141                         info->domain_controller_address_type,
142                         UUID_FLAT_SIZE, guid_flat.info,
143                         info->domain_name,
144                         info->dns_forest_name,
145                         info->flags,
146                         info->dc_site_name,
147                         info->client_site_name);
148
149         if (buflen < len) {
150                 SAFE_FREE(buffer);
151                 if ((buffer = SMB_MALLOC_ARRAY(unsigned char, len)) == NULL ) {
152                         DEBUG(0,("pack_dsdcinfo: failed to alloc buffer!\n"));
153                         buflen = -1;
154                         goto done;
155                 }
156                 buflen = len;
157                 goto again;
158         }
159
160         *buf = buffer;
161
162  done:
163         return buflen;
164 }
165
166 /*********************************************************************
167  ********************************************************************/
168
169 static NTSTATUS unpack_dsdcinfo(TALLOC_CTX *mem_ctx,
170                                 unsigned char *buf,
171                                 int buflen,
172                                 struct DS_DOMAIN_CONTROLLER_INFO **info_ret)
173 {
174         int len = 0;
175         struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
176         uint32_t guid_len = 0;
177         unsigned char *guid_buf = NULL;
178         UUID_FLAT guid_flat;
179
180         /* forgive me 6 times */
181         fstring domain_controller_name;
182         fstring domain_controller_address;
183         fstring domain_name;
184         fstring dns_forest_name;
185         fstring dc_site_name;
186         fstring client_site_name;
187
188         info = TALLOC_ZERO_P(mem_ctx, struct DS_DOMAIN_CONTROLLER_INFO);
189         NT_STATUS_HAVE_NO_MEMORY(info);
190
191         len += tdb_unpack(buf+len, buflen-len, "ffdBffdff",
192                           &domain_controller_name,
193                           &domain_controller_address,
194                           &info->domain_controller_address_type,
195                           &guid_len, &guid_buf,
196                           &domain_name,
197                           &dns_forest_name,
198                           &info->flags,
199                           &dc_site_name,
200                           &client_site_name);
201         if (len == -1) {
202                 DEBUG(5,("unpack_dsdcinfo: Failed to unpack domain\n"));
203                 goto failed;
204         }
205
206         info->domain_controller_name =
207                 talloc_strdup(mem_ctx, domain_controller_name);
208         info->domain_controller_address =
209                 talloc_strdup(mem_ctx, domain_controller_address);
210         info->domain_name =
211                 talloc_strdup(mem_ctx, domain_name);
212         info->dns_forest_name =
213                 talloc_strdup(mem_ctx, dns_forest_name);
214         info->dc_site_name =
215                 talloc_strdup(mem_ctx, dc_site_name);
216         info->client_site_name =
217                 talloc_strdup(mem_ctx, client_site_name);
218
219         if (!info->domain_controller_name ||
220             !info->domain_controller_address ||
221             !info->domain_name ||
222             !info->dns_forest_name ||
223             !info->dc_site_name ||
224             !info->client_site_name) {
225                 goto failed;
226         }
227
228         if (guid_len > 0) {
229                 struct GUID guid;
230
231                 if (guid_len != UUID_FLAT_SIZE) {
232                         goto failed;
233                 }
234
235                 memcpy(&guid_flat.info, guid_buf, guid_len);
236                 smb_uuid_unpack(guid_flat, &guid);
237
238                 info->domain_guid = (struct GUID *)talloc_memdup(
239                         mem_ctx, &guid, sizeof(guid));
240                 if (!info->domain_guid) {
241                         goto failed;
242                 }
243                 SAFE_FREE(guid_buf);
244         }
245
246         DEBUG(10,("unpack_dcscinfo: Unpacked domain %s (%s)\n",
247                   info->domain_name, info->domain_controller_name));
248
249         *info_ret = info;
250
251         return NT_STATUS_OK;
252
253  failed:
254         TALLOC_FREE(info);
255         SAFE_FREE(guid_buf);
256         return NT_STATUS_NO_MEMORY;
257 }
258
259 /****************************************************************
260 ****************************************************************/
261
262 static char *DsGetDcName_cache_key(TALLOC_CTX *mem_ctx, const char *domain)
263 {
264         if (!mem_ctx || !domain) {
265                 return NULL;
266         }
267
268         return talloc_asprintf_strupper_m(mem_ctx, DSGETDCNAME_FMT, domain);
269 }
270
271 /****************************************************************
272 ****************************************************************/
273
274 static NTSTATUS DsGetDcName_cache_delete(TALLOC_CTX *mem_ctx,
275                                         const char *domain_name)
276 {
277         char *key;
278
279         if (!gencache_init()) {
280                 return NT_STATUS_INTERNAL_DB_ERROR;
281         }
282
283         key = DsGetDcName_cache_key(mem_ctx, domain_name);
284         if (!key) {
285                 return NT_STATUS_NO_MEMORY;
286         }
287
288         if (!gencache_del(key)) {
289                 return NT_STATUS_UNSUCCESSFUL;
290         }
291
292         return NT_STATUS_OK;
293 }
294
295 /****************************************************************
296 ****************************************************************/
297
298 static NTSTATUS DsGetDcName_cache_store(TALLOC_CTX *mem_ctx,
299                                         const char *domain_name,
300                                         struct DS_DOMAIN_CONTROLLER_INFO *info)
301 {
302         time_t expire_time;
303         char *key;
304         bool ret = False;
305         DATA_BLOB blob;
306         unsigned char *buf = NULL;
307         int len = 0;
308
309         if (!gencache_init()) {
310                 return NT_STATUS_INTERNAL_DB_ERROR;
311         }
312
313         key = DsGetDcName_cache_key(mem_ctx, domain_name);
314         if (!key) {
315                 return NT_STATUS_NO_MEMORY;
316         }
317
318         expire_time = time(NULL) + DSGETDCNAME_CACHE_TTL;
319
320         len = pack_dsdcinfo(info, &buf);
321         if (len == -1) {
322                 return NT_STATUS_UNSUCCESSFUL;
323         }
324
325         blob = data_blob(buf, len);
326         SAFE_FREE(buf);
327
328         if (gencache_lock_entry(key) != 0) {
329                 data_blob_free(&blob);
330                 return NT_STATUS_LOCK_NOT_GRANTED;
331         }
332
333         ret = gencache_set_data_blob(key, &blob, expire_time);
334         data_blob_free(&blob);
335
336         gencache_unlock_entry(key);
337
338         return ret ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
339 }
340
341 /****************************************************************
342 ****************************************************************/
343
344 static NTSTATUS DsGetDcName_cache_refresh(TALLOC_CTX *mem_ctx,
345                                           const char *domain_name,
346                                           struct GUID *domain_guid,
347                                           uint32_t flags,
348                                           const char *site_name,
349                                           struct DS_DOMAIN_CONTROLLER_INFO *info)
350 {
351         struct cldap_netlogon_reply r;
352
353         /* check if matching entry is older then 15 minutes, if yes, send
354          * CLDAP/MAILSLOT ping again and store the cached data */
355
356         ZERO_STRUCT(r);
357
358         if (ads_cldap_netlogon(info->domain_controller_name,
359                                info->domain_name, &r)) {
360
361                 DsGetDcName_cache_delete(mem_ctx, domain_name);
362
363                 return DsGetDcName_cache_store(mem_ctx,
364                                                info->domain_name,
365                                                info);
366         }
367
368         return NT_STATUS_INVALID_NETWORK_RESPONSE;
369 }
370
371 /****************************************************************
372 ****************************************************************/
373
374 #define RETURN_ON_FALSE(x) if (!x) return False;
375
376 static bool check_cldap_reply_required_flags(uint32_t ret_flags,
377                                              uint32_t req_flags)
378 {
379         if (req_flags & DS_PDC_REQUIRED)
380                 RETURN_ON_FALSE(ret_flags & ADS_PDC);
381
382         if (req_flags & DS_GC_SERVER_REQUIRED)
383                 RETURN_ON_FALSE(ret_flags & ADS_GC);
384
385         if (req_flags & DS_ONLY_LDAP_NEEDED)
386                 RETURN_ON_FALSE(ret_flags & ADS_LDAP);
387
388         if ((req_flags & DS_DIRECTORY_SERVICE_REQUIRED) ||
389             (req_flags & DS_DIRECTORY_SERVICE_PREFERRED))
390                 RETURN_ON_FALSE(ret_flags & ADS_DS);
391
392         if (req_flags & DS_KDC_REQUIRED)
393                 RETURN_ON_FALSE(ret_flags & ADS_KDC);
394
395         if (req_flags & DS_TIMESERV_REQUIRED)
396                 RETURN_ON_FALSE(ret_flags & ADS_TIMESERV);
397
398         if (req_flags & DS_WRITABLE_REQUIRED)
399                 RETURN_ON_FALSE(ret_flags & ADS_WRITABLE);
400
401         return True;
402 }
403
404 /****************************************************************
405 ****************************************************************/
406
407 static NTSTATUS DsGetDcName_cache_fetch(TALLOC_CTX *mem_ctx,
408                                         const char *domain_name,
409                                         struct GUID *domain_guid,
410                                         uint32_t flags,
411                                         const char *site_name,
412                                         struct DS_DOMAIN_CONTROLLER_INFO **info,
413                                         bool *expired)
414 {
415         char *key;
416         DATA_BLOB blob;
417         NTSTATUS status;
418
419         if (!gencache_init()) {
420                 return NT_STATUS_INTERNAL_DB_ERROR;
421         }
422
423         key = DsGetDcName_cache_key(mem_ctx, domain_name);
424         if (!key) {
425                 return NT_STATUS_NO_MEMORY;
426         }
427
428         if (!gencache_get_data_blob(key, &blob, expired)) {
429                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
430         }
431
432         status = unpack_dsdcinfo(mem_ctx, blob.data, blob.length, info);
433         if (!NT_STATUS_IS_OK(status)) {
434                 data_blob_free(&blob);
435                 return status;
436         }
437
438         data_blob_free(&blob);
439
440         /* check flags */
441         if (!check_cldap_reply_required_flags((*info)->flags, flags)) {
442                 DEBUG(10,("invalid flags\n"));
443                 return NT_STATUS_INVALID_PARAMETER;
444         }
445
446         if ((flags & DS_IP_REQUIRED) &&
447             ((*info)->domain_controller_address_type != ADS_INET_ADDRESS)) {
448                 return NT_STATUS_INVALID_PARAMETER_MIX;
449         }
450
451         return NT_STATUS_OK;
452 }
453
454 /****************************************************************
455 ****************************************************************/
456
457 static NTSTATUS DsGetDcName_cached(TALLOC_CTX *mem_ctx,
458                                    const char *domain_name,
459                                    struct GUID *domain_guid,
460                                    uint32_t flags,
461                                    const char *site_name,
462                                    struct DS_DOMAIN_CONTROLLER_INFO **info)
463 {
464         NTSTATUS status;
465         bool expired = False;
466
467         status = DsGetDcName_cache_fetch(mem_ctx, domain_name, domain_guid,
468                                          flags, site_name, info, &expired);
469         if (!NT_STATUS_IS_OK(status)) {
470                 DEBUG(10,("DsGetDcName_cached: cache fetch failed with: %s\n",
471                         nt_errstr(status)));
472                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
473         }
474
475         if (flags & DS_BACKGROUND_ONLY) {
476                 return status;
477         }
478
479         if (expired) {
480                 status = DsGetDcName_cache_refresh(mem_ctx, domain_name,
481                                                    domain_guid, flags,
482                                                    site_name, *info);
483                 if (!NT_STATUS_IS_OK(status)) {
484                         return status;
485                 }
486         }
487
488         return status;
489 }
490
491 /****************************************************************
492 ****************************************************************/
493
494 static bool check_allowed_required_flags(uint32_t flags)
495 {
496         uint32_t return_type = flags & (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME);
497         uint32_t offered_type = flags & (DS_IS_FLAT_NAME|DS_IS_DNS_NAME);
498         uint32_t query_type = flags & (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY);
499
500         /* FIXME: check for DSGETDC_VALID_FLAGS and check for excluse bits
501          * (DS_PDC_REQUIRED, DS_KDC_REQUIRED, DS_GC_SERVER_REQUIRED) */
502
503         debug_dsdcinfo_flags(10, flags);
504
505         if (return_type == (DS_RETURN_FLAT_NAME|DS_RETURN_DNS_NAME)) {
506                 return False;
507         }
508
509         if (offered_type == (DS_IS_DNS_NAME|DS_IS_FLAT_NAME)) {
510                 return False;
511         }
512
513         if (query_type == (DS_BACKGROUND_ONLY|DS_FORCE_REDISCOVERY)) {
514                 return False;
515         }
516
517 #if 0
518         if ((flags & DS_RETURN_DNS_NAME) && (!(flags & DS_IP_REQUIRED))) {
519                 printf("gd: here5 \n");
520                 return False;
521         }
522 #endif
523         return True;
524 }
525
526 /****************************************************************
527 ****************************************************************/
528
529 static NTSTATUS discover_dc_netbios(TALLOC_CTX *mem_ctx,
530                                     const char *domain_name,
531                                     uint32_t flags,
532                                     struct ip_service_name **returned_dclist,
533                                     int *return_count)
534 {
535         if (lp_disable_netbios()) {
536                 return NT_STATUS_NOT_SUPPORTED;
537         }
538
539         /* FIXME: code here */
540
541         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
542 }
543
544 /****************************************************************
545 ****************************************************************/
546
547 static NTSTATUS discover_dc_dns(TALLOC_CTX *mem_ctx,
548                                 const char *domain_name,
549                                 struct GUID *domain_guid,
550                                 uint32_t flags,
551                                 const char *site_name,
552                                 struct ip_service_name **returned_dclist,
553                                 int *return_count)
554 {
555         int i, j;
556         NTSTATUS status;
557         struct dns_rr_srv *dcs = NULL;
558         int numdcs = 0;
559         int numaddrs = 0;
560
561         if ((!(flags & DS_DIRECTORY_SERVICE_REQUIRED)) &&
562             (!(flags & DS_KDC_REQUIRED)) &&
563             (!(flags & DS_GC_SERVER_REQUIRED)) &&
564             (!(flags & DS_PDC_REQUIRED))) {
565                 DEBUG(1,("discover_dc_dns: invalid flags\n"));
566                 return NT_STATUS_INVALID_PARAMETER;
567         }
568
569         if (flags & DS_PDC_REQUIRED) {
570                 status = ads_dns_query_pdc(mem_ctx, domain_name,
571                                            &dcs, &numdcs);
572         } else if (flags & DS_GC_SERVER_REQUIRED) {
573                 status = ads_dns_query_gcs(mem_ctx, domain_name, site_name,
574                                            &dcs, &numdcs);
575         } else if (flags & DS_KDC_REQUIRED) {
576                 status = ads_dns_query_kdcs(mem_ctx, domain_name, site_name,
577                                             &dcs, &numdcs);
578         } else if (flags & DS_DIRECTORY_SERVICE_REQUIRED) {
579                 status = ads_dns_query_dcs(mem_ctx, domain_name, site_name,
580                                            &dcs, &numdcs);
581         } else if (domain_guid) {
582                 status = ads_dns_query_dcs_guid(mem_ctx, domain_name,
583                                                 domain_guid, &dcs, &numdcs);
584         } else {
585                 /* FIXME: ? */
586                 DEBUG(1,("discover_dc_dns: not enough input\n"));
587                 status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
588         }
589
590         if (!NT_STATUS_IS_OK(status)) {
591                 return status;
592         }
593
594         if (numdcs == 0) {
595                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
596         }
597
598         for (i=0;i<numdcs;i++) {
599                 numaddrs += MAX(dcs[i].num_ips,1);
600         }
601
602         if ((*returned_dclist = TALLOC_ZERO_ARRAY(mem_ctx,
603                                                   struct ip_service_name,
604                                                   numaddrs)) == NULL) {
605                 return NT_STATUS_NO_MEMORY;
606         }
607
608         /* now unroll the list of IP addresses */
609
610         *return_count = 0;
611         i = 0;
612         j = 0;
613         while (i < numdcs && (*return_count<numaddrs)) {
614
615                 struct ip_service_name *r = &(*returned_dclist)[*return_count];
616
617                 r->port = dcs[i].port;
618                 r->hostname = dcs[i].hostname;
619
620                 if (!(flags & DS_IP_REQUIRED)) {
621                         (*return_count)++;
622                         continue;
623                 }
624
625                 /* If we don't have an IP list for a name, lookup it up */
626
627                 if (!dcs[i].ss_s) {
628                         interpret_string_addr(&r->ss, dcs[i].hostname, 0);
629                         i++;
630                         j = 0;
631                 } else {
632                         /* use the IP addresses from the SRV sresponse */
633
634                         if (j >= dcs[i].num_ips) {
635                                 i++;
636                                 j = 0;
637                                 continue;
638                         }
639
640                         r->ss = dcs[i].ss_s[j];
641                         j++;
642                 }
643
644                 /* make sure it is a valid IP.  I considered checking the
645                  * negative connection cache, but this is the wrong place for
646                  * it.  Maybe only as a hac.  After think about it, if all of
647                  * the IP addresses retuend from DNS are dead, what hope does a
648                  * netbios name lookup have?  The standard reason for falling
649                  * back to netbios lookups is that our DNS server doesn't know
650                  * anything about the DC's   -- jerry */
651
652                 if (!is_zero_addr(&r->ss)) {
653                         (*return_count)++;
654                         continue;
655                 }
656         }
657
658         return (*return_count > 0) ? NT_STATUS_OK :
659                                      NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
660 }
661
662 /****************************************************************
663 ****************************************************************/
664
665 static NTSTATUS make_domain_controller_info(TALLOC_CTX *mem_ctx,
666                                             const char *domain_controller_name,
667                                             const char *domain_controller_address,
668                                             uint32_t domain_controller_address_type,
669                                             const struct GUID *domain_guid,
670                                             const char *domain_name,
671                                             const char *dns_forest_name,
672                                             uint32_t flags,
673                                             const char *dc_site_name,
674                                             const char *client_site_name,
675                                             struct DS_DOMAIN_CONTROLLER_INFO **info_out)
676 {
677         struct DS_DOMAIN_CONTROLLER_INFO *info;
678
679         info = TALLOC_ZERO_P(mem_ctx, struct DS_DOMAIN_CONTROLLER_INFO);
680         NT_STATUS_HAVE_NO_MEMORY(info);
681
682         if (domain_controller_name) {
683                 info->domain_controller_name = talloc_strdup(mem_ctx,
684                         domain_controller_name);
685                 NT_STATUS_HAVE_NO_MEMORY(info->domain_controller_name);
686         }
687
688         if (domain_controller_address) {
689                 info->domain_controller_address = talloc_strdup(mem_ctx,
690                         domain_controller_address);
691                 NT_STATUS_HAVE_NO_MEMORY(info->domain_controller_address);
692         }
693
694         info->domain_controller_address_type = domain_controller_address_type;
695
696         if (domain_guid) {
697                 info->domain_guid = (struct GUID *)talloc_memdup(
698                         mem_ctx, domain_guid, sizeof(*domain_guid));
699                 NT_STATUS_HAVE_NO_MEMORY(info->domain_guid);
700         }
701
702         if (domain_name) {
703                 info->domain_name = talloc_strdup(mem_ctx, domain_name);
704                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
705         }
706
707         if (dns_forest_name) {
708                 info->dns_forest_name = talloc_strdup(mem_ctx,
709                                                       dns_forest_name);
710                 NT_STATUS_HAVE_NO_MEMORY(info->dns_forest_name);
711         }
712
713         info->flags = flags;
714
715         if (dc_site_name) {
716                 info->dc_site_name = talloc_strdup(mem_ctx, dc_site_name);
717                 NT_STATUS_HAVE_NO_MEMORY(info->dc_site_name);
718         }
719
720         if (client_site_name) {
721                 info->client_site_name = talloc_strdup(mem_ctx,
722                                                        client_site_name);
723                 NT_STATUS_HAVE_NO_MEMORY(info->client_site_name);
724         }
725
726         *info_out = info;
727
728         return NT_STATUS_OK;
729 }
730
731 /****************************************************************
732 ****************************************************************/
733
734 static NTSTATUS process_dc_dns(TALLOC_CTX *mem_ctx,
735                                const char *domain_name,
736                                uint32_t flags,
737                                struct ip_service_name **dclist,
738                                int num_dcs,
739                                struct DS_DOMAIN_CONTROLLER_INFO **info)
740 {
741         int i = 0;
742         bool valid_dc = False;
743         struct cldap_netlogon_reply r;
744         const char *dc_hostname, *dc_domain_name;
745         const char *dc_address;
746         uint32_t dc_address_type;
747         uint32_t dc_flags;
748         struct GUID dc_guid;
749
750         for (i=0; i<num_dcs; i++) {
751
752                 ZERO_STRUCT(r);
753
754                 if ((ads_cldap_netlogon(dclist[i]->hostname,
755                                         domain_name, &r)) &&
756                     (check_cldap_reply_required_flags(r.flags, flags))) {
757                         valid_dc = True;
758                         break;
759                 }
760
761                 continue;
762         }
763
764         if (!valid_dc) {
765                 return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
766         }
767
768         dc_flags = r.flags;
769
770         if (flags & DS_RETURN_FLAT_NAME) {
771                 if (!strlen(r.netbios_hostname) || !strlen(r.netbios_domain)) {
772                         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
773                 }
774                 dc_hostname = r.netbios_hostname;
775                 dc_domain_name = r.netbios_domain;
776         } else if (flags & DS_RETURN_DNS_NAME) {
777                 if (!strlen(r.hostname) || !strlen(r.domain)) {
778                         return NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
779                 }
780                 dc_hostname = r.hostname;
781                 dc_domain_name = r.domain;
782                 dc_flags |= ADS_DNS_DOMAIN | ADS_DNS_CONTROLLER;
783         } else {
784                 /* FIXME */
785                 dc_hostname = r.hostname;
786                 dc_domain_name = r.domain;
787                 dc_flags |= ADS_DNS_DOMAIN | ADS_DNS_CONTROLLER;
788         }
789
790         if (flags & DS_IP_REQUIRED) {
791                 char addr[INET6_ADDRSTRLEN];
792                 print_sockaddr(addr, sizeof(addr), &dclist[i]->ss);
793                 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
794                                                 addr);
795                 dc_address_type = ADS_INET_ADDRESS;
796         } else {
797                 dc_address = talloc_asprintf(mem_ctx, "\\\\%s",
798                                              r.netbios_hostname);
799                 dc_address_type = ADS_NETBIOS_ADDRESS;
800         }
801         NT_STATUS_HAVE_NO_MEMORY(dc_address);
802         smb_uuid_unpack(r.guid, &dc_guid);
803
804         if (r.forest) {
805                 dc_flags |= ADS_DNS_FOREST;
806         }
807
808         return make_domain_controller_info(mem_ctx,
809                                            dc_hostname,
810                                            dc_address,
811                                            dc_address_type,
812                                            &dc_guid,
813                                            dc_domain_name,
814                                            r.forest,
815                                            dc_flags,
816                                            r.server_site_name,
817                                            r.client_site_name,
818                                            info);
819
820 }
821
822 /****************************************************************
823 ****************************************************************/
824
825 static NTSTATUS process_dc_netbios(TALLOC_CTX *mem_ctx,
826                                    const char *domain_name,
827                                    uint32_t flags,
828                                    struct ip_service_name **dclist,
829                                    int num_dcs,
830                                    struct DS_DOMAIN_CONTROLLER_INFO **info)
831 {
832         /* FIXME: code here */
833
834         return NT_STATUS_NOT_SUPPORTED;
835 }
836
837 /****************************************************************
838 ****************************************************************/
839
840 static NTSTATUS DsGetDcName_rediscover(TALLOC_CTX *mem_ctx,
841                                        const char *domain_name,
842                                        struct GUID *domain_guid,
843                                        uint32_t flags,
844                                        const char *site_name,
845                                        struct DS_DOMAIN_CONTROLLER_INFO **info)
846 {
847         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
848         struct ip_service_name *dclist;
849         int num_dcs;
850
851         DEBUG(10,("DsGetDcName_rediscover\n"));
852
853         if (flags & DS_IS_FLAT_NAME) {
854
855                 status = discover_dc_netbios(mem_ctx, domain_name, flags,
856                                              &dclist, &num_dcs);
857                 NT_STATUS_NOT_OK_RETURN(status);
858
859                 return process_dc_netbios(mem_ctx, domain_name, flags,
860                                           &dclist, num_dcs, info);
861         }
862
863         if (flags & DS_IS_DNS_NAME) {
864
865                 status = discover_dc_dns(mem_ctx, domain_name, domain_guid,
866                                          flags, site_name, &dclist, &num_dcs);
867                 NT_STATUS_NOT_OK_RETURN(status);
868
869                 return process_dc_dns(mem_ctx, domain_name, flags,
870                                       &dclist, num_dcs, info);
871         }
872
873         status = discover_dc_dns(mem_ctx, domain_name, domain_guid, flags,
874                                  site_name, &dclist, &num_dcs);
875
876         if (NT_STATUS_IS_OK(status) && num_dcs != 0) {
877
878                 status = process_dc_dns(mem_ctx, domain_name, flags, &dclist,
879                                         num_dcs, info);
880                 if (NT_STATUS_IS_OK(status)) {
881                         return status;
882                 }
883         }
884
885         status = discover_dc_netbios(mem_ctx, domain_name, flags, &dclist,
886                                      &num_dcs);
887         NT_STATUS_NOT_OK_RETURN(status);
888
889         return process_dc_netbios(mem_ctx, domain_name, flags, &dclist,
890                                   num_dcs, info);
891 }
892
893 /********************************************************************
894  DsGetDcName.
895
896  This will be the only public function here.
897 ********************************************************************/
898
899 NTSTATUS DsGetDcName(TALLOC_CTX *mem_ctx,
900                      const char *computer_name,
901                      const char *domain_name,
902                      struct GUID *domain_guid,
903                      const char *site_name,
904                      uint32_t flags,
905                      struct DS_DOMAIN_CONTROLLER_INFO **info)
906 {
907         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
908         struct DS_DOMAIN_CONTROLLER_INFO *myinfo = NULL;
909
910         DEBUG(10,("DsGetDcName: computer_name: %s, domain_name: %s, "
911                   "domain_guid: %s, site_name: %s, flags: 0x%08x\n",
912                   computer_name, domain_name,
913                   domain_guid ? GUID_string(mem_ctx, domain_guid) : "(null)",
914                   site_name, flags));
915
916         *info = NULL;
917
918         if (!check_allowed_required_flags(flags)) {
919                 DEBUG(0,("invalid flags specified\n"));
920                 return NT_STATUS_INVALID_PARAMETER;
921         }
922
923         if (flags & DS_FORCE_REDISCOVERY) {
924                 goto rediscover;
925         }
926
927         status = DsGetDcName_cached(mem_ctx, domain_name, domain_guid,
928                                     flags, site_name, &myinfo);
929         if (NT_STATUS_IS_OK(status)) {
930                 *info = myinfo;
931                 return status;
932         }
933
934         if (flags & DS_BACKGROUND_ONLY) {
935                 return status;
936         }
937
938  rediscover:
939         status = DsGetDcName_rediscover(mem_ctx, domain_name,
940                                         domain_guid, flags, site_name,
941                                         &myinfo);
942
943         if (NT_STATUS_IS_OK(status)) {
944                 DsGetDcName_cache_store(mem_ctx, domain_name, myinfo);
945                 *info = myinfo;
946         }
947
948         return status;
949 }