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/smbd.h"
30 #include "smbd/globals.h"
31 #include "libcli/security/security.h"
32 #include "rpc_server/srv_pipe_hnd.h"
33 #include "auth/auth_util.h"
34 #include "librpc/rpc/dcerpc_helper.h"
36 /****************************************************************************
37 Reply to an open and X on a named pipe.
38 This code is basically stolen from reply_open_and_X with some
39 wrinkles to handle pipes.
40 ****************************************************************************/
42 void reply_open_pipe_and_X(connection_struct
*conn
, struct smb_request
*req
)
44 const char *fname
= NULL
;
45 char *pipe_name
= NULL
;
47 TALLOC_CTX
*ctx
= talloc_tos();
50 /* XXXX we need to handle passed times, sattr and flags */
51 srvstr_pull_req_talloc(ctx
, req
, &pipe_name
, req
->buf
, STR_TERMINATE
);
53 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
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. */
63 #define PIPELEN strlen(PIPE)
66 while (fname
[0] == '\\') {
69 if (!strnequal(fname
, PIPE
, PIPELEN
)) {
70 reply_nterror(req
, NT_STATUS_OBJECT_PATH_SYNTAX_BAD
);
74 while (fname
[0] == '\\') {
78 DEBUG(4,("Opening pipe %s => %s.\n", pipe_name
, fname
));
82 * Hack for NT printers... JRA.
84 if(should_fail_next_srvsvc_open(fname
)) {
85 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
90 status
= open_np_file(req
, fname
, &fsp
);
91 if (!NT_STATUS_IS_OK(status
)) {
92 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
93 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
97 reply_nterror(req
, status
);
101 /* Prepare the reply */
102 reply_smb1_outbuf(req
, 15, 0);
104 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
105 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
107 /* Mark the opened file as an existing named pipe in message mode. */
108 SSVAL(req
->outbuf
,smb_vwv9
,2);
109 SSVAL(req
->outbuf
,smb_vwv10
,0xc700);
111 SSVAL(req
->outbuf
, smb_vwv2
, fsp
->fnum
);
112 SSVAL(req
->outbuf
, smb_vwv3
, 0); /* fmode */
113 srv_put_dos_date3((char *)req
->outbuf
, smb_vwv4
, 0); /* mtime */
114 SIVAL(req
->outbuf
, smb_vwv6
, 0); /* size */
115 SSVAL(req
->outbuf
, smb_vwv8
, 0); /* rmode */
116 SSVAL(req
->outbuf
, smb_vwv11
, 0x0001);
119 /****************************************************************************
120 Reply to a write and X.
122 This code is basically stolen from reply_write_and_X with some
123 wrinkles to handle pipes.
124 ****************************************************************************/
126 struct pipe_write_andx_state
{
127 bool pipe_start_message_raw
;
131 static void pipe_write_andx_done(struct tevent_req
*subreq
);
133 void reply_pipe_write_and_X(struct smb_request
*req
)
135 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+2, 0));
136 int smb_doff
= SVAL(req
->vwv
+11, 0);
138 struct pipe_write_andx_state
*state
;
139 struct tevent_req
*subreq
;
141 if (!fsp_is_np(fsp
)) {
142 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
146 if (fsp
->vuid
!= req
->vuid
) {
147 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
151 state
= talloc(req
, struct pipe_write_andx_state
);
153 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
156 req
->async_priv
= state
;
158 state
->numtowrite
= SVAL(req
->vwv
+10, 0);
159 state
->pipe_start_message_raw
=
160 ((SVAL(req
->vwv
+7, 0) & (PIPE_START_MESSAGE
|PIPE_RAW_MODE
))
161 == (PIPE_START_MESSAGE
|PIPE_RAW_MODE
));
163 DEBUG(6, ("reply_pipe_write_and_X: %s, name: %s len: %d\n",
164 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
), (int)state
->numtowrite
));
166 data
= (const uint8_t *)smb_base(req
->inbuf
) + smb_doff
;
168 if (state
->pipe_start_message_raw
) {
170 * For the start of a message in named pipe byte mode,
171 * the first two bytes are a length-of-pdu field. Ignore
172 * them (we don't trust the client). JRA.
174 if (state
->numtowrite
< 2) {
175 DEBUG(0,("reply_pipe_write_and_X: start of message "
176 "set and not enough data sent.(%u)\n",
177 (unsigned int)state
->numtowrite
));
178 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
183 state
->numtowrite
-= 2;
186 subreq
= np_write_send(state
, req
->sconn
->ev_ctx
,
187 fsp
->fake_file_handle
, data
, state
->numtowrite
);
188 if (subreq
== NULL
) {
190 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
193 tevent_req_set_callback(subreq
, pipe_write_andx_done
,
194 talloc_move(req
->conn
, &req
));
197 static void pipe_write_andx_done(struct tevent_req
*subreq
)
199 struct smb_request
*req
= tevent_req_callback_data(
200 subreq
, struct smb_request
);
201 struct pipe_write_andx_state
*state
= talloc_get_type_abort(
202 req
->async_priv
, struct pipe_write_andx_state
);
204 ssize_t nwritten
= -1;
206 status
= np_write_recv(subreq
, &nwritten
);
209 if (!NT_STATUS_IS_OK(status
)) {
210 reply_nterror(req
, status
);
214 /* Looks bogus to me now. Is this error message correct ? JRA. */
215 if (nwritten
!= state
->numtowrite
) {
216 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
220 reply_smb1_outbuf(req
, 6, 0);
222 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
223 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
225 nwritten
= (state
->pipe_start_message_raw
? nwritten
+ 2 : nwritten
);
226 SSVAL(req
->outbuf
,smb_vwv2
,nwritten
);
228 DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten
));
232 * We must free here as the ownership of req was
233 * moved to the connection struct in reply_pipe_write_and_X().
235 smb_request_done(req
);
238 /****************************************************************************
239 Reply to a read and X.
240 This code is basically stolen from reply_read_and_X with some
241 wrinkles to handle pipes.
242 ****************************************************************************/
244 struct pipe_read_andx_state
{
250 static void pipe_read_andx_done(struct tevent_req
*subreq
);
252 void reply_pipe_read_and_X(struct smb_request
*req
)
254 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
256 struct pipe_read_andx_state
*state
;
257 struct tevent_req
*subreq
;
259 /* we don't use the offset given to use for pipe reads. This
260 is deliberate, instead we always return the next lump of
263 uint32_t smb_offs
= IVAL(req
->vwv
+3, 0);
266 if (!fsp_is_np(fsp
)) {
267 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
271 if (fsp
->vuid
!= req
->vuid
) {
272 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
276 state
= talloc(req
, struct pipe_read_andx_state
);
278 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
281 req
->async_priv
= state
;
283 state
->smb_maxcnt
= SVAL(req
->vwv
+5, 0);
284 state
->smb_mincnt
= SVAL(req
->vwv
+6, 0);
286 reply_smb1_outbuf(req
, 12, state
->smb_maxcnt
+ 1 /* padding byte */);
287 SSVAL(req
->outbuf
, smb_vwv0
, 0xff); /* andx chain ends */
288 SSVAL(req
->outbuf
, smb_vwv1
, 0); /* no andx offset */
289 SCVAL(smb_buf(req
->outbuf
), 0, 0); /* padding byte */
291 data
= (uint8_t *)smb_buf(req
->outbuf
) + 1 /* padding byte */;
294 * We have to tell the upper layers that we're async.
296 state
->outbuf
= req
->outbuf
;
299 subreq
= np_read_send(state
, req
->sconn
->ev_ctx
,
300 fsp
->fake_file_handle
, data
,
302 if (subreq
== NULL
) {
303 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
306 tevent_req_set_callback(subreq
, pipe_read_andx_done
,
307 talloc_move(req
->conn
, &req
));
310 static void pipe_read_andx_done(struct tevent_req
*subreq
)
312 struct smb_request
*req
= tevent_req_callback_data(
313 subreq
, struct smb_request
);
314 struct pipe_read_andx_state
*state
= talloc_get_type_abort(
315 req
->async_priv
, struct pipe_read_andx_state
);
318 bool is_data_outstanding
;
320 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
322 if (!NT_STATUS_IS_OK(status
)) {
323 NTSTATUS old
= status
;
324 status
= nt_status_np_pipe(old
);
325 reply_nterror(req
, status
);
329 req
->outbuf
= state
->outbuf
;
330 state
->outbuf
= NULL
;
332 srv_smb1_set_message((char *)req
->outbuf
, 12, nread
+ 1 /* padding byte */,
337 * we should return STATUS_BUFFER_OVERFLOW if there's
340 * But we can't enable it yet, as it has bad interactions
341 * with fixup_chain_error_packet() in chain_reply().
343 if (is_data_outstanding
) {
344 error_packet_set((char *)req
->outbuf
, ERRDOS
, ERRmoredata
,
345 STATUS_BUFFER_OVERFLOW
, __LINE__
, __FILE__
);
349 SSVAL(req
->outbuf
,smb_vwv5
,nread
);
350 SSVAL(req
->outbuf
,smb_vwv6
,
351 (smb_wct
- 4) /* offset from smb header to wct */
352 + 1 /* the wct field */
353 + 12 * sizeof(uint16_t) /* vwv */
354 + 2 /* the buflen field */
355 + 1); /* padding byte */
357 DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
358 state
->smb_mincnt
, state
->smb_maxcnt
, (int)nread
));
362 * We must free here as the ownership of req was
363 * moved to the connection struct in reply_pipe_read_and_X().
365 smb_request_done(req
);
368 /****************************************************************************
369 Reply to a write on a pipe.
370 ****************************************************************************/
372 struct pipe_write_state
{
376 static void pipe_write_done(struct tevent_req
*subreq
);
378 void reply_pipe_write(struct smb_request
*req
)
380 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
382 struct pipe_write_state
*state
;
383 struct tevent_req
*subreq
;
385 if (!fsp_is_np(fsp
)) {
386 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
390 if (fsp
->vuid
!= req
->vuid
) {
391 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
395 state
= talloc(req
, struct pipe_write_state
);
397 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
400 req
->async_priv
= state
;
402 state
->numtowrite
= SVAL(req
->vwv
+1, 0);
406 DEBUG(6, ("reply_pipe_write: %s, name: %s len: %d\n", fsp_fnum_dbg(fsp
),
407 fsp_str_dbg(fsp
), (int)state
->numtowrite
));
409 subreq
= np_write_send(state
, req
->sconn
->ev_ctx
,
410 fsp
->fake_file_handle
, data
, state
->numtowrite
);
411 if (subreq
== NULL
) {
413 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
416 tevent_req_set_callback(subreq
, pipe_write_done
,
417 talloc_move(req
->conn
, &req
));
420 static void pipe_write_done(struct tevent_req
*subreq
)
422 struct smb_request
*req
= tevent_req_callback_data(
423 subreq
, struct smb_request
);
424 struct pipe_write_state
*state
= talloc_get_type_abort(
425 req
->async_priv
, struct pipe_write_state
);
427 ssize_t nwritten
= -1;
429 status
= np_write_recv(subreq
, &nwritten
);
432 reply_nterror(req
, status
);
436 /* Looks bogus to me now. Needs to be removed ? JRA. */
437 if ((nwritten
== 0 && state
->numtowrite
!= 0)) {
438 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
442 reply_smb1_outbuf(req
, 1, 0);
444 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
446 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten
));
449 if (!smb1_srv_send(req
->xconn
,
453 IS_CONN_ENCRYPTED(req
->conn
) || req
->encrypted
)) {
454 exit_server_cleanly("construct_reply: smb1_srv_send failed.");