r15855: more talloc_set_destructor() typesafe fixes. nearly done ...
[jelmer/samba4-debian.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 #include "includes.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 "ntvfs/ntvfs.h"
27 #include "rpc_server/srvsvc/proto.h"
28 #include "lib/socket/socket.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         int snum;
62
63         snum = lp_find_valid_service(share);
64         if (snum == -1) {
65                 DEBUG(0,("srvsvc_create_ntvfs_context: couldn't find service %s\n", share));
66                 return NT_STATUS_BAD_NETWORK_NAME;
67         }
68
69 #if 0 /* TODO: fix access cecking */
70         if (!socket_check_access(dce_call->connection->socket, 
71                                  lp_servicename(snum), 
72                                  lp_hostsallow(snum), 
73                                  lp_hostsdeny(snum))) {
74                 return NT_STATUS_ACCESS_DENIED;
75         }
76 #endif
77
78         /* work out what sort of connection this is */
79         if (strcmp(lp_fstype(snum), "IPC") == 0) {
80                 type = NTVFS_IPC;
81         } else if (lp_print_ok(snum)) {
82                 type = NTVFS_PRINT;
83         } else {
84                 type = NTVFS_DISK;
85         }
86
87         c = talloc(mem_ctx, struct srvsvc_ntvfs_ctx);
88         NT_STATUS_HAVE_NO_MEMORY(c);
89         
90         /* init ntvfs function pointers */
91         status = ntvfs_init_connection(c, snum, type,
92                                        PROTOCOL_NT1,
93                                        dce_call->event_ctx,
94                                        dce_call->conn->msg_ctx,
95                                        dce_call->conn->server_id,
96                                        &c->ntvfs);
97         if (!NT_STATUS_IS_OK(status)) {
98                 DEBUG(0, ("srvsvc_create_ntvfs_context: ntvfs_init_connection failed for service %s\n", 
99                           lp_servicename(snum)));
100                 return status;
101         }
102         talloc_set_destructor(c, srvsvc_ntvfs_ctx_destructor);
103
104         /*
105          * NOTE: we only set the addr callbacks as we're not interesseted in oplocks or in getting file handles
106          */
107         status = ntvfs_set_addr_callbacks(c->ntvfs, srvsvc_get_my_addr, srvsvc_get_peer_addr, dce_call->conn);
108         if (!NT_STATUS_IS_OK(status)) {
109                 DEBUG(0,("srvsvc_create_ntvfs_context: NTVFS failed to set the addr callbacks!\n"));
110                 return status;
111         }
112
113         ntvfs_req = ntvfs_request_create(c->ntvfs, mem_ctx,
114                                          dce_call->conn->auth_state.session_info,
115                                          0, /* TODO: fill in PID */
116                                          0, /* TODO: fill in MID */
117                                          dce_call->time,
118                                          NULL, NULL, 0);
119         NT_STATUS_HAVE_NO_MEMORY(ntvfs_req);
120
121         /* Invoke NTVFS connection hook */
122         status = ntvfs_connect(ntvfs_req, lp_servicename(snum));
123         if (!NT_STATUS_IS_OK(status)) {
124                 DEBUG(0,("srvsvc_create_ntvfs_context: NTVFS ntvfs_connect() failed!\n"));
125                 return status;
126         }
127
128         *_ntvfs = c->ntvfs;
129         return NT_STATUS_OK;
130 }