Revert "Make get_rpc_pipe() static"
[samba.git] / source3 / rpc_server / srv_pipe_hnd.c
index 0dd3ee82a73ed1eb8c1c2059682373702db7baa4..c55e3287051965047ddbc232b2e7167932066b27 100644 (file)
@@ -24,9 +24,6 @@
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
-#define        PIPE            "\\PIPE\\"
-#define        PIPELEN         strlen(PIPE)
-
 static smb_np_struct *chain_p;
 static int pipes_open;
 
@@ -61,26 +58,7 @@ static struct bitmap *bmap;
  * system _anyway_.  so that's the next step...
  */
 
-static ssize_t read_from_internal_pipe(void *np_conn, char *data, size_t n,
-               BOOL *is_data_outstanding);
-static ssize_t write_to_internal_pipe(void *np_conn, char *data, size_t n);
-static BOOL close_internal_rpc_pipe_hnd(void *np_conn);
-static void *make_internal_rpc_pipe_p(const char *pipe_name, 
-                             connection_struct *conn, uint16 vuid);
-
-/****************************************************************************
- Pipe iterator functions.
-****************************************************************************/
-
-smb_np_struct *get_first_pipe(void)
-{
-       return Pipes;
-}
-
-smb_np_struct *get_next_pipe(smb_np_struct *p)
-{
-       return p->next;
-}
+static int close_internal_rpc_pipe_hnd(struct pipes_struct *p);
 
 /****************************************************************************
  Internal Pipe iterator functions.
@@ -137,7 +115,7 @@ void init_rpc_pipe_hnd(void)
  Initialise an outgoing packet.
 ****************************************************************************/
 
-static BOOL pipe_init_outgoing_data(pipes_struct *p)
+static bool pipe_init_outgoing_data(pipes_struct *p)
 {
        output_data *o_data = &p->out_data;
 
@@ -173,7 +151,7 @@ smb_np_struct *open_rpc_pipe_p(const char *pipe_name,
        int i;
        smb_np_struct *p, *p_it;
        static int next_pipe;
-       BOOL is_spoolss_pipe = False;
+       bool is_spoolss_pipe = False;
 
        DEBUG(4,("Open pipe requested %s (pipes_open=%d)\n",
                 pipe_name, pipes_open));
@@ -209,26 +187,33 @@ smb_np_struct *open_rpc_pipe_p(const char *pipe_name,
                DEBUG(5,("open_rpc_pipe_p: name %s pnum=%x\n", p->name, p->pnum));  
        }
 
-       p = SMB_MALLOC_P(smb_np_struct);
+       p = talloc(NULL, smb_np_struct);
        if (!p) {
-               DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
+               DEBUG(0,("ERROR! no memory for smb_np_struct!\n"));
                return NULL;
        }
 
        ZERO_STRUCTP(p);
 
+       p->name = talloc_strdup(p, pipe_name);
+       if (p->name == NULL) {
+               TALLOC_FREE(p);
+               DEBUG(0,("ERROR! no memory for pipe name!\n"));
+               return NULL;
+       }
+
        /* add a dso mechanism instead of this, here */
 
        p->namedpipe_create = make_internal_rpc_pipe_p;
        p->namedpipe_read = read_from_internal_pipe;
        p->namedpipe_write = write_to_internal_pipe;
-       p->namedpipe_close = close_internal_rpc_pipe_hnd;
 
-       p->np_state = p->namedpipe_create(pipe_name, conn, vuid);
+       p->np_state = p->namedpipe_create(pipe_name, conn->client_address,
+                                         conn->server_info, vuid);
 
        if (p->np_state == NULL) {
                DEBUG(0,("open_rpc_pipe_p: make_internal_rpc_pipe_p failed.\n"));
-               SAFE_FREE(p);
+               TALLOC_FREE(p);
                return NULL;
        }
 
@@ -255,9 +240,7 @@ smb_np_struct *open_rpc_pipe_p(const char *pipe_name,
        p->vuid  = vuid;
 
        p->max_trans_reply = 0;
-       
-       fstrcpy(p->name, pipe_name);
-       
+
        DEBUG(4,("Opened pipe %s with handle %x (pipes_open=%d)\n",
                 pipe_name, i, pipes_open));
        
@@ -275,46 +258,32 @@ smb_np_struct *open_rpc_pipe_p(const char *pipe_name,
  Make an internal namedpipes structure
 ****************************************************************************/
 
-static void *make_internal_rpc_pipe_p(const char *pipe_name, 
-                             connection_struct *conn, uint16 vuid)
+struct pipes_struct *make_internal_rpc_pipe_p(const char *pipe_name,
+                                             const char *client_address,
+                                             struct auth_serversupplied_info *server_info,
+                                             uint16_t vuid)
 {
        pipes_struct *p;
-       user_struct *vuser = get_valid_user_struct(vuid);
 
        DEBUG(4,("Create pipe requested %s\n", pipe_name));
 
-       if (!vuser && vuid != UID_FIELD_INVALID) {
-               DEBUG(0,("ERROR! vuid %d did not map to a valid vuser struct!\n", vuid));
-               return NULL;
-       }
-
-       p = SMB_MALLOC_P(pipes_struct);
+       p = TALLOC_ZERO_P(NULL, pipes_struct);
 
        if (!p) {
                DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
                return NULL;
        }
 
-       ZERO_STRUCTP(p);
-
        if ((p->mem_ctx = talloc_init("pipe %s %p", pipe_name, p)) == NULL) {
                DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
-               SAFE_FREE(p);
-               return NULL;
-       }
-
-       if ((p->pipe_state_mem_ctx = talloc_init("pipe_state %s %p", pipe_name, p)) == NULL) {
-               DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
-               talloc_destroy(p->mem_ctx);
-               SAFE_FREE(p);
+               TALLOC_FREE(p);
                return NULL;
        }
 
        if (!init_pipe_handle_list(p, pipe_name)) {
                DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
                talloc_destroy(p->mem_ctx);
-               talloc_destroy(p->pipe_state_mem_ctx);
-               SAFE_FREE(p);
+               TALLOC_FREE(p);
                return NULL;
        }
 
@@ -328,41 +297,46 @@ static void *make_internal_rpc_pipe_p(const char *pipe_name,
        if(!prs_init(&p->in_data.data, RPC_MAX_PDU_FRAG_LEN, p->mem_ctx, MARSHALL)) {
                DEBUG(0,("open_rpc_pipe_p: malloc fail for in_data struct.\n"));
                talloc_destroy(p->mem_ctx);
-               talloc_destroy(p->pipe_state_mem_ctx);
                close_policy_by_pipe(p);
-               SAFE_FREE(p);
+               TALLOC_FREE(p);
                return NULL;
        }
 
-       DLIST_ADD(InternalPipes, p);
+       p->server_info = copy_serverinfo(p, server_info);
+       if (p->server_info == NULL) {
+               DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
+               talloc_destroy(p->mem_ctx);
+               close_policy_by_pipe(p);
+               TALLOC_FREE(p);
+               return NULL;
+       }
 
-       p->conn = conn;
+       DLIST_ADD(InternalPipes, p);
 
-       p->vuid  = vuid;
+       memcpy(p->client_address, client_address, sizeof(p->client_address));
 
        p->endian = RPC_LITTLE_ENDIAN;
 
        ZERO_STRUCT(p->pipe_user);
 
+       p->pipe_user.vuid = vuid;
        p->pipe_user.ut.uid = (uid_t)-1;
        p->pipe_user.ut.gid = (gid_t)-1;
-       
-       /* Store the session key and NT_TOKEN */
-       if (vuser) {
-               p->session_key = data_blob(vuser->session_key.data, vuser->session_key.length);
-       }
+       p->pipe_user.nt_user_token = dup_nt_token(NULL, server_info->ptok);
 
        /*
         * Initialize the outgoing RPC data buffer with no memory.
         */     
-       prs_init(&p->out_data.rdata, 0, p->mem_ctx, MARSHALL);
+       prs_init_empty(&p->out_data.rdata, p->mem_ctx, MARSHALL);
        
        fstrcpy(p->name, pipe_name);
        
        DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
                 pipe_name, pipes_open));
 
-       return (void*)p;
+       talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
+
+       return p;
 }
 
 /****************************************************************************
@@ -375,8 +349,8 @@ static void set_incoming_fault(pipes_struct *p)
        p->in_data.pdu_needed_len = 0;
        p->in_data.pdu_received_len = 0;
        p->fault_state = True;
-       DEBUG(10,("set_incoming_fault: Setting fault state on pipe %s : vuid = 0x%x\n",
-               p->name, p->vuid ));
+       DEBUG(10, ("set_incoming_fault: Setting fault state on pipe %s\n",
+                  p->name));
 }
 
 /****************************************************************************
@@ -415,7 +389,7 @@ static ssize_t unmarshall_rpc_header(pipes_struct *p)
                return -1;
        }
 
-       prs_init( &rpc_in, 0, p->mem_ctx, UNMARSHALL);
+       prs_init_empty( &rpc_in, p->mem_ctx, UNMARSHALL);
        prs_set_endian_data( &rpc_in, p->endian);
 
        prs_give_memory( &rpc_in, (char *)&p->in_data.current_in_pdu[0],
@@ -538,7 +512,7 @@ static void free_pipe_context(pipes_struct *p)
  appends the data into the complete stream if the LAST flag is not set.
 ****************************************************************************/
 
-static BOOL process_request_pdu(pipes_struct *p, prs_struct *rpc_in_p)
+static bool process_request_pdu(pipes_struct *p, prs_struct *rpc_in_p)
 {
        uint32 ss_padding_len = 0;
        size_t data_len = p->hdr.frag_len - RPC_HEADER_LEN - RPC_HDR_REQ_LEN -
@@ -627,7 +601,7 @@ static BOOL process_request_pdu(pipes_struct *p, prs_struct *rpc_in_p)
        }
 
        if(p->hdr.flags & RPC_FLG_LAST) {
-               BOOL ret = False;
+               bool ret = False;
                /*
                 * Ok - we finally have a complete RPC stream.
                 * Call the rpc command to process it.
@@ -690,7 +664,7 @@ static void process_complete_pdu(pipes_struct *p)
        prs_struct rpc_in;
        size_t data_len = p->in_data.pdu_received_len - RPC_HEADER_LEN;
        char *data_p = (char *)&p->in_data.current_in_pdu[RPC_HEADER_LEN];
-       BOOL reply = False;
+       bool reply = False;
 
        if(p->fault_state) {
                DEBUG(10,("process_complete_pdu: pipe %s in fault state.\n",
@@ -700,7 +674,7 @@ static void process_complete_pdu(pipes_struct *p)
                return;
        }
 
-       prs_init( &rpc_in, 0, p->mem_ctx, UNMARSHALL);
+       prs_init_empty( &rpc_in, p->mem_ctx, UNMARSHALL);
 
        /*
         * Ensure we're using the corrent endianness for both the 
@@ -945,9 +919,8 @@ ssize_t write_to_pipe(smb_np_struct *p, char *data, size_t n)
  Accepts incoming data on an internal rpc pipe.
 ****************************************************************************/
 
-static ssize_t write_to_internal_pipe(void *np_conn, char *data, size_t n)
+ssize_t write_to_internal_pipe(struct pipes_struct *p, char *data, size_t n)
 {
-       pipes_struct *p = (pipes_struct*)np_conn;
        size_t data_left = n;
 
        while(data_left) {
@@ -982,7 +955,7 @@ static ssize_t write_to_internal_pipe(void *np_conn, char *data, size_t n)
 ****************************************************************************/
 
 ssize_t read_from_pipe(smb_np_struct *p, char *data, size_t n,
-               BOOL *is_data_outstanding)
+               bool *is_data_outstanding)
 {
        if (!p || !p->open) {
                DEBUG(0,("read_from_pipe: pipe not open\n"));
@@ -1005,10 +978,9 @@ ssize_t read_from_pipe(smb_np_struct *p, char *data, size_t n,
  have been prepared into arrays of headers + data stream sections.
 ****************************************************************************/
 
-static ssize_t read_from_internal_pipe(void *np_conn, char *data, size_t n,
-               BOOL *is_data_outstanding)
+ssize_t read_from_internal_pipe(struct pipes_struct *p, char *data, size_t n,
+                               bool *is_data_outstanding)
 {
-       pipes_struct *p = (pipes_struct*)np_conn;
        uint32 pdu_remaining = 0;
        ssize_t data_returned = 0;
 
@@ -1100,7 +1072,7 @@ returning %d bytes.\n", p->name, (unsigned int)p->out_data.current_pdu_len,
  Wait device state on a pipe. Exactly what this is for is unknown...
 ****************************************************************************/
 
-BOOL wait_rpc_pipe_hnd_state(smb_np_struct *p, uint16 priority)
+bool wait_rpc_pipe_hnd_state(smb_np_struct *p, uint16 priority)
 {
        if (p == NULL) {
                return False;
@@ -1125,7 +1097,7 @@ BOOL wait_rpc_pipe_hnd_state(smb_np_struct *p, uint16 priority)
  Set device state on a pipe. Exactly what this is for is unknown...
 ****************************************************************************/
 
-BOOL set_rpc_pipe_hnd_state(smb_np_struct *p, uint16 device_state)
+bool set_rpc_pipe_hnd_state(smb_np_struct *p, uint16 device_state)
 {
        if (p == NULL) {
                return False;
@@ -1150,14 +1122,14 @@ BOOL set_rpc_pipe_hnd_state(smb_np_struct *p, uint16 device_state)
  Close an rpc pipe.
 ****************************************************************************/
 
-BOOL close_rpc_pipe_hnd(smb_np_struct *p)
+bool close_rpc_pipe_hnd(smb_np_struct *p)
 {
        if (!p) {
                DEBUG(0,("Invalid pipe in close_rpc_pipe_hnd\n"));
                return False;
        }
 
-       p->namedpipe_close(p->np_state);
+       TALLOC_FREE(p->np_state);
 
        bitmap_clear(bmap, p->pnum - pipe_handle_offset);
 
@@ -1175,9 +1147,7 @@ BOOL close_rpc_pipe_hnd(smb_np_struct *p)
                        "pipe from open db.\n", p->name));
        }
 
-       ZERO_STRUCTP(p);
-
-       SAFE_FREE(p);
+       TALLOC_FREE(p);
 
        return True;
 }
@@ -1202,9 +1172,8 @@ void pipe_close_conn(connection_struct *conn)
  Close an rpc pipe.
 ****************************************************************************/
 
-static BOOL close_internal_rpc_pipe_hnd(void *np_conn)
+static int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
 {
-       pipes_struct *p = (pipes_struct *)np_conn;
        if (!p) {
                DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
                return False;
@@ -1221,24 +1190,19 @@ static BOOL close_internal_rpc_pipe_hnd(void *np_conn)
                talloc_destroy(p->mem_ctx);
        }
 
-       if (p->pipe_state_mem_ctx) {
-               talloc_destroy(p->pipe_state_mem_ctx);
-       }
-
        free_pipe_rpc_context( p->contexts );
 
        /* Free the handles database. */
        close_policy_by_pipe(p);
 
        TALLOC_FREE(p->pipe_user.nt_user_token);
-       data_blob_free(&p->session_key);
        SAFE_FREE(p->pipe_user.ut.groups);
 
        DLIST_REMOVE(InternalPipes, p);
 
        ZERO_STRUCTP(p);
 
-       SAFE_FREE(p);
+       TALLOC_FREE(p);
        
        return True;
 }