large change:
[obnox/samba/samba-obnox.git] / source3 / libsmb / 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  Utility function to return the name of a DC. The name is guaranteed to be 
29  valid since we have already done a name_status_find on it 
30  ***************************************************************************/
31
32 BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
33 {
34         struct ip_service *ip_list = NULL;
35         struct in_addr dc_ip, exclude_ip;
36         int count, i;
37         BOOL use_pdc_only;
38         NTSTATUS result;
39         
40         zero_ip(&exclude_ip);
41
42         use_pdc_only = must_use_pdc(domain);
43         
44         /* Lookup domain controller name */
45            
46         if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) 
47         {
48                 DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
49                 
50                 /* check the connection cache and perform the node status 
51                    lookup only if the IP is not found to be bad */
52
53                 if (name_status_find(domain, 0x1b, 0x20, dc_ip, srv_name) ) {
54                         result = check_negative_conn_cache( domain, srv_name );
55                         if ( NT_STATUS_IS_OK(result) )
56                                 goto done;
57                 }
58                 /* Didn't get name, remember not to talk to this DC. */
59                 exclude_ip = dc_ip;
60         }
61
62         /* get a list of all domain controllers */
63         
64         if ( !get_sorted_dc_list(domain, &ip_list, &count, False) ) {
65                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
66                 return False;
67         }
68
69         /* Remove the entry we've already failed with (should be the PDC). */
70
71         if ( use_pdc_only ) {
72                 for (i = 0; i < count; i++) {   
73                         if (ip_equal( exclude_ip, ip_list[i].ip))
74                                 zero_ip(&ip_list[i].ip);
75                 }
76         }
77
78         for (i = 0; i < count; i++) {
79                 if (is_zero_ip(ip_list[i].ip))
80                         continue;
81
82                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i].ip, srv_name)) {
83                         result = check_negative_conn_cache( domain, srv_name );
84                         if ( NT_STATUS_IS_OK(result) ) {
85                                 dc_ip = ip_list[i].ip;
86                                 goto done;
87                         }
88                 }
89         }
90         
91
92         SAFE_FREE(ip_list);
93
94         /* No-one to talk to )-: */
95         return False;           /* Boo-hoo */
96         
97  done:
98         /* We have the netbios name and IP address of a domain controller.
99            Ideally we should sent a SAMLOGON request to determine whether
100            the DC is alive and kicking.  If we can catch a dead DC before
101            performing a cli_connect() we can avoid a 30-second timeout. */
102
103         DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
104                   inet_ntoa(dc_ip), domain));
105
106         *ip_out = dc_ip;
107
108         SAFE_FREE(ip_list);
109
110         return True;
111 }