s3: Lift the server_messaging_context from notify_job_status
[Samba/gbeck.git] / source3 / smbd / pipes.c
blobd321c7a73de26452444c60e57ef439f788a994c8
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"
31 #define PIPE "\\PIPE\\"
32 #define PIPELEN strlen(PIPE)
34 #define MAX_PIPE_NAME_LEN 24
36 NTSTATUS open_np_file(struct smb_request *smb_req, const char *name,
37 struct files_struct **pfsp)
39 struct connection_struct *conn = smb_req->conn;
40 struct files_struct *fsp;
41 struct smb_filename *smb_fname = NULL;
42 NTSTATUS status;
44 status = file_new(smb_req, conn, &fsp);
45 if (!NT_STATUS_IS_OK(status)) {
46 DEBUG(0, ("file_new failed: %s\n", nt_errstr(status)));
47 return status;
50 fsp->conn = conn;
51 fsp->fh->fd = -1;
52 fsp->vuid = smb_req->vuid;
53 fsp->can_lock = false;
54 fsp->access_mask = FILE_READ_DATA | FILE_WRITE_DATA;
56 status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
57 &smb_fname);
58 if (!NT_STATUS_IS_OK(status)) {
59 file_free(smb_req, fsp);
60 return status;
62 status = fsp_set_smb_fname(fsp, smb_fname);
63 TALLOC_FREE(smb_fname);
64 if (!NT_STATUS_IS_OK(status)) {
65 file_free(smb_req, fsp);
66 return status;
69 status = np_open(fsp, name,
70 conn->sconn->local_address,
71 conn->sconn->remote_address,
72 conn->server_info,
73 conn->sconn->msg_ctx,
74 &fsp->fake_file_handle);
75 if (!NT_STATUS_IS_OK(status)) {
76 DEBUG(10, ("np_open(%s) returned %s\n", name,
77 nt_errstr(status)));
78 file_free(smb_req, fsp);
79 return status;
82 *pfsp = fsp;
84 return NT_STATUS_OK;
87 /****************************************************************************
88 Reply to an open and X on a named pipe.
89 This code is basically stolen from reply_open_and_X with some
90 wrinkles to handle pipes.
91 ****************************************************************************/
93 void reply_open_pipe_and_X(connection_struct *conn, struct smb_request *req)
95 const char *fname = NULL;
96 char *pipe_name = NULL;
97 files_struct *fsp;
98 TALLOC_CTX *ctx = talloc_tos();
99 NTSTATUS status;
101 /* XXXX we need to handle passed times, sattr and flags */
102 srvstr_pull_req_talloc(ctx, req, &pipe_name, req->buf, STR_TERMINATE);
103 if (!pipe_name) {
104 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
105 ERRDOS, ERRbadpipe);
106 return;
109 /* If the name doesn't start \PIPE\ then this is directed */
110 /* at a mailslot or something we really, really don't understand, */
111 /* not just something we really don't understand. */
112 if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
113 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
114 return;
117 DEBUG(4,("Opening pipe %s.\n", pipe_name));
119 /* Strip \PIPE\ off the name. */
120 fname = pipe_name + PIPELEN;
122 #if 0
124 * Hack for NT printers... JRA.
126 if(should_fail_next_srvsvc_open(fname)) {
127 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
128 return;
130 #endif
132 status = open_np_file(req, fname, &fsp);
133 if (!NT_STATUS_IS_OK(status)) {
134 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
135 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
136 ERRDOS, ERRbadpipe);
137 return;
139 reply_nterror(req, status);
140 return;
143 /* Prepare the reply */
144 reply_outbuf(req, 15, 0);
146 /* Mark the opened file as an existing named pipe in message mode. */
147 SSVAL(req->outbuf,smb_vwv9,2);
148 SSVAL(req->outbuf,smb_vwv10,0xc700);
150 SSVAL(req->outbuf, smb_vwv2, fsp->fnum);
151 SSVAL(req->outbuf, smb_vwv3, 0); /* fmode */
152 srv_put_dos_date3((char *)req->outbuf, smb_vwv4, 0); /* mtime */
153 SIVAL(req->outbuf, smb_vwv6, 0); /* size */
154 SSVAL(req->outbuf, smb_vwv8, 0); /* rmode */
155 SSVAL(req->outbuf, smb_vwv11, 0x0001);
157 chain_reply(req);
158 return;
161 /****************************************************************************
162 Reply to a write on a pipe.
163 ****************************************************************************/
165 struct pipe_write_state {
166 size_t numtowrite;
169 static void pipe_write_done(struct tevent_req *subreq);
171 void reply_pipe_write(struct smb_request *req)
173 files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
174 const uint8_t *data;
175 struct pipe_write_state *state;
176 struct tevent_req *subreq;
178 if (!fsp_is_np(fsp)) {
179 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
180 return;
183 if (fsp->vuid != req->vuid) {
184 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
185 return;
188 state = talloc(req, struct pipe_write_state);
189 if (state == NULL) {
190 reply_nterror(req, NT_STATUS_NO_MEMORY);
191 return;
193 req->async_priv = state;
195 state->numtowrite = SVAL(req->vwv+1, 0);
197 data = req->buf + 3;
199 DEBUG(6, ("reply_pipe_write: %x name: %s len: %d\n", (int)fsp->fnum,
200 fsp_str_dbg(fsp), (int)state->numtowrite));
202 subreq = np_write_send(state, smbd_event_context(),
203 fsp->fake_file_handle, data, state->numtowrite);
204 if (subreq == NULL) {
205 TALLOC_FREE(state);
206 reply_nterror(req, NT_STATUS_NO_MEMORY);
207 return;
209 tevent_req_set_callback(subreq, pipe_write_done,
210 talloc_move(req->conn, &req));
213 static void pipe_write_done(struct tevent_req *subreq)
215 struct smb_request *req = tevent_req_callback_data(
216 subreq, struct smb_request);
217 struct pipe_write_state *state = talloc_get_type_abort(
218 req->async_priv, struct pipe_write_state);
219 NTSTATUS status;
220 ssize_t nwritten = -1;
222 status = np_write_recv(subreq, &nwritten);
223 TALLOC_FREE(subreq);
224 if (nwritten < 0) {
225 reply_nterror(req, status);
226 goto send;
229 /* Looks bogus to me now. Needs to be removed ? JRA. */
230 if ((nwritten == 0 && state->numtowrite != 0)) {
231 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
232 goto send;
235 reply_outbuf(req, 1, 0);
237 SSVAL(req->outbuf,smb_vwv0,nwritten);
239 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten));
241 send:
242 if (!srv_send_smb(smbd_server_fd(), (char *)req->outbuf,
243 true, req->seqnum+1,
244 IS_CONN_ENCRYPTED(req->conn)||req->encrypted,
245 &req->pcd)) {
246 exit_server_cleanly("construct_reply: srv_send_smb failed.");
248 TALLOC_FREE(req);
251 /****************************************************************************
252 Reply to a write and X.
254 This code is basically stolen from reply_write_and_X with some
255 wrinkles to handle pipes.
256 ****************************************************************************/
258 struct pipe_write_andx_state {
259 bool pipe_start_message_raw;
260 size_t numtowrite;
263 static void pipe_write_andx_done(struct tevent_req *subreq);
265 void reply_pipe_write_and_X(struct smb_request *req)
267 files_struct *fsp = file_fsp(req, SVAL(req->vwv+2, 0));
268 int smb_doff = SVAL(req->vwv+11, 0);
269 uint8_t *data;
270 struct pipe_write_andx_state *state;
271 struct tevent_req *subreq;
273 if (!fsp_is_np(fsp)) {
274 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
275 return;
278 if (fsp->vuid != req->vuid) {
279 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
280 return;
283 state = talloc(req, struct pipe_write_andx_state);
284 if (state == NULL) {
285 reply_nterror(req, NT_STATUS_NO_MEMORY);
286 return;
288 req->async_priv = state;
290 state->numtowrite = SVAL(req->vwv+10, 0);
291 state->pipe_start_message_raw =
292 ((SVAL(req->vwv+7, 0) & (PIPE_START_MESSAGE|PIPE_RAW_MODE))
293 == (PIPE_START_MESSAGE|PIPE_RAW_MODE));
295 DEBUG(6, ("reply_pipe_write_and_X: %x name: %s len: %d\n",
296 (int)fsp->fnum, fsp_str_dbg(fsp), (int)state->numtowrite));
298 data = (uint8_t *)smb_base(req->inbuf) + smb_doff;
300 if (state->pipe_start_message_raw) {
302 * For the start of a message in named pipe byte mode,
303 * the first two bytes are a length-of-pdu field. Ignore
304 * them (we don't trust the client). JRA.
306 if (state->numtowrite < 2) {
307 DEBUG(0,("reply_pipe_write_and_X: start of message "
308 "set and not enough data sent.(%u)\n",
309 (unsigned int)state->numtowrite ));
310 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
311 return;
314 data += 2;
315 state->numtowrite -= 2;
318 subreq = np_write_send(state, smbd_event_context(),
319 fsp->fake_file_handle, data, state->numtowrite);
320 if (subreq == NULL) {
321 TALLOC_FREE(state);
322 reply_nterror(req, NT_STATUS_NO_MEMORY);
323 return;
325 tevent_req_set_callback(subreq, pipe_write_andx_done,
326 talloc_move(req->conn, &req));
329 static void pipe_write_andx_done(struct tevent_req *subreq)
331 struct smb_request *req = tevent_req_callback_data(
332 subreq, struct smb_request);
333 struct pipe_write_andx_state *state = talloc_get_type_abort(
334 req->async_priv, struct pipe_write_andx_state);
335 NTSTATUS status;
336 ssize_t nwritten = -1;
338 status = np_write_recv(subreq, &nwritten);
339 TALLOC_FREE(subreq);
341 if (!NT_STATUS_IS_OK(status)) {
342 reply_nterror(req, status);
343 goto done;
346 /* Looks bogus to me now. Is this error message correct ? JRA. */
347 if (nwritten != state->numtowrite) {
348 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
349 goto done;
352 reply_outbuf(req, 6, 0);
354 nwritten = (state->pipe_start_message_raw ? nwritten + 2 : nwritten);
355 SSVAL(req->outbuf,smb_vwv2,nwritten);
357 DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten));
359 done:
360 chain_reply(req);
362 * We must free here as the ownership of req was
363 * moved to the connection struct in reply_pipe_write_and_X().
365 TALLOC_FREE(req);
368 /****************************************************************************
369 Reply to a read and X.
370 This code is basically stolen from reply_read_and_X with some
371 wrinkles to handle pipes.
372 ****************************************************************************/
374 struct pipe_read_andx_state {
375 uint8_t *outbuf;
376 int smb_mincnt;
377 int smb_maxcnt;
380 static void pipe_read_andx_done(struct tevent_req *subreq);
382 void reply_pipe_read_and_X(struct smb_request *req)
384 files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
385 uint8_t *data;
386 struct pipe_read_andx_state *state;
387 struct tevent_req *subreq;
389 /* we don't use the offset given to use for pipe reads. This
390 is deliberate, instead we always return the next lump of
391 data on the pipe */
392 #if 0
393 uint32 smb_offs = IVAL(req->vwv+3, 0);
394 #endif
396 if (!fsp_is_np(fsp)) {
397 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
398 return;
401 if (fsp->vuid != req->vuid) {
402 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
403 return;
406 state = talloc(req, struct pipe_read_andx_state);
407 if (state == NULL) {
408 reply_nterror(req, NT_STATUS_NO_MEMORY);
409 return;
411 req->async_priv = state;
413 state->smb_maxcnt = SVAL(req->vwv+5, 0);
414 state->smb_mincnt = SVAL(req->vwv+6, 0);
416 reply_outbuf(req, 12, state->smb_maxcnt);
417 data = (uint8_t *)smb_buf(req->outbuf);
420 * We have to tell the upper layers that we're async.
422 state->outbuf = req->outbuf;
423 req->outbuf = NULL;
425 subreq = np_read_send(state, smbd_event_context(),
426 fsp->fake_file_handle, data,
427 state->smb_maxcnt);
428 if (subreq == NULL) {
429 reply_nterror(req, NT_STATUS_NO_MEMORY);
430 return;
432 tevent_req_set_callback(subreq, pipe_read_andx_done,
433 talloc_move(req->conn, &req));
436 static void pipe_read_andx_done(struct tevent_req *subreq)
438 struct smb_request *req = tevent_req_callback_data(
439 subreq, struct smb_request);
440 struct pipe_read_andx_state *state = talloc_get_type_abort(
441 req->async_priv, struct pipe_read_andx_state);
442 NTSTATUS status;
443 ssize_t nread;
444 bool is_data_outstanding;
446 status = np_read_recv(subreq, &nread, &is_data_outstanding);
447 TALLOC_FREE(subreq);
448 if (!NT_STATUS_IS_OK(status)) {
449 reply_nterror(req, status);
450 goto done;
453 req->outbuf = state->outbuf;
454 state->outbuf = NULL;
456 srv_set_message((char *)req->outbuf, 12, nread, False);
458 #if 0
460 * we should return STATUS_BUFFER_OVERFLOW if there's
461 * out standing data.
463 * But we can't enable it yet, as it has bad interactions
464 * with fixup_chain_error_packet() in chain_reply().
466 if (is_data_outstanding) {
467 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
468 STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
470 #endif
472 SSVAL(req->outbuf,smb_vwv5,nread);
473 SSVAL(req->outbuf,smb_vwv6,
474 req_wct_ofs(req)
475 + 1 /* the wct field */
476 + 12 * sizeof(uint16_t) /* vwv */
477 + 2); /* the buflen field */
478 SSVAL(req->outbuf,smb_vwv11,state->smb_maxcnt);
480 DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
481 state->smb_mincnt, state->smb_maxcnt, (int)nread));
483 done:
484 chain_reply(req);
486 * We must free here as the ownership of req was
487 * moved to the connection struct in reply_pipe_read_and_X().
489 TALLOC_FREE(req);