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 3 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
31 #define PIPE "\\PIPE\\"
32 #define PIPELEN strlen(PIPE)
34 #define MAX_PIPE_NAME_LEN 24
36 /* PIPE/<name>/<pid>/<pnum> */
37 #define PIPEDB_KEY_FORMAT "PIPE/%s/%u/%d"
44 char name[MAX_PIPE_NAME_LEN];
49 extern struct pipe_id_info pipe_names[];
51 /****************************************************************************
52 Reply to an open and X on a named pipe.
53 This code is basically stolen from reply_open_and_X with some
54 wrinkles to handle pipes.
55 ****************************************************************************/
57 int reply_open_pipe_and_X(connection_struct *conn,
58 char *inbuf,char *outbuf,int length,int bufsize)
62 uint16 vuid = SVAL(inbuf, smb_uid);
64 int size=0,fmode=0,mtime=0,rmode=0;
67 /* XXXX we need to handle passed times, sattr and flags */
68 srvstr_pull_buf(inbuf, SVAL(inbuf, smb_flg2), pipe_name,
69 smb_buf(inbuf), sizeof(pipe_name), STR_TERMINATE);
71 /* If the name doesn't start \PIPE\ then this is directed */
72 /* at a mailslot or something we really, really don't understand, */
73 /* not just something we really don't understand. */
74 if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
75 return(ERROR_DOS(ERRSRV,ERRaccess));
78 DEBUG(4,("Opening pipe %s.\n", pipe_name));
80 /* See if it is one we want to handle. */
81 for( i = 0; pipe_names[i].client_pipe ; i++ ) {
82 if( strequal(pipe_name,pipe_names[i].client_pipe)) {
87 if (pipe_names[i].client_pipe == NULL) {
88 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
91 /* Strip \PIPE\ off the name. */
92 pstrcpy(fname, pipe_name + PIPELEN);
96 * Hack for NT printers... JRA.
98 if(should_fail_next_srvsvc_open(fname))
99 return(ERROR(ERRSRV,ERRaccess));
102 /* Known pipes arrive with DIR attribs. Remove it so a regular file */
103 /* can be opened and add it in after the open. */
104 DEBUG(3,("Known pipe %s opening.\n",fname));
106 p = open_rpc_pipe_p(fname, conn, vuid);
108 return(ERROR_DOS(ERRSRV,ERRnofids));
111 /* Prepare the reply */
112 set_message(inbuf,outbuf,15,0,True);
114 /* Mark the opened file as an existing named pipe in message mode. */
115 SSVAL(outbuf,smb_vwv9,2);
116 SSVAL(outbuf,smb_vwv10,0xc700);
119 DEBUG(4,("Resetting open result to open from create.\n"));
123 SSVAL(outbuf,smb_vwv2, p->pnum);
124 SSVAL(outbuf,smb_vwv3,fmode);
125 srv_put_dos_date3(outbuf,smb_vwv4,mtime);
126 SIVAL(outbuf,smb_vwv6,size);
127 SSVAL(outbuf,smb_vwv8,rmode);
128 SSVAL(outbuf,smb_vwv11,0x0001);
130 return chain_reply(inbuf,outbuf,length,bufsize);
133 /****************************************************************************
134 Reply to a write on a pipe.
135 ****************************************************************************/
137 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
139 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
140 uint16 vuid = SVAL(inbuf,smb_uid);
141 size_t numtowrite = SVAL(inbuf,smb_vwv1);
147 return(ERROR_DOS(ERRDOS,ERRbadfid));
150 if (p->vuid != vuid) {
151 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
154 data = smb_buf(inbuf) + 3;
156 if (numtowrite == 0) {
159 nwritten = write_to_pipe(p, data, numtowrite);
162 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
163 return (UNIXERROR(ERRDOS,ERRnoaccess));
166 outsize = set_message(inbuf,outbuf,1,0,True);
168 SSVAL(outbuf,smb_vwv0,nwritten);
170 DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
175 /****************************************************************************
176 Reply to a write and X.
178 This code is basically stolen from reply_write_and_X with some
179 wrinkles to handle pipes.
180 ****************************************************************************/
182 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
184 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
185 uint16 vuid = SVAL(inbuf,smb_uid);
186 size_t numtowrite = SVAL(inbuf,smb_vwv10);
188 int smb_doff = SVAL(inbuf, smb_vwv11);
189 BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
190 (PIPE_START_MESSAGE|PIPE_RAW_MODE));
194 return(ERROR_DOS(ERRDOS,ERRbadfid));
197 if (p->vuid != vuid) {
198 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
201 data = smb_base(inbuf) + smb_doff;
203 if (numtowrite == 0) {
206 if(pipe_start_message_raw) {
208 * For the start of a message in named pipe byte mode,
209 * the first two bytes are a length-of-pdu field. Ignore
210 * them (we don't trust the client). JRA.
213 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
214 (unsigned int)numtowrite ));
215 return (UNIXERROR(ERRDOS,ERRnoaccess));
221 nwritten = write_to_pipe(p, data, numtowrite);
224 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
225 return (UNIXERROR(ERRDOS,ERRnoaccess));
228 set_message(inbuf,outbuf,6,0,True);
230 nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
231 SSVAL(outbuf,smb_vwv2,nwritten);
233 DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
235 return chain_reply(inbuf,outbuf,length,bufsize);
238 /****************************************************************************
239 Reply to a read and X.
240 This code is basically stolen from reply_read_and_X with some
241 wrinkles to handle pipes.
242 ****************************************************************************/
244 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
246 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
247 int smb_maxcnt = SVAL(inbuf,smb_vwv5);
248 int smb_mincnt = SVAL(inbuf,smb_vwv6);
253 /* we don't use the offset given to use for pipe reads. This
254 is deliberate, instead we always return the next lump of
257 uint32 smb_offs = IVAL(inbuf,smb_vwv3);
261 return(ERROR_DOS(ERRDOS,ERRbadfid));
264 set_message(inbuf,outbuf,12,0,True);
265 data = smb_buf(outbuf);
267 nread = read_from_pipe(p, data, smb_maxcnt, &unused);
270 return(UNIXERROR(ERRDOS,ERRnoaccess));
273 SSVAL(outbuf,smb_vwv5,nread);
274 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
275 SSVAL(smb_buf(outbuf),-2,nread);
277 DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
278 p->pnum, smb_mincnt, smb_maxcnt, nread));
280 /* Ensure we set up the message length to include the data length read. */
281 set_message_bcc(inbuf,outbuf,nread);
282 return chain_reply(inbuf,outbuf,length,bufsize);
285 /****************************************************************************
287 ****************************************************************************/
289 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
291 smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
292 int outsize = set_message(inbuf,outbuf,0,0,True);
295 return(ERROR_DOS(ERRDOS,ERRbadfid));
298 DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
300 if (!close_rpc_pipe_hnd(p)) {
301 return ERROR_DOS(ERRDOS,ERRbadfid);
304 /* TODO: REMOVE PIPE FROM DB */