Finish removal of iconv_convenience in public API's.
[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)) return NT_STATUS_INVALID_PARAMETER;
232                 name++;
233         }
234         return NT_STATUS_OK;
235 }
236
237 /*
238   open a file - used for MSRPC pipes
239 */
240 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
241                          struct ntvfs_request *req, union smb_open *oi)
242 {
243         NTSTATUS status;
244         struct pipe_state *p;
245         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
246                                     struct ipc_private);
247         struct ntvfs_handle *h;
248         struct ipc_open_state *state;
249         struct tevent_req *subreq;
250         const char *fname;
251         const char *directory;
252         struct tsocket_address *client_addr;
253         struct tsocket_address *server_addr;
254         int ret;
255         DATA_BLOB delegated_creds = data_blob_null;
256
257         switch (oi->generic.level) {
258         case RAW_OPEN_NTCREATEX:
259         case RAW_OPEN_NTTRANS_CREATE:
260                 fname = oi->ntcreatex.in.fname;
261                 break;
262         case RAW_OPEN_OPENX:
263                 fname = oi->openx.in.fname;
264                 break;
265         case RAW_OPEN_SMB2:
266                 fname = oi->smb2.in.fname;
267                 break;
268         default:
269                 return NT_STATUS_NOT_SUPPORTED;
270         }
271
272         directory = talloc_asprintf(req, "%s/np",
273                                     lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
274         NT_STATUS_HAVE_NO_MEMORY(directory);
275
276         state = talloc(req, struct ipc_open_state);
277         NT_STATUS_HAVE_NO_MEMORY(state);
278
279         status = ntvfs_handle_new(ntvfs, req, &h);
280         NT_STATUS_NOT_OK_RETURN(status);
281
282         p = talloc(h, struct pipe_state);
283         NT_STATUS_HAVE_NO_MEMORY(p);
284
285         while (fname[0] == '\\') fname++;
286
287         /* check for valid characters in name */
288         fname = strlower_talloc(p, fname);
289
290         status = validate_pipename(fname);
291         NT_STATUS_NOT_OK_RETURN(status);
292
293         p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
294         NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
295
296         p->handle = h;
297         p->ipriv = ipriv;
298
299         p->write_queue = tevent_queue_create(p, "ipc_write_queue");
300         NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
301
302         p->read_queue = tevent_queue_create(p, "ipc_read_queue");
303         NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
304
305         state->ipriv = ipriv;
306         state->p = p;
307         state->req = req;
308         state->oi = oi;
309
310         status = auth_convert_server_info_saminfo3(state,
311                                                    req->session_info->server_info,
312                                                    &state->info3);
313         NT_STATUS_NOT_OK_RETURN(status);
314
315         client_addr = ntvfs_get_local_address(ipriv->ntvfs);
316         server_addr = ntvfs_get_remote_address(ipriv->ntvfs);
317
318         if (req->session_info->credentials) {
319                 struct gssapi_creds_container *gcc;
320                 OM_uint32 gret;
321                 OM_uint32 minor_status;
322                 gss_buffer_desc cred_token;
323                 const char *error_string;
324
325                 ret = cli_credentials_get_client_gss_creds(req->session_info->credentials,
326                                                            ipriv->ntvfs->ctx->event_ctx,
327                                                            ipriv->ntvfs->ctx->lp_ctx,
328                                                            &gcc, &error_string);
329                 if (ret) {
330                         goto skip;
331                 }
332
333                 gret = gss_export_cred(&minor_status,
334                                        gcc->creds,
335                                        &cred_token);
336                 if (gret != GSS_S_COMPLETE) {
337                         return NT_STATUS_INTERNAL_ERROR;
338                 }
339
340                 if (cred_token.length) {
341                         delegated_creds = data_blob_talloc(req,
342                                                            cred_token.value,
343                                                            cred_token.length);
344                         gss_release_buffer(&minor_status, &cred_token);
345                         NT_STATUS_HAVE_NO_MEMORY(delegated_creds.data);
346                 }
347         }
348
349 skip:
350
351         subreq = tstream_npa_connect_send(p,
352                                           ipriv->ntvfs->ctx->event_ctx,
353                                           directory,
354                                           fname,
355                                           client_addr,
356                                           NULL,
357                                           server_addr,
358                                           NULL,
359                                           state->info3,
360                                           req->session_info->session_key,
361                                           delegated_creds);
362         NT_STATUS_HAVE_NO_MEMORY(subreq);
363         tevent_req_set_callback(subreq, ipc_open_done, state);
364
365         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
366         return NT_STATUS_OK;
367 }
368
369 static void ipc_open_done(struct tevent_req *subreq)
370 {
371         struct ipc_open_state *state = tevent_req_callback_data(subreq,
372                                        struct ipc_open_state);
373         struct ipc_private *ipriv = state->ipriv;
374         struct pipe_state *p = state->p;
375         struct ntvfs_request *req = state->req;
376         union smb_open *oi = state->oi;
377         int ret;
378         int sys_errno;
379         NTSTATUS status;
380
381         ret = tstream_npa_connect_recv(subreq, &sys_errno,
382                                        p, &p->npipe,
383                                        &p->file_type,
384                                        &p->device_state,
385                                        &p->allocation_size);
386         TALLOC_FREE(subreq);
387         if (ret == -1) {
388                 status = map_nt_error_from_unix(sys_errno);
389                 goto reply;
390         }
391
392         DLIST_ADD(ipriv->pipe_list, p);
393         talloc_set_destructor(p, ipc_fd_destructor);
394
395         status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
396         if (!NT_STATUS_IS_OK(status)) {
397                 goto reply;
398         }
399
400         switch (oi->generic.level) {
401         case RAW_OPEN_NTCREATEX:
402                 ZERO_STRUCT(oi->ntcreatex.out);
403                 oi->ntcreatex.out.file.ntvfs    = p->handle;
404                 oi->ntcreatex.out.oplock_level  = 0;
405                 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
406                 oi->ntcreatex.out.create_time   = 0;
407                 oi->ntcreatex.out.access_time   = 0;
408                 oi->ntcreatex.out.write_time    = 0;
409                 oi->ntcreatex.out.change_time   = 0;
410                 oi->ntcreatex.out.attrib        = FILE_ATTRIBUTE_NORMAL;
411                 oi->ntcreatex.out.alloc_size    = p->allocation_size;
412                 oi->ntcreatex.out.size          = 0;
413                 oi->ntcreatex.out.file_type     = p->file_type;
414                 oi->ntcreatex.out.ipc_state     = p->device_state;
415                 oi->ntcreatex.out.is_directory  = 0;
416                 break;
417         case RAW_OPEN_OPENX:
418                 ZERO_STRUCT(oi->openx.out);
419                 oi->openx.out.file.ntvfs        = p->handle;
420                 oi->openx.out.attrib            = FILE_ATTRIBUTE_NORMAL;
421                 oi->openx.out.write_time        = 0;
422                 oi->openx.out.size              = 0;
423                 oi->openx.out.access            = 0;
424                 oi->openx.out.ftype             = p->file_type;
425                 oi->openx.out.devstate          = p->device_state;
426                 oi->openx.out.action            = 0;
427                 oi->openx.out.unique_fid        = 0;
428                 oi->openx.out.access_mask       = 0;
429                 oi->openx.out.unknown           = 0;
430                 break;
431         case RAW_OPEN_SMB2:
432                 ZERO_STRUCT(oi->smb2.out);
433                 oi->smb2.out.file.ntvfs         = p->handle;
434                 oi->smb2.out.oplock_level       = oi->smb2.in.oplock_level;
435                 oi->smb2.out.create_action      = NTCREATEX_ACTION_EXISTED;
436                 oi->smb2.out.create_time        = 0;
437                 oi->smb2.out.access_time        = 0;
438                 oi->smb2.out.write_time         = 0;
439                 oi->smb2.out.change_time        = 0;
440                 oi->smb2.out.alloc_size         = p->allocation_size;
441                 oi->smb2.out.size               = 0;
442                 oi->smb2.out.file_attr          = FILE_ATTRIBUTE_NORMAL;
443                 oi->smb2.out.reserved2          = 0;
444                 break;
445         default:
446                 break;
447         }
448
449 reply:
450         req->async_states->status = status;
451         req->async_states->send_fn(req);
452 }
453
454 /*
455   create a directory
456 */
457 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
458                           struct ntvfs_request *req, union smb_mkdir *md)
459 {
460         return NT_STATUS_ACCESS_DENIED;
461 }
462
463 /*
464   remove a directory
465 */
466 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
467                           struct ntvfs_request *req, struct smb_rmdir *rd)
468 {
469         return NT_STATUS_ACCESS_DENIED;
470 }
471
472 /*
473   rename a set of files
474 */
475 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
476                            struct ntvfs_request *req, union smb_rename *ren)
477 {
478         return NT_STATUS_ACCESS_DENIED;
479 }
480
481 /*
482   copy a set of files
483 */
484 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
485                          struct ntvfs_request *req, struct smb_copy *cp)
486 {
487         return NT_STATUS_ACCESS_DENIED;
488 }
489
490 struct ipc_readv_next_vector_state {
491         uint8_t *buf;
492         size_t len;
493         off_t ofs;
494         size_t remaining;
495 };
496
497 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
498                                        uint8_t *buf, size_t len)
499 {
500         ZERO_STRUCTP(s);
501
502         s->buf = buf;
503         s->len = MIN(len, UINT16_MAX);
504 }
505
506 static int ipc_readv_next_vector(struct tstream_context *stream,
507                                  void *private_data,
508                                  TALLOC_CTX *mem_ctx,
509                                  struct iovec **_vector,
510                                  size_t *count)
511 {
512         struct ipc_readv_next_vector_state *state =
513                 (struct ipc_readv_next_vector_state *)private_data;
514         struct iovec *vector;
515         ssize_t pending;
516         size_t wanted;
517
518         if (state->ofs == state->len) {
519                 *_vector = NULL;
520                 *count = 0;
521                 return 0;
522         }
523
524         pending = tstream_pending_bytes(stream);
525         if (pending == -1) {
526                 return -1;
527         }
528
529         if (pending == 0 && state->ofs != 0) {
530                 /* return a short read */
531                 *_vector = NULL;
532                 *count = 0;
533                 return 0;
534         }
535
536         if (pending == 0) {
537                 /* we want at least one byte and recheck again */
538                 wanted = 1;
539         } else {
540                 size_t missing = state->len - state->ofs;
541                 if (pending > missing) {
542                         /* there's more available */
543                         state->remaining = pending - missing;
544                         wanted = missing;
545                 } else {
546                         /* read what we can get and recheck in the next cycle */
547                         wanted = pending;
548                 }
549         }
550
551         vector = talloc_array(mem_ctx, struct iovec, 1);
552         if (!vector) {
553                 return -1;
554         }
555
556         vector[0].iov_base = state->buf + state->ofs;
557         vector[0].iov_len = wanted;
558
559         state->ofs += wanted;
560
561         *_vector = vector;
562         *count = 1;
563         return 0;
564 }
565
566 struct ipc_read_state {
567         struct ipc_private *ipriv;
568         struct pipe_state *p;
569         struct ntvfs_request *req;
570         union smb_read *rd;
571         struct ipc_readv_next_vector_state next_vector;
572 };
573
574 static void ipc_read_done(struct tevent_req *subreq);
575
576 /*
577   read from a file
578 */
579 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
580                          struct ntvfs_request *req, union smb_read *rd)
581 {
582         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
583                                     struct ipc_private);
584         struct pipe_state *p;
585         struct ipc_read_state *state;
586         struct tevent_req *subreq;
587
588         if (rd->generic.level != RAW_READ_GENERIC) {
589                 return ntvfs_map_read(ntvfs, req, rd);
590         }
591
592         p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
593         if (!p) {
594                 return NT_STATUS_INVALID_HANDLE;
595         }
596
597         state = talloc(req, struct ipc_read_state);
598         NT_STATUS_HAVE_NO_MEMORY(state);
599
600         state->ipriv = ipriv;
601         state->p = p;
602         state->req = req;
603         state->rd = rd;
604
605         /* rd->readx.out.data is already allocated */
606         ipc_readv_next_vector_init(&state->next_vector,
607                                    rd->readx.out.data,
608                                    rd->readx.in.maxcnt);
609
610         subreq = tstream_readv_pdu_queue_send(req,
611                                               ipriv->ntvfs->ctx->event_ctx,
612                                               p->npipe,
613                                               p->read_queue,
614                                               ipc_readv_next_vector,
615                                               &state->next_vector);
616         NT_STATUS_HAVE_NO_MEMORY(subreq);
617         tevent_req_set_callback(subreq, ipc_read_done, state);
618
619         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
620         return NT_STATUS_OK;
621 }
622
623 static void ipc_read_done(struct tevent_req *subreq)
624 {
625         struct ipc_read_state *state =
626                 tevent_req_callback_data(subreq,
627                 struct ipc_read_state);
628         struct ntvfs_request *req = state->req;
629         union smb_read *rd = state->rd;
630         int ret;
631         int sys_errno;
632         NTSTATUS status;
633
634         ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
635         TALLOC_FREE(subreq);
636         if (ret == -1) {
637                 status = map_nt_error_from_unix(sys_errno);
638                 goto reply;
639         }
640
641         status = NT_STATUS_OK;
642         if (state->next_vector.remaining > 0) {
643                 status = STATUS_BUFFER_OVERFLOW;
644         }
645
646         rd->readx.out.remaining = state->next_vector.remaining;
647         rd->readx.out.compaction_mode = 0;
648         rd->readx.out.nread = ret;
649
650 reply:
651         req->async_states->status = status;
652         req->async_states->send_fn(req);
653 }
654
655 struct ipc_write_state {
656         struct ipc_private *ipriv;
657         struct pipe_state *p;
658         struct ntvfs_request *req;
659         union smb_write *wr;
660         struct iovec iov;
661 };
662
663 static void ipc_write_done(struct tevent_req *subreq);
664
665 /*
666   write to a file
667 */
668 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
669                           struct ntvfs_request *req, union smb_write *wr)
670 {
671         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
672                                     struct ipc_private);
673         struct pipe_state *p;
674         struct tevent_req *subreq;
675         struct ipc_write_state *state;
676
677         if (wr->generic.level != RAW_WRITE_GENERIC) {
678                 return ntvfs_map_write(ntvfs, req, wr);
679         }
680
681         p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
682         if (!p) {
683                 return NT_STATUS_INVALID_HANDLE;
684         }
685
686         state = talloc(req, struct ipc_write_state);
687         NT_STATUS_HAVE_NO_MEMORY(state);
688
689         state->ipriv = ipriv;
690         state->p = p;
691         state->req = req;
692         state->wr = wr;
693         state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
694         state->iov.iov_len = wr->writex.in.count;
695
696         subreq = tstream_writev_queue_send(state,
697                                            ipriv->ntvfs->ctx->event_ctx,
698                                            p->npipe,
699                                            p->write_queue,
700                                            &state->iov, 1);
701         NT_STATUS_HAVE_NO_MEMORY(subreq);
702         tevent_req_set_callback(subreq, ipc_write_done, state);
703
704         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
705         return NT_STATUS_OK;
706 }
707
708 static void ipc_write_done(struct tevent_req *subreq)
709 {
710         struct ipc_write_state *state =
711                 tevent_req_callback_data(subreq,
712                 struct ipc_write_state);
713         struct ntvfs_request *req = state->req;
714         union smb_write *wr = state->wr;
715         int ret;
716         int sys_errno;
717         NTSTATUS status;
718
719         ret = tstream_writev_queue_recv(subreq, &sys_errno);
720         TALLOC_FREE(subreq);
721         if (ret == -1) {
722                 status = map_nt_error_from_unix(sys_errno);
723                 goto reply;
724         }
725
726         status = NT_STATUS_OK;
727
728         wr->writex.out.nwritten = ret;
729         wr->writex.out.remaining = 0;
730
731 reply:
732         req->async_states->status = status;
733         req->async_states->send_fn(req);
734 }
735
736 /*
737   seek in a file
738 */
739 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
740                          struct ntvfs_request *req,
741                          union smb_seek *io)
742 {
743         return NT_STATUS_ACCESS_DENIED;
744 }
745
746 /*
747   flush a file
748 */
749 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
750                           struct ntvfs_request *req,
751                           union smb_flush *io)
752 {
753         return NT_STATUS_ACCESS_DENIED;
754 }
755
756 /*
757   close a file
758 */
759 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
760                           struct ntvfs_request *req, union smb_close *io)
761 {
762         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
763                                     struct ipc_private);
764         struct pipe_state *p;
765
766         if (io->generic.level != RAW_CLOSE_CLOSE) {
767                 return ntvfs_map_close(ntvfs, req, io);
768         }
769
770         p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
771         if (!p) {
772                 return NT_STATUS_INVALID_HANDLE;
773         }
774
775         talloc_free(p);
776
777         return NT_STATUS_OK;
778 }
779
780 /*
781   exit - closing files
782 */
783 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
784                          struct ntvfs_request *req)
785 {
786         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
787                                     struct ipc_private);
788         struct pipe_state *p, *next;
789         
790         for (p=ipriv->pipe_list; p; p=next) {
791                 next = p->next;
792                 if (p->handle->session_info == req->session_info &&
793                     p->handle->smbpid == req->smbpid) {
794                         talloc_free(p);
795                 }
796         }
797
798         return NT_STATUS_OK;
799 }
800
801 /*
802   logoff - closing files open by the user
803 */
804 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
805                            struct ntvfs_request *req)
806 {
807         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
808                                     struct ipc_private);
809         struct pipe_state *p, *next;
810         
811         for (p=ipriv->pipe_list; p; p=next) {
812                 next = p->next;
813                 if (p->handle->session_info == req->session_info) {
814                         talloc_free(p);
815                 }
816         }
817
818         return NT_STATUS_OK;
819 }
820
821 /*
822   setup for an async call
823 */
824 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
825                                 struct ntvfs_request *req,
826                                 void *private_data)
827 {
828         return NT_STATUS_OK;
829 }
830
831 /*
832   cancel an async call
833 */
834 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
835                            struct ntvfs_request *req)
836 {
837         return NT_STATUS_UNSUCCESSFUL;
838 }
839
840 /*
841   lock a byte range
842 */
843 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
844                          struct ntvfs_request *req, union smb_lock *lck)
845 {
846         return NT_STATUS_ACCESS_DENIED;
847 }
848
849 /*
850   set info on a open file
851 */
852 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
853                                 struct ntvfs_request *req, union smb_setfileinfo *info)
854 {
855         return NT_STATUS_ACCESS_DENIED;
856 }
857
858 /*
859   query info on a open file
860 */
861 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
862                               struct ntvfs_request *req, union smb_fileinfo *info)
863 {
864         struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
865                                     struct ipc_private);
866         struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
867         if (!p) {
868                 return NT_STATUS_INVALID_HANDLE;
869         }
870         switch (info->generic.level) {
871         case RAW_FILEINFO_GENERIC: 
872         {
873                 ZERO_STRUCT(info->generic.out);
874                 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
875                 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
876                 info->generic.out.alloc_size = 4096;
877                 info->generic.out.nlink = 1;
878                 /* What the heck?  Match Win2k3: IPC$ pipes are delete pending */
879                 info->generic.out.delete_pending = 1;
880                 return NT_STATUS_OK;
881         }
882         case RAW_FILEINFO_ALT_NAME_INFO:
883         case RAW_FILEINFO_ALT_NAME_INFORMATION:
884         case RAW_FILEINFO_STREAM_INFO:
885         case RAW_FILEINFO_STREAM_INFORMATION:
886         case RAW_FILEINFO_COMPRESSION_INFO:
887         case RAW_FILEINFO_COMPRESSION_INFORMATION:
888         case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
889         case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
890                 return NT_STATUS_INVALID_PARAMETER;
891         case  RAW_FILEINFO_ALL_EAS:
892                 return NT_STATUS_ACCESS_DENIED;
893         default:
894                 return ntvfs_map_qfileinfo(ntvfs, req, info);
895         }
896         
897         return NT_STATUS_ACCESS_DENIED;
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 = 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 = 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         return NT_STATUS_ACCESS_DENIED;
1322 }
1323
1324
1325 /*
1326   initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1327  */
1328 NTSTATUS ntvfs_ipc_init(void)
1329 {
1330         NTSTATUS ret;
1331         struct ntvfs_ops ops;
1332         NTVFS_CURRENT_CRITICAL_SIZES(vers);
1333
1334         ZERO_STRUCT(ops);
1335         
1336         /* fill in the name and type */
1337         ops.name = "default";
1338         ops.type = NTVFS_IPC;
1339
1340         /* fill in all the operations */
1341         ops.connect = ipc_connect;
1342         ops.disconnect = ipc_disconnect;
1343         ops.unlink = ipc_unlink;
1344         ops.chkpath = ipc_chkpath;
1345         ops.qpathinfo = ipc_qpathinfo;
1346         ops.setpathinfo = ipc_setpathinfo;
1347         ops.open = ipc_open;
1348         ops.mkdir = ipc_mkdir;
1349         ops.rmdir = ipc_rmdir;
1350         ops.rename = ipc_rename;
1351         ops.copy = ipc_copy;
1352         ops.ioctl = ipc_ioctl;
1353         ops.read = ipc_read;
1354         ops.write = ipc_write;
1355         ops.seek = ipc_seek;
1356         ops.flush = ipc_flush;  
1357         ops.close = ipc_close;
1358         ops.exit = ipc_exit;
1359         ops.lock = ipc_lock;
1360         ops.setfileinfo = ipc_setfileinfo;
1361         ops.qfileinfo = ipc_qfileinfo;
1362         ops.fsinfo = ipc_fsinfo;
1363         ops.lpq = ipc_lpq;
1364         ops.search_first = ipc_search_first;
1365         ops.search_next = ipc_search_next;
1366         ops.search_close = ipc_search_close;
1367         ops.trans = ipc_trans;
1368         ops.logoff = ipc_logoff;
1369         ops.async_setup = ipc_async_setup;
1370         ops.cancel = ipc_cancel;
1371
1372         /* register ourselves with the NTVFS subsystem. */
1373         ret = ntvfs_register(&ops, &vers);
1374
1375         if (!NT_STATUS_IS_OK(ret)) {
1376                 DEBUG(0,("Failed to register IPC backend!\n"));
1377                 return ret;
1378         }
1379
1380         return ret;
1381 }