corrections to get data stream for 2nd and subsequent pdus copied from
[samba.git] / source / rpc_server / srv_pipe_hnd.c
index 54b9ce1539386db81d8f01579234f62cf7f77d61..ca5dde18d25a07d8b2ed9661800feea59412245f 100644 (file)
@@ -3,8 +3,8 @@
  *  Unix SMB/Netbios implementation.
  *  Version 1.9.
  *  RPC Pipe client / server routines
- *  Copyright (C) Andrew Tridgell              1992-1997,
- *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
+ *  Copyright (C) Andrew Tridgell              1992-1998,
+ *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -28,9 +28,6 @@
 #define        PIPE            "\\PIPE\\"
 #define        PIPELEN         strlen(PIPE)
 
-/* this must be larger than the sum of the open files and directories */
-#define PIPE_HANDLE_OFFSET 0x7000
-
 extern int DEBUGLEVEL;
 static pipes_struct *chain_p;
 static int pipes_open;
@@ -42,6 +39,21 @@ static int pipes_open;
 static pipes_struct *Pipes;
 static struct bitmap *bmap;
 
+/* this must be larger than the sum of the open files and directories */
+static int pipe_handle_offset;
+
+/****************************************************************************
+ Set the pipe_handle_offset. Called from smbd/files.c
+****************************************************************************/
+
+void set_pipe_handle_offset(int max_open_files)
+{
+  if(max_open_files < 0x7000)
+    pipe_handle_offset = 0x7000;
+  else
+    pipe_handle_offset = max_open_files + 10; /* For safety. :-) */
+}
+
 /****************************************************************************
   reset pipe chain handle number
 ****************************************************************************/
@@ -72,6 +84,9 @@ pipes_struct *open_rpc_pipe_p(char *pipe_name,
        pipes_struct *p;
        static int next_pipe;
 
+       DEBUG(4,("Open pipe requested %s (pipes_open=%d)\n",
+                pipe_name, pipes_open));
+       
        /* not repeating pipe numbers makes it easier to track things in 
           log files and prevents client bugs where pipe numbers are reused
           over connection restarts */
@@ -88,23 +103,29 @@ pipes_struct *open_rpc_pipe_p(char *pipe_name,
 
        next_pipe = (i+1) % MAX_OPEN_PIPES;
 
+       for (p = Pipes; p; p = p->next)
+       {
+               DEBUG(5,("open pipes: name %s pnum=%x\n", p->name, p->pnum));  
+       }
+
        p = (pipes_struct *)malloc(sizeof(*p));
        if (!p) return NULL;
 
+       ZERO_STRUCTP(p);
        DLIST_ADD(Pipes, p);
 
        bitmap_set(bmap, i);
-       i += PIPE_HANDLE_OFFSET;
+       i += pipe_handle_offset;
 
        pipes_open++;
 
-       memset(p, 0, sizeof(*p));
        p->pnum = i;
 
        p->open = True;
        p->device_state = 0;
+       p->priority = 0;
        p->conn = conn;
-       p->uid  = vuid;
+       p->vuid  = vuid;
        
        p->rhdr.data  = NULL;
        p->rdata.data = NULL;
@@ -112,9 +133,11 @@ pipes_struct *open_rpc_pipe_p(char *pipe_name,
        p->rdata.offset = 0;
        
        p->file_offset     = 0;
+       p->prev_pdu_file_offset = 0;
        p->hdr_offsets     = 0;
-       p->frag_len_left   = 0;
-       p->next_frag_start = 0;
+       
+       p->ntlmssp_validated = False;
+       p->ntlmssp_auth      = False;
        
        fstrcpy(p->name, pipe_name);
        
@@ -123,7 +146,45 @@ pipes_struct *open_rpc_pipe_p(char *pipe_name,
        
        chain_p = p;
        
-       return p;
+       /* OVERWRITE p as a temp variable, to display all open pipes */ 
+       for (p = Pipes; p; p = p->next)
+       {
+               DEBUG(5,("open pipes: name %s pnum=%x\n", p->name, p->pnum));  
+       }
+
+       return chain_p;
+}
+
+
+/****************************************************************************
+ writes data to a pipe.
+
+ SERIOUSLY ALPHA CODE!
+ ****************************************************************************/
+ssize_t write_pipe(pipes_struct *p, char *data, size_t n)
+{
+       prs_struct pd;
+       struct mem_buf data_buf;
+
+       DEBUG(6,("write_pipe: %x", p->pnum));
+
+       DEBUG(6,("name: %s open: %s len: %d",
+                p->name, BOOLSTR(p->open), n));
+
+       dump_data(50, data, n);
+
+       /* fake up a data buffer from the write_pipe data parameters */
+       mem_create(&data_buf, data, 0, n, 0, False);
+       data_buf.offset.start = 0;
+       data_buf.offset.end   = n;
+
+       /* fake up a parsing structure */
+       pd.data = &data_buf;
+       pd.align = 4;
+       pd.io = True;
+       pd.offset = 0;
+
+       return rpc_command(p, &pd) ? ((ssize_t)n) : -1;
 }
 
 
@@ -134,17 +195,18 @@ pipes_struct *open_rpc_pipe_p(char *pipe_name,
  this function is called, the start of the data could possibly have been
  read by an SMBtrans (file_offset != 0).
 
- calling create_rpc_request() here is a fudge.  the data should already
+ calling create_rpc_reply() here is a fudge.  the data should already
  have been prepared into arrays of headers + data stream sections.
 
  ****************************************************************************/
 int read_pipe(pipes_struct *p, char *data, uint32 pos, int n)
 {
        int num = 0;
-       int len = 0;
+       int pdu_len = 0;
        uint32 hdr_num = 0;
-       int data_hdr_pos;
-       int data_pos;
+       int pdu_data_sent; /* amount of current pdu already sent */
+       int data_pos; /* entire rpc data sent - no headers, no auth verifiers */
+       int this_pdu_data_pos;
 
        DEBUG(6,("read_pipe: %x", p->pnum));
 
@@ -153,87 +215,108 @@ int read_pipe(pipes_struct *p, char *data, uint32 pos, int n)
                 BOOLSTR(p->open),
                 pos, n));
 
-       if (!p || !p->open) {
+       if (!p || !p->open)
+       {
                DEBUG(6,("pipe not open\n"));
                return -1;              
        }
 
 
        if (p->rhdr.data == NULL || p->rhdr.data->data == NULL ||
-           p->rhdr.data->data_used == 0) {
+           p->rhdr.data->data_used == 0)
+       {
                return 0;
        }
 
        DEBUG(6,("read_pipe: p: %p file_offset: %d file_pos: %d\n",
                 p, p->file_offset, n));
-       DEBUG(6,("read_pipe: frag_len_left: %d next_frag_start: %d\n",
-                p->frag_len_left, p->next_frag_start));
 
        /* the read request starts from where the SMBtrans2 left off. */
-       data_pos     = p->file_offset - p->hdr_offsets;
-       data_hdr_pos = p->file_offset;
+       data_pos = p->file_offset - p->hdr_offsets;
+       this_pdu_data_pos = data_pos - p->prev_pdu_file_offset;
+       pdu_data_sent = p->file_offset - p->prev_pdu_file_offset;
 
-       len = mem_buf_len(p->rhdr.data);
-       num = len - (int)data_pos;
-       
-       DEBUG(6,("read_pipe: len: %d num: %d n: %d\n", len, num, n));
-       
-       if (num > n) num = n;
-       if (num <= 0) {
-               DEBUG(5,("read_pipe: 0 or -ve data length\n"));
-               return 0;
-       }
-
-       if (!IS_BITS_SET_ALL(p->hdr.flags, RPC_FLG_LAST)) {
+       if (!IS_BITS_SET_ALL(p->hdr.flags, RPC_FLG_LAST))
+       {
                /* intermediate fragment - possibility of another header */
                
-               DEBUG(5,("read_pipe: frag_len: %d data_pos: %d data_hdr_pos: %d\n",
-                        p->hdr.frag_len, data_pos, data_hdr_pos));
+               DEBUG(5,("read_pipe: frag_len: %d data_pos: %d pdu_data_sent: %d\n",
+                        p->hdr.frag_len, data_pos, pdu_data_sent));
                
-               if (data_hdr_pos == p->next_frag_start) {
+               if (pdu_data_sent == 0)
+               {
                        DEBUG(6,("read_pipe: next fragment header\n"));
 
                        /* this is subtracted from the total data bytes, later */
                        hdr_num = 0x18;
+                       p->hdr_offsets += 0x18;
+                       data_pos -= 0x18;
 
                        /* create and copy in a new header. */
                        create_rpc_reply(p, data_pos, p->rdata.offset);
-                       mem_buf_copy(data, p->rhdr.data, 0, 0x18);
-                       
-                       data += 0x18;
-                       p->frag_len_left = p->hdr.frag_len;
-                       p->next_frag_start += p->hdr.frag_len;
-                       p->hdr_offsets += 0x18;
                }                       
-               
        }
        
-       if (num < hdr_num) {
+       pdu_len = mem_buf_len(p->rhdr.data);
+       num = pdu_len - (int)this_pdu_data_pos;
+       
+       DEBUG(6,("read_pipe: pdu_len: %d num: %d n: %d\n", pdu_len, num, n));
+       
+       if (num > n) num = n;
+       if (num <= 0)
+       {
+               DEBUG(5,("read_pipe: 0 or -ve data length\n"));
+               return 0;
+       }
+
+       if (num < hdr_num)
+       {
                DEBUG(5,("read_pipe: warning - data read only part of a header\n"));
        }
 
-       DEBUG(6,("read_pipe: adjusted data_pos: %d num-hdr_num: %d\n",
-                data_pos, num - hdr_num));
-       mem_buf_copy(data, p->rhdr.data, data_pos, num - hdr_num);
+       mem_buf_copy(data, p->rhdr.data, pdu_data_sent, num);
        
-       data_pos += num;
-       data_hdr_pos += num;
+       p->file_offset  += num;
+       pdu_data_sent  += num;
        
-       if (hdr_num == 0x18 && num == 0x18) {
+       if (hdr_num == 0x18 && num == 0x18)
+       {
                DEBUG(6,("read_pipe: just header read\n"));
+       }
 
-               /* advance to the next fragment */
-               p->frag_len_left -= 0x18; 
-       } else if (data_hdr_pos == p->next_frag_start) {
+       if (pdu_data_sent == p->hdr.frag_len)
+       {
                DEBUG(6,("read_pipe: next fragment expected\n"));
+               p->prev_pdu_file_offset = p->file_offset;
        }
 
-       p->file_offset  += num;
-       
        return num;
 }
 
 
+/****************************************************************************
+  wait device state on a pipe.  exactly what this is for is unknown...
+****************************************************************************/
+BOOL wait_rpc_pipe_hnd_state(pipes_struct *p, uint16 priority)
+{
+       if (p == NULL) return False;
+
+       if (p->open)
+       {
+               DEBUG(3,("%s Setting pipe wait state priority=%x on pipe (name=%s)\n",
+                        timestring(), priority, p->name));
+
+               p->priority = priority;
+               
+               return True;
+       } 
+
+       DEBUG(3,("%s Error setting pipe wait state priority=%x (name=%s)\n",
+                timestring(), priority, p->name));
+       return False;
+}
+
+
 /****************************************************************************
   set device state on a pipe.  exactly what this is for is unknown...
 ****************************************************************************/
@@ -269,7 +352,7 @@ BOOL close_rpc_pipe_hnd(pipes_struct *p, connection_struct *conn)
        mem_buf_free(&(p->rdata.data));
        mem_buf_free(&(p->rhdr .data));
 
-       bitmap_clear(bmap, p->pnum - PIPE_HANDLE_OFFSET);
+       bitmap_clear(bmap, p->pnum - pipe_handle_offset);
 
        pipes_open--;
 
@@ -278,7 +361,7 @@ BOOL close_rpc_pipe_hnd(pipes_struct *p, connection_struct *conn)
 
        DLIST_REMOVE(Pipes, p);
 
-       memset(p, 0, sizeof(*p));
+       ZERO_STRUCTP(p);
 
        free(p);
        
@@ -304,8 +387,18 @@ pipes_struct *get_rpc_pipe(int pnum)
 {
        pipes_struct *p;
 
-       for (p=Pipes;p;p=p->next) {
-               if (p->pnum == pnum) {
+       DEBUG(4,("search for pipe pnum=%x\n", pnum));
+
+       for (p=Pipes;p;p=p->next)
+       {
+               DEBUG(5,("pipe name %s pnum=%x (pipes_open=%d)\n", 
+                         p->name, p->pnum, pipes_open));  
+       }
+
+       for (p=Pipes;p;p=p->next)
+       {
+               if (p->pnum == pnum)
+               {
                        chain_p = p;
                        return p;
                }