Merge "virtio-9p: enable the virtfs on Windows." into tizen
[sdk/emulator/qemu.git] / hw / 9pfs / virtio-9p-posix-acl.c
1 /*
2  * Virtio 9p system.posix* 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
14 #include <sys/types.h>
15 #include "qemu/xattr.h"
16 #include "hw/virtio/virtio.h"
17 #include "virtio-9p.h"
18 #include "fsdev/file-op-9p.h"
19 #include "virtio-9p-xattr.h"
20
21 #define MAP_ACL_ACCESS "user.virtfs.system.posix_acl_access"
22 #define MAP_ACL_DEFAULT "user.virtfs.system.posix_acl_default"
23 #define ACL_ACCESS "system.posix_acl_access"
24 #define ACL_DEFAULT "system.posix_acl_default"
25
26 static ssize_t mp_pacl_getxattr(FsContext *ctx, const char *path,
27                                 const char *name, void *value, size_t size)
28 {
29     char buffer[PATH_MAX];
30 #ifdef CONFIG_LINUX
31     return lgetxattr(rpath(ctx, path, buffer), MAP_ACL_ACCESS, value, size);
32 #else
33     return getxattr(rpath(ctx, path, buffer), MAP_ACL_ACCESS, value, size, 0, XATTR_NOFOLLOW);
34 #endif
35 }
36
37 static ssize_t mp_pacl_listxattr(FsContext *ctx, const char *path,
38                                  char *name, void *value, size_t osize)
39 {
40     ssize_t len = sizeof(ACL_ACCESS);
41
42     if (!value) {
43         return len;
44     }
45
46     if (osize < len) {
47         errno = ERANGE;
48         return -1;
49     }
50
51     /* len includes the trailing NUL */
52     memcpy(value, ACL_ACCESS, len);
53     return 0;
54 }
55
56 static int mp_pacl_setxattr(FsContext *ctx, const char *path, const char *name,
57                             void *value, size_t size, int flags)
58 {
59     char buffer[PATH_MAX];
60 #ifdef CONFIG_LINUX
61     return lsetxattr(rpath(ctx, path, buffer), MAP_ACL_ACCESS, value,
62             size, flags);
63 #else
64     return setxattr(rpath(ctx, path, buffer), MAP_ACL_ACCESS, value,
65             size, 0, flags | XATTR_NOFOLLOW);
66 #endif
67 }
68
69 static int mp_pacl_removexattr(FsContext *ctx,
70                                const char *path, const char *name)
71 {
72     int ret;
73     char buffer[PATH_MAX];
74 #ifdef CONFIG_LINUX
75     ret  = lremovexattr(rpath(ctx, path, buffer), MAP_ACL_ACCESS);
76     if (ret == -1 && errno == ENODATA) {
77         /*
78          * We don't get ENODATA error when trying to remove a
79          * posix acl that is not present. So don't throw the error
80          * even in case of mapped security model
81          */
82         errno = 0;
83         ret = 0;
84     }
85 #else
86     ret  = removexattr(rpath(ctx, path, buffer), MAP_ACL_ACCESS, XATTR_NOFOLLOW);
87     if (ret == -1 && errno == ENODATA) {
88         /*
89          * We don't get ENODATA error when trying to remove a
90          * posix acl that is not present. So don't throw the error
91          * even in case of mapped security model
92          */
93         errno = 0;
94         ret = 0;
95     }
96 #endif
97     return ret;
98 }
99
100 static ssize_t mp_dacl_getxattr(FsContext *ctx, const char *path,
101                                 const char *name, void *value, size_t size)
102 {
103     char buffer[PATH_MAX];
104 #ifdef CONFIG_LINUX
105     return lgetxattr(rpath(ctx, path, buffer), MAP_ACL_DEFAULT, value, size);
106 #else
107     return getxattr(rpath(ctx, path, buffer), MAP_ACL_DEFAULT, value, size, 0, XATTR_NOFOLLOW);
108 #endif
109 }
110
111 static ssize_t mp_dacl_listxattr(FsContext *ctx, const char *path,
112                                  char *name, void *value, size_t osize)
113 {
114     ssize_t len = sizeof(ACL_DEFAULT);
115
116     if (!value) {
117         return len;
118     }
119
120     if (osize < len) {
121         errno = ERANGE;
122         return -1;
123     }
124
125     /* len includes the trailing NUL */
126     memcpy(value, ACL_ACCESS, len);
127     return 0;
128 }
129
130 static int mp_dacl_setxattr(FsContext *ctx, const char *path, const char *name,
131                             void *value, size_t size, int flags)
132 {
133     char buffer[PATH_MAX];
134 #ifdef CONFIG_LINUX
135     return lsetxattr(rpath(ctx, path, buffer), MAP_ACL_DEFAULT, value,
136             size, flags);
137 #else
138     return setxattr(rpath(ctx, path, buffer), MAP_ACL_DEFAULT, value,
139             size, 0, flags | XATTR_NOFOLLOW);
140 #endif
141 }
142
143 static int mp_dacl_removexattr(FsContext *ctx,
144                                const char *path, const char *name)
145 {
146     int ret;
147     char buffer[PATH_MAX];
148 #ifdef CONFIG_LINUX
149     ret  = lremovexattr(rpath(ctx, path, buffer), MAP_ACL_DEFAULT);
150     if (ret == -1 && errno == ENODATA) {
151         /*
152          * We don't get ENODATA error when trying to remove a
153          * posix acl that is not present. So don't throw the error
154          * even in case of mapped security model
155          */
156         errno = 0;
157         ret = 0;
158     }
159 #else
160     ret  = removexattr(rpath(ctx, path, buffer), MAP_ACL_DEFAULT, XATTR_NOFOLLOW);
161     if (ret == -1 && errno == ENODATA) {
162         /*
163          * We don't get ENODATA error when trying to remove a
164          * posix acl that is not present. So don't throw the error
165          * even in case of mapped security model
166          */
167         errno = 0;
168         ret = 0;
169     }
170 #endif
171
172     return ret;
173 }
174
175
176 XattrOperations mapped_pacl_xattr = {
177     .name = "system.posix_acl_access",
178     .getxattr = mp_pacl_getxattr,
179     .setxattr = mp_pacl_setxattr,
180     .listxattr = mp_pacl_listxattr,
181     .removexattr = mp_pacl_removexattr,
182 };
183
184 XattrOperations mapped_dacl_xattr = {
185     .name = "system.posix_acl_default",
186     .getxattr = mp_dacl_getxattr,
187     .setxattr = mp_dacl_setxattr,
188     .listxattr = mp_dacl_listxattr,
189     .removexattr = mp_dacl_removexattr,
190 };
191
192 XattrOperations passthrough_acl_xattr = {
193     .name = "system.posix_acl_",
194     .getxattr = pt_getxattr,
195     .setxattr = pt_setxattr,
196     .listxattr = pt_listxattr,
197     .removexattr = pt_removexattr,
198 };
199
200 XattrOperations none_acl_xattr = {
201     .name = "system.posix_acl_",
202     .getxattr = notsup_getxattr,
203     .setxattr = notsup_setxattr,
204     .listxattr = notsup_listxattr,
205     .removexattr = notsup_removexattr,
206 };