summaryrefslogtreecommitdiffstats
path: root/source/rpc_server/srv_pipe.c
blob: 58367660f25a866278290650cadf166ed16c0a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

/* 
 *  Unix SMB/Netbios implementation.
 *  Version 1.9.
 *  RPC Pipe client / server routines
 *  Copyright (C) Andrew Tridgell              1992-1998
 *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
 *  Copyright (C) Paul Ashton                  1997-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
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "includes.h"
#include "nterr.h"

extern int DEBUGLEVEL;

/*******************************************************************
 entry point from msrpc to smb.  adds data received to pdu; checks
 pdu; hands pdu off to msrpc, which gets a pdu back (except in the
 case of the RPC_BINDCONT pdu).
 ********************************************************************/
BOOL readwrite_pipe(pipes_struct *p, char *data, int len,
		char **rdata, int *rlen,
		BOOL *pipe_outstanding)
{
	fd_set fds;
	int selrtn;
	struct timeval timeout;

	DEBUG(10,("rpc_to_smb_readwrite: len %d\n", len));

	if (write(p->m->fd, data, len) != len)
	{
		return False;
	}

	if ((*rlen) == 0)
	{
		return False;
	}

	(*rdata) = (char*)Realloc((*rdata), (*rlen));
	if ((*rdata) == NULL)
	{
		return False;
	}

	/* compromise.  MUST read a minimum of an rpc header.
	 * timeout waiting for the rest for 10 seconds */
	(*rlen) = read_with_timeout(p->m->fd, (*rdata), 16,(*rlen), 10000);
	if ((*rlen) < 0)
	{
		return False;
	}
	(*rdata) = (char*)Realloc((*rdata), (*rlen));
	if ((*rdata) == NULL)
	{
		return False;
	}

	/* now check whether there is outstanding data on the pipe.
	 * this is needed to as to report a STATUS message back to
	 * the NT client.  yes, NT clients fail to operate if you
	 * don't set a STATUS_BUFFER_OVERFLOW warning on the SMBtrans
	 * response.  *dur*!  what's msrpc got to do with smb, ANYWAY!!
	 */

	FD_ZERO(&fds);
	FD_SET(p->m->fd,&fds);

	/* Set initial timeout to zero */
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;

	selrtn = sys_select(p->m->fd+1,&fds,NULL, &timeout);

	/* Check if error */
	if (selrtn == -1)
	{
		/* something is wrong. Maybe the socket is dead? */
		return -1;
	}

	*pipe_outstanding = FD_ISSET(p->m->fd, &fds);

	return True;
}

/****************************************************************************
writes data to a pipe.
****************************************************************************/
ssize_t write_pipe(pipes_struct *p, char *data, size_t n)
{
	DEBUG(6,("write_pipe: %x", p->pnum));
	DEBUG(6,("name: %s len: %d", p->name, n));

	dump_data(50, data, n);

	return write(p->m->fd, data, n);
}


/****************************************************************************
 reads data from a pipe.

 headers are interspersed with the data at regular intervals.  by the time
 this function is called, the start of the data could possibly have been
 read by an SMBtrans (file_offset != 0).

 ****************************************************************************/
int read_pipe(pipes_struct *p, char *data, int n)
{
	DEBUG(6,("read_pipe: %x name: %s len: %d", p->pnum, p->name, n));

	if (!p)
	{
		DEBUG(6,("pipe not open\n"));
		return -1;		
	}

	/* read a minimum of 1 byte! :-) */
	return read_with_timeout(p->m->fd, data, 1, n, 10000);
}