e2648852203e53a7c71081356bd1505e2081a51e
[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 *client_addr;
259         const struct tsocket_address *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         client_addr = ntvfs_get_local_address(ipriv->ntvfs);
327         server_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                                           client_addr,
334                                           NULL,
335                                           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         if (tevent_coroutine_nomem(subreq, coro)) {
680                 return tevent_coroutine_return(coro);
681         }
682         tevent_coroutine_yield(coro, subreq);
683         ok = tevent_wakeup_recv(subreq);
684
685         DEBUG(0,("%s[%p]: 2. %s wakeup[%d]\n", __location__, coro, state->string, ok));
686
687         tevent_coroutine_done(coro);
688         return tevent_coroutine_return(coro);
689 }
690
691 static int example_ocor_recv(struct tevent_req *req, int *perrno)
692 {
693         if (tevent_req_is_unix_error(req, perrno)) {
694                 return -1;
695         }
696
697         return 0;
698 }
699
700 struct example_coro_state {
701         const char *string;
702 };
703
704 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
705                                                          struct tevent_context *ev,
706                                                          void *private_data);
707
708 static struct tevent_req *example_coro_send(TALLOC_CTX *mem_ctx,
709                                      struct tevent_context *ev,
710                                      const char *string)
711 {
712         struct tevent_req *req;
713         struct example_coro_state *state;
714         struct tevent_coroutine *coro;
715
716         req = tevent_req_create(mem_ctx, &state, struct example_coro_state);
717         if (req == NULL) {
718                 return NULL;
719         }
720
721         state->string = string;
722
723         coro = tevent_coroutine_create(req, ev, example_coro_body);
724         if (tevent_req_nomem(coro, req)) {
725                 return tevent_req_post(req, ev);
726         }
727
728         tevent_coroutine_run(coro);
729         if (!tevent_req_is_in_progress(req)) {
730                 return tevent_req_post(req, ev);
731         }
732
733         return req;
734 }
735
736 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
737                                                          struct tevent_context *ev,
738                                                          void *private_data)
739 {
740         struct example_coro_state *state = talloc_get_type_abort(private_data,
741                                            struct example_coro_state);
742         struct tevent_req *subreq;
743         int ret;
744         int sys_errno;
745
746         DEBUG(0,("%s:%s[%p]: 1. %s\n", __location__, __FUNCTION__, coro, state->string));
747
748         subreq = example_ocor_send(state, ev, state->string);
749         if (tevent_coroutine_nomem(subreq, coro)) {
750                 return tevent_coroutine_return(coro);
751         }
752         tevent_coroutine_yield(coro, subreq);
753         ret = example_ocor_recv(subreq, &sys_errno);
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         tevent_coroutine_done(coro);
762         return tevent_coroutine_return(coro);
763 }
764
765 static int example_coro_recv(struct tevent_req *req, int *perrno)
766 {
767         if (tevent_req_is_unix_error(req, perrno)) {
768                 return -1;
769         }
770
771         return 0;
772 }
773
774 struct ipc_write_state {
775         struct ipc_private *ipriv;
776         struct pipe_state *p;
777         struct ntvfs_request *req;
778         union smb_write *wr;
779         struct iovec iov;
780 };
781
782 static void ipc_write_done(struct tevent_req *subreq);
783
784 static void ipc_write_coro_done(struct tevent_req *subreq);
785
786 /*
787   write to a file
788 */
789 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
790                           struct ntvfs_request *req, union smb_write *wr)
791 {
792         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
793                                     struct ipc_private);
794         struct pipe_state *p;
795         struct tevent_req *subreq;
796         struct ipc_write_state *state;
797
798         if (wr->generic.level != RAW_WRITE_GENERIC) {
799                 return ntvfs_map_write(ntvfs, req, wr);
800         }
801
802         p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
803         if (!p) {
804                 return NT_STATUS_INVALID_HANDLE;
805         }
806
807         state = talloc(req, struct ipc_write_state);
808         NT_STATUS_HAVE_NO_MEMORY(state);
809
810         state->ipriv = ipriv;
811         state->p = p;
812         state->req = req;
813         state->wr = wr;
814         state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
815         state->iov.iov_len = wr->writex.in.count;
816
817         subreq = example_coro_send(state,
818                                    ipriv->ntvfs->ctx->event_ctx,
819                                    "write_start");
820         NT_STATUS_HAVE_NO_MEMORY(subreq);
821         tevent_req_set_callback(subreq, ipc_write_coro_done, state);
822
823         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
824         return NT_STATUS_OK;
825 }
826
827 static void ipc_write_coro_done(struct tevent_req *subreq)
828 {
829         struct ipc_write_state *state =
830                 tevent_req_callback_data(subreq,
831                 struct ipc_write_state);
832         struct pipe_state *p = state->p;
833         struct ipc_private *ipriv = state->ipriv;
834         struct ntvfs_request *req = state->req;
835         int ret;
836         int sys_errno;
837         NTSTATUS status;
838
839         ret = example_coro_recv(subreq, &sys_errno);
840         TALLOC_FREE(subreq);
841         if (ret == -1) {
842                 status = map_nt_error_from_unix(sys_errno);
843                 goto reply;
844         }
845
846         subreq = tstream_writev_queue_send(state,
847                                            ipriv->ntvfs->ctx->event_ctx,
848                                            p->npipe,
849                                            p->write_queue,
850                                            &state->iov, 1);
851         if (!subreq) {
852                 status = NT_STATUS_NO_MEMORY;
853                 goto reply;
854         }
855         tevent_req_set_callback(subreq, ipc_write_done, state);
856         return;
857
858 reply:
859         req->async_states->status = status;
860         req->async_states->send_fn(req);
861 }
862
863 static void ipc_write_done(struct tevent_req *subreq)
864 {
865         struct ipc_write_state *state =
866                 tevent_req_callback_data(subreq,
867                 struct ipc_write_state);
868         struct ntvfs_request *req = state->req;
869         union smb_write *wr = state->wr;
870         int ret;
871         int sys_errno;
872         NTSTATUS status;
873
874         ret = tstream_writev_queue_recv(subreq, &sys_errno);
875         TALLOC_FREE(subreq);
876         if (ret == -1) {
877                 status = map_nt_error_from_unix_common(sys_errno);
878                 goto reply;
879         }
880
881         status = NT_STATUS_OK;
882
883         wr->writex.out.nwritten = ret;
884         wr->writex.out.remaining = 0;
885
886 reply:
887         req->async_states->status = status;
888         req->async_states->send_fn(req);
889 }
890
891 /*
892   seek in a file
893 */
894 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
895                          struct ntvfs_request *req,
896                          union smb_seek *io)
897 {
898         return NT_STATUS_ACCESS_DENIED;
899 }
900
901 /*
902   flush a file
903 */
904 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
905                           struct ntvfs_request *req,
906                           union smb_flush *io)
907 {
908         return NT_STATUS_ACCESS_DENIED;
909 }
910
911 /*
912   close a file
913 */
914 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
915                           struct ntvfs_request *req, union smb_close *io)
916 {
917         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
918                                     struct ipc_private);
919         struct pipe_state *p;
920
921         if (io->generic.level != RAW_CLOSE_CLOSE) {
922                 return ntvfs_map_close(ntvfs, req, io);
923         }
924
925         p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
926         if (!p) {
927                 return NT_STATUS_INVALID_HANDLE;
928         }
929
930         talloc_free(p);
931
932         return NT_STATUS_OK;
933 }
934
935 /*
936   exit - closing files
937 */
938 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
939                          struct ntvfs_request *req)
940 {
941         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
942                                     struct ipc_private);
943         struct pipe_state *p, *next;
944         
945         for (p=ipriv->pipe_list; p; p=next) {
946                 next = p->next;
947                 if (p->handle->session_info == req->session_info &&
948                     p->handle->smbpid == req->smbpid) {
949                         talloc_free(p);
950                 }
951         }
952
953         return NT_STATUS_OK;
954 }
955
956 /*
957   logoff - closing files open by the user
958 */
959 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
960                            struct ntvfs_request *req)
961 {
962         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
963                                     struct ipc_private);
964         struct pipe_state *p, *next;
965         
966         for (p=ipriv->pipe_list; p; p=next) {
967                 next = p->next;
968                 if (p->handle->session_info == req->session_info) {
969                         talloc_free(p);
970                 }
971         }
972
973         return NT_STATUS_OK;
974 }
975
976 /*
977   setup for an async call
978 */
979 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
980                                 struct ntvfs_request *req,
981                                 void *private_data)
982 {
983         return NT_STATUS_OK;
984 }
985
986 /*
987   cancel an async call
988 */
989 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
990                            struct ntvfs_request *req)
991 {
992         return NT_STATUS_UNSUCCESSFUL;
993 }
994
995 /*
996   lock a byte range
997 */
998 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
999                          struct ntvfs_request *req, union smb_lock *lck)
1000 {
1001         return NT_STATUS_ACCESS_DENIED;
1002 }
1003
1004 /*
1005   set info on a open file
1006 */
1007 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
1008                                 struct ntvfs_request *req, union smb_setfileinfo *info)
1009 {
1010         return NT_STATUS_ACCESS_DENIED;
1011 }
1012
1013 /*
1014   query info on a open file
1015 */
1016 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
1017                               struct ntvfs_request *req, union smb_fileinfo *info)
1018 {
1019         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1020                                     struct ipc_private);
1021         struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
1022         if (!p) {
1023                 return NT_STATUS_INVALID_HANDLE;
1024         }
1025         switch (info->generic.level) {
1026         case RAW_FILEINFO_GENERIC: 
1027         {
1028                 ZERO_STRUCT(info->generic.out);
1029                 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
1030                 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
1031                 info->generic.out.alloc_size = 4096;
1032                 info->generic.out.nlink = 1;
1033                 /* What the heck?  Match Win2k3: IPC$ pipes are delete pending */
1034                 info->generic.out.delete_pending = 1;
1035                 return NT_STATUS_OK;
1036         }
1037         case RAW_FILEINFO_ALT_NAME_INFO:
1038         case RAW_FILEINFO_ALT_NAME_INFORMATION:
1039         case RAW_FILEINFO_STREAM_INFO:
1040         case RAW_FILEINFO_STREAM_INFORMATION:
1041         case RAW_FILEINFO_COMPRESSION_INFO:
1042         case RAW_FILEINFO_COMPRESSION_INFORMATION:
1043         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1044         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1045                 return NT_STATUS_INVALID_PARAMETER;
1046         case  RAW_FILEINFO_ALL_EAS:
1047                 return NT_STATUS_ACCESS_DENIED;
1048         default:
1049                 return ntvfs_map_qfileinfo(ntvfs, req, info);
1050         }
1051 }
1052
1053
1054 /*
1055   return filesystem info
1056 */
1057 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
1058                            struct ntvfs_request *req, union smb_fsinfo *fs)
1059 {
1060         return NT_STATUS_ACCESS_DENIED;
1061 }
1062
1063 /*
1064   return print queue info
1065 */
1066 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
1067                         struct ntvfs_request *req, union smb_lpq *lpq)
1068 {
1069         return NT_STATUS_ACCESS_DENIED;
1070 }
1071
1072 /* 
1073    list files in a directory matching a wildcard pattern
1074 */
1075 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
1076                           struct ntvfs_request *req, union smb_search_first *io,
1077                           void *search_private, 
1078                           bool (*callback)(void *, const union smb_search_data *))
1079 {
1080         return NT_STATUS_ACCESS_DENIED;
1081 }
1082
1083 /* 
1084    continue listing files in a directory 
1085 */
1086 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
1087                          struct ntvfs_request *req, union smb_search_next *io,
1088                          void *search_private, 
1089                          bool (*callback)(void *, const union smb_search_data *))
1090 {
1091         return NT_STATUS_ACCESS_DENIED;
1092 }
1093
1094 /* 
1095    end listing files in a directory 
1096 */
1097 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
1098                           struct ntvfs_request *req, union smb_search_close *io)
1099 {
1100         return NT_STATUS_ACCESS_DENIED;
1101 }
1102
1103 struct ipc_trans_state {
1104         struct ipc_private *ipriv;
1105         struct pipe_state *p;
1106         struct ntvfs_request *req;
1107         struct smb_trans2 *trans;
1108         struct iovec writev_iov;
1109         struct ipc_readv_next_vector_state next_vector;
1110 };
1111
1112 static void ipc_trans_writev_done(struct tevent_req *subreq);
1113 static void ipc_trans_readv_done(struct tevent_req *subreq);
1114
1115 /* SMBtrans - handle a DCERPC command */
1116 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
1117                                struct ntvfs_request *req, struct smb_trans2 *trans)
1118 {
1119         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1120                                     struct ipc_private);
1121         struct pipe_state *p;
1122         DATA_BLOB fnum_key;
1123         uint16_t fnum;
1124         struct ipc_trans_state *state;
1125         struct tevent_req *subreq;
1126
1127         /*
1128          * the fnum is in setup[1], a 16 bit value
1129          * the setup[*] values are already in host byteorder
1130          * but ntvfs_handle_search_by_wire_key() expects
1131          * network byteorder
1132          */
1133         SSVAL(&fnum, 0, trans->in.setup[1]);
1134         fnum_key = data_blob_const(&fnum, 2);
1135
1136         p = pipe_state_find_key(ipriv, req, &fnum_key);
1137         if (!p) {
1138                 return NT_STATUS_INVALID_HANDLE;
1139         }
1140
1141         /*
1142          * Trans requests are only allowed
1143          * if no other Trans or Read is active
1144          */
1145         if (tevent_queue_length(p->read_queue) > 0) {
1146                 return NT_STATUS_PIPE_BUSY;
1147         }
1148
1149         state = talloc(req, struct ipc_trans_state);
1150         NT_STATUS_HAVE_NO_MEMORY(state);
1151
1152         trans->out.setup_count = 0;
1153         trans->out.setup = NULL;
1154         trans->out.params = data_blob(NULL, 0);
1155         trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1156         NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1157
1158         state->ipriv = ipriv;
1159         state->p = p;
1160         state->req = req;
1161         state->trans = trans;
1162         state->writev_iov.iov_base = (char *) trans->in.data.data;
1163         state->writev_iov.iov_len = trans->in.data.length;
1164
1165         ipc_readv_next_vector_init(&state->next_vector,
1166                                    trans->out.data.data,
1167                                    trans->out.data.length);
1168
1169         subreq = tstream_writev_queue_send(state,
1170                                            ipriv->ntvfs->ctx->event_ctx,
1171                                            p->npipe,
1172                                            p->write_queue,
1173                                            &state->writev_iov, 1);
1174         NT_STATUS_HAVE_NO_MEMORY(subreq);
1175         tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1176
1177         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1178         return NT_STATUS_OK;
1179 }
1180
1181 static void ipc_trans_writev_done(struct tevent_req *subreq)
1182 {
1183         struct ipc_trans_state *state =
1184                 tevent_req_callback_data(subreq,
1185                 struct ipc_trans_state);
1186         struct ipc_private *ipriv = state->ipriv;
1187         struct pipe_state *p = state->p;
1188         struct ntvfs_request *req = state->req;
1189         int ret;
1190         int sys_errno;
1191         NTSTATUS status;
1192
1193         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1194         TALLOC_FREE(subreq);
1195         if (ret == 0) {
1196                 status = NT_STATUS_PIPE_DISCONNECTED;
1197                 goto reply;
1198         } else if (ret == -1) {
1199                 status = map_nt_error_from_unix_common(sys_errno);
1200                 goto reply;
1201         }
1202
1203         subreq = tstream_readv_pdu_queue_send(state,
1204                                               ipriv->ntvfs->ctx->event_ctx,
1205                                               p->npipe,
1206                                               p->read_queue,
1207                                               ipc_readv_next_vector,
1208                                               &state->next_vector);
1209         if (!subreq) {
1210                 status = NT_STATUS_NO_MEMORY;
1211                 goto reply;
1212         }
1213         tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1214         return;
1215
1216 reply:
1217         req->async_states->status = status;
1218         req->async_states->send_fn(req);
1219 }
1220
1221 static void ipc_trans_readv_done(struct tevent_req *subreq)
1222 {
1223         struct ipc_trans_state *state =
1224                 tevent_req_callback_data(subreq,
1225                 struct ipc_trans_state);
1226         struct ntvfs_request *req = state->req;
1227         struct smb_trans2 *trans = state->trans;
1228         int ret;
1229         int sys_errno;
1230         NTSTATUS status;
1231
1232         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1233         TALLOC_FREE(subreq);
1234         if (ret == -1) {
1235                 status = map_nt_error_from_unix_common(sys_errno);
1236                 goto reply;
1237         }
1238
1239         status = NT_STATUS_OK;
1240         if (state->next_vector.remaining > 0) {
1241                 status = STATUS_BUFFER_OVERFLOW;
1242         }
1243
1244         trans->out.data.length = ret;
1245
1246 reply:
1247         req->async_states->status = status;
1248         req->async_states->send_fn(req);
1249 }
1250
1251 /* SMBtrans - set named pipe state */
1252 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1253                                       struct ntvfs_request *req, struct smb_trans2 *trans)
1254 {
1255         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1256                                     struct ipc_private);
1257         struct pipe_state *p;
1258         DATA_BLOB fnum_key;
1259
1260         /* the fnum is in setup[1] */
1261         fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1262
1263         p = pipe_state_find_key(ipriv, req, &fnum_key);
1264         if (!p) {
1265                 return NT_STATUS_INVALID_HANDLE;
1266         }
1267
1268         if (trans->in.params.length != 2) {
1269                 return NT_STATUS_INVALID_PARAMETER;
1270         }
1271
1272         /*
1273          * TODO: pass this to the tstream_npa logic
1274          */
1275         p->device_state = SVAL(trans->in.params.data, 0);
1276
1277         trans->out.setup_count = 0;
1278         trans->out.setup = NULL;
1279         trans->out.params = data_blob(NULL, 0);
1280         trans->out.data = data_blob(NULL, 0);
1281
1282         return NT_STATUS_OK;
1283 }
1284
1285
1286 /* SMBtrans - used to provide access to SMB pipes */
1287 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1288                                 struct ntvfs_request *req, struct smb_trans2 *trans)
1289 {
1290         NTSTATUS status;
1291
1292         if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1293                 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1294
1295         if (trans->in.setup_count != 2) {
1296                 return NT_STATUS_INVALID_PARAMETER;
1297         }
1298
1299         switch (trans->in.setup[0]) {
1300         case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1301                 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1302                 break;
1303         case TRANSACT_DCERPCCMD:
1304                 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1305                 break;
1306         default:
1307                 status = NT_STATUS_INVALID_PARAMETER;
1308                 break;
1309         }
1310
1311         return status;
1312 }
1313
1314 struct ipc_ioctl_state {
1315         struct ipc_private *ipriv;
1316         struct pipe_state *p;
1317         struct ntvfs_request *req;
1318         union smb_ioctl *io;
1319         struct iovec writev_iov;
1320         struct ipc_readv_next_vector_state next_vector;
1321 };
1322
1323 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1324 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1325
1326 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1327                                struct ntvfs_request *req, union smb_ioctl *io)
1328 {
1329         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1330                                     struct ipc_private);
1331         struct pipe_state *p;
1332         struct ipc_ioctl_state *state;
1333         struct tevent_req *subreq;
1334
1335         switch (io->smb2.in.function) {
1336         case FSCTL_NAMED_PIPE_READ_WRITE:
1337                 break;
1338
1339         default:
1340                 return NT_STATUS_FS_DRIVER_REQUIRED;
1341         }
1342
1343         p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1344         if (!p) {
1345                 return NT_STATUS_INVALID_HANDLE;
1346         }
1347
1348         /*
1349          * Trans requests are only allowed
1350          * if no other Trans or Read is active
1351          */
1352         if (tevent_queue_length(p->read_queue) > 0) {
1353                 return NT_STATUS_PIPE_BUSY;
1354         }
1355
1356         state = talloc(req, struct ipc_ioctl_state);
1357         NT_STATUS_HAVE_NO_MEMORY(state);
1358
1359         io->smb2.out._pad       = 0;
1360         io->smb2.out.function   = io->smb2.in.function;
1361         io->smb2.out.unknown2   = 0;
1362         io->smb2.out.unknown3   = 0;
1363         io->smb2.out.in         = data_blob_null;
1364         io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1365         NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1366
1367         state->ipriv = ipriv;
1368         state->p = p;
1369         state->req = req;
1370         state->io = io;
1371         state->writev_iov.iov_base = (char *) io->smb2.in.out.data;
1372         state->writev_iov.iov_len = io->smb2.in.out.length;
1373
1374         ipc_readv_next_vector_init(&state->next_vector,
1375                                    io->smb2.out.out.data,
1376                                    io->smb2.out.out.length);
1377
1378         subreq = tstream_writev_queue_send(state,
1379                                            ipriv->ntvfs->ctx->event_ctx,
1380                                            p->npipe,
1381                                            p->write_queue,
1382                                            &state->writev_iov, 1);
1383         NT_STATUS_HAVE_NO_MEMORY(subreq);
1384         tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1385
1386         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1387         return NT_STATUS_OK;
1388 }
1389
1390 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1391 {
1392         struct ipc_ioctl_state *state =
1393                 tevent_req_callback_data(subreq,
1394                 struct ipc_ioctl_state);
1395         struct ipc_private *ipriv = state->ipriv;
1396         struct pipe_state *p = state->p;
1397         struct ntvfs_request *req = state->req;
1398         int ret;
1399         int sys_errno;
1400         NTSTATUS status;
1401
1402         ret = tstream_writev_queue_recv(subreq, &sys_errno);
1403         TALLOC_FREE(subreq);
1404         if (ret == -1) {
1405                 status = map_nt_error_from_unix_common(sys_errno);
1406                 goto reply;
1407         }
1408
1409         subreq = tstream_readv_pdu_queue_send(state,
1410                                               ipriv->ntvfs->ctx->event_ctx,
1411                                               p->npipe,
1412                                               p->read_queue,
1413                                               ipc_readv_next_vector,
1414                                               &state->next_vector);
1415         if (!subreq) {
1416                 status = NT_STATUS_NO_MEMORY;
1417                 goto reply;
1418         }
1419         tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1420         return;
1421
1422 reply:
1423         req->async_states->status = status;
1424         req->async_states->send_fn(req);
1425 }
1426
1427 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1428 {
1429         struct ipc_ioctl_state *state =
1430                 tevent_req_callback_data(subreq,
1431                 struct ipc_ioctl_state);
1432         struct ntvfs_request *req = state->req;
1433         union smb_ioctl *io = state->io;
1434         int ret;
1435         int sys_errno;
1436         NTSTATUS status;
1437
1438         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1439         TALLOC_FREE(subreq);
1440         if (ret == -1) {
1441                 status = map_nt_error_from_unix_common(sys_errno);
1442                 goto reply;
1443         }
1444
1445         status = NT_STATUS_OK;
1446         if (state->next_vector.remaining > 0) {
1447                 status = STATUS_BUFFER_OVERFLOW;
1448         }
1449
1450         io->smb2.out.out.length = ret;
1451
1452 reply:
1453         req->async_states->status = status;
1454         req->async_states->send_fn(req);
1455 }
1456
1457 /*
1458   ioctl interface
1459 */
1460 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1461                           struct ntvfs_request *req, union smb_ioctl *io)
1462 {
1463         switch (io->generic.level) {
1464         case RAW_IOCTL_SMB2:
1465                 return ipc_ioctl_smb2(ntvfs, req, io);
1466
1467         case RAW_IOCTL_SMB2_NO_HANDLE:
1468                 return NT_STATUS_FS_DRIVER_REQUIRED;
1469
1470         default:
1471                 return NT_STATUS_ACCESS_DENIED;
1472         }
1473 }
1474
1475
1476 /*
1477   initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1478  */
1479 NTSTATUS ntvfs_ipc_init(void)
1480 {
1481         NTSTATUS ret;
1482         struct ntvfs_ops ops;
1483         NTVFS_CURRENT_CRITICAL_SIZES(vers);
1484
1485         ZERO_STRUCT(ops);
1486         
1487         /* fill in the name and type */
1488         ops.name = "default";
1489         ops.type = NTVFS_IPC;
1490
1491         /* fill in all the operations */
1492         ops.connect_fn = ipc_connect;
1493         ops.disconnect_fn = ipc_disconnect;
1494         ops.unlink_fn = ipc_unlink;
1495         ops.chkpath_fn = ipc_chkpath;
1496         ops.qpathinfo_fn = ipc_qpathinfo;
1497         ops.setpathinfo_fn = ipc_setpathinfo;
1498         ops.open_fn = ipc_open;
1499         ops.mkdir_fn = ipc_mkdir;
1500         ops.rmdir_fn = ipc_rmdir;
1501         ops.rename_fn = ipc_rename;
1502         ops.copy_fn = ipc_copy;
1503         ops.ioctl_fn = ipc_ioctl;
1504         ops.read_fn = ipc_read;
1505         ops.write_fn = ipc_write;
1506         ops.seek_fn = ipc_seek;
1507         ops.flush_fn = ipc_flush;
1508         ops.close_fn = ipc_close;
1509         ops.exit_fn = ipc_exit;
1510         ops.lock_fn = ipc_lock;
1511         ops.setfileinfo_fn = ipc_setfileinfo;
1512         ops.qfileinfo_fn = ipc_qfileinfo;
1513         ops.fsinfo_fn = ipc_fsinfo;
1514         ops.lpq_fn = ipc_lpq;
1515         ops.search_first_fn = ipc_search_first;
1516         ops.search_next_fn = ipc_search_next;
1517         ops.search_close_fn = ipc_search_close;
1518         ops.trans_fn = ipc_trans;
1519         ops.logoff_fn = ipc_logoff;
1520         ops.async_setup_fn = ipc_async_setup;
1521         ops.cancel_fn = ipc_cancel;
1522
1523         /* register ourselves with the NTVFS subsystem. */
1524         ret = ntvfs_register(&ops, &vers);
1525
1526         if (!NT_STATUS_IS_OK(ret)) {
1527                 DEBUG(0,("Failed to register IPC backend!\n"));
1528                 return ret;
1529         }
1530
1531         return ret;
1532 }