s3: Fix Coverity ID 2195: NO_EFFECT
[Samba.git] / source3 / smbd / pipes.c
blob73398abcd8193c5feef040929dec211f426d91bf
1 /*
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
28 #include "includes.h"
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;
43 NTSTATUS status;
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)));
48 return status;
51 fsp->conn = conn;
52 fsp->fh->fd = -1;
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,
58 &smb_fname);
59 if (!NT_STATUS_IS_OK(status)) {
60 file_free(smb_req, fsp);
61 return status;
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);
67 return status;
70 status = np_open(fsp, name,
71 conn->sconn->local_address,
72 conn->sconn->remote_address,
73 &conn->sconn->client_id,
74 conn->session_info,
75 conn->sconn->msg_ctx,
76 &fsp->fake_file_handle);
77 if (!NT_STATUS_IS_OK(status)) {
78 DEBUG(10, ("np_open(%s) returned %s\n", name,
79 nt_errstr(status)));
80 file_free(smb_req, fsp);
81 return status;
84 *pfsp = fsp;
86 return NT_STATUS_OK;
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;
99 files_struct *fsp;
100 TALLOC_CTX *ctx = talloc_tos();
101 NTSTATUS status;
103 /* XXXX we need to handle passed times, sattr and flags */
104 srvstr_pull_req_talloc(ctx, req, &pipe_name, req->buf, STR_TERMINATE);
105 if (!pipe_name) {
106 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
107 ERRDOS, ERRbadpipe);
108 return;
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);
116 return;
119 DEBUG(4,("Opening pipe %s.\n", pipe_name));
121 /* Strip \PIPE\ off the name. */
122 fname = pipe_name + PIPELEN;
124 #if 0
126 * Hack for NT printers... JRA.
128 if(should_fail_next_srvsvc_open(fname)) {
129 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
130 return;
132 #endif
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,
138 ERRDOS, ERRbadpipe);
139 return;
141 reply_nterror(req, status);
142 return;
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);
159 chain_reply(req);
160 return;
163 /****************************************************************************
164 Reply to a write on a pipe.
165 ****************************************************************************/
167 struct pipe_write_state {
168 size_t numtowrite;
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));
176 const uint8_t *data;
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);
182 return;
185 if (fsp->vuid != req->vuid) {
186 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
187 return;
190 state = talloc(req, struct pipe_write_state);
191 if (state == NULL) {
192 reply_nterror(req, NT_STATUS_NO_MEMORY);
193 return;
195 req->async_priv = state;
197 state->numtowrite = SVAL(req->vwv+1, 0);
199 data = req->buf + 3;
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) {
207 TALLOC_FREE(state);
208 reply_nterror(req, NT_STATUS_NO_MEMORY);
209 return;
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);
221 NTSTATUS status;
222 ssize_t nwritten = -1;
224 status = np_write_recv(subreq, &nwritten);
225 TALLOC_FREE(subreq);
226 if (nwritten < 0) {
227 reply_nterror(req, status);
228 goto send;
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);
234 goto send;
237 reply_outbuf(req, 1, 0);
239 SSVAL(req->outbuf,smb_vwv0,nwritten);
241 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten));
243 send:
244 if (!srv_send_smb(req->sconn, (char *)req->outbuf,
245 true, req->seqnum+1,
246 IS_CONN_ENCRYPTED(req->conn)||req->encrypted,
247 &req->pcd)) {
248 exit_server_cleanly("construct_reply: srv_send_smb failed.");
250 TALLOC_FREE(req);
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;
262 size_t numtowrite;
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);
271 uint8_t *data;
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);
277 return;
280 if (fsp->vuid != req->vuid) {
281 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
282 return;
285 state = talloc(req, struct pipe_write_andx_state);
286 if (state == NULL) {
287 reply_nterror(req, NT_STATUS_NO_MEMORY);
288 return;
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);
313 return;
316 data += 2;
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) {
323 TALLOC_FREE(state);
324 reply_nterror(req, NT_STATUS_NO_MEMORY);
325 return;
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);
337 NTSTATUS status;
338 ssize_t nwritten = -1;
340 status = np_write_recv(subreq, &nwritten);
341 TALLOC_FREE(subreq);
343 if (!NT_STATUS_IS_OK(status)) {
344 reply_nterror(req, status);
345 goto done;
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);
351 goto done;
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));
361 done:
362 chain_reply(req);
364 * We must free here as the ownership of req was
365 * moved to the connection struct in reply_pipe_write_and_X().
367 TALLOC_FREE(req);
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 {
377 uint8_t *outbuf;
378 int smb_mincnt;
379 int smb_maxcnt;
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));
387 uint8_t *data;
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
393 data on the pipe */
394 #if 0
395 uint32 smb_offs = IVAL(req->vwv+3, 0);
396 #endif
398 if (!fsp_is_np(fsp)) {
399 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
400 return;
403 if (fsp->vuid != req->vuid) {
404 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
405 return;
408 state = talloc(req, struct pipe_read_andx_state);
409 if (state == NULL) {
410 reply_nterror(req, NT_STATUS_NO_MEMORY);
411 return;
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;
425 req->outbuf = NULL;
427 subreq = np_read_send(state, smbd_event_context(),
428 fsp->fake_file_handle, data,
429 state->smb_maxcnt);
430 if (subreq == NULL) {
431 reply_nterror(req, NT_STATUS_NO_MEMORY);
432 return;
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);
444 NTSTATUS status;
445 ssize_t nread;
446 bool is_data_outstanding;
448 status = np_read_recv(subreq, &nread, &is_data_outstanding);
449 TALLOC_FREE(subreq);
450 if (!NT_STATUS_IS_OK(status)) {
451 reply_nterror(req, status);
452 goto done;
455 req->outbuf = state->outbuf;
456 state->outbuf = NULL;
458 srv_set_message((char *)req->outbuf, 12, nread, False);
460 #if 0
462 * we should return STATUS_BUFFER_OVERFLOW if there's
463 * out standing data.
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__);
472 #endif
474 SSVAL(req->outbuf,smb_vwv5,nread);
475 SSVAL(req->outbuf,smb_vwv6,
476 req_wct_ofs(req)
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));
485 done:
486 chain_reply(req);
488 * We must free here as the ownership of req was
489 * moved to the connection struct in reply_pipe_read_and_X().
491 TALLOC_FREE(req);