selftest: explicitly set "client min protocol = CORE"
[Samba.git] / source3 / modules / vfs_acl_xattr.c
blobb8190fd4b1cbf8c1fc51c90c5420e105531cf599
1 /*
2 * Store Windows ACLs in xattrs.
4 * Copyright (C) Volker Lendecke, 2008
5 * Copyright (C) Jeremy Allison, 2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "librpc/gen_ndr/xattr.h"
24 #include "auth.h"
25 #include "vfs_acl_common.h"
27 /* Pull in the common functions. */
28 #define ACL_MODULE_NAME "acl_xattr"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_VFS
33 /*******************************************************************
34 Pull a security descriptor into a DATA_BLOB from a xattr.
35 *******************************************************************/
37 static ssize_t getxattr_do(vfs_handle_struct *handle,
38 files_struct *fsp,
39 const struct smb_filename *smb_fname,
40 const char *xattr_name,
41 uint8_t *val,
42 size_t size)
44 ssize_t sizeret;
45 int saved_errno = 0;
47 become_root();
48 if (fsp && fsp->fh->fd != -1) {
49 sizeret = SMB_VFS_FGETXATTR(fsp, xattr_name, val, size);
50 } else {
51 sizeret = SMB_VFS_GETXATTR(handle->conn, smb_fname,
52 XATTR_NTACL_NAME, val, size);
54 if (sizeret == -1) {
55 saved_errno = errno;
57 unbecome_root();
59 if (saved_errno != 0) {
60 errno = saved_errno;
63 return sizeret;
66 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
67 vfs_handle_struct *handle,
68 files_struct *fsp,
69 const struct smb_filename *smb_fname,
70 DATA_BLOB *pblob)
72 size_t size = 4096;
73 uint8_t *val = NULL;
74 uint8_t *tmp;
75 ssize_t sizeret;
77 ZERO_STRUCTP(pblob);
79 again:
81 tmp = talloc_realloc(ctx, val, uint8_t, size);
82 if (tmp == NULL) {
83 TALLOC_FREE(val);
84 return NT_STATUS_NO_MEMORY;
86 val = tmp;
88 sizeret =
89 getxattr_do(handle, fsp, smb_fname, XATTR_NTACL_NAME, val, size);
91 if (sizeret >= 0) {
92 pblob->data = val;
93 pblob->length = sizeret;
94 return NT_STATUS_OK;
97 if (errno != ERANGE) {
98 goto err;
101 /* Too small, try again. */
102 sizeret =
103 getxattr_do(handle, fsp, smb_fname, XATTR_NTACL_NAME, NULL, 0);
104 if (sizeret < 0) {
105 goto err;
108 if (size < sizeret) {
109 size = sizeret;
112 if (size > 65536) {
113 /* Max ACL size is 65536 bytes. */
114 errno = ERANGE;
115 goto err;
118 goto again;
119 err:
120 /* Real error - exit here. */
121 TALLOC_FREE(val);
122 return map_nt_error_from_unix(errno);
125 /*******************************************************************
126 Store a DATA_BLOB into an xattr given an fsp pointer.
127 *******************************************************************/
129 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
130 files_struct *fsp,
131 DATA_BLOB *pblob)
133 int ret;
134 int saved_errno = 0;
136 DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
137 (unsigned int)pblob->length, fsp_str_dbg(fsp)));
139 become_root();
140 if (fsp->fh->fd != -1) {
141 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
142 pblob->data, pblob->length, 0);
143 } else {
144 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name,
145 XATTR_NTACL_NAME,
146 pblob->data, pblob->length, 0);
148 if (ret) {
149 saved_errno = errno;
151 unbecome_root();
152 if (ret) {
153 DEBUG(5, ("store_acl_blob_fsp: setting attr failed for file %s"
154 "with error %s\n",
155 fsp_str_dbg(fsp),
156 strerror(saved_errno) ));
157 errno = saved_errno;
158 return map_nt_error_from_unix(saved_errno);
160 return NT_STATUS_OK;
163 /*********************************************************************
164 Remove a Windows ACL - we're setting the underlying POSIX ACL.
165 *********************************************************************/
167 static int sys_acl_set_file_xattr(vfs_handle_struct *handle,
168 const struct smb_filename *smb_fname,
169 SMB_ACL_TYPE_T type,
170 SMB_ACL_T theacl)
172 int ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
173 smb_fname,
174 type,
175 theacl);
176 if (ret == -1) {
177 return -1;
180 become_root();
181 SMB_VFS_REMOVEXATTR(handle->conn, smb_fname,
182 XATTR_NTACL_NAME);
183 unbecome_root();
185 return ret;
188 /*********************************************************************
189 Remove a Windows ACL - we're setting the underlying POSIX ACL.
190 *********************************************************************/
192 static int sys_acl_set_fd_xattr(vfs_handle_struct *handle,
193 files_struct *fsp,
194 SMB_ACL_T theacl)
196 int ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
197 fsp,
198 theacl);
199 if (ret == -1) {
200 return -1;
203 become_root();
204 SMB_VFS_FREMOVEXATTR(fsp, XATTR_NTACL_NAME);
205 unbecome_root();
207 return ret;
210 static int connect_acl_xattr(struct vfs_handle_struct *handle,
211 const char *service,
212 const char *user)
214 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
215 bool ok;
216 struct acl_common_config *config = NULL;
218 if (ret < 0) {
219 return ret;
222 ok = init_acl_common_config(handle, ACL_MODULE_NAME);
223 if (!ok) {
224 DBG_ERR("init_acl_common_config failed\n");
225 return -1;
228 /* Ensure we have the parameters correct if we're
229 * using this module. */
230 DEBUG(2,("connect_acl_xattr: setting 'inherit acls = true' "
231 "'dos filemode = true' and "
232 "'force unknown acl user = true' for service %s\n",
233 service ));
235 lp_do_parameter(SNUM(handle->conn), "inherit acls", "true");
236 lp_do_parameter(SNUM(handle->conn), "dos filemode", "true");
237 lp_do_parameter(SNUM(handle->conn), "force unknown acl user", "true");
239 SMB_VFS_HANDLE_GET_DATA(handle, config,
240 struct acl_common_config,
241 return -1);
243 if (config->ignore_system_acls) {
244 mode_t create_mask = lp_create_mask(SNUM(handle->conn));
245 char *create_mask_str = NULL;
247 if ((create_mask & 0666) != 0666) {
248 create_mask |= 0666;
249 create_mask_str = talloc_asprintf(handle, "0%o",
250 create_mask);
251 if (create_mask_str == NULL) {
252 DBG_ERR("talloc_asprintf failed\n");
253 return -1;
256 DBG_NOTICE("setting 'create mask = %s'\n", create_mask_str);
258 lp_do_parameter (SNUM(handle->conn),
259 "create mask", create_mask_str);
261 TALLOC_FREE(create_mask_str);
264 DBG_NOTICE("setting 'directory mask = 0777', "
265 "'store dos attributes = yes' and all "
266 "'map ...' options to 'no'\n");
268 lp_do_parameter(SNUM(handle->conn), "directory mask", "0777");
269 lp_do_parameter(SNUM(handle->conn), "map archive", "no");
270 lp_do_parameter(SNUM(handle->conn), "map hidden", "no");
271 lp_do_parameter(SNUM(handle->conn), "map readonly", "no");
272 lp_do_parameter(SNUM(handle->conn), "map system", "no");
273 lp_do_parameter(SNUM(handle->conn), "store dos attributes",
274 "yes");
277 return 0;
280 static NTSTATUS acl_xattr_fget_nt_acl(vfs_handle_struct *handle,
281 files_struct *fsp,
282 uint32_t security_info,
283 TALLOC_CTX *mem_ctx,
284 struct security_descriptor **ppdesc)
286 NTSTATUS status;
287 status = get_nt_acl_common(get_acl_blob, handle, fsp, NULL,
288 security_info, mem_ctx, ppdesc);
289 return status;
292 static NTSTATUS acl_xattr_get_nt_acl(vfs_handle_struct *handle,
293 const struct smb_filename *smb_fname,
294 uint32_t security_info,
295 TALLOC_CTX *mem_ctx,
296 struct security_descriptor **ppdesc)
298 NTSTATUS status;
299 status = get_nt_acl_common(get_acl_blob, handle, NULL, smb_fname,
300 security_info, mem_ctx, ppdesc);
301 return status;
304 static NTSTATUS acl_xattr_fset_nt_acl(vfs_handle_struct *handle,
305 files_struct *fsp,
306 uint32_t security_info_sent,
307 const struct security_descriptor *psd)
309 NTSTATUS status;
310 status = fset_nt_acl_common(get_acl_blob, store_acl_blob_fsp,
311 ACL_MODULE_NAME,
312 handle, fsp, security_info_sent, psd);
313 return status;
316 static struct vfs_fn_pointers vfs_acl_xattr_fns = {
317 .connect_fn = connect_acl_xattr,
318 .rmdir_fn = rmdir_acl_common,
319 .unlink_fn = unlink_acl_common,
320 .chmod_fn = chmod_acl_module_common,
321 .fchmod_fn = fchmod_acl_module_common,
322 .fget_nt_acl_fn = acl_xattr_fget_nt_acl,
323 .get_nt_acl_fn = acl_xattr_get_nt_acl,
324 .fset_nt_acl_fn = acl_xattr_fset_nt_acl,
325 .sys_acl_set_file_fn = sys_acl_set_file_xattr,
326 .sys_acl_set_fd_fn = sys_acl_set_fd_xattr
329 static_decl_vfs;
330 NTSTATUS vfs_acl_xattr_init(TALLOC_CTX *ctx)
332 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr",
333 &vfs_acl_xattr_fns);