s3-netapi: use NetApiBufferAllocate() for returned buffer in getdc calls.
[ira/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
28 /********************************************************************
29 ********************************************************************/
30
31 WERROR NetGetDCName_l(struct libnetapi_ctx *ctx,
32                       struct NetGetDCName *r)
33 {
34         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetDCName);
35 }
36
37 /********************************************************************
38 ********************************************************************/
39
40 WERROR NetGetDCName_r(struct libnetapi_ctx *ctx,
41                       struct NetGetDCName *r)
42 {
43         NTSTATUS status;
44         WERROR werr;
45         struct dcerpc_binding_handle *b;
46         const char *dcname;
47         void *buffer;
48
49         werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
50                                             &ndr_table_netlogon.syntax_id,
51                                             &b);
52         if (!W_ERROR_IS_OK(werr)) {
53                 goto done;
54         }
55
56         status = dcerpc_netr_GetDcName(b, talloc_tos(),
57                                        r->in.server_name,
58                                        r->in.domain_name,
59                                        &dcname,
60                                        &werr);
61
62         if (!NT_STATUS_IS_OK(status)) {
63                 werr = ntstatus_to_werror(status);
64                 goto done;
65         }
66
67         if (!W_ERROR_IS_OK(werr)) {
68                 goto done;
69         }
70
71         if (NetApiBufferAllocate(strlen_m_term(dcname), &buffer)) {
72                 werr = WERR_NOMEM;
73                 goto done;
74         }
75         memcpy(buffer, dcname, strlen_m_term(dcname));
76         *r->out.buffer = buffer;
77  done:
78
79         return werr;
80 }
81
82 /********************************************************************
83 ********************************************************************/
84
85 WERROR NetGetAnyDCName_l(struct libnetapi_ctx *ctx,
86                          struct NetGetAnyDCName *r)
87 {
88         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetAnyDCName);
89 }
90
91 /********************************************************************
92 ********************************************************************/
93
94 WERROR NetGetAnyDCName_r(struct libnetapi_ctx *ctx,
95                          struct NetGetAnyDCName *r)
96 {
97         NTSTATUS status;
98         WERROR werr;
99         struct dcerpc_binding_handle *b;
100         const char *dcname;
101         void *buffer;
102
103         werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
104                                             &ndr_table_netlogon.syntax_id,
105                                             &b);
106         if (!W_ERROR_IS_OK(werr)) {
107                 goto done;
108         }
109
110         status = dcerpc_netr_GetAnyDCName(b, talloc_tos(),
111                                           r->in.server_name,
112                                           r->in.domain_name,
113                                           &dcname,
114                                           &werr);
115         if (!NT_STATUS_IS_OK(status)) {
116                 werr = ntstatus_to_werror(status);
117                 goto done;
118         }
119
120         if (!W_ERROR_IS_OK(werr)) {
121                 goto done;
122         }
123
124         if (NetApiBufferAllocate(strlen_m_term(dcname), &buffer)) {
125                 werr = WERR_NOMEM;
126                 goto done;
127         }
128         memcpy(buffer, dcname, strlen_m_term(dcname));
129         *r->out.buffer = buffer;
130
131  done:
132
133         return werr;
134
135 }
136
137 /********************************************************************
138 ********************************************************************/
139
140 WERROR DsGetDcName_l(struct libnetapi_ctx *ctx,
141                      struct DsGetDcName *r)
142 {
143         NTSTATUS status;
144         struct libnetapi_private_ctx *priv;
145
146         priv = talloc_get_type_abort(ctx->private_data,
147                 struct libnetapi_private_ctx);
148
149         status = dsgetdcname(ctx,
150                              priv->msg_ctx,
151                              r->in.domain_name,
152                              r->in.domain_guid,
153                              r->in.site_name,
154                              r->in.flags,
155                              (struct netr_DsRGetDCNameInfo **)r->out.dc_info);
156         if (!NT_STATUS_IS_OK(status)) {
157                 libnetapi_set_error_string(ctx,
158                         "failed to find DC: %s",
159                         get_friendly_nt_error_msg(status));
160         }
161
162         return ntstatus_to_werror(status);
163 }
164
165 /********************************************************************
166 ********************************************************************/
167
168 WERROR DsGetDcName_r(struct libnetapi_ctx *ctx,
169                      struct DsGetDcName *r)
170 {
171         WERROR werr;
172         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
173         struct dcerpc_binding_handle *b;
174
175         werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
176                                             &ndr_table_netlogon.syntax_id,
177                                             &b);
178         if (!W_ERROR_IS_OK(werr)) {
179                 goto done;
180         }
181
182         status = dcerpc_netr_DsRGetDCNameEx(b,
183                                             ctx,
184                                             r->in.server_name,
185                                             r->in.domain_name,
186                                             r->in.domain_guid,
187                                             r->in.site_name,
188                                             r->in.flags,
189                                             (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
190                                             &werr);
191         if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(werr)) {
192                 goto done;
193         }
194
195         status = dcerpc_netr_DsRGetDCName(b,
196                                           ctx,
197                                           r->in.server_name,
198                                           r->in.domain_name,
199                                           r->in.domain_guid,
200                                           NULL,
201                                           r->in.flags,
202                                           (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
203                                           &werr);
204         if (!NT_STATUS_IS_OK(status)) {
205                 werr = ntstatus_to_werror(status);
206                 goto done;
207         }
208
209  done:
210         return werr;
211 }