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