Make sure we don't clobber the stack when response consists of the empty
[Samba/gebeck_regimport.git] / source3 / libsmb / namequery_dc.c
blob31d759e0d2cde636b52501ee99fd819738cfd569
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, const char *realm, struct in_addr *dc_ip, fstring srv_name)
34 ADS_STRUCT *ads;
36 if (!realm && strequal(domain, lp_workgroup()))
37 realm = lp_realm();
39 ads = ads_init(realm, domain, NULL);
40 if (!ads)
41 return False;
43 DEBUG(4,("ads_dc_name: domain=%s\n", domain));
45 #ifdef HAVE_ADS
46 /* we don't need to bind, just connect */
47 ads->auth.flags |= ADS_AUTH_NO_BIND;
49 ads_connect(ads);
50 #endif
52 if (!ads->config.realm)
53 return False;
55 fstrcpy(srv_name, ads->config.ldap_server_name);
56 strupper_m(srv_name);
57 *dc_ip = ads->ldap_ip;
58 ads_destroy(&ads);
60 DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n",
61 srv_name, inet_ntoa(*dc_ip)));
63 return True;
66 /****************************************************************************
67 Utility function to return the name of a DC. The name is guaranteed to be
68 valid since we have already done a name_status_find on it
69 ***************************************************************************/
71 static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
73 struct ip_service *ip_list = NULL;
74 struct in_addr dc_ip, exclude_ip;
75 int count, i;
76 BOOL use_pdc_only;
77 NTSTATUS result;
79 zero_ip(&exclude_ip);
81 use_pdc_only = must_use_pdc(domain);
83 /* Lookup domain controller name */
85 if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) )
87 DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
89 /* check the connection cache and perform the node status
90 lookup only if the IP is not found to be bad */
92 if (name_status_find(domain, 0x1b, 0x20, dc_ip, srv_name) ) {
93 result = check_negative_conn_cache( domain, srv_name );
94 if ( NT_STATUS_IS_OK(result) )
95 goto done;
97 /* Didn't get name, remember not to talk to this DC. */
98 exclude_ip = dc_ip;
101 /* get a list of all domain controllers */
103 if ( !get_sorted_dc_list(domain, &ip_list, &count, False) ) {
104 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
105 return False;
108 /* Remove the entry we've already failed with (should be the PDC). */
110 if ( use_pdc_only ) {
111 for (i = 0; i < count; i++) {
112 if (ip_equal( exclude_ip, ip_list[i].ip))
113 zero_ip(&ip_list[i].ip);
117 for (i = 0; i < count; i++) {
118 if (is_zero_ip(ip_list[i].ip))
119 continue;
121 if (name_status_find(domain, 0x1c, 0x20, ip_list[i].ip, srv_name)) {
122 result = check_negative_conn_cache( domain, srv_name );
123 if ( NT_STATUS_IS_OK(result) ) {
124 dc_ip = ip_list[i].ip;
125 goto done;
131 SAFE_FREE(ip_list);
133 /* No-one to talk to )-: */
134 return False; /* Boo-hoo */
136 done:
137 /* We have the netbios name and IP address of a domain controller.
138 Ideally we should sent a SAMLOGON request to determine whether
139 the DC is alive and kicking. If we can catch a dead DC before
140 performing a cli_connect() we can avoid a 30-second timeout. */
142 DEBUG(3, ("rpc_dc_name: Returning DC %s (%s) for domain %s\n", srv_name,
143 inet_ntoa(dc_ip), domain));
145 *ip_out = dc_ip;
147 SAFE_FREE(ip_list);
149 return True;
152 /**********************************************************************
153 wrapper around ads and rpc methods of finds DC's
154 **********************************************************************/
156 BOOL get_dc_name(const char *domain, const char *realm, fstring srv_name, struct in_addr *ip_out)
158 struct in_addr dc_ip;
159 BOOL ret;
160 BOOL our_domain = False;
162 zero_ip(&dc_ip);
164 ret = False;
166 if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
167 our_domain = True;
169 /* always try to obey what the admin specified in smb.conf
170 (for the local domain) */
172 if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
173 ret = ads_dc_name(domain, realm, &dc_ip, srv_name);
176 if (!ret) {
177 /* fall back on rpc methods if the ADS methods fail */
178 ret = rpc_dc_name(domain, srv_name, &dc_ip);
181 *ip_out = dc_ip;
183 return ret;