This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[tprouty/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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 extern struct pipe_id_info pipe_names[];
34
35 /****************************************************************************
36   reply to an open and X on a named pipe
37
38   This code is basically stolen from reply_open_and_X with some
39   wrinkles to handle pipes.
40 ****************************************************************************/
41 int reply_open_pipe_and_X(connection_struct *conn,
42                           char *inbuf,char *outbuf,int length,int bufsize)
43 {
44         pstring fname;
45         pstring pipe_name;
46         uint16 vuid = SVAL(inbuf, smb_uid);
47         smb_np_struct *p;
48         int smb_ofun = SVAL(inbuf,smb_vwv8);
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(inbuf, pipe_name, smb_buf(inbuf), sizeof(pipe_name), -1, 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         DEBUG(4,("Opening pipe %s.\n", pipe_name));
62
63         /* See if it is one we want to handle. */
64         for( i = 0; pipe_names[i].client_pipe ; i++ )
65                 if( strequal(pipe_name,pipe_names[i].client_pipe) )
66                         break;
67
68         if (pipe_names[i].client_pipe == NULL)
69                 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
70
71         /* Strip \PIPE\ off the name. */
72         pstrcpy(fname, pipe_name + PIPELEN);
73
74
75 #if 0
76         /*
77          * Hack for NT printers... JRA.
78          */
79     if(should_fail_next_srvsvc_open(fname))
80       return(ERROR(ERRSRV,ERRaccess));
81 #endif
82
83         /* Known pipes arrive with DIR attribs. Remove it so a regular file */
84         /* can be opened and add it in after the open. */
85         DEBUG(3,("Known pipe %s opening.\n",fname));
86         smb_ofun |= FILE_CREATE_IF_NOT_EXIST;
87
88         p = open_rpc_pipe_p(fname, conn, vuid);
89         if (!p) return(ERROR_DOS(ERRSRV,ERRnofids));
90
91         /* Prepare the reply */
92         set_message(outbuf,15,0,True);
93
94         /* Mark the opened file as an existing named pipe in message mode. */
95         SSVAL(outbuf,smb_vwv9,2);
96         SSVAL(outbuf,smb_vwv10,0xc700);
97
98         if (rmode == 2) {
99                 DEBUG(4,("Resetting open result to open from create.\n"));
100                 rmode = 1;
101         }
102
103         SSVAL(outbuf,smb_vwv2, p->pnum);
104         SSVAL(outbuf,smb_vwv3,fmode);
105         put_dos_date3(outbuf,smb_vwv4,mtime);
106         SIVAL(outbuf,smb_vwv6,size);
107         SSVAL(outbuf,smb_vwv8,rmode);
108         SSVAL(outbuf,smb_vwv11,0x0001);
109
110         return chain_reply(inbuf,outbuf,length,bufsize);
111 }
112
113 /****************************************************************************
114   reply to a write on a pipe
115 ****************************************************************************/
116 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
117 {
118         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
119         size_t numtowrite = SVAL(inbuf,smb_vwv1);
120         int nwritten;
121         int outsize;
122         char *data;
123
124         if (!p)
125                 return(ERROR_DOS(ERRDOS,ERRbadfid));
126
127         data = smb_buf(inbuf) + 3;
128
129         if (numtowrite == 0)
130                 nwritten = 0;
131         else
132                 nwritten = write_to_pipe(p, data, numtowrite);
133
134         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
135                 return (UNIXERROR(ERRDOS,ERRnoaccess));
136   
137         outsize = set_message(outbuf,1,0,True);
138
139         SSVAL(outbuf,smb_vwv0,nwritten);
140   
141         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n",
142                  p->pnum, nwritten));
143
144         return(outsize);
145 }
146
147 /****************************************************************************
148  Reply to a write and X.
149
150  This code is basically stolen from reply_write_and_X with some
151  wrinkles to handle pipes.
152 ****************************************************************************/
153
154 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
155 {
156         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
157         size_t numtowrite = SVAL(inbuf,smb_vwv10);
158         int nwritten = -1;
159         int smb_doff = SVAL(inbuf, smb_vwv11);
160         BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
161                                                                         (PIPE_START_MESSAGE|PIPE_RAW_MODE));
162         char *data;
163
164         if (!p)
165                 return(ERROR_DOS(ERRDOS,ERRbadfid));
166
167         data = smb_base(inbuf) + smb_doff;
168
169         if (numtowrite == 0)
170                 nwritten = 0;
171         else {
172                 if(pipe_start_message_raw) {
173                         /*
174                          * For the start of a message in named pipe byte mode,
175                          * the first two bytes are a length-of-pdu field. Ignore
176                          * them (we don't trust the client. JRA.
177                          */
178                 if(numtowrite < 2) {
179                                 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
180                                         (unsigned int)numtowrite ));
181                                 return (UNIXERROR(ERRDOS,ERRnoaccess));
182                         }
183
184                         data += 2;
185                         numtowrite -= 2;
186         }                        
187                 nwritten = write_to_pipe(p, data, numtowrite);
188         }
189
190         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
191                 return (UNIXERROR(ERRDOS,ERRnoaccess));
192   
193         set_message(outbuf,6,0,True);
194
195         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
196         SSVAL(outbuf,smb_vwv2,nwritten);
197   
198         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n",
199                  p->pnum, nwritten));
200
201         return chain_reply(inbuf,outbuf,length,bufsize);
202 }
203
204 /****************************************************************************
205   reply to a read and X
206
207   This code is basically stolen from reply_read_and_X with some
208   wrinkles to handle pipes.
209 ****************************************************************************/
210 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
211 {
212         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
213         int smb_maxcnt = SVAL(inbuf,smb_vwv5);
214         int smb_mincnt = SVAL(inbuf,smb_vwv6);
215         int nread = -1;
216         char *data;
217         BOOL unused;
218
219         /* we don't use the offset given to use for pipe reads. This
220            is deliberate, instead we always return the next lump of
221            data on the pipe */
222 #if 0
223         uint32 smb_offs = IVAL(inbuf,smb_vwv3);
224 #endif
225
226         if (!p)
227                 return(ERROR_DOS(ERRDOS,ERRbadfid));
228
229         set_message(outbuf,12,0,True);
230         data = smb_buf(outbuf);
231
232         nread = read_from_pipe(p, data, smb_maxcnt, &unused);
233
234         if (nread < 0)
235                 return(UNIXERROR(ERRDOS,ERRnoaccess));
236   
237         SSVAL(outbuf,smb_vwv5,nread);
238         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
239         SSVAL(smb_buf(outbuf),-2,nread);
240   
241         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
242                  p->pnum, smb_mincnt, smb_maxcnt, nread));
243
244         return chain_reply(inbuf,outbuf,length,bufsize);
245 }
246
247 /****************************************************************************
248   reply to a close
249 ****************************************************************************/
250 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
251 {
252         smb_np_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
253         int outsize = set_message(outbuf,0,0,True);
254
255         if (!p)
256                 return(ERROR_DOS(ERRDOS,ERRbadfid));
257
258         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
259
260         if (!close_rpc_pipe_hnd(p))
261                 return ERROR_DOS(ERRDOS,ERRbadfid);
262
263         return(outsize);
264 }