Fixing get/set of security descriptors.
[samba.git] / source3 / smbd / pipes.c
1 #define OLD_NTDOMAIN 1
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    Pipe SMB reply routines
6    Copyright (C) Andrew Tridgell 1992-1998
7    Copyright (C) Luke Kenneth Casson Leighton 1996-1998
8    Copyright (C) Paul Ashton  1997-1998.
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24 /*
25    This file handles reply_ calls on named pipes that the server
26    makes to handle specific protocols
27 */
28
29
30 #include "includes.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 #if 0
78         /*
79          * Hack for NT printers... JRA.
80          */
81     if(should_fail_next_srvsvc_open(fname))
82       return(ERROR(ERRSRV,ERRaccess));
83 #endif
84
85         /* Known pipes arrive with DIR attribs. Remove it so a regular file */
86         /* can be opened and add it in after the open. */
87         DEBUG(3,("Known pipe %s opening.\n",fname));
88         smb_ofun |= FILE_CREATE_IF_NOT_EXIST;
89
90         p = open_rpc_pipe_p(fname, conn, vuid);
91         if (!p) return(ERROR(ERRSRV,ERRnofids));
92
93         /* Prepare the reply */
94         set_message(outbuf,15,0,True);
95
96         /* Mark the opened file as an existing named pipe in message mode. */
97         SSVAL(outbuf,smb_vwv9,2);
98         SSVAL(outbuf,smb_vwv10,0xc700);
99
100         if (rmode == 2) {
101                 DEBUG(4,("Resetting open result to open from create.\n"));
102                 rmode = 1;
103         }
104
105         SSVAL(outbuf,smb_vwv2, p->pnum);
106         SSVAL(outbuf,smb_vwv3,fmode);
107         put_dos_date3(outbuf,smb_vwv4,mtime);
108         SIVAL(outbuf,smb_vwv6,size);
109         SSVAL(outbuf,smb_vwv8,rmode);
110         SSVAL(outbuf,smb_vwv11,0x0001);
111
112         return chain_reply(inbuf,outbuf,length,bufsize);
113 }
114
115 /****************************************************************************
116   reply to a write on a pipe
117 ****************************************************************************/
118 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
119 {
120         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
121         size_t numtowrite = SVAL(inbuf,smb_vwv1);
122         int nwritten;
123         int outsize;
124         char *data;
125
126         if (!p)
127                 return(ERROR(ERRDOS,ERRbadfid));
128
129         data = smb_buf(inbuf) + 3;
130
131         if (numtowrite == 0)
132                 nwritten = 0;
133         else
134                 nwritten = write_to_pipe(p, data, numtowrite);
135
136         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
137                 return (UNIXERROR(ERRDOS,ERRnoaccess));
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
156 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
157 {
158         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
159         size_t numtowrite = SVAL(inbuf,smb_vwv10);
160         int nwritten = -1;
161         int smb_doff = SVAL(inbuf, smb_vwv11);
162         BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
163                                                                         (PIPE_START_MESSAGE|PIPE_RAW_MODE));
164         char *data;
165
166         if (!p)
167                 return(ERROR(ERRDOS,ERRbadfid));
168
169         data = smb_base(inbuf) + smb_doff;
170
171         if (numtowrite == 0)
172                 nwritten = 0;
173         else {
174                 if(pipe_start_message_raw) {
175                         /*
176                          * For the start of a message in named pipe byte mode,
177                          * the first two bytes are a length-of-pdu field. Ignore
178                          * them (we don't trust the client. JRA.
179                          */
180                 if(numtowrite < 2) {
181                                 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
182                                         (unsigned int)numtowrite ));
183                                 return (UNIXERROR(ERRDOS,ERRnoaccess));
184                         }
185
186                         data += 2;
187                         numtowrite -= 2;
188         }                        
189                 nwritten = write_to_pipe(p, data, numtowrite);
190         }
191
192         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
193                 return (UNIXERROR(ERRDOS,ERRnoaccess));
194   
195         set_message(outbuf,6,0,True);
196
197         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
198         SSVAL(outbuf,smb_vwv2,nwritten);
199   
200         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n",
201                  p->pnum, nwritten));
202
203         return chain_reply(inbuf,outbuf,length,bufsize);
204 }
205
206 /****************************************************************************
207   reply to a read and X
208
209   This code is basically stolen from reply_read_and_X with some
210   wrinkles to handle pipes.
211 ****************************************************************************/
212 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
213 {
214         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
215         int smb_maxcnt = SVAL(inbuf,smb_vwv5);
216         int smb_mincnt = SVAL(inbuf,smb_vwv6);
217         int nread = -1;
218         char *data;
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(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);
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         pipes_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(ERRDOS,ERRbadfid));
257
258         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
259
260         if (!close_rpc_pipe_hnd(p, conn))
261                 return(ERROR(ERRDOS,ERRbadfid));
262
263         return(outsize);
264 }
265
266 #undef OLD_NTDOMAIN