9e5dfc3018087a0e24369820c8eadfc3e47e3676
[garming/samba-autobuild/.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         /* Mark the opened file as an existing named pipe in message mode. */
150         SSVAL(req->outbuf,smb_vwv9,2);
151         SSVAL(req->outbuf,smb_vwv10,0xc700);
152
153         SSVAL(req->outbuf, smb_vwv2, fsp->fnum);
154         SSVAL(req->outbuf, smb_vwv3, 0);        /* fmode */
155         srv_put_dos_date3((char *)req->outbuf, smb_vwv4, 0);    /* mtime */
156         SIVAL(req->outbuf, smb_vwv6, 0);        /* size */
157         SSVAL(req->outbuf, smb_vwv8, 0);        /* rmode */
158         SSVAL(req->outbuf, smb_vwv11, 0x0001);
159
160         chain_reply(req);
161         return;
162 }
163
164 /****************************************************************************
165  Reply to a write on a pipe.
166 ****************************************************************************/
167
168 struct pipe_write_state {
169         size_t numtowrite;
170 };
171
172 static void pipe_write_done(struct tevent_req *subreq);
173
174 void reply_pipe_write(struct smb_request *req)
175 {
176         files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
177         const uint8_t *data;
178         struct pipe_write_state *state;
179         struct tevent_req *subreq;
180
181         if (!fsp_is_np(fsp)) {
182                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
183                 return;
184         }
185
186         if (fsp->vuid != req->vuid) {
187                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
188                 return;
189         }
190
191         state = talloc(req, struct pipe_write_state);
192         if (state == NULL) {
193                 reply_nterror(req, NT_STATUS_NO_MEMORY);
194                 return;
195         }
196         req->async_priv = state;
197
198         state->numtowrite = SVAL(req->vwv+1, 0);
199
200         data = req->buf + 3;
201
202         DEBUG(6, ("reply_pipe_write: %x name: %s len: %d\n", (int)fsp->fnum,
203                   fsp_str_dbg(fsp), (int)state->numtowrite));
204
205         subreq = np_write_send(state, req->sconn->ev_ctx,
206                                fsp->fake_file_handle, data, state->numtowrite);
207         if (subreq == NULL) {
208                 TALLOC_FREE(state);
209                 reply_nterror(req, NT_STATUS_NO_MEMORY);
210                 return;
211         }
212         tevent_req_set_callback(subreq, pipe_write_done,
213                                 talloc_move(req->conn, &req));
214 }
215
216 static void pipe_write_done(struct tevent_req *subreq)
217 {
218         struct smb_request *req = tevent_req_callback_data(
219                 subreq, struct smb_request);
220         struct pipe_write_state *state = talloc_get_type_abort(
221                 req->async_priv, struct pipe_write_state);
222         NTSTATUS status;
223         ssize_t nwritten = -1;
224
225         status = np_write_recv(subreq, &nwritten);
226         TALLOC_FREE(subreq);
227         if (nwritten < 0) {
228                 reply_nterror(req, status);
229                 goto send;
230         }
231
232         /* Looks bogus to me now. Needs to be removed ? JRA. */
233         if ((nwritten == 0 && state->numtowrite != 0)) {
234                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
235                 goto send;
236         }
237
238         reply_outbuf(req, 1, 0);
239
240         SSVAL(req->outbuf,smb_vwv0,nwritten);
241
242         DEBUG(3,("write-IPC nwritten=%d\n", (int)nwritten));
243
244  send:
245         if (!srv_send_smb(req->sconn, (char *)req->outbuf,
246                           true, req->seqnum+1,
247                           IS_CONN_ENCRYPTED(req->conn)||req->encrypted,
248                           &req->pcd)) {
249                 exit_server_cleanly("construct_reply: srv_send_smb failed.");
250         }
251         TALLOC_FREE(req);
252 }
253
254 /****************************************************************************
255  Reply to a write and X.
256
257  This code is basically stolen from reply_write_and_X with some
258  wrinkles to handle pipes.
259 ****************************************************************************/
260
261 struct pipe_write_andx_state {
262         bool pipe_start_message_raw;
263         size_t numtowrite;
264 };
265
266 static void pipe_write_andx_done(struct tevent_req *subreq);
267
268 void reply_pipe_write_and_X(struct smb_request *req)
269 {
270         files_struct *fsp = file_fsp(req, SVAL(req->vwv+2, 0));
271         int smb_doff = SVAL(req->vwv+11, 0);
272         const uint8_t *data;
273         struct pipe_write_andx_state *state;
274         struct tevent_req *subreq;
275
276         if (!fsp_is_np(fsp)) {
277                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
278                 return;
279         }
280
281         if (fsp->vuid != req->vuid) {
282                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
283                 return;
284         }
285
286         state = talloc(req, struct pipe_write_andx_state);
287         if (state == NULL) {
288                 reply_nterror(req, NT_STATUS_NO_MEMORY);
289                 return;
290         }
291         req->async_priv = state;
292
293         state->numtowrite = SVAL(req->vwv+10, 0);
294         state->pipe_start_message_raw =
295                 ((SVAL(req->vwv+7, 0) & (PIPE_START_MESSAGE|PIPE_RAW_MODE))
296                  == (PIPE_START_MESSAGE|PIPE_RAW_MODE));
297
298         DEBUG(6, ("reply_pipe_write_and_X: %x name: %s len: %d\n",
299                   (int)fsp->fnum, fsp_str_dbg(fsp), (int)state->numtowrite));
300
301         data = (const uint8_t *)smb_base(req->inbuf) + smb_doff;
302
303         if (state->pipe_start_message_raw) {
304                 /*
305                  * For the start of a message in named pipe byte mode,
306                  * the first two bytes are a length-of-pdu field. Ignore
307                  * them (we don't trust the client). JRA.
308                  */
309                 if (state->numtowrite < 2) {
310                         DEBUG(0,("reply_pipe_write_and_X: start of message "
311                                  "set and not enough data sent.(%u)\n",
312                                  (unsigned int)state->numtowrite ));
313                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
314                         return;
315                 }
316
317                 data += 2;
318                 state->numtowrite -= 2;
319         }
320
321         subreq = np_write_send(state, req->sconn->ev_ctx,
322                                fsp->fake_file_handle, data, state->numtowrite);
323         if (subreq == NULL) {
324                 TALLOC_FREE(state);
325                 reply_nterror(req, NT_STATUS_NO_MEMORY);
326                 return;
327         }
328         tevent_req_set_callback(subreq, pipe_write_andx_done,
329                                 talloc_move(req->conn, &req));
330 }
331
332 static void pipe_write_andx_done(struct tevent_req *subreq)
333 {
334         struct smb_request *req = tevent_req_callback_data(
335                 subreq, struct smb_request);
336         struct pipe_write_andx_state *state = talloc_get_type_abort(
337                 req->async_priv, struct pipe_write_andx_state);
338         NTSTATUS status;
339         ssize_t nwritten = -1;
340
341         status = np_write_recv(subreq, &nwritten);
342         TALLOC_FREE(subreq);
343
344         if (!NT_STATUS_IS_OK(status)) {
345                 reply_nterror(req, status);
346                 goto done;
347         }
348
349         /* Looks bogus to me now. Is this error message correct ? JRA. */
350         if (nwritten != state->numtowrite) {
351                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
352                 goto done;
353         }
354
355         reply_outbuf(req, 6, 0);
356
357         nwritten = (state->pipe_start_message_raw ? nwritten + 2 : nwritten);
358         SSVAL(req->outbuf,smb_vwv2,nwritten);
359
360         DEBUG(3,("writeX-IPC nwritten=%d\n", (int)nwritten));
361
362  done:
363         chain_reply(req);
364         /*
365          * We must free here as the ownership of req was
366          * moved to the connection struct in reply_pipe_write_and_X().
367          */
368         TALLOC_FREE(req);
369 }
370
371 /****************************************************************************
372  Reply to a read and X.
373  This code is basically stolen from reply_read_and_X with some
374  wrinkles to handle pipes.
375 ****************************************************************************/
376
377 struct pipe_read_andx_state {
378         uint8_t *outbuf;
379         int smb_mincnt;
380         int smb_maxcnt;
381 };
382
383 static void pipe_read_andx_done(struct tevent_req *subreq);
384
385 void reply_pipe_read_and_X(struct smb_request *req)
386 {
387         files_struct *fsp = file_fsp(req, SVAL(req->vwv+0, 0));
388         uint8_t *data;
389         struct pipe_read_andx_state *state;
390         struct tevent_req *subreq;
391
392         /* we don't use the offset given to use for pipe reads. This
393            is deliberate, instead we always return the next lump of
394            data on the pipe */
395 #if 0
396         uint32 smb_offs = IVAL(req->vwv+3, 0);
397 #endif
398
399         if (!fsp_is_np(fsp)) {
400                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
401                 return;
402         }
403
404         if (fsp->vuid != req->vuid) {
405                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
406                 return;
407         }
408
409         state = talloc(req, struct pipe_read_andx_state);
410         if (state == NULL) {
411                 reply_nterror(req, NT_STATUS_NO_MEMORY);
412                 return;
413         }
414         req->async_priv = state;
415
416         state->smb_maxcnt = SVAL(req->vwv+5, 0);
417         state->smb_mincnt = SVAL(req->vwv+6, 0);
418
419         reply_outbuf(req, 12, state->smb_maxcnt);
420         data = (uint8_t *)smb_buf(req->outbuf);
421
422         /*
423          * We have to tell the upper layers that we're async.
424          */
425         state->outbuf = req->outbuf;
426         req->outbuf = NULL;
427
428         subreq = np_read_send(state, req->sconn->ev_ctx,
429                               fsp->fake_file_handle, data,
430                               state->smb_maxcnt);
431         if (subreq == NULL) {
432                 reply_nterror(req, NT_STATUS_NO_MEMORY);
433                 return;
434         }
435         tevent_req_set_callback(subreq, pipe_read_andx_done,
436                                 talloc_move(req->conn, &req));
437 }
438
439 static void pipe_read_andx_done(struct tevent_req *subreq)
440 {
441         struct smb_request *req = tevent_req_callback_data(
442                 subreq, struct smb_request);
443         struct pipe_read_andx_state *state = talloc_get_type_abort(
444                 req->async_priv, struct pipe_read_andx_state);
445         NTSTATUS status;
446         ssize_t nread;
447         bool is_data_outstanding;
448
449         status = np_read_recv(subreq, &nread, &is_data_outstanding);
450         TALLOC_FREE(subreq);
451         if (!NT_STATUS_IS_OK(status)) {
452                 reply_nterror(req, status);
453                 goto done;
454         }
455
456         req->outbuf = state->outbuf;
457         state->outbuf = NULL;
458
459         srv_set_message((char *)req->outbuf, 12, nread, False);
460
461 #if 0
462         /*
463          * we should return STATUS_BUFFER_OVERFLOW if there's
464          * out standing data.
465          *
466          * But we can't enable it yet, as it has bad interactions
467          * with fixup_chain_error_packet() in chain_reply().
468          */
469         if (is_data_outstanding) {
470                 error_packet_set((char *)req->outbuf, ERRDOS, ERRmoredata,
471                                  STATUS_BUFFER_OVERFLOW, __LINE__, __FILE__);
472         }
473 #endif
474
475         SSVAL(req->outbuf,smb_vwv5,nread);
476         SSVAL(req->outbuf,smb_vwv6,
477               req_wct_ofs(req)
478               + 1               /* the wct field */
479               + 12 * sizeof(uint16_t) /* vwv */
480               + 2);             /* the buflen field */
481         SSVAL(req->outbuf,smb_vwv11,state->smb_maxcnt);
482
483         DEBUG(3,("readX-IPC min=%d max=%d nread=%d\n",
484                  state->smb_mincnt, state->smb_maxcnt, (int)nread));
485
486  done:
487         chain_reply(req);
488         /*
489          * We must free here as the ownership of req was
490          * moved to the connection struct in reply_pipe_read_and_X().
491          */
492         TALLOC_FREE(req);
493 }