preparing for release of 3.0-alpha13
[Samba.git] / source / rpc_server / srv_pipe_hnd.c
blobcc6415cce7c9ca0170e2155d64c0d6b425fd2171
1 /*
2 * Unix SMB/Netbios implementation.
3 * Version 1.9.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-1998,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
7 * Copyright (C) Jeremy Allison 1999.
8 *
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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 #define PIPE "\\PIPE\\"
27 #define PIPELEN strlen(PIPE)
29 static pipes_struct *chain_p;
30 static int pipes_open;
32 #ifndef MAX_OPEN_PIPES
33 #define MAX_OPEN_PIPES 2048
34 #endif
36 static pipes_struct *Pipes;
37 static struct bitmap *bmap;
39 /****************************************************************************
40 Pipe iterator functions.
41 ****************************************************************************/
43 pipes_struct *get_first_pipe(void)
45 return Pipes;
48 pipes_struct *get_next_pipe(pipes_struct *p)
50 return p->next;
53 /* this must be larger than the sum of the open files and directories */
54 static int pipe_handle_offset;
56 /****************************************************************************
57 Set the pipe_handle_offset. Called from smbd/files.c
58 ****************************************************************************/
60 void set_pipe_handle_offset(int max_open_files)
62 if(max_open_files < 0x7000)
63 pipe_handle_offset = 0x7000;
64 else
65 pipe_handle_offset = max_open_files + 10; /* For safety. :-) */
68 /****************************************************************************
69 Reset pipe chain handle number.
70 ****************************************************************************/
71 void reset_chain_p(void)
73 chain_p = NULL;
76 /****************************************************************************
77 Initialise pipe handle states.
78 ****************************************************************************/
80 void init_rpc_pipe_hnd(void)
82 bmap = bitmap_allocate(MAX_OPEN_PIPES);
83 if (!bmap)
84 exit_server("out of memory in init_rpc_pipe_hnd");
87 /****************************************************************************
88 Initialise an outgoing packet.
89 ****************************************************************************/
91 static BOOL pipe_init_outgoing_data(pipes_struct *p)
93 output_data *o_data = &p->out_data;
95 /* Reset the offset counters. */
96 o_data->data_sent_length = 0;
97 o_data->current_pdu_len = 0;
98 o_data->current_pdu_sent = 0;
100 memset(o_data->current_pdu, '\0', sizeof(o_data->current_pdu));
102 /* Free any memory in the current return data buffer. */
103 prs_mem_free(&o_data->rdata);
106 * Initialize the outgoing RPC data buffer.
107 * we will use this as the raw data area for replying to rpc requests.
109 if(!prs_init(&o_data->rdata, MAX_PDU_FRAG_LEN, p->mem_ctx, MARSHALL)) {
110 DEBUG(0,("pipe_init_outgoing_data: malloc fail.\n"));
111 return False;
114 return True;
117 /****************************************************************************
118 Find first available pipe slot.
119 ****************************************************************************/
121 pipes_struct *open_rpc_pipe_p(char *pipe_name,
122 connection_struct *conn, uint16 vuid)
124 int i;
125 pipes_struct *p;
126 static int next_pipe;
128 DEBUG(4,("Open pipe requested %s (pipes_open=%d)\n",
129 pipe_name, pipes_open));
132 /* not repeating pipe numbers makes it easier to track things in
133 log files and prevents client bugs where pipe numbers are reused
134 over connection restarts */
135 if (next_pipe == 0)
136 next_pipe = (sys_getpid() ^ time(NULL)) % MAX_OPEN_PIPES;
138 i = bitmap_find(bmap, next_pipe);
140 if (i == -1) {
141 DEBUG(0,("ERROR! Out of pipe structures\n"));
142 return NULL;
145 next_pipe = (i+1) % MAX_OPEN_PIPES;
147 for (p = Pipes; p; p = p->next)
148 DEBUG(5,("open_rpc_pipe_p: name %s pnum=%x\n", p->name, p->pnum));
150 p = (pipes_struct *)malloc(sizeof(*p));
152 if (!p)
153 return NULL;
155 ZERO_STRUCTP(p);
157 if ((p->mem_ctx = talloc_init()) == NULL) {
158 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
159 SAFE_FREE(p);
160 return NULL;
163 if (!init_pipe_handle_list(p, pipe_name)) {
164 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
165 talloc_destroy(p->mem_ctx);
166 SAFE_FREE(p);
167 return NULL;
171 DLIST_ADD(Pipes, p);
174 * Initialize the incoming RPC data buffer with one PDU worth of memory.
175 * We cheat here and say we're marshalling, as we intend to add incoming
176 * data directly into the prs_struct and we want it to auto grow. We will
177 * change the type to UNMARSALLING before processing the stream.
180 if(!prs_init(&p->in_data.data, MAX_PDU_FRAG_LEN, p->mem_ctx, MARSHALL)) {
181 DEBUG(0,("open_rpc_pipe_p: malloc fail for in_data struct.\n"));
182 return NULL;
185 bitmap_set(bmap, i);
186 i += pipe_handle_offset;
188 pipes_open++;
190 p->pnum = i;
192 p->open = True;
193 p->device_state = 0;
194 p->priority = 0;
195 p->conn = conn;
196 p->vuid = vuid;
198 p->max_trans_reply = 0;
200 p->ntlmssp_chal_flags = 0;
201 p->ntlmssp_auth_validated = False;
202 p->ntlmssp_auth_requested = False;
204 p->pipe_bound = False;
205 p->fault_state = False;
206 p->endian = RPC_LITTLE_ENDIAN;
209 * Initialize the incoming RPC struct.
212 p->in_data.pdu_needed_len = 0;
213 p->in_data.pdu_received_len = 0;
216 * Initialize the outgoing RPC struct.
219 p->out_data.current_pdu_len = 0;
220 p->out_data.current_pdu_sent = 0;
221 p->out_data.data_sent_length = 0;
224 * Initialize the outgoing RPC data buffer with no memory.
226 prs_init(&p->out_data.rdata, 0, p->mem_ctx, MARSHALL);
228 ZERO_STRUCT(p->pipe_user);
230 p->pipe_user.uid = (uid_t)-1;
231 p->pipe_user.gid = (gid_t)-1;
233 fstrcpy(p->name, pipe_name);
235 DEBUG(4,("Opened pipe %s with handle %x (pipes_open=%d)\n",
236 pipe_name, i, pipes_open));
238 chain_p = p;
240 /* OVERWRITE p as a temp variable, to display all open pipes */
241 for (p = Pipes; p; p = p->next)
242 DEBUG(5,("open pipes: name %s pnum=%x\n", p->name, p->pnum));
244 return chain_p;
247 /****************************************************************************
248 Sets the fault state on incoming packets.
249 ****************************************************************************/
251 static void set_incoming_fault(pipes_struct *p)
253 prs_mem_free(&p->in_data.data);
254 p->in_data.pdu_needed_len = 0;
255 p->in_data.pdu_received_len = 0;
256 p->fault_state = True;
257 DEBUG(10,("set_incoming_fault: Setting fault state on pipe %s : pnum = 0x%x\n",
258 p->name, p->pnum ));
261 /****************************************************************************
262 Ensures we have at least RPC_HEADER_LEN amount of data in the incoming buffer.
263 ****************************************************************************/
265 static ssize_t fill_rpc_header(pipes_struct *p, char *data, size_t data_to_copy)
267 size_t len_needed_to_complete_hdr = MIN(data_to_copy, RPC_HEADER_LEN - p->in_data.pdu_received_len);
269 DEBUG(10,("fill_rpc_header: data_to_copy = %u, len_needed_to_complete_hdr = %u, receive_len = %u\n",
270 (unsigned int)data_to_copy, (unsigned int)len_needed_to_complete_hdr,
271 (unsigned int)p->in_data.pdu_received_len ));
273 memcpy((char *)&p->in_data.current_in_pdu[p->in_data.pdu_received_len], data, len_needed_to_complete_hdr);
274 p->in_data.pdu_received_len += len_needed_to_complete_hdr;
276 return (ssize_t)len_needed_to_complete_hdr;
279 /****************************************************************************
280 Unmarshalls a new PDU header. Assumes the raw header data is in current_in_pdu.
281 ****************************************************************************/
283 static ssize_t unmarshall_rpc_header(pipes_struct *p)
286 * Unmarshall the header to determine the needed length.
289 prs_struct rpc_in;
291 if(p->in_data.pdu_received_len != RPC_HEADER_LEN) {
292 DEBUG(0,("unmarshall_rpc_header: assert on rpc header length failed.\n"));
293 set_incoming_fault(p);
294 return -1;
297 prs_init( &rpc_in, 0, p->mem_ctx, UNMARSHALL);
298 prs_set_endian_data( &rpc_in, p->endian);
300 prs_give_memory( &rpc_in, (char *)&p->in_data.current_in_pdu[0],
301 p->in_data.pdu_received_len, False);
304 * Unmarshall the header as this will tell us how much
305 * data we need to read to get the complete pdu.
306 * This also sets the endian flag in rpc_in.
309 if(!smb_io_rpc_hdr("", &p->hdr, &rpc_in, 0)) {
310 DEBUG(0,("unmarshall_rpc_header: failed to unmarshall RPC_HDR.\n"));
311 set_incoming_fault(p);
312 prs_mem_free(&rpc_in);
313 return -1;
317 * Validate the RPC header.
320 if(p->hdr.major != 5 && p->hdr.minor != 0) {
321 DEBUG(0,("unmarshall_rpc_header: invalid major/minor numbers in RPC_HDR.\n"));
322 set_incoming_fault(p);
323 prs_mem_free(&rpc_in);
324 return -1;
328 * If there's not data in the incoming buffer this should be the start of a new RPC.
331 if(prs_offset(&p->in_data.data) == 0) {
334 * AS/U doesn't set FIRST flag in a BIND packet it seems.
337 if ((p->hdr.pkt_type == RPC_REQUEST) && !(p->hdr.flags & RPC_FLG_FIRST)) {
339 * Ensure that the FIRST flag is set. If not then we have
340 * a stream missmatch.
343 DEBUG(0,("unmarshall_rpc_header: FIRST flag not set in first PDU !\n"));
344 set_incoming_fault(p);
345 prs_mem_free(&rpc_in);
346 return -1;
350 * If this is the first PDU then set the endianness
351 * flag in the pipe. We will need this when parsing all
352 * data in this RPC.
355 p->endian = rpc_in.bigendian_data;
357 DEBUG(5,("unmarshall_rpc_header: using %sendian RPC\n",
358 p->endian == RPC_LITTLE_ENDIAN ? "little-" : "big-" ));
360 } else {
363 * If this is *NOT* the first PDU then check the endianness
364 * flag in the pipe is the same as that in the PDU.
367 if (p->endian != rpc_in.bigendian_data) {
368 DEBUG(0,("unmarshall_rpc_header: FIRST endianness flag (%d) different in next PDU !\n", (int)p->endian));
369 set_incoming_fault(p);
370 prs_mem_free(&rpc_in);
371 return -1;
376 * Ensure that the pdu length is sane.
379 if((p->hdr.frag_len < RPC_HEADER_LEN) || (p->hdr.frag_len > MAX_PDU_FRAG_LEN)) {
380 DEBUG(0,("unmarshall_rpc_header: assert on frag length failed.\n"));
381 set_incoming_fault(p);
382 prs_mem_free(&rpc_in);
383 return -1;
386 DEBUG(10,("unmarshall_rpc_header: type = %u, flags = %u\n", (unsigned int)p->hdr.pkt_type,
387 (unsigned int)p->hdr.flags ));
390 * Adjust for the header we just ate.
392 p->in_data.pdu_received_len = 0;
393 p->in_data.pdu_needed_len = (uint32)p->hdr.frag_len - RPC_HEADER_LEN;
396 * Null the data we just ate.
399 memset((char *)&p->in_data.current_in_pdu[0], '\0', RPC_HEADER_LEN);
401 prs_mem_free(&rpc_in);
403 return 0; /* No extra data processed. */
406 /****************************************************************************
407 Call this to free any talloc'ed memory. Do this before and after processing
408 a complete PDU.
409 ****************************************************************************/
411 void free_pipe_context(pipes_struct *p)
413 if (p->mem_ctx) {
414 DEBUG(3,("free_pipe_context: destroying talloc pool of size %u\n", talloc_pool_size(p->mem_ctx) ));
415 talloc_destroy_pool(p->mem_ctx);
416 } else {
417 p->mem_ctx = talloc_init();
418 if (p->mem_ctx == NULL)
419 p->fault_state = True;
423 /****************************************************************************
424 Processes a request pdu. This will do auth processing if needed, and
425 appends the data into the complete stream if the LAST flag is not set.
426 ****************************************************************************/
428 static BOOL process_request_pdu(pipes_struct *p, prs_struct *rpc_in_p)
430 BOOL auth_verify = ((p->ntlmssp_chal_flags & NTLMSSP_NEGOTIATE_SIGN) != 0);
431 size_t data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
432 (auth_verify ? RPC_HDR_AUTH_LEN : 0) - p->hdr.auth_len;
434 if(!p->pipe_bound) {
435 DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
436 set_incoming_fault(p);
437 return False;
441 * Check if we need to do authentication processing.
442 * This is only done on requests, not binds.
446 * Read the RPC request header.
449 if(!smb_io_rpc_hdr_req("req", &p->hdr_req, rpc_in_p, 0)) {
450 DEBUG(0,("process_request_pdu: failed to unmarshall RPC_HDR_REQ.\n"));
451 set_incoming_fault(p);
452 return False;
455 if(p->ntlmssp_auth_validated && !api_pipe_auth_process(p, rpc_in_p)) {
456 DEBUG(0,("process_request_pdu: failed to do auth processing.\n"));
457 set_incoming_fault(p);
458 return False;
461 if (p->ntlmssp_auth_requested && !p->ntlmssp_auth_validated) {
464 * Authentication _was_ requested and it already failed.
467 DEBUG(0,("process_request_pdu: RPC request received on pipe %s where \
468 authentication failed. Denying the request.\n", p->name));
469 set_incoming_fault(p);
470 return False;
474 * Check the data length doesn't go over the 10Mb limit.
477 if(prs_data_size(&p->in_data.data) + data_len > 10*1024*1024) {
478 DEBUG(0,("process_request_pdu: rpc data buffer too large (%u) + (%u)\n",
479 (unsigned int)prs_data_size(&p->in_data.data), (unsigned int)data_len ));
480 set_incoming_fault(p);
481 return False;
485 * Append the data portion into the buffer and return.
489 char *data_from = prs_data_p(rpc_in_p) + prs_offset(rpc_in_p);
491 if(!prs_append_data(&p->in_data.data, data_from, data_len)) {
492 DEBUG(0,("process_request_pdu: Unable to append data size %u to parse buffer of size %u.\n",
493 (unsigned int)data_len, (unsigned int)prs_data_size(&p->in_data.data) ));
494 set_incoming_fault(p);
495 return False;
500 if(p->hdr.flags & RPC_FLG_LAST) {
501 BOOL ret = False;
503 * Ok - we finally have a complete RPC stream.
504 * Call the rpc command to process it.
508 * Ensure the internal prs buffer size is *exactly* the same
509 * size as the current offset.
512 if(!prs_set_buffer_size(&p->in_data.data, prs_offset(&p->in_data.data)))
514 DEBUG(0,("process_request_pdu: Call to prs_set_buffer_size failed!\n"));
515 set_incoming_fault(p);
516 return False;
520 * Set the parse offset to the start of the data and set the
521 * prs_struct to UNMARSHALL.
524 prs_set_offset(&p->in_data.data, 0);
525 prs_switch_type(&p->in_data.data, UNMARSHALL);
528 * Process the complete data stream here.
531 free_pipe_context(p);
533 if(pipe_init_outgoing_data(p))
534 ret = api_pipe_request(p);
536 free_pipe_context(p);
539 * We have consumed the whole data stream. Set back to
540 * marshalling and set the offset back to the start of
541 * the buffer to re-use it (we could also do a prs_mem_free()
542 * and then re_init on the next start of PDU. Not sure which
543 * is best here.... JRA.
546 prs_switch_type(&p->in_data.data, MARSHALL);
547 prs_set_offset(&p->in_data.data, 0);
548 return ret;
551 return True;
554 /****************************************************************************
555 Processes a finished PDU stored in current_in_pdu. The RPC_HEADER has
556 already been parsed and stored in p->hdr.
557 ****************************************************************************/
559 static ssize_t process_complete_pdu(pipes_struct *p)
561 prs_struct rpc_in;
562 size_t data_len = p->in_data.pdu_received_len;
563 char *data_p = (char *)&p->in_data.current_in_pdu[0];
564 BOOL reply = False;
566 if(p->fault_state) {
567 DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
568 p->name ));
569 set_incoming_fault(p);
570 setup_fault_pdu(p, NT_STATUS(0x1c010002));
571 return (ssize_t)data_len;
574 prs_init( &rpc_in, 0, p->mem_ctx, UNMARSHALL);
577 * Ensure we're using the corrent endianness for both the
578 * RPC header flags and the raw data we will be reading from.
581 prs_set_endian_data( &rpc_in, p->endian);
582 prs_set_endian_data( &p->in_data.data, p->endian);
584 prs_give_memory( &rpc_in, data_p, (uint32)data_len, False);
586 DEBUG(10,("process_complete_pdu: processing packet type %u\n",
587 (unsigned int)p->hdr.pkt_type ));
589 switch (p->hdr.pkt_type) {
590 case RPC_BIND:
591 case RPC_ALTCONT:
593 * We assume that a pipe bind is only in one pdu.
595 if(pipe_init_outgoing_data(p))
596 reply = api_pipe_bind_req(p, &rpc_in);
597 break;
598 case RPC_BINDRESP:
600 * We assume that a pipe bind_resp is only in one pdu.
602 if(pipe_init_outgoing_data(p))
603 reply = api_pipe_bind_auth_resp(p, &rpc_in);
604 break;
605 case RPC_REQUEST:
606 reply = process_request_pdu(p, &rpc_in);
607 break;
608 default:
609 DEBUG(0,("process_complete_pdu: Unknown rpc type = %u received.\n", (unsigned int)p->hdr.pkt_type ));
610 break;
613 /* Reset to little endian. Probably don't need this but it won't hurt. */
614 prs_set_endian_data( &p->in_data.data, RPC_LITTLE_ENDIAN);
616 if (!reply) {
617 DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on pipe %s\n", p->pipe_srv_name));
618 set_incoming_fault(p);
619 setup_fault_pdu(p, NT_STATUS(0x1c010002));
620 prs_mem_free(&rpc_in);
621 } else {
623 * Reset the lengths. We're ready for a new pdu.
625 p->in_data.pdu_needed_len = 0;
626 p->in_data.pdu_received_len = 0;
629 prs_mem_free(&rpc_in);
630 return (ssize_t)data_len;
633 /****************************************************************************
634 Accepts incoming data on an rpc pipe. Processes the data in pdu sized units.
635 ****************************************************************************/
637 static ssize_t process_incoming_data(pipes_struct *p, char *data, size_t n)
639 size_t data_to_copy = MIN(n, MAX_PDU_FRAG_LEN - p->in_data.pdu_received_len);
641 DEBUG(10,("process_incoming_data: Start: pdu_received_len = %u, pdu_needed_len = %u, incoming data = %u\n",
642 (unsigned int)p->in_data.pdu_received_len, (unsigned int)p->in_data.pdu_needed_len,
643 (unsigned int)n ));
645 if(data_to_copy == 0) {
647 * This is an error - data is being received and there is no
648 * space in the PDU. Free the received data and go into the fault state.
650 DEBUG(0,("process_incoming_data: No space in incoming pdu buffer. Current size = %u \
651 incoming data size = %u\n", (unsigned int)p->in_data.pdu_received_len, (unsigned int)n ));
652 set_incoming_fault(p);
653 return -1;
657 * If we have no data already, wait until we get at least a RPC_HEADER_LEN
658 * number of bytes before we can do anything.
661 if((p->in_data.pdu_needed_len == 0) && (p->in_data.pdu_received_len < RPC_HEADER_LEN)) {
663 * Always return here. If we have more data then the RPC_HEADER
664 * will be processed the next time around the loop.
666 return fill_rpc_header(p, data, data_to_copy);
670 * At this point we know we have at least an RPC_HEADER_LEN amount of data
671 * stored in current_in_pdu.
675 * If pdu_needed_len is zero this is a new pdu.
676 * Unmarshall the header so we know how much more
677 * data we need, then loop again.
680 if(p->in_data.pdu_needed_len == 0)
681 return unmarshall_rpc_header(p);
684 * Ok - at this point we have a valid RPC_HEADER in p->hdr.
685 * Keep reading until we have a full pdu.
688 data_to_copy = MIN(data_to_copy, p->in_data.pdu_needed_len);
691 * Copy as much of the data as we need into the current_in_pdu buffer.
694 memcpy( (char *)&p->in_data.current_in_pdu[p->in_data.pdu_received_len], data, data_to_copy);
695 p->in_data.pdu_received_len += data_to_copy;
698 * Do we have a complete PDU ?
701 if(p->in_data.pdu_received_len == p->in_data.pdu_needed_len)
702 return process_complete_pdu(p);
704 DEBUG(10,("process_incoming_data: not a complete PDU yet. pdu_received_len = %u, pdu_needed_len = %u\n",
705 (unsigned int)p->in_data.pdu_received_len, (unsigned int)p->in_data.pdu_needed_len ));
707 return (ssize_t)data_to_copy;
711 /****************************************************************************
712 Accepts incoming data on an rpc pipe.
713 ****************************************************************************/
715 ssize_t write_to_pipe(pipes_struct *p, char *data, size_t n)
717 size_t data_left = n;
719 DEBUG(6,("write_to_pipe: %x", p->pnum));
721 DEBUG(6,(" name: %s open: %s len: %d\n",
722 p->name, BOOLSTR(p->open), (int)n));
724 dump_data(50, data, n);
726 while(data_left) {
727 ssize_t data_used;
729 DEBUG(10,("write_to_pipe: data_left = %u\n", (unsigned int)data_left ));
731 data_used = process_incoming_data(p, data, data_left);
733 DEBUG(10,("write_to_pipe: data_used = %d\n", (int)data_used ));
735 if(data_used < 0)
736 return -1;
738 data_left -= data_used;
739 data += data_used;
742 return n;
745 /****************************************************************************
746 Replies to a request to read data from a pipe.
748 Headers are interspersed with the data at PDU intervals. By the time
749 this function is called, the start of the data could possibly have been
750 read by an SMBtrans (file_offset != 0).
752 Calling create_rpc_reply() here is a hack. The data should already
753 have been prepared into arrays of headers + data stream sections.
754 ****************************************************************************/
756 ssize_t read_from_pipe(pipes_struct *p, char *data, size_t n)
758 uint32 pdu_remaining = 0;
759 ssize_t data_returned = 0;
761 if (!p || !p->open) {
762 DEBUG(0,("read_from_pipe: pipe not open\n"));
763 return -1;
766 DEBUG(6,("read_from_pipe: %x", p->pnum));
768 DEBUG(6,(" name: %s len: %u\n", p->name, (unsigned int)n));
771 * We cannot return more than one PDU length per
772 * read request.
776 * This condition should result in the connection being closed.
777 * Netapp filers seem to set it to 0xffff which results in domain
778 * authentications failing. Just ignore it so things work.
781 if(n > MAX_PDU_FRAG_LEN) {
782 DEBUG(5,("read_from_pipe: too large read (%u) requested on \
783 pipe %s. We can only service %d sized reads.\n", (unsigned int)n, p->name, MAX_PDU_FRAG_LEN ));
787 * Determine if there is still data to send in the
788 * pipe PDU buffer. Always send this first. Never
789 * send more than is left in the current PDU. The
790 * client should send a new read request for a new
791 * PDU.
794 if((pdu_remaining = p->out_data.current_pdu_len - p->out_data.current_pdu_sent) > 0) {
795 data_returned = (ssize_t)MIN(n, pdu_remaining);
797 DEBUG(10,("read_from_pipe: %s: current_pdu_len = %u, current_pdu_sent = %u \
798 returning %d bytes.\n", p->name, (unsigned int)p->out_data.current_pdu_len,
799 (unsigned int)p->out_data.current_pdu_sent, (int)data_returned));
801 memcpy( data, &p->out_data.current_pdu[p->out_data.current_pdu_sent], (size_t)data_returned);
802 p->out_data.current_pdu_sent += (uint32)data_returned;
803 goto out;
807 * At this point p->current_pdu_len == p->current_pdu_sent (which
808 * may of course be zero if this is the first return fragment.
811 DEBUG(10,("read_from_pipe: %s: fault_state = %d : data_sent_length \
812 = %u, prs_offset(&p->out_data.rdata) = %u.\n",
813 p->name, (int)p->fault_state, (unsigned int)p->out_data.data_sent_length, (unsigned int)prs_offset(&p->out_data.rdata) ));
815 if(p->out_data.data_sent_length >= prs_offset(&p->out_data.rdata)) {
817 * We have sent all possible data, return 0.
819 data_returned = 0;
820 goto out;
824 * We need to create a new PDU from the data left in p->rdata.
825 * Create the header/data/footers. This also sets up the fields
826 * p->current_pdu_len, p->current_pdu_sent, p->data_sent_length
827 * and stores the outgoing PDU in p->current_pdu.
830 if(!create_next_pdu(p)) {
831 DEBUG(0,("read_from_pipe: %s: create_next_pdu failed.\n", p->name));
832 return -1;
835 data_returned = MIN(n, p->out_data.current_pdu_len);
837 memcpy( data, p->out_data.current_pdu, (size_t)data_returned);
838 p->out_data.current_pdu_sent += (uint32)data_returned;
840 out:
842 return data_returned;
845 /****************************************************************************
846 Wait device state on a pipe. Exactly what this is for is unknown...
847 ****************************************************************************/
849 BOOL wait_rpc_pipe_hnd_state(pipes_struct *p, uint16 priority)
851 if (p == NULL)
852 return False;
854 if (p->open) {
855 DEBUG(3,("wait_rpc_pipe_hnd_state: Setting pipe wait state priority=%x on pipe (name=%s)\n",
856 priority, p->name));
858 p->priority = priority;
860 return True;
863 DEBUG(3,("wait_rpc_pipe_hnd_state: Error setting pipe wait state priority=%x (name=%s)\n",
864 priority, p->name));
865 return False;
869 /****************************************************************************
870 Set device state on a pipe. Exactly what this is for is unknown...
871 ****************************************************************************/
873 BOOL set_rpc_pipe_hnd_state(pipes_struct *p, uint16 device_state)
875 if (p == NULL)
876 return False;
878 if (p->open) {
879 DEBUG(3,("set_rpc_pipe_hnd_state: Setting pipe device state=%x on pipe (name=%s)\n",
880 device_state, p->name));
882 p->device_state = device_state;
884 return True;
887 DEBUG(3,("set_rpc_pipe_hnd_state: Error setting pipe device state=%x (name=%s)\n",
888 device_state, p->name));
889 return False;
893 /****************************************************************************
894 Close an rpc pipe.
895 ****************************************************************************/
897 BOOL close_rpc_pipe_hnd(pipes_struct *p, connection_struct *conn)
899 if (!p) {
900 DEBUG(0,("Invalid pipe in close_rpc_pipe_hnd\n"));
901 return False;
904 prs_mem_free(&p->out_data.rdata);
905 prs_mem_free(&p->in_data.data);
907 if (p->mem_ctx)
908 talloc_destroy(p->mem_ctx);
910 /* Free the handles database. */
911 close_policy_by_pipe(p);
913 bitmap_clear(bmap, p->pnum - pipe_handle_offset);
915 pipes_open--;
917 DEBUG(4,("closed pipe name %s pnum=%x (pipes_open=%d)\n",
918 p->name, p->pnum, pipes_open));
920 DLIST_REMOVE(Pipes, p);
922 delete_nt_token(&p->pipe_user.nt_user_token);
923 SAFE_FREE(p->pipe_user.groups);
925 ZERO_STRUCTP(p);
927 SAFE_FREE(p);
929 return True;
932 /****************************************************************************
933 Find an rpc pipe given a pipe handle in a buffer and an offset.
934 ****************************************************************************/
936 pipes_struct *get_rpc_pipe_p(char *buf, int where)
938 int pnum = SVAL(buf,where);
940 if (chain_p)
941 return chain_p;
943 return get_rpc_pipe(pnum);
946 /****************************************************************************
947 Find an rpc pipe given a pipe handle.
948 ****************************************************************************/
950 pipes_struct *get_rpc_pipe(int pnum)
952 pipes_struct *p;
954 DEBUG(4,("search for pipe pnum=%x\n", pnum));
956 for (p=Pipes;p;p=p->next)
957 DEBUG(5,("pipe name %s pnum=%x (pipes_open=%d)\n",
958 p->name, p->pnum, pipes_open));
960 for (p=Pipes;p;p=p->next) {
961 if (p->pnum == pnum) {
962 chain_p = p;
963 return p;
967 return NULL;