ctdb-scripts: Move connection tracking to 10.interface
[samba4-gss.git] / source3 / smbd / smb1_pipes.c
blobea2fae5ebc094c27e9dfd35250272bc515777dd1
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 status = open_np_file(req, fname, &fsp);
81 if (!NT_STATUS_IS_OK(status)) {
82 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
83 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
84 ERRDOS, ERRbadpipe);
85 return;
87 reply_nterror(req, status);
88 return;
91 /* Prepare the reply */
92 reply_smb1_outbuf(req, 15, 0);
94 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
95 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
97 /* Mark the opened file as an existing named pipe in message mode. */
98 SSVAL(req->outbuf,smb_vwv9,2);
99 SSVAL(req->outbuf,smb_vwv10,0xc700);
101 SSVAL(req->outbuf, smb_vwv2, fsp->fnum);
102 SSVAL(req->outbuf, smb_vwv3, 0); /* fmode */
103 srv_put_dos_date3((char *)req->outbuf, smb_vwv4, 0); /* mtime */
104 SIVAL(req->outbuf, smb_vwv6, 0); /* size */
105 SSVAL(req->outbuf, smb_vwv8, 0); /* rmode */
106 SSVAL(req->outbuf, smb_vwv11, 0x0001);
109 /****************************************************************************
110 Reply to a write and X.
112 This code is basically stolen from reply_write_and_X with some
113 wrinkles to handle pipes.
114 ****************************************************************************/
116 struct pipe_write_andx_state {
117 bool pipe_start_message_raw;
118 size_t numtowrite;
121 static void pipe_write_andx_done(struct tevent_req *subreq);
123 void reply_pipe_write_and_X(struct smb_request *req)
125 files_struct *fsp = file_fsp(req, SVAL(req->vwv+2, 0));
126 int smb_doff = SVAL(req->vwv+11, 0);
127 const uint8_t *data;
128 struct pipe_write_andx_state *state;
129 struct tevent_req *subreq;
131 if (!fsp_is_np(fsp)) {
132 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
133 return;
136 if (fsp->vuid != req->vuid) {
137 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
138 return;
141 state = talloc(req, struct pipe_write_andx_state);
142 if (state == NULL) {
143 reply_nterror(req, NT_STATUS_NO_MEMORY);
144 return;
146 req->async_priv = state;
148 state->numtowrite = SVAL(req->vwv+10, 0);
149 state->pipe_start_message_raw =
150 ((SVAL(req->vwv+7, 0) & (PIPE_START_MESSAGE|PIPE_RAW_MODE))
151 == (PIPE_START_MESSAGE|PIPE_RAW_MODE));
153 DEBUG(6, ("reply_pipe_write_and_X: %s, name: %s len: %d\n",
154 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp), (int)state->numtowrite));
156 data = (const uint8_t *)smb_base(req->inbuf) + smb_doff;
158 if (state->pipe_start_message_raw) {
160 * For the start of a message in named pipe byte mode,
161 * the first two bytes are a length-of-pdu field. Ignore
162 * them (we don't trust the client). JRA.
164 if (state->numtowrite < 2) {
165 DEBUG(0,("reply_pipe_write_and_X: start of message "
166 "set and not enough data sent.(%u)\n",
167 (unsigned int)state->numtowrite ));
168 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
169 return;
172 data += 2;
173 state->numtowrite -= 2;
176 subreq = np_write_send(state, req->sconn->ev_ctx,
177 fsp->fake_file_handle, data, state->numtowrite);
178 if (subreq == NULL) {
179 TALLOC_FREE(state);
180 reply_nterror(req, NT_STATUS_NO_MEMORY);
181 return;
183 tevent_req_set_callback(subreq, pipe_write_andx_done,
184 talloc_move(req->conn, &req));
187 static void pipe_write_andx_done(struct tevent_req *subreq)
189 struct smb_request *req = tevent_req_callback_data(
190 subreq, struct smb_request);
191 struct pipe_write_andx_state *state = talloc_get_type_abort(
192 req->async_priv, struct pipe_write_andx_state);
193 NTSTATUS status;
194 ssize_t nwritten = -1;
196 status = np_write_recv(subreq, &nwritten);
197 TALLOC_FREE(subreq);
199 if (!NT_STATUS_IS_OK(status)) {
200 reply_nterror(req, status);
201 goto done;
204 /* Looks bogus to me now. Is this error message correct ? JRA. */
205 if (nwritten != state->numtowrite) {
206 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
207 goto done;
210 reply_smb1_outbuf(req, 6, 0);
212 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
213 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
215 nwritten = (state->pipe_start_message_raw ? nwritten + 2 : nwritten);
216 SSVAL(req->outbuf,smb_vwv2,nwritten);
218 DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten));
220 done:
222 * We must free here as the ownership of req was
223 * moved to the connection struct in reply_pipe_write_and_X().
225 smb_request_done(req);
228 /****************************************************************************
229 Reply to a read and X.
230 This code is basically stolen from reply_read_and_X with some
231 wrinkles to handle pipes.
232 ****************************************************************************/
234 struct pipe_read_andx_state {
235 uint8_t *outbuf;
236 int smb_mincnt;
237 int smb_maxcnt;
240 static void pipe_read_andx_done(struct tevent_req *subreq);
242 void reply_pipe_read_and_X(struct smb_request *req)
244 files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
245 uint8_t *data;
246 struct pipe_read_andx_state *state;
247 struct tevent_req *subreq;
249 /* we don't use the offset given to use for pipe reads. This
250 is deliberate, instead we always return the next lump of
251 data on the pipe */
252 #if 0
253 uint32_t smb_offs = IVAL(req->vwv+3, 0);
254 #endif
256 if (!fsp_is_np(fsp)) {
257 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
258 return;
261 if (fsp->vuid != req->vuid) {
262 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
263 return;
266 state = talloc(req, struct pipe_read_andx_state);
267 if (state == NULL) {
268 reply_nterror(req, NT_STATUS_NO_MEMORY);
269 return;
271 req->async_priv = state;
273 state->smb_maxcnt = SVAL(req->vwv+5, 0);
274 state->smb_mincnt = SVAL(req->vwv+6, 0);
276 reply_smb1_outbuf(req, 12, state->smb_maxcnt + 1 /* padding byte */);
277 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
278 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
279 SCVAL(smb_buf(req->outbuf), 0, 0); /* padding byte */
281 data = (uint8_t *)smb_buf(req->outbuf) + 1 /* padding byte */;
284 * We have to tell the upper layers that we're async.
286 state->outbuf = req->outbuf;
287 req->outbuf = NULL;
289 subreq = np_read_send(state, req->sconn->ev_ctx,
290 fsp->fake_file_handle, data,
291 state->smb_maxcnt);
292 if (subreq == NULL) {
293 reply_nterror(req, NT_STATUS_NO_MEMORY);
294 return;
296 tevent_req_set_callback(subreq, pipe_read_andx_done,
297 talloc_move(req->conn, &req));
300 static void pipe_read_andx_done(struct tevent_req *subreq)
302 struct smb_request *req = tevent_req_callback_data(
303 subreq, struct smb_request);
304 struct pipe_read_andx_state *state = talloc_get_type_abort(
305 req->async_priv, struct pipe_read_andx_state);
306 NTSTATUS status;
307 ssize_t nread;
308 bool is_data_outstanding;
310 status = np_read_recv(subreq, &nread, &is_data_outstanding);
311 TALLOC_FREE(subreq);
312 if (!NT_STATUS_IS_OK(status)) {
313 NTSTATUS old = status;
314 status = nt_status_np_pipe(old);
315 reply_nterror(req, status);
316 goto done;
319 req->outbuf = state->outbuf;
320 state->outbuf = NULL;
322 srv_smb1_set_message((char *)req->outbuf, 12, nread + 1 /* padding byte */,
323 false);
325 #if 0
327 * we should return STATUS_BUFFER_OVERFLOW if there's
328 * out standing data.
330 * But we can't enable it yet, as it has bad interactions
331 * with fixup_chain_error_packet() in chain_reply().
333 if (is_data_outstanding) {
334 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
335 STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
337 #endif
339 SSVAL(req->outbuf,smb_vwv5,nread);
340 SSVAL(req->outbuf,smb_vwv6,
341 (smb_wct - 4) /* offset from smb header to wct */
342 + 1 /* the wct field */
343 + 12 * sizeof(uint16_t) /* vwv */
344 + 2 /* the buflen field */
345 + 1); /* padding byte */
347 DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
348 state->smb_mincnt, state->smb_maxcnt, (int)nread));
350 done:
352 * We must free here as the ownership of req was
353 * moved to the connection struct in reply_pipe_read_and_X().
355 smb_request_done(req);
358 /****************************************************************************
359 Reply to a write on a pipe.
360 ****************************************************************************/
362 struct pipe_write_state {
363 size_t numtowrite;
366 static void pipe_write_done(struct tevent_req *subreq);
368 void reply_pipe_write(struct smb_request *req)
370 files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
371 const uint8_t *data;
372 struct pipe_write_state *state;
373 struct tevent_req *subreq;
375 if (!fsp_is_np(fsp)) {
376 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
377 return;
380 if (fsp->vuid != req->vuid) {
381 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
382 return;
385 state = talloc(req, struct pipe_write_state);
386 if (state == NULL) {
387 reply_nterror(req, NT_STATUS_NO_MEMORY);
388 return;
390 req->async_priv = state;
392 state->numtowrite = SVAL(req->vwv+1, 0);
394 data = req->buf + 3;
396 DEBUG(6, ("reply_pipe_write: %s, name: %s len: %d\n", fsp_fnum_dbg(fsp),
397 fsp_str_dbg(fsp), (int)state->numtowrite));
399 subreq = np_write_send(state, req->sconn->ev_ctx,
400 fsp->fake_file_handle, data, state->numtowrite);
401 if (subreq == NULL) {
402 TALLOC_FREE(state);
403 reply_nterror(req, NT_STATUS_NO_MEMORY);
404 return;
406 tevent_req_set_callback(subreq, pipe_write_done,
407 talloc_move(req->conn, &req));
410 static void pipe_write_done(struct tevent_req *subreq)
412 struct smb_request *req = tevent_req_callback_data(
413 subreq, struct smb_request);
414 struct pipe_write_state *state = talloc_get_type_abort(
415 req->async_priv, struct pipe_write_state);
416 NTSTATUS status;
417 ssize_t nwritten = -1;
419 status = np_write_recv(subreq, &nwritten);
420 TALLOC_FREE(subreq);
421 if (nwritten < 0) {
422 reply_nterror(req, status);
423 goto send;
426 /* Looks bogus to me now. Needs to be removed ? JRA. */
427 if ((nwritten == 0 && state->numtowrite != 0)) {
428 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
429 goto send;
432 reply_smb1_outbuf(req, 1, 0);
434 SSVAL(req->outbuf,smb_vwv0,nwritten);
436 DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten));
438 send:
439 if (!smb1_srv_send(req->xconn,
440 (char *)req->outbuf,
441 true,
442 req->seqnum + 1,
443 IS_CONN_ENCRYPTED(req->conn) || req->encrypted)) {
444 exit_server_cleanly("construct_reply: smb1_srv_send failed.");
446 TALLOC_FREE(req);