s3:libsmb: allow store_cldap_reply() to work with a ipv6 response
[samba.git] / source3 / modules / vfs_dfs_samba4.c
1 /*
2  * VFS module to retrieve DFS referrals from AD
3  *
4  * Copyright (C) 2007, Stefan Metzmacher
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "system/filesys.h"
24 #include "source3/include/msdfs.h"
25 #include "librpc/gen_ndr/ndr_dfsblobs.h"
26 #include "source4/lib/events/events.h"
27 #include "source4/auth/session.h"
28 #include "lib/param/param.h"
29 #include "source4/dsdb/samdb/samdb.h"
30 #include "dfs_server/dfs_server_ad.h"
31
32 static int vfs_dfs_samba4_debug_level = DBGC_VFS;
33
34 #undef DBGC_CLASS
35 #define DBGC_CLASS vfs_dfs_samba4_debug_level
36
37 struct dfs_samba4_handle_data {
38         struct tevent_context *ev;
39         struct loadparm_context *lp_ctx;
40         struct ldb_context *sam_ctx;
41 };
42
43 static int dfs_samba4_connect(struct vfs_handle_struct *handle,
44                               const char *service, const char *user)
45 {
46         struct dfs_samba4_handle_data *data;
47         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
48
49         if (ret < 0) {
50                 return ret;
51         }
52
53         data = talloc_zero(handle->conn, struct dfs_samba4_handle_data);
54         if (!data) {
55                 DEBUG(0, ("talloc_zero() failed\n"));
56                 SMB_VFS_NEXT_DISCONNECT(handle);
57                 return -1;
58         }
59
60         data->ev = s4_event_context_init(data);
61         if (!data->ev) {
62                 DEBUG(0, ("s4_event_context_init failed\n"));
63                 SMB_VFS_NEXT_DISCONNECT(handle);
64                 return -1;
65         }
66
67         data->lp_ctx = loadparm_init_s3(data, loadparm_s3_helpers());
68         if (data->lp_ctx == NULL) {
69                 DEBUG(0, ("loadparm_init_s3 failed\n"));
70                 SMB_VFS_NEXT_DISCONNECT(handle);
71                 return -1;
72         }
73
74         data->sam_ctx = samdb_connect(data,
75                                       data->ev,
76                                       data->lp_ctx,
77                                       system_session(data->lp_ctx),
78                                       NULL,
79                                       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         const struct loadparm_substitution *lp_sub =
99                 loadparm_s3_global_substitution();
100
101         DEBUG(10,("dfs_samba4_disconnect() connect to service[%s].\n",
102                   lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn))));
103
104         SMB_VFS_NEXT_DISCONNECT(handle);
105 }
106
107 static NTSTATUS dfs_samba4_get_referrals(struct vfs_handle_struct *handle,
108                                          struct dfs_GetDFSReferral *r)
109 {
110         struct dfs_samba4_handle_data *data;
111         NTSTATUS status;
112
113         SMB_VFS_HANDLE_GET_DATA(handle, data,
114                                 struct dfs_samba4_handle_data,
115                                 return NT_STATUS_INTERNAL_ERROR);
116
117         DEBUG(8, ("dfs_samba4: Requested DFS name: %s utf16-length: %u\n",
118                    r->in.req.servername,
119                    (unsigned int)strlen_m(r->in.req.servername)*2));
120
121         status = dfs_server_ad_get_referrals(data->lp_ctx,
122                                              data->sam_ctx,
123                                              handle->conn->sconn->remote_address,
124                                              r);
125         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
126                 return SMB_VFS_NEXT_GET_DFS_REFERRALS(handle, r);
127         }
128         if (!NT_STATUS_IS_OK(status)) {
129                 return status;
130         }
131
132         return NT_STATUS_OK;
133 }
134
135 static struct vfs_fn_pointers vfs_dfs_samba4_fns = {
136         .connect_fn = dfs_samba4_connect,
137         .disconnect_fn = dfs_samba4_disconnect,
138         .get_dfs_referrals_fn = dfs_samba4_get_referrals,
139 };
140
141 static_decl_vfs;
142 NTSTATUS vfs_dfs_samba4_init(TALLOC_CTX *ctx)
143 {
144         NTSTATUS ret;
145
146         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "dfs_samba4",
147                                &vfs_dfs_samba4_fns);
148         if (!NT_STATUS_IS_OK(ret)) {
149                 return ret;
150         }
151
152         vfs_dfs_samba4_debug_level = debug_add_class("dfs_samba4");
153         if (vfs_dfs_samba4_debug_level == -1) {
154                 vfs_dfs_samba4_debug_level = DBGC_VFS;
155                 DEBUG(0, ("vfs_dfs_samba4: Couldn't register custom debugging class!\n"));
156         } else {
157                 DEBUG(10, ("vfs_dfs_samba4: Debug class number of 'fileid': %d\n",
158                       vfs_dfs_samba4_debug_level));
159         }
160
161         return ret;
162 }