allow to use usrmgr/svrmgr tools in win95
[samba.git] / source / 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
31 #define PIPE            "\\PIPE\\"
32 #define PIPELEN         strlen(PIPE)
33
34 extern int DEBUGLEVEL;
35
36 extern struct pipe_id_info pipe_names[];
37
38 /****************************************************************************
39   reply to an open and X on a named pipe
40
41   This code is basically stolen from reply_open_and_X with some
42   wrinkles to handle pipes.
43 ****************************************************************************/
44 int reply_open_pipe_and_X(connection_struct *conn,
45                           char *inbuf,char *outbuf,int length,int bufsize)
46 {
47         pstring fname;
48         pstring pipe_name;
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         srvstr_pull(inbuf, pipe_name, smb_buf(inbuf), sizeof(pipe_name), -1, STR_TERMINATE);
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(pipe_name,PIPE,PIPELEN) != 0 )
62                 return(ERROR(ERRSRV,ERRaccess));
63
64         DEBUG(4,("Opening pipe %s.\n", pipe_name));
65
66         /* See if it is one we want to handle. */
67         for( i = 0; pipe_names[i].client_pipe ; i++ )
68                 if( strequal(pipe_name,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, pipe_name + PIPELEN);
76
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         smb_ofun |= FILE_CREATE_IF_NOT_EXIST;
90
91         p = open_rpc_pipe_p(fname, conn, vuid);
92         if (!p) return(ERROR(ERRSRV,ERRnofids));
93
94         /* Prepare the reply */
95         set_message(outbuf,15,0,True);
96
97         /* Mark the opened file as an existing named pipe in message mode. */
98         SSVAL(outbuf,smb_vwv9,2);
99         SSVAL(outbuf,smb_vwv10,0xc700);
100
101         if (rmode == 2) {
102                 DEBUG(4,("Resetting open result to open from create.\n"));
103                 rmode = 1;
104         }
105
106         SSVAL(outbuf,smb_vwv2, p->pnum);
107         SSVAL(outbuf,smb_vwv3,fmode);
108         put_dos_date3(outbuf,smb_vwv4,mtime);
109         SIVAL(outbuf,smb_vwv6,size);
110         SSVAL(outbuf,smb_vwv8,rmode);
111         SSVAL(outbuf,smb_vwv11,0x0001);
112
113         return chain_reply(inbuf,outbuf,length,bufsize);
114 }
115
116 /****************************************************************************
117   reply to a write on a pipe
118 ****************************************************************************/
119 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
120 {
121         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
122         size_t numtowrite = SVAL(inbuf,smb_vwv1);
123         int nwritten;
124         int outsize;
125         char *data;
126
127         if (!p)
128                 return(ERROR(ERRDOS,ERRbadfid));
129
130         data = smb_buf(inbuf) + 3;
131
132         if (numtowrite == 0)
133                 nwritten = 0;
134         else
135                 nwritten = write_to_pipe(p, data, numtowrite);
136
137         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
138                 return (UNIXERROR(ERRDOS,ERRnoaccess));
139   
140         outsize = set_message(outbuf,1,0,True);
141
142         SSVAL(outbuf,smb_vwv0,nwritten);
143   
144         DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n",
145                  p->pnum, nwritten));
146
147         return(outsize);
148 }
149
150 /****************************************************************************
151  Reply to a write and X.
152
153  This code is basically stolen from reply_write_and_X with some
154  wrinkles to handle pipes.
155 ****************************************************************************/
156
157 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
158 {
159         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
160         size_t numtowrite = SVAL(inbuf,smb_vwv10);
161         int nwritten = -1;
162         int smb_doff = SVAL(inbuf, smb_vwv11);
163         BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
164                                                                         (PIPE_START_MESSAGE|PIPE_RAW_MODE));
165         char *data;
166
167         if (!p)
168                 return(ERROR(ERRDOS,ERRbadfid));
169
170         data = smb_base(inbuf) + smb_doff;
171
172         if (numtowrite == 0)
173                 nwritten = 0;
174         else {
175                 if(pipe_start_message_raw) {
176                         /*
177                          * For the start of a message in named pipe byte mode,
178                          * the first two bytes are a length-of-pdu field. Ignore
179                          * them (we don't trust the client. JRA.
180                          */
181                 if(numtowrite < 2) {
182                                 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
183                                         (unsigned int)numtowrite ));
184                                 return (UNIXERROR(ERRDOS,ERRnoaccess));
185                         }
186
187                         data += 2;
188                         numtowrite -= 2;
189         }                        
190                 nwritten = write_to_pipe(p, data, numtowrite);
191         }
192
193         if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
194                 return (UNIXERROR(ERRDOS,ERRnoaccess));
195   
196         set_message(outbuf,6,0,True);
197
198         nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
199         SSVAL(outbuf,smb_vwv2,nwritten);
200   
201         DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n",
202                  p->pnum, nwritten));
203
204         return chain_reply(inbuf,outbuf,length,bufsize);
205 }
206
207 /****************************************************************************
208   reply to a read and X
209
210   This code is basically stolen from reply_read_and_X with some
211   wrinkles to handle pipes.
212 ****************************************************************************/
213 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
214 {
215         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
216         int smb_maxcnt = SVAL(inbuf,smb_vwv5);
217         int smb_mincnt = SVAL(inbuf,smb_vwv6);
218         int nread = -1;
219         char *data;
220         /* we don't use the offset given to use for pipe reads. This
221            is deliberate, instead we always return the next lump of
222            data on the pipe */
223 #if 0
224         uint32 smb_offs = IVAL(inbuf,smb_vwv3);
225 #endif
226
227         if (!p)
228                 return(ERROR(ERRDOS,ERRbadfid));
229
230         set_message(outbuf,12,0,True);
231         data = smb_buf(outbuf);
232
233         nread = read_from_pipe(p, data, smb_maxcnt);
234
235         if (nread < 0)
236                 return(UNIXERROR(ERRDOS,ERRnoaccess));
237   
238         SSVAL(outbuf,smb_vwv5,nread);
239         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
240         SSVAL(smb_buf(outbuf),-2,nread);
241   
242         DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
243                  p->pnum, smb_mincnt, smb_maxcnt, nread));
244
245         return chain_reply(inbuf,outbuf,length,bufsize);
246 }
247
248 /****************************************************************************
249   reply to a close
250 ****************************************************************************/
251 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
252 {
253         pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
254         int outsize = set_message(outbuf,0,0,True);
255
256         if (!p)
257                 return(ERROR(ERRDOS,ERRbadfid));
258
259         DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
260
261         if (!close_rpc_pipe_hnd(p, conn))
262                 return(ERROR(ERRDOS,ERRbadfid));
263
264         return(outsize);
265 }