4 * Copyright (c) International Business Machines Corp., 2003, 2007
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"
34 #define MAX_EA_VALUE_SIZE CIFSMaxBufSize
35 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl" /* DACL only */
36 #define CIFS_XATTR_CIFS_NTSD "system.cifs_ntsd" /* owner plus DACL */
37 #define CIFS_XATTR_CIFS_NTSD_FULL "system.cifs_ntsd_full" /* owner/DACL/SACL */
38 #define CIFS_XATTR_ATTRIB "cifs.dosattrib" /* full name: user.cifs.dosattrib */
39 #define CIFS_XATTR_CREATETIME "cifs.creationtime" /* user.cifs.creationtime */
41 * Although these three are just aliases for the above, need to move away from
42 * confusing users and using the 20+ year old term 'cifs' when it is no longer
43 * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
45 #define SMB3_XATTR_CIFS_ACL "system.smb3_acl" /* DACL only */
46 #define SMB3_XATTR_CIFS_NTSD "system.smb3_ntsd" /* owner plus DACL */
47 #define SMB3_XATTR_CIFS_NTSD_FULL "system.smb3_ntsd_full" /* owner/DACL/SACL */
48 #define SMB3_XATTR_ATTRIB "smb3.dosattrib" /* full name: user.smb3.dosattrib */
49 #define SMB3_XATTR_CREATETIME "smb3.creationtime" /* user.smb3.creationtime */
50 /* BB need to add server (Samba e.g) support for security and trusted prefix */
52 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
53 XATTR_CIFS_NTSD, XATTR_CIFS_NTSD_FULL };
55 static int cifs_attrib_set(unsigned int xid, struct cifs_tcon *pTcon,
56 struct inode *inode, char *full_path,
57 const void *value, size_t size)
59 ssize_t rc = -EOPNOTSUPP;
60 __u32 *pattrib = (__u32 *)value;
62 FILE_BASIC_INFO info_buf;
64 if ((value == NULL) || (size != sizeof(__u32)))
67 memset(&info_buf, 0, sizeof(info_buf));
69 info_buf.Attributes = cpu_to_le32(attrib);
70 if (pTcon->ses->server->ops->set_file_info)
71 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
74 CIFS_I(inode)->cifsAttrs = attrib;
79 static int cifs_creation_time_set(unsigned int xid, struct cifs_tcon *pTcon,
80 struct inode *inode, char *full_path,
81 const void *value, size_t size)
83 ssize_t rc = -EOPNOTSUPP;
84 __u64 *pcreation_time = (__u64 *)value;
86 FILE_BASIC_INFO info_buf;
88 if ((value == NULL) || (size != sizeof(__u64)))
91 memset(&info_buf, 0, sizeof(info_buf));
92 creation_time = *pcreation_time;
93 info_buf.CreationTime = cpu_to_le64(creation_time);
94 if (pTcon->ses->server->ops->set_file_info)
95 rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
98 CIFS_I(inode)->createtime = creation_time;
103 static int cifs_xattr_set(const struct xattr_handler *handler,
104 struct user_namespace *mnt_userns,
105 struct dentry *dentry, struct inode *inode,
106 const char *name, const void *value,
107 size_t size, int flags)
109 int rc = -EOPNOTSUPP;
111 struct super_block *sb = dentry->d_sb;
112 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
113 struct tcon_link *tlink;
114 struct cifs_tcon *pTcon;
117 tlink = cifs_sb_tlink(cifs_sb);
119 return PTR_ERR(tlink);
120 pTcon = tlink_tcon(tlink);
124 full_path = build_path_from_dentry(dentry);
125 if (full_path == NULL) {
129 /* return dos attributes as pseudo xattr */
130 /* return alt name if available as pseudo attr */
132 /* if proc/fs/cifs/streamstoxattr is set then
133 search server for EAs or streams to
135 if (size > MAX_EA_VALUE_SIZE) {
136 cifs_dbg(FYI, "size of EA value too large\n");
141 switch (handler->flags) {
143 cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
144 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
145 (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
146 rc = cifs_attrib_set(xid, pTcon, inode, full_path,
148 if (rc == 0) /* force revalidate of the inode */
149 CIFS_I(inode)->time = 0;
151 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
152 (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
153 rc = cifs_creation_time_set(xid, pTcon, inode,
154 full_path, value, size);
155 if (rc == 0) /* force revalidate of the inode */
156 CIFS_I(inode)->time = 0;
160 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
163 if (pTcon->ses->server->ops->set_EA)
164 rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
165 full_path, name, value, (__u16)size,
166 cifs_sb->local_nls, cifs_sb);
170 case XATTR_CIFS_NTSD:
171 case XATTR_CIFS_NTSD_FULL: {
172 struct cifs_ntsd *pacl;
176 pacl = kmalloc(size, GFP_KERNEL);
180 memcpy(pacl, value, size);
181 if (pTcon->ses->server->ops->set_acl) {
185 switch (handler->flags) {
186 case XATTR_CIFS_NTSD_FULL:
187 aclflags = (CIFS_ACL_OWNER |
191 case XATTR_CIFS_NTSD:
192 aclflags = (CIFS_ACL_OWNER |
197 aclflags = CIFS_ACL_DACL;
200 rc = pTcon->ses->server->ops->set_acl(pacl,
201 size, inode, full_path, aclflags);
205 if (rc == 0) /* force revalidate of the inode */
206 CIFS_I(inode)->time = 0;
212 case XATTR_ACL_ACCESS:
213 #ifdef CONFIG_CIFS_POSIX
216 if (sb->s_flags & SB_POSIXACL)
217 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
218 value, (const int)size,
219 ACL_TYPE_ACCESS, cifs_sb->local_nls,
220 cifs_remap(cifs_sb));
221 #endif /* CONFIG_CIFS_POSIX */
224 case XATTR_ACL_DEFAULT:
225 #ifdef CONFIG_CIFS_POSIX
228 if (sb->s_flags & SB_POSIXACL)
229 rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
230 value, (const int)size,
231 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
232 cifs_remap(cifs_sb));
233 #endif /* CONFIG_CIFS_POSIX */
240 cifs_put_tlink(tlink);
244 static int cifs_attrib_get(struct dentry *dentry,
245 struct inode *inode, void *value,
251 rc = cifs_revalidate_dentry_attr(dentry);
256 if ((value == NULL) || (size == 0))
257 return sizeof(__u32);
258 else if (size < sizeof(__u32))
261 /* return dos attributes as pseudo xattr */
262 pattribute = (__u32 *)value;
263 *pattribute = CIFS_I(inode)->cifsAttrs;
265 return sizeof(__u32);
268 static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
269 void *value, size_t size)
274 rc = cifs_revalidate_dentry_attr(dentry);
278 if ((value == NULL) || (size == 0))
279 return sizeof(__u64);
280 else if (size < sizeof(__u64))
283 /* return dos attributes as pseudo xattr */
284 pcreatetime = (__u64 *)value;
285 *pcreatetime = CIFS_I(inode)->createtime;
286 return sizeof(__u64);
290 static int cifs_xattr_get(const struct xattr_handler *handler,
291 struct dentry *dentry, struct inode *inode,
292 const char *name, void *value, size_t size)
294 ssize_t rc = -EOPNOTSUPP;
296 struct super_block *sb = dentry->d_sb;
297 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
298 struct tcon_link *tlink;
299 struct cifs_tcon *pTcon;
302 tlink = cifs_sb_tlink(cifs_sb);
304 return PTR_ERR(tlink);
305 pTcon = tlink_tcon(tlink);
309 full_path = build_path_from_dentry(dentry);
310 if (full_path == NULL) {
315 /* return alt name if available as pseudo attr */
316 switch (handler->flags) {
318 cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
319 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
320 (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
321 rc = cifs_attrib_get(dentry, inode, value, size);
323 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
324 (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
325 rc = cifs_creation_time_get(dentry, inode, value, size);
329 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
332 if (pTcon->ses->server->ops->query_all_EAs)
333 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
334 full_path, name, value, size, cifs_sb);
338 case XATTR_CIFS_NTSD:
339 case XATTR_CIFS_NTSD_FULL: {
341 * fetch owner, DACL, and SACL if asked for full descriptor,
342 * fetch owner and DACL otherwise
344 u32 acllen, extra_info;
345 struct cifs_ntsd *pacl;
347 if (pTcon->ses->server->ops->get_acl == NULL)
348 goto out; /* rc already EOPNOTSUPP */
350 if (handler->flags == XATTR_CIFS_NTSD_FULL) {
351 extra_info = SACL_SECINFO;
355 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
356 inode, full_path, &acllen, extra_info);
359 cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
366 memcpy(value, pacl, acllen);
374 case XATTR_ACL_ACCESS:
375 #ifdef CONFIG_CIFS_POSIX
376 if (sb->s_flags & SB_POSIXACL)
377 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
378 value, size, ACL_TYPE_ACCESS,
380 cifs_remap(cifs_sb));
381 #endif /* CONFIG_CIFS_POSIX */
384 case XATTR_ACL_DEFAULT:
385 #ifdef CONFIG_CIFS_POSIX
386 if (sb->s_flags & SB_POSIXACL)
387 rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
388 value, size, ACL_TYPE_DEFAULT,
390 cifs_remap(cifs_sb));
391 #endif /* CONFIG_CIFS_POSIX */
395 /* We could add an additional check for streams ie
396 if proc/fs/cifs/streamstoxattr is set then
397 search server for EAs or streams to
406 cifs_put_tlink(tlink);
410 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
412 ssize_t rc = -EOPNOTSUPP;
414 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
415 struct tcon_link *tlink;
416 struct cifs_tcon *pTcon;
419 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
422 tlink = cifs_sb_tlink(cifs_sb);
424 return PTR_ERR(tlink);
425 pTcon = tlink_tcon(tlink);
429 full_path = build_path_from_dentry(direntry);
430 if (full_path == NULL) {
434 /* return dos attributes as pseudo xattr */
435 /* return alt name if available as pseudo attr */
437 /* if proc/fs/cifs/streamstoxattr is set then
438 search server for EAs or streams to
441 if (pTcon->ses->server->ops->query_all_EAs)
442 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
443 full_path, NULL, data, buf_size, cifs_sb);
447 cifs_put_tlink(tlink);
451 static const struct xattr_handler cifs_user_xattr_handler = {
452 .prefix = XATTR_USER_PREFIX,
454 .get = cifs_xattr_get,
455 .set = cifs_xattr_set,
458 /* os2.* attributes are treated like user.* attributes */
459 static const struct xattr_handler cifs_os2_xattr_handler = {
460 .prefix = XATTR_OS2_PREFIX,
462 .get = cifs_xattr_get,
463 .set = cifs_xattr_set,
466 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
467 .name = CIFS_XATTR_CIFS_ACL,
468 .flags = XATTR_CIFS_ACL,
469 .get = cifs_xattr_get,
470 .set = cifs_xattr_set,
474 * Although this is just an alias for the above, need to move away from
475 * confusing users and using the 20 year old term 'cifs' when it is no
476 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
477 * SMB3 and later are highly secure.
479 static const struct xattr_handler smb3_acl_xattr_handler = {
480 .name = SMB3_XATTR_CIFS_ACL,
481 .flags = XATTR_CIFS_ACL,
482 .get = cifs_xattr_get,
483 .set = cifs_xattr_set,
486 static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
487 .name = CIFS_XATTR_CIFS_NTSD,
488 .flags = XATTR_CIFS_NTSD,
489 .get = cifs_xattr_get,
490 .set = cifs_xattr_set,
494 * Although this is just an alias for the above, need to move away from
495 * confusing users and using the 20 year old term 'cifs' when it is no
496 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
497 * SMB3 and later are highly secure.
499 static const struct xattr_handler smb3_ntsd_xattr_handler = {
500 .name = SMB3_XATTR_CIFS_NTSD,
501 .flags = XATTR_CIFS_NTSD,
502 .get = cifs_xattr_get,
503 .set = cifs_xattr_set,
506 static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
507 .name = CIFS_XATTR_CIFS_NTSD_FULL,
508 .flags = XATTR_CIFS_NTSD_FULL,
509 .get = cifs_xattr_get,
510 .set = cifs_xattr_set,
514 * Although this is just an alias for the above, need to move away from
515 * confusing users and using the 20 year old term 'cifs' when it is no
516 * longer secure and was replaced by SMB2/SMB3 a long time ago, and
517 * SMB3 and later are highly secure.
519 static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
520 .name = SMB3_XATTR_CIFS_NTSD_FULL,
521 .flags = XATTR_CIFS_NTSD_FULL,
522 .get = cifs_xattr_get,
523 .set = cifs_xattr_set,
527 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
528 .name = XATTR_NAME_POSIX_ACL_ACCESS,
529 .flags = XATTR_ACL_ACCESS,
530 .get = cifs_xattr_get,
531 .set = cifs_xattr_set,
534 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
535 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
536 .flags = XATTR_ACL_DEFAULT,
537 .get = cifs_xattr_get,
538 .set = cifs_xattr_set,
541 const struct xattr_handler *cifs_xattr_handlers[] = {
542 &cifs_user_xattr_handler,
543 &cifs_os2_xattr_handler,
544 &cifs_cifs_acl_xattr_handler,
545 &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
546 &cifs_cifs_ntsd_xattr_handler,
547 &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
548 &cifs_cifs_ntsd_full_xattr_handler,
549 &smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
550 &cifs_posix_acl_access_xattr_handler,
551 &cifs_posix_acl_default_xattr_handler,