if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / smbd / pipes.c
blob2837187f4bf15828e76c9ae0474eb20786cffd05
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.
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.
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.
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.
24 This file handles reply_ calls on named pipes that the server
25 makes to handle specific protocols
29 #include "includes.h"
31 #define PIPE "\\PIPE\\"
32 #define PIPELEN strlen(PIPE)
34 extern struct pipe_id_info pipe_names[];
36 /****************************************************************************
37 reply to an open and X on a named pipe
39 This code is basically stolen from reply_open_and_X with some
40 wrinkles to handle pipes.
41 ****************************************************************************/
42 int reply_open_pipe_and_X(connection_struct *conn,
43 char *inbuf,char *outbuf,int length,int bufsize)
45 pstring fname;
46 uint16 vuid = SVAL(inbuf, smb_uid);
47 pipes_struct *p;
48 int smb_ofun = SVAL(inbuf,smb_vwv8);
49 int size=0,fmode=0,mtime=0,rmode=0;
50 int i;
52 /* XXXX we need to handle passed times, sattr and flags */
53 pstrcpy(fname,smb_buf(inbuf));
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(fname,PIPE,PIPELEN) != 0 )
59 return(ERROR_DOS(ERRSRV,ERRaccess));
61 DEBUG(4,("Opening pipe %s.\n", fname));
63 /* See if it is one we want to handle. */
64 for( i = 0; pipe_names[i].client_pipe ; i++ )
65 if( strequal(fname,pipe_names[i].client_pipe) )
66 break;
68 if (pipe_names[i].client_pipe == NULL)
69 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
71 /* Strip \PIPE\ off the name. */
72 pstrcpy(fname,smb_buf(inbuf) + PIPELEN);
74 #if 0
76 * Hack for NT printers... JRA.
78 if(should_fail_next_srvsvc_open(fname))
79 return(ERROR_DOS(ERRSRV,ERRaccess));
80 #endif
82 /* Known pipes arrive with DIR attribs. Remove it so a regular file */
83 /* can be opened and add it in after the open. */
84 DEBUG(3,("Known pipe %s opening.\n",fname));
85 smb_ofun |= FILE_CREATE_IF_NOT_EXIST;
87 p = open_rpc_pipe_p(fname, conn, vuid);
88 if (!p) return(ERROR_DOS(ERRSRV,ERRnofids));
90 /* Prepare the reply */
91 set_message(outbuf,15,0,True);
93 /* Mark the opened file as an existing named pipe in message mode. */
94 SSVAL(outbuf,smb_vwv9,2);
95 SSVAL(outbuf,smb_vwv10,0xc700);
97 if (rmode == 2) {
98 DEBUG(4,("Resetting open result to open from create.\n"));
99 rmode = 1;
102 SSVAL(outbuf,smb_vwv2, p->pnum);
103 SSVAL(outbuf,smb_vwv3,fmode);
104 put_dos_date3(outbuf,smb_vwv4,mtime);
105 SIVAL(outbuf,smb_vwv6,size);
106 SSVAL(outbuf,smb_vwv8,rmode);
107 SSVAL(outbuf,smb_vwv11,0x0001);
109 return chain_reply(inbuf,outbuf,length,bufsize);
112 /****************************************************************************
113 reply to a write on a pipe
114 ****************************************************************************/
115 int reply_pipe_write(char *inbuf,char *outbuf,int length,int dum_bufsize)
117 pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
118 size_t numtowrite = SVAL(inbuf,smb_vwv1);
119 int nwritten;
120 int outsize;
121 char *data;
123 if (!p)
124 return(ERROR_DOS(ERRDOS,ERRbadfid));
126 data = smb_buf(inbuf) + 3;
128 if (numtowrite == 0)
129 nwritten = 0;
130 else
131 nwritten = write_to_pipe(p, data, numtowrite);
133 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
134 return (UNIXERROR(ERRDOS,ERRnoaccess));
136 outsize = set_message(outbuf,1,0,True);
138 SSVAL(outbuf,smb_vwv0,nwritten);
140 DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n",
141 p->pnum, nwritten));
143 return(outsize);
146 /****************************************************************************
147 Reply to a write and X.
149 This code is basically stolen from reply_write_and_X with some
150 wrinkles to handle pipes.
151 ****************************************************************************/
153 int reply_pipe_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
155 pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
156 size_t numtowrite = SVAL(inbuf,smb_vwv10);
157 int nwritten = -1;
158 int smb_doff = SVAL(inbuf, smb_vwv11);
159 BOOL pipe_start_message_raw = ((SVAL(inbuf, smb_vwv7) & (PIPE_START_MESSAGE|PIPE_RAW_MODE)) ==
160 (PIPE_START_MESSAGE|PIPE_RAW_MODE));
161 char *data;
163 if (!p)
164 return(ERROR_DOS(ERRDOS,ERRbadfid));
166 data = smb_base(inbuf) + smb_doff;
168 if (numtowrite == 0)
169 nwritten = 0;
170 else {
171 if(pipe_start_message_raw) {
173 * For the start of a message in named pipe byte mode,
174 * the first two bytes are a length-of-pdu field. Ignore
175 * them (we don't trust the client. JRA.
177 if(numtowrite < 2) {
178 DEBUG(0,("reply_pipe_write_and_X: start of message set and not enough data sent.(%u)\n",
179 (unsigned int)numtowrite ));
180 return (UNIXERROR(ERRDOS,ERRnoaccess));
183 data += 2;
184 numtowrite -= 2;
186 nwritten = write_to_pipe(p, data, numtowrite);
189 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0))
190 return (UNIXERROR(ERRDOS,ERRnoaccess));
192 set_message(outbuf,6,0,True);
194 nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
195 SSVAL(outbuf,smb_vwv2,nwritten);
197 DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n",
198 p->pnum, nwritten));
200 return chain_reply(inbuf,outbuf,length,bufsize);
203 /****************************************************************************
204 reply to a read and X
206 This code is basically stolen from reply_read_and_X with some
207 wrinkles to handle pipes.
208 ****************************************************************************/
209 int reply_pipe_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
211 pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv2);
212 int smb_maxcnt = SVAL(inbuf,smb_vwv5);
213 int smb_mincnt = SVAL(inbuf,smb_vwv6);
214 int nread = -1;
215 char *data;
216 /* we don't use the offset given to use for pipe reads. This
217 is deliberate, instead we always return the next lump of
218 data on the pipe */
219 #if 0
220 uint32 smb_offs = IVAL(inbuf,smb_vwv3);
221 #endif
223 if (!p)
224 return(ERROR_DOS(ERRDOS,ERRbadfid));
226 set_message(outbuf,12,0,True);
227 data = smb_buf(outbuf);
229 nread = read_from_pipe(p, data, smb_maxcnt);
231 if (nread < 0)
232 return(UNIXERROR(ERRDOS,ERRnoaccess));
234 SSVAL(outbuf,smb_vwv5,nread);
235 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
236 SSVAL(smb_buf(outbuf),-2,nread);
238 DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
239 p->pnum, smb_mincnt, smb_maxcnt, nread));
241 return chain_reply(inbuf,outbuf,length,bufsize);
244 /****************************************************************************
245 reply to a close
246 ****************************************************************************/
247 int reply_pipe_close(connection_struct *conn, char *inbuf,char *outbuf)
249 pipes_struct *p = get_rpc_pipe_p(inbuf,smb_vwv0);
250 int outsize = set_message(outbuf,0,0,True);
252 if (!p)
253 return(ERROR_DOS(ERRDOS,ERRbadfid));
255 DEBUG(5,("reply_pipe_close: pnum:%x\n", p->pnum));
257 if (!close_rpc_pipe_hnd(p, conn))
258 return(ERROR_DOS(ERRDOS,ERRbadfid));
260 return(outsize);