r23792: convert Samba4 to GPLv3
[bbaumbach/samba-autobuild/.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
29 struct socket_address *srvsvc_get_my_addr(void *p, TALLOC_CTX *mem_ctx)
30 {
31         struct dcesrv_connection *conn = talloc_get_type(p, struct dcesrv_connection);
32         return dcesrv_connection_get_my_addr(conn, mem_ctx);
33 }
34
35 struct socket_address *srvsvc_get_peer_addr(void *p, TALLOC_CTX *mem_ctx)
36 {
37         struct dcesrv_connection *conn = talloc_get_type(p, struct dcesrv_connection);
38         return dcesrv_connection_get_peer_addr(conn, mem_ctx);
39 }
40
41 struct srvsvc_ntvfs_ctx {
42         struct ntvfs_context *ntvfs;
43 };
44
45 static int srvsvc_ntvfs_ctx_destructor(struct srvsvc_ntvfs_ctx *c)
46 {
47         ntvfs_disconnect(c->ntvfs);
48         return 0;
49 }
50
51 NTSTATUS srvsvc_create_ntvfs_context(struct dcesrv_call_state *dce_call,
52                                      TALLOC_CTX *mem_ctx,
53                                      const char *share,
54                                      struct ntvfs_context **_ntvfs)
55 {
56         NTSTATUS status;
57         struct srvsvc_ntvfs_ctx *c;
58         struct ntvfs_request *ntvfs_req;
59         enum ntvfs_type type;
60         struct share_context *sctx;
61         struct share_config *scfg;
62         const char *sharetype;
63
64         status = share_get_context(mem_ctx, &sctx);
65         if (!NT_STATUS_IS_OK(status)) {
66                 return status;
67         }
68
69         status = share_get_config(mem_ctx, sctx, share, &scfg);
70         if (!NT_STATUS_IS_OK(status)) {
71                 DEBUG(0,("srvsvc_create_ntvfs_context: couldn't find service %s\n", share));
72                 return status;
73         }
74
75 #if 0 /* TODO: fix access cecking */
76         if (!socket_check_access(dce_call->connection->socket, 
77                                  scfg->name, 
78                                  share_string_list_option(scfg, SHARE_HOSTS_ALLOW), 
79                                  share_string_list_option(scfg, SHARE_HOSTS_DENY))) {
80                 return NT_STATUS_ACCESS_DENIED;
81         }
82 #endif
83
84         /* work out what sort of connection this is */
85         sharetype = share_string_option(scfg, SHARE_TYPE, SHARE_TYPE_DEFAULT);
86         if (sharetype && strcmp(sharetype, "IPC") == 0) {
87                 type = NTVFS_IPC;
88         } else if (sharetype && strcmp(sharetype, "PRINTER")) {
89                 type = NTVFS_PRINT;
90         } else {
91                 type = NTVFS_DISK;
92         }
93
94         c = talloc(mem_ctx, struct srvsvc_ntvfs_ctx);
95         NT_STATUS_HAVE_NO_MEMORY(c);
96         
97         /* init ntvfs function pointers */
98         status = ntvfs_init_connection(c, scfg, type,
99                                        PROTOCOL_NT1,
100                                        dce_call->event_ctx,
101                                        dce_call->conn->msg_ctx,
102                                        dce_call->conn->server_id,
103                                        &c->ntvfs);
104         if (!NT_STATUS_IS_OK(status)) {
105                 DEBUG(0, ("srvsvc_create_ntvfs_context: ntvfs_init_connection failed for service %s\n", 
106                           scfg->name));
107                 return status;
108         }
109         talloc_set_destructor(c, srvsvc_ntvfs_ctx_destructor);
110
111         /*
112          * NOTE: we only set the addr callbacks as we're not interesseted in oplocks or in getting file handles
113          */
114         status = ntvfs_set_addr_callbacks(c->ntvfs, srvsvc_get_my_addr, srvsvc_get_peer_addr, dce_call->conn);
115         if (!NT_STATUS_IS_OK(status)) {
116                 DEBUG(0,("srvsvc_create_ntvfs_context: NTVFS failed to set the addr callbacks!\n"));
117                 return status;
118         }
119
120         ntvfs_req = ntvfs_request_create(c->ntvfs, mem_ctx,
121                                          dce_call->conn->auth_state.session_info,
122                                          0, /* TODO: fill in PID */
123                                          dce_call->time,
124                                          NULL, NULL, 0);
125         NT_STATUS_HAVE_NO_MEMORY(ntvfs_req);
126
127         /* Invoke NTVFS connection hook */
128         status = ntvfs_connect(ntvfs_req, scfg->name);
129         if (!NT_STATUS_IS_OK(status)) {
130                 DEBUG(0,("srvsvc_create_ntvfs_context: NTVFS ntvfs_connect() failed!\n"));
131                 return status;
132         }
133
134         *_ntvfs = c->ntvfs;
135         return NT_STATUS_OK;
136 }