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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "rpc_server/dcerpc_server.h"
24 #include "credentials.h"
26 struct dcesrv_remote_private {
27 struct dcerpc_pipe *c_pipe;
30 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
33 struct dcesrv_remote_private *private;
34 const char *binding = lp_parm_string(-1, "dcerpc_remote", "binding");
35 struct cli_credentials *credentials;
38 DEBUG(0,("You must specify a ncacn binding string\n"));
39 return NT_STATUS_INVALID_PARAMETER;
42 private = talloc(dce_call->conn, struct dcesrv_remote_private);
44 return NT_STATUS_NO_MEMORY;
47 credentials = cli_credentials_init(private);
49 cli_credentials_set_username(credentials, lp_parm_string(-1, "dcerpc_remote", "username"), CRED_SPECIFIED);
50 cli_credentials_set_workstation(credentials, lp_netbios_name(), CRED_SPECIFIED);
51 cli_credentials_set_domain(credentials, lp_workgroup(), CRED_SPECIFIED);
52 cli_credentials_set_password(credentials, lp_parm_string(-1, "dcerpc_remote", "password"), CRED_SPECIFIED);
54 status = dcerpc_pipe_connect(private,
55 &(private->c_pipe), binding,
56 iface->uuid, iface->if_version,
57 credentials, dce_call->event_ctx);
59 talloc_free(credentials);
60 if (!NT_STATUS_IS_OK(status)) {
64 dce_call->context->private = private;
69 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
71 struct dcesrv_remote_private *private = context->private;
73 talloc_free(private->c_pipe);
78 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
81 const struct dcerpc_interface_table *table = dce_call->context->iface->private;
82 uint16_t opnum = dce_call->pkt.u.request.opnum;
84 dce_call->fault_code = 0;
86 if (opnum >= table->num_calls) {
87 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
88 return NT_STATUS_NET_WRITE_FAULT;
91 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
93 return NT_STATUS_NO_MEMORY;
96 /* unravel the NDR for the packet */
97 status = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
98 if (!NT_STATUS_IS_OK(status)) {
99 dcerpc_log_packet(table, opnum, NDR_IN,
100 &dce_call->pkt.u.request.stub_and_verifier);
101 dce_call->fault_code = DCERPC_FAULT_NDR;
102 return NT_STATUS_NET_WRITE_FAULT;
108 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
110 struct dcesrv_remote_private *private = dce_call->context->private;
111 uint16_t opnum = dce_call->pkt.u.request.opnum;
112 const struct dcerpc_interface_table *table = dce_call->context->iface->private;
113 const struct dcerpc_interface_call *call;
116 name = table->calls[opnum].name;
117 call = &table->calls[opnum];
119 if (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
120 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
123 /* we didn't use the return code of this function as we only check the last_fault_code */
124 dcerpc_ndr_request(private->c_pipe, NULL, table, opnum, mem_ctx,r);
126 dce_call->fault_code = private->c_pipe->last_fault_code;
127 if (dce_call->fault_code != 0) {
128 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
129 return NT_STATUS_NET_WRITE_FAULT;
132 if ((dce_call->fault_code == 0) &&
133 (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
134 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);
140 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
143 const struct dcerpc_interface_table *table = dce_call->context->iface->private;
144 uint16_t opnum = dce_call->pkt.u.request.opnum;
146 /* unravel the NDR for the packet */
147 status = table->calls[opnum].ndr_push(push, NDR_OUT, r);
148 if (!NT_STATUS_IS_OK(status)) {
149 dce_call->fault_code = DCERPC_FAULT_NDR;
150 return NT_STATUS_NET_WRITE_FAULT;
156 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
159 const struct dcerpc_interface_table *table = iface->private;
161 for (i=0;i<table->endpoints->count;i++) {
163 const char *name = table->endpoints->names[i];
165 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
166 if (!NT_STATUS_IS_OK(ret)) {
167 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
175 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
178 const char **ifaces = str_list_make(dce_ctx, lp_parm_string(-1,"dcerpc_remote","interfaces"),NULL);
181 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
185 for (i=0;ifaces[i];i++) {
187 struct dcesrv_interface iface;
189 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
190 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
192 return NT_STATUS_UNSUCCESSFUL;
195 ret = remote_register_one_iface(dce_ctx, &iface);
196 if (!NT_STATUS_IS_OK(ret)) {
197 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
207 static BOOL remote_fill_interface(struct dcesrv_interface *iface, const struct dcerpc_interface_table *if_tabl)
209 iface->name = if_tabl->name;
210 iface->uuid = if_tabl->uuid;
211 iface->if_version = if_tabl->if_version;
213 iface->bind = remote_op_bind;
214 iface->unbind = remote_op_unbind;
216 iface->ndr_pull = remote_op_ndr_pull;
217 iface->dispatch = remote_op_dispatch;
218 iface->ndr_push = remote_op_ndr_push;
220 iface->private = if_tabl;
225 static BOOL remote_op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
227 const struct dcerpc_interface_list *l;
229 for (l=librpc_dcerpc_pipes();l;l=l->next) {
230 if (l->table->if_version == if_version &&
231 strcmp(l->table->uuid, uuid)==0) {
232 return remote_fill_interface(iface, l->table);
239 static BOOL remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
241 const struct dcerpc_interface_list *l;
243 for (l=librpc_dcerpc_pipes();l;l=l->next) {
244 if (strcmp(l->table->name, name)==0) {
245 return remote_fill_interface(iface, l->table);
252 NTSTATUS dcerpc_server_remote_init(void)
255 struct dcesrv_endpoint_server ep_server;
257 ZERO_STRUCT(ep_server);
259 /* fill in our name */
260 ep_server.name = "remote";
262 /* fill in all the operations */
263 ep_server.init_server = remote_op_init_server;
265 ep_server.interface_by_uuid = remote_op_interface_by_uuid;
266 ep_server.interface_by_name = remote_op_interface_by_name;
268 /* register ourselves with the DCERPC subsystem. */
269 ret = dcerpc_register_ep_server(&ep_server);
270 if (!NT_STATUS_IS_OK(ret)) {
271 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));