2 Unix SMB/CIFS implementation.
4 server side dcerpc common code
6 Copyright (C) Andrew Tridgell 2003-2010
7 Copyright (C) Stefan (metze) Metzmacher 2004-2005
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/>.
24 #include "auth/auth.h"
25 #include "auth/gensec/gensec.h"
26 #include "../lib/util/dlinklist.h"
27 #include "rpc_server/dcerpc_server.h"
28 #include "rpc_server/dcerpc_server_proto.h"
29 #include "rpc_server/common/proto.h"
30 #include "librpc/rpc/dcerpc_proto.h"
31 #include "system/filesys.h"
32 #include "libcli/security/security.h"
33 #include "param/param.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/named_pipe_auth/npa_tstream.h"
36 #include "smbd/service_stream.h"
37 #include "../lib/tsocket/tsocket.h"
38 #include "lib/socket/socket.h"
39 #include "smbd/process_model.h"
40 #include "lib/messaging/irpc.h"
41 #include "librpc/rpc/rpc_common.h"
45 move a call from an existing linked list to the specified list. This
46 prevents bugs where we forget to remove the call from a previous
49 static void dcesrv_call_set_list(struct dcesrv_call_state *call,
50 enum dcesrv_call_list list)
53 case DCESRV_LIST_NONE:
55 case DCESRV_LIST_CALL_LIST:
56 DLIST_REMOVE(call->conn->call_list, call);
58 case DCESRV_LIST_FRAGMENTED_CALL_LIST:
59 DLIST_REMOVE(call->conn->incoming_fragmented_call_list, call);
61 case DCESRV_LIST_PENDING_CALL_LIST:
62 DLIST_REMOVE(call->conn->pending_call_list, call);
67 case DCESRV_LIST_NONE:
69 case DCESRV_LIST_CALL_LIST:
70 DLIST_ADD_END(call->conn->call_list, call);
72 case DCESRV_LIST_FRAGMENTED_CALL_LIST:
73 DLIST_ADD_END(call->conn->incoming_fragmented_call_list, call);
75 case DCESRV_LIST_PENDING_CALL_LIST:
76 DLIST_ADD_END(call->conn->pending_call_list, call);
82 void dcesrv_init_hdr(struct ncacn_packet *pkt, bool bigendian)
85 pkt->rpc_vers_minor = 0;
89 pkt->drep[0] = DCERPC_DREP_LE;
100 NTSTATUS dcesrv_fault(struct dcesrv_call_state *call, uint32_t fault_code)
102 struct ncacn_packet pkt;
103 struct data_blob_list_item *rep;
104 static const uint8_t zeros[4] = { 0, };
108 dcesrv_init_hdr(&pkt, lpcfg_rpc_big_endian(call->conn->dce_ctx->lp_ctx));
110 pkt.call_id = call->pkt.call_id;
111 pkt.ptype = DCERPC_PKT_FAULT;
112 pkt.pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
113 pkt.u.fault.alloc_hint = 0;
114 pkt.u.fault.context_id = 0;
115 pkt.u.fault.cancel_count = 0;
116 pkt.u.fault.status = fault_code;
117 pkt.u.fault._pad = data_blob_const(zeros, sizeof(zeros));
119 rep = talloc_zero(call, struct data_blob_list_item);
121 return NT_STATUS_NO_MEMORY;
124 status = ncacn_push_auth(&rep->blob, call, &pkt, NULL);
125 if (!NT_STATUS_IS_OK(status)) {
129 dcerpc_set_frag_length(&rep->blob, rep->blob.length);
131 DLIST_ADD_END(call->replies, rep);
132 dcesrv_call_set_list(call, DCESRV_LIST_CALL_LIST);
134 if (call->conn->call_list && call->conn->call_list->replies) {
135 if (call->conn->transport.report_output_data) {
136 call->conn->transport.report_output_data(call->conn);
145 _PUBLIC_ NTSTATUS dcesrv_reply(struct dcesrv_call_state *call)
147 struct ndr_push *push;
150 uint32_t total_length, chunk_size;
151 struct dcesrv_connection_context *context = call->context;
154 /* call the reply function */
155 status = context->iface->reply(call, call, call->r);
156 if (!NT_STATUS_IS_OK(status)) {
157 return dcesrv_fault(call, call->fault_code);
160 /* form the reply NDR */
161 push = ndr_push_init_ctx(call);
162 NT_STATUS_HAVE_NO_MEMORY(push);
164 /* carry over the pointer count to the reply in case we are
165 using full pointer. See NDR specification for full
167 push->ptr_count = call->ndr_pull->ptr_count;
169 if (lpcfg_rpc_big_endian(call->conn->dce_ctx->lp_ctx)) {
170 push->flags |= LIBNDR_FLAG_BIGENDIAN;
173 status = context->iface->ndr_push(call, call, push, call->r);
174 if (!NT_STATUS_IS_OK(status)) {
175 return dcesrv_fault(call, call->fault_code);
178 stub = ndr_push_blob(push);
180 total_length = stub.length;
182 /* we can write a full max_recv_frag size, minus the dcerpc
183 request header size */
184 chunk_size = call->conn->max_xmit_frag;
185 chunk_size -= DCERPC_REQUEST_LENGTH;
186 if (call->conn->auth_state.auth_info &&
187 call->conn->auth_state.gensec_security) {
188 size_t max_payload = chunk_size;
190 max_payload -= DCERPC_AUTH_TRAILER_LENGTH;
191 max_payload -= (max_payload % DCERPC_AUTH_PAD_ALIGNMENT);
193 sig_size = gensec_sig_size(call->conn->auth_state.gensec_security,
196 chunk_size -= DCERPC_AUTH_TRAILER_LENGTH;
197 chunk_size -= sig_size;
200 chunk_size -= (chunk_size % DCERPC_AUTH_PAD_ALIGNMENT);
204 struct data_blob_list_item *rep;
205 struct ncacn_packet pkt;
207 rep = talloc_zero(call, struct data_blob_list_item);
208 NT_STATUS_HAVE_NO_MEMORY(rep);
210 length = MIN(chunk_size, stub.length);
212 /* form the dcerpc response packet */
213 dcesrv_init_hdr(&pkt,
214 lpcfg_rpc_big_endian(call->conn->dce_ctx->lp_ctx));
216 pkt.call_id = call->pkt.call_id;
217 pkt.ptype = DCERPC_PKT_RESPONSE;
219 if (stub.length == total_length) {
220 pkt.pfc_flags |= DCERPC_PFC_FLAG_FIRST;
222 if (length == stub.length) {
223 pkt.pfc_flags |= DCERPC_PFC_FLAG_LAST;
225 pkt.u.response.alloc_hint = stub.length;
226 pkt.u.response.context_id = call->pkt.u.request.context_id;
227 pkt.u.response.cancel_count = 0;
228 pkt.u.response._pad.data = call->pkt.u.request._pad.data;
229 pkt.u.response._pad.length = call->pkt.u.request._pad.length;
230 pkt.u.response.stub_and_verifier.data = stub.data;
231 pkt.u.response.stub_and_verifier.length = length;
233 if (!dcesrv_auth_response(call, &rep->blob, sig_size, &pkt)) {
234 return dcesrv_fault(call, DCERPC_FAULT_OTHER);
237 dcerpc_set_frag_length(&rep->blob, rep->blob.length);
239 DLIST_ADD_END(call->replies, rep);
242 stub.length -= length;
243 } while (stub.length != 0);
245 /* move the call from the pending to the finished calls list */
246 dcesrv_call_set_list(call, DCESRV_LIST_CALL_LIST);
248 if (call->conn->call_list && call->conn->call_list->replies) {
249 if (call->conn->transport.report_output_data) {
250 call->conn->transport.report_output_data(call->conn);
257 NTSTATUS dcesrv_generic_session_key(struct dcesrv_connection *c,
258 DATA_BLOB *session_key)
260 enum dcerpc_transport_t transport =
261 dcerpc_binding_get_transport(c->endpoint->ep_description);
263 if (transport != NCALRPC && transport != NCACN_UNIX_STREAM) {
264 return NT_STATUS_NO_USER_SESSION_KEY;
267 return dcerpc_generic_session_key(NULL, session_key);