2 Unix SMB/CIFS implementation.
3 remote dcerpc operations
5 Copyright (C) Stefan (metze) Metzmacher 2004
6 Copyright (C) Julien Kerihuel 2008-2009
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2010
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.
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, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "auth/auth.h"
26 #include "auth/credentials/credentials.h"
27 #include "librpc/ndr/ndr_table.h"
28 #include "param/param.h"
31 struct dcesrv_remote_private {
32 struct dcerpc_pipe *c_pipe;
35 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
40 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
43 const struct ndr_interface_table *table;
44 struct dcesrv_remote_private *priv;
45 const char *binding = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "binding");
46 const char *user, *pass, *domain;
47 struct cli_credentials *credentials;
48 bool must_free_credentials = true;
50 struct dcerpc_binding *b;
51 struct composite_context *pipe_conn_req;
53 machine_account = lp_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "use_machine_account", false);
55 priv = talloc(dce_call->conn, struct dcesrv_remote_private);
57 return NT_STATUS_NO_MEMORY;
61 dce_call->context->private_data = priv;
64 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
65 return NT_STATUS_INVALID_PARAMETER;
68 user = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
69 pass = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
70 domain = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
72 table = ndr_table_by_uuid(&iface->syntax_id.uuid); /* FIXME: What about if_version ? */
74 dce_call->fault_code = DCERPC_FAULT_UNK_IF;
75 return NT_STATUS_NET_WRITE_FAULT;
79 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
80 credentials = cli_credentials_init(priv);
82 return NT_STATUS_NO_MEMORY;
84 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
85 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
87 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
89 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
90 } else if (machine_account) {
91 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
92 credentials = cli_credentials_init(priv);
93 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
95 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
97 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
98 if (!NT_STATUS_IS_OK(status)) {
101 } else if (dce_call->conn->auth_state.session_info->credentials) {
102 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
103 credentials = dce_call->conn->auth_state.session_info->credentials;
104 must_free_credentials = false;
106 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
107 return NT_STATUS_INVALID_PARAMETER;
110 /* parse binding string to the structure */
111 status = dcerpc_parse_binding(dce_call->context, binding, &b);
112 if (!NT_STATUS_IS_OK(status)) {
113 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
117 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
119 /* If we already have a remote association group ID, then use that */
120 if (dce_call->context->assoc_group->proxied_id != 0) {
121 b->assoc_group_id = dce_call->context->assoc_group->proxied_id;
124 pipe_conn_req = dcerpc_pipe_connect_b_send(dce_call->context, b, table,
125 credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
126 status = dcerpc_pipe_connect_b_recv(pipe_conn_req, dce_call->context, &(priv->c_pipe));
128 if (must_free_credentials) {
129 talloc_free(credentials);
132 if (!NT_STATUS_IS_OK(status)) {
136 if (dce_call->context->assoc_group->proxied_id == 0) {
137 dce_call->context->assoc_group->proxied_id = priv->c_pipe->assoc_group_id;
140 if (!NT_STATUS_IS_OK(status)) {
147 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
149 struct dcesrv_remote_private *priv = (struct dcesrv_remote_private *)context->private_data;
151 talloc_free(priv->c_pipe);
156 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
158 enum ndr_err_code ndr_err;
159 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
160 uint16_t opnum = dce_call->pkt.u.request.opnum;
162 dce_call->fault_code = 0;
164 if (opnum >= table->num_calls) {
165 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
166 return NT_STATUS_NET_WRITE_FAULT;
169 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
171 return NT_STATUS_NO_MEMORY;
174 /* unravel the NDR for the packet */
175 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 dcerpc_log_packet(dce_call->conn->packet_log_dir,
178 table, opnum, NDR_IN,
179 &dce_call->pkt.u.request.stub_and_verifier);
180 dce_call->fault_code = DCERPC_FAULT_NDR;
181 return NT_STATUS_NET_WRITE_FAULT;
187 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
189 struct dcesrv_remote_private *priv = dce_call->context->private_data;
190 uint16_t opnum = dce_call->pkt.u.request.opnum;
191 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
192 const struct ndr_interface_call *call;
195 name = table->calls[opnum].name;
196 call = &table->calls[opnum];
198 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
199 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
202 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
204 /* we didn't use the return code of this function as we only check the last_fault_code */
205 dcerpc_ndr_request(priv->c_pipe, NULL, table, opnum, mem_ctx,r);
207 dce_call->fault_code = priv->c_pipe->last_fault_code;
208 if (dce_call->fault_code != 0) {
209 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
210 return NT_STATUS_NET_WRITE_FAULT;
213 if ((dce_call->fault_code == 0) &&
214 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
215 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);
221 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
223 enum ndr_err_code ndr_err;
224 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
225 uint16_t opnum = dce_call->pkt.u.request.opnum;
227 /* unravel the NDR for the packet */
228 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
229 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
230 dce_call->fault_code = DCERPC_FAULT_NDR;
231 return NT_STATUS_NET_WRITE_FAULT;
237 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
240 const struct ndr_interface_table *table = iface->private_data;
242 for (i=0;i<table->endpoints->count;i++) {
244 const char *name = table->endpoints->names[i];
246 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
247 if (!NT_STATUS_IS_OK(ret)) {
248 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
256 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
259 const char **ifaces = (const char **)str_list_make(dce_ctx, lp_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
262 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
266 for (i=0;ifaces[i];i++) {
268 struct dcesrv_interface iface;
270 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
271 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
273 return NT_STATUS_UNSUCCESSFUL;
276 ret = remote_register_one_iface(dce_ctx, &iface);
277 if (!NT_STATUS_IS_OK(ret)) {
278 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
288 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
290 iface->name = if_tabl->name;
291 iface->syntax_id = if_tabl->syntax_id;
293 iface->bind = remote_op_bind;
294 iface->unbind = remote_op_unbind;
296 iface->ndr_pull = remote_op_ndr_pull;
297 iface->dispatch = remote_op_dispatch;
298 iface->reply = remote_op_reply;
299 iface->ndr_push = remote_op_ndr_push;
301 iface->private_data = if_tabl;
306 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
308 const struct ndr_interface_list *l;
310 for (l=ndr_table_list();l;l=l->next) {
311 if (l->table->syntax_id.if_version == if_version &&
312 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
313 return remote_fill_interface(iface, l->table);
320 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
322 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
325 return remote_fill_interface(iface, tbl);
330 NTSTATUS dcerpc_server_remote_init(void)
333 struct dcesrv_endpoint_server ep_server;
335 ZERO_STRUCT(ep_server);
337 /* fill in our name */
338 ep_server.name = "remote";
340 /* fill in all the operations */
341 ep_server.init_server = remote_op_init_server;
343 ep_server.interface_by_uuid = remote_op_interface_by_uuid;
344 ep_server.interface_by_name = remote_op_interface_by_name;
346 /* register ourselves with the DCERPC subsystem. */
347 ret = dcerpc_register_ep_server(&ep_server);
348 if (!NT_STATUS_IS_OK(ret)) {
349 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
353 /* We need the full DCE/RPC interface table */