52b6dc06f13ba6438bcf1a5a837299bd6ba4258d
[samba.git] / source / winbindd / winbindd_locator.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - miscellaneous other functions
5
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Andrew Bartlett 2002
8
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 3 of the License, or
12    (at your option) any later version.
13
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.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 static struct winbindd_child static_locator_child;
30
31 void init_locator_child(void)
32 {
33         setup_domain_child(NULL, &static_locator_child, "locator");
34 }
35
36 struct winbindd_child *locator_child(void)
37 {
38         return &static_locator_child;
39 }
40
41 void winbindd_dsgetdcname(struct winbindd_cli_state *state)
42 {
43         state->request.domain_name
44                 [sizeof(state->request.domain_name)-1] = '\0';
45
46         DEBUG(3, ("[%5lu]: DsGetDcName for %s\n", (unsigned long)state->pid,
47                   state->request.domain_name));
48
49         sendto_child(state, locator_child());
50 }
51
52 enum winbindd_result winbindd_dual_dsgetdcname(struct winbindd_domain *domain,
53                                                struct winbindd_cli_state *state)
54 {
55         NTSTATUS result;
56         struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
57         const char *dc = NULL;
58
59         state->request.domain_name
60                 [sizeof(state->request.domain_name)-1] = '\0';
61
62         DEBUG(3, ("[%5lu]: DsGetDcName for %s\n", (unsigned long)state->pid,
63                   state->request.domain_name));
64
65         result = DsGetDcName(state->mem_ctx, NULL, state->request.domain_name,
66                              NULL, NULL, state->request.flags, &info);
67
68         if (!NT_STATUS_IS_OK(result)) {
69                 return WINBINDD_ERROR;
70         }
71
72         if (info->domain_controller_address) {
73                 dc = info->domain_controller_address;
74                 if ((dc[0] == '\\') && (dc[1] == '\\')) {
75                         dc += 2;
76                 }
77         }
78
79         if ((!dc || !is_ipaddress(dc)) && info->domain_controller_name) {
80                 dc = info->domain_controller_name;
81         }
82
83         if (!dc || !*dc) {
84                 return WINBINDD_ERROR;
85         }
86
87         fstrcpy(state->response.data.dc_name, dc);
88
89         return WINBINDD_OK;
90 }