r10400: commit merge patch from jra
[Samba/gbeck.git] / source / smbd / pipes.c
blob951c192e396b905733fb5c28d79ac536521ae0bd
1 /*
2 Unix SMB/CIFS implementation.
3 Pipe SMB reply routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6 Copyright (C) Paul Ashton 1997-1998.
7 Copyright (C) Jeremy Allison 2005.
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.
24 This file handles reply_ calls on named pipes that the server
25 makes to handle specific protocols
29 #include "includes.h"
31 #define PIPE "\\PIPE\\"
32 #define PIPELEN strlen(PIPE)
34 extern struct pipe_id_info pipe_names[];
36 /****************************************************************************
37 Reply to an open and X on a named pipe.
38 This code is basically stolen from reply_open_and_X with some
39 wrinkles to handle pipes.
40 ****************************************************************************/
42 int reply_open_pipe_and_X(connection_struct *conn,
43 char *inbuf,char *outbuf,int length,int bufsize)
45 pstring fname;
46 pstring pipe_name;
47 uint16 vuid = SVAL(inbuf, smb_uid);
48 smb_np_struct *p;
49 int size=0,fmode=0,mtime=0,rmode=0;
50 int i;
52 /* XXXX we need to handle passed times, sattr and flags */
53 srvstr_pull_buf(inbuf, pipe_name, smb_buf(inbuf), sizeof(pipe_name), STR_TERMINATE);
55 /* If the name doesn't start \PIPE\ then this is directed */
56 /* at a mailslot or something we really, really don't understand, */
57 /* not just something we really don't understand. */
58 if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
59 return(ERROR_DOS(ERRSRV,ERRaccess));
62 DEBUG(4,("Opening pipe %s.\n", pipe_name));
64 /* See if it is one we want to handle. */
65 for( i = 0; pipe_names[i].client_pipe ; i++ ) {
66 if( strequal(pipe_name,pipe_names[i].client_pipe)) {
67 break;
71 if (pipe_names[i].client_pipe == NULL) {
72 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
75 /* Strip \PIPE\ off the name. */
76 pstrcpy(fname, pipe_name + PIPELEN);
78 #if 0
80 * Hack for NT printers... JRA.
82 if(should_fail_next_srvsvc_open(fname))
83 return(ERROR(ERRSRV,ERRaccess));
84 #endif
86 /* Known pipes arrive with DIR attribs. Remove it so a regular file */
87 /* can be opened and add it in after the open. */
88 DEBUG(3,("Known pipe %s opening.\n",fname));
90 p = open_rpc_pipe_p(fname, conn, vuid);
91 if (!p) {
92 return(ERROR_DOS(ERRSRV,ERRnofids));
95 /* Prepare the reply */
96 set_message(outbuf,15,0,True);
98 /* Mark the opened file as an existing named pipe in message mode. */
99 SSVAL(outbuf,smb_vwv9,2);
100 SSVAL(outbuf,smb_vwv10,0xc700);
102 if (rmode == 2) {
103 DEBUG(4,("Resetting open result to open from create.\n"));
104 rmode = 1;
107 SSVAL(outbuf,smb_vwv2, p->pnum);
108 SSVAL(outbuf,smb_vwv3,fmode);
109 put_dos_date3(outbuf,smb_vwv4,mtime);
110 SIVAL(outbuf,smb_vwv6,size);
111 SSVAL(outbuf,smb_vwv8,rmode);
112 SSVAL(outbuf,smb_vwv11,0x0001);
114 return chain_reply(inbuf,outbuf,length,bufsize);
117 /****************************************************************************
118 Reply to a write on a pipe.
119 ****************************************************************************/
121 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
123 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
124 size_t numtowrite = SVAL(inbuf,smb_vwv1);
125 int nwritten;
126 int outsize;
127 char *data;
129 if (!p) {
130 return(ERROR_DOS(ERRDOS,ERRbadfid));
133 data = smb_buf(inbuf) + 3;
135 if (numtowrite == 0) {
136 nwritten = 0;
137 } else {
138 nwritten = write_to_pipe(p, data, numtowrite);
141 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
142 return (UNIXERROR(ERRDOS,ERRnoaccess));
145 outsize = set_message(outbuf,1,0,True);
147 SSVAL(outbuf,smb_vwv0,nwritten);
149 DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
151 return(outsize);
154 /****************************************************************************
155 Reply to a write and X.
157 This code is basically stolen from reply_write_and_X with some
158 wrinkles to handle pipes.
159 ****************************************************************************/
161 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
163 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
164 size_t numtowrite = SVAL(inbuf,smb_vwv10);
165 int nwritten = -1;
166 int smb_doff = SVAL(inbuf, smb_vwv11);
167 BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
168 (PIPE_START_MESSAGE|PIPE_RAW_MODE));
169 char *data;
171 if (!p) {
172 return(ERROR_DOS(ERRDOS,ERRbadfid));
175 data = smb_base(inbuf) + smb_doff;
177 if (numtowrite == 0) {
178 nwritten = 0;
179 } else {
180 if(pipe_start_message_raw) {
182 * For the start of a message in named pipe byte mode,
183 * the first two bytes are a length-of-pdu field. Ignore
184 * them (we don't trust the client). JRA.
186 if(numtowrite < 2) {
187 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
188 (unsigned int)numtowrite ));
189 return (UNIXERROR(ERRDOS,ERRnoaccess));
192 data += 2;
193 numtowrite -= 2;
195 nwritten = write_to_pipe(p, data, numtowrite);
198 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
199 return (UNIXERROR(ERRDOS,ERRnoaccess));
202 set_message(outbuf,6,0,True);
204 nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
205 SSVAL(outbuf,smb_vwv2,nwritten);
207 DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
209 return chain_reply(inbuf,outbuf,length,bufsize);
212 /****************************************************************************
213 Reply to a read and X.
214 This code is basically stolen from reply_read_and_X with some
215 wrinkles to handle pipes.
216 ****************************************************************************/
218 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
220 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
221 int smb_maxcnt = SVAL(inbuf,smb_vwv5);
222 int smb_mincnt = SVAL(inbuf,smb_vwv6);
223 int nread = -1;
224 char *data;
225 BOOL unused;
227 /* we don't use the offset given to use for pipe reads. This
228 is deliberate, instead we always return the next lump of
229 data on the pipe */
230 #if 0
231 uint32 smb_offs = IVAL(inbuf,smb_vwv3);
232 #endif
234 if (!p) {
235 return(ERROR_DOS(ERRDOS,ERRbadfid));
238 set_message(outbuf,12,0,True);
239 data = smb_buf(outbuf);
241 nread = read_from_pipe(p, data, smb_maxcnt, &unused);
243 if (nread < 0) {
244 return(UNIXERROR(ERRDOS,ERRnoaccess));
247 SSVAL(outbuf,smb_vwv5,nread);
248 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
249 SSVAL(smb_buf(outbuf),-2,nread);
251 DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
252 p->pnum, smb_mincnt, smb_maxcnt, nread));
254 /* Ensure we set up the message length to include the data length read. */
255 set_message_bcc(outbuf,nread);
256 return chain_reply(inbuf,outbuf,length,bufsize);
259 /****************************************************************************
260 Reply to a close.
261 ****************************************************************************/
263 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
265 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
266 int outsize = set_message(outbuf,0,0,True);
268 if (!p) {
269 return(ERROR_DOS(ERRDOS,ERRbadfid));
272 DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
274 if (!close_rpc_pipe_hnd(p)) {
275 return ERROR_DOS(ERRDOS,ERRbadfid);
278 return(outsize);