Initial import
[samba] / source / 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    Copyright (C) Gerald Carter 2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
26 #include "includes.h"
27
28 /**************************************************************************
29  Find the name and IP address for a server in he realm/domain
30  *************************************************************************/
31  
32 static BOOL ads_dc_name(const char *domain, const char *realm, struct in_addr *dc_ip, fstring srv_name)
33 {
34         ADS_STRUCT *ads;
35
36         if (!realm && strequal(domain, lp_workgroup()))
37                 realm = lp_realm();
38
39         ads = ads_init(realm, domain, NULL);
40         if (!ads)
41                 return False;
42
43         DEBUG(4,("ads_dc_name: domain=%s\n", domain));
44
45 #ifdef HAVE_ADS
46         /* we don't need to bind, just connect */
47         ads->auth.flags |= ADS_AUTH_NO_BIND;
48
49         ads_connect(ads);
50 #endif
51
52         if (!ads->config.realm) {
53                 ads_destroy(&ads);
54                 return False;
55         }
56
57         fstrcpy(srv_name, ads->config.ldap_server_name);
58         strupper_m(srv_name);
59         *dc_ip = ads->ldap_ip;
60         ads_destroy(&ads);
61         
62         DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
63                  srv_name, inet_ntoa(*dc_ip)));
64         
65         return True;
66 }
67
68 /****************************************************************************
69  Utility function to return the name of a DC. The name is guaranteed to be 
70  valid since we have already done a name_status_find on it 
71  ***************************************************************************/
72
73 static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
74 {
75         struct ip_service *ip_list = NULL;
76         struct in_addr dc_ip, exclude_ip;
77         int count, i;
78         BOOL use_pdc_only;
79         NTSTATUS result;
80         
81         zero_ip(&exclude_ip);
82
83         use_pdc_only = must_use_pdc(domain);
84         
85         /* Lookup domain controller name */
86            
87         if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) 
88         {
89                 DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
90                 
91                 /* check the connection cache and perform the node status 
92                    lookup only if the IP is not found to be bad */
93
94                 if (name_status_find(domain, 0x1b, 0x20, dc_ip, srv_name) ) {
95                         result = check_negative_conn_cache( domain, srv_name );
96                         if ( NT_STATUS_IS_OK(result) )
97                                 goto done;
98                 }
99                 /* Didn't get name, remember not to talk to this DC. */
100                 exclude_ip = dc_ip;
101         }
102
103         /* get a list of all domain controllers */
104         
105         if ( !get_sorted_dc_list(domain, &ip_list, &count, False) ) {
106                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
107                 return False;
108         }
109
110         /* Remove the entry we've already failed with (should be the PDC). */
111
112         if ( use_pdc_only ) {
113                 for (i = 0; i < count; i++) {   
114                         if (ip_equal( exclude_ip, ip_list[i].ip))
115                                 zero_ip(&ip_list[i].ip);
116                 }
117         }
118
119         for (i = 0; i < count; i++) {
120                 if (is_zero_ip(ip_list[i].ip))
121                         continue;
122
123                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i].ip, srv_name)) {
124                         result = check_negative_conn_cache( domain, srv_name );
125                         if ( NT_STATUS_IS_OK(result) ) {
126                                 dc_ip = ip_list[i].ip;
127                                 goto done;
128                         }
129                 }
130         }
131         
132
133         SAFE_FREE(ip_list);
134
135         /* No-one to talk to )-: */
136         return False;           /* Boo-hoo */
137         
138  done:
139         /* We have the netbios name and IP address of a domain controller.
140            Ideally we should sent a SAMLOGON request to determine whether
141            the DC is alive and kicking.  If we can catch a dead DC before
142            performing a cli_connect() we can avoid a 30-second timeout. */
143
144         DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
145                   inet_ntoa(dc_ip), domain));
146
147         *ip_out = dc_ip;
148
149         SAFE_FREE(ip_list);
150
151         return True;
152 }
153
154 /**********************************************************************
155  wrapper around ads and rpc methods of finds DC's
156 **********************************************************************/
157
158 BOOL get_dc_name(const char *domain, const char *realm, fstring srv_name, struct in_addr *ip_out)
159 {
160         struct in_addr dc_ip;
161         BOOL ret;
162         BOOL our_domain = False;
163
164         zero_ip(&dc_ip);
165
166         ret = False;
167         
168         if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
169                 our_domain = True;
170         
171         /* always try to obey what the admin specified in smb.conf 
172            (for the local domain) */
173         
174         if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
175                 ret = ads_dc_name(domain, realm, &dc_ip, srv_name);
176         }
177         
178         if (!ret) {
179                 /* fall back on rpc methods if the ADS methods fail */
180                 ret = rpc_dc_name(domain, srv_name, &dc_ip);
181         }
182                 
183         *ip_out = dc_ip;
184
185         return ret;
186 }
187