s4:ntvfs/ipc: remote // debug comments
[kai/samba.git] / source4 / ntvfs / ipc / vfs_ipc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    default IPC$ NTVFS backend
4
5    Copyright (C) Andrew Tridgell 2003
6    Copyright (C) Stefan (metze) Metzmacher 2004-2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 /*
22   this implements the IPC$ backend, called by the NTVFS subsystem to
23   handle requests on IPC$ shares
24 */
25
26
27 #include "includes.h"
28 #include "../lib/util/dlinklist.h"
29 #include "ntvfs/ntvfs.h"
30 #include "../librpc/gen_ndr/rap.h"
31 #include "ntvfs/ipc/proto.h"
32 #include "libcli/raw/ioctl.h"
33 #include "param/param.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/named_pipe_auth/npa_tstream.h"
36 #include "auth/auth.h"
37 #include "auth/auth_sam_reply.h"
38 #include "lib/socket/socket.h"
39 #include "auth/credentials/credentials.h"
40 #include "auth/credentials/credentials_krb5.h"
41 #include <gssapi/gssapi.h>
42 #include "system/locale.h"
43
44 /* this is the private structure used to keep the state of an open
45    ipc$ connection. It needs to keep information about all open
46    pipes */
47 struct ipc_private {
48         struct ntvfs_module_context *ntvfs;
49
50         /* a list of open pipes */
51         struct pipe_state {
52                 struct pipe_state *next, *prev;
53                 struct ipc_private *ipriv;
54                 const char *pipe_name;
55                 struct ntvfs_handle *handle;
56                 struct tstream_context *npipe;
57                 uint16_t file_type;
58                 uint16_t device_state;
59                 uint64_t allocation_size;
60                 struct tevent_queue *write_queue;
61                 struct tevent_queue *read_queue;
62         } *pipe_list;
63 };
64
65
66 /*
67   find a open pipe give a file handle
68 */
69 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
70 {
71         struct pipe_state *s;
72         void *p;
73
74         p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
75         if (!p) return NULL;
76
77         s = talloc_get_type(p, struct pipe_state);
78         if (!s) return NULL;
79
80         return s;
81 }
82
83 /*
84   find a open pipe give a wire fnum
85 */
86 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
87 {
88         struct ntvfs_handle *h;
89
90         h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
91         if (!h) return NULL;
92
93         return pipe_state_find(ipriv, h);
94 }
95
96
97 /*
98   connect to a share - always works 
99 */
100 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
101                             struct ntvfs_request *req,
102                             union smb_tcon* tcon)
103 {
104         struct ipc_private *ipriv;
105         const char *sharename;
106
107         switch (tcon->generic.level) {
108         case RAW_TCON_TCON:
109                 sharename = tcon->tcon.in.service;
110                 break;
111         case RAW_TCON_TCONX:
112                 sharename = tcon->tconx.in.path;
113                 break;
114         case RAW_TCON_SMB2:
115                 sharename = tcon->smb2.in.path;
116                 break;
117         default:
118                 return NT_STATUS_INVALID_LEVEL;
119         }
120
121         if (strncmp(sharename, "\\\\", 2) == 0) {
122                 char *p = strchr(sharename+2, '\\');
123                 if (p) {
124                         sharename = p + 1;
125                 }
126         }
127
128         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
129         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
130
131         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
132         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
133
134         if (tcon->generic.level == RAW_TCON_TCONX) {
135                 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
136                 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
137         }
138
139         /* prepare the private state for this connection */
140         ipriv = talloc(ntvfs, struct ipc_private);
141         NT_STATUS_HAVE_NO_MEMORY(ipriv);
142
143         ntvfs->private_data = ipriv;
144
145         ipriv->ntvfs = ntvfs;
146         ipriv->pipe_list = NULL;
147
148         return NT_STATUS_OK;
149 }
150
151 /*
152   disconnect from a share
153 */
154 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
155 {
156         return NT_STATUS_OK;
157 }
158
159 /*
160   delete a file
161 */
162 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
163                            struct ntvfs_request *req,
164                            union smb_unlink *unl)
165 {
166         return NT_STATUS_ACCESS_DENIED;
167 }
168
169 /*
170   check if a directory exists
171 */
172 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
173                             struct ntvfs_request *req,
174                             union smb_chkpath *cp)
175 {
176         return NT_STATUS_ACCESS_DENIED;
177 }
178
179 /*
180   return info on a pathname
181 */
182 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
183                               struct ntvfs_request *req, union smb_fileinfo *info)
184 {
185         switch (info->generic.level) {
186         case  RAW_FILEINFO_GENERIC:
187                 return NT_STATUS_INVALID_DEVICE_REQUEST;
188         case RAW_FILEINFO_GETATTR:
189                 return NT_STATUS_ACCESS_DENIED;
190         default:
191                 return ntvfs_map_qpathinfo(ntvfs, req, info);
192         }
193 }
194
195 /*
196   set info on a pathname
197 */
198 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
199                                 struct ntvfs_request *req, union smb_setfileinfo *st)
200 {
201         return NT_STATUS_ACCESS_DENIED;
202 }
203
204
205 /*
206   destroy a open pipe structure
207 */
208 static int ipc_fd_destructor(struct pipe_state *p)
209 {
210         DLIST_REMOVE(p->ipriv->pipe_list, p);
211         ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
212         return 0;
213 }
214
215 struct ipc_open_state {
216         struct ipc_private *ipriv;
217         struct pipe_state *p;
218         struct ntvfs_request *req;
219         union smb_open *oi;
220         struct netr_SamInfo3 *info3;
221 };
222
223 static void ipc_open_done(struct tevent_req *subreq);
224
225 /*
226   check the pipename is valid
227  */
228 static NTSTATUS validate_pipename(const char *name)
229 {
230         while (*name) {
231                 if (!isalnum(*name)) return NT_STATUS_INVALID_PARAMETER;
232                 name++;
233         }
234         return NT_STATUS_OK;
235 }
236
237 /*
238   open a file - used for MSRPC pipes
239 */
240 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
241                          struct ntvfs_request *req, union smb_open *oi)
242 {
243         NTSTATUS status;
244         struct pipe_state *p;
245         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
246                                     struct ipc_private);
247         struct smb_iconv_convenience *smb_ic
248                 = lp_iconv_convenience(ipriv->ntvfs->ctx->lp_ctx);
249         struct ntvfs_handle *h;
250         struct ipc_open_state *state;
251         struct tevent_req *subreq;
252         const char *fname;
253         const char *directory;
254         struct tsocket_address *client_addr;
255         struct tsocket_address *server_addr;
256         int ret;
257         DATA_BLOB delegated_creds = data_blob_null;
258
259         switch (oi->generic.level) {
260         case RAW_OPEN_NTCREATEX:
261         case RAW_OPEN_NTTRANS_CREATE:
262                 fname = oi->ntcreatex.in.fname;
263                 break;
264         case RAW_OPEN_OPENX:
265                 fname = oi->openx.in.fname;
266                 break;
267         case RAW_OPEN_SMB2:
268                 fname = oi->smb2.in.fname;
269                 break;
270         default:
271                 return NT_STATUS_NOT_SUPPORTED;
272         }
273
274         directory = talloc_asprintf(req, "%s/np",
275                                     lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
276         NT_STATUS_HAVE_NO_MEMORY(directory);
277
278         state = talloc(req, struct ipc_open_state);
279         NT_STATUS_HAVE_NO_MEMORY(state);
280
281         status = ntvfs_handle_new(ntvfs, req, &h);
282         NT_STATUS_NOT_OK_RETURN(status);
283
284         p = talloc(h, struct pipe_state);
285         NT_STATUS_HAVE_NO_MEMORY(p);
286
287         while (fname[0] == '\\') fname++;
288
289         /* check for valid characters in name */
290         fname = strlower_talloc(p, fname);
291
292         status = validate_pipename(fname);
293         NT_STATUS_NOT_OK_RETURN(status);
294
295         p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
296         NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
297
298         p->handle = h;
299         p->ipriv = ipriv;
300
301         p->write_queue = tevent_queue_create(p, "ipc_write_queue");
302         NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
303
304         p->read_queue = tevent_queue_create(p, "ipc_read_queue");
305         NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
306
307         state->ipriv = ipriv;
308         state->p = p;
309         state->req = req;
310         state->oi = oi;
311
312         status = auth_convert_server_info_saminfo3(state,
313                                                    req->session_info->server_info,
314                                                    &state->info3);
315         NT_STATUS_NOT_OK_RETURN(status);
316
317         client_addr = ntvfs_get_local_address(ipriv->ntvfs);
318         server_addr = ntvfs_get_remote_address(ipriv->ntvfs);
319
320         if (req->session_info->credentials) {
321                 struct gssapi_creds_container *gcc;
322                 OM_uint32 gret;
323                 OM_uint32 minor_status;
324                 gss_buffer_desc cred_token;
325                 const char *error_string;
326
327                 ret = cli_credentials_get_client_gss_creds(req->session_info->credentials,
328                                                            ipriv->ntvfs->ctx->event_ctx,
329                                                            ipriv->ntvfs->ctx->lp_ctx,
330                                                            &gcc, &error_string);
331                 if (ret) {
332                         goto skip;
333                 }
334
335                 gret = gss_export_cred(&minor_status,
336                                        gcc->creds,
337                                        &cred_token);
338                 if (gret != GSS_S_COMPLETE) {
339                         return NT_STATUS_INTERNAL_ERROR;
340                 }
341
342                 if (cred_token.length) {
343                         delegated_creds = data_blob_talloc(req,
344                                                            cred_token.value,
345                                                            cred_token.length);
346                         gss_release_buffer(&minor_status, &cred_token);
347                         NT_STATUS_HAVE_NO_MEMORY(delegated_creds.data);
348                 }
349         }
350
351 skip:
352
353         subreq = tstream_npa_connect_send(p,
354                                           ipriv->ntvfs->ctx->event_ctx,
355                                           smb_ic,
356                                           directory,
357                                           fname,
358                                           client_addr,
359                                           NULL,
360                                           server_addr,
361                                           NULL,
362                                           state->info3,
363                                           req->session_info->session_key,
364                                           delegated_creds);
365         NT_STATUS_HAVE_NO_MEMORY(subreq);
366         tevent_req_set_callback(subreq, ipc_open_done, state);
367
368         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
369         return NT_STATUS_OK;
370 }
371
372 static void ipc_open_done(struct tevent_req *subreq)
373 {
374         struct ipc_open_state *state = tevent_req_callback_data(subreq,
375                                        struct ipc_open_state);
376         struct ipc_private *ipriv = state->ipriv;
377         struct pipe_state *p = state->p;
378         struct ntvfs_request *req = state->req;
379         union smb_open *oi = state->oi;
380         int ret;
381         int sys_errno;
382         NTSTATUS status;
383
384         ret = tstream_npa_connect_recv(subreq, &sys_errno,
385                                        p, &p->npipe,
386                                        &p->file_type,
387                                        &p->device_state,
388                                        &p->allocation_size);
389         TALLOC_FREE(subreq);
390         if (ret == -1) {
391                 status = map_nt_error_from_unix(sys_errno);
392                 goto reply;
393         }
394
395         DLIST_ADD(ipriv->pipe_list, p);
396         talloc_set_destructor(p, ipc_fd_destructor);
397
398         status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
399         if (!NT_STATUS_IS_OK(status)) {
400                 goto reply;
401         }
402
403         switch (oi->generic.level) {
404         case RAW_OPEN_NTCREATEX:
405                 ZERO_STRUCT(oi->ntcreatex.out);
406                 oi->ntcreatex.out.file.ntvfs    = p->handle;
407                 oi->ntcreatex.out.oplock_level  = 0;
408                 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
409                 oi->ntcreatex.out.create_time   = 0;
410                 oi->ntcreatex.out.access_time   = 0;
411                 oi->ntcreatex.out.write_time    = 0;
412                 oi->ntcreatex.out.change_time   = 0;
413                 oi->ntcreatex.out.attrib        = FILE_ATTRIBUTE_NORMAL;
414                 oi->ntcreatex.out.alloc_size    = p->allocation_size;
415                 oi->ntcreatex.out.size          = 0;
416                 oi->ntcreatex.out.file_type     = p->file_type;
417                 oi->ntcreatex.out.ipc_state     = p->device_state;
418                 oi->ntcreatex.out.is_directory  = 0;
419                 break;
420         case RAW_OPEN_OPENX:
421                 ZERO_STRUCT(oi->openx.out);
422                 oi->openx.out.file.ntvfs        = p->handle;
423                 oi->openx.out.attrib            = FILE_ATTRIBUTE_NORMAL;
424                 oi->openx.out.write_time        = 0;
425                 oi->openx.out.size              = 0;
426                 oi->openx.out.access            = 0;
427                 oi->openx.out.ftype             = p->file_type;
428                 oi->openx.out.devstate          = p->device_state;
429                 oi->openx.out.action            = 0;
430                 oi->openx.out.unique_fid        = 0;
431                 oi->openx.out.access_mask       = 0;
432                 oi->openx.out.unknown           = 0;
433                 break;
434         case RAW_OPEN_SMB2:
435                 ZERO_STRUCT(oi->smb2.out);
436                 oi->smb2.out.file.ntvfs         = p->handle;
437                 oi->smb2.out.oplock_level       = oi->smb2.in.oplock_level;
438                 oi->smb2.out.create_action      = NTCREATEX_ACTION_EXISTED;
439                 oi->smb2.out.create_time        = 0;
440                 oi->smb2.out.access_time        = 0;
441                 oi->smb2.out.write_time         = 0;
442                 oi->smb2.out.change_time        = 0;
443                 oi->smb2.out.alloc_size         = p->allocation_size;
444                 oi->smb2.out.size               = 0;
445                 oi->smb2.out.file_attr          = FILE_ATTRIBUTE_NORMAL;
446                 oi->smb2.out.reserved2          = 0;
447                 break;
448         default:
449                 break;
450         }
451
452 reply:
453         req->async_states->status = status;
454         req->async_states->send_fn(req);
455 }
456
457 /*
458   create a directory
459 */
460 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
461                           struct ntvfs_request *req, union smb_mkdir *md)
462 {
463         return NT_STATUS_ACCESS_DENIED;
464 }
465
466 /*
467   remove a directory
468 */
469 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
470                           struct ntvfs_request *req, struct smb_rmdir *rd)
471 {
472         return NT_STATUS_ACCESS_DENIED;
473 }
474
475 /*
476   rename a set of files
477 */
478 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
479                            struct ntvfs_request *req, union smb_rename *ren)
480 {
481         return NT_STATUS_ACCESS_DENIED;
482 }
483
484 /*
485   copy a set of files
486 */
487 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
488                          struct ntvfs_request *req, struct smb_copy *cp)
489 {
490         return NT_STATUS_ACCESS_DENIED;
491 }
492
493 struct ipc_readv_next_vector_state {
494         uint8_t *buf;
495         size_t len;
496         off_t ofs;
497         size_t remaining;
498 };
499
500 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
501                                        uint8_t *buf, size_t len)
502 {
503         ZERO_STRUCTP(s);
504
505         s->buf = buf;
506         s->len = MIN(len, UINT16_MAX);
507 }
508
509 static int ipc_readv_next_vector(struct tstream_context *stream,
510                                  void *private_data,
511                                  TALLOC_CTX *mem_ctx,
512                                  struct iovec **_vector,
513                                  size_t *count)
514 {
515         struct ipc_readv_next_vector_state *state =
516                 (struct ipc_readv_next_vector_state *)private_data;
517         struct iovec *vector;
518         ssize_t pending;
519         size_t wanted;
520
521         if (state->ofs == state->len) {
522                 *_vector = NULL;
523                 *count = 0;
524                 return 0;
525         }
526
527         pending = tstream_pending_bytes(stream);
528         if (pending == -1) {
529                 return -1;
530         }
531
532         if (pending == 0 && state->ofs != 0) {
533                 /* return a short read */
534                 *_vector = NULL;
535                 *count = 0;
536                 return 0;
537         }
538
539         if (pending == 0) {
540                 /* we want at least one byte and recheck again */
541                 wanted = 1;
542         } else {
543                 size_t missing = state->len - state->ofs;
544                 if (pending > missing) {
545                         /* there's more available */
546                         state->remaining = pending - missing;
547                         wanted = missing;
548                 } else {
549                         /* read what we can get and recheck in the next cycle */
550                         wanted = pending;
551                 }
552         }
553
554         vector = talloc_array(mem_ctx, struct iovec, 1);
555         if (!vector) {
556                 return -1;
557         }
558
559         vector[0].iov_base = state->buf + state->ofs;
560         vector[0].iov_len = wanted;
561
562         state->ofs += wanted;
563
564         *_vector = vector;
565         *count = 1;
566         return 0;
567 }
568
569 struct ipc_read_state {
570         struct ipc_private *ipriv;
571         struct pipe_state *p;
572         struct ntvfs_request *req;
573         union smb_read *rd;
574         struct ipc_readv_next_vector_state next_vector;
575 };
576
577 static void ipc_read_done(struct tevent_req *subreq);
578
579 /*
580   read from a file
581 */
582 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
583                          struct ntvfs_request *req, union smb_read *rd)
584 {
585         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
586                                     struct ipc_private);
587         struct pipe_state *p;
588         struct ipc_read_state *state;
589         struct tevent_req *subreq;
590
591         if (rd->generic.level != RAW_READ_GENERIC) {
592                 return ntvfs_map_read(ntvfs, req, rd);
593         }
594
595         p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
596         if (!p) {
597                 return NT_STATUS_INVALID_HANDLE;
598         }
599
600         state = talloc(req, struct ipc_read_state);
601         NT_STATUS_HAVE_NO_MEMORY(state);
602
603         state->ipriv = ipriv;
604         state->p = p;
605         state->req = req;
606         state->rd = rd;
607
608         /* rd->readx.out.data is already allocated */
609         ipc_readv_next_vector_init(&state->next_vector,
610                                    rd->readx.out.data,
611                                    rd->readx.in.maxcnt);
612
613         subreq = tstream_readv_pdu_queue_send(req,
614                                               ipriv->ntvfs->ctx->event_ctx,
615                                               p->npipe,
616                                               p->read_queue,
617                                               ipc_readv_next_vector,
618                                               &state->next_vector);
619         NT_STATUS_HAVE_NO_MEMORY(subreq);
620         tevent_req_set_callback(subreq, ipc_read_done, state);
621
622         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
623         return NT_STATUS_OK;
624 }
625
626 static void ipc_read_done(struct tevent_req *subreq)
627 {
628         struct ipc_read_state *state =
629                 tevent_req_callback_data(subreq,
630                 struct ipc_read_state);
631         struct ntvfs_request *req = state->req;
632         union smb_read *rd = state->rd;
633         int ret;
634         int sys_errno;
635         NTSTATUS status;
636
637         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
638         TALLOC_FREE(subreq);
639         if (ret == -1) {
640                 status = map_nt_error_from_unix(sys_errno);
641                 goto reply;
642         }
643
644         status = NT_STATUS_OK;
645         if (state->next_vector.remaining > 0) {
646                 status = STATUS_BUFFER_OVERFLOW;
647         }
648
649         rd->readx.out.remaining = state->next_vector.remaining;
650         rd->readx.out.compaction_mode = 0;
651         rd->readx.out.nread = ret;
652
653 reply:
654         req->async_states->status = status;
655         req->async_states->send_fn(req);
656 }
657
658 struct ipc_write_state {
659         struct ipc_private *ipriv;
660         struct pipe_state *p;
661         struct ntvfs_request *req;
662         union smb_write *wr;
663         struct iovec iov;
664 };
665
666 static void ipc_write_done(struct tevent_req *subreq);
667
668 /*
669   write to a file
670 */
671 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
672                           struct ntvfs_request *req, union smb_write *wr)
673 {
674         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
675                                     struct ipc_private);
676         struct pipe_state *p;
677         struct tevent_req *subreq;
678         struct ipc_write_state *state;
679
680         if (wr->generic.level != RAW_WRITE_GENERIC) {
681                 return ntvfs_map_write(ntvfs, req, wr);
682         }
683
684         p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
685         if (!p) {
686                 return NT_STATUS_INVALID_HANDLE;
687         }
688
689         state = talloc(req, struct ipc_write_state);
690         NT_STATUS_HAVE_NO_MEMORY(state);
691
692         state->ipriv = ipriv;
693         state->p = p;
694         state->req = req;
695         state->wr = wr;
696         state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
697         state->iov.iov_len = wr->writex.in.count;
698
699         subreq = tstream_writev_queue_send(state,
700                                            ipriv->ntvfs->ctx->event_ctx,
701                                            p->npipe,
702                                            p->write_queue,
703                                            &state->iov, 1);
704         NT_STATUS_HAVE_NO_MEMORY(subreq);
705         tevent_req_set_callback(subreq, ipc_write_done, state);
706
707         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
708         return NT_STATUS_OK;
709 }
710
711 static void ipc_write_done(struct tevent_req *subreq)
712 {
713         struct ipc_write_state *state =
714                 tevent_req_callback_data(subreq,
715                 struct ipc_write_state);
716         struct ntvfs_request *req = state->req;
717         union smb_write *wr = state->wr;
718         int ret;
719         int sys_errno;
720         NTSTATUS status;
721
722         ret = tstream_writev_queue_recv(subreq, &sys_errno);
723         TALLOC_FREE(subreq);
724         if (ret == -1) {
725                 status = map_nt_error_from_unix(sys_errno);
726                 goto reply;
727         }
728
729         status = NT_STATUS_OK;
730
731         wr->writex.out.nwritten = ret;
732         wr->writex.out.remaining = 0;
733
734 reply:
735         req->async_states->status = status;
736         req->async_states->send_fn(req);
737 }
738
739 /*
740   seek in a file
741 */
742 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
743                          struct ntvfs_request *req,
744                          union smb_seek *io)
745 {
746         return NT_STATUS_ACCESS_DENIED;
747 }
748
749 /*
750   flush a file
751 */
752 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
753                           struct ntvfs_request *req,
754                           union smb_flush *io)
755 {
756         return NT_STATUS_ACCESS_DENIED;
757 }
758
759 /*
760   close a file
761 */
762 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
763                           struct ntvfs_request *req, union smb_close *io)
764 {
765         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
766                                     struct ipc_private);
767         struct pipe_state *p;
768
769         if (io->generic.level != RAW_CLOSE_CLOSE) {
770                 return ntvfs_map_close(ntvfs, req, io);
771         }
772
773         p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
774         if (!p) {
775                 return NT_STATUS_INVALID_HANDLE;
776         }
777
778         talloc_free(p);
779
780         return NT_STATUS_OK;
781 }
782
783 /*
784   exit - closing files
785 */
786 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
787                          struct ntvfs_request *req)
788 {
789         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
790                                     struct ipc_private);
791         struct pipe_state *p, *next;
792         
793         for (p=ipriv->pipe_list; p; p=next) {
794                 next = p->next;
795                 if (p->handle->session_info == req->session_info &&
796                     p->handle->smbpid == req->smbpid) {
797                         talloc_free(p);
798                 }
799         }
800
801         return NT_STATUS_OK;
802 }
803
804 /*
805   logoff - closing files open by the user
806 */
807 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
808                            struct ntvfs_request *req)
809 {
810         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
811                                     struct ipc_private);
812         struct pipe_state *p, *next;
813         
814         for (p=ipriv->pipe_list; p; p=next) {
815                 next = p->next;
816                 if (p->handle->session_info == req->session_info) {
817                         talloc_free(p);
818                 }
819         }
820
821         return NT_STATUS_OK;
822 }
823
824 /*
825   setup for an async call
826 */
827 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
828                                 struct ntvfs_request *req,
829                                 void *private_data)
830 {
831         return NT_STATUS_OK;
832 }
833
834 /*
835   cancel an async call
836 */
837 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
838                            struct ntvfs_request *req)
839 {
840         return NT_STATUS_UNSUCCESSFUL;
841 }
842
843 /*
844   lock a byte range
845 */
846 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
847                          struct ntvfs_request *req, union smb_lock *lck)
848 {
849         return NT_STATUS_ACCESS_DENIED;
850 }
851
852 /*
853   set info on a open file
854 */
855 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
856                                 struct ntvfs_request *req, union smb_setfileinfo *info)
857 {
858         return NT_STATUS_ACCESS_DENIED;
859 }
860
861 /*
862   query info on a open file
863 */
864 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
865                               struct ntvfs_request *req, union smb_fileinfo *info)
866 {
867         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
868                                     struct ipc_private);
869         struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
870         if (!p) {
871                 return NT_STATUS_INVALID_HANDLE;
872         }
873         switch (info->generic.level) {
874         case RAW_FILEINFO_GENERIC: 
875         {
876                 ZERO_STRUCT(info->generic.out);
877                 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
878                 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
879                 info->generic.out.alloc_size = 4096;
880                 info->generic.out.nlink = 1;
881                 /* What the heck?  Match Win2k3: IPC$ pipes are delete pending */
882                 info->generic.out.delete_pending = 1;
883                 return NT_STATUS_OK;
884         }
885         case RAW_FILEINFO_ALT_NAME_INFO:
886         case RAW_FILEINFO_ALT_NAME_INFORMATION:
887         case RAW_FILEINFO_STREAM_INFO:
888         case RAW_FILEINFO_STREAM_INFORMATION:
889         case RAW_FILEINFO_COMPRESSION_INFO:
890         case RAW_FILEINFO_COMPRESSION_INFORMATION:
891         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
892         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
893                 return NT_STATUS_INVALID_PARAMETER;
894         case  RAW_FILEINFO_ALL_EAS:
895                 return NT_STATUS_ACCESS_DENIED;
896         default:
897                 return ntvfs_map_qfileinfo(ntvfs, req, info);
898         }
899         
900         return NT_STATUS_ACCESS_DENIED;
901 }
902
903
904 /*
905   return filesystem info
906 */
907 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
908                            struct ntvfs_request *req, union smb_fsinfo *fs)
909 {
910         return NT_STATUS_ACCESS_DENIED;
911 }
912
913 /*
914   return print queue info
915 */
916 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
917                         struct ntvfs_request *req, union smb_lpq *lpq)
918 {
919         return NT_STATUS_ACCESS_DENIED;
920 }
921
922 /* 
923    list files in a directory matching a wildcard pattern
924 */
925 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
926                           struct ntvfs_request *req, union smb_search_first *io,
927                           void *search_private, 
928                           bool (*callback)(void *, const union smb_search_data *))
929 {
930         return NT_STATUS_ACCESS_DENIED;
931 }
932
933 /* 
934    continue listing files in a directory 
935 */
936 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
937                          struct ntvfs_request *req, union smb_search_next *io,
938                          void *search_private, 
939                          bool (*callback)(void *, const union smb_search_data *))
940 {
941         return NT_STATUS_ACCESS_DENIED;
942 }
943
944 /* 
945    end listing files in a directory 
946 */
947 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
948                           struct ntvfs_request *req, union smb_search_close *io)
949 {
950         return NT_STATUS_ACCESS_DENIED;
951 }
952
953 struct ipc_trans_state {
954         struct ipc_private *ipriv;
955         struct pipe_state *p;
956         struct ntvfs_request *req;
957         struct smb_trans2 *trans;
958         struct iovec writev_iov;
959         struct ipc_readv_next_vector_state next_vector;
960 };
961
962 static void ipc_trans_writev_done(struct tevent_req *subreq);
963 static void ipc_trans_readv_done(struct tevent_req *subreq);
964
965 /* SMBtrans - handle a DCERPC command */
966 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
967                                struct ntvfs_request *req, struct smb_trans2 *trans)
968 {
969         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
970                                     struct ipc_private);
971         struct pipe_state *p;
972         DATA_BLOB fnum_key;
973         uint16_t fnum;
974         struct ipc_trans_state *state;
975         struct tevent_req *subreq;
976
977         /*
978          * the fnum is in setup[1], a 16 bit value
979          * the setup[*] values are already in host byteorder
980          * but ntvfs_handle_search_by_wire_key() expects
981          * network byteorder
982          */
983         SSVAL(&fnum, 0, trans->in.setup[1]);
984         fnum_key = data_blob_const(&fnum, 2);
985
986         p = pipe_state_find_key(ipriv, req, &fnum_key);
987         if (!p) {
988                 return NT_STATUS_INVALID_HANDLE;
989         }
990
991         /*
992          * Trans requests are only allowed
993          * if no other Trans or Read is active
994          */
995         if (tevent_queue_length(p->read_queue) > 0) {
996                 return NT_STATUS_PIPE_BUSY;
997         }
998
999         state = talloc(req, struct ipc_trans_state);
1000         NT_STATUS_HAVE_NO_MEMORY(state);
1001
1002         trans->out.setup_count = 0;
1003         trans->out.setup = NULL;
1004         trans->out.params = data_blob(NULL, 0);
1005         trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1006         NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1007
1008         state->ipriv = ipriv;
1009         state->p = p;
1010         state->req = req;
1011         state->trans = trans;
1012         state->writev_iov.iov_base = trans->in.data.data;
1013         state->writev_iov.iov_len = trans->in.data.length;
1014
1015         ipc_readv_next_vector_init(&state->next_vector,
1016                                    trans->out.data.data,
1017                                    trans->out.data.length);
1018
1019         subreq = tstream_writev_queue_send(state,
1020                                            ipriv->ntvfs->ctx->event_ctx,
1021                                            p->npipe,
1022                                            p->write_queue,
1023                                            &state->writev_iov, 1);
1024         NT_STATUS_HAVE_NO_MEMORY(subreq);
1025         tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1026
1027         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1028         return NT_STATUS_OK;
1029 }
1030
1031 static void ipc_trans_writev_done(struct tevent_req *subreq)
1032 {
1033         struct ipc_trans_state *state =
1034                 tevent_req_callback_data(subreq,
1035                 struct ipc_trans_state);
1036         struct ipc_private *ipriv = state->ipriv;
1037         struct pipe_state *p = state->p;
1038         struct ntvfs_request *req = state->req;
1039         int ret;
1040         int sys_errno;
1041         NTSTATUS status;
1042
1043         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1044         TALLOC_FREE(subreq);
1045         if (ret == 0) {
1046                 status = NT_STATUS_PIPE_DISCONNECTED;
1047                 goto reply;
1048         } else if (ret == -1) {
1049                 status = map_nt_error_from_unix(sys_errno);
1050                 goto reply;
1051         }
1052
1053         subreq = tstream_readv_pdu_queue_send(state,
1054                                               ipriv->ntvfs->ctx->event_ctx,
1055                                               p->npipe,
1056                                               p->read_queue,
1057                                               ipc_readv_next_vector,
1058                                               &state->next_vector);
1059         if (!subreq) {
1060                 status = NT_STATUS_NO_MEMORY;
1061                 goto reply;
1062         }
1063         tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1064         return;
1065
1066 reply:
1067         req->async_states->status = status;
1068         req->async_states->send_fn(req);
1069 }
1070
1071 static void ipc_trans_readv_done(struct tevent_req *subreq)
1072 {
1073         struct ipc_trans_state *state =
1074                 tevent_req_callback_data(subreq,
1075                 struct ipc_trans_state);
1076         struct ntvfs_request *req = state->req;
1077         struct smb_trans2 *trans = state->trans;
1078         int ret;
1079         int sys_errno;
1080         NTSTATUS status;
1081
1082         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1083         TALLOC_FREE(subreq);
1084         if (ret == -1) {
1085                 status = map_nt_error_from_unix(sys_errno);
1086                 goto reply;
1087         }
1088
1089         status = NT_STATUS_OK;
1090         if (state->next_vector.remaining > 0) {
1091                 status = STATUS_BUFFER_OVERFLOW;
1092         }
1093
1094         trans->out.data.length = ret;
1095
1096 reply:
1097         req->async_states->status = status;
1098         req->async_states->send_fn(req);
1099 }
1100
1101 /* SMBtrans - set named pipe state */
1102 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1103                                       struct ntvfs_request *req, struct smb_trans2 *trans)
1104 {
1105         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1106                                     struct ipc_private);
1107         struct pipe_state *p;
1108         DATA_BLOB fnum_key;
1109
1110         /* the fnum is in setup[1] */
1111         fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1112
1113         p = pipe_state_find_key(ipriv, req, &fnum_key);
1114         if (!p) {
1115                 return NT_STATUS_INVALID_HANDLE;
1116         }
1117
1118         if (trans->in.params.length != 2) {
1119                 return NT_STATUS_INVALID_PARAMETER;
1120         }
1121
1122         /*
1123          * TODO: pass this to the tstream_npa logic
1124          */
1125         p->device_state = SVAL(trans->in.params.data, 0);
1126
1127         trans->out.setup_count = 0;
1128         trans->out.setup = NULL;
1129         trans->out.params = data_blob(NULL, 0);
1130         trans->out.data = data_blob(NULL, 0);
1131
1132         return NT_STATUS_OK;
1133 }
1134
1135
1136 /* SMBtrans - used to provide access to SMB pipes */
1137 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1138                                 struct ntvfs_request *req, struct smb_trans2 *trans)
1139 {
1140         NTSTATUS status;
1141
1142         if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1143                 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1144
1145         if (trans->in.setup_count != 2) {
1146                 return NT_STATUS_INVALID_PARAMETER;
1147         }
1148
1149         switch (trans->in.setup[0]) {
1150         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1151                 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1152                 break;
1153         case TRANSACT_DCERPCCMD:
1154                 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1155                 break;
1156         default:
1157                 status = NT_STATUS_INVALID_PARAMETER;
1158                 break;
1159         }
1160
1161         return status;
1162 }
1163
1164 struct ipc_ioctl_state {
1165         struct ipc_private *ipriv;
1166         struct pipe_state *p;
1167         struct ntvfs_request *req;
1168         union smb_ioctl *io;
1169         struct iovec writev_iov;
1170         struct ipc_readv_next_vector_state next_vector;
1171 };
1172
1173 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1174 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1175
1176 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1177                                struct ntvfs_request *req, union smb_ioctl *io)
1178 {
1179         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1180                                     struct ipc_private);
1181         struct pipe_state *p;
1182         struct ipc_ioctl_state *state;
1183         struct tevent_req *subreq;
1184
1185         switch (io->smb2.in.function) {
1186         case FSCTL_NAMED_PIPE_READ_WRITE:
1187                 break;
1188
1189         default:
1190                 return NT_STATUS_FS_DRIVER_REQUIRED;
1191         }
1192
1193         p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1194         if (!p) {
1195                 return NT_STATUS_INVALID_HANDLE;
1196         }
1197
1198         /*
1199          * Trans requests are only allowed
1200          * if no other Trans or Read is active
1201          */
1202         if (tevent_queue_length(p->read_queue) > 0) {
1203                 return NT_STATUS_PIPE_BUSY;
1204         }
1205
1206         state = talloc(req, struct ipc_ioctl_state);
1207         NT_STATUS_HAVE_NO_MEMORY(state);
1208
1209         io->smb2.out._pad       = 0;
1210         io->smb2.out.function   = io->smb2.in.function;
1211         io->smb2.out.unknown2   = 0;
1212         io->smb2.out.unknown3   = 0;
1213         io->smb2.out.in         = io->smb2.in.out;
1214         io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1215         NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1216
1217         state->ipriv = ipriv;
1218         state->p = p;
1219         state->req = req;
1220         state->io = io;
1221         state->writev_iov.iov_base = io->smb2.in.out.data;
1222         state->writev_iov.iov_len = io->smb2.in.out.length;
1223
1224         ipc_readv_next_vector_init(&state->next_vector,
1225                                    io->smb2.out.out.data,
1226                                    io->smb2.out.out.length);
1227
1228         subreq = tstream_writev_queue_send(state,
1229                                            ipriv->ntvfs->ctx->event_ctx,
1230                                            p->npipe,
1231                                            p->write_queue,
1232                                            &state->writev_iov, 1);
1233         NT_STATUS_HAVE_NO_MEMORY(subreq);
1234         tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1235
1236         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1237         return NT_STATUS_OK;
1238 }
1239
1240 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1241 {
1242         struct ipc_ioctl_state *state =
1243                 tevent_req_callback_data(subreq,
1244                 struct ipc_ioctl_state);
1245         struct ipc_private *ipriv = state->ipriv;
1246         struct pipe_state *p = state->p;
1247         struct ntvfs_request *req = state->req;
1248         int ret;
1249         int sys_errno;
1250         NTSTATUS status;
1251
1252         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1253         TALLOC_FREE(subreq);
1254         if (ret == -1) {
1255                 status = map_nt_error_from_unix(sys_errno);
1256                 goto reply;
1257         }
1258
1259         subreq = tstream_readv_pdu_queue_send(state,
1260                                               ipriv->ntvfs->ctx->event_ctx,
1261                                               p->npipe,
1262                                               p->read_queue,
1263                                               ipc_readv_next_vector,
1264                                               &state->next_vector);
1265         if (!subreq) {
1266                 status = NT_STATUS_NO_MEMORY;
1267                 goto reply;
1268         }
1269         tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1270         return;
1271
1272 reply:
1273         req->async_states->status = status;
1274         req->async_states->send_fn(req);
1275 }
1276
1277 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1278 {
1279         struct ipc_ioctl_state *state =
1280                 tevent_req_callback_data(subreq,
1281                 struct ipc_ioctl_state);
1282         struct ntvfs_request *req = state->req;
1283         union smb_ioctl *io = state->io;
1284         int ret;
1285         int sys_errno;
1286         NTSTATUS status;
1287
1288         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1289         TALLOC_FREE(subreq);
1290         if (ret == -1) {
1291                 status = map_nt_error_from_unix(sys_errno);
1292                 goto reply;
1293         }
1294
1295         status = NT_STATUS_OK;
1296         if (state->next_vector.remaining > 0) {
1297                 status = STATUS_BUFFER_OVERFLOW;
1298         }
1299
1300         io->smb2.out.out.length = ret;
1301
1302 reply:
1303         req->async_states->status = status;
1304         req->async_states->send_fn(req);
1305 }
1306
1307 /*
1308   ioctl interface
1309 */
1310 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1311                           struct ntvfs_request *req, union smb_ioctl *io)
1312 {
1313         switch (io->generic.level) {
1314         case RAW_IOCTL_SMB2:
1315                 return ipc_ioctl_smb2(ntvfs, req, io);
1316
1317         case RAW_IOCTL_SMB2_NO_HANDLE:
1318                 return NT_STATUS_FS_DRIVER_REQUIRED;
1319
1320         default:
1321                 return NT_STATUS_ACCESS_DENIED;
1322         }
1323
1324         return NT_STATUS_ACCESS_DENIED;
1325 }
1326
1327
1328 /*
1329   initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1330  */
1331 NTSTATUS ntvfs_ipc_init(void)
1332 {
1333         NTSTATUS ret;
1334         struct ntvfs_ops ops;
1335         NTVFS_CURRENT_CRITICAL_SIZES(vers);
1336
1337         ZERO_STRUCT(ops);
1338         
1339         /* fill in the name and type */
1340         ops.name = "default";
1341         ops.type = NTVFS_IPC;
1342
1343         /* fill in all the operations */
1344         ops.connect = ipc_connect;
1345         ops.disconnect = ipc_disconnect;
1346         ops.unlink = ipc_unlink;
1347         ops.chkpath = ipc_chkpath;
1348         ops.qpathinfo = ipc_qpathinfo;
1349         ops.setpathinfo = ipc_setpathinfo;
1350         ops.open = ipc_open;
1351         ops.mkdir = ipc_mkdir;
1352         ops.rmdir = ipc_rmdir;
1353         ops.rename = ipc_rename;
1354         ops.copy = ipc_copy;
1355         ops.ioctl = ipc_ioctl;
1356         ops.read = ipc_read;
1357         ops.write = ipc_write;
1358         ops.seek = ipc_seek;
1359         ops.flush = ipc_flush;  
1360         ops.close = ipc_close;
1361         ops.exit = ipc_exit;
1362         ops.lock = ipc_lock;
1363         ops.setfileinfo = ipc_setfileinfo;
1364         ops.qfileinfo = ipc_qfileinfo;
1365         ops.fsinfo = ipc_fsinfo;
1366         ops.lpq = ipc_lpq;
1367         ops.search_first = ipc_search_first;
1368         ops.search_next = ipc_search_next;
1369         ops.search_close = ipc_search_close;
1370         ops.trans = ipc_trans;
1371         ops.logoff = ipc_logoff;
1372         ops.async_setup = ipc_async_setup;
1373         ops.cancel = ipc_cancel;
1374
1375         /* register ourselves with the NTVFS subsystem. */
1376         ret = ntvfs_register(&ops, &vers);
1377
1378         if (!NT_STATUS_IS_OK(ret)) {
1379                 DEBUG(0,("Failed to register IPC backend!\n"));
1380                 return ret;
1381         }
1382
1383         return ret;
1384 }