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