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"
32 struct dcesrv_remote_private
{
33 struct dcerpc_pipe
*c_pipe
;
36 static NTSTATUS
remote_op_reply(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, void *r
)
41 static NTSTATUS
remote_op_bind(struct dcesrv_call_state
*dce_call
, const struct dcesrv_interface
*iface
, uint32_t if_version
)
44 const struct ndr_interface_table
*table
;
45 struct dcesrv_remote_private
*priv
;
46 const char *binding
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "binding");
47 const char *user
, *pass
, *domain
;
48 struct cli_credentials
*credentials
;
49 bool must_free_credentials
= true;
51 struct dcerpc_binding
*b
;
52 struct composite_context
*pipe_conn_req
;
54 machine_account
= lpcfg_parm_bool(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "use_machine_account", false);
56 priv
= talloc(dce_call
->conn
, struct dcesrv_remote_private
);
58 return NT_STATUS_NO_MEMORY
;
62 dce_call
->context
->private_data
= priv
;
65 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
66 return NT_STATUS_INVALID_PARAMETER
;
69 user
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "user");
70 pass
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "password");
71 domain
= lpcfg_parm_string(dce_call
->conn
->dce_ctx
->lp_ctx
, NULL
, "dceprc_remote", "domain");
73 table
= ndr_table_by_uuid(&iface
->syntax_id
.uuid
); /* FIXME: What about if_version ? */
75 dce_call
->fault_code
= DCERPC_FAULT_UNK_IF
;
76 return NT_STATUS_NET_WRITE_FAULT
;
80 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
81 credentials
= cli_credentials_init(priv
);
83 return NT_STATUS_NO_MEMORY
;
85 cli_credentials_set_conf(credentials
, dce_call
->conn
->dce_ctx
->lp_ctx
);
86 cli_credentials_set_username(credentials
, user
, CRED_SPECIFIED
);
88 cli_credentials_set_domain(credentials
, domain
, CRED_SPECIFIED
);
90 cli_credentials_set_password(credentials
, pass
, CRED_SPECIFIED
);
91 } else if (machine_account
) {
92 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
93 credentials
= cli_credentials_init(priv
);
94 cli_credentials_set_conf(credentials
, dce_call
->conn
->dce_ctx
->lp_ctx
);
96 cli_credentials_set_domain(credentials
, domain
, CRED_SPECIFIED
);
98 status
= cli_credentials_set_machine_account(credentials
, dce_call
->conn
->dce_ctx
->lp_ctx
);
99 if (!NT_STATUS_IS_OK(status
)) {
102 } else if (dce_call
->conn
->auth_state
.session_info
->credentials
) {
103 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
104 credentials
= dce_call
->conn
->auth_state
.session_info
->credentials
;
105 must_free_credentials
= false;
107 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
108 return NT_STATUS_INVALID_PARAMETER
;
111 /* parse binding string to the structure */
112 status
= dcerpc_parse_binding(dce_call
->context
, binding
, &b
);
113 if (!NT_STATUS_IS_OK(status
)) {
114 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding
));
118 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call
->context
, b
)));
120 /* If we already have a remote association group ID, then use that */
121 if (dce_call
->context
->assoc_group
->proxied_id
!= 0) {
122 b
->assoc_group_id
= dce_call
->context
->assoc_group
->proxied_id
;
125 b
->object
.if_version
= if_version
;
127 pipe_conn_req
= dcerpc_pipe_connect_b_send(dce_call
->context
, b
, table
,
128 credentials
, dce_call
->event_ctx
, dce_call
->conn
->dce_ctx
->lp_ctx
);
129 status
= dcerpc_pipe_connect_b_recv(pipe_conn_req
, dce_call
->context
, &(priv
->c_pipe
));
131 if (must_free_credentials
) {
132 talloc_free(credentials
);
135 if (!NT_STATUS_IS_OK(status
)) {
139 if (dce_call
->context
->assoc_group
->proxied_id
== 0) {
140 dce_call
->context
->assoc_group
->proxied_id
= priv
->c_pipe
->assoc_group_id
;
143 if (!NT_STATUS_IS_OK(status
)) {
150 static NTSTATUS
remote_op_ndr_pull(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, struct ndr_pull
*pull
, void **r
)
152 enum ndr_err_code ndr_err
;
153 const struct ndr_interface_table
*table
= (const struct ndr_interface_table
*)dce_call
->context
->iface
->private_data
;
154 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
156 dce_call
->fault_code
= 0;
158 if (opnum
>= table
->num_calls
) {
159 dce_call
->fault_code
= DCERPC_FAULT_OP_RNG_ERROR
;
160 return NT_STATUS_NET_WRITE_FAULT
;
163 *r
= talloc_size(mem_ctx
, table
->calls
[opnum
].struct_size
);
165 return NT_STATUS_NO_MEMORY
;
168 /* unravel the NDR for the packet */
169 ndr_err
= table
->calls
[opnum
].ndr_pull(pull
, NDR_IN
, *r
);
170 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
171 dcerpc_log_packet(dce_call
->conn
->packet_log_dir
,
172 table
, opnum
, NDR_IN
,
173 &dce_call
->pkt
.u
.request
.stub_and_verifier
);
174 dce_call
->fault_code
= DCERPC_FAULT_NDR
;
175 return NT_STATUS_NET_WRITE_FAULT
;
181 static void remote_op_dispatch_done(struct tevent_req
*subreq
);
183 static NTSTATUS
remote_op_dispatch(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, void *r
)
185 struct dcesrv_remote_private
*priv
= talloc_get_type_abort(dce_call
->context
->private_data
,
186 struct dcesrv_remote_private
);
187 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
188 const struct ndr_interface_table
*table
= dce_call
->context
->iface
->private_data
;
189 const struct ndr_interface_call
*call
;
191 struct tevent_req
*subreq
;
193 name
= table
->calls
[opnum
].name
;
194 call
= &table
->calls
[opnum
];
196 if (priv
->c_pipe
->conn
->flags
& DCERPC_DEBUG_PRINT_IN
) {
197 ndr_print_function_debug(call
->ndr_print
, name
, NDR_IN
| NDR_SET_VALUES
, r
);
200 priv
->c_pipe
->conn
->flags
|= DCERPC_NDR_REF_ALLOC
;
202 /* we didn't use the return code of this function as we only check the last_fault_code */
203 subreq
= dcerpc_binding_handle_call_send(dce_call
, dce_call
->event_ctx
,
204 priv
->c_pipe
->binding_handle
,
207 if (subreq
== NULL
) {
208 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name
));
209 return NT_STATUS_NO_MEMORY
;
211 tevent_req_set_callback(subreq
, remote_op_dispatch_done
, dce_call
);
213 dce_call
->state_flags
|= DCESRV_CALL_STATE_FLAG_ASYNC
;
217 static void remote_op_dispatch_done(struct tevent_req
*subreq
)
219 struct dcesrv_call_state
*dce_call
= tevent_req_callback_data(subreq
,
220 struct dcesrv_call_state
);
221 struct dcesrv_remote_private
*priv
= talloc_get_type_abort(dce_call
->context
->private_data
,
222 struct dcesrv_remote_private
);
223 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
224 const struct ndr_interface_table
*table
= dce_call
->context
->iface
->private_data
;
225 const struct ndr_interface_call
*call
;
229 name
= table
->calls
[opnum
].name
;
230 call
= &table
->calls
[opnum
];
232 /* we didn't use the return code of this function as we only check the last_fault_code */
233 status
= dcerpc_binding_handle_call_recv(subreq
);
236 dce_call
->fault_code
= priv
->c_pipe
->last_fault_code
;
237 if (dce_call
->fault_code
!= 0) {
238 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
239 name
, dcerpc_errstr(dce_call
, dce_call
->fault_code
)));
243 if (NT_STATUS_IS_OK(status
) &&
244 (priv
->c_pipe
->conn
->flags
& DCERPC_DEBUG_PRINT_OUT
)) {
245 ndr_print_function_debug(call
->ndr_print
, name
, NDR_OUT
, dce_call
->r
);
249 status
= dcesrv_reply(dce_call
);
250 if (!NT_STATUS_IS_OK(status
)) {
251 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
252 name
, nt_errstr(status
)));
256 static NTSTATUS
remote_op_ndr_push(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, struct ndr_push
*push
, const void *r
)
258 enum ndr_err_code ndr_err
;
259 const struct ndr_interface_table
*table
= dce_call
->context
->iface
->private_data
;
260 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
262 /* unravel the NDR for the packet */
263 ndr_err
= table
->calls
[opnum
].ndr_push(push
, NDR_OUT
, r
);
264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
265 dce_call
->fault_code
= DCERPC_FAULT_NDR
;
266 return NT_STATUS_NET_WRITE_FAULT
;
272 static NTSTATUS
remote_register_one_iface(struct dcesrv_context
*dce_ctx
, const struct dcesrv_interface
*iface
)
275 const struct ndr_interface_table
*table
= iface
->private_data
;
277 for (i
=0;i
<table
->endpoints
->count
;i
++) {
279 const char *name
= table
->endpoints
->names
[i
];
281 ret
= dcesrv_interface_register(dce_ctx
, name
, iface
, NULL
);
282 if (!NT_STATUS_IS_OK(ret
)) {
283 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name
));
291 static NTSTATUS
remote_op_init_server(struct dcesrv_context
*dce_ctx
, const struct dcesrv_endpoint_server
*ep_server
)
294 const char **ifaces
= (const char **)str_list_make(dce_ctx
, lpcfg_parm_string(dce_ctx
->lp_ctx
, NULL
, "dcerpc_remote", "interfaces"),NULL
);
297 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
301 for (i
=0;ifaces
[i
];i
++) {
303 struct dcesrv_interface iface
;
305 if (!ep_server
->interface_by_name(&iface
, ifaces
[i
])) {
306 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces
[i
]));
308 return NT_STATUS_UNSUCCESSFUL
;
311 ret
= remote_register_one_iface(dce_ctx
, &iface
);
312 if (!NT_STATUS_IS_OK(ret
)) {
313 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces
[i
]));
323 static bool remote_fill_interface(struct dcesrv_interface
*iface
, const struct ndr_interface_table
*if_tabl
)
325 iface
->name
= if_tabl
->name
;
326 iface
->syntax_id
= if_tabl
->syntax_id
;
328 iface
->bind
= remote_op_bind
;
329 iface
->unbind
= NULL
;
331 iface
->ndr_pull
= remote_op_ndr_pull
;
332 iface
->dispatch
= remote_op_dispatch
;
333 iface
->reply
= remote_op_reply
;
334 iface
->ndr_push
= remote_op_ndr_push
;
336 iface
->private_data
= if_tabl
;
341 static bool remote_op_interface_by_uuid(struct dcesrv_interface
*iface
, const struct GUID
*uuid
, uint32_t if_version
)
343 const struct ndr_interface_list
*l
;
345 for (l
=ndr_table_list();l
;l
=l
->next
) {
346 if (l
->table
->syntax_id
.if_version
== if_version
&&
347 GUID_equal(&l
->table
->syntax_id
.uuid
, uuid
)==0) {
348 return remote_fill_interface(iface
, l
->table
);
355 static bool remote_op_interface_by_name(struct dcesrv_interface
*iface
, const char *name
)
357 const struct ndr_interface_table
*tbl
= ndr_table_by_name(name
);
360 return remote_fill_interface(iface
, tbl
);
365 NTSTATUS
dcerpc_server_remote_init(void)
368 struct dcesrv_endpoint_server ep_server
;
370 ZERO_STRUCT(ep_server
);
372 /* fill in our name */
373 ep_server
.name
= "remote";
375 /* fill in all the operations */
376 ep_server
.init_server
= remote_op_init_server
;
378 ep_server
.interface_by_uuid
= remote_op_interface_by_uuid
;
379 ep_server
.interface_by_name
= remote_op_interface_by_name
;
381 /* register ourselves with the DCERPC subsystem. */
382 ret
= dcerpc_register_ep_server(&ep_server
);
383 if (!NT_STATUS_IS_OK(ret
)) {
384 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
388 /* We need the full DCE/RPC interface table */