Merge branch 'tizen_qemu_2.0' into tizen
[sdk/emulator/qemu.git] / hw / 9pfs / virtio-9p-xattr-user.c
1 /*
2  * Virtio 9p user. xattr callback
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 #include <sys/types.h>
14 #include "hw/virtio/virtio.h"
15 #include "virtio-9p.h"
16 #include "fsdev/file-op-9p.h"
17 #include "virtio-9p-xattr.h"
18
19
20 static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
21                                 const char *name, void *value, size_t size)
22 {
23     char *buffer;
24     ssize_t ret;
25
26     if (strncmp(name, "user.virtfs.", 12) == 0) {
27         /*
28          * Don't allow fetch of user.virtfs namesapce
29          * in case of mapped security
30          */
31         errno = ENOATTR;
32         return -1;
33     }
34     buffer = rpath(ctx, path);
35 #ifdef CONFIG_LINUX
36     ret = lgetxattr(buffer, name, value, size);
37 #else
38     ret = getxattr(buffer, name, value, size, 0, XATTR_NOFOLLOW);
39 #endif
40     g_free(buffer);
41     return ret;
42 }
43
44 static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
45                                  char *name, void *value, size_t size)
46 {
47     int name_size = strlen(name) + 1;
48     if (strncmp(name, "user.virtfs.", 12) == 0) {
49
50         /*  check if it is a mapped posix acl */
51         if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
52             /* adjust the name and size */
53             name += 12;
54             name_size -= 12;
55         } else {
56             /*
57              * Don't allow fetch of user.virtfs namesapce
58              * in case of mapped security
59              */
60             return 0;
61         }
62     }
63     if (!value) {
64         return name_size;
65     }
66
67     if (size < name_size) {
68         errno = ERANGE;
69         return -1;
70     }
71
72     /* name_size includes the trailing NUL. */
73     memcpy(value, name, name_size);
74     return name_size;
75 }
76
77 static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
78                             void *value, size_t size, int flags)
79 {
80     char *buffer;
81     int ret;
82
83     if (strncmp(name, "user.virtfs.", 12) == 0) {
84         /*
85          * Don't allow fetch of user.virtfs namesapce
86          * in case of mapped security
87          */
88         errno = EACCES;
89         return -1;
90     }
91     buffer = rpath(ctx, path);
92 #ifdef CONFIG_LINUX
93     ret = lsetxattr(buffer, name, value, size, flags);
94 #else
95     ret = setxattr(buffer, name, value, size, 0, flags | XATTR_NOFOLLOW);
96 #endif
97     g_free(buffer);
98     return ret;
99 }
100
101 static int mp_user_removexattr(FsContext *ctx,
102                                const char *path, const char *name)
103 {
104     char *buffer;
105     int ret;
106
107     if (strncmp(name, "user.virtfs.", 12) == 0) {
108         /*
109          * Don't allow fetch of user.virtfs namesapce
110          * in case of mapped security
111          */
112         errno = EACCES;
113         return -1;
114     }
115     buffer = rpath(ctx, path);
116 #ifdef CONFIG_LINUX
117     ret = lremovexattr(buffer, name);
118 #else
119     ret = removexattr(buffer, name, XATTR_NOFOLLOW);
120 #endif
121     g_free(buffer);
122     return ret;
123 }
124
125 XattrOperations mapped_user_xattr = {
126     .name = "user.",
127     .getxattr = mp_user_getxattr,
128     .setxattr = mp_user_setxattr,
129     .listxattr = mp_user_listxattr,
130     .removexattr = mp_user_removexattr,
131 };
132
133 XattrOperations passthrough_user_xattr = {
134     .name = "user.",
135     .getxattr = pt_getxattr,
136     .setxattr = pt_setxattr,
137     .listxattr = pt_listxattr,
138     .removexattr = pt_removexattr,
139 };