merging for 2.2.6pre1
[Samba.git] / source / rpc_server / srv_pipe_hnd.c
blobf1d26095fa3858285c0c6c083240ffb99fab38ec
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.
25 #include "includes.h"
28 #define PIPE "\\PIPE\\"
29 #define PIPELEN strlen(PIPE)
31 static pipes_struct *chain_p;
32 static int pipes_open;
34 #ifndef MAX_OPEN_PIPES
35 #define MAX_OPEN_PIPES 2048
36 #endif
39 * Sometimes I can't decide if I hate Windows printer driver
40 * writers more than I hate the Windows spooler service driver
41 * writers. This gets around a combination of bugs in the spooler
42 * and the HP 8500 PCL driver that causes a spooler spin. JRA.
44 * bumped up from 20 -> 64 after viewing traffic from WordPerfect
45 * 2002 running on NT 4.- SP6
48 #ifndef MAX_OPEN_SPOOLSS_PIPES
49 #define MAX_OPEN_SPOOLSS_PIPES 256
50 #endif
51 static int current_spoolss_pipes_open;
53 static pipes_struct *Pipes;
54 static struct bitmap *bmap;
56 /****************************************************************************
57 Pipe iterator functions.
58 ****************************************************************************/
60 pipes_struct *get_first_pipe(void)
62 return Pipes;
65 pipes_struct *get_next_pipe(pipes_struct *p)
67 return p->next;
70 /* this must be larger than the sum of the open files and directories */
71 static int pipe_handle_offset;
73 /****************************************************************************
74 Set the pipe_handle_offset. Called from smbd/files.c
75 ****************************************************************************/
77 void set_pipe_handle_offset(int max_open_files)
79 if(max_open_files < 0x7000)
80 pipe_handle_offset = 0x7000;
81 else
82 pipe_handle_offset = max_open_files + 10; /* For safety. :-) */
85 /****************************************************************************
86 Reset pipe chain handle number.
87 ****************************************************************************/
88 void reset_chain_p(void)
90 chain_p = NULL;
93 /****************************************************************************
94 Initialise pipe handle states.
95 ****************************************************************************/
97 void init_rpc_pipe_hnd(void)
99 bmap = bitmap_allocate(MAX_OPEN_PIPES);
100 if (!bmap)
101 exit_server("out of memory in init_rpc_pipe_hnd");
104 /****************************************************************************
105 Initialise an outgoing packet.
106 ****************************************************************************/
108 static BOOL pipe_init_outgoing_data(pipes_struct *p)
110 output_data *o_data = &p->out_data;
112 /* Reset the offset counters. */
113 o_data->data_sent_length = 0;
114 o_data->current_pdu_len = 0;
115 o_data->current_pdu_sent = 0;
117 memset(o_data->current_pdu, '\0', sizeof(o_data->current_pdu));
119 /* Free any memory in the current return data buffer. */
120 prs_mem_free(&o_data->rdata);
123 * Initialize the outgoing RPC data buffer.
124 * we will use this as the raw data area for replying to rpc requests.
126 if(!prs_init(&o_data->rdata, MAX_PDU_FRAG_LEN, p->mem_ctx, MARSHALL)) {
127 DEBUG(0,("pipe_init_outgoing_data: malloc fail.\n"));
128 return False;
131 return True;
134 /****************************************************************************
135 Find first available pipe slot.
136 ****************************************************************************/
138 pipes_struct *open_rpc_pipe_p(char *pipe_name,
139 connection_struct *conn, uint16 vuid)
141 int i;
142 pipes_struct *p;
143 static int next_pipe;
144 BOOL is_spoolss_pipe = False;
146 DEBUG(4,("Open pipe requested %s (pipes_open=%d)\n",
147 pipe_name, pipes_open));
149 if (strstr(pipe_name, "spoolss"))
150 is_spoolss_pipe = True;
152 if (is_spoolss_pipe && current_spoolss_pipes_open >= MAX_OPEN_SPOOLSS_PIPES) {
153 DEBUG(10,("open_rpc_pipe_p: spooler bug workaround. Denying open on pipe %s\n",
154 pipe_name ));
155 return NULL;
158 /* not repeating pipe numbers makes it easier to track things in
159 log files and prevents client bugs where pipe numbers are reused
160 over connection restarts */
161 if (next_pipe == 0)
162 next_pipe = (sys_getpid() ^ time(NULL)) % MAX_OPEN_PIPES;
164 i = bitmap_find(bmap, next_pipe);
166 if (i == -1) {
167 DEBUG(0,("ERROR! Out of pipe structures\n"));
168 return NULL;
171 next_pipe = (i+1) % MAX_OPEN_PIPES;
173 for (p = Pipes; p; p = p->next)
174 DEBUG(5,("open_rpc_pipe_p: name %s pnum=%x\n", p->name, p->pnum));
176 p = (pipes_struct *)malloc(sizeof(*p));
178 if (!p)
179 return NULL;
181 ZERO_STRUCTP(p);
183 if ((p->mem_ctx = talloc_init()) == NULL) {
184 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
185 SAFE_FREE(p);
186 return NULL;
189 if (!init_pipe_handle_list(p, pipe_name)) {
190 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
191 talloc_destroy(p->mem_ctx);
192 SAFE_FREE(p);
193 return NULL;
197 DLIST_ADD(Pipes, p);
200 * Initialize the incoming RPC data buffer with one PDU worth of memory.
201 * We cheat here and say we're marshalling, as we intend to add incoming
202 * data directly into the prs_struct and we want it to auto grow. We will
203 * change the type to UNMARSALLING before processing the stream.
206 if(!prs_init(&p->in_data.data, MAX_PDU_FRAG_LEN, p->mem_ctx, MARSHALL)) {
207 DEBUG(0,("open_rpc_pipe_p: malloc fail for in_data struct.\n"));
208 return NULL;
211 bitmap_set(bmap, i);
212 i += pipe_handle_offset;
214 pipes_open++;
216 p->pnum = i;
218 p->open = True;
219 p->device_state = 0;
220 p->priority = 0;
221 p->conn = conn;
222 p->vuid = vuid;
224 p->max_trans_reply = 0;
226 p->ntlmssp_chal_flags = 0;
227 p->ntlmssp_auth_validated = False;
228 p->ntlmssp_auth_requested = False;
230 p->pipe_bound = False;
231 p->fault_state = False;
232 p->endian = RPC_LITTLE_ENDIAN;
235 * Initialize the incoming RPC struct.
238 p->in_data.pdu_needed_len = 0;
239 p->in_data.pdu_received_len = 0;
242 * Initialize the outgoing RPC struct.
245 p->out_data.current_pdu_len = 0;
246 p->out_data.current_pdu_sent = 0;
247 p->out_data.data_sent_length = 0;
250 * Initialize the outgoing RPC data buffer with no memory.
252 prs_init(&p->out_data.rdata, 0, p->mem_ctx, MARSHALL);
254 ZERO_STRUCT(p->pipe_user);
256 p->pipe_user.uid = (uid_t)-1;
257 p->pipe_user.gid = (gid_t)-1;
259 fstrcpy(p->name, pipe_name);
261 DEBUG(4,("Opened pipe %s with handle %x (pipes_open=%d)\n",
262 pipe_name, i, pipes_open));
264 chain_p = p;
266 /* OVERWRITE p as a temp variable, to display all open pipes */
267 for (p = Pipes; p; p = p->next)
268 DEBUG(5,("open pipes: name %s pnum=%x\n", p->name, p->pnum));
270 if (is_spoolss_pipe)
271 current_spoolss_pipes_open++;
274 * The connection can be idled whilst this pipe is open
275 * if there are no handles open. JRA.
278 return chain_p;
281 /****************************************************************************
282 Sets the fault state on incoming packets.
283 ****************************************************************************/
285 static void set_incoming_fault(pipes_struct *p)
287 prs_mem_free(&p->in_data.data);
288 p->in_data.pdu_needed_len = 0;
289 p->in_data.pdu_received_len = 0;
290 p->fault_state = True;
291 DEBUG(10,("set_incoming_fault: Setting fault state on pipe %s : pnum = 0x%x\n",
292 p->name, p->pnum ));
295 /****************************************************************************
296 Ensures we have at least RPC_HEADER_LEN amount of data in the incoming buffer.
297 ****************************************************************************/
299 static ssize_t fill_rpc_header(pipes_struct *p, char *data, size_t data_to_copy)
301 size_t len_needed_to_complete_hdr = MIN(data_to_copy, RPC_HEADER_LEN - p->in_data.pdu_received_len);
303 DEBUG(10,("fill_rpc_header: data_to_copy = %u, len_needed_to_complete_hdr = %u, receive_len = %u\n",
304 (unsigned int)data_to_copy, (unsigned int)len_needed_to_complete_hdr,
305 (unsigned int)p->in_data.pdu_received_len ));
307 memcpy((char *)&p->in_data.current_in_pdu[p->in_data.pdu_received_len], data, len_needed_to_complete_hdr);
308 p->in_data.pdu_received_len += len_needed_to_complete_hdr;
310 return (ssize_t)len_needed_to_complete_hdr;
313 /****************************************************************************
314 Unmarshalls a new PDU header. Assumes the raw header data is in current_in_pdu.
315 ****************************************************************************/
317 static ssize_t unmarshall_rpc_header(pipes_struct *p)
320 * Unmarshall the header to determine the needed length.
323 prs_struct rpc_in;
325 if(p->in_data.pdu_received_len != RPC_HEADER_LEN) {
326 DEBUG(0,("unmarshall_rpc_header: assert on rpc header length failed.\n"));
327 set_incoming_fault(p);
328 return -1;
331 prs_init( &rpc_in, 0, p->mem_ctx, UNMARSHALL);
332 prs_set_endian_data( &rpc_in, p->endian);
334 prs_give_memory( &rpc_in, (char *)&p->in_data.current_in_pdu[0],
335 p->in_data.pdu_received_len, False);
338 * Unmarshall the header as this will tell us how much
339 * data we need to read to get the complete pdu.
340 * This also sets the endian flag in rpc_in.
343 if(!smb_io_rpc_hdr("", &p->hdr, &rpc_in, 0)) {
344 DEBUG(0,("unmarshall_rpc_header: failed to unmarshall RPC_HDR.\n"));
345 set_incoming_fault(p);
346 prs_mem_free(&rpc_in);
347 return -1;
351 * Validate the RPC header.
354 if(p->hdr.major != 5 && p->hdr.minor != 0) {
355 DEBUG(0,("unmarshall_rpc_header: invalid major/minor numbers in RPC_HDR.\n"));
356 set_incoming_fault(p);
357 prs_mem_free(&rpc_in);
358 return -1;
362 * If there's not data in the incoming buffer this should be the start of a new RPC.
365 if(prs_offset(&p->in_data.data) == 0) {
368 * AS/U doesn't set FIRST flag in a BIND packet it seems.
371 if ((p->hdr.pkt_type == RPC_REQUEST) && !(p->hdr.flags & RPC_FLG_FIRST)) {
373 * Ensure that the FIRST flag is set. If not then we have
374 * a stream missmatch.
377 DEBUG(0,("unmarshall_rpc_header: FIRST flag not set in first PDU !\n"));
378 set_incoming_fault(p);
379 prs_mem_free(&rpc_in);
380 return -1;
384 * If this is the first PDU then set the endianness
385 * flag in the pipe. We will need this when parsing all
386 * data in this RPC.
389 p->endian = rpc_in.bigendian_data;
391 DEBUG(5,("unmarshall_rpc_header: using %sendian RPC\n",
392 p->endian == RPC_LITTLE_ENDIAN ? "little-" : "big-" ));
394 } else {
397 * If this is *NOT* the first PDU then check the endianness
398 * flag in the pipe is the same as that in the PDU.
401 if (p->endian != rpc_in.bigendian_data) {
402 DEBUG(0,("unmarshall_rpc_header: FIRST endianness flag (%d) different in next PDU !\n", (int)p->endian));
403 set_incoming_fault(p);
404 prs_mem_free(&rpc_in);
405 return -1;
410 * Ensure that the pdu length is sane.
413 if((p->hdr.frag_len < RPC_HEADER_LEN) || (p->hdr.frag_len > MAX_PDU_FRAG_LEN)) {
414 DEBUG(0,("unmarshall_rpc_header: assert on frag length failed.\n"));
415 set_incoming_fault(p);
416 prs_mem_free(&rpc_in);
417 return -1;
420 DEBUG(10,("unmarshall_rpc_header: type = %u, flags = %u\n", (unsigned int)p->hdr.pkt_type,
421 (unsigned int)p->hdr.flags ));
424 * Adjust for the header we just ate.
426 p->in_data.pdu_received_len = 0;
427 p->in_data.pdu_needed_len = (uint32)p->hdr.frag_len - RPC_HEADER_LEN;
430 * Null the data we just ate.
433 memset((char *)&p->in_data.current_in_pdu[0], '\0', RPC_HEADER_LEN);
435 prs_mem_free(&rpc_in);
437 return 0; /* No extra data processed. */
440 /****************************************************************************
441 Call this to free any talloc'ed memory. Do this before and after processing
442 a complete PDU.
443 ****************************************************************************/
445 void free_pipe_context(pipes_struct *p)
447 if (p->mem_ctx) {
448 DEBUG(3,("free_pipe_context: destroying talloc pool of size %u\n", talloc_pool_size(p->mem_ctx) ));
449 talloc_destroy_pool(p->mem_ctx);
450 } else {
451 p->mem_ctx = talloc_init();
452 if (p->mem_ctx == NULL)
453 p->fault_state = True;
457 /****************************************************************************
458 Processes a request pdu. This will do auth processing if needed, and
459 appends the data into the complete stream if the LAST flag is not set.
460 ****************************************************************************/
462 static BOOL process_request_pdu(pipes_struct *p, prs_struct *rpc_in_p)
464 BOOL auth_verify = ((p->ntlmssp_chal_flags & NTLMSSP_NEGOTIATE_SIGN) != 0);
465 size_t data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
466 (auth_verify ? RPC_HDR_AUTH_LEN : 0) - p->hdr.auth_len;
468 if(!p->pipe_bound) {
469 DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
470 set_incoming_fault(p);
471 return False;
475 * Check if we need to do authentication processing.
476 * This is only done on requests, not binds.
480 * Read the RPC request header.
483 if(!smb_io_rpc_hdr_req("req", &p->hdr_req, rpc_in_p, 0)) {
484 DEBUG(0,("process_request_pdu: failed to unmarshall RPC_HDR_REQ.\n"));
485 set_incoming_fault(p);
486 return False;
489 if(p->ntlmssp_auth_validated && !api_pipe_auth_process(p, rpc_in_p)) {
490 DEBUG(0,("process_request_pdu: failed to do auth processing.\n"));
491 set_incoming_fault(p);
492 return False;
495 if (p->ntlmssp_auth_requested && !p->ntlmssp_auth_validated) {
498 * Authentication _was_ requested and it already failed.
501 DEBUG(0,("process_request_pdu: RPC request received on pipe %s where \
502 authentication failed. Denying the request.\n", p->name));
503 set_incoming_fault(p);
504 return False;
508 * Check the data length doesn't go over the 15Mb limit.
509 * increased after observing a bug in the Windows NT 4.0 SP6a
510 * spoolsv.exe when the response to a GETPRINTERDRIVER2 RPC
511 * will not fit in the initial buffer of size 0x1068 --jerry 22/01/2002
514 if(prs_offset(&p->in_data.data) + data_len > 15*1024*1024) {
515 DEBUG(0,("process_request_pdu: rpc data buffer too large (%u) + (%u)\n",
516 (unsigned int)prs_data_size(&p->in_data.data), (unsigned int)data_len ));
517 set_incoming_fault(p);
518 return False;
522 * Append the data portion into the buffer and return.
526 char *data_from = prs_data_p(rpc_in_p) + prs_offset(rpc_in_p);
528 if(!prs_append_data(&p->in_data.data, data_from, data_len)) {
529 DEBUG(0,("process_request_pdu: Unable to append data size %u to parse buffer of size %u.\n",
530 (unsigned int)data_len, (unsigned int)prs_data_size(&p->in_data.data) ));
531 set_incoming_fault(p);
532 return False;
537 if(p->hdr.flags & RPC_FLG_LAST) {
538 BOOL ret = False;
540 * Ok - we finally have a complete RPC stream.
541 * Call the rpc command to process it.
545 * Ensure the internal prs buffer size is *exactly* the same
546 * size as the current offset.
549 if(!prs_set_buffer_size(&p->in_data.data, prs_offset(&p->in_data.data)))
551 DEBUG(0,("process_request_pdu: Call to prs_set_buffer_size failed!\n"));
552 set_incoming_fault(p);
553 return False;
557 * Set the parse offset to the start of the data and set the
558 * prs_struct to UNMARSHALL.
561 prs_set_offset(&p->in_data.data, 0);
562 prs_switch_type(&p->in_data.data, UNMARSHALL);
565 * Process the complete data stream here.
568 free_pipe_context(p);
570 if(pipe_init_outgoing_data(p))
571 ret = api_pipe_request(p);
573 free_pipe_context(p);
576 * We have consumed the whole data stream. Set back to
577 * marshalling and set the offset back to the start of
578 * the buffer to re-use it (we could also do a prs_mem_free()
579 * and then re_init on the next start of PDU. Not sure which
580 * is best here.... JRA.
583 prs_switch_type(&p->in_data.data, MARSHALL);
584 prs_set_offset(&p->in_data.data, 0);
585 return ret;
588 return True;
591 /****************************************************************************
592 Processes a finished PDU stored in current_in_pdu. The RPC_HEADER has
593 already been parsed and stored in p->hdr.
594 ****************************************************************************/
596 static ssize_t process_complete_pdu(pipes_struct *p)
598 prs_struct rpc_in;
599 size_t data_len = p->in_data.pdu_received_len;
600 char *data_p = (char *)&p->in_data.current_in_pdu[0];
601 BOOL reply = False;
603 if(p->fault_state) {
604 DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
605 p->name ));
606 set_incoming_fault(p);
607 setup_fault_pdu(p, NT_STATUS(0x1c010002));
608 return (ssize_t)data_len;
611 prs_init( &rpc_in, 0, p->mem_ctx, UNMARSHALL);
614 * Ensure we're using the corrent endianness for both the
615 * RPC header flags and the raw data we will be reading from.
618 prs_set_endian_data( &rpc_in, p->endian);
619 prs_set_endian_data( &p->in_data.data, p->endian);
621 prs_give_memory( &rpc_in, data_p, (uint32)data_len, False);
623 DEBUG(10,("process_complete_pdu: processing packet type %u\n",
624 (unsigned int)p->hdr.pkt_type ));
626 switch (p->hdr.pkt_type) {
627 case RPC_BIND:
628 case RPC_ALTCONT:
630 * We assume that a pipe bind is only in one pdu.
632 if(pipe_init_outgoing_data(p))
633 reply = api_pipe_bind_req(p, &rpc_in);
634 break;
635 case RPC_BINDRESP:
637 * We assume that a pipe bind_resp is only in one pdu.
639 if(pipe_init_outgoing_data(p))
640 reply = api_pipe_bind_auth_resp(p, &rpc_in);
641 break;
642 case RPC_REQUEST:
643 reply = process_request_pdu(p, &rpc_in);
644 break;
645 default:
646 DEBUG(0,("process_complete_pdu: Unknown rpc type = %u received.\n", (unsigned int)p->hdr.pkt_type ));
647 break;
650 /* Reset to little endian. Probably don't need this but it won't hurt. */
651 prs_set_endian_data( &p->in_data.data, RPC_LITTLE_ENDIAN);
653 if (!reply) {
654 DEBUG(3,("process_complete_pdu: DCE/RPC fault sent on pipe %s\n", p->pipe_srv_name));
655 set_incoming_fault(p);
656 setup_fault_pdu(p, NT_STATUS(0x1c010002));
657 prs_mem_free(&rpc_in);
658 } else {
660 * Reset the lengths. We're ready for a new pdu.
662 p->in_data.pdu_needed_len = 0;
663 p->in_data.pdu_received_len = 0;
666 prs_mem_free(&rpc_in);
667 return (ssize_t)data_len;
670 /****************************************************************************
671 Accepts incoming data on an rpc pipe. Processes the data in pdu sized units.
672 ****************************************************************************/
674 static ssize_t process_incoming_data(pipes_struct *p, char *data, size_t n)
676 size_t data_to_copy = MIN(n, MAX_PDU_FRAG_LEN - p->in_data.pdu_received_len);
678 DEBUG(10,("process_incoming_data: Start: pdu_received_len = %u, pdu_needed_len = %u, incoming data = %u\n",
679 (unsigned int)p->in_data.pdu_received_len, (unsigned int)p->in_data.pdu_needed_len,
680 (unsigned int)n ));
682 if(data_to_copy == 0) {
684 * This is an error - data is being received and there is no
685 * space in the PDU. Free the received data and go into the fault state.
687 DEBUG(0,("process_incoming_data: No space in incoming pdu buffer. Current size = %u \
688 incoming data size = %u\n", (unsigned int)p->in_data.pdu_received_len, (unsigned int)n ));
689 set_incoming_fault(p);
690 return -1;
694 * If we have no data already, wait until we get at least a RPC_HEADER_LEN
695 * number of bytes before we can do anything.
698 if((p->in_data.pdu_needed_len == 0) && (p->in_data.pdu_received_len < RPC_HEADER_LEN)) {
700 * Always return here. If we have more data then the RPC_HEADER
701 * will be processed the next time around the loop.
703 return fill_rpc_header(p, data, data_to_copy);
707 * At this point we know we have at least an RPC_HEADER_LEN amount of data
708 * stored in current_in_pdu.
712 * If pdu_needed_len is zero this is a new pdu.
713 * Unmarshall the header so we know how much more
714 * data we need, then loop again.
717 if(p->in_data.pdu_needed_len == 0)
718 return unmarshall_rpc_header(p);
721 * Ok - at this point we have a valid RPC_HEADER in p->hdr.
722 * Keep reading until we have a full pdu.
725 data_to_copy = MIN(data_to_copy, p->in_data.pdu_needed_len);
728 * Copy as much of the data as we need into the current_in_pdu buffer.
731 memcpy( (char *)&p->in_data.current_in_pdu[p->in_data.pdu_received_len], data, data_to_copy);
732 p->in_data.pdu_received_len += data_to_copy;
735 * Do we have a complete PDU ?
738 if(p->in_data.pdu_received_len == p->in_data.pdu_needed_len)
739 return process_complete_pdu(p);
741 DEBUG(10,("process_incoming_data: not a complete PDU yet. pdu_received_len = %u, pdu_needed_len = %u\n",
742 (unsigned int)p->in_data.pdu_received_len, (unsigned int)p->in_data.pdu_needed_len ));
744 return (ssize_t)data_to_copy;
748 /****************************************************************************
749 Accepts incoming data on an rpc pipe.
750 ****************************************************************************/
752 ssize_t write_to_pipe(pipes_struct *p, char *data, size_t n)
754 size_t data_left = n;
756 DEBUG(6,("write_to_pipe: %x", p->pnum));
758 DEBUG(6,(" name: %s open: %s len: %d\n",
759 p->name, BOOLSTR(p->open), (int)n));
761 dump_data(50, data, n);
763 while(data_left) {
764 ssize_t data_used;
766 DEBUG(10,("write_to_pipe: data_left = %u\n", (unsigned int)data_left ));
768 data_used = process_incoming_data(p, data, data_left);
770 DEBUG(10,("write_to_pipe: data_used = %d\n", (int)data_used ));
772 if(data_used < 0)
773 return -1;
775 data_left -= data_used;
776 data += data_used;
779 return n;
782 /****************************************************************************
783 Replies to a request to read data from a pipe.
785 Headers are interspersed with the data at PDU intervals. By the time
786 this function is called, the start of the data could possibly have been
787 read by an SMBtrans (file_offset != 0).
789 Calling create_rpc_reply() here is a hack. The data should already
790 have been prepared into arrays of headers + data stream sections.
791 ****************************************************************************/
793 ssize_t read_from_pipe(pipes_struct *p, char *data, size_t n)
795 uint32 pdu_remaining = 0;
796 ssize_t data_returned = 0;
798 if (!p || !p->open) {
799 DEBUG(0,("read_from_pipe: pipe not open\n"));
800 return -1;
803 DEBUG(6,("read_from_pipe: %x", p->pnum));
805 DEBUG(6,(" name: %s len: %u\n", p->name, (unsigned int)n));
808 * We cannot return more than one PDU length per
809 * read request.
813 * This condition should result in the connection being closed.
814 * Netapp filers seem to set it to 0xffff which results in domain
815 * authentications failing. Just ignore it so things work.
818 if(n > MAX_PDU_FRAG_LEN) {
819 DEBUG(5,("read_from_pipe: too large read (%u) requested on \
820 pipe %s. We can only service %d sized reads.\n", (unsigned int)n, p->name, MAX_PDU_FRAG_LEN ));
824 * Determine if there is still data to send in the
825 * pipe PDU buffer. Always send this first. Never
826 * send more than is left in the current PDU. The
827 * client should send a new read request for a new
828 * PDU.
831 if((pdu_remaining = p->out_data.current_pdu_len - p->out_data.current_pdu_sent) > 0) {
832 data_returned = (ssize_t)MIN(n, pdu_remaining);
834 DEBUG(10,("read_from_pipe: %s: current_pdu_len = %u, current_pdu_sent = %u \
835 returning %d bytes.\n", p->name, (unsigned int)p->out_data.current_pdu_len,
836 (unsigned int)p->out_data.current_pdu_sent, (int)data_returned));
838 memcpy( data, &p->out_data.current_pdu[p->out_data.current_pdu_sent], (size_t)data_returned);
839 p->out_data.current_pdu_sent += (uint32)data_returned;
840 goto out;
844 * At this point p->current_pdu_len == p->current_pdu_sent (which
845 * may of course be zero if this is the first return fragment.
848 DEBUG(10,("read_from_pipe: %s: fault_state = %d : data_sent_length \
849 = %u, prs_offset(&p->out_data.rdata) = %u.\n",
850 p->name, (int)p->fault_state, (unsigned int)p->out_data.data_sent_length, (unsigned int)prs_offset(&p->out_data.rdata) ));
852 if(p->out_data.data_sent_length >= prs_offset(&p->out_data.rdata)) {
854 * We have sent all possible data, return 0.
856 data_returned = 0;
857 goto out;
861 * We need to create a new PDU from the data left in p->rdata.
862 * Create the header/data/footers. This also sets up the fields
863 * p->current_pdu_len, p->current_pdu_sent, p->data_sent_length
864 * and stores the outgoing PDU in p->current_pdu.
867 if(!create_next_pdu(p)) {
868 DEBUG(0,("read_from_pipe: %s: create_next_pdu failed.\n", p->name));
869 return -1;
872 data_returned = MIN(n, p->out_data.current_pdu_len);
874 memcpy( data, p->out_data.current_pdu, (size_t)data_returned);
875 p->out_data.current_pdu_sent += (uint32)data_returned;
877 out:
879 return data_returned;
882 /****************************************************************************
883 Wait device state on a pipe. Exactly what this is for is unknown...
884 ****************************************************************************/
886 BOOL wait_rpc_pipe_hnd_state(pipes_struct *p, uint16 priority)
888 if (p == NULL)
889 return False;
891 if (p->open) {
892 DEBUG(3,("wait_rpc_pipe_hnd_state: Setting pipe wait state priority=%x on pipe (name=%s)\n",
893 priority, p->name));
895 p->priority = priority;
897 return True;
900 DEBUG(3,("wait_rpc_pipe_hnd_state: Error setting pipe wait state priority=%x (name=%s)\n",
901 priority, p->name));
902 return False;
906 /****************************************************************************
907 Set device state on a pipe. Exactly what this is for is unknown...
908 ****************************************************************************/
910 BOOL set_rpc_pipe_hnd_state(pipes_struct *p, uint16 device_state)
912 if (p == NULL)
913 return False;
915 if (p->open) {
916 DEBUG(3,("set_rpc_pipe_hnd_state: Setting pipe device state=%x on pipe (name=%s)\n",
917 device_state, p->name));
919 p->device_state = device_state;
921 return True;
924 DEBUG(3,("set_rpc_pipe_hnd_state: Error setting pipe device state=%x (name=%s)\n",
925 device_state, p->name));
926 return False;
930 /****************************************************************************
931 Close an rpc pipe.
932 ****************************************************************************/
934 BOOL close_rpc_pipe_hnd(pipes_struct *p, connection_struct *conn)
936 if (!p) {
937 DEBUG(0,("Invalid pipe in close_rpc_pipe_hnd\n"));
938 return False;
941 if (strstr(p->name, "spoolss"))
942 current_spoolss_pipes_open--;
944 prs_mem_free(&p->out_data.rdata);
945 prs_mem_free(&p->in_data.data);
947 if (p->mem_ctx)
948 talloc_destroy(p->mem_ctx);
950 /* Free the handles database. */
951 close_policy_by_pipe(p);
953 bitmap_clear(bmap, p->pnum - pipe_handle_offset);
955 pipes_open--;
957 DEBUG(4,("closed pipe name %s pnum=%x (pipes_open=%d)\n",
958 p->name, p->pnum, pipes_open));
960 DLIST_REMOVE(Pipes, p);
962 delete_nt_token(&p->pipe_user.nt_user_token);
963 safe_free(p->pipe_user.groups);
965 ZERO_STRUCTP(p);
967 SAFE_FREE(p);
969 return True;
972 /****************************************************************************
973 Find an rpc pipe given a pipe handle in a buffer and an offset.
974 ****************************************************************************/
976 pipes_struct *get_rpc_pipe_p(char *buf, int where)
978 int pnum = SVAL(buf,where);
980 if (chain_p)
981 return chain_p;
983 return get_rpc_pipe(pnum);
986 /****************************************************************************
987 Find an rpc pipe given a pipe handle.
988 ****************************************************************************/
990 pipes_struct *get_rpc_pipe(int pnum)
992 pipes_struct *p;
994 DEBUG(4,("search for pipe pnum=%x\n", pnum));
996 for (p=Pipes;p;p=p->next)
997 DEBUG(5,("pipe name %s pnum=%x (pipes_open=%d)\n",
998 p->name, p->pnum, pipes_open));
1000 for (p=Pipes;p;p=p->next) {
1001 if (p->pnum == pnum) {
1002 chain_p = p;
1003 return p;
1007 return NULL;