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"
25 struct dcesrv_remote_private {
26 struct dcerpc_pipe *c_pipe;
29 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
32 struct dcesrv_remote_private *private;
33 const char *binding = lp_parm_string(-1, "dcerpc_remote", "binding");
34 struct cli_credentials *credentials;
37 DEBUG(0,("You must specify a ncacn binding string\n"));
38 return NT_STATUS_INVALID_PARAMETER;
41 private = talloc(dce_call->conn, struct dcesrv_remote_private);
43 return NT_STATUS_NO_MEMORY;
46 credentials = cli_credentials_init(private);
48 cli_credentials_set_username(credentials, lp_parm_string(-1, "dcerpc_remote", "username"), CRED_SPECIFIED);
49 cli_credentials_set_workstation(credentials, lp_netbios_name(), CRED_SPECIFIED);
50 cli_credentials_set_domain(credentials, lp_workgroup(), CRED_SPECIFIED);
51 cli_credentials_set_password(credentials, lp_parm_string(-1, "dcerpc_remote", "password"), CRED_SPECIFIED);
53 status = dcerpc_pipe_connect(private,
54 &(private->c_pipe), binding,
55 iface->uuid, iface->if_version,
56 credentials, dce_call->event_ctx);
58 talloc_free(credentials);
59 if (!NT_STATUS_IS_OK(status)) {
63 dce_call->context->private = private;
68 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
70 struct dcesrv_remote_private *private = context->private;
72 talloc_free(private->c_pipe);
77 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
80 const struct dcerpc_interface_table *table = dce_call->context->iface->private;
81 uint16_t opnum = dce_call->pkt.u.request.opnum;
83 dce_call->fault_code = 0;
85 if (opnum >= table->num_calls) {
86 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
87 return NT_STATUS_NET_WRITE_FAULT;
90 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
92 return NT_STATUS_NO_MEMORY;
95 /* unravel the NDR for the packet */
96 status = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
97 if (!NT_STATUS_IS_OK(status)) {
98 dcerpc_log_packet(table, opnum, NDR_IN,
99 &dce_call->pkt.u.request.stub_and_verifier);
100 dce_call->fault_code = DCERPC_FAULT_NDR;
101 return NT_STATUS_NET_WRITE_FAULT;
107 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
109 struct dcesrv_remote_private *private = dce_call->context->private;
110 uint16_t opnum = dce_call->pkt.u.request.opnum;
111 const struct dcerpc_interface_table *table = dce_call->context->iface->private;
112 const struct dcerpc_interface_call *call;
115 name = table->calls[opnum].name;
116 call = &table->calls[opnum];
118 if (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
119 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
122 /* we didn't use the return code of this function as we only check the last_fault_code */
123 dcerpc_ndr_request(private->c_pipe, NULL, table, opnum, mem_ctx,r);
125 dce_call->fault_code = private->c_pipe->last_fault_code;
126 if (dce_call->fault_code != 0) {
127 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
128 return NT_STATUS_NET_WRITE_FAULT;
131 if ((dce_call->fault_code == 0) &&
132 (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
133 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);
139 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
142 const struct dcerpc_interface_table *table = dce_call->context->iface->private;
143 uint16_t opnum = dce_call->pkt.u.request.opnum;
145 /* unravel the NDR for the packet */
146 status = table->calls[opnum].ndr_push(push, NDR_OUT, r);
147 if (!NT_STATUS_IS_OK(status)) {
148 dce_call->fault_code = DCERPC_FAULT_NDR;
149 return NT_STATUS_NET_WRITE_FAULT;
155 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
158 const struct dcerpc_interface_table *table = iface->private;
160 for (i=0;i<table->endpoints->count;i++) {
162 const char *name = table->endpoints->names[i];
164 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
165 if (!NT_STATUS_IS_OK(ret)) {
166 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
174 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
177 const char **ifaces = str_list_make(dce_ctx, lp_parm_string(-1,"dcerpc_remote","interfaces"),NULL);
180 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
184 for (i=0;ifaces[i];i++) {
186 struct dcesrv_interface iface;
188 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
189 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
191 return NT_STATUS_UNSUCCESSFUL;
194 ret = remote_register_one_iface(dce_ctx, &iface);
195 if (!NT_STATUS_IS_OK(ret)) {
196 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
206 static BOOL remote_fill_interface(struct dcesrv_interface *iface, const struct dcerpc_interface_table *if_tabl)
208 iface->name = if_tabl->name;
209 iface->uuid = if_tabl->uuid;
210 iface->if_version = if_tabl->if_version;
212 iface->bind = remote_op_bind;
213 iface->unbind = remote_op_unbind;
215 iface->ndr_pull = remote_op_ndr_pull;
216 iface->dispatch = remote_op_dispatch;
217 iface->ndr_push = remote_op_ndr_push;
219 iface->private = if_tabl;
224 static BOOL remote_op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
226 const struct dcerpc_interface_list *l;
228 for (l=librpc_dcerpc_pipes();l;l=l->next) {
229 if (l->table->if_version == if_version &&
230 strcmp(l->table->uuid, uuid)==0) {
231 return remote_fill_interface(iface, l->table);
238 static BOOL remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
240 const struct dcerpc_interface_list *l;
242 for (l=librpc_dcerpc_pipes();l;l=l->next) {
243 if (strcmp(l->table->name, name)==0) {
244 return remote_fill_interface(iface, l->table);
251 NTSTATUS dcerpc_server_remote_init(void)
254 struct dcesrv_endpoint_server ep_server;
256 ZERO_STRUCT(ep_server);
258 /* fill in our name */
259 ep_server.name = "remote";
261 /* fill in all the operations */
262 ep_server.init_server = remote_op_init_server;
264 ep_server.interface_by_uuid = remote_op_interface_by_uuid;
265 ep_server.interface_by_name = remote_op_interface_by_name;
267 /* register ourselves with the DCERPC subsystem. */
268 ret = dcerpc_register_ep_server(&ep_server);
269 if (!NT_STATUS_IS_OK(ret)) {
270 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));