r15911: Make us survive rpc-authcontext committed next
[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 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         uint16 vuid = SVAL(inbuf,smb_uid);
125         size_t numtowrite = SVAL(inbuf,smb_vwv1);
126         int nwritten;
127         int outsize;
128         char *data;
129
130         if (!p) {
131                 return(ERROR_DOS(ERRDOS,ERRbadfid));
132         }
133
134         if (p->vuid != vuid) {
135                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
136         }
137
138         data = smb_buf(inbuf) + 3;
139
140         if (numtowrite == 0) {
141                 nwritten = 0;
142         } else {
143                 nwritten = write_to_pipe(p, data, numtowrite);
144         }
145
146         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
147                 return (UNIXERROR(ERRDOS,ERRnoaccess));
148         }
149   
150         outsize = set_message(outbuf,1,0,True);
151
152         SSVAL(outbuf,smb_vwv0,nwritten);
153   
154         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
155
156         return(outsize);
157 }
158
159 /****************************************************************************
160  Reply to a write and X.
161
162  This code is basically stolen from reply_write_and_X with some
163  wrinkles to handle pipes.
164 ****************************************************************************/
165
166 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
167 {
168         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
169         uint16 vuid = SVAL(inbuf,smb_uid);
170         size_t numtowrite = SVAL(inbuf,smb_vwv10);
171         int nwritten = -1;
172         int smb_doff = SVAL(inbuf, smb_vwv11);
173         BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
174                                                                 (PIPE_START_MESSAGE|PIPE_RAW_MODE));
175         char *data;
176
177         if (!p) {
178                 return(ERROR_DOS(ERRDOS,ERRbadfid));
179         }
180
181         if (p->vuid != vuid) {
182                 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
183         }
184
185         data = smb_base(inbuf) + smb_doff;
186
187         if (numtowrite == 0) {
188                 nwritten = 0;
189         } else {
190                 if(pipe_start_message_raw) {
191                         /*
192                          * For the start of a message in named pipe byte mode,
193                          * the first two bytes are a length-of-pdu field. Ignore
194                          * them (we don't trust the client). JRA.
195                          */
196                        if(numtowrite < 2) {
197                                 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
198                                         (unsigned int)numtowrite ));
199                                 return (UNIXERROR(ERRDOS,ERRnoaccess));
200                         }
201
202                         data += 2;
203                         numtowrite -= 2;
204                 }                        
205                 nwritten = write_to_pipe(p, data, numtowrite);
206         }
207
208         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
209                 return (UNIXERROR(ERRDOS,ERRnoaccess));
210         }
211   
212         set_message(outbuf,6,0,True);
213
214         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
215         SSVAL(outbuf,smb_vwv2,nwritten);
216   
217         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", p->pnum, nwritten));
218
219         return chain_reply(inbuf,outbuf,length,bufsize);
220 }
221
222 /****************************************************************************
223  Reply to a read and X.
224  This code is basically stolen from reply_read_and_X with some
225  wrinkles to handle pipes.
226 ****************************************************************************/
227
228 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
229 {
230         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
231         int smb_maxcnt = SVAL(inbuf,smb_vwv5);
232         int smb_mincnt = SVAL(inbuf,smb_vwv6);
233         int nread = -1;
234         char *data;
235         BOOL unused;
236
237         /* we don't use the offset given to use for pipe reads. This
238            is deliberate, instead we always return the next lump of
239            data on the pipe */
240 #if 0
241         uint32 smb_offs = IVAL(inbuf,smb_vwv3);
242 #endif
243
244         if (!p) {
245                 return(ERROR_DOS(ERRDOS,ERRbadfid));
246         }
247
248         set_message(outbuf,12,0,True);
249         data = smb_buf(outbuf);
250
251         nread = read_from_pipe(p, data, smb_maxcnt, &unused);
252
253         if (nread < 0) {
254                 return(UNIXERROR(ERRDOS,ERRnoaccess));
255         }
256   
257         SSVAL(outbuf,smb_vwv5,nread);
258         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
259         SSVAL(smb_buf(outbuf),-2,nread);
260   
261         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
262                  p->pnum, smb_mincnt, smb_maxcnt, nread));
263
264         /* Ensure we set up the message length to include the data length read. */
265         set_message_bcc(outbuf,nread);
266         return chain_reply(inbuf,outbuf,length,bufsize);
267 }
268
269 /****************************************************************************
270  Reply to a close.
271 ****************************************************************************/
272
273 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
274 {
275         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
276         int outsize = set_message(outbuf,0,0,True);
277
278         if (!p) {
279                 return(ERROR_DOS(ERRDOS,ERRbadfid));
280         }
281
282         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
283
284         if (!close_rpc_pipe_hnd(p)) {
285                 return ERROR_DOS(ERRDOS,ERRbadfid);
286         }
287
288         return(outsize);
289 }