r24106: Pass fnum instead of buf/offset into get_rpc_pipe_p
[garming/samba-autobuild/.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22 /*
23    This file handles reply_ calls on named pipes that the server
24    makes to handle specific protocols
25 */
26
27
28 #include "includes.h"
29
30 #define PIPE            "\\PIPE\\"
31 #define PIPELEN         strlen(PIPE)
32
33 #define MAX_PIPE_NAME_LEN       24
34
35 /* PIPE/<name>/<pid>/<pnum> */
36 #define PIPEDB_KEY_FORMAT "PIPE/%s/%u/%d"
37
38 struct pipe_dbrec {
39         struct server_id pid;
40         int pnum;
41         uid_t uid;
42
43         char name[MAX_PIPE_NAME_LEN];
44         fstring user;
45 };
46
47
48 extern struct pipe_id_info pipe_names[];
49
50 /****************************************************************************
51  Reply to an open and X on a named pipe.
52  This code is basically stolen from reply_open_and_X with some
53  wrinkles to handle pipes.
54 ****************************************************************************/
55
56 void reply_open_pipe_and_X(connection_struct *conn, struct smb_request *req)
57 {
58         pstring fname;
59         pstring pipe_name;
60         smb_np_struct *p;
61         int size=0,fmode=0,mtime=0,rmode=0;
62         int i;
63
64         /* XXXX we need to handle passed times, sattr and flags */
65         srvstr_pull_buf(req->inbuf, req->flags2, pipe_name,
66                         smb_buf(req->inbuf), sizeof(pipe_name), STR_TERMINATE);
67
68         /* If the name doesn't start \PIPE\ then this is directed */
69         /* at a mailslot or something we really, really don't understand, */
70         /* not just something we really don't understand. */
71         if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
72                 reply_doserror(req, ERRSRV, ERRaccess);
73                 return;
74         }
75
76         DEBUG(4,("Opening pipe %s.\n", pipe_name));
77
78         /* See if it is one we want to handle. */
79         for( i = 0; pipe_names[i].client_pipe ; i++ ) {
80                 if( strequal(pipe_name,pipe_names[i].client_pipe)) {
81                         break;
82                 }
83         }
84
85         if (pipe_names[i].client_pipe == NULL) {
86                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
87                                 ERRDOS, ERRbadpipe);
88                 return;
89         }
90
91         /* Strip \PIPE\ off the name. */
92         pstrcpy(fname, pipe_name + PIPELEN);
93
94 #if 0
95         /*
96          * Hack for NT printers... JRA.
97          */
98         if(should_fail_next_srvsvc_open(fname)) {
99                 reply_doserror(req, ERRSRV, ERRaccess);
100                 return;
101         }
102 #endif
103
104         /* Known pipes arrive with DIR attribs. Remove it so a regular file */
105         /* can be opened and add it in after the open. */
106         DEBUG(3,("Known pipe %s opening.\n",fname));
107
108         p = open_rpc_pipe_p(fname, conn, req->vuid);
109         if (!p) {
110                 reply_doserror(req, ERRSRV, ERRnofids);
111                 return;
112         }
113
114         /* Prepare the reply */
115         reply_outbuf(req, 15, 0);
116
117         /* Mark the opened file as an existing named pipe in message mode. */
118         SSVAL(req->outbuf,smb_vwv9,2);
119         SSVAL(req->outbuf,smb_vwv10,0xc700);
120
121         if (rmode == 2) {
122                 DEBUG(4,("Resetting open result to open from create.\n"));
123                 rmode = 1;
124         }
125
126         SSVAL(req->outbuf,smb_vwv2, p->pnum);
127         SSVAL(req->outbuf,smb_vwv3,fmode);
128         srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
129         SIVAL(req->outbuf,smb_vwv6,size);
130         SSVAL(req->outbuf,smb_vwv8,rmode);
131         SSVAL(req->outbuf,smb_vwv11,0x0001);
132
133         chain_reply_new(req);
134         return;
135 }
136
137 /****************************************************************************
138  Reply to a write on a pipe.
139 ****************************************************************************/
140
141 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
142 {
143         smb_np_struct *p = get_rpc_pipe_p(SVAL(inbuf,smb_vwv0));
144         uint16 vuid = SVAL(inbuf,smb_uid);
145         size_t numtowrite = SVAL(inbuf,smb_vwv1);
146         int nwritten;
147         int outsize;
148         char *data;
149
150         if (!p) {
151                 return(ERROR_DOS(ERRDOS,ERRbadfid));
152         }
153
154         if (p->vuid != vuid) {
155                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
156         }
157
158         data = smb_buf(inbuf) + 3;
159
160         if (numtowrite == 0) {
161                 nwritten = 0;
162         } else {
163                 nwritten = write_to_pipe(p, data, numtowrite);
164         }
165
166         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
167                 return (UNIXERROR(ERRDOS,ERRnoaccess));
168         }
169   
170         outsize = set_message(inbuf,outbuf,1,0,True);
171
172         SSVAL(outbuf,smb_vwv0,nwritten);
173   
174         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
175
176         return(outsize);
177 }
178
179 /****************************************************************************
180  Reply to a write and X.
181
182  This code is basically stolen from reply_write_and_X with some
183  wrinkles to handle pipes.
184 ****************************************************************************/
185
186 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
187 {
188         smb_np_struct *p = get_rpc_pipe_p(SVAL(inbuf,smb_vwv2));
189         uint16 vuid = SVAL(inbuf,smb_uid);
190         size_t numtowrite = SVAL(inbuf,smb_vwv10);
191         int nwritten = -1;
192         int smb_doff = SVAL(inbuf, smb_vwv11);
193         BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
194                                                                 (PIPE_START_MESSAGE|PIPE_RAW_MODE));
195         char *data;
196
197         if (!p) {
198                 return(ERROR_DOS(ERRDOS,ERRbadfid));
199         }
200
201         if (p->vuid != vuid) {
202                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
203         }
204
205         data = smb_base(inbuf) + smb_doff;
206
207         if (numtowrite == 0) {
208                 nwritten = 0;
209         } else {
210                 if(pipe_start_message_raw) {
211                         /*
212                          * For the start of a message in named pipe byte mode,
213                          * the first two bytes are a length-of-pdu field. Ignore
214                          * them (we don't trust the client). JRA.
215                          */
216                        if(numtowrite < 2) {
217                                 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
218                                         (unsigned int)numtowrite ));
219                                 return (UNIXERROR(ERRDOS,ERRnoaccess));
220                         }
221
222                         data += 2;
223                         numtowrite -= 2;
224                 }                        
225                 nwritten = write_to_pipe(p, data, numtowrite);
226         }
227
228         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
229                 return (UNIXERROR(ERRDOS,ERRnoaccess));
230         }
231   
232         set_message(inbuf,outbuf,6,0,True);
233
234         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
235         SSVAL(outbuf,smb_vwv2,nwritten);
236   
237         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
238
239         return chain_reply(inbuf,&outbuf,length,bufsize);
240 }
241
242 /****************************************************************************
243  Reply to a read and X.
244  This code is basically stolen from reply_read_and_X with some
245  wrinkles to handle pipes.
246 ****************************************************************************/
247
248 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
249 {
250         smb_np_struct *p = get_rpc_pipe_p(SVAL(inbuf,smb_vwv2));
251         int smb_maxcnt = SVAL(inbuf,smb_vwv5);
252         int smb_mincnt = SVAL(inbuf,smb_vwv6);
253         int nread = -1;
254         char *data;
255         BOOL unused;
256
257         /* we don't use the offset given to use for pipe reads. This
258            is deliberate, instead we always return the next lump of
259            data on the pipe */
260 #if 0
261         uint32 smb_offs = IVAL(inbuf,smb_vwv3);
262 #endif
263
264         if (!p) {
265                 return(ERROR_DOS(ERRDOS,ERRbadfid));
266         }
267
268         set_message(inbuf,outbuf,12,0,True);
269         data = smb_buf(outbuf);
270
271         nread = read_from_pipe(p, data, smb_maxcnt, &unused);
272
273         if (nread < 0) {
274                 return(UNIXERROR(ERRDOS,ERRnoaccess));
275         }
276   
277         SSVAL(outbuf,smb_vwv5,nread);
278         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
279         SSVAL(smb_buf(outbuf),-2,nread);
280   
281         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
282                  p->pnum, smb_mincnt, smb_maxcnt, nread));
283
284         /* Ensure we set up the message length to include the data length read. */
285         set_message_bcc(inbuf,outbuf,nread);
286         return chain_reply(inbuf,&outbuf,length,bufsize);
287 }
288
289 /****************************************************************************
290  Reply to a close.
291 ****************************************************************************/
292
293 void reply_pipe_close(connection_struct *conn, struct smb_request *req)
294 {
295         smb_np_struct *p = get_rpc_pipe_p(SVAL(req->inbuf,smb_vwv0));
296
297         if (!p) {
298                 reply_doserror(req, ERRDOS, ERRbadfid);
299                 return;
300         }
301
302         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
303
304         if (!close_rpc_pipe_hnd(p)) {
305                 reply_doserror(req, ERRDOS, ERRbadfid);
306                 return;
307         }
308         
309         /* TODO: REMOVE PIPE FROM DB */
310
311         reply_outbuf(req, 0, 0);
312         return;
313 }