2 Unix SMB/CIFS implementation.
3 remote dcerpc operations
5 Copyright (C) Stefan (metze) Metzmacher 2004
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "rpc_server/dcerpc_server.h"
23 #include "auth/auth.h"
24 #include "auth/credentials/credentials.h"
25 #include "librpc/ndr/ndr_table.h"
26 #include "param/param.h"
29 struct dcesrv_remote_private {
30 struct dcerpc_pipe *c_pipe;
33 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
38 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
41 const struct ndr_interface_table *table;
42 struct dcesrv_remote_private *private;
43 const char *binding = lp_parm_string(global_loadparm, NULL, "dcerpc_remote", "binding");
44 const char *user, *pass, *domain;
45 struct cli_credentials *credentials;
48 machine_account = lp_parm_bool(global_loadparm, NULL, "dcerpc_remote", "use_machine_account", false);
50 private = talloc(dce_call->conn, struct dcesrv_remote_private);
52 return NT_STATUS_NO_MEMORY;
55 private->c_pipe = NULL;
56 dce_call->context->private = private;
59 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
60 return NT_STATUS_INVALID_PARAMETER;
63 user = lp_parm_string(global_loadparm, NULL, "dcerpc_remote", "user");
64 pass = lp_parm_string(global_loadparm, NULL, "dcerpc_remote", "password");
65 domain = lp_parm_string(global_loadparm, NULL, "dceprc_remote", "domain");
67 table = ndr_table_by_uuid(&iface->syntax_id.uuid); /* FIXME: What about if_version ? */
69 dce_call->fault_code = DCERPC_FAULT_UNK_IF;
70 return NT_STATUS_NET_WRITE_FAULT;
74 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
75 credentials = cli_credentials_init(private);
77 return NT_STATUS_NO_MEMORY;
79 cli_credentials_set_conf(credentials, global_loadparm);
80 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
82 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
84 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
85 } else if (machine_account) {
86 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
87 credentials = cli_credentials_init(private);
88 cli_credentials_set_conf(credentials, global_loadparm);
90 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
92 status = cli_credentials_set_machine_account(credentials);
93 if (!NT_STATUS_IS_OK(status)) {
96 } else if (dce_call->conn->auth_state.session_info->credentials) {
97 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
98 credentials = dce_call->conn->auth_state.session_info->credentials;
100 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
101 return NT_STATUS_INVALID_PARAMETER;
104 status = dcerpc_pipe_connect(private,
105 &(private->c_pipe), binding, table,
106 credentials, dce_call->event_ctx);
108 talloc_free(credentials);
109 if (!NT_STATUS_IS_OK(status)) {
116 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
118 struct dcesrv_remote_private *private = (struct dcesrv_remote_private *)context->private;
120 talloc_free(private->c_pipe);
125 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
128 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private;
129 uint16_t opnum = dce_call->pkt.u.request.opnum;
131 dce_call->fault_code = 0;
133 if (opnum >= table->num_calls) {
134 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
135 return NT_STATUS_NET_WRITE_FAULT;
138 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
140 return NT_STATUS_NO_MEMORY;
143 /* unravel the NDR for the packet */
144 status = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
145 if (!NT_STATUS_IS_OK(status)) {
146 dcerpc_log_packet(table, opnum, NDR_IN,
147 &dce_call->pkt.u.request.stub_and_verifier);
148 dce_call->fault_code = DCERPC_FAULT_NDR;
149 return NT_STATUS_NET_WRITE_FAULT;
155 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
157 struct dcesrv_remote_private *private = dce_call->context->private;
158 uint16_t opnum = dce_call->pkt.u.request.opnum;
159 const struct ndr_interface_table *table = dce_call->context->iface->private;
160 const struct ndr_interface_call *call;
163 name = table->calls[opnum].name;
164 call = &table->calls[opnum];
166 if (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
167 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
170 private->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
172 /* we didn't use the return code of this function as we only check the last_fault_code */
173 dcerpc_ndr_request(private->c_pipe, NULL, table, opnum, mem_ctx,r);
175 dce_call->fault_code = private->c_pipe->last_fault_code;
176 if (dce_call->fault_code != 0) {
177 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
178 return NT_STATUS_NET_WRITE_FAULT;
181 if ((dce_call->fault_code == 0) &&
182 (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
183 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);
189 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
192 const struct ndr_interface_table *table = dce_call->context->iface->private;
193 uint16_t opnum = dce_call->pkt.u.request.opnum;
195 /* unravel the NDR for the packet */
196 status = table->calls[opnum].ndr_push(push, NDR_OUT, r);
197 if (!NT_STATUS_IS_OK(status)) {
198 dce_call->fault_code = DCERPC_FAULT_NDR;
199 return NT_STATUS_NET_WRITE_FAULT;
205 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
208 const struct ndr_interface_table *table = iface->private;
210 for (i=0;i<table->endpoints->count;i++) {
212 const char *name = table->endpoints->names[i];
214 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
215 if (!NT_STATUS_IS_OK(ret)) {
216 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
224 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
227 const char **ifaces = str_list_make(dce_ctx, lp_parm_string(global_loadparm, NULL, "dcerpc_remote", "interfaces"),NULL);
230 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
234 for (i=0;ifaces[i];i++) {
236 struct dcesrv_interface iface;
238 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
239 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
241 return NT_STATUS_UNSUCCESSFUL;
244 ret = remote_register_one_iface(dce_ctx, &iface);
245 if (!NT_STATUS_IS_OK(ret)) {
246 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
256 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
258 iface->name = if_tabl->name;
259 iface->syntax_id = if_tabl->syntax_id;
261 iface->bind = remote_op_bind;
262 iface->unbind = remote_op_unbind;
264 iface->ndr_pull = remote_op_ndr_pull;
265 iface->dispatch = remote_op_dispatch;
266 iface->reply = remote_op_reply;
267 iface->ndr_push = remote_op_ndr_push;
269 iface->private = if_tabl;
274 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
276 const struct ndr_interface_list *l;
278 for (l=ndr_table_list();l;l=l->next) {
279 if (l->table->syntax_id.if_version == if_version &&
280 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
281 return remote_fill_interface(iface, l->table);
288 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
290 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
293 return remote_fill_interface(iface, tbl);
298 NTSTATUS dcerpc_server_remote_init(void)
301 struct dcesrv_endpoint_server ep_server;
303 ZERO_STRUCT(ep_server);
305 /* fill in our name */
306 ep_server.name = "remote";
308 /* fill in all the operations */
309 ep_server.init_server = remote_op_init_server;
311 ep_server.interface_by_uuid = remote_op_interface_by_uuid;
312 ep_server.interface_by_name = remote_op_interface_by_name;
314 /* register ourselves with the DCERPC subsystem. */
315 ret = dcerpc_register_ep_server(&ep_server);
316 if (!NT_STATUS_IS_OK(ret)) {
317 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
321 /* We need the full DCE/RPC interface table */