libsmb: Give dsgetdcname.c its own header
[metze/samba/wip.git] / source3 / lib / netapi / getdc.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi GetDC Support
4  *  Copyright (C) Guenther Deschner 2007
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21
22 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
23 #include "librpc/gen_ndr/libnetapi.h"
24 #include "lib/netapi/netapi.h"
25 #include "lib/netapi/netapi_private.h"
26 #include "lib/netapi/libnetapi.h"
27 #include "libsmb/dsgetdcname.h"
28
29 /********************************************************************
30 ********************************************************************/
31
32 WERROR NetGetDCName_l(struct libnetapi_ctx *ctx,
33                       struct NetGetDCName *r)
34 {
35         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetDCName);
36 }
37
38 /********************************************************************
39 ********************************************************************/
40
41 WERROR NetGetDCName_r(struct libnetapi_ctx *ctx,
42                       struct NetGetDCName *r)
43 {
44         NTSTATUS status;
45         WERROR werr;
46         struct dcerpc_binding_handle *b;
47         const char *dcname;
48         void *buffer;
49
50         werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
51                                             &ndr_table_netlogon,
52                                             &b);
53         if (!W_ERROR_IS_OK(werr)) {
54                 goto done;
55         }
56
57         status = dcerpc_netr_GetDcName(b, talloc_tos(),
58                                        r->in.server_name,
59                                        r->in.domain_name,
60                                        &dcname,
61                                        &werr);
62
63         if (!NT_STATUS_IS_OK(status)) {
64                 werr = ntstatus_to_werror(status);
65                 goto done;
66         }
67
68         if (!W_ERROR_IS_OK(werr)) {
69                 goto done;
70         }
71
72         if (NetApiBufferAllocate(strlen_m_term(dcname), &buffer)) {
73                 werr = WERR_NOT_ENOUGH_MEMORY;
74                 goto done;
75         }
76         memcpy(buffer, dcname, strlen_m_term(dcname));
77         *r->out.buffer = buffer;
78  done:
79
80         return werr;
81 }
82
83 /********************************************************************
84 ********************************************************************/
85
86 WERROR NetGetAnyDCName_l(struct libnetapi_ctx *ctx,
87                          struct NetGetAnyDCName *r)
88 {
89         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetAnyDCName);
90 }
91
92 /********************************************************************
93 ********************************************************************/
94
95 WERROR NetGetAnyDCName_r(struct libnetapi_ctx *ctx,
96                          struct NetGetAnyDCName *r)
97 {
98         NTSTATUS status;
99         WERROR werr;
100         struct dcerpc_binding_handle *b;
101         const char *dcname;
102         void *buffer;
103
104         werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
105                                             &ndr_table_netlogon,
106                                             &b);
107         if (!W_ERROR_IS_OK(werr)) {
108                 goto done;
109         }
110
111         status = dcerpc_netr_GetAnyDCName(b, talloc_tos(),
112                                           r->in.server_name,
113                                           r->in.domain_name,
114                                           &dcname,
115                                           &werr);
116         if (!NT_STATUS_IS_OK(status)) {
117                 werr = ntstatus_to_werror(status);
118                 goto done;
119         }
120
121         if (!W_ERROR_IS_OK(werr)) {
122                 goto done;
123         }
124
125         if (NetApiBufferAllocate(strlen_m_term(dcname), &buffer)) {
126                 werr = WERR_NOT_ENOUGH_MEMORY;
127                 goto done;
128         }
129         memcpy(buffer, dcname, strlen_m_term(dcname));
130         *r->out.buffer = buffer;
131
132  done:
133
134         return werr;
135
136 }
137
138 /********************************************************************
139 ********************************************************************/
140
141 WERROR DsGetDcName_l(struct libnetapi_ctx *ctx,
142                      struct DsGetDcName *r)
143 {
144         NTSTATUS status;
145         struct libnetapi_private_ctx *priv;
146
147         priv = talloc_get_type_abort(ctx->private_data,
148                 struct libnetapi_private_ctx);
149
150         status = dsgetdcname(ctx,
151                              priv->msg_ctx,
152                              r->in.domain_name,
153                              r->in.domain_guid,
154                              r->in.site_name,
155                              r->in.flags,
156                              (struct netr_DsRGetDCNameInfo **)r->out.dc_info);
157         if (!NT_STATUS_IS_OK(status)) {
158                 libnetapi_set_error_string(ctx,
159                         "failed to find DC: %s",
160                         get_friendly_nt_error_msg(status));
161         }
162
163         return ntstatus_to_werror(status);
164 }
165
166 /********************************************************************
167 ********************************************************************/
168
169 WERROR DsGetDcName_r(struct libnetapi_ctx *ctx,
170                      struct DsGetDcName *r)
171 {
172         WERROR werr;
173         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
174         struct dcerpc_binding_handle *b;
175
176         werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
177                                             &ndr_table_netlogon,
178                                             &b);
179         if (!W_ERROR_IS_OK(werr)) {
180                 goto done;
181         }
182
183         status = dcerpc_netr_DsRGetDCNameEx(b,
184                                             ctx,
185                                             r->in.server_name,
186                                             r->in.domain_name,
187                                             r->in.domain_guid,
188                                             r->in.site_name,
189                                             r->in.flags,
190                                             (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
191                                             &werr);
192         if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(werr)) {
193                 goto done;
194         }
195
196         status = dcerpc_netr_DsRGetDCName(b,
197                                           ctx,
198                                           r->in.server_name,
199                                           r->in.domain_name,
200                                           r->in.domain_guid,
201                                           NULL,
202                                           r->in.flags,
203                                           (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
204                                           &werr);
205         if (!NT_STATUS_IS_OK(status)) {
206                 werr = ntstatus_to_werror(status);
207                 goto done;
208         }
209
210  done:
211         return werr;
212 }