16e5282497b9600945d8c0b2e4fc5f2dea518cd5
[samba.git] / source4 / libcli / namequery_dc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon connection manager
5
6    Copyright (C) Tim Potter 2001
7    Copyright (C) Andrew Bartlett 2002
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25 #include "includes.h"
26
27
28 /*
29   find the DC for a domain using methods appropriate for a RPC domain
30 */
31 BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out)
32 {
33         struct in_addr *ip_list = NULL, dc_ip, exclude_ip;
34         int count, i;
35         BOOL list_ordered;
36         BOOL use_pdc_only;
37         
38         zero_ip(&exclude_ip);
39
40         use_pdc_only = must_use_pdc(domain);
41         
42         /* Lookup domain controller name */
43            
44         if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) {
45                 DEBUG(10,("rpc_find_dc: Atempting to lookup PDC to avoid sam sync delays\n"));
46                 
47                 if (name_status_find(domain, 0x1c, 0x20, dc_ip, srv_name)) {
48                         goto done;
49                 }
50                 /* Didn't get name, remember not to talk to this DC. */
51                 exclude_ip = dc_ip;
52         }
53
54         /* get a list of all domain controllers */
55         
56         if (!get_dc_list( domain, &ip_list, &count, &list_ordered) ) {
57                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
58                 return False;
59         }
60
61         /* Remove the entry we've already failed with (should be the PDC). */
62
63         if ( use_pdc_only ) {
64                 for (i = 0; i < count; i++) {   
65                         if (ip_equal( exclude_ip, ip_list[i]))
66                                 zero_ip(&ip_list[i]);
67                 }
68         }
69
70         /* Pick a nice close server, but only if the list was not ordered */
71         if (!list_ordered && (count > 1) ) {
72                 qsort(ip_list, count, sizeof(struct in_addr), QSORT_CAST ip_compare);
73         }
74
75         for (i = 0; i < count; i++) {
76                 if (is_zero_ip(ip_list[i]))
77                         continue;
78
79                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i], srv_name)) {
80                         dc_ip = ip_list[i];
81                         goto done;
82                 }
83         }
84
85
86         SAFE_FREE(ip_list);
87
88         return False;
89 done:
90         /* We have the netbios name and IP address of a domain controller.
91            Ideally we should sent a SAMLOGON request to determine whether
92            the DC is alive and kicking.  If we can catch a dead DC before
93            performing a smbcli_connect() we can avoid a 30-second timeout. */
94
95         DEBUG(3, ("rpc_find_dc: Returning DC %s (%s) for domain %s\n", srv_name,
96                   inet_ntoa(dc_ip), domain));
97
98         *ip_out = dc_ip;
99
100         SAFE_FREE(ip_list);
101
102         return True;
103 }
104