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
29 #include "smbd/globals.h"
30 #include "libcli/security/security.h"
32 #define PIPE "\\PIPE\\"
33 #define PIPELEN strlen(PIPE)
35 #define MAX_PIPE_NAME_LEN 24
37 NTSTATUS
open_np_file(struct smb_request
*smb_req
, const char *name
,
38 struct files_struct
**pfsp
)
40 struct connection_struct
*conn
= smb_req
->conn
;
41 struct files_struct
*fsp
;
42 struct smb_filename
*smb_fname
= NULL
;
45 status
= file_new(smb_req
, conn
, &fsp
);
46 if (!NT_STATUS_IS_OK(status
)) {
47 DEBUG(0, ("file_new failed: %s\n", nt_errstr(status
)));
53 fsp
->vuid
= smb_req
->vuid
;
54 fsp
->can_lock
= false;
55 fsp
->access_mask
= FILE_READ_DATA
| FILE_WRITE_DATA
;
57 status
= create_synthetic_smb_fname(talloc_tos(), name
, NULL
, NULL
,
59 if (!NT_STATUS_IS_OK(status
)) {
60 file_free(smb_req
, fsp
);
63 status
= fsp_set_smb_fname(fsp
, smb_fname
);
64 TALLOC_FREE(smb_fname
);
65 if (!NT_STATUS_IS_OK(status
)) {
66 file_free(smb_req
, fsp
);
70 status
= np_open(fsp
, name
,
71 conn
->sconn
->local_address
,
72 conn
->sconn
->remote_address
,
73 &conn
->sconn
->client_id
,
76 &fsp
->fake_file_handle
);
77 if (!NT_STATUS_IS_OK(status
)) {
78 DEBUG(10, ("np_open(%s) returned %s\n", name
,
80 file_free(smb_req
, fsp
);
89 /****************************************************************************
90 Reply to an open and X on a named pipe.
91 This code is basically stolen from reply_open_and_X with some
92 wrinkles to handle pipes.
93 ****************************************************************************/
95 void reply_open_pipe_and_X(connection_struct
*conn
, struct smb_request
*req
)
97 const char *fname
= NULL
;
98 char *pipe_name
= NULL
;
100 TALLOC_CTX
*ctx
= talloc_tos();
103 /* XXXX we need to handle passed times, sattr and flags */
104 srvstr_pull_req_talloc(ctx
, req
, &pipe_name
, req
->buf
, STR_TERMINATE
);
106 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
111 /* If the name doesn't start \PIPE\ then this is directed */
112 /* at a mailslot or something we really, really don't understand, */
113 /* not just something we really don't understand. */
114 if ( strncmp(pipe_name
,PIPE
,PIPELEN
) != 0 ) {
115 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
119 DEBUG(4,("Opening pipe %s.\n", pipe_name
));
121 /* Strip \PIPE\ off the name. */
122 fname
= pipe_name
+ PIPELEN
;
126 * Hack for NT printers... JRA.
128 if(should_fail_next_srvsvc_open(fname
)) {
129 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
134 status
= open_np_file(req
, fname
, &fsp
);
135 if (!NT_STATUS_IS_OK(status
)) {
136 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
137 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
141 reply_nterror(req
, status
);
145 /* Prepare the reply */
146 reply_outbuf(req
, 15, 0);
148 /* Mark the opened file as an existing named pipe in message mode. */
149 SSVAL(req
->outbuf
,smb_vwv9
,2);
150 SSVAL(req
->outbuf
,smb_vwv10
,0xc700);
152 SSVAL(req
->outbuf
, smb_vwv2
, fsp
->fnum
);
153 SSVAL(req
->outbuf
, smb_vwv3
, 0); /* fmode */
154 srv_put_dos_date3((char *)req
->outbuf
, smb_vwv4
, 0); /* mtime */
155 SIVAL(req
->outbuf
, smb_vwv6
, 0); /* size */
156 SSVAL(req
->outbuf
, smb_vwv8
, 0); /* rmode */
157 SSVAL(req
->outbuf
, smb_vwv11
, 0x0001);
163 /****************************************************************************
164 Reply to a write on a pipe.
165 ****************************************************************************/
167 struct pipe_write_state
{
171 static void pipe_write_done(struct tevent_req
*subreq
);
173 void reply_pipe_write(struct smb_request
*req
)
175 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
177 struct pipe_write_state
*state
;
178 struct tevent_req
*subreq
;
180 if (!fsp_is_np(fsp
)) {
181 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
185 if (fsp
->vuid
!= req
->vuid
) {
186 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
190 state
= talloc(req
, struct pipe_write_state
);
192 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
195 req
->async_priv
= state
;
197 state
->numtowrite
= SVAL(req
->vwv
+1, 0);
201 DEBUG(6, ("reply_pipe_write: %x name: %s len: %d\n", (int)fsp
->fnum
,
202 fsp_str_dbg(fsp
), (int)state
->numtowrite
));
204 subreq
= np_write_send(state
, smbd_event_context(),
205 fsp
->fake_file_handle
, data
, state
->numtowrite
);
206 if (subreq
== NULL
) {
208 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
211 tevent_req_set_callback(subreq
, pipe_write_done
,
212 talloc_move(req
->conn
, &req
));
215 static void pipe_write_done(struct tevent_req
*subreq
)
217 struct smb_request
*req
= tevent_req_callback_data(
218 subreq
, struct smb_request
);
219 struct pipe_write_state
*state
= talloc_get_type_abort(
220 req
->async_priv
, struct pipe_write_state
);
222 ssize_t nwritten
= -1;
224 status
= np_write_recv(subreq
, &nwritten
);
227 reply_nterror(req
, status
);
231 /* Looks bogus to me now. Needs to be removed ? JRA. */
232 if ((nwritten
== 0 && state
->numtowrite
!= 0)) {
233 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
237 reply_outbuf(req
, 1, 0);
239 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
241 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten
));
244 if (!srv_send_smb(req
->sconn
, (char *)req
->outbuf
,
246 IS_CONN_ENCRYPTED(req
->conn
)||req
->encrypted
,
248 exit_server_cleanly("construct_reply: srv_send_smb failed.");
253 /****************************************************************************
254 Reply to a write and X.
256 This code is basically stolen from reply_write_and_X with some
257 wrinkles to handle pipes.
258 ****************************************************************************/
260 struct pipe_write_andx_state
{
261 bool pipe_start_message_raw
;
265 static void pipe_write_andx_done(struct tevent_req
*subreq
);
267 void reply_pipe_write_and_X(struct smb_request
*req
)
269 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+2, 0));
270 int smb_doff
= SVAL(req
->vwv
+11, 0);
272 struct pipe_write_andx_state
*state
;
273 struct tevent_req
*subreq
;
275 if (!fsp_is_np(fsp
)) {
276 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
280 if (fsp
->vuid
!= req
->vuid
) {
281 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
285 state
= talloc(req
, struct pipe_write_andx_state
);
287 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
290 req
->async_priv
= state
;
292 state
->numtowrite
= SVAL(req
->vwv
+10, 0);
293 state
->pipe_start_message_raw
=
294 ((SVAL(req
->vwv
+7, 0) & (PIPE_START_MESSAGE
|PIPE_RAW_MODE
))
295 == (PIPE_START_MESSAGE
|PIPE_RAW_MODE
));
297 DEBUG(6, ("reply_pipe_write_and_X: %x name: %s len: %d\n",
298 (int)fsp
->fnum
, fsp_str_dbg(fsp
), (int)state
->numtowrite
));
300 data
= (uint8_t *)smb_base(req
->inbuf
) + smb_doff
;
302 if (state
->pipe_start_message_raw
) {
304 * For the start of a message in named pipe byte mode,
305 * the first two bytes are a length-of-pdu field. Ignore
306 * them (we don't trust the client). JRA.
308 if (state
->numtowrite
< 2) {
309 DEBUG(0,("reply_pipe_write_and_X: start of message "
310 "set and not enough data sent.(%u)\n",
311 (unsigned int)state
->numtowrite
));
312 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
317 state
->numtowrite
-= 2;
320 subreq
= np_write_send(state
, smbd_event_context(),
321 fsp
->fake_file_handle
, data
, state
->numtowrite
);
322 if (subreq
== NULL
) {
324 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
327 tevent_req_set_callback(subreq
, pipe_write_andx_done
,
328 talloc_move(req
->conn
, &req
));
331 static void pipe_write_andx_done(struct tevent_req
*subreq
)
333 struct smb_request
*req
= tevent_req_callback_data(
334 subreq
, struct smb_request
);
335 struct pipe_write_andx_state
*state
= talloc_get_type_abort(
336 req
->async_priv
, struct pipe_write_andx_state
);
338 ssize_t nwritten
= -1;
340 status
= np_write_recv(subreq
, &nwritten
);
343 if (!NT_STATUS_IS_OK(status
)) {
344 reply_nterror(req
, status
);
348 /* Looks bogus to me now. Is this error message correct ? JRA. */
349 if (nwritten
!= state
->numtowrite
) {
350 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
354 reply_outbuf(req
, 6, 0);
356 nwritten
= (state
->pipe_start_message_raw
? nwritten
+ 2 : nwritten
);
357 SSVAL(req
->outbuf
,smb_vwv2
,nwritten
);
359 DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten
));
364 * We must free here as the ownership of req was
365 * moved to the connection struct in reply_pipe_write_and_X().
370 /****************************************************************************
371 Reply to a read and X.
372 This code is basically stolen from reply_read_and_X with some
373 wrinkles to handle pipes.
374 ****************************************************************************/
376 struct pipe_read_andx_state
{
382 static void pipe_read_andx_done(struct tevent_req
*subreq
);
384 void reply_pipe_read_and_X(struct smb_request
*req
)
386 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
388 struct pipe_read_andx_state
*state
;
389 struct tevent_req
*subreq
;
391 /* we don't use the offset given to use for pipe reads. This
392 is deliberate, instead we always return the next lump of
395 uint32 smb_offs
= IVAL(req
->vwv
+3, 0);
398 if (!fsp_is_np(fsp
)) {
399 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
403 if (fsp
->vuid
!= req
->vuid
) {
404 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
408 state
= talloc(req
, struct pipe_read_andx_state
);
410 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
413 req
->async_priv
= state
;
415 state
->smb_maxcnt
= SVAL(req
->vwv
+5, 0);
416 state
->smb_mincnt
= SVAL(req
->vwv
+6, 0);
418 reply_outbuf(req
, 12, state
->smb_maxcnt
);
419 data
= (uint8_t *)smb_buf(req
->outbuf
);
422 * We have to tell the upper layers that we're async.
424 state
->outbuf
= req
->outbuf
;
427 subreq
= np_read_send(state
, smbd_event_context(),
428 fsp
->fake_file_handle
, data
,
430 if (subreq
== NULL
) {
431 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
434 tevent_req_set_callback(subreq
, pipe_read_andx_done
,
435 talloc_move(req
->conn
, &req
));
438 static void pipe_read_andx_done(struct tevent_req
*subreq
)
440 struct smb_request
*req
= tevent_req_callback_data(
441 subreq
, struct smb_request
);
442 struct pipe_read_andx_state
*state
= talloc_get_type_abort(
443 req
->async_priv
, struct pipe_read_andx_state
);
446 bool is_data_outstanding
;
448 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
450 if (!NT_STATUS_IS_OK(status
)) {
451 reply_nterror(req
, status
);
455 req
->outbuf
= state
->outbuf
;
456 state
->outbuf
= NULL
;
458 srv_set_message((char *)req
->outbuf
, 12, nread
, False
);
462 * we should return STATUS_BUFFER_OVERFLOW if there's
465 * But we can't enable it yet, as it has bad interactions
466 * with fixup_chain_error_packet() in chain_reply().
468 if (is_data_outstanding
) {
469 error_packet_set((char *)req
->outbuf
, ERRDOS
, ERRmoredata
,
470 STATUS_BUFFER_OVERFLOW
, __LINE__
, __FILE__
);
474 SSVAL(req
->outbuf
,smb_vwv5
,nread
);
475 SSVAL(req
->outbuf
,smb_vwv6
,
477 + 1 /* the wct field */
478 + 12 * sizeof(uint16_t) /* vwv */
479 + 2); /* the buflen field */
480 SSVAL(req
->outbuf
,smb_vwv11
,state
->smb_maxcnt
);
482 DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
483 state
->smb_mincnt
, state
->smb_maxcnt
, (int)nread
));
488 * We must free here as the ownership of req was
489 * moved to the connection struct in reply_pipe_read_and_X().