15bd749ad1b2eee79eedca91775638363bbfa50a
[ira/wip.git] / source4 / rpc_server / srvsvc / srvsvc_ntvfs.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    srvsvc pipe ntvfs helper functions
5
6    Copyright (C) Stefan (metze) Metzmacher 2006
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 #include "includes.h"
22 #include "ntvfs/ntvfs.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "librpc/gen_ndr/ndr_srvsvc.h"
25 #include "rpc_server/common/common.h"
26 #include "rpc_server/srvsvc/proto.h"
27 #include "lib/socket/socket.h"
28 #include "param/param.h"
29
30 struct socket_address *srvsvc_get_my_addr(void *p, TALLOC_CTX *mem_ctx)
31 {
32         struct dcesrv_connection *conn = talloc_get_type(p, struct dcesrv_connection);
33         return dcesrv_connection_get_my_addr(conn, mem_ctx);
34 }
35
36 struct socket_address *srvsvc_get_peer_addr(void *p, TALLOC_CTX *mem_ctx)
37 {
38         struct dcesrv_connection *conn = talloc_get_type(p, struct dcesrv_connection);
39         return dcesrv_connection_get_peer_addr(conn, mem_ctx);
40 }
41
42 struct srvsvc_ntvfs_ctx {
43         struct ntvfs_context *ntvfs;
44 };
45
46 static int srvsvc_ntvfs_ctx_destructor(struct srvsvc_ntvfs_ctx *c)
47 {
48         ntvfs_disconnect(c->ntvfs);
49         return 0;
50 }
51
52 NTSTATUS srvsvc_create_ntvfs_context(struct dcesrv_call_state *dce_call,
53                                      TALLOC_CTX *mem_ctx,
54                                      const char *share,
55                                      struct ntvfs_context **_ntvfs)
56 {
57         NTSTATUS status;
58         struct srvsvc_ntvfs_ctx *c;
59         struct ntvfs_request *ntvfs_req;
60         enum ntvfs_type type;
61         struct share_context *sctx;
62         struct share_config *scfg;
63         const char *sharetype;
64
65         status = share_get_context_by_name(mem_ctx, lp_share_backend(dce_call->conn->dce_ctx->lp_ctx), dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, &sctx);
66         if (!NT_STATUS_IS_OK(status)) {
67                 return status;
68         }
69
70         status = share_get_config(mem_ctx, sctx, share, &scfg);
71         if (!NT_STATUS_IS_OK(status)) {
72                 DEBUG(0,("srvsvc_create_ntvfs_context: couldn't find service %s\n", share));
73                 return status;
74         }
75
76 #if 0 /* TODO: fix access cecking */
77         if (!socket_check_access(dce_call->connection->socket, 
78                                  scfg->name, 
79                                  share_string_list_option(scfg, SHARE_HOSTS_ALLOW), 
80                                  share_string_list_option(scfg, SHARE_HOSTS_DENY))) {
81                 return NT_STATUS_ACCESS_DENIED;
82         }
83 #endif
84
85         /* work out what sort of connection this is */
86         sharetype = share_string_option(scfg, SHARE_TYPE, SHARE_TYPE_DEFAULT);
87         if (sharetype && strcmp(sharetype, "IPC") == 0) {
88                 type = NTVFS_IPC;
89         } else if (sharetype && strcmp(sharetype, "PRINTER")) {
90                 type = NTVFS_PRINT;
91         } else {
92                 type = NTVFS_DISK;
93         }
94
95         c = talloc(mem_ctx, struct srvsvc_ntvfs_ctx);
96         NT_STATUS_HAVE_NO_MEMORY(c);
97         
98         /* init ntvfs function pointers */
99         status = ntvfs_init_connection(c, scfg, type,
100                                        PROTOCOL_NT1,
101                                        0,/* ntvfs_client_caps */
102                                        dce_call->event_ctx,
103                                        dce_call->conn->msg_ctx,
104                                        dce_call->conn->dce_ctx->lp_ctx,
105                                        dce_call->conn->server_id,
106                                        &c->ntvfs);
107         if (!NT_STATUS_IS_OK(status)) {
108                 DEBUG(0, ("srvsvc_create_ntvfs_context: ntvfs_init_connection failed for service %s\n", 
109                           scfg->name));
110                 return status;
111         }
112         talloc_set_destructor(c, srvsvc_ntvfs_ctx_destructor);
113
114         /*
115          * NOTE: we only set the addr callbacks as we're not interesseted in oplocks or in getting file handles
116          */
117         status = ntvfs_set_addr_callbacks(c->ntvfs, srvsvc_get_my_addr, srvsvc_get_peer_addr, dce_call->conn);
118         if (!NT_STATUS_IS_OK(status)) {
119                 DEBUG(0,("srvsvc_create_ntvfs_context: NTVFS failed to set the addr callbacks!\n"));
120                 return status;
121         }
122
123         ntvfs_req = ntvfs_request_create(c->ntvfs, mem_ctx,
124                                          dce_call->conn->auth_state.session_info,
125                                          0, /* TODO: fill in PID */
126                                          dce_call->time,
127                                          NULL, NULL, 0);
128         NT_STATUS_HAVE_NO_MEMORY(ntvfs_req);
129
130         /* Invoke NTVFS connection hook */
131         /* FIXME: Here is the right parameter missing!
132          * status = ntvfs_connect(ntvfs_req, <TODO>); */
133         status = NT_STATUS_UNSUCCESSFUL; /* return this for now */
134         if (!NT_STATUS_IS_OK(status)) {
135                 DEBUG(0,("srvsvc_create_ntvfs_context: NTVFS ntvfs_connect() failed!\n"));
136                 return status;
137         }
138
139         *_ntvfs = c->ntvfs;
140         return NT_STATUS_OK;
141 }