wb-ndr: implement WINBIND_DC_INFO_LEVEL_COMPAT_DS in the locator child
[metze/samba/wb-ndr.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
30 static const struct winbindd_child_dispatch_table locator_dispatch_table[];
31
32 static struct winbindd_child static_locator_child;
33
34 void init_locator_child(void)
35 {
36         setup_child(&static_locator_child,
37                     locator_dispatch_table,
38                     "log.winbindd", "locator");
39 }
40
41 struct winbindd_child *locator_child(void)
42 {
43         return &static_locator_child;
44 }
45
46 void winbindd_dsgetdcname(struct winbindd_cli_state *state)
47 {
48         state->request.domain_name
49                 [sizeof(state->request.domain_name)-1] = '\0';
50
51         DEBUG(3, ("[%5lu]: dsgetdcname for %s\n", (unsigned long)state->pid,
52                   state->request.domain_name));
53
54         sendto_child(state, locator_child());
55 }
56
57 static enum winbindd_result dual_dsgetdcname(struct winbindd_domain *domain,
58                                              struct winbindd_cli_state *state)
59 {
60         NTSTATUS result;
61         struct netr_DsRGetDCNameInfo *info = NULL;
62         const char *dc = NULL;
63
64         state->request.domain_name
65                 [sizeof(state->request.domain_name)-1] = '\0';
66
67         DEBUG(3, ("[%5lu]: dsgetdcname for %s\n", (unsigned long)state->pid,
68                   state->request.domain_name));
69
70         result = dsgetdcname(state->mem_ctx, state->request.domain_name,
71                              NULL, NULL, state->request.flags, &info);
72
73         if (!NT_STATUS_IS_OK(result)) {
74                 return WINBINDD_ERROR;
75         }
76
77         if (info->dc_address) {
78                 dc = info->dc_address;
79                 if ((dc[0] == '\\') && (dc[1] == '\\')) {
80                         dc += 2;
81                 }
82         }
83
84         if ((!dc || !is_ipaddress_v4(dc)) && info->dc_unc) {
85                 dc = info->dc_unc;
86         }
87
88         if (!dc || !*dc) {
89                 return WINBINDD_ERROR;
90         }
91
92         fstrcpy(state->response.data.dc_name, dc);
93
94         return WINBINDD_OK;
95 }
96
97 static void ndr_child_get_dc_info_comapt_ds(struct winbindd_domain *domain,
98                                             struct winbindd_cli_state *state,
99                                             struct winbind_get_dc_info *r)
100 {
101         NTSTATUS result;
102         struct DS_DOMAIN_CONTROLLER_INFO *info = NULL;
103         const char *dc = NULL;
104
105         DEBUG(3, ("dsgetdcname for '%s'\n", r->in.domain_name));
106
107         result = dsgetdcname(state->mem_ctx, r->in.domain_name,
108                              NULL, NULL, r->in.params.flags, &info);
109         if (!NT_STATUS_IS_OK(result)) {
110                 DEBUG(1, ("dsgetdcname failed: %s\n", nt_errstr(result)));
111                 r->out.result = WINBIND_STATUS_FOOBAR;
112                 return;
113         }
114
115         if (info->domain_controller_address) {
116                 dc = info->domain_controller_address;
117                 if ((dc[0] == '\\') && (dc[1] == '\\')) {
118                         dc += 2;
119                 }
120         }
121
122         if ((!dc || !is_ipaddress_v4(dc)) && info->domain_controller_name) {
123                 dc = info->domain_controller_name;
124         }
125
126         if (!dc || !*dc) {
127                 TALLOC_FREE(info);
128                 DEBUG(1, ("DsGetDcName failed: no dc name\n"));
129                 r->out.result = WINBIND_STATUS_FOOBAR;
130                 return;
131         }
132
133         r->out.dc_info->name = talloc_strdup(r, dc);
134         TALLOC_FREE(info);
135         if (!r->out.dc_info->name) {
136                 r->out.result = WINBIND_STATUS_NO_MEMORY;
137                 return;
138         }
139         r->out.result = WINBIND_STATUS_OK;
140 }
141
142 void winbindd_ndr_locator_child_get_dc_info(struct winbindd_domain *domain,
143                                             struct winbindd_cli_state *state)
144 {
145         struct winbind_get_dc_info *r;
146
147         r = talloc_get_type_abort(state->c.ndr.r,
148                                   struct winbind_get_dc_info);
149
150         switch (*r->in.level) {
151         case WINBIND_DC_INFO_LEVEL_COMPAT_NT4:
152                 r->out.result = WINBIND_STATUS_INVALID_LEVEL;
153                 return;
154
155         case WINBIND_DC_INFO_LEVEL_COMPAT_DS:
156                 ndr_child_get_dc_info_comapt_ds(domain, state, r);
157                 return;
158         }
159
160         r->out.result = WINBIND_STATUS_UNKNOWN_LEVEL;
161         return;
162 }
163
164 static const struct winbindd_child_dispatch_table locator_dispatch_table[] = {
165         {
166                 .name           = "DSGETDCNAME",
167                 .struct_cmd     = WINBINDD_DSGETDCNAME,
168                 .struct_fn      = dual_dsgetdcname,
169         },{
170                 .name           = "NDR_WINBIND_GET_DC_INFO",
171                 .ndr_opnum      = NDR_WINBIND_GET_DC_INFO,
172                 .ndr_fn         = winbindd_ndr_locator_child_get_dc_info,
173         },{
174                 .name           = NULL,
175         }
176 };