fix 2 bugs:
[tprouty/samba.git] / 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, struct in_addr *dc_ip, fstring srv_name)
33 {
34         ADS_STRUCT *ads;
35         const char *realm = domain;
36
37         if (strcasecmp(realm, lp_workgroup()) == 0)
38                 realm = lp_realm();
39
40         ads = ads_init(realm, domain, NULL);
41         if (!ads)
42                 return False;
43
44         /* we don't need to bind, just connect */
45         ads->auth.flags |= ADS_AUTH_NO_BIND;
46
47         DEBUG(4,("ads_dc_name: domain=%s\n", domain));
48
49 #ifdef HAVE_ADS
50         /* a full ads_connect() is actually overkill, as we don't srictly need
51            to do the SASL auth in order to get the info we need, but libads
52            doesn't offer a better way right now */
53         ads_connect(ads);
54 #endif
55
56         if (!ads->config.realm)
57                 return False;
58
59         fstrcpy(srv_name, ads->config.ldap_server_name);
60         strupper_m(srv_name);
61         *dc_ip = ads->ldap_ip;
62         ads_destroy(&ads);
63         
64         DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
65                  srv_name, inet_ntoa(*dc_ip)));
66         
67         return True;
68 }
69
70 /****************************************************************************
71  Utility function to return the name of a DC. The name is guaranteed to be 
72  valid since we have already done a name_status_find on it 
73  ***************************************************************************/
74
75 static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
76 {
77         struct ip_service *ip_list = NULL;
78         struct in_addr dc_ip, exclude_ip;
79         int count, i;
80         BOOL use_pdc_only;
81         NTSTATUS result;
82         
83         zero_ip(&exclude_ip);
84
85         use_pdc_only = must_use_pdc(domain);
86         
87         /* Lookup domain controller name */
88            
89         if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) 
90         {
91                 DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
92                 
93                 /* check the connection cache and perform the node status 
94                    lookup only if the IP is not found to be bad */
95
96                 if (name_status_find(domain, 0x1b, 0x20, dc_ip, srv_name) ) {
97                         result = check_negative_conn_cache( domain, srv_name );
98                         if ( NT_STATUS_IS_OK(result) )
99                                 goto done;
100                 }
101                 /* Didn't get name, remember not to talk to this DC. */
102                 exclude_ip = dc_ip;
103         }
104
105         /* get a list of all domain controllers */
106         
107         if ( !get_sorted_dc_list(domain, &ip_list, &count, False) ) {
108                 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
109                 return False;
110         }
111
112         /* Remove the entry we've already failed with (should be the PDC). */
113
114         if ( use_pdc_only ) {
115                 for (i = 0; i < count; i++) {   
116                         if (ip_equal( exclude_ip, ip_list[i].ip))
117                                 zero_ip(&ip_list[i].ip);
118                 }
119         }
120
121         for (i = 0; i < count; i++) {
122                 if (is_zero_ip(ip_list[i].ip))
123                         continue;
124
125                 if (name_status_find(domain, 0x1c, 0x20, ip_list[i].ip, srv_name)) {
126                         result = check_negative_conn_cache( domain, srv_name );
127                         if ( NT_STATUS_IS_OK(result) ) {
128                                 dc_ip = ip_list[i].ip;
129                                 goto done;
130                         }
131                 }
132         }
133         
134
135         SAFE_FREE(ip_list);
136
137         /* No-one to talk to )-: */
138         return False;           /* Boo-hoo */
139         
140  done:
141         /* We have the netbios name and IP address of a domain controller.
142            Ideally we should sent a SAMLOGON request to determine whether
143            the DC is alive and kicking.  If we can catch a dead DC before
144            performing a cli_connect() we can avoid a 30-second timeout. */
145
146         DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
147                   inet_ntoa(dc_ip), domain));
148
149         *ip_out = dc_ip;
150
151         SAFE_FREE(ip_list);
152
153         return True;
154 }
155
156 /**********************************************************************
157  wrapper around ads and rpc methods of finds DC's
158 **********************************************************************/
159
160 BOOL get_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
161 {
162         struct in_addr dc_ip;
163         BOOL ret;
164         BOOL our_domain = False;
165
166         zero_ip(&dc_ip);
167
168         ret = False;
169         
170         if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), domain) )
171                 our_domain = True;
172         
173         /* always try to obey what the admin specified in smb.conf.
174            If it is not our domain, assume that domain names with periods 
175            in them are realm names */
176         
177         if ( (our_domain && lp_security()==SEC_ADS) || strchr_m(domain, '.') ) {
178                 ret = ads_dc_name(domain, &dc_ip, srv_name);
179         }
180         
181         if (!ret) {
182                 /* fall back on rpc methods if the ADS methods fail */
183                 ret = rpc_dc_name(domain, srv_name, &dc_ip);
184         }
185                 
186         *ip_out = dc_ip;
187
188         return ret;
189 }
190