root / hw / virtio-9p-local.c @ 00ec5c37
History | View | Annotate | Download (10.2 kB)
1 |
/*
|
---|---|
2 |
* Virtio 9p Posix callback
|
3 |
*
|
4 |
* Copyright IBM, Corp. 2010
|
5 |
*
|
6 |
* Authors:
|
7 |
* Anthony Liguori <aliguori@us.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 "virtio.h" |
14 |
#include "virtio-9p.h" |
15 |
#include <arpa/inet.h> |
16 |
#include <pwd.h> |
17 |
#include <grp.h> |
18 |
#include <sys/socket.h> |
19 |
#include <sys/un.h> |
20 |
#include <attr/xattr.h> |
21 |
|
22 |
static const char *rpath(FsContext *ctx, const char *path) |
23 |
{ |
24 |
/* FIXME: so wrong... */
|
25 |
static char buffer[4096]; |
26 |
snprintf(buffer, sizeof(buffer), "%s/%s", ctx->fs_root, path); |
27 |
return buffer;
|
28 |
} |
29 |
|
30 |
|
31 |
static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf) |
32 |
{ |
33 |
int err;
|
34 |
err = lstat(rpath(fs_ctx, path), stbuf); |
35 |
if (err) {
|
36 |
return err;
|
37 |
} |
38 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
39 |
/* Actual credentials are part of extended attrs */
|
40 |
uid_t tmp_uid; |
41 |
gid_t tmp_gid; |
42 |
mode_t tmp_mode; |
43 |
dev_t tmp_dev; |
44 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.uid", &tmp_uid, |
45 |
sizeof(uid_t)) > 0) { |
46 |
stbuf->st_uid = tmp_uid; |
47 |
} |
48 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.gid", &tmp_gid, |
49 |
sizeof(gid_t)) > 0) { |
50 |
stbuf->st_gid = tmp_gid; |
51 |
} |
52 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.mode", &tmp_mode, |
53 |
sizeof(mode_t)) > 0) { |
54 |
stbuf->st_mode = tmp_mode; |
55 |
} |
56 |
if (getxattr(rpath(fs_ctx, path), "user.virtfs.rdev", &tmp_dev, |
57 |
sizeof(dev_t)) > 0) { |
58 |
stbuf->st_rdev = tmp_dev; |
59 |
} |
60 |
} |
61 |
return err;
|
62 |
} |
63 |
|
64 |
static int local_set_xattr(const char *path, FsCred *credp) |
65 |
{ |
66 |
int err;
|
67 |
if (credp->fc_uid != -1) { |
68 |
err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t), |
69 |
0);
|
70 |
if (err) {
|
71 |
return err;
|
72 |
} |
73 |
} |
74 |
if (credp->fc_gid != -1) { |
75 |
err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t), |
76 |
0);
|
77 |
if (err) {
|
78 |
return err;
|
79 |
} |
80 |
} |
81 |
if (credp->fc_mode != -1) { |
82 |
err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
|
83 |
sizeof(mode_t), 0); |
84 |
if (err) {
|
85 |
return err;
|
86 |
} |
87 |
} |
88 |
if (credp->fc_rdev != -1) { |
89 |
err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
|
90 |
sizeof(dev_t), 0); |
91 |
if (err) {
|
92 |
return err;
|
93 |
} |
94 |
} |
95 |
return 0; |
96 |
} |
97 |
|
98 |
static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, |
99 |
FsCred *credp) |
100 |
{ |
101 |
if (chmod(rpath(fs_ctx, path), credp->fc_mode & 07777) < 0) { |
102 |
return -1; |
103 |
} |
104 |
if (chown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid) < 0) { |
105 |
return -1; |
106 |
} |
107 |
return 0; |
108 |
} |
109 |
|
110 |
static ssize_t local_readlink(FsContext *ctx, const char *path, |
111 |
char *buf, size_t bufsz)
|
112 |
{ |
113 |
return readlink(rpath(ctx, path), buf, bufsz);
|
114 |
} |
115 |
|
116 |
static int local_close(FsContext *ctx, int fd) |
117 |
{ |
118 |
return close(fd);
|
119 |
} |
120 |
|
121 |
static int local_closedir(FsContext *ctx, DIR *dir) |
122 |
{ |
123 |
return closedir(dir);
|
124 |
} |
125 |
|
126 |
static int local_open(FsContext *ctx, const char *path, int flags) |
127 |
{ |
128 |
return open(rpath(ctx, path), flags);
|
129 |
} |
130 |
|
131 |
static DIR *local_opendir(FsContext *ctx, const char *path) |
132 |
{ |
133 |
return opendir(rpath(ctx, path));
|
134 |
} |
135 |
|
136 |
static void local_rewinddir(FsContext *ctx, DIR *dir) |
137 |
{ |
138 |
return rewinddir(dir);
|
139 |
} |
140 |
|
141 |
static off_t local_telldir(FsContext *ctx, DIR *dir)
|
142 |
{ |
143 |
return telldir(dir);
|
144 |
} |
145 |
|
146 |
static struct dirent *local_readdir(FsContext *ctx, DIR *dir) |
147 |
{ |
148 |
return readdir(dir);
|
149 |
} |
150 |
|
151 |
static void local_seekdir(FsContext *ctx, DIR *dir, off_t off) |
152 |
{ |
153 |
return seekdir(dir, off);
|
154 |
} |
155 |
|
156 |
static ssize_t local_readv(FsContext *ctx, int fd, const struct iovec *iov, |
157 |
int iovcnt)
|
158 |
{ |
159 |
return readv(fd, iov, iovcnt);
|
160 |
} |
161 |
|
162 |
static off_t local_lseek(FsContext *ctx, int fd, off_t offset, int whence) |
163 |
{ |
164 |
return lseek(fd, offset, whence);
|
165 |
} |
166 |
|
167 |
static ssize_t local_writev(FsContext *ctx, int fd, const struct iovec *iov, |
168 |
int iovcnt)
|
169 |
{ |
170 |
return writev(fd, iov, iovcnt);
|
171 |
} |
172 |
|
173 |
static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp) |
174 |
{ |
175 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
176 |
return local_set_xattr(rpath(fs_ctx, path), credp);
|
177 |
} else if (fs_ctx->fs_sm == SM_PASSTHROUGH) { |
178 |
return chmod(rpath(fs_ctx, path), credp->fc_mode);
|
179 |
} |
180 |
return -1; |
181 |
} |
182 |
|
183 |
static int local_mknod(FsContext *ctx, const char *path, mode_t mode, dev_t dev) |
184 |
{ |
185 |
return mknod(rpath(ctx, path), mode, dev);
|
186 |
} |
187 |
|
188 |
static int local_mksock(FsContext *ctx2, const char *path) |
189 |
{ |
190 |
struct sockaddr_un addr;
|
191 |
int s;
|
192 |
|
193 |
addr.sun_family = AF_UNIX; |
194 |
snprintf(addr.sun_path, 108, "%s", rpath(ctx2, path)); |
195 |
|
196 |
s = socket(PF_UNIX, SOCK_STREAM, 0);
|
197 |
if (s == -1) { |
198 |
return -1; |
199 |
} |
200 |
|
201 |
if (bind(s, (struct sockaddr *)&addr, sizeof(addr))) { |
202 |
close(s); |
203 |
return -1; |
204 |
} |
205 |
|
206 |
close(s); |
207 |
return 0; |
208 |
} |
209 |
|
210 |
static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp) |
211 |
{ |
212 |
int err = -1; |
213 |
int serrno = 0; |
214 |
|
215 |
/* Determine the security model */
|
216 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
217 |
err = mkdir(rpath(fs_ctx, path), SM_LOCAL_DIR_MODE_BITS); |
218 |
if (err == -1) { |
219 |
return err;
|
220 |
} |
221 |
credp->fc_mode = credp->fc_mode|S_IFDIR; |
222 |
err = local_set_xattr(rpath(fs_ctx, path), credp); |
223 |
if (err == -1) { |
224 |
serrno = errno; |
225 |
goto err_end;
|
226 |
} |
227 |
} else if (fs_ctx->fs_sm == SM_PASSTHROUGH) { |
228 |
err = mkdir(rpath(fs_ctx, path), credp->fc_mode); |
229 |
if (err == -1) { |
230 |
return err;
|
231 |
} |
232 |
err = local_post_create_passthrough(fs_ctx, path, credp); |
233 |
if (err == -1) { |
234 |
serrno = errno; |
235 |
goto err_end;
|
236 |
} |
237 |
} |
238 |
return err;
|
239 |
|
240 |
err_end:
|
241 |
remove(rpath(fs_ctx, path)); |
242 |
errno = serrno; |
243 |
return err;
|
244 |
} |
245 |
|
246 |
static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf) |
247 |
{ |
248 |
int err;
|
249 |
err = fstat(fd, stbuf); |
250 |
if (err) {
|
251 |
return err;
|
252 |
} |
253 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
254 |
/* Actual credentials are part of extended attrs */
|
255 |
uid_t tmp_uid; |
256 |
gid_t tmp_gid; |
257 |
mode_t tmp_mode; |
258 |
dev_t tmp_dev; |
259 |
|
260 |
if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { |
261 |
stbuf->st_uid = tmp_uid; |
262 |
} |
263 |
if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { |
264 |
stbuf->st_gid = tmp_gid; |
265 |
} |
266 |
if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { |
267 |
stbuf->st_mode = tmp_mode; |
268 |
} |
269 |
if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { |
270 |
stbuf->st_rdev = tmp_dev; |
271 |
} |
272 |
} |
273 |
return err;
|
274 |
} |
275 |
|
276 |
static int local_open2(FsContext *fs_ctx, const char *path, int flags, |
277 |
FsCred *credp) |
278 |
{ |
279 |
int fd = -1; |
280 |
int err = -1; |
281 |
int serrno = 0; |
282 |
|
283 |
/* Determine the security model */
|
284 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
285 |
fd = open(rpath(fs_ctx, path), flags, SM_LOCAL_MODE_BITS); |
286 |
if (fd == -1) { |
287 |
return fd;
|
288 |
} |
289 |
credp->fc_mode = credp->fc_mode|S_IFREG; |
290 |
/* Set cleint credentials in xattr */
|
291 |
err = local_set_xattr(rpath(fs_ctx, path), credp); |
292 |
if (err == -1) { |
293 |
serrno = errno; |
294 |
goto err_end;
|
295 |
} |
296 |
} else if (fs_ctx->fs_sm == SM_PASSTHROUGH) { |
297 |
fd = open(rpath(fs_ctx, path), flags, credp->fc_mode); |
298 |
if (fd == -1) { |
299 |
return fd;
|
300 |
} |
301 |
err = local_post_create_passthrough(fs_ctx, path, credp); |
302 |
if (err == -1) { |
303 |
serrno = errno; |
304 |
goto err_end;
|
305 |
} |
306 |
} |
307 |
return fd;
|
308 |
|
309 |
err_end:
|
310 |
close(fd); |
311 |
remove(rpath(fs_ctx, path)); |
312 |
errno = serrno; |
313 |
return err;
|
314 |
} |
315 |
|
316 |
|
317 |
static int local_symlink(FsContext *ctx, const char *oldpath, |
318 |
const char *newpath) |
319 |
{ |
320 |
return symlink(oldpath, rpath(ctx, newpath));
|
321 |
} |
322 |
|
323 |
static int local_link(FsContext *ctx, const char *oldpath, const char *newpath) |
324 |
{ |
325 |
char *tmp = qemu_strdup(rpath(ctx, oldpath));
|
326 |
int err, serrno = 0; |
327 |
|
328 |
if (tmp == NULL) { |
329 |
return -ENOMEM;
|
330 |
} |
331 |
|
332 |
err = link(tmp, rpath(ctx, newpath)); |
333 |
if (err == -1) { |
334 |
serrno = errno; |
335 |
} |
336 |
|
337 |
qemu_free(tmp); |
338 |
|
339 |
if (err == -1) { |
340 |
errno = serrno; |
341 |
} |
342 |
|
343 |
return err;
|
344 |
} |
345 |
|
346 |
static int local_truncate(FsContext *ctx, const char *path, off_t size) |
347 |
{ |
348 |
return truncate(rpath(ctx, path), size);
|
349 |
} |
350 |
|
351 |
static int local_rename(FsContext *ctx, const char *oldpath, |
352 |
const char *newpath) |
353 |
{ |
354 |
char *tmp;
|
355 |
int err;
|
356 |
|
357 |
tmp = qemu_strdup(rpath(ctx, oldpath)); |
358 |
if (tmp == NULL) { |
359 |
return -1; |
360 |
} |
361 |
|
362 |
err = rename(tmp, rpath(ctx, newpath)); |
363 |
if (err == -1) { |
364 |
int serrno = errno;
|
365 |
qemu_free(tmp); |
366 |
errno = serrno; |
367 |
} else {
|
368 |
qemu_free(tmp); |
369 |
} |
370 |
|
371 |
return err;
|
372 |
|
373 |
} |
374 |
|
375 |
static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp) |
376 |
{ |
377 |
if (fs_ctx->fs_sm == SM_MAPPED) {
|
378 |
return local_set_xattr(rpath(fs_ctx, path), credp);
|
379 |
} else if (fs_ctx->fs_sm == SM_PASSTHROUGH) { |
380 |
return lchown(rpath(fs_ctx, path), credp->fc_uid, credp->fc_gid);
|
381 |
} |
382 |
return -1; |
383 |
} |
384 |
|
385 |
static int local_utime(FsContext *ctx, const char *path, |
386 |
const struct utimbuf *buf) |
387 |
{ |
388 |
return utime(rpath(ctx, path), buf);
|
389 |
} |
390 |
|
391 |
static int local_remove(FsContext *ctx, const char *path) |
392 |
{ |
393 |
return remove(rpath(ctx, path));
|
394 |
} |
395 |
|
396 |
static int local_fsync(FsContext *ctx, int fd) |
397 |
{ |
398 |
return fsync(fd);
|
399 |
} |
400 |
|
401 |
FileOperations local_ops = { |
402 |
.lstat = local_lstat, |
403 |
.readlink = local_readlink, |
404 |
.close = local_close, |
405 |
.closedir = local_closedir, |
406 |
.open = local_open, |
407 |
.opendir = local_opendir, |
408 |
.rewinddir = local_rewinddir, |
409 |
.telldir = local_telldir, |
410 |
.readdir = local_readdir, |
411 |
.seekdir = local_seekdir, |
412 |
.readv = local_readv, |
413 |
.lseek = local_lseek, |
414 |
.writev = local_writev, |
415 |
.chmod = local_chmod, |
416 |
.mknod = local_mknod, |
417 |
.mksock = local_mksock, |
418 |
.mkdir = local_mkdir, |
419 |
.fstat = local_fstat, |
420 |
.open2 = local_open2, |
421 |
.symlink = local_symlink, |
422 |
.link = local_link, |
423 |
.truncate = local_truncate, |
424 |
.rename = local_rename, |
425 |
.chown = local_chown, |
426 |
.utime = local_utime, |
427 |
.remove = local_remove, |
428 |
.fsync = local_fsync, |
429 |
}; |