CVE-2023-4154 dsdb/tests: Add test for SEARCH_FLAG_RODC_ATTRIBUTE behaviour
[Samba.git] / source3 / modules / vfs_dfs_samba4.c
blob8b4724f7b6bd59ada17da20c2d838f6dbe4c477d
1 /*
2 * VFS module to retrieve DFS referrals from AD
4 * Copyright (C) 2007, Stefan Metzmacher
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.
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.
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/>.
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"
32 static int vfs_dfs_samba4_debug_level = DBGC_VFS;
34 #undef DBGC_CLASS
35 #define DBGC_CLASS vfs_dfs_samba4_debug_level
37 struct dfs_samba4_handle_data {
38 struct tevent_context *ev;
39 struct loadparm_context *lp_ctx;
40 struct ldb_context *sam_ctx;
43 static int dfs_samba4_connect(struct vfs_handle_struct *handle,
44 const char *service, const char *user)
46 struct dfs_samba4_handle_data *data;
47 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
49 if (ret < 0) {
50 return ret;
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;
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;
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;
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;
86 SMB_VFS_HANDLE_SET_DATA(handle, data, NULL,
87 struct dfs_samba4_handle_data,
88 return -1);
90 DEBUG(10,("dfs_samba4: connect to service[%s]\n",
91 service));
93 return 0;
96 static void dfs_samba4_disconnect(struct vfs_handle_struct *handle)
98 const struct loadparm_substitution *lp_sub =
99 loadparm_s3_global_substitution();
101 DEBUG(10,("dfs_samba4_disconnect() connect to service[%s].\n",
102 lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn))));
104 SMB_VFS_NEXT_DISCONNECT(handle);
107 static NTSTATUS dfs_samba4_get_referrals(struct vfs_handle_struct *handle,
108 struct dfs_GetDFSReferral *r)
110 struct dfs_samba4_handle_data *data;
111 NTSTATUS status;
113 SMB_VFS_HANDLE_GET_DATA(handle, data,
114 struct dfs_samba4_handle_data,
115 return NT_STATUS_INTERNAL_ERROR);
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));
121 status = dfs_server_ad_get_referrals(data->lp_ctx,
122 data->sam_ctx,
123 handle->conn->sconn->remote_address,
125 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
126 return SMB_VFS_NEXT_GET_DFS_REFERRALS(handle, r);
128 if (!NT_STATUS_IS_OK(status)) {
129 return status;
132 return NT_STATUS_OK;
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,
141 static_decl_vfs;
142 NTSTATUS vfs_dfs_samba4_init(TALLOC_CTX *ctx)
144 NTSTATUS ret;
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;
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));
161 return ret;