#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;
/*
#endif
static int current_spoolss_pipes_open;
-static smb_np_struct *Pipes;
static pipes_struct *InternalPipes;
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(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.
}
}
-/****************************************************************************
- Reset pipe chain handle number.
-****************************************************************************/
-
-void reset_chain_p(void)
-{
- chain_p = NULL;
-}
-
/****************************************************************************
Initialise pipe handle states.
****************************************************************************/
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;
return True;
}
-/****************************************************************************
- Find first available pipe slot.
-****************************************************************************/
-
-smb_np_struct *open_rpc_pipe_p(char *pipe_name,
- connection_struct *conn, uint16 vuid)
-{
- int i;
- smb_np_struct *p, *p_it;
- static int next_pipe;
- BOOL is_spoolss_pipe = False;
-
- DEBUG(4,("Open pipe requested %s (pipes_open=%d)\n",
- pipe_name, pipes_open));
-
- if (strstr(pipe_name, "spoolss")) {
- is_spoolss_pipe = True;
- }
-
- if (is_spoolss_pipe && current_spoolss_pipes_open >= MAX_OPEN_SPOOLSS_PIPES) {
- DEBUG(10,("open_rpc_pipe_p: spooler bug workaround. Denying open on pipe %s\n",
- pipe_name ));
- return NULL;
- }
-
- /* 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 */
-
- if (next_pipe == 0) {
- next_pipe = (sys_getpid() ^ time(NULL)) % MAX_OPEN_PIPES;
- }
-
- i = bitmap_find(bmap, next_pipe);
-
- if (i == -1) {
- DEBUG(0,("ERROR! Out of pipe structures\n"));
- return NULL;
- }
-
- next_pipe = (i+1) % MAX_OPEN_PIPES;
-
- for (p = Pipes; p; p = p->next) {
- DEBUG(5,("open_rpc_pipe_p: name %s pnum=%x\n", p->name, p->pnum));
- }
-
- p = SMB_MALLOC_P(smb_np_struct);
- if (!p) {
- DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
- return NULL;
- }
-
- ZERO_STRUCTP(p);
-
- /* 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);
-
- if (p->np_state == NULL) {
- DEBUG(0,("open_rpc_pipe_p: make_internal_rpc_pipe_p failed.\n"));
- SAFE_FREE(p);
- return NULL;
- }
-
- DLIST_ADD(Pipes, p);
-
- /*
- * Initialize the incoming RPC data buffer with one PDU worth of memory.
- * We cheat here and say we're marshalling, as we intend to add incoming
- * data directly into the prs_struct and we want it to auto grow. We will
- * change the type to UNMARSALLING before processing the stream.
- */
-
- bitmap_set(bmap, i);
- i += pipe_handle_offset;
-
- pipes_open++;
-
- p->pnum = i;
-
- p->open = True;
- p->device_state = 0;
- p->priority = 0;
- p->conn = conn;
- 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));
-
- chain_p = p;
-
- /* Iterate over p_it as a temp variable, to display all open pipes */
- for (p_it = Pipes; p_it; p_it = p_it->next) {
- DEBUG(5,("open pipes: name %s pnum=%x\n", p_it->name, p_it->pnum));
- }
-
- return chain_p;
-}
-
/****************************************************************************
Make an internal namedpipes structure
****************************************************************************/
-static void *make_internal_rpc_pipe_p(char *pipe_name,
- connection_struct *conn, uint16 vuid)
+struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
+ 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(mem_ctx, struct 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;
}
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;
}
/****************************************************************************
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));
}
/****************************************************************************
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],
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 -
}
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.
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",
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
return (ssize_t)data_to_copy;
}
-/****************************************************************************
- Accepts incoming data on an rpc pipe.
-****************************************************************************/
-
-ssize_t write_to_pipe(smb_np_struct *p, char *data, size_t n)
-{
- DEBUG(6,("write_to_pipe: %x", p->pnum));
-
- DEBUG(6,(" name: %s open: %s len: %d\n",
- p->name, BOOLSTR(p->open), (int)n));
-
- dump_data(50, (uint8 *)data, n);
-
- return p->namedpipe_write(p->np_state, data, 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) {
have been prepared into arrays of headers + data stream sections.
****************************************************************************/
-ssize_t read_from_pipe(smb_np_struct *p, char *data, size_t n,
- BOOL *is_data_outstanding)
-{
- if (!p || !p->open) {
- DEBUG(0,("read_from_pipe: pipe not open\n"));
- return -1;
- }
-
- DEBUG(6,("read_from_pipe: %x", p->pnum));
-
- return p->namedpipe_read(p->np_state, data, n, is_data_outstanding);
-}
-
-/****************************************************************************
- Replies to a request to read data from a pipe.
-
- Headers are interspersed with the data at PDU intervals. By the time
- this function is called, the start of the data could possibly have been
- read by an SMBtrans (file_offset != 0).
-
- Calling create_rpc_reply() here is a hack. The data should already
- 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;
if(n > RPC_MAX_PDU_FRAG_LEN) {
DEBUG(5,("read_from_pipe: too large read (%u) requested on \
pipe %s. We can only service %d sized reads.\n", (unsigned int)n, p->name, RPC_MAX_PDU_FRAG_LEN ));
+ n = RPC_MAX_PDU_FRAG_LEN;
}
/*
return data_returned;
}
-/****************************************************************************
- 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)
-{
- if (p == NULL) {
- return False;
- }
-
- if (p->open) {
- DEBUG(3,("wait_rpc_pipe_hnd_state: Setting pipe wait state priority=%x on pipe (name=%s)\n",
- priority, p->name));
-
- p->priority = priority;
-
- return True;
- }
-
- DEBUG(3,("wait_rpc_pipe_hnd_state: Error setting pipe wait state priority=%x (name=%s)\n",
- priority, p->name));
- return False;
-}
-
-
-/****************************************************************************
- 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)
-{
- if (p == NULL) {
- return False;
- }
-
- if (p->open) {
- DEBUG(3,("set_rpc_pipe_hnd_state: Setting pipe device state=%x on pipe (name=%s)\n",
- device_state, p->name));
-
- p->device_state = device_state;
-
- return True;
- }
-
- DEBUG(3,("set_rpc_pipe_hnd_state: Error setting pipe device state=%x (name=%s)\n",
- device_state, p->name));
- return False;
-}
-
-
-/****************************************************************************
- Close an rpc pipe.
-****************************************************************************/
-
-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);
-
- bitmap_clear(bmap, p->pnum - pipe_handle_offset);
-
- pipes_open--;
-
- DEBUG(4,("closed pipe name %s pnum=%x (pipes_open=%d)\n",
- p->name, p->pnum, pipes_open));
-
- DLIST_REMOVE(Pipes, p);
-
- /* TODO: Remove from pipe open db */
-
- if ( !delete_pipe_opendb( p ) ) {
- DEBUG(3,("close_rpc_pipe_hnd: failed to delete %s "
- "pipe from open db.\n", p->name));
- }
-
- ZERO_STRUCTP(p);
-
- SAFE_FREE(p);
-
- return True;
-}
-
-/****************************************************************************
- Close all pipes on a connection.
-****************************************************************************/
-
-void pipe_close_conn(connection_struct *conn)
-{
- smb_np_struct *p, *next;
-
- for (p=Pipes;p;p=next) {
- next = p->next;
- if (p->conn == conn) {
- close_rpc_pipe_hnd(p);
- }
- }
-}
-
/****************************************************************************
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;
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;
}
-/****************************************************************************
- Find an rpc pipe given a pipe handle in a buffer and an offset.
-****************************************************************************/
+bool fsp_is_np(struct files_struct *fsp)
+{
+ return ((fsp != NULL)
+ && (fsp->fake_file_handle != NULL)
+ && (fsp->fake_file_handle->type == FAKE_FILE_TYPE_NAMED_PIPE));
+}
-smb_np_struct *get_rpc_pipe_p(const char *buf, int where)
+NTSTATUS np_open(struct smb_request *smb_req, struct connection_struct *conn,
+ const char *name, struct files_struct **pfsp)
{
- int pnum = SVAL(buf,where);
+ NTSTATUS status;
+ struct files_struct *fsp;
+ struct pipes_struct *p;
+
+ status = file_new(smb_req, conn, &fsp);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0, ("file_new failed: %s\n", nt_errstr(status)));
+ return status;
+ }
- if (chain_p) {
- return chain_p;
+ fsp->conn = conn;
+ fsp->fh->fd = -1;
+ fsp->vuid = smb_req->vuid;
+ fsp->can_lock = false;
+ fsp->access_mask = FILE_READ_DATA | FILE_WRITE_DATA;
+ string_set(&fsp->fsp_name, name);
+
+ fsp->fake_file_handle = talloc(NULL, struct fake_file_handle);
+ if (fsp->fake_file_handle == NULL) {
+ file_free(smb_req, fsp);
+ return NT_STATUS_NO_MEMORY;
}
+ fsp->fake_file_handle->type = FAKE_FILE_TYPE_NAMED_PIPE;
- return get_rpc_pipe(pnum);
-}
+ p = make_internal_rpc_pipe_p(fsp->fake_file_handle, name,
+ conn->client_address, conn->server_info,
+ smb_req->vuid);
+ if (p == NULL) {
+ file_free(smb_req, fsp);
+ return NT_STATUS_PIPE_NOT_AVAILABLE;
+ }
+ fsp->fake_file_handle->private_data = p;
-/****************************************************************************
- Find an rpc pipe given a pipe handle.
-****************************************************************************/
+ *pfsp = fsp;
-smb_np_struct *get_rpc_pipe(int pnum)
-{
- smb_np_struct *p;
+ return NT_STATUS_OK;
+}
- DEBUG(4,("search for pipe pnum=%x\n", pnum));
+NTSTATUS np_write(struct files_struct *fsp, uint8_t *data, size_t len,
+ ssize_t *nwritten)
+{
+ struct pipes_struct *p;
- for (p=Pipes;p;p=p->next) {
- DEBUG(5,("pipe name %s pnum=%x (pipes_open=%d)\n",
- p->name, p->pnum, pipes_open));
+ if (!fsp_is_np(fsp)) {
+ return NT_STATUS_INVALID_HANDLE;
}
- for (p=Pipes;p;p=p->next) {
- if (p->pnum == pnum) {
- chain_p = p;
- return p;
- }
+ p = talloc_get_type_abort(
+ fsp->fake_file_handle->private_data, struct pipes_struct);
+
+ DEBUG(6, ("np_write: %x name: %s len: %d\n", (int)fsp->fnum,
+ fsp->fsp_name, (int)len));
+ dump_data(50, data, len);
+
+ *nwritten = write_to_internal_pipe(p, (char *)data, len);
+
+ return ((*nwritten) >= 0)
+ ? NT_STATUS_OK : NT_STATUS_UNEXPECTED_IO_ERROR;
+}
+
+NTSTATUS np_read(struct files_struct *fsp, uint8_t *data, size_t len,
+ ssize_t *nread, bool *is_data_outstanding)
+{
+ struct pipes_struct *p;
+
+ if (!fsp_is_np(fsp)) {
+ return NT_STATUS_INVALID_HANDLE;
}
- return NULL;
+ p = talloc_get_type_abort(
+ fsp->fake_file_handle->private_data, struct pipes_struct);
+
+ *nread = read_from_internal_pipe(p, (char *)data, len,
+ is_data_outstanding);
+
+ return ((*nread) >= 0)
+ ? NT_STATUS_OK : NT_STATUS_UNEXPECTED_IO_ERROR;
+
}