Initial import
[samba] / source / smbd / pipes.c
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.
8    
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.
13    
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.
18    
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.
22 */
23 /*
24    This file handles reply_ calls on named pipes that the server
25    makes to handle specific protocols
26 */
27
28
29 #include "includes.h"
30
31 #define PIPE            "\\PIPE\\"
32 #define PIPELEN         strlen(PIPE)
33
34 extern struct pipe_id_info pipe_names[];
35
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 ****************************************************************************/
41
42 int reply_open_pipe_and_X(connection_struct *conn,
43                           char *inbuf,char *outbuf,int length,int bufsize)
44 {
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;
51
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);
54
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));
60         }
61
62         DEBUG(4,("Opening pipe %s.\n", pipe_name));
63
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;
68                 }
69         }
70
71         if (pipe_names[i].client_pipe == NULL) {
72                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
73         }
74
75         /* Strip \PIPE\ off the name. */
76         pstrcpy(fname, pipe_name + PIPELEN);
77
78 #if 0
79         /*
80          * Hack for NT printers... JRA.
81          */
82     if(should_fail_next_srvsvc_open(fname))
83       return(ERROR(ERRSRV,ERRaccess));
84 #endif
85
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));
89
90         p = open_rpc_pipe_p(fname, conn, vuid);
91         if (!p) {
92                 return(ERROR_DOS(ERRSRV,ERRnofids));
93         }
94
95         /* Prepare the reply */
96         set_message(outbuf,15,0,True);
97
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);
101
102         if (rmode == 2) {
103                 DEBUG(4,("Resetting open result to open from create.\n"));
104                 rmode = 1;
105         }
106
107         SSVAL(outbuf,smb_vwv2, p->pnum);
108         SSVAL(outbuf,smb_vwv3,fmode);
109         srv_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);
113
114         return chain_reply(inbuf,outbuf,length,bufsize);
115 }
116
117 /****************************************************************************
118  Reply to a write on a pipe.
119 ****************************************************************************/
120
121 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
122 {
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;
128
129         if (!p) {
130                 return(ERROR_DOS(ERRDOS,ERRbadfid));
131         }
132
133         data = smb_buf(inbuf) + 3;
134
135         if (numtowrite == 0) {
136                 nwritten = 0;
137         } else {
138                 nwritten = write_to_pipe(p, data, numtowrite);
139         }
140
141         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
142                 return (UNIXERROR(ERRDOS,ERRnoaccess));
143         }
144   
145         outsize = set_message(outbuf,1,0,True);
146
147         SSVAL(outbuf,smb_vwv0,nwritten);
148   
149         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
150
151         return(outsize);
152 }
153
154 /****************************************************************************
155  Reply to a write and X.
156
157  This code is basically stolen from reply_write_and_X with some
158  wrinkles to handle pipes.
159 ****************************************************************************/
160
161 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
162 {
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;
170
171         if (!p) {
172                 return(ERROR_DOS(ERRDOS,ERRbadfid));
173         }
174
175         data = smb_base(inbuf) + smb_doff;
176
177         if (numtowrite == 0) {
178                 nwritten = 0;
179         } else {
180                 if(pipe_start_message_raw) {
181                         /*
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.
185                          */
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));
190                         }
191
192                         data += 2;
193                         numtowrite -= 2;
194                 }                        
195                 nwritten = write_to_pipe(p, data, numtowrite);
196         }
197
198         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
199                 return (UNIXERROR(ERRDOS,ERRnoaccess));
200         }
201   
202         set_message(outbuf,6,0,True);
203
204         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
205         SSVAL(outbuf,smb_vwv2,nwritten);
206   
207         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
208
209         return chain_reply(inbuf,outbuf,length,bufsize);
210 }
211
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 ****************************************************************************/
217
218 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
219 {
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;
226
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
233
234         if (!p) {
235                 return(ERROR_DOS(ERRDOS,ERRbadfid));
236         }
237
238         set_message(outbuf,12,0,True);
239         data = smb_buf(outbuf);
240
241         nread = read_from_pipe(p, data, smb_maxcnt, &unused);
242
243         if (nread < 0) {
244                 return(UNIXERROR(ERRDOS,ERRnoaccess));
245         }
246   
247         SSVAL(outbuf,smb_vwv5,nread);
248         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
249         SSVAL(smb_buf(outbuf),-2,nread);
250   
251         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
252                  p->pnum, smb_mincnt, smb_maxcnt, nread));
253
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);
257 }
258
259 /****************************************************************************
260  Reply to a close.
261 ****************************************************************************/
262
263 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
264 {
265         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
266         int outsize = set_message(outbuf,0,0,True);
267
268         if (!p) {
269                 return(ERROR_DOS(ERRDOS,ERRbadfid));
270         }
271
272         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
273
274         if (!close_rpc_pipe_hnd(p)) {
275                 return ERROR_DOS(ERRDOS,ERRbadfid);
276         }
277
278         return(outsize);
279 }