Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next
[linux-2.6-microblaze.git] / fs / cifs / xattr.c
1 /*
2  *   fs/cifs/xattr.c
3  *
4  *   Copyright (c) International Business Machines  Corp., 2003, 2007
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <linux/fs.h>
23 #include <linux/posix_acl_xattr.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
26 #include "cifsfs.h"
27 #include "cifspdu.h"
28 #include "cifsglob.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
32 #include "cifs_unicode.h"
33
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 */
40 /*
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
44  */
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 */
51
52 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
53         XATTR_CIFS_NTSD, XATTR_CIFS_NTSD_FULL };
54
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)
58 {
59         ssize_t rc = -EOPNOTSUPP;
60         __u32 *pattrib = (__u32 *)value;
61         __u32 attrib;
62         FILE_BASIC_INFO info_buf;
63
64         if ((value == NULL) || (size != sizeof(__u32)))
65                 return -ERANGE;
66
67         memset(&info_buf, 0, sizeof(info_buf));
68         attrib = *pattrib;
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,
72                                 &info_buf, xid);
73         if (rc == 0)
74                 CIFS_I(inode)->cifsAttrs = attrib;
75
76         return rc;
77 }
78
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)
82 {
83         ssize_t rc = -EOPNOTSUPP;
84         __u64 *pcreation_time = (__u64 *)value;
85         __u64 creation_time;
86         FILE_BASIC_INFO info_buf;
87
88         if ((value == NULL) || (size != sizeof(__u64)))
89                 return -ERANGE;
90
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,
96                                 &info_buf, xid);
97         if (rc == 0)
98                 CIFS_I(inode)->createtime = creation_time;
99
100         return rc;
101 }
102
103 static int cifs_xattr_set(const struct xattr_handler *handler,
104                           struct dentry *dentry, struct inode *inode,
105                           const char *name, const void *value,
106                           size_t size, int flags)
107 {
108         int rc = -EOPNOTSUPP;
109         unsigned int xid;
110         struct super_block *sb = dentry->d_sb;
111         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
112         struct tcon_link *tlink;
113         struct cifs_tcon *pTcon;
114         char *full_path;
115
116         tlink = cifs_sb_tlink(cifs_sb);
117         if (IS_ERR(tlink))
118                 return PTR_ERR(tlink);
119         pTcon = tlink_tcon(tlink);
120
121         xid = get_xid();
122
123         full_path = build_path_from_dentry(dentry);
124         if (full_path == NULL) {
125                 rc = -ENOMEM;
126                 goto out;
127         }
128         /* return dos attributes as pseudo xattr */
129         /* return alt name if available as pseudo attr */
130
131         /* if proc/fs/cifs/streamstoxattr is set then
132                 search server for EAs or streams to
133                 returns as xattrs */
134         if (size > MAX_EA_VALUE_SIZE) {
135                 cifs_dbg(FYI, "size of EA value too large\n");
136                 rc = -EOPNOTSUPP;
137                 goto out;
138         }
139
140         switch (handler->flags) {
141         case XATTR_USER:
142                 cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
143                 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
144                     (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
145                         rc = cifs_attrib_set(xid, pTcon, inode, full_path,
146                                         value, size);
147                         if (rc == 0) /* force revalidate of the inode */
148                                 CIFS_I(inode)->time = 0;
149                         break;
150                 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
151                            (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
152                         rc = cifs_creation_time_set(xid, pTcon, inode,
153                                         full_path, value, size);
154                         if (rc == 0) /* force revalidate of the inode */
155                                 CIFS_I(inode)->time = 0;
156                         break;
157                 }
158
159                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
160                         goto out;
161
162                 if (pTcon->ses->server->ops->set_EA)
163                         rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
164                                 full_path, name, value, (__u16)size,
165                                 cifs_sb->local_nls, cifs_sb);
166                 break;
167
168         case XATTR_CIFS_ACL:
169         case XATTR_CIFS_NTSD:
170         case XATTR_CIFS_NTSD_FULL: {
171                 struct cifs_ntsd *pacl;
172
173                 if (!value)
174                         goto out;
175                 pacl = kmalloc(size, GFP_KERNEL);
176                 if (!pacl) {
177                         rc = -ENOMEM;
178                 } else {
179                         memcpy(pacl, value, size);
180                         if (pTcon->ses->server->ops->set_acl) {
181                                 int aclflags = 0;
182                                 rc = 0;
183
184                                 switch (handler->flags) {
185                                 case XATTR_CIFS_NTSD_FULL:
186                                         aclflags = (CIFS_ACL_OWNER |
187                                                     CIFS_ACL_DACL |
188                                                     CIFS_ACL_SACL);
189                                         break;
190                                 case XATTR_CIFS_NTSD:
191                                         aclflags = (CIFS_ACL_OWNER |
192                                                     CIFS_ACL_DACL);
193                                         break;
194                                 case XATTR_CIFS_ACL:
195                                 default:
196                                         aclflags = CIFS_ACL_DACL;
197                                 }
198
199                                 rc = pTcon->ses->server->ops->set_acl(pacl,
200                                         size, inode, full_path, aclflags);
201                         } else {
202                                 rc = -EOPNOTSUPP;
203                         }
204                         if (rc == 0) /* force revalidate of the inode */
205                                 CIFS_I(inode)->time = 0;
206                         kfree(pacl);
207                 }
208                 break;
209         }
210
211         case XATTR_ACL_ACCESS:
212 #ifdef CONFIG_CIFS_POSIX
213                 if (!value)
214                         goto out;
215                 if (sb->s_flags & SB_POSIXACL)
216                         rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
217                                 value, (const int)size,
218                                 ACL_TYPE_ACCESS, cifs_sb->local_nls,
219                                 cifs_remap(cifs_sb));
220 #endif  /* CONFIG_CIFS_POSIX */
221                 break;
222
223         case XATTR_ACL_DEFAULT:
224 #ifdef CONFIG_CIFS_POSIX
225                 if (!value)
226                         goto out;
227                 if (sb->s_flags & SB_POSIXACL)
228                         rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
229                                 value, (const int)size,
230                                 ACL_TYPE_DEFAULT, cifs_sb->local_nls,
231                                 cifs_remap(cifs_sb));
232 #endif  /* CONFIG_CIFS_POSIX */
233                 break;
234         }
235
236 out:
237         kfree(full_path);
238         free_xid(xid);
239         cifs_put_tlink(tlink);
240         return rc;
241 }
242
243 static int cifs_attrib_get(struct dentry *dentry,
244                            struct inode *inode, void *value,
245                            size_t size)
246 {
247         ssize_t rc;
248         __u32 *pattribute;
249
250         rc = cifs_revalidate_dentry_attr(dentry);
251
252         if (rc)
253                 return rc;
254
255         if ((value == NULL) || (size == 0))
256                 return sizeof(__u32);
257         else if (size < sizeof(__u32))
258                 return -ERANGE;
259
260         /* return dos attributes as pseudo xattr */
261         pattribute = (__u32 *)value;
262         *pattribute = CIFS_I(inode)->cifsAttrs;
263
264         return sizeof(__u32);
265 }
266
267 static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
268                                   void *value, size_t size)
269 {
270         ssize_t rc;
271         __u64 *pcreatetime;
272
273         rc = cifs_revalidate_dentry_attr(dentry);
274         if (rc)
275                 return rc;
276
277         if ((value == NULL) || (size == 0))
278                 return sizeof(__u64);
279         else if (size < sizeof(__u64))
280                 return -ERANGE;
281
282         /* return dos attributes as pseudo xattr */
283         pcreatetime = (__u64 *)value;
284         *pcreatetime = CIFS_I(inode)->createtime;
285         return sizeof(__u64);
286 }
287
288
289 static int cifs_xattr_get(const struct xattr_handler *handler,
290                           struct dentry *dentry, struct inode *inode,
291                           const char *name, void *value, size_t size)
292 {
293         ssize_t rc = -EOPNOTSUPP;
294         unsigned int xid;
295         struct super_block *sb = dentry->d_sb;
296         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
297         struct tcon_link *tlink;
298         struct cifs_tcon *pTcon;
299         char *full_path;
300
301         tlink = cifs_sb_tlink(cifs_sb);
302         if (IS_ERR(tlink))
303                 return PTR_ERR(tlink);
304         pTcon = tlink_tcon(tlink);
305
306         xid = get_xid();
307
308         full_path = build_path_from_dentry(dentry);
309         if (full_path == NULL) {
310                 rc = -ENOMEM;
311                 goto out;
312         }
313
314         /* return alt name if available as pseudo attr */
315         switch (handler->flags) {
316         case XATTR_USER:
317                 cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
318                 if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
319                     (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
320                         rc = cifs_attrib_get(dentry, inode, value, size);
321                         break;
322                 } else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
323                     (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
324                         rc = cifs_creation_time_get(dentry, inode, value, size);
325                         break;
326                 }
327
328                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
329                         goto out;
330
331                 if (pTcon->ses->server->ops->query_all_EAs)
332                         rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
333                                 full_path, name, value, size, cifs_sb);
334                 break;
335
336         case XATTR_CIFS_ACL:
337         case XATTR_CIFS_NTSD:
338         case XATTR_CIFS_NTSD_FULL: {
339                 /*
340                  * fetch owner, DACL, and SACL if asked for full descriptor,
341                  * fetch owner and DACL otherwise
342                  */
343                 u32 acllen, extra_info;
344                 struct cifs_ntsd *pacl;
345
346                 if (pTcon->ses->server->ops->get_acl == NULL)
347                         goto out; /* rc already EOPNOTSUPP */
348
349                 if (handler->flags == XATTR_CIFS_NTSD_FULL) {
350                         extra_info = SACL_SECINFO;
351                 } else {
352                         extra_info = 0;
353                 }
354                 pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
355                                 inode, full_path, &acllen, extra_info);
356                 if (IS_ERR(pacl)) {
357                         rc = PTR_ERR(pacl);
358                         cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
359                                  __func__, rc);
360                 } else {
361                         if (value) {
362                                 if (acllen > size)
363                                         acllen = -ERANGE;
364                                 else
365                                         memcpy(value, pacl, acllen);
366                         }
367                         rc = acllen;
368                         kfree(pacl);
369                 }
370                 break;
371         }
372
373         case XATTR_ACL_ACCESS:
374 #ifdef CONFIG_CIFS_POSIX
375                 if (sb->s_flags & SB_POSIXACL)
376                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
377                                 value, size, ACL_TYPE_ACCESS,
378                                 cifs_sb->local_nls,
379                                 cifs_remap(cifs_sb));
380 #endif  /* CONFIG_CIFS_POSIX */
381                 break;
382
383         case XATTR_ACL_DEFAULT:
384 #ifdef CONFIG_CIFS_POSIX
385                 if (sb->s_flags & SB_POSIXACL)
386                         rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
387                                 value, size, ACL_TYPE_DEFAULT,
388                                 cifs_sb->local_nls,
389                                 cifs_remap(cifs_sb));
390 #endif  /* CONFIG_CIFS_POSIX */
391                 break;
392         }
393
394         /* We could add an additional check for streams ie
395             if proc/fs/cifs/streamstoxattr is set then
396                 search server for EAs or streams to
397                 returns as xattrs */
398
399         if (rc == -EINVAL)
400                 rc = -EOPNOTSUPP;
401
402 out:
403         kfree(full_path);
404         free_xid(xid);
405         cifs_put_tlink(tlink);
406         return rc;
407 }
408
409 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
410 {
411         ssize_t rc = -EOPNOTSUPP;
412         unsigned int xid;
413         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
414         struct tcon_link *tlink;
415         struct cifs_tcon *pTcon;
416         char *full_path;
417
418         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
419                 return -EOPNOTSUPP;
420
421         tlink = cifs_sb_tlink(cifs_sb);
422         if (IS_ERR(tlink))
423                 return PTR_ERR(tlink);
424         pTcon = tlink_tcon(tlink);
425
426         xid = get_xid();
427
428         full_path = build_path_from_dentry(direntry);
429         if (full_path == NULL) {
430                 rc = -ENOMEM;
431                 goto list_ea_exit;
432         }
433         /* return dos attributes as pseudo xattr */
434         /* return alt name if available as pseudo attr */
435
436         /* if proc/fs/cifs/streamstoxattr is set then
437                 search server for EAs or streams to
438                 returns as xattrs */
439
440         if (pTcon->ses->server->ops->query_all_EAs)
441                 rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
442                                 full_path, NULL, data, buf_size, cifs_sb);
443 list_ea_exit:
444         kfree(full_path);
445         free_xid(xid);
446         cifs_put_tlink(tlink);
447         return rc;
448 }
449
450 static const struct xattr_handler cifs_user_xattr_handler = {
451         .prefix = XATTR_USER_PREFIX,
452         .flags = XATTR_USER,
453         .get = cifs_xattr_get,
454         .set = cifs_xattr_set,
455 };
456
457 /* os2.* attributes are treated like user.* attributes */
458 static const struct xattr_handler cifs_os2_xattr_handler = {
459         .prefix = XATTR_OS2_PREFIX,
460         .flags = XATTR_USER,
461         .get = cifs_xattr_get,
462         .set = cifs_xattr_set,
463 };
464
465 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
466         .name = CIFS_XATTR_CIFS_ACL,
467         .flags = XATTR_CIFS_ACL,
468         .get = cifs_xattr_get,
469         .set = cifs_xattr_set,
470 };
471
472 /*
473  * Although this is just an alias for the above, need to move away from
474  * confusing users and using the 20 year old term 'cifs' when it is no
475  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
476  * SMB3 and later are highly secure.
477  */
478 static const struct xattr_handler smb3_acl_xattr_handler = {
479         .name = SMB3_XATTR_CIFS_ACL,
480         .flags = XATTR_CIFS_ACL,
481         .get = cifs_xattr_get,
482         .set = cifs_xattr_set,
483 };
484
485 static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
486         .name = CIFS_XATTR_CIFS_NTSD,
487         .flags = XATTR_CIFS_NTSD,
488         .get = cifs_xattr_get,
489         .set = cifs_xattr_set,
490 };
491
492 /*
493  * Although this is just an alias for the above, need to move away from
494  * confusing users and using the 20 year old term 'cifs' when it is no
495  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
496  * SMB3 and later are highly secure.
497  */
498 static const struct xattr_handler smb3_ntsd_xattr_handler = {
499         .name = SMB3_XATTR_CIFS_NTSD,
500         .flags = XATTR_CIFS_NTSD,
501         .get = cifs_xattr_get,
502         .set = cifs_xattr_set,
503 };
504
505 static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
506         .name = CIFS_XATTR_CIFS_NTSD_FULL,
507         .flags = XATTR_CIFS_NTSD_FULL,
508         .get = cifs_xattr_get,
509         .set = cifs_xattr_set,
510 };
511
512 /*
513  * Although this is just an alias for the above, need to move away from
514  * confusing users and using the 20 year old term 'cifs' when it is no
515  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
516  * SMB3 and later are highly secure.
517  */
518 static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
519         .name = SMB3_XATTR_CIFS_NTSD_FULL,
520         .flags = XATTR_CIFS_NTSD_FULL,
521         .get = cifs_xattr_get,
522         .set = cifs_xattr_set,
523 };
524
525
526 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
527         .name = XATTR_NAME_POSIX_ACL_ACCESS,
528         .flags = XATTR_ACL_ACCESS,
529         .get = cifs_xattr_get,
530         .set = cifs_xattr_set,
531 };
532
533 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
534         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
535         .flags = XATTR_ACL_DEFAULT,
536         .get = cifs_xattr_get,
537         .set = cifs_xattr_set,
538 };
539
540 const struct xattr_handler *cifs_xattr_handlers[] = {
541         &cifs_user_xattr_handler,
542         &cifs_os2_xattr_handler,
543         &cifs_cifs_acl_xattr_handler,
544         &smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
545         &cifs_cifs_ntsd_xattr_handler,
546         &smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
547         &cifs_cifs_ntsd_full_xattr_handler,
548         &smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
549         &cifs_posix_acl_access_xattr_handler,
550         &cifs_posix_acl_default_xattr_handler,
551         NULL
552 };