s3: libsmb: In cli_qpathinfo_send() (SMBtrans2:TRANSACT2_QPATHINFO) check for DFS...
[Samba.git] / source3 / smbd / smb1_pipes.c
blob21ec2df5c381960709d337edad045314d766608e
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/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;
46 files_struct *fsp;
47 TALLOC_CTX *ctx = talloc_tos();
48 NTSTATUS status;
50 /* XXXX we need to handle passed times, sattr and flags */
51 srvstr_pull_req_talloc(ctx, req, &pipe_name, req->buf, STR_TERMINATE);
52 if (!pipe_name) {
53 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
54 ERRDOS, ERRbadpipe);
55 return;
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. */
62 #define PIPE "PIPE\\"
63 #define PIPELEN strlen(PIPE)
65 fname = pipe_name;
66 while (fname[0] == '\\') {
67 fname++;
69 if (!strnequal(fname, PIPE, PIPELEN)) {
70 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
71 return;
73 fname += PIPELEN;
74 while (fname[0] == '\\') {
75 fname++;
78 DEBUG(4,("Opening pipe %s => %s.\n", pipe_name, fname));
80 #if 0
82 * Hack for NT printers... JRA.
84 if(should_fail_next_srvsvc_open(fname)) {
85 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
86 return;
88 #endif
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,
94 ERRDOS, ERRbadpipe);
95 return;
97 reply_nterror(req, status);
98 return;
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;
128 size_t numtowrite;
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);
137 const uint8_t *data;
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);
143 return;
146 if (fsp->vuid != req->vuid) {
147 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
148 return;
151 state = talloc(req, struct pipe_write_andx_state);
152 if (state == NULL) {
153 reply_nterror(req, NT_STATUS_NO_MEMORY);
154 return;
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);
179 return;
182 data += 2;
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) {
189 TALLOC_FREE(state);
190 reply_nterror(req, NT_STATUS_NO_MEMORY);
191 return;
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);
203 NTSTATUS status;
204 ssize_t nwritten = -1;
206 status = np_write_recv(subreq, &nwritten);
207 TALLOC_FREE(subreq);
209 if (!NT_STATUS_IS_OK(status)) {
210 reply_nterror(req, status);
211 goto done;
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);
217 goto done;
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));
230 done:
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 {
245 uint8_t *outbuf;
246 int smb_mincnt;
247 int smb_maxcnt;
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));
255 uint8_t *data;
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
261 data on the pipe */
262 #if 0
263 uint32_t smb_offs = IVAL(req->vwv+3, 0);
264 #endif
266 if (!fsp_is_np(fsp)) {
267 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
268 return;
271 if (fsp->vuid != req->vuid) {
272 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
273 return;
276 state = talloc(req, struct pipe_read_andx_state);
277 if (state == NULL) {
278 reply_nterror(req, NT_STATUS_NO_MEMORY);
279 return;
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;
297 req->outbuf = NULL;
299 subreq = np_read_send(state, req->sconn->ev_ctx,
300 fsp->fake_file_handle, data,
301 state->smb_maxcnt);
302 if (subreq == NULL) {
303 reply_nterror(req, NT_STATUS_NO_MEMORY);
304 return;
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);
316 NTSTATUS status;
317 ssize_t nread;
318 bool is_data_outstanding;
320 status = np_read_recv(subreq, &nread, &is_data_outstanding);
321 TALLOC_FREE(subreq);
322 if (!NT_STATUS_IS_OK(status)) {
323 NTSTATUS old = status;
324 status = nt_status_np_pipe(old);
325 reply_nterror(req, status);
326 goto done;
329 req->outbuf = state->outbuf;
330 state->outbuf = NULL;
332 srv_smb1_set_message((char *)req->outbuf, 12, nread + 1 /* padding byte */,
333 false);
335 #if 0
337 * we should return STATUS_BUFFER_OVERFLOW if there's
338 * out standing data.
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__);
347 #endif
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));
360 done:
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 {
373 size_t numtowrite;
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));
381 const uint8_t *data;
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);
387 return;
390 if (fsp->vuid != req->vuid) {
391 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
392 return;
395 state = talloc(req, struct pipe_write_state);
396 if (state == NULL) {
397 reply_nterror(req, NT_STATUS_NO_MEMORY);
398 return;
400 req->async_priv = state;
402 state->numtowrite = SVAL(req->vwv+1, 0);
404 data = req->buf + 3;
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) {
412 TALLOC_FREE(state);
413 reply_nterror(req, NT_STATUS_NO_MEMORY);
414 return;
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);
426 NTSTATUS status;
427 ssize_t nwritten = -1;
429 status = np_write_recv(subreq, &nwritten);
430 TALLOC_FREE(subreq);
431 if (nwritten < 0) {
432 reply_nterror(req, status);
433 goto send;
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);
439 goto send;
442 reply_smb1_outbuf(req, 1, 0);
444 SSVAL(req->outbuf,smb_vwv0,nwritten);
446 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten));
448 send:
449 if (!smb1_srv_send(req->xconn, (char *)req->outbuf,
450 true, req->seqnum+1,
451 IS_CONN_ENCRYPTED(req->conn)||req->encrypted,
452 &req->pcd)) {
453 exit_server_cleanly("construct_reply: smb1_srv_send failed.");
455 TALLOC_FREE(req);