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