r23779: Change from v2 or later to v3 or later.
[kai/samba.git] / 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 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, 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 #define MAX_PIPE_NAME_LEN       24
35
36 /* PIPE/<name>/<pid>/<pnum> */
37 #define PIPEDB_KEY_FORMAT "PIPE/%s/%u/%d"
38
39 struct pipe_dbrec {
40         struct server_id pid;
41         int pnum;
42         uid_t uid;
43
44         char name[MAX_PIPE_NAME_LEN];
45         fstring user;
46 };
47
48
49 extern struct pipe_id_info pipe_names[];
50
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 ****************************************************************************/
56
57 int reply_open_pipe_and_X(connection_struct *conn,
58                           char *inbuf,char *outbuf,int length,int bufsize)
59 {
60         pstring fname;
61         pstring pipe_name;
62         uint16 vuid = SVAL(inbuf, smb_uid);
63         smb_np_struct *p;
64         int size=0,fmode=0,mtime=0,rmode=0;
65         int i;
66
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);
70
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));
76         }
77
78         DEBUG(4,("Opening pipe %s.\n", pipe_name));
79
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)) {
83                         break;
84                 }
85         }
86
87         if (pipe_names[i].client_pipe == NULL) {
88                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
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       return(ERROR(ERRSRV,ERRaccess));
100 #endif
101
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));
105
106         p = open_rpc_pipe_p(fname, conn, vuid);
107         if (!p) {
108                 return(ERROR_DOS(ERRSRV,ERRnofids));
109         }
110
111         /* Prepare the reply */
112         set_message(inbuf,outbuf,15,0,True);
113
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);
117
118         if (rmode == 2) {
119                 DEBUG(4,("Resetting open result to open from create.\n"));
120                 rmode = 1;
121         }
122
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);
129
130         return chain_reply(inbuf,outbuf,length,bufsize);
131 }
132
133 /****************************************************************************
134  Reply to a write on a pipe.
135 ****************************************************************************/
136
137 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
138 {
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);
142         int nwritten;
143         int outsize;
144         char *data;
145
146         if (!p) {
147                 return(ERROR_DOS(ERRDOS,ERRbadfid));
148         }
149
150         if (p->vuid != vuid) {
151                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
152         }
153
154         data = smb_buf(inbuf) + 3;
155
156         if (numtowrite == 0) {
157                 nwritten = 0;
158         } else {
159                 nwritten = write_to_pipe(p, data, numtowrite);
160         }
161
162         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
163                 return (UNIXERROR(ERRDOS,ERRnoaccess));
164         }
165   
166         outsize = set_message(inbuf,outbuf,1,0,True);
167
168         SSVAL(outbuf,smb_vwv0,nwritten);
169   
170         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
171
172         return(outsize);
173 }
174
175 /****************************************************************************
176  Reply to a write and X.
177
178  This code is basically stolen from reply_write_and_X with some
179  wrinkles to handle pipes.
180 ****************************************************************************/
181
182 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
183 {
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);
187         int nwritten = -1;
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));
191         char *data;
192
193         if (!p) {
194                 return(ERROR_DOS(ERRDOS,ERRbadfid));
195         }
196
197         if (p->vuid != vuid) {
198                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
199         }
200
201         data = smb_base(inbuf) + smb_doff;
202
203         if (numtowrite == 0) {
204                 nwritten = 0;
205         } else {
206                 if(pipe_start_message_raw) {
207                         /*
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.
211                          */
212                        if(numtowrite < 2) {
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));
216                         }
217
218                         data += 2;
219                         numtowrite -= 2;
220                 }                        
221                 nwritten = write_to_pipe(p, data, numtowrite);
222         }
223
224         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
225                 return (UNIXERROR(ERRDOS,ERRnoaccess));
226         }
227   
228         set_message(inbuf,outbuf,6,0,True);
229
230         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
231         SSVAL(outbuf,smb_vwv2,nwritten);
232   
233         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
234
235         return chain_reply(inbuf,outbuf,length,bufsize);
236 }
237
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 ****************************************************************************/
243
244 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
245 {
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);
249         int nread = -1;
250         char *data;
251         BOOL unused;
252
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
255            data on the pipe */
256 #if 0
257         uint32 smb_offs = IVAL(inbuf,smb_vwv3);
258 #endif
259
260         if (!p) {
261                 return(ERROR_DOS(ERRDOS,ERRbadfid));
262         }
263
264         set_message(inbuf,outbuf,12,0,True);
265         data = smb_buf(outbuf);
266
267         nread = read_from_pipe(p, data, smb_maxcnt, &unused);
268
269         if (nread < 0) {
270                 return(UNIXERROR(ERRDOS,ERRnoaccess));
271         }
272   
273         SSVAL(outbuf,smb_vwv5,nread);
274         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
275         SSVAL(smb_buf(outbuf),-2,nread);
276   
277         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
278                  p->pnum, smb_mincnt, smb_maxcnt, nread));
279
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);
283 }
284
285 /****************************************************************************
286  Reply to a close.
287 ****************************************************************************/
288
289 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
290 {
291         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
292         int outsize = set_message(inbuf,outbuf,0,0,True);
293
294         if (!p) {
295                 return(ERROR_DOS(ERRDOS,ERRbadfid));
296         }
297
298         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
299
300         if (!close_rpc_pipe_hnd(p)) {
301                 return ERROR_DOS(ERRDOS,ERRbadfid);
302         }
303         
304         /* TODO: REMOVE PIPE FROM DB */
305
306         return(outsize);
307 }