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