- added DCE/RPC "fault" PDU support.
[samba.git] / source3 / smbd / pipes.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Pipe SMB reply routines
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
7    Copyright (C) Paul Ashton  1997-1998.
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 #include "trans2.h"
31
32 #define PIPE            "\\PIPE\\"
33 #define PIPELEN         strlen(PIPE)
34
35 extern int DEBUGLEVEL;
36
37 extern struct pipe_id_info pipe_names[];
38
39 /****************************************************************************
40   reply to an open and X on a named pipe
41
42   This code is basically stolen from reply_open_and_X with some
43   wrinkles to handle pipes.
44 ****************************************************************************/
45 int reply_open_pipe_and_X(connection_struct *conn,
46                           char *inbuf,char *outbuf,int length,int bufsize)
47 {
48         pstring fname;
49         uint16 vuid = SVAL(inbuf, smb_uid);
50         pipes_struct *p;
51         int smb_ofun = SVAL(inbuf,smb_vwv8);
52         int size=0,fmode=0,mtime=0,rmode=0;
53         int i;
54
55         /* XXXX we need to handle passed times, sattr and flags */
56         pstrcpy(fname,smb_buf(inbuf));
57
58         /* If the name doesn't start \PIPE\ then this is directed */
59         /* at a mailslot or something we really, really don't understand, */
60         /* not just something we really don't understand. */
61         if ( strncmp(fname,PIPE,PIPELEN) != 0 )
62                 return(ERROR(ERRSRV,ERRaccess));
63
64         DEBUG(4,("Opening pipe %s.\n", fname));
65
66         /* See if it is one we want to handle. */
67         for( i = 0; pipe_names[i].client_pipe ; i++ )
68                 if( strequal(fname,pipe_names[i].client_pipe) )
69                         break;
70
71         if (pipe_names[i].client_pipe == NULL)
72                 return(ERROR(ERRSRV,ERRaccess));
73
74         /* Strip \PIPE\ off the name. */
75         pstrcpy(fname,smb_buf(inbuf) + PIPELEN);
76
77         /* Known pipes arrive with DIR attribs. Remove it so a regular file */
78         /* can be opened and add it in after the open. */
79         DEBUG(3,("Known pipe %s opening.\n",fname));
80         smb_ofun |= 0x10;               /* Add Create it not exists flag */
81
82         p = open_rpc_pipe_p(fname, conn, vuid);
83         if (!p) return(ERROR(ERRSRV,ERRnofids));
84
85         /* Prepare the reply */
86         set_message(outbuf,15,0,True);
87
88         /* Mark the opened file as an existing named pipe in message mode. */
89         SSVAL(outbuf,smb_vwv9,2);
90         SSVAL(outbuf,smb_vwv10,0xc700);
91
92         if (rmode == 2) {
93                 DEBUG(4,("Resetting open result to open from create.\n"));
94                 rmode = 1;
95         }
96
97         SSVAL(outbuf,smb_vwv2, p->pnum);
98         SSVAL(outbuf,smb_vwv3,fmode);
99         put_dos_date3(outbuf,smb_vwv4,mtime);
100         SIVAL(outbuf,smb_vwv6,size);
101         SSVAL(outbuf,smb_vwv8,rmode);
102         SSVAL(outbuf,smb_vwv11,0x0001);
103
104         return chain_reply(inbuf,outbuf,length,bufsize);
105 }
106
107 /****************************************************************************
108   reply to a write 
109
110   This code is basically stolen from reply_write with some
111   wrinkles to handle pipes.
112 ****************************************************************************/
113 int reply_pipe_write(char *inbuf,char *outbuf,int length,int bufsize)
114 {
115         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
116         size_t numtowrite = SVAL(inbuf,smb_vwv1);
117         int nwritten = -1;
118         char *data;
119         size_t outsize;
120
121         if (!p) return(ERROR(ERRDOS,ERRbadfid));
122
123         data = smb_buf(inbuf) + 3;
124
125         if (numtowrite == 0)
126         {
127                 nwritten = 0;
128         }
129         else
130         {
131                 nwritten = write_pipe(p, data, numtowrite);
132         }
133
134         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
135         {
136                 return (UNIXERROR(ERRDOS,ERRnoaccess));
137         }
138   
139         outsize = set_message(outbuf,1,0,True);
140
141         SSVAL(outbuf,smb_vwv0,nwritten);
142
143         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n",
144                  p->pnum, nwritten));
145
146         return outsize;
147 }
148
149 /****************************************************************************
150   reply to a write and X
151
152   This code is basically stolen from reply_write_and_X with some
153   wrinkles to handle pipes.
154 ****************************************************************************/
155 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
156 {
157         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
158         size_t numtowrite = SVAL(inbuf,smb_vwv10);
159         int nwritten = -1;
160         int smb_doff = SVAL(inbuf, smb_vwv11);
161         char *data;
162
163         if (!p) return(ERROR(ERRDOS,ERRbadfid));
164
165         data = smb_base(inbuf) + smb_doff;
166
167         if (numtowrite == 0)
168         {
169                 nwritten = 0;
170         }
171         else
172         {
173                 nwritten = write_pipe(p, data, numtowrite);
174         }
175
176         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
177         {
178                 return (UNIXERROR(ERRDOS,ERRnoaccess));
179         }
180   
181         set_message(outbuf,6,0,True);
182
183         SSVAL(outbuf,smb_vwv2,nwritten);
184   
185         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n",
186                  p->pnum, nwritten));
187
188         return chain_reply(inbuf,outbuf,length,bufsize);
189 }
190
191 /****************************************************************************
192   reply to a read and X
193
194   This code is basically stolen from reply_read_and_X with some
195   wrinkles to handle pipes.
196 ****************************************************************************/
197 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
198 {
199         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
200         uint32 smb_offs = IVAL(inbuf,smb_vwv3);
201         int smb_maxcnt = SVAL(inbuf,smb_vwv5);
202         int smb_mincnt = SVAL(inbuf,smb_vwv6);
203         int nread = -1;
204         char *data;
205
206         if (!p) return(ERROR(ERRDOS,ERRbadfid));
207
208         set_message(outbuf,12,0,True);
209         data = smb_buf(outbuf);
210
211         nread = read_pipe(p, data, smb_offs, smb_maxcnt);
212
213         if (nread < 0)
214                 return(UNIXERROR(ERRDOS,ERRnoaccess));
215   
216         SSVAL(outbuf,smb_vwv5,nread);
217         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
218         SSVAL(smb_buf(outbuf),-2,nread);
219   
220         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
221                  p->pnum, smb_mincnt, smb_maxcnt, nread));
222
223         return chain_reply(inbuf,outbuf,length,bufsize);
224 }
225
226 /****************************************************************************
227   reply to a close
228 ****************************************************************************/
229 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
230 {
231         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
232         int outsize = set_message(outbuf,0,0,True);
233
234         if (!p) return(ERROR(ERRDOS,ERRbadfid));
235
236         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
237
238         if (!close_rpc_pipe_hnd(p, conn)) return(ERROR(ERRDOS,ERRbadfid));
239
240         return(outsize);
241 }
242