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