d567537ed6144b0dfc19c099883c174a404b7936
[kai/samba.git] / source3 / smbd / pipes.c
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.
8
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.
13
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.
18
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/>.
21 */
22 /*
23    This file handles reply_ calls on named pipes that the server
24    makes to handle specific protocols
25 */
26
27
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
34 #define PIPE            "\\PIPE\\"
35 #define PIPELEN         strlen(PIPE)
36
37 #define MAX_PIPE_NAME_LEN       24
38
39 NTSTATUS open_np_file(struct smb_request *smb_req, const char *name,
40                       struct files_struct **pfsp)
41 {
42         struct connection_struct *conn = smb_req->conn;
43         struct files_struct *fsp;
44         struct smb_filename *smb_fname = NULL;
45         NTSTATUS status;
46
47         status = file_new(smb_req, conn, &fsp);
48         if (!NT_STATUS_IS_OK(status)) {
49                 DEBUG(0, ("file_new failed: %s\n", nt_errstr(status)));
50                 return status;
51         }
52
53         fsp->conn = conn;
54         fsp->fh->fd = -1;
55         fsp->vuid = smb_req->vuid;
56         fsp->can_lock = false;
57         fsp->access_mask = FILE_READ_DATA | FILE_WRITE_DATA;
58
59         status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
60                                             &smb_fname);
61                 if (!NT_STATUS_IS_OK(status)) {
62                 file_free(smb_req, fsp);
63                 return status;
64         }
65         status = fsp_set_smb_fname(fsp, smb_fname);
66         TALLOC_FREE(smb_fname);
67         if (!NT_STATUS_IS_OK(status)) {
68                 file_free(smb_req, fsp);
69                 return status;
70         }
71
72         status = np_open(fsp, name,
73                          conn->sconn->local_address,
74                          conn->sconn->remote_address,
75                          conn->session_info,
76                          conn->sconn->msg_ctx,
77                          &fsp->fake_file_handle);
78         if (!NT_STATUS_IS_OK(status)) {
79                 DEBUG(10, ("np_open(%s) returned %s\n", name,
80                            nt_errstr(status)));
81                 file_free(smb_req, fsp);
82                 return status;
83         }
84
85         *pfsp = fsp;
86
87         return NT_STATUS_OK;
88 }
89
90 /****************************************************************************
91  Reply to an open and X on a named pipe.
92  This code is basically stolen from reply_open_and_X with some
93  wrinkles to handle pipes.
94 ****************************************************************************/
95
96 void reply_open_pipe_and_X(connection_struct *conn, struct smb_request *req)
97 {
98         const char *fname = NULL;
99         char *pipe_name = NULL;
100         files_struct *fsp;
101         TALLOC_CTX *ctx = talloc_tos();
102         NTSTATUS status;
103
104         /* XXXX we need to handle passed times, sattr and flags */
105         srvstr_pull_req_talloc(ctx, req, &pipe_name, req->buf, STR_TERMINATE);
106         if (!pipe_name) {
107                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
108                                 ERRDOS, ERRbadpipe);
109                 return;
110         }
111
112         /* If the name doesn't start \PIPE\ then this is directed */
113         /* at a mailslot or something we really, really don't understand, */
114         /* not just something we really don't understand. */
115         if ( strncmp(pipe_name,PIPE,PIPELEN) != 0 ) {
116                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
117                 return;
118         }
119
120         DEBUG(4,("Opening pipe %s.\n", pipe_name));
121
122         /* Strip \PIPE\ off the name. */
123         fname = pipe_name + PIPELEN;
124
125 #if 0
126         /*
127          * Hack for NT printers... JRA.
128          */
129         if(should_fail_next_srvsvc_open(fname)) {
130                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
131                 return;
132         }
133 #endif
134
135         status = open_np_file(req, fname, &fsp);
136         if (!NT_STATUS_IS_OK(status)) {
137                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
138                         reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
139                                         ERRDOS, ERRbadpipe);
140                         return;
141                 }
142                 reply_nterror(req, status);
143                 return;
144         }
145
146         /* Prepare the reply */
147         reply_outbuf(req, 15, 0);
148
149         SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
150         SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
151
152         /* Mark the opened file as an existing named pipe in message mode. */
153         SSVAL(req->outbuf,smb_vwv9,2);
154         SSVAL(req->outbuf,smb_vwv10,0xc700);
155
156         SSVAL(req->outbuf, smb_vwv2, fsp->fnum);
157         SSVAL(req->outbuf, smb_vwv3, 0);        /* fmode */
158         srv_put_dos_date3((char *)req->outbuf, smb_vwv4, 0);    /* mtime */
159         SIVAL(req->outbuf, smb_vwv6, 0);        /* size */
160         SSVAL(req->outbuf, smb_vwv8, 0);        /* rmode */
161         SSVAL(req->outbuf, smb_vwv11, 0x0001);
162
163         chain_reply(req);
164         return;
165 }
166
167 /****************************************************************************
168  Reply to a write on a pipe.
169 ****************************************************************************/
170
171 struct pipe_write_state {
172         size_t numtowrite;
173 };
174
175 static void pipe_write_done(struct tevent_req *subreq);
176
177 void reply_pipe_write(struct smb_request *req)
178 {
179         files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
180         const uint8_t *data;
181         struct pipe_write_state *state;
182         struct tevent_req *subreq;
183
184         if (!fsp_is_np(fsp)) {
185                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
186                 return;
187         }
188
189         if (fsp->vuid != req->vuid) {
190                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
191                 return;
192         }
193
194         state = talloc(req, struct pipe_write_state);
195         if (state == NULL) {
196                 reply_nterror(req, NT_STATUS_NO_MEMORY);
197                 return;
198         }
199         req->async_priv = state;
200
201         state->numtowrite = SVAL(req->vwv+1, 0);
202
203         data = req->buf + 3;
204
205         DEBUG(6, ("reply_pipe_write: %x name: %s len: %d\n", (int)fsp->fnum,
206                   fsp_str_dbg(fsp), (int)state->numtowrite));
207
208         subreq = np_write_send(state, req->sconn->ev_ctx,
209                                fsp->fake_file_handle, data, state->numtowrite);
210         if (subreq == NULL) {
211                 TALLOC_FREE(state);
212                 reply_nterror(req, NT_STATUS_NO_MEMORY);
213                 return;
214         }
215         tevent_req_set_callback(subreq, pipe_write_done,
216                                 talloc_move(req->conn, &req));
217 }
218
219 static void pipe_write_done(struct tevent_req *subreq)
220 {
221         struct smb_request *req = tevent_req_callback_data(
222                 subreq, struct smb_request);
223         struct pipe_write_state *state = talloc_get_type_abort(
224                 req->async_priv, struct pipe_write_state);
225         NTSTATUS status;
226         ssize_t nwritten = -1;
227
228         status = np_write_recv(subreq, &nwritten);
229         TALLOC_FREE(subreq);
230         if (nwritten < 0) {
231                 reply_nterror(req, status);
232                 goto send;
233         }
234
235         /* Looks bogus to me now. Needs to be removed ? JRA. */
236         if ((nwritten == 0 && state->numtowrite != 0)) {
237                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
238                 goto send;
239         }
240
241         reply_outbuf(req, 1, 0);
242
243         SSVAL(req->outbuf,smb_vwv0,nwritten);
244
245         DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten));
246
247  send:
248         if (!srv_send_smb(req->sconn, (char *)req->outbuf,
249                           true, req->seqnum+1,
250                           IS_CONN_ENCRYPTED(req->conn)||req->encrypted,
251                           &req->pcd)) {
252                 exit_server_cleanly("construct_reply: srv_send_smb failed.");
253         }
254         TALLOC_FREE(req);
255 }
256
257 /****************************************************************************
258  Reply to a write and X.
259
260  This code is basically stolen from reply_write_and_X with some
261  wrinkles to handle pipes.
262 ****************************************************************************/
263
264 struct pipe_write_andx_state {
265         bool pipe_start_message_raw;
266         size_t numtowrite;
267 };
268
269 static void pipe_write_andx_done(struct tevent_req *subreq);
270
271 void reply_pipe_write_and_X(struct smb_request *req)
272 {
273         files_struct *fsp = file_fsp(req, SVAL(req->vwv+2, 0));
274         int smb_doff = SVAL(req->vwv+11, 0);
275         const uint8_t *data;
276         struct pipe_write_andx_state *state;
277         struct tevent_req *subreq;
278
279         if (!fsp_is_np(fsp)) {
280                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
281                 return;
282         }
283
284         if (fsp->vuid != req->vuid) {
285                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
286                 return;
287         }
288
289         state = talloc(req, struct pipe_write_andx_state);
290         if (state == NULL) {
291                 reply_nterror(req, NT_STATUS_NO_MEMORY);
292                 return;
293         }
294         req->async_priv = state;
295
296         state->numtowrite = SVAL(req->vwv+10, 0);
297         state->pipe_start_message_raw =
298                 ((SVAL(req->vwv+7, 0) & (PIPE_START_MESSAGE|PIPE_RAW_MODE))
299                  == (PIPE_START_MESSAGE|PIPE_RAW_MODE));
300
301         DEBUG(6, ("reply_pipe_write_and_X: %x name: %s len: %d\n",
302                   (int)fsp->fnum, fsp_str_dbg(fsp), (int)state->numtowrite));
303
304         data = (const uint8_t *)smb_base(req->inbuf) + smb_doff;
305
306         if (state->pipe_start_message_raw) {
307                 /*
308                  * For the start of a message in named pipe byte mode,
309                  * the first two bytes are a length-of-pdu field. Ignore
310                  * them (we don't trust the client). JRA.
311                  */
312                 if (state->numtowrite < 2) {
313                         DEBUG(0,("reply_pipe_write_and_X: start of message "
314                                  "set and not enough data sent.(%u)\n",
315                                  (unsigned int)state->numtowrite ));
316                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
317                         return;
318                 }
319
320                 data += 2;
321                 state->numtowrite -= 2;
322         }
323
324         subreq = np_write_send(state, req->sconn->ev_ctx,
325                                fsp->fake_file_handle, data, state->numtowrite);
326         if (subreq == NULL) {
327                 TALLOC_FREE(state);
328                 reply_nterror(req, NT_STATUS_NO_MEMORY);
329                 return;
330         }
331         tevent_req_set_callback(subreq, pipe_write_andx_done,
332                                 talloc_move(req->conn, &req));
333 }
334
335 static void pipe_write_andx_done(struct tevent_req *subreq)
336 {
337         struct smb_request *req = tevent_req_callback_data(
338                 subreq, struct smb_request);
339         struct pipe_write_andx_state *state = talloc_get_type_abort(
340                 req->async_priv, struct pipe_write_andx_state);
341         NTSTATUS status;
342         ssize_t nwritten = -1;
343
344         status = np_write_recv(subreq, &nwritten);
345         TALLOC_FREE(subreq);
346
347         if (!NT_STATUS_IS_OK(status)) {
348                 reply_nterror(req, status);
349                 goto done;
350         }
351
352         /* Looks bogus to me now. Is this error message correct ? JRA. */
353         if (nwritten != state->numtowrite) {
354                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
355                 goto done;
356         }
357
358         reply_outbuf(req, 6, 0);
359
360         SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
361         SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
362
363         nwritten = (state->pipe_start_message_raw ? nwritten + 2 : nwritten);
364         SSVAL(req->outbuf,smb_vwv2,nwritten);
365
366         DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten));
367
368  done:
369         chain_reply(req);
370         /*
371          * We must free here as the ownership of req was
372          * moved to the connection struct in reply_pipe_write_and_X().
373          */
374         TALLOC_FREE(req);
375 }
376
377 /****************************************************************************
378  Reply to a read and X.
379  This code is basically stolen from reply_read_and_X with some
380  wrinkles to handle pipes.
381 ****************************************************************************/
382
383 struct pipe_read_andx_state {
384         uint8_t *outbuf;
385         int smb_mincnt;
386         int smb_maxcnt;
387 };
388
389 static void pipe_read_andx_done(struct tevent_req *subreq);
390
391 void reply_pipe_read_and_X(struct smb_request *req)
392 {
393         files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
394         uint8_t *data;
395         struct pipe_read_andx_state *state;
396         struct tevent_req *subreq;
397
398         /* we don't use the offset given to use for pipe reads. This
399            is deliberate, instead we always return the next lump of
400            data on the pipe */
401 #if 0
402         uint32 smb_offs = IVAL(req->vwv+3, 0);
403 #endif
404
405         if (!fsp_is_np(fsp)) {
406                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
407                 return;
408         }
409
410         if (fsp->vuid != req->vuid) {
411                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
412                 return;
413         }
414
415         state = talloc(req, struct pipe_read_andx_state);
416         if (state == NULL) {
417                 reply_nterror(req, NT_STATUS_NO_MEMORY);
418                 return;
419         }
420         req->async_priv = state;
421
422         state->smb_maxcnt = SVAL(req->vwv+5, 0);
423         state->smb_mincnt = SVAL(req->vwv+6, 0);
424
425         reply_outbuf(req, 12, state->smb_maxcnt);
426         SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
427         SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
428
429         data = (uint8_t *)smb_buf(req->outbuf);
430
431         /*
432          * We have to tell the upper layers that we're async.
433          */
434         state->outbuf = req->outbuf;
435         req->outbuf = NULL;
436
437         subreq = np_read_send(state, req->sconn->ev_ctx,
438                               fsp->fake_file_handle, data,
439                               state->smb_maxcnt);
440         if (subreq == NULL) {
441                 reply_nterror(req, NT_STATUS_NO_MEMORY);
442                 return;
443         }
444         tevent_req_set_callback(subreq, pipe_read_andx_done,
445                                 talloc_move(req->conn, &req));
446 }
447
448 static void pipe_read_andx_done(struct tevent_req *subreq)
449 {
450         struct smb_request *req = tevent_req_callback_data(
451                 subreq, struct smb_request);
452         struct pipe_read_andx_state *state = talloc_get_type_abort(
453                 req->async_priv, struct pipe_read_andx_state);
454         NTSTATUS status;
455         ssize_t nread;
456         bool is_data_outstanding;
457
458         status = np_read_recv(subreq, &nread, &is_data_outstanding);
459         TALLOC_FREE(subreq);
460         if (!NT_STATUS_IS_OK(status)) {
461                 reply_nterror(req, status);
462                 goto done;
463         }
464
465         req->outbuf = state->outbuf;
466         state->outbuf = NULL;
467
468         srv_set_message((char *)req->outbuf, 12, nread, False);
469
470 #if 0
471         /*
472          * we should return STATUS_BUFFER_OVERFLOW if there's
473          * out standing data.
474          *
475          * But we can't enable it yet, as it has bad interactions
476          * with fixup_chain_error_packet() in chain_reply().
477          */
478         if (is_data_outstanding) {
479                 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
480                                  STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
481         }
482 #endif
483
484         SSVAL(req->outbuf,smb_vwv5,nread);
485         SSVAL(req->outbuf,smb_vwv6,
486               req_wct_ofs(req)
487               + 1               /* the wct field */
488               + 12 * sizeof(uint16_t) /* vwv */
489               + 2);             /* the buflen field */
490         SSVAL(req->outbuf,smb_vwv11,state->smb_maxcnt);
491
492         DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
493                  state->smb_mincnt, state->smb_maxcnt, (int)nread));
494
495  done:
496         chain_reply(req);
497         /*
498          * We must free here as the ownership of req was
499          * moved to the connection struct in reply_pipe_read_and_X().
500          */
501         TALLOC_FREE(req);
502 }