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 NTSTATUS
open_np_file(struct smb_request
*smb_req
, const char *name
,
36 struct files_struct
**pfsp
)
38 struct connection_struct
*conn
= smb_req
->conn
;
39 struct files_struct
*fsp
;
42 status
= file_new(smb_req
, conn
, &fsp
);
43 if (!NT_STATUS_IS_OK(status
)) {
44 DEBUG(0, ("file_new failed: %s\n", nt_errstr(status
)));
50 fsp
->vuid
= smb_req
->vuid
;
51 fsp
->can_lock
= false;
52 fsp
->access_mask
= FILE_READ_DATA
| FILE_WRITE_DATA
;
53 string_set(&fsp
->fsp_name
, name
);
55 status
= np_open(NULL
, name
, conn
->client_address
,
56 conn
->server_info
, &fsp
->fake_file_handle
);
57 if (!NT_STATUS_IS_OK(status
)) {
58 DEBUG(10, ("np_open(%s) returned %s\n", name
,
60 file_free(smb_req
, fsp
);
69 /****************************************************************************
70 Reply to an open and X on a named pipe.
71 This code is basically stolen from reply_open_and_X with some
72 wrinkles to handle pipes.
73 ****************************************************************************/
75 void reply_open_pipe_and_X(connection_struct
*conn
, struct smb_request
*req
)
77 const char *fname
= NULL
;
78 char *pipe_name
= NULL
;
80 TALLOC_CTX
*ctx
= talloc_tos();
83 /* XXXX we need to handle passed times, sattr and flags */
84 srvstr_pull_req_talloc(ctx
, req
, &pipe_name
, req
->buf
, STR_TERMINATE
);
86 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
91 /* If the name doesn't start \PIPE\ then this is directed */
92 /* at a mailslot or something we really, really don't understand, */
93 /* not just something we really don't understand. */
94 if ( strncmp(pipe_name
,PIPE
,PIPELEN
) != 0 ) {
95 reply_doserror(req
, ERRSRV
, ERRaccess
);
99 DEBUG(4,("Opening pipe %s.\n", pipe_name
));
101 /* Strip \PIPE\ off the name. */
102 fname
= pipe_name
+ PIPELEN
;
106 * Hack for NT printers... JRA.
108 if(should_fail_next_srvsvc_open(fname
)) {
109 reply_doserror(req
, ERRSRV
, ERRaccess
);
114 status
= open_np_file(req
, fname
, &fsp
);
115 if (!NT_STATUS_IS_OK(status
)) {
116 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
117 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
121 reply_nterror(req
, status
);
125 /* Prepare the reply */
126 reply_outbuf(req
, 15, 0);
128 /* Mark the opened file as an existing named pipe in message mode. */
129 SSVAL(req
->outbuf
,smb_vwv9
,2);
130 SSVAL(req
->outbuf
,smb_vwv10
,0xc700);
132 SSVAL(req
->outbuf
, smb_vwv2
, fsp
->fnum
);
133 SSVAL(req
->outbuf
, smb_vwv3
, 0); /* fmode */
134 srv_put_dos_date3((char *)req
->outbuf
, smb_vwv4
, 0); /* mtime */
135 SIVAL(req
->outbuf
, smb_vwv6
, 0); /* size */
136 SSVAL(req
->outbuf
, smb_vwv8
, 0); /* rmode */
137 SSVAL(req
->outbuf
, smb_vwv11
, 0x0001);
143 /****************************************************************************
144 Reply to a write on a pipe.
145 ****************************************************************************/
147 struct pipe_write_state
{
151 static void pipe_write_done(struct tevent_req
*subreq
);
153 void reply_pipe_write(struct smb_request
*req
)
155 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
157 struct pipe_write_state
*state
;
158 struct tevent_req
*subreq
;
160 if (!fsp_is_np(fsp
)) {
161 reply_doserror(req
, ERRDOS
, ERRbadfid
);
165 if (fsp
->vuid
!= req
->vuid
) {
166 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
170 state
= talloc(req
, struct pipe_write_state
);
172 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
175 req
->async_priv
= state
;
177 state
->numtowrite
= SVAL(req
->vwv
+1, 0);
181 DEBUG(6, ("reply_pipe_write: %x name: %s len: %d\n", (int)fsp
->fnum
,
182 fsp
->fsp_name
, (int)state
->numtowrite
));
184 subreq
= np_write_send(state
, smbd_event_context(),
185 fsp
->fake_file_handle
, data
, state
->numtowrite
);
186 if (subreq
== NULL
) {
188 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
191 tevent_req_set_callback(subreq
, pipe_write_done
,
192 talloc_move(req
->conn
, &req
));
195 static void pipe_write_done(struct tevent_req
*subreq
)
197 struct smb_request
*req
= tevent_req_callback_data(
198 subreq
, struct smb_request
);
199 struct pipe_write_state
*state
= talloc_get_type_abort(
200 req
->async_priv
, struct pipe_write_state
);
202 ssize_t nwritten
= -1;
204 status
= np_write_recv(subreq
, &nwritten
);
206 if ((nwritten
== 0 && state
->numtowrite
!= 0) || (nwritten
< 0)) {
207 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
211 reply_outbuf(req
, 1, 0);
213 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
215 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten
));
218 if (!srv_send_smb(smbd_server_fd(), (char *)req
->outbuf
,
220 IS_CONN_ENCRYPTED(req
->conn
)||req
->encrypted
,
222 exit_server_cleanly("construct_reply: srv_send_smb failed.");
227 /****************************************************************************
228 Reply to a write and X.
230 This code is basically stolen from reply_write_and_X with some
231 wrinkles to handle pipes.
232 ****************************************************************************/
234 struct pipe_write_andx_state
{
235 bool pipe_start_message_raw
;
239 static void pipe_write_andx_done(struct tevent_req
*subreq
);
241 void reply_pipe_write_and_X(struct smb_request
*req
)
243 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+2, 0));
244 int smb_doff
= SVAL(req
->vwv
+11, 0);
246 struct pipe_write_andx_state
*state
;
247 struct tevent_req
*subreq
;
249 if (!fsp_is_np(fsp
)) {
250 reply_doserror(req
, ERRDOS
, ERRbadfid
);
254 if (fsp
->vuid
!= req
->vuid
) {
255 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
259 state
= talloc(req
, struct pipe_write_andx_state
);
261 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
264 req
->async_priv
= state
;
266 state
->numtowrite
= SVAL(req
->vwv
+10, 0);
267 state
->pipe_start_message_raw
=
268 ((SVAL(req
->vwv
+7, 0) & (PIPE_START_MESSAGE
|PIPE_RAW_MODE
))
269 == (PIPE_START_MESSAGE
|PIPE_RAW_MODE
));
271 DEBUG(6, ("reply_pipe_write_and_X: %x name: %s len: %d\n",
272 (int)fsp
->fnum
, fsp
->fsp_name
, (int)state
->numtowrite
));
274 data
= (uint8_t *)smb_base(req
->inbuf
) + smb_doff
;
276 if (state
->pipe_start_message_raw
) {
278 * For the start of a message in named pipe byte mode,
279 * the first two bytes are a length-of-pdu field. Ignore
280 * them (we don't trust the client). JRA.
282 if (state
->numtowrite
< 2) {
283 DEBUG(0,("reply_pipe_write_and_X: start of message "
284 "set and not enough data sent.(%u)\n",
285 (unsigned int)state
->numtowrite
));
286 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
291 state
->numtowrite
-= 2;
294 subreq
= np_write_send(state
, smbd_event_context(),
295 fsp
->fake_file_handle
, data
, state
->numtowrite
);
296 if (subreq
== NULL
) {
298 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
301 tevent_req_set_callback(subreq
, pipe_write_andx_done
,
302 talloc_move(req
->conn
, &req
));
305 static void pipe_write_andx_done(struct tevent_req
*subreq
)
307 struct smb_request
*req
= tevent_req_callback_data(
308 subreq
, struct smb_request
);
309 struct pipe_write_andx_state
*state
= talloc_get_type_abort(
310 req
->async_priv
, struct pipe_write_andx_state
);
312 ssize_t nwritten
= -1;
314 status
= np_write_recv(subreq
, &nwritten
);
316 if (!NT_STATUS_IS_OK(status
) || (nwritten
!= state
->numtowrite
)) {
317 reply_unixerror(req
, ERRDOS
,ERRnoaccess
);
321 reply_outbuf(req
, 6, 0);
323 nwritten
= (state
->pipe_start_message_raw
? nwritten
+ 2 : nwritten
);
324 SSVAL(req
->outbuf
,smb_vwv2
,nwritten
);
326 DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten
));
332 /****************************************************************************
333 Reply to a read and X.
334 This code is basically stolen from reply_read_and_X with some
335 wrinkles to handle pipes.
336 ****************************************************************************/
338 struct pipe_read_andx_state
{
344 static void pipe_read_andx_done(struct tevent_req
*subreq
);
346 void reply_pipe_read_and_X(struct smb_request
*req
)
348 files_struct
*fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
350 struct pipe_read_andx_state
*state
;
351 struct tevent_req
*subreq
;
353 /* we don't use the offset given to use for pipe reads. This
354 is deliberate, instead we always return the next lump of
357 uint32 smb_offs
= IVAL(req
->vwv
+3, 0);
360 if (!fsp_is_np(fsp
)) {
361 reply_doserror(req
, ERRDOS
, ERRbadfid
);
365 if (fsp
->vuid
!= req
->vuid
) {
366 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
370 state
= talloc(req
, struct pipe_read_andx_state
);
372 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
375 req
->async_priv
= state
;
377 state
->smb_maxcnt
= SVAL(req
->vwv
+5, 0);
378 state
->smb_mincnt
= SVAL(req
->vwv
+6, 0);
380 reply_outbuf(req
, 12, state
->smb_maxcnt
);
381 data
= (uint8_t *)smb_buf(req
->outbuf
);
384 * We have to tell the upper layers that we're async.
386 state
->outbuf
= req
->outbuf
;
389 subreq
= np_read_send(state
, smbd_event_context(),
390 fsp
->fake_file_handle
, data
,
392 if (subreq
== NULL
) {
393 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
396 tevent_req_set_callback(subreq
, pipe_read_andx_done
,
397 talloc_move(req
->conn
, &req
));
400 static void pipe_read_andx_done(struct tevent_req
*subreq
)
402 struct smb_request
*req
= tevent_req_callback_data(
403 subreq
, struct smb_request
);
404 struct pipe_read_andx_state
*state
= talloc_get_type_abort(
405 req
->async_priv
, struct pipe_read_andx_state
);
408 bool is_data_outstanding
;
410 status
= np_read_recv(subreq
, &nread
, &is_data_outstanding
);
412 if (!NT_STATUS_IS_OK(status
)) {
413 reply_nterror(req
, status
);
417 req
->outbuf
= state
->outbuf
;
418 state
->outbuf
= NULL
;
420 srv_set_message((char *)req
->outbuf
, 12, nread
, False
);
422 SSVAL(req
->outbuf
,smb_vwv5
,nread
);
423 SSVAL(req
->outbuf
,smb_vwv6
,
425 + 1 /* the wct field */
426 + 12 * sizeof(uint16_t) /* vwv */
427 + 2); /* the buflen field */
428 SSVAL(req
->outbuf
,smb_vwv11
,state
->smb_maxcnt
);
430 DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
431 state
->smb_mincnt
, state
->smb_maxcnt
, (int)nread
));