Store printer guid in the dsspooler registry key so we don't have to
[Samba/gebeck_regimport.git] / source / libsmb / namequery_dc.c
blobffc64139e9b5ee9506bbc32c3f7ad4ffbb553a63
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon connection manager
6 Copyright (C) Tim Potter 2001
7 Copyright (C) Andrew Bartlett 2002
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.
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.
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.
25 #include "includes.h"
29 find the DC for a domain using methods appropriate for a RPC domain
31 BOOL rpc_find_dc(const char *domain, fstring srv_name, struct in_addr *ip_out)
33 struct in_addr *ip_list = NULL, dc_ip, exclude_ip;
34 int count, i;
35 BOOL list_ordered;
36 BOOL use_pdc_only;
38 zero_ip(&exclude_ip);
40 use_pdc_only = must_use_pdc(domain);
42 /* Lookup domain controller name */
44 if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) {
45 DEBUG(10,("rpc_find_dc: Atempting to lookup PDC to avoid sam sync delays\n"));
47 if (name_status_find(domain, 0x1c, 0x20, dc_ip, srv_name)) {
48 goto done;
50 /* Didn't get name, remember not to talk to this DC. */
51 exclude_ip = dc_ip;
54 /* get a list of all domain controllers */
56 if (!get_dc_list( domain, &ip_list, &count, &list_ordered) ) {
57 DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
58 return False;
61 /* Remove the entry we've already failed with (should be the PDC). */
63 if ( use_pdc_only ) {
64 for (i = 0; i < count; i++) {
65 if (ip_equal( exclude_ip, ip_list[i]))
66 zero_ip(&ip_list[i]);
70 /* Pick a nice close server, but only if the list was not ordered */
71 if (!list_ordered && (count > 1) ) {
72 qsort(ip_list, count, sizeof(struct in_addr), QSORT_CAST ip_compare);
75 for (i = 0; i < count; i++) {
76 if (is_zero_ip(ip_list[i]))
77 continue;
79 if (name_status_find(domain, 0x1c, 0x20, ip_list[i], srv_name)) {
80 dc_ip = ip_list[i];
81 goto done;
86 SAFE_FREE(ip_list);
88 return False;
89 done:
90 /* We have the netbios name and IP address of a domain controller.
91 Ideally we should sent a SAMLOGON request to determine whether
92 the DC is alive and kicking. If we can catch a dead DC before
93 performing a cli_connect() we can avoid a 30-second timeout. */
95 DEBUG(3, ("rpc_find_dc: Returning DC %s (%s) for domain %s\n", srv_name,
96 inet_ntoa(dc_ip), domain));
98 *ip_out = dc_ip;
100 SAFE_FREE(ip_list);
102 return True;