4 * Copyright (c) International Business Machines Corp., 2003, 2007
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library 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
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
32 #include "cifs_unicode.h"
33 #include "cifs_ioctl.h"
35 #define MAX_EA_VALUE_SIZE CIFSMaxBufSize
36 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl" /* DACL only */
37 #define CIFS_XATTR_CIFS_NTSD "system.cifs_ntsd" /* owner plus DACL */
38 #define CIFS_XATTR_CIFS_NTSD_FULL "system.cifs_ntsd_full" /* owner/DACL/SACL */
39 #define CIFS_XATTR_ATTRIB "cifs.dosattrib" /* full name: user.cifs.dosattrib */
40 #define CIFS_XATTR_CREATETIME "cifs.creationtime" /* user.cifs.creationtime */
42 * Although these three are just aliases for the above, need to move away from
43 * confusing users and using the 20+ year old term 'cifs' when it is no longer
44 * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
46 #define SMB3_XATTR_CIFS_ACL "system.smb3_acl" /* DACL only */
47 #define SMB3_XATTR_CIFS_NTSD "system.smb3_ntsd" /* owner plus DACL */
48 #define SMB3_XATTR_CIFS_NTSD_FULL "system.smb3_ntsd_full" /* owner/DACL/SACL */
49 #define SMB3_XATTR_ATTRIB "smb3.dosattrib" /* full name: user.smb3.dosattrib */
50 #define SMB3_XATTR_CREATETIME "smb3.creationtime" /* user.smb3.creationtime */
51 /* BB need to add server (Samba e.g) support for security and trusted prefix */
53 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
54 XATTR_CIFS_NTSD, XATTR_CIFS_NTSD_FULL };
56 static int cifs_attrib_set(unsigned int xid, struct cifs_tcon *pTcon,
57 struct inode *inode, const char *full_path,
58 const void *value, size_t size)
60 ssize_t rc = -EOPNOTSUPP;
61 __u32 *pattrib = (__u32 *)value;
63 FILE_BASIC_INFO info_buf;
65 if ((value == NULL) || (size != sizeof(__u32)))
68 memset(&info_buf, 0, sizeof(info_buf));
70 info_buf.Attributes = cpu_to_le32(attrib);
71 if (pTcon->ses->server->ops->set_file_info)
72 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
75 CIFS_I(inode)->cifsAttrs = attrib;
80 static int cifs_creation_time_set(unsigned int xid, struct cifs_tcon *pTcon,
81 struct inode *inode, const char *full_path,
82 const void *value, size_t size)
84 ssize_t rc = -EOPNOTSUPP;
85 __u64 *pcreation_time = (__u64 *)value;
87 FILE_BASIC_INFO info_buf;
89 if ((value == NULL) || (size != sizeof(__u64)))
92 memset(&info_buf, 0, sizeof(info_buf));
93 creation_time = *pcreation_time;
94 info_buf.CreationTime = cpu_to_le64(creation_time);
95 if (pTcon->ses->server->ops->set_file_info)
96 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
99 CIFS_I(inode)->createtime = creation_time;
104 static int cifs_xattr_set(const struct xattr_handler *handler,
105 struct user_namespace *mnt_userns,
106 struct dentry *dentry, struct inode *inode,
107 const char *name, const void *value,
108 size_t size, int flags)
110 int rc = -EOPNOTSUPP;
112 struct super_block *sb = dentry->d_sb;
113 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
114 struct tcon_link *tlink;
115 struct cifs_tcon *pTcon;
116 const char *full_path;
119 tlink = cifs_sb_tlink(cifs_sb);
121 return PTR_ERR(tlink);
122 pTcon = tlink_tcon(tlink);
125 page = alloc_dentry_path();
127 full_path = build_path_from_dentry(dentry, page);
128 if (IS_ERR(full_path)) {
129 rc = PTR_ERR(full_path);
132 /* return dos attributes as pseudo xattr */
133 /* return alt name if available as pseudo attr */
135 /* if proc/fs/cifs/streamstoxattr is set then
136 search server for EAs or streams to
138 if (size > MAX_EA_VALUE_SIZE) {
139 cifs_dbg(FYI, "size of EA value too large\n");
144 switch (handler->flags) {
146 cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
147 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
148 (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
149 rc = cifs_attrib_set(xid, pTcon, inode, full_path,
151 if (rc == 0) /* force revalidate of the inode */
152 CIFS_I(inode)->time = 0;
154 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
155 (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
156 rc = cifs_creation_time_set(xid, pTcon, inode,
157 full_path, value, size);
158 if (rc == 0) /* force revalidate of the inode */
159 CIFS_I(inode)->time = 0;
163 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
166 if (pTcon->ses->server->ops->set_EA)
167 rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
168 full_path, name, value, (__u16)size,
169 cifs_sb->local_nls, cifs_sb);
173 case XATTR_CIFS_NTSD:
174 case XATTR_CIFS_NTSD_FULL: {
175 struct cifs_ntsd *pacl;
179 pacl = kmalloc(size, GFP_KERNEL);
183 memcpy(pacl, value, size);
184 if (pTcon->ses->server->ops->set_acl) {
188 switch (handler->flags) {
189 case XATTR_CIFS_NTSD_FULL:
190 aclflags = (CIFS_ACL_OWNER |
194 case XATTR_CIFS_NTSD:
195 aclflags = (CIFS_ACL_OWNER |
200 aclflags = CIFS_ACL_DACL;
203 rc = pTcon->ses->server->ops->set_acl(pacl,
204 size, inode, full_path, aclflags);
208 if (rc == 0) /* force revalidate of the inode */
209 CIFS_I(inode)->time = 0;
215 case XATTR_ACL_ACCESS:
216 #ifdef CONFIG_CIFS_POSIX
219 if (sb->s_flags & SB_POSIXACL)
220 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
221 value, (const int)size,
222 ACL_TYPE_ACCESS, cifs_sb->local_nls,
223 cifs_remap(cifs_sb));
224 #endif /* CONFIG_CIFS_POSIX */
227 case XATTR_ACL_DEFAULT:
228 #ifdef CONFIG_CIFS_POSIX
231 if (sb->s_flags & SB_POSIXACL)
232 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
233 value, (const int)size,
234 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
235 cifs_remap(cifs_sb));
236 #endif /* CONFIG_CIFS_POSIX */
241 free_dentry_path(page);
243 cifs_put_tlink(tlink);
247 static int cifs_attrib_get(struct dentry *dentry,
248 struct inode *inode, void *value,
254 rc = cifs_revalidate_dentry_attr(dentry);
259 if ((value == NULL) || (size == 0))
260 return sizeof(__u32);
261 else if (size < sizeof(__u32))
264 /* return dos attributes as pseudo xattr */
265 pattribute = (__u32 *)value;
266 *pattribute = CIFS_I(inode)->cifsAttrs;
268 return sizeof(__u32);
271 static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
272 void *value, size_t size)
277 rc = cifs_revalidate_dentry_attr(dentry);
281 if ((value == NULL) || (size == 0))
282 return sizeof(__u64);
283 else if (size < sizeof(__u64))
286 /* return dos attributes as pseudo xattr */
287 pcreatetime = (__u64 *)value;
288 *pcreatetime = CIFS_I(inode)->createtime;
289 return sizeof(__u64);
293 static int cifs_xattr_get(const struct xattr_handler *handler,
294 struct dentry *dentry, struct inode *inode,
295 const char *name, void *value, size_t size)
297 ssize_t rc = -EOPNOTSUPP;
299 struct super_block *sb = dentry->d_sb;
300 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
301 struct tcon_link *tlink;
302 struct cifs_tcon *pTcon;
303 const char *full_path;
306 tlink = cifs_sb_tlink(cifs_sb);
308 return PTR_ERR(tlink);
309 pTcon = tlink_tcon(tlink);
312 page = alloc_dentry_path();
314 full_path = build_path_from_dentry(dentry, page);
315 if (IS_ERR(full_path)) {
316 rc = PTR_ERR(full_path);
320 /* return alt name if available as pseudo attr */
321 switch (handler->flags) {
323 cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
324 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
325 (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
326 rc = cifs_attrib_get(dentry, inode, value, size);
328 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
329 (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
330 rc = cifs_creation_time_get(dentry, inode, value, size);
334 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
337 if (pTcon->ses->server->ops->query_all_EAs)
338 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
339 full_path, name, value, size, cifs_sb);
343 case XATTR_CIFS_NTSD:
344 case XATTR_CIFS_NTSD_FULL: {
346 * fetch owner, DACL, and SACL if asked for full descriptor,
347 * fetch owner and DACL otherwise
349 u32 acllen, extra_info;
350 struct cifs_ntsd *pacl;
352 if (pTcon->ses->server->ops->get_acl == NULL)
353 goto out; /* rc already EOPNOTSUPP */
355 if (handler->flags == XATTR_CIFS_NTSD_FULL) {
356 extra_info = SACL_SECINFO;
360 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
361 inode, full_path, &acllen, extra_info);
364 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
371 memcpy(value, pacl, acllen);
379 case XATTR_ACL_ACCESS:
380 #ifdef CONFIG_CIFS_POSIX
381 if (sb->s_flags & SB_POSIXACL)
382 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
383 value, size, ACL_TYPE_ACCESS,
385 cifs_remap(cifs_sb));
386 #endif /* CONFIG_CIFS_POSIX */
389 case XATTR_ACL_DEFAULT:
390 #ifdef CONFIG_CIFS_POSIX
391 if (sb->s_flags & SB_POSIXACL)
392 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
393 value, size, ACL_TYPE_DEFAULT,
395 cifs_remap(cifs_sb));
396 #endif /* CONFIG_CIFS_POSIX */
400 /* We could add an additional check for streams ie
401 if proc/fs/cifs/streamstoxattr is set then
402 search server for EAs or streams to
409 free_dentry_path(page);
411 cifs_put_tlink(tlink);
415 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
417 ssize_t rc = -EOPNOTSUPP;
419 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
420 struct tcon_link *tlink;
421 struct cifs_tcon *pTcon;
422 const char *full_path;
425 if (unlikely(cifs_forced_shutdown(cifs_sb)))
428 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
431 tlink = cifs_sb_tlink(cifs_sb);
433 return PTR_ERR(tlink);
434 pTcon = tlink_tcon(tlink);
437 page = alloc_dentry_path();
439 full_path = build_path_from_dentry(direntry, page);
440 if (IS_ERR(full_path)) {
441 rc = PTR_ERR(full_path);
444 /* return dos attributes as pseudo xattr */
445 /* return alt name if available as pseudo attr */
447 /* if proc/fs/cifs/streamstoxattr is set then
448 search server for EAs or streams to
451 if (pTcon->ses->server->ops->query_all_EAs)
452 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
453 full_path, NULL, data, buf_size, cifs_sb);
455 free_dentry_path(page);
457 cifs_put_tlink(tlink);
461 static const struct xattr_handler cifs_user_xattr_handler = {
462 .prefix = XATTR_USER_PREFIX,
464 .get = cifs_xattr_get,
465 .set = cifs_xattr_set,
468 /* os2.* attributes are treated like user.* attributes */
469 static const struct xattr_handler cifs_os2_xattr_handler = {
470 .prefix = XATTR_OS2_PREFIX,
472 .get = cifs_xattr_get,
473 .set = cifs_xattr_set,
476 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
477 .name = CIFS_XATTR_CIFS_ACL,
478 .flags = XATTR_CIFS_ACL,
479 .get = cifs_xattr_get,
480 .set = cifs_xattr_set,
484 * Although this is just an alias for the above, need to move away from
485 * confusing users and using the 20 year old term 'cifs' when it is no
486 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
487 * SMB3 and later are highly secure.
489 static const struct xattr_handler smb3_acl_xattr_handler = {
490 .name = SMB3_XATTR_CIFS_ACL,
491 .flags = XATTR_CIFS_ACL,
492 .get = cifs_xattr_get,
493 .set = cifs_xattr_set,
496 static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
497 .name = CIFS_XATTR_CIFS_NTSD,
498 .flags = XATTR_CIFS_NTSD,
499 .get = cifs_xattr_get,
500 .set = cifs_xattr_set,
504 * Although this is just an alias for the above, need to move away from
505 * confusing users and using the 20 year old term 'cifs' when it is no
506 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
507 * SMB3 and later are highly secure.
509 static const struct xattr_handler smb3_ntsd_xattr_handler = {
510 .name = SMB3_XATTR_CIFS_NTSD,
511 .flags = XATTR_CIFS_NTSD,
512 .get = cifs_xattr_get,
513 .set = cifs_xattr_set,
516 static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
517 .name = CIFS_XATTR_CIFS_NTSD_FULL,
518 .flags = XATTR_CIFS_NTSD_FULL,
519 .get = cifs_xattr_get,
520 .set = cifs_xattr_set,
524 * Although this is just an alias for the above, need to move away from
525 * confusing users and using the 20 year old term 'cifs' when it is no
526 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
527 * SMB3 and later are highly secure.
529 static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
530 .name = SMB3_XATTR_CIFS_NTSD_FULL,
531 .flags = XATTR_CIFS_NTSD_FULL,
532 .get = cifs_xattr_get,
533 .set = cifs_xattr_set,
537 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
538 .name = XATTR_NAME_POSIX_ACL_ACCESS,
539 .flags = XATTR_ACL_ACCESS,
540 .get = cifs_xattr_get,
541 .set = cifs_xattr_set,
544 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
545 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
546 .flags = XATTR_ACL_DEFAULT,
547 .get = cifs_xattr_get,
548 .set = cifs_xattr_set,
551 const struct xattr_handler *cifs_xattr_handlers[] = {
552 &cifs_user_xattr_handler,
553 &cifs_os2_xattr_handler,
554 &cifs_cifs_acl_xattr_handler,
555 &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
556 &cifs_cifs_ntsd_xattr_handler,
557 &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
558 &cifs_cifs_ntsd_full_xattr_handler,
559 &smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
560 &cifs_posix_acl_access_xattr_handler,
561 &cifs_posix_acl_default_xattr_handler,