Two trivial warnings
[Samba/gebeck_regimport.git] / source3 / libsmb / namequery_dc.c
blobdf7f856cd7b1837ee06d8bf7c955cc228dd1147d
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon connection manager
6 Copyright (C) Tim Potter 2001
7 Copyright (C) Andrew Bartlett 2002
8 Copyright (C) Gerald Carter 2003
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.
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.
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.
26 #include "includes.h"
28 /**************************************************************************
29 Find the name and IP address for a server in he realm/domain
30 *************************************************************************/
32 static BOOL ads_dc_name(const char *domain, struct in_addr *dc_ip, fstring srv_name)
34 ADS_STRUCT *ads;
35 const char *realm = domain;
37 if (strequal(realm, lp_workgroup()))
38 realm = lp_realm();
40 ads = ads_init(realm, domain, NULL);
41 if (!ads)
42 return False;
44 /* we don't need to bind, just connect */
45 ads->auth.flags |= ADS_AUTH_NO_BIND;
47 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
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
56 if (!ads->config.realm)
57 return False;
59 fstrcpy(srv_name, ads->config.ldap_server_name);
60 strupper_m(srv_name);
61 *dc_ip = ads->ldap_ip;
62 ads_destroy(&ads);
64 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
65 srv_name, inet_ntoa(*dc_ip)));
67 return True;
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 ***************************************************************************/
75 static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
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;
83 zero_ip(&exclude_ip);
85 use_pdc_only = must_use_pdc(domain);
87 /* Lookup domain controller name */
89 if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) )
91 DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
93 /* check the connection cache and perform the node status
94 lookup only if the IP is not found to be bad */
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;
101 /* Didn't get name, remember not to talk to this DC. */
102 exclude_ip = dc_ip;
105 /* get a list of all domain controllers */
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;
112 /* Remove the entry we've already failed with (should be the PDC). */
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);
121 for (i = 0; i < count; i++) {
122 if (is_zero_ip(ip_list[i].ip))
123 continue;
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;
135 SAFE_FREE(ip_list);
137 /* No-one to talk to )-: */
138 return False; /* Boo-hoo */
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. */
146 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
147 inet_ntoa(dc_ip), domain));
149 *ip_out = dc_ip;
151 SAFE_FREE(ip_list);
153 return True;
156 /**********************************************************************
157 wrapper around ads and rpc methods of finds DC's
158 **********************************************************************/
160 BOOL get_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
162 struct in_addr dc_ip;
163 BOOL ret;
164 BOOL our_domain = False;
166 zero_ip(&dc_ip);
168 ret = False;
170 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), domain) )
171 our_domain = True;
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 */
177 if ( (our_domain && lp_security()==SEC_ADS) || strchr_m(domain, '.') ) {
178 ret = ads_dc_name(domain, &dc_ip, srv_name);
181 if (!ret) {
182 /* fall back on rpc methods if the ADS methods fail */
183 ret = rpc_dc_name(domain, srv_name, &dc_ip);
186 *ip_out = dc_ip;
188 return ret;