1 /*
2    Unix SMB/CIFS implementation.
3    remote dcerpc operations
4 
5    Copyright (C) Stefan (metze) Metzmacher 2004
6 
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.
11 
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.
16 
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.
20 */
21 
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "auth/auth.h"
25 #include "auth/credentials/credentials.h"
26 #include "librpc/rpc/dcerpc_table.h"
27 
28 
29 struct dcesrv_remote_private {
30 	struct dcerpc_pipe *c_pipe;
31 };
32 
remote_op_reply(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,void * r)33 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
34 {
35 	return NT_STATUS_OK;
36 }
37 
remote_op_bind(struct dcesrv_call_state * dce_call,const struct dcesrv_interface * iface)38 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
39 {
40         NTSTATUS status;
41 		const struct dcerpc_interface_table *table;
42         struct dcesrv_remote_private *private;
43 	const char *binding = lp_parm_string(-1, "dcerpc_remote", "binding");
44 	const char *user, *pass, *domain;
45 	struct cli_credentials *credentials;
46 	BOOL machine_account;
47 
48 	machine_account = lp_parm_bool(-1, "dcerpc_remote", "use_machine_account", False);
49 
50 	private = talloc(dce_call->conn, struct dcesrv_remote_private);
51 	if (!private) {
52 		return NT_STATUS_NO_MEMORY;
53 	}
54 
55 	private->c_pipe = NULL;
56 	dce_call->context->private = private;
57 
58 	if (!binding) {
59 		DEBUG(0,("You must specify a DCE/RPC binding string\n"));
60 		return NT_STATUS_INVALID_PARAMETER;
61 	}
62 
63 	user = lp_parm_string(-1, "dcerpc_remote", "user");
64 	pass = lp_parm_string(-1, "dcerpc_remote", "password");
65 	domain = lp_parm_string(-1, "dceprc_remote", "domain");
66 
67 	table = idl_iface_by_uuid(&iface->syntax_id.uuid); /* FIXME: What about if_version ? */
68 	if (!table) {
69 		dce_call->fault_code = DCERPC_FAULT_UNK_IF;
70 		return NT_STATUS_NET_WRITE_FAULT;
71 	}
72 
73 	if (user && pass) {
74 		DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
75 		credentials = cli_credentials_init(private);
76 		if (!credentials) {
77 			return NT_STATUS_NO_MEMORY;
78 		}
79 		cli_credentials_set_conf(credentials);
80 		cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
81 		if (domain) {
82 			cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
83 		}
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);
89 		if (domain) {
90 			cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
91 		}
92 		status = cli_credentials_set_machine_account(credentials);
93 		if (!NT_STATUS_IS_OK(status)) {
94 			return status;
95 		}
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;
99 	} else {
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;
102 	}
103 
104 	status = dcerpc_pipe_connect(private,
105 				     &(private->c_pipe), binding, table,
106 				     credentials, dce_call->event_ctx);
107 
108 	talloc_free(credentials);
109 	if (!NT_STATUS_IS_OK(status)) {
110 		return status;
111 	}
112 
113 	return NT_STATUS_OK;
114 }
115 
remote_op_unbind(struct dcesrv_connection_context * context,const struct dcesrv_interface * iface)116 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
117 {
118 	struct dcesrv_remote_private *private = context->private;
119 
120 	talloc_free(private->c_pipe);
121 
122 	return;
123 }
124 
remote_op_ndr_pull(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct ndr_pull * pull,void ** r)125 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
126 {
127 	NTSTATUS status;
128 	const struct dcerpc_interface_table *table = dce_call->context->iface->private;
129 	uint16_t opnum = dce_call->pkt.u.request.opnum;
130 
131 	dce_call->fault_code = 0;
132 
133 	if (opnum >= table->num_calls) {
134 		dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
135 		return NT_STATUS_NET_WRITE_FAULT;
136 	}
137 
138 	*r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
139 	if (!*r) {
140 		return NT_STATUS_NO_MEMORY;
141 	}
142 
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;
150 	}
151 
152 	return NT_STATUS_OK;
153 }
154 
remote_op_dispatch(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,void * r)155 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
156 {
157 	struct dcesrv_remote_private *private = dce_call->context->private;
158 	uint16_t opnum = dce_call->pkt.u.request.opnum;
159 	const struct dcerpc_interface_table *table = dce_call->context->iface->private;
160 	const struct dcerpc_interface_call *call;
161 	const char *name;
162 
163 	name = table->calls[opnum].name;
164 	call = &table->calls[opnum];
165 
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);
168 	}
169 
170 	/* we didn't use the return code of this function as we only check the last_fault_code */
171 	dcerpc_ndr_request(private->c_pipe, NULL, table, opnum, mem_ctx,r);
172 
173 	dce_call->fault_code = private->c_pipe->last_fault_code;
174 	if (dce_call->fault_code != 0) {
175 		DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
176 		return NT_STATUS_NET_WRITE_FAULT;
177 	}
178 
179 	if ((dce_call->fault_code == 0) &&
180 	    (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
181 		ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);
182 	}
183 
184 	return NT_STATUS_OK;
185 }
186 
remote_op_ndr_push(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct ndr_push * push,const void * r)187 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
188 {
189 	NTSTATUS status;
190 	const struct dcerpc_interface_table *table = dce_call->context->iface->private;
191 	uint16_t opnum = dce_call->pkt.u.request.opnum;
192 
193         /* unravel the NDR for the packet */
194 	status = table->calls[opnum].ndr_push(push, NDR_OUT, r);
195 	if (!NT_STATUS_IS_OK(status)) {
196 		dce_call->fault_code = DCERPC_FAULT_NDR;
197 		return NT_STATUS_NET_WRITE_FAULT;
198 	}
199 
200 	return NT_STATUS_OK;
201 }
202 
remote_register_one_iface(struct dcesrv_context * dce_ctx,const struct dcesrv_interface * iface)203 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
204 {
205 	int i;
206 	const struct dcerpc_interface_table *table = iface->private;
207 
208 	for (i=0;i<table->endpoints->count;i++) {
209 		NTSTATUS ret;
210 		const char *name = table->endpoints->names[i];
211 
212 		ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
213 		if (!NT_STATUS_IS_OK(ret)) {
214 			DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
215 			return ret;
216 		}
217 	}
218 
219 	return NT_STATUS_OK;
220 }
221 
remote_op_init_server(struct dcesrv_context * dce_ctx,const struct dcesrv_endpoint_server * ep_server)222 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
223 {
224 	int i;
225 	const char **ifaces = str_list_make(dce_ctx, lp_parm_string(-1,"dcerpc_remote","interfaces"),NULL);
226 
227 	if (!ifaces) {
228 		DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
229 		return NT_STATUS_OK;
230 	}
231 
232 	for (i=0;ifaces[i];i++) {
233 		NTSTATUS ret;
234 		struct dcesrv_interface iface;
235 
236 		if (!ep_server->interface_by_name(&iface, ifaces[i])) {
237 			DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
238 			talloc_free(ifaces);
239 			return NT_STATUS_UNSUCCESSFUL;
240 		}
241 
242 		ret = remote_register_one_iface(dce_ctx, &iface);
243 		if (!NT_STATUS_IS_OK(ret)) {
244 			DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
245 			talloc_free(ifaces);
246 			return ret;
247 		}
248 	}
249 
250 	talloc_free(ifaces);
251 	return NT_STATUS_OK;
252 }
253 
remote_fill_interface(struct dcesrv_interface * iface,const struct dcerpc_interface_table * if_tabl)254 static BOOL remote_fill_interface(struct dcesrv_interface *iface, const struct dcerpc_interface_table *if_tabl)
255 {
256 	iface->name = if_tabl->name;
257 	iface->syntax_id = if_tabl->syntax_id;
258 
259 	iface->bind = remote_op_bind;
260 	iface->unbind = remote_op_unbind;
261 
262 	iface->ndr_pull = remote_op_ndr_pull;
263 	iface->dispatch = remote_op_dispatch;
264 	iface->reply = remote_op_reply;
265 	iface->ndr_push = remote_op_ndr_push;
266 
267 	iface->private = if_tabl;
268 
269 	return True;
270 }
271 
remote_op_interface_by_uuid(struct dcesrv_interface * iface,const struct GUID * uuid,uint32_t if_version)272 static BOOL remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
273 {
274 	const struct dcerpc_interface_list *l;
275 
276 	for (l=librpc_dcerpc_pipes();l;l=l->next) {
277 		if (l->table->syntax_id.if_version == if_version &&
278 			GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
279 			return remote_fill_interface(iface, l->table);
280 		}
281 	}
282 
283 	return False;
284 }
285 
remote_op_interface_by_name(struct dcesrv_interface * iface,const char * name)286 static BOOL remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
287 {
288 	const struct dcerpc_interface_table *tbl = idl_iface_by_name(name);
289 
290 	if (tbl)
291 		return remote_fill_interface(iface, tbl);
292 
293 	return False;
294 }
295 
dcerpc_server_remote_init(void)296 NTSTATUS dcerpc_server_remote_init(void)
297 {
298 	NTSTATUS ret;
299 	struct dcesrv_endpoint_server ep_server;
300 
301 	ZERO_STRUCT(ep_server);
302 
303 	/* fill in our name */
304 	ep_server.name = "remote";
305 
306 	/* fill in all the operations */
307 	ep_server.init_server = remote_op_init_server;
308 
309 	ep_server.interface_by_uuid = remote_op_interface_by_uuid;
310 	ep_server.interface_by_name = remote_op_interface_by_name;
311 
312 	/* register ourselves with the DCERPC subsystem. */
313 	ret = dcerpc_register_ep_server(&ep_server);
314 	if (!NT_STATUS_IS_OK(ret)) {
315 		DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
316 		return ret;
317 	}
318 
319 	/* We need the full DCE/RPC interface table */
320 	dcerpc_table_init();
321 
322 	return ret;
323 }
324