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/>.
25 #include "rpc_server/dcerpc_server.h"
26 #include "auth/auth.h"
27 #include "auth/credentials/credentials.h"
28 #include "librpc/ndr/ndr_table.h"
29 #include "param/param.h"
31 NTSTATUS
dcerpc_server_remote_init(void);
33 struct dcesrv_remote_private
{
34 struct dcerpc_pipe
*c_pipe
;
37 static NTSTATUS
remote_op_reply(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, void *r
)
42 static NTSTATUS
remote_op_bind(struct dcesrv_call_state
*dce_call
, const struct dcesrv_interface
*iface
, uint32_t if_version
)
45 const struct ndr_interface_table
*table
;
46 struct dcesrv_remote_private
*priv
;
47 const char *binding
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "binding");
48 const char *user
, *pass
, *domain
;
49 struct cli_credentials
*credentials
;
50 bool must_free_credentials
= true;
52 struct dcerpc_binding
*b
;
53 struct composite_context
*pipe_conn_req
;
56 machine_account
= lpcfg_parm_bool(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "use_machine_account", false);
58 priv
= talloc(dce_call
->conn
, struct dcesrv_remote_private
);
60 return NT_STATUS_NO_MEMORY
;
64 dce_call
->context
->private_data
= priv
;
67 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
68 return NT_STATUS_INVALID_PARAMETER
;
71 user
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "user");
72 pass
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "password");
73 domain
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dceprc_remote", "domain");
75 table
= ndr_table_by_syntax(&iface
->syntax_id
);
77 dce_call
->fault_code
= DCERPC_FAULT_UNK_IF
;
78 return NT_STATUS_NET_WRITE_FAULT
;
82 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
83 credentials
= cli_credentials_init(priv
);
85 return NT_STATUS_NO_MEMORY
;
87 cli_credentials_set_conf(credentials
, dce_call
->conn
->dce_ctx
->lp_ctx
);
88 cli_credentials_set_username(credentials
, user
, CRED_SPECIFIED
);
90 cli_credentials_set_domain(credentials
, domain
, CRED_SPECIFIED
);
92 cli_credentials_set_password(credentials
, pass
, CRED_SPECIFIED
);
93 } else if (machine_account
) {
94 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
95 credentials
= cli_credentials_init(priv
);
96 cli_credentials_set_conf(credentials
, dce_call
->conn
->dce_ctx
->lp_ctx
);
98 cli_credentials_set_domain(credentials
, domain
, CRED_SPECIFIED
);
100 status
= cli_credentials_set_machine_account(credentials
, dce_call
->conn
->dce_ctx
->lp_ctx
);
101 if (!NT_STATUS_IS_OK(status
)) {
104 } else if (dce_call
->conn
->auth_state
.session_info
->credentials
) {
105 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
106 credentials
= dce_call
->conn
->auth_state
.session_info
->credentials
;
107 must_free_credentials
= false;
109 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
110 return NT_STATUS_INVALID_PARAMETER
;
113 /* parse binding string to the structure */
114 status
= dcerpc_parse_binding(dce_call
->context
, binding
, &b
);
115 if (!NT_STATUS_IS_OK(status
)) {
116 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding
));
120 /* If we already have a remote association group ID, then use that */
121 if (dce_call
->conn
->assoc_group
->proxied_id
!= 0) {
122 status
= dcerpc_binding_set_assoc_group_id(b
,
123 dce_call
->conn
->assoc_group
->proxied_id
);
124 if (!NT_STATUS_IS_OK(status
)) {
125 DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
131 status
= dcerpc_binding_set_abstract_syntax(b
, &iface
->syntax_id
);
132 if (!NT_STATUS_IS_OK(status
)) {
133 DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
138 if (dce_call
->pkt
.pfc_flags
& DCERPC_PFC_FLAG_CONC_MPX
) {
139 status
= dcerpc_binding_set_flags(b
, DCERPC_CONCURRENT_MULTIPLEX
, 0);
140 if (!NT_STATUS_IS_OK(status
)) {
141 DEBUG(0, ("dcerpc_binding_set_flags(CONC_MPX) - %s'\n",
147 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call
->context
, b
)));
149 pipe_conn_req
= dcerpc_pipe_connect_b_send(dce_call
->context
, b
, table
,
150 credentials
, dce_call
->event_ctx
, dce_call
->conn
->dce_ctx
->lp_ctx
);
151 status
= dcerpc_pipe_connect_b_recv(pipe_conn_req
, dce_call
->context
, &(priv
->c_pipe
));
153 if (must_free_credentials
) {
154 talloc_free(credentials
);
157 if (!NT_STATUS_IS_OK(status
)) {
161 flags
= dcerpc_binding_get_flags(priv
->c_pipe
->binding
);
162 if (!(flags
& DCERPC_CONCURRENT_MULTIPLEX
)) {
163 dce_call
->state_flags
&= ~DCESRV_CALL_STATE_FLAG_MULTIPLEXED
;
166 if (dce_call
->conn
->assoc_group
->proxied_id
== 0) {
167 dce_call
->conn
->assoc_group
->proxied_id
=
168 dcerpc_binding_get_assoc_group_id(priv
->c_pipe
->binding
);
171 if (!NT_STATUS_IS_OK(status
)) {
178 static NTSTATUS
remote_op_ndr_pull(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, struct ndr_pull
*pull
, void **r
)
180 enum ndr_err_code ndr_err
;
181 const struct ndr_interface_table
*table
= (const struct ndr_interface_table
*)dce_call
->context
->iface
->private_data
;
182 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
184 dce_call
->fault_code
= 0;
186 if (opnum
>= table
->num_calls
) {
187 dce_call
->fault_code
= DCERPC_FAULT_OP_RNG_ERROR
;
188 return NT_STATUS_NET_WRITE_FAULT
;
192 * We don't have support for calls with pipes.
194 if (table
->calls
[opnum
].in_pipes
.num_pipes
!= 0) {
195 dce_call
->fault_code
= DCERPC_FAULT_OP_RNG_ERROR
;
196 return NT_STATUS_NET_WRITE_FAULT
;
198 if (table
->calls
[opnum
].out_pipes
.num_pipes
!= 0) {
199 dce_call
->fault_code
= DCERPC_FAULT_OP_RNG_ERROR
;
200 return NT_STATUS_NET_WRITE_FAULT
;
203 *r
= talloc_size(mem_ctx
, table
->calls
[opnum
].struct_size
);
205 return NT_STATUS_NO_MEMORY
;
208 /* unravel the NDR for the packet */
209 ndr_err
= table
->calls
[opnum
].ndr_pull(pull
, NDR_IN
, *r
);
210 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
211 dcerpc_log_packet(dce_call
->conn
->packet_log_dir
,
212 table
, opnum
, NDR_IN
,
213 &dce_call
->pkt
.u
.request
.stub_and_verifier
);
214 dce_call
->fault_code
= DCERPC_FAULT_NDR
;
215 return NT_STATUS_NET_WRITE_FAULT
;
221 static void remote_op_dispatch_done(struct tevent_req
*subreq
);
223 static NTSTATUS
remote_op_dispatch(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, void *r
)
225 struct dcesrv_remote_private
*priv
= talloc_get_type_abort(dce_call
->context
->private_data
,
226 struct dcesrv_remote_private
);
227 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
228 const struct ndr_interface_table
*table
= dce_call
->context
->iface
->private_data
;
229 const struct ndr_interface_call
*call
;
231 struct tevent_req
*subreq
;
233 name
= table
->calls
[opnum
].name
;
234 call
= &table
->calls
[opnum
];
236 if (priv
->c_pipe
->conn
->flags
& DCERPC_DEBUG_PRINT_IN
) {
237 ndr_print_function_debug(call
->ndr_print
, name
, NDR_IN
| NDR_SET_VALUES
, r
);
240 priv
->c_pipe
->conn
->flags
|= DCERPC_NDR_REF_ALLOC
;
242 /* we didn't use the return code of this function as we only check the last_fault_code */
243 subreq
= dcerpc_binding_handle_call_send(dce_call
, dce_call
->event_ctx
,
244 priv
->c_pipe
->binding_handle
,
247 if (subreq
== NULL
) {
248 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name
));
249 return NT_STATUS_NO_MEMORY
;
251 tevent_req_set_callback(subreq
, remote_op_dispatch_done
, dce_call
);
253 dce_call
->state_flags
|= DCESRV_CALL_STATE_FLAG_ASYNC
;
257 static void remote_op_dispatch_done(struct tevent_req
*subreq
)
259 struct dcesrv_call_state
*dce_call
= tevent_req_callback_data(subreq
,
260 struct dcesrv_call_state
);
261 struct dcesrv_remote_private
*priv
= talloc_get_type_abort(dce_call
->context
->private_data
,
262 struct dcesrv_remote_private
);
263 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
264 const struct ndr_interface_table
*table
= dce_call
->context
->iface
->private_data
;
265 const struct ndr_interface_call
*call
;
269 name
= table
->calls
[opnum
].name
;
270 call
= &table
->calls
[opnum
];
272 /* we didn't use the return code of this function as we only check the last_fault_code */
273 status
= dcerpc_binding_handle_call_recv(subreq
);
276 dce_call
->fault_code
= priv
->c_pipe
->last_fault_code
;
277 if (dce_call
->fault_code
!= 0) {
278 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
279 name
, dcerpc_errstr(dce_call
, dce_call
->fault_code
)));
283 if (NT_STATUS_IS_OK(status
) &&
284 (priv
->c_pipe
->conn
->flags
& DCERPC_DEBUG_PRINT_OUT
)) {
285 ndr_print_function_debug(call
->ndr_print
, name
, NDR_OUT
, dce_call
->r
);
289 status
= dcesrv_reply(dce_call
);
290 if (!NT_STATUS_IS_OK(status
)) {
291 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
292 name
, nt_errstr(status
)));
296 static NTSTATUS
remote_op_ndr_push(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, struct ndr_push
*push
, const void *r
)
298 enum ndr_err_code ndr_err
;
299 const struct ndr_interface_table
*table
= dce_call
->context
->iface
->private_data
;
300 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
302 /* unravel the NDR for the packet */
303 ndr_err
= table
->calls
[opnum
].ndr_push(push
, NDR_OUT
, r
);
304 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
305 dce_call
->fault_code
= DCERPC_FAULT_NDR
;
306 return NT_STATUS_NET_WRITE_FAULT
;
312 static NTSTATUS
remote_register_one_iface(struct dcesrv_context
*dce_ctx
, const struct dcesrv_interface
*iface
)
315 const struct ndr_interface_table
*table
= iface
->private_data
;
317 for (i
=0;i
<table
->endpoints
->count
;i
++) {
319 const char *name
= table
->endpoints
->names
[i
];
321 ret
= dcesrv_interface_register(dce_ctx
, name
, iface
, NULL
);
322 if (!NT_STATUS_IS_OK(ret
)) {
323 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name
));
331 static NTSTATUS
remote_op_init_server(struct dcesrv_context
*dce_ctx
, const struct dcesrv_endpoint_server
*ep_server
)
334 char **ifaces
= str_list_make(dce_ctx
, lpcfg_parm_string(dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "interfaces"),NULL
);
337 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
341 for (i
=0;ifaces
[i
];i
++) {
343 struct dcesrv_interface iface
;
345 if (!ep_server
->interface_by_name(&iface
, ifaces
[i
])) {
346 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces
[i
]));
348 return NT_STATUS_UNSUCCESSFUL
;
351 ret
= remote_register_one_iface(dce_ctx
, &iface
);
352 if (!NT_STATUS_IS_OK(ret
)) {
353 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces
[i
]));
363 static bool remote_fill_interface(struct dcesrv_interface
*iface
, const struct ndr_interface_table
*if_tabl
)
365 iface
->name
= if_tabl
->name
;
366 iface
->syntax_id
= if_tabl
->syntax_id
;
368 iface
->bind
= remote_op_bind
;
369 iface
->unbind
= NULL
;
371 iface
->ndr_pull
= remote_op_ndr_pull
;
372 iface
->dispatch
= remote_op_dispatch
;
373 iface
->reply
= remote_op_reply
;
374 iface
->ndr_push
= remote_op_ndr_push
;
376 iface
->private_data
= if_tabl
;
381 static bool remote_op_interface_by_uuid(struct dcesrv_interface
*iface
, const struct GUID
*uuid
, uint32_t if_version
)
383 const struct ndr_interface_list
*l
;
385 for (l
=ndr_table_list();l
;l
=l
->next
) {
386 if (l
->table
->syntax_id
.if_version
== if_version
&&
387 GUID_equal(&l
->table
->syntax_id
.uuid
, uuid
)==0) {
388 return remote_fill_interface(iface
, l
->table
);
395 static bool remote_op_interface_by_name(struct dcesrv_interface
*iface
, const char *name
)
397 const struct ndr_interface_table
*tbl
= ndr_table_by_name(name
);
400 return remote_fill_interface(iface
, tbl
);
405 NTSTATUS
dcerpc_server_remote_init(void)
408 struct dcesrv_endpoint_server ep_server
;
410 ZERO_STRUCT(ep_server
);
412 /* fill in our name */
413 ep_server
.name
= "remote";
415 /* fill in all the operations */
416 ep_server
.init_server
= remote_op_init_server
;
418 ep_server
.interface_by_uuid
= remote_op_interface_by_uuid
;
419 ep_server
.interface_by_name
= remote_op_interface_by_name
;
421 /* register ourselves with the DCERPC subsystem. */
422 ret
= dcerpc_register_ep_server(&ep_server
);
423 if (!NT_STATUS_IS_OK(ret
)) {
424 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
428 /* We need the full DCE/RPC interface table */