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 Copyright (C) Jeremy Allison 2005.
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 3 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, see <http://www.gnu.org/licenses/>.
23 This file handles reply_ calls on named pipes that the server
24 makes to handle specific protocols
30 #define PIPE "\\PIPE\\"
31 #define PIPELEN strlen(PIPE)
33 #define MAX_PIPE_NAME_LEN 24
35 /* PIPE/<name>/<pid>/<pnum> */
36 #define PIPEDB_KEY_FORMAT "PIPE/%s/%u/%d"
43 char name[MAX_PIPE_NAME_LEN];
47 /****************************************************************************
48 Reply to an open and X on a named pipe.
49 This code is basically stolen from reply_open_and_X with some
50 wrinkles to handle pipes.
51 ****************************************************************************/
53 void reply_open_pipe_and_X(connection_struct *conn, struct smb_request *req)
55 const char *fname = NULL;
56 char *pipe_name = NULL;
58 int size=0,fmode=0,mtime=0,rmode=0;
59 TALLOC_CTX *ctx = talloc_tos();
62 /* XXXX we need to handle passed times, sattr and flags */
63 srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2, &pipe_name,
64 smb_buf(req->inbuf), STR_TERMINATE);
66 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
71 /* If the name doesn't start \PIPE\ then this is directed */
72 /* at a mailslot or something we really, really don't understand, */
73 /* not just something we really don't understand. */
74 if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
75 reply_doserror(req, ERRSRV, ERRaccess);
79 DEBUG(4,("Opening pipe %s.\n", pipe_name));
81 /* See if it is one we want to handle. */
82 if (!is_known_pipename(pipe_name)) {
83 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
88 /* Strip \PIPE\ off the name. */
89 fname = pipe_name + PIPELEN;
93 * Hack for NT printers... JRA.
95 if(should_fail_next_srvsvc_open(fname)) {
96 reply_doserror(req, ERRSRV, ERRaccess);
101 /* Known pipes arrive with DIR attribs. Remove it so a regular file */
102 /* can be opened and add it in after the open. */
103 DEBUG(3,("Known pipe %s opening.\n",fname));
105 status = np_open(req, conn, fname, &fsp);
106 if (!NT_STATUS_IS_OK(status)) {
107 reply_nterror(req, status);
111 /* Prepare the reply */
112 reply_outbuf(req, 15, 0);
114 /* Mark the opened file as an existing named pipe in message mode. */
115 SSVAL(req->outbuf,smb_vwv9,2);
116 SSVAL(req->outbuf,smb_vwv10,0xc700);
119 DEBUG(4,("Resetting open result to open from create.\n"));
123 SSVAL(req->outbuf,smb_vwv2, fsp->fnum);
124 SSVAL(req->outbuf,smb_vwv3,fmode);
125 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
126 SIVAL(req->outbuf,smb_vwv6,size);
127 SSVAL(req->outbuf,smb_vwv8,rmode);
128 SSVAL(req->outbuf,smb_vwv11,0x0001);
134 /****************************************************************************
135 Reply to a write on a pipe.
136 ****************************************************************************/
138 void reply_pipe_write(struct smb_request *req)
140 files_struct *fsp = file_fsp(req, SVAL(req->inbuf,smb_vwv0));
141 size_t numtowrite = SVAL(req->inbuf,smb_vwv1);
145 if (!fsp_is_np(fsp)) {
146 reply_doserror(req, ERRDOS, ERRbadfid);
150 if (fsp->vuid != req->vuid) {
151 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
155 data = (uint8_t *)smb_buf(req->inbuf) + 3;
157 if (numtowrite == 0) {
161 status = np_write(fsp, data, numtowrite, &nwritten);
162 if (!NT_STATUS_IS_OK(status)) {
163 reply_nterror(req, status);
168 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
169 reply_unixerror(req, ERRDOS, ERRnoaccess);
173 reply_outbuf(req, 1, 0);
175 SSVAL(req->outbuf,smb_vwv0,nwritten);
177 DEBUG(3,("write-IPC pnum=%04x nwritten=%d\n", fsp->fnum,
183 /****************************************************************************
184 Reply to a write and X.
186 This code is basically stolen from reply_write_and_X with some
187 wrinkles to handle pipes.
188 ****************************************************************************/
190 void reply_pipe_write_and_X(struct smb_request *req)
192 files_struct *fsp = file_fsp(req, SVAL(req->inbuf, smb_vwv2));
193 size_t numtowrite = SVAL(req->inbuf,smb_vwv10);
195 int smb_doff = SVAL(req->inbuf, smb_vwv11);
196 bool pipe_start_message_raw =
197 ((SVAL(req->inbuf, smb_vwv7)
198 & (PIPE_START_MESSAGE|PIPE_RAW_MODE))
199 == (PIPE_START_MESSAGE|PIPE_RAW_MODE));
202 if (!fsp_is_np(fsp)) {
203 reply_doserror(req, ERRDOS, ERRbadfid);
207 if (fsp->vuid != req->vuid) {
208 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
212 data = (uint8_t *)smb_base(req->inbuf) + smb_doff;
214 if (numtowrite == 0) {
219 if(pipe_start_message_raw) {
221 * For the start of a message in named pipe byte mode,
222 * the first two bytes are a length-of-pdu field. Ignore
223 * them (we don't trust the client). JRA.
226 DEBUG(0,("reply_pipe_write_and_X: start of "
227 "message set and not enough data "
229 (unsigned int)numtowrite ));
230 reply_unixerror(req, ERRDOS, ERRnoaccess);
237 status = np_write(fsp, data, numtowrite, &nwritten);
238 if (!NT_STATUS_IS_OK(status)) {
239 reply_nterror(req, status);
244 if ((nwritten == 0 && numtowrite != 0) || (nwritten < 0)) {
245 reply_unixerror(req, ERRDOS,ERRnoaccess);
249 reply_outbuf(req, 6, 0);
251 nwritten = (pipe_start_message_raw ? nwritten + 2 : nwritten);
252 SSVAL(req->outbuf,smb_vwv2,nwritten);
254 DEBUG(3,("writeX-IPC pnum=%04x nwritten=%d\n", fsp->fnum,
260 /****************************************************************************
261 Reply to a read and X.
262 This code is basically stolen from reply_read_and_X with some
263 wrinkles to handle pipes.
264 ****************************************************************************/
266 void reply_pipe_read_and_X(struct smb_request *req)
268 files_struct *fsp = file_fsp(req, SVAL(req->inbuf,smb_vwv0));
269 int smb_maxcnt = SVAL(req->inbuf,smb_vwv5);
270 int smb_mincnt = SVAL(req->inbuf,smb_vwv6);
276 /* we don't use the offset given to use for pipe reads. This
277 is deliberate, instead we always return the next lump of
280 uint32 smb_offs = IVAL(req->inbuf,smb_vwv3);
283 if (!fsp_is_np(fsp)) {
284 reply_doserror(req, ERRDOS, ERRbadfid);
288 if (fsp->vuid != req->vuid) {
289 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
293 reply_outbuf(req, 12, smb_maxcnt);
295 data = (uint8_t *)smb_buf(req->outbuf);
297 status = np_read(fsp, data, smb_maxcnt, &nread, &unused);
299 if (!NT_STATUS_IS_OK(status)) {
300 reply_doserror(req, ERRDOS, ERRnoaccess);
304 srv_set_message((char *)req->outbuf, 12, nread, False);
306 SSVAL(req->outbuf,smb_vwv5,nread);
307 SSVAL(req->outbuf,smb_vwv6,smb_offset(data,req->outbuf));
308 SSVAL(smb_buf(req->outbuf),-2,nread);
310 DEBUG(3,("readX-IPC pnum=%04x min=%d max=%d nread=%d\n",
311 fsp->fnum, smb_mincnt, smb_maxcnt, (int)nread));