s3:modules: add vfs_dfs_samba4
[kai/samba.git] / source3 / modules / vfs_dfs_samba4.c
1 /*
2  * VFS module to alter the algorithm to calculate
3  * the struct file_id used as key for the share mode
4  * and byte range locking db's.
5  *
6  * Copyright (C) 2007, Stefan Metzmacher
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 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "system/filesys.h"
26 #include "source3/include/msdfs.h"
27 #include "librpc/gen_ndr/ndr_dfsblobs.h"
28 #include "source4/lib/events/events.h"
29 #include "source4/auth/session.h"
30 #include "source4/param/param.h"
31 #include "source4/dsdb/samdb/samdb.h"
32 #include "dfs_server/dfs_server_ad.h"
33
34 static int vfs_dfs_samba4_debug_level = DBGC_VFS;
35
36 #undef DBGC_CLASS
37 #define DBGC_CLASS vfs_dfs_samba4_debug_level
38
39 struct dfs_samba4_handle_data {
40         struct tevent_context *ev;
41         struct loadparm_context *lp_ctx;
42         struct ldb_context *sam_ctx;
43 };
44
45 static int dfs_samba4_connect(struct vfs_handle_struct *handle,
46                               const char *service, const char *user)
47 {
48         struct dfs_samba4_handle_data *data;
49         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
50
51         if (ret < 0) {
52                 return ret;
53         }
54
55         data = talloc_zero(handle->conn, struct dfs_samba4_handle_data);
56         if (!data) {
57                 DEBUG(0, ("talloc_zero() failed\n"));
58                 SMB_VFS_NEXT_DISCONNECT(handle);
59                 return -1;
60         }
61
62         data->ev = s4_event_context_init(data);
63         if (!data->ev) {
64                 DEBUG(0, ("s4_event_context_init failed\n"));
65                 SMB_VFS_NEXT_DISCONNECT(handle);
66                 return -1;
67         }
68
69         data->lp_ctx = loadparm_init_s3(data, loadparm_s3_context());
70         if (data->lp_ctx == NULL) {
71                 DEBUG(0, ("loadparm_init_s3 failed\n"));
72                 SMB_VFS_NEXT_DISCONNECT(handle);
73                 return -1;
74         }
75
76         data->sam_ctx = samdb_connect(data,
77                                       data->ev,
78                                       data->lp_ctx,
79                                       system_session(data->lp_ctx), 0);
80         if (!data->sam_ctx) {
81                 DEBUG(0, ("samdb_connect failed\n"));
82                 SMB_VFS_NEXT_DISCONNECT(handle);
83                 return -1;
84         }
85
86         SMB_VFS_HANDLE_SET_DATA(handle, data, NULL,
87                                 struct dfs_samba4_handle_data,
88                                 return -1);
89
90         DEBUG(10,("dfs_samba4: connect to service[%s]\n",
91                   service));
92
93         return 0;
94 }
95
96 static void dfs_samba4_disconnect(struct vfs_handle_struct *handle)
97 {
98         DEBUG(10,("dfs_samba4_disconnect() connect to service[%s].\n",
99                   lp_servicename(SNUM(handle->conn))));
100
101         SMB_VFS_NEXT_DISCONNECT(handle);
102 }
103
104 static NTSTATUS dfs_samba4_get_referrals(struct vfs_handle_struct *handle,
105                                          struct dfs_GetDFSReferral *r)
106 {
107         struct dfs_samba4_handle_data *data;
108         NTSTATUS status;
109
110         SMB_VFS_HANDLE_GET_DATA(handle, data,
111                                 struct dfs_samba4_handle_data,
112                                 return NT_STATUS_INTERNAL_ERROR);
113
114         DEBUG(8, ("dfs_samba4: Requested DFS name: %s utf16-length: %u\n",
115                    r->in.req.servername,
116                    (unsigned int)strlen_m(r->in.req.servername)*2));
117
118         status = dfs_server_ad_get_referrals(data->lp_ctx,
119                                              data->sam_ctx,
120                                              handle->conn->sconn->remote_address,
121                                              r);
122         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
123                 return SMB_VFS_NEXT_GET_DFS_REFERRALS(handle, r);
124         }
125         if (!NT_STATUS_IS_OK(status)) {
126                 return status;
127         }
128
129         return NT_STATUS_OK;
130 }
131
132 static struct vfs_fn_pointers vfs_dfs_samba4_fns = {
133         .connect_fn = dfs_samba4_connect,
134         .disconnect = dfs_samba4_disconnect,
135         .get_dfs_referrals = dfs_samba4_get_referrals,
136 };
137
138 NTSTATUS vfs_dfs_samba4_init(void);
139 NTSTATUS vfs_dfs_samba4_init(void)
140 {
141         NTSTATUS ret;
142
143         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dfs_samba4",
144                                &vfs_dfs_samba4_fns);
145         if (!NT_STATUS_IS_OK(ret)) {
146                 return ret;
147         }
148
149         vfs_dfs_samba4_debug_level = debug_add_class("dfs_samba4");
150         if (vfs_dfs_samba4_debug_level == -1) {
151                 vfs_dfs_samba4_debug_level = DBGC_VFS;
152                 DEBUG(0, ("vfs_dfs_samba4: Couldn't register custom debugging class!\n"));
153         } else {
154                 DEBUG(10, ("vfs_dfs_samba4: Debug class number of 'fileid': %d\n",
155                       vfs_dfs_samba4_debug_level));
156         }
157
158         return ret;
159 }