]> git.proxmox.com Git - mirror_qemu.git/blob - fsdev/virtfs-proxy-helper.c
virtfs-proxy-helper: add missing long option terminator
[mirror_qemu.git] / fsdev / virtfs-proxy-helper.c
1 /*
2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
4 *
5 * Authors:
6 * M. Mohan Kumar <mohan@in.ibm.com>
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
10 */
11
12 #include <sys/resource.h>
13 #include <getopt.h>
14 #include <syslog.h>
15 #include <sys/capability.h>
16 #include <sys/fsuid.h>
17 #include <sys/vfs.h>
18 #include <sys/ioctl.h>
19 #include <linux/fs.h>
20 #ifdef CONFIG_LINUX_MAGIC_H
21 #include <linux/magic.h>
22 #endif
23 #include "qemu-common.h"
24 #include "qemu/sockets.h"
25 #include "qemu/xattr.h"
26 #include "virtio-9p-marshal.h"
27 #include "hw/9pfs/virtio-9p-proxy.h"
28 #include "fsdev/virtio-9p-marshal.h"
29
30 #define PROGNAME "virtfs-proxy-helper"
31
32 #ifndef XFS_SUPER_MAGIC
33 #define XFS_SUPER_MAGIC 0x58465342
34 #endif
35 #ifndef EXT2_SUPER_MAGIC
36 #define EXT2_SUPER_MAGIC 0xEF53
37 #endif
38 #ifndef REISERFS_SUPER_MAGIC
39 #define REISERFS_SUPER_MAGIC 0x52654973
40 #endif
41 #ifndef BTRFS_SUPER_MAGIC
42 #define BTRFS_SUPER_MAGIC 0x9123683E
43 #endif
44
45 static struct option helper_opts[] = {
46 {"fd", required_argument, NULL, 'f'},
47 {"path", required_argument, NULL, 'p'},
48 {"nodaemon", no_argument, NULL, 'n'},
49 {"socket", required_argument, NULL, 's'},
50 {"uid", required_argument, NULL, 'u'},
51 {"gid", required_argument, NULL, 'g'},
52 {},
53 };
54
55 static bool is_daemon;
56 static bool get_version; /* IOC getversion IOCTL supported */
57
58 static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
59 {
60 va_list ap;
61
62 va_start(ap, format);
63 if (is_daemon) {
64 vsyslog(LOG_CRIT, format, ap);
65 } else {
66 vfprintf(stderr, format, ap);
67 }
68 va_end(ap);
69 }
70
71 static void do_perror(const char *string)
72 {
73 if (is_daemon) {
74 syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
75 } else {
76 fprintf(stderr, "%s:%s\n", string, strerror(errno));
77 }
78 }
79
80 static int do_cap_set(cap_value_t *cap_value, int size, int reset)
81 {
82 cap_t caps;
83 if (reset) {
84 /*
85 * Start with an empty set and set permitted and effective
86 */
87 caps = cap_init();
88 if (caps == NULL) {
89 do_perror("cap_init");
90 return -1;
91 }
92 if (cap_set_flag(caps, CAP_PERMITTED, size, cap_value, CAP_SET) < 0) {
93 do_perror("cap_set_flag");
94 goto error;
95 }
96 } else {
97 caps = cap_get_proc();
98 if (!caps) {
99 do_perror("cap_get_proc");
100 return -1;
101 }
102 }
103 if (cap_set_flag(caps, CAP_EFFECTIVE, size, cap_value, CAP_SET) < 0) {
104 do_perror("cap_set_flag");
105 goto error;
106 }
107 if (cap_set_proc(caps) < 0) {
108 do_perror("cap_set_proc");
109 goto error;
110 }
111 cap_free(caps);
112 return 0;
113
114 error:
115 cap_free(caps);
116 return -1;
117 }
118
119 static int init_capabilities(void)
120 {
121 /* helper needs following capabilities only */
122 cap_value_t cap_list[] = {
123 CAP_CHOWN,
124 CAP_DAC_OVERRIDE,
125 CAP_FOWNER,
126 CAP_FSETID,
127 CAP_SETGID,
128 CAP_MKNOD,
129 CAP_SETUID,
130 };
131 return do_cap_set(cap_list, ARRAY_SIZE(cap_list), 1);
132 }
133
134 static int socket_read(int sockfd, void *buff, ssize_t size)
135 {
136 ssize_t retval, total = 0;
137
138 while (size) {
139 retval = read(sockfd, buff, size);
140 if (retval == 0) {
141 return -EIO;
142 }
143 if (retval < 0) {
144 if (errno == EINTR) {
145 continue;
146 }
147 return -errno;
148 }
149 size -= retval;
150 buff += retval;
151 total += retval;
152 }
153 return total;
154 }
155
156 static int socket_write(int sockfd, void *buff, ssize_t size)
157 {
158 ssize_t retval, total = 0;
159
160 while (size) {
161 retval = write(sockfd, buff, size);
162 if (retval < 0) {
163 if (errno == EINTR) {
164 continue;
165 }
166 return -errno;
167 }
168 size -= retval;
169 buff += retval;
170 total += retval;
171 }
172 return total;
173 }
174
175 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
176 {
177 int retval;
178
179 /*
180 * read the request header.
181 */
182 iovec->iov_len = 0;
183 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
184 if (retval < 0) {
185 return retval;
186 }
187 iovec->iov_len = PROXY_HDR_SZ;
188 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
189 if (retval < 0) {
190 return retval;
191 }
192 /*
193 * We can't process message.size > PROXY_MAX_IO_SZ.
194 * Treat it as fatal error
195 */
196 if (header->size > PROXY_MAX_IO_SZ) {
197 return -ENOBUFS;
198 }
199 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
200 if (retval < 0) {
201 return retval;
202 }
203 iovec->iov_len += header->size;
204 return 0;
205 }
206
207 static int send_fd(int sockfd, int fd)
208 {
209 struct msghdr msg;
210 struct iovec iov;
211 int retval, data;
212 struct cmsghdr *cmsg;
213 union MsgControl msg_control;
214
215 iov.iov_base = &data;
216 iov.iov_len = sizeof(data);
217
218 memset(&msg, 0, sizeof(msg));
219 msg.msg_iov = &iov;
220 msg.msg_iovlen = 1;
221 /* No ancillary data on error */
222 if (fd < 0) {
223 /* fd is really negative errno if the request failed */
224 data = fd;
225 } else {
226 data = V9FS_FD_VALID;
227 msg.msg_control = &msg_control;
228 msg.msg_controllen = sizeof(msg_control);
229
230 cmsg = &msg_control.cmsg;
231 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
232 cmsg->cmsg_level = SOL_SOCKET;
233 cmsg->cmsg_type = SCM_RIGHTS;
234 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
235 }
236
237 do {
238 retval = sendmsg(sockfd, &msg, 0);
239 } while (retval < 0 && errno == EINTR);
240 if (fd >= 0) {
241 close(fd);
242 }
243 if (retval < 0) {
244 return retval;
245 }
246 return 0;
247 }
248
249 static int send_status(int sockfd, struct iovec *iovec, int status)
250 {
251 ProxyHeader header;
252 int retval, msg_size;
253
254 if (status < 0) {
255 header.type = T_ERROR;
256 } else {
257 header.type = T_SUCCESS;
258 }
259 header.size = sizeof(status);
260 /*
261 * marshal the return status. We don't check error.
262 * because we are sure we have enough space for the status
263 */
264 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
265 header.size, status);
266 if (msg_size < 0) {
267 return msg_size;
268 }
269 retval = socket_write(sockfd, iovec->iov_base, msg_size);
270 if (retval < 0) {
271 return retval;
272 }
273 return 0;
274 }
275
276 /*
277 * from man 7 capabilities, section
278 * Effect of User ID Changes on Capabilities:
279 * If the effective user ID is changed from nonzero to 0, then the permitted
280 * set is copied to the effective set. If the effective user ID is changed
281 * from 0 to nonzero, then all capabilities are are cleared from the effective
282 * set.
283 *
284 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
285 * expose the program to unwanted signals, but this is not true anymore: for an
286 * unprivileged (without CAP_KILL) program to send a signal, the real or
287 * effective user ID of the sending process must equal the real or saved user
288 * ID of the target process. Even when dropping privileges, it is enough to
289 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
290 * be exposed to signals. So just use setresuid/setresgid.
291 */
292 static int setugid(int uid, int gid, int *suid, int *sgid)
293 {
294 int retval;
295
296 /*
297 * We still need DAC_OVERRIDE because we don't change
298 * supplementary group ids, and hence may be subjected DAC rules
299 */
300 cap_value_t cap_list[] = {
301 CAP_DAC_OVERRIDE,
302 };
303
304 *suid = geteuid();
305 *sgid = getegid();
306
307 if (setresgid(-1, gid, *sgid) == -1) {
308 retval = -errno;
309 goto err_out;
310 }
311
312 if (setresuid(-1, uid, *suid) == -1) {
313 retval = -errno;
314 goto err_sgid;
315 }
316
317 if (uid != 0 || gid != 0) {
318 if (do_cap_set(cap_list, ARRAY_SIZE(cap_list), 0) < 0) {
319 retval = -errno;
320 goto err_suid;
321 }
322 }
323 return 0;
324
325 err_suid:
326 if (setresuid(-1, *suid, *suid) == -1) {
327 abort();
328 }
329 err_sgid:
330 if (setresgid(-1, *sgid, *sgid) == -1) {
331 abort();
332 }
333 err_out:
334 return retval;
335 }
336
337 /*
338 * This is used to reset the ugid back with the saved values
339 * There is nothing much we can do checking error values here.
340 */
341 static void resetugid(int suid, int sgid)
342 {
343 if (setresgid(-1, sgid, sgid) == -1) {
344 abort();
345 }
346 if (setresuid(-1, suid, suid) == -1) {
347 abort();
348 }
349 }
350
351 /*
352 * send response in two parts
353 * 1) ProxyHeader
354 * 2) Response or error status
355 * This function should be called with marshaled response
356 * send_response constructs header part and error part only.
357 * send response sends {ProxyHeader,Response} if the request was success
358 * otherwise sends {ProxyHeader,error status}
359 */
360 static int send_response(int sock, struct iovec *iovec, int size)
361 {
362 int retval;
363 ProxyHeader header;
364
365 /*
366 * If response size exceeds available iovec->iov_len,
367 * we return ENOBUFS
368 */
369 if (size > PROXY_MAX_IO_SZ) {
370 size = -ENOBUFS;
371 }
372
373 if (size < 0) {
374 /*
375 * In case of error we would not have got the error encoded
376 * already so encode the error here.
377 */
378 header.type = T_ERROR;
379 header.size = sizeof(size);
380 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
381 } else {
382 header.type = T_SUCCESS;
383 header.size = size;
384 }
385 proxy_marshal(iovec, 0, "dd", header.type, header.size);
386 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
387 if (retval < 0) {
388 return retval;
389 }
390 return 0;
391 }
392
393 /*
394 * gets generation number
395 * returns -errno on failure and sizeof(generation number) on success
396 */
397 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
398 {
399 uint64_t version;
400 int retval = -ENOTTY;
401 #ifdef FS_IOC_GETVERSION
402 int fd;
403 V9fsString path;
404 #endif
405
406
407 /* no need to issue ioctl */
408 if (!get_version) {
409 version = 0;
410 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
411 return retval;
412 }
413 #ifdef FS_IOC_GETVERSION
414 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
415 if (retval < 0) {
416 return retval;
417 }
418
419 fd = open(path.data, O_RDONLY);
420 if (fd < 0) {
421 retval = -errno;
422 goto err_out;
423 }
424 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
425 retval = -errno;
426 } else {
427 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
428 }
429 close(fd);
430 err_out:
431 v9fs_string_free(&path);
432 #endif
433 return retval;
434 }
435
436 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
437 {
438 int size = 0, offset, retval;
439 V9fsString path, name, xattr;
440
441 v9fs_string_init(&xattr);
442 v9fs_string_init(&path);
443 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
444 if (retval < 0) {
445 return retval;
446 }
447 offset = PROXY_HDR_SZ + retval;
448
449 if (size) {
450 xattr.data = g_malloc(size);
451 xattr.size = size;
452 }
453 switch (type) {
454 case T_LGETXATTR:
455 v9fs_string_init(&name);
456 retval = proxy_unmarshal(iovec, offset, "s", &name);
457 if (retval > 0) {
458 retval = lgetxattr(path.data, name.data, xattr.data, size);
459 if (retval < 0) {
460 retval = -errno;
461 } else {
462 xattr.size = retval;
463 }
464 }
465 v9fs_string_free(&name);
466 break;
467 case T_LLISTXATTR:
468 retval = llistxattr(path.data, xattr.data, size);
469 if (retval < 0) {
470 retval = -errno;
471 } else {
472 xattr.size = retval;
473 }
474 break;
475 }
476 if (retval < 0) {
477 goto err_out;
478 }
479
480 if (!size) {
481 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
482 retval = sizeof(retval);
483 } else {
484 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
485 }
486 err_out:
487 v9fs_string_free(&xattr);
488 v9fs_string_free(&path);
489 return retval;
490 }
491
492 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
493 {
494 memset(pr_stat, 0, sizeof(*pr_stat));
495 pr_stat->st_dev = stat->st_dev;
496 pr_stat->st_ino = stat->st_ino;
497 pr_stat->st_nlink = stat->st_nlink;
498 pr_stat->st_mode = stat->st_mode;
499 pr_stat->st_uid = stat->st_uid;
500 pr_stat->st_gid = stat->st_gid;
501 pr_stat->st_rdev = stat->st_rdev;
502 pr_stat->st_size = stat->st_size;
503 pr_stat->st_blksize = stat->st_blksize;
504 pr_stat->st_blocks = stat->st_blocks;
505 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
506 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
507 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
508 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
509 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
510 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
511 }
512
513 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
514 {
515 memset(pr_stfs, 0, sizeof(*pr_stfs));
516 pr_stfs->f_type = stfs->f_type;
517 pr_stfs->f_bsize = stfs->f_bsize;
518 pr_stfs->f_blocks = stfs->f_blocks;
519 pr_stfs->f_bfree = stfs->f_bfree;
520 pr_stfs->f_bavail = stfs->f_bavail;
521 pr_stfs->f_files = stfs->f_files;
522 pr_stfs->f_ffree = stfs->f_ffree;
523 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
524 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
525 pr_stfs->f_namelen = stfs->f_namelen;
526 pr_stfs->f_frsize = stfs->f_frsize;
527 }
528
529 /*
530 * Gets stat/statfs information and packs in out_iovec structure
531 * on success returns number of bytes packed in out_iovec struture
532 * otherwise returns -errno
533 */
534 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
535 {
536 int retval;
537 V9fsString path;
538 ProxyStat pr_stat;
539 ProxyStatFS pr_stfs;
540 struct stat st_buf;
541 struct statfs stfs_buf;
542
543 v9fs_string_init(&path);
544 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
545 if (retval < 0) {
546 return retval;
547 }
548
549 switch (type) {
550 case T_LSTAT:
551 retval = lstat(path.data, &st_buf);
552 if (retval < 0) {
553 retval = -errno;
554 } else {
555 stat_to_prstat(&pr_stat, &st_buf);
556 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
557 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
558 pr_stat.st_ino, pr_stat.st_nlink,
559 pr_stat.st_mode, pr_stat.st_uid,
560 pr_stat.st_gid, pr_stat.st_rdev,
561 pr_stat.st_size, pr_stat.st_blksize,
562 pr_stat.st_blocks,
563 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
564 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
565 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
566 }
567 break;
568 case T_STATFS:
569 retval = statfs(path.data, &stfs_buf);
570 if (retval < 0) {
571 retval = -errno;
572 } else {
573 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
574 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
575 "qqqqqqqqqqq", pr_stfs.f_type,
576 pr_stfs.f_bsize, pr_stfs.f_blocks,
577 pr_stfs.f_bfree, pr_stfs.f_bavail,
578 pr_stfs.f_files, pr_stfs.f_ffree,
579 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
580 pr_stfs.f_namelen, pr_stfs.f_frsize);
581 }
582 break;
583 }
584 v9fs_string_free(&path);
585 return retval;
586 }
587
588 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
589 {
590 char *buffer;
591 int size, retval;
592 V9fsString target, path;
593
594 v9fs_string_init(&path);
595 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
596 if (retval < 0) {
597 v9fs_string_free(&path);
598 return retval;
599 }
600 buffer = g_malloc(size);
601 v9fs_string_init(&target);
602 retval = readlink(path.data, buffer, size - 1);
603 if (retval > 0) {
604 buffer[retval] = '\0';
605 v9fs_string_sprintf(&target, "%s", buffer);
606 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
607 } else {
608 retval = -errno;
609 }
610 g_free(buffer);
611 v9fs_string_free(&target);
612 v9fs_string_free(&path);
613 return retval;
614 }
615
616 /*
617 * create other filesystem objects and send 0 on success
618 * return -errno on error
619 */
620 static int do_create_others(int type, struct iovec *iovec)
621 {
622 dev_t rdev;
623 int retval = 0;
624 int offset = PROXY_HDR_SZ;
625 V9fsString oldpath, path;
626 int mode, uid, gid, cur_uid, cur_gid;
627
628 v9fs_string_init(&path);
629 v9fs_string_init(&oldpath);
630
631 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
632 if (retval < 0) {
633 return retval;
634 }
635 offset += retval;
636 retval = setugid(uid, gid, &cur_uid, &cur_gid);
637 if (retval < 0) {
638 goto unmarshal_err_out;
639 }
640 switch (type) {
641 case T_MKNOD:
642 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
643 if (retval < 0) {
644 goto err_out;
645 }
646 retval = mknod(path.data, mode, rdev);
647 break;
648 case T_MKDIR:
649 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
650 if (retval < 0) {
651 goto err_out;
652 }
653 retval = mkdir(path.data, mode);
654 break;
655 case T_SYMLINK:
656 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
657 if (retval < 0) {
658 goto err_out;
659 }
660 retval = symlink(oldpath.data, path.data);
661 break;
662 }
663 if (retval < 0) {
664 retval = -errno;
665 }
666
667 err_out:
668 resetugid(cur_uid, cur_gid);
669 unmarshal_err_out:
670 v9fs_string_free(&path);
671 v9fs_string_free(&oldpath);
672 return retval;
673 }
674
675 /*
676 * create a file and send fd on success
677 * return -errno on error
678 */
679 static int do_create(struct iovec *iovec)
680 {
681 int ret;
682 V9fsString path;
683 int flags, mode, uid, gid, cur_uid, cur_gid;
684
685 v9fs_string_init(&path);
686 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
687 &path, &flags, &mode, &uid, &gid);
688 if (ret < 0) {
689 goto unmarshal_err_out;
690 }
691 ret = setugid(uid, gid, &cur_uid, &cur_gid);
692 if (ret < 0) {
693 goto unmarshal_err_out;
694 }
695 ret = open(path.data, flags, mode);
696 if (ret < 0) {
697 ret = -errno;
698 }
699
700 resetugid(cur_uid, cur_gid);
701 unmarshal_err_out:
702 v9fs_string_free(&path);
703 return ret;
704 }
705
706 /*
707 * open a file and send fd on success
708 * return -errno on error
709 */
710 static int do_open(struct iovec *iovec)
711 {
712 int flags, ret;
713 V9fsString path;
714
715 v9fs_string_init(&path);
716 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
717 if (ret < 0) {
718 goto err_out;
719 }
720 ret = open(path.data, flags);
721 if (ret < 0) {
722 ret = -errno;
723 }
724 err_out:
725 v9fs_string_free(&path);
726 return ret;
727 }
728
729 /* create unix domain socket and return the descriptor */
730 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
731 {
732 int sock, client;
733 struct sockaddr_un proxy, qemu;
734 socklen_t size;
735
736 /* requested socket already exists, refuse to start */
737 if (!access(path, F_OK)) {
738 do_log(LOG_CRIT, "socket already exists\n");
739 return -1;
740 }
741
742 g_assert(strlen(path) < sizeof(proxy.sun_path));
743 sock = socket(AF_UNIX, SOCK_STREAM, 0);
744 if (sock < 0) {
745 do_perror("socket");
746 return -1;
747 }
748
749 /* mask other part of mode bits */
750 umask(7);
751
752 proxy.sun_family = AF_UNIX;
753 strcpy(proxy.sun_path, path);
754 if (bind(sock, (struct sockaddr *)&proxy,
755 sizeof(struct sockaddr_un)) < 0) {
756 do_perror("bind");
757 goto error;
758 }
759 if (chown(proxy.sun_path, uid, gid) < 0) {
760 do_perror("chown");
761 goto error;
762 }
763 if (listen(sock, 1) < 0) {
764 do_perror("listen");
765 goto error;
766 }
767
768 size = sizeof(qemu);
769 client = accept(sock, (struct sockaddr *)&qemu, &size);
770 if (client < 0) {
771 do_perror("accept");
772 goto error;
773 }
774 close(sock);
775 return client;
776
777 error:
778 close(sock);
779 return -1;
780 }
781
782 static void usage(char *prog)
783 {
784 fprintf(stderr, "usage: %s\n"
785 " -p|--path <path> 9p path to export\n"
786 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
787 " {-s|--socket <socketname> socket file used for communication\n"
788 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
789 " access to this socket\n"
790 " \tNote: -s & -f can not be used together\n"
791 " [-n|--nodaemon] Run as a normal program\n",
792 basename(prog));
793 }
794
795 static int process_reply(int sock, int type,
796 struct iovec *out_iovec, int retval)
797 {
798 switch (type) {
799 case T_OPEN:
800 case T_CREATE:
801 if (send_fd(sock, retval) < 0) {
802 return -1;
803 }
804 break;
805 case T_MKNOD:
806 case T_MKDIR:
807 case T_SYMLINK:
808 case T_LINK:
809 case T_CHMOD:
810 case T_CHOWN:
811 case T_TRUNCATE:
812 case T_UTIME:
813 case T_RENAME:
814 case T_REMOVE:
815 case T_LSETXATTR:
816 case T_LREMOVEXATTR:
817 if (send_status(sock, out_iovec, retval) < 0) {
818 return -1;
819 }
820 break;
821 case T_LSTAT:
822 case T_STATFS:
823 case T_READLINK:
824 case T_LGETXATTR:
825 case T_LLISTXATTR:
826 case T_GETVERSION:
827 if (send_response(sock, out_iovec, retval) < 0) {
828 return -1;
829 }
830 break;
831 default:
832 return -1;
833 break;
834 }
835 return 0;
836 }
837
838 static int process_requests(int sock)
839 {
840 int flags;
841 int size = 0;
842 int retval = 0;
843 uint64_t offset;
844 ProxyHeader header;
845 int mode, uid, gid;
846 V9fsString name, value;
847 struct timespec spec[2];
848 V9fsString oldpath, path;
849 struct iovec in_iovec, out_iovec;
850
851 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
852 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
853 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
854 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
855
856 while (1) {
857 /*
858 * initialize the header type, so that we send
859 * response to proper request type.
860 */
861 header.type = 0;
862 retval = read_request(sock, &in_iovec, &header);
863 if (retval < 0) {
864 goto err_out;
865 }
866
867 switch (header.type) {
868 case T_OPEN:
869 retval = do_open(&in_iovec);
870 break;
871 case T_CREATE:
872 retval = do_create(&in_iovec);
873 break;
874 case T_MKNOD:
875 case T_MKDIR:
876 case T_SYMLINK:
877 retval = do_create_others(header.type, &in_iovec);
878 break;
879 case T_LINK:
880 v9fs_string_init(&path);
881 v9fs_string_init(&oldpath);
882 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
883 "ss", &oldpath, &path);
884 if (retval > 0) {
885 retval = link(oldpath.data, path.data);
886 if (retval < 0) {
887 retval = -errno;
888 }
889 }
890 v9fs_string_free(&oldpath);
891 v9fs_string_free(&path);
892 break;
893 case T_LSTAT:
894 case T_STATFS:
895 retval = do_stat(header.type, &in_iovec, &out_iovec);
896 break;
897 case T_READLINK:
898 retval = do_readlink(&in_iovec, &out_iovec);
899 break;
900 case T_CHMOD:
901 v9fs_string_init(&path);
902 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
903 "sd", &path, &mode);
904 if (retval > 0) {
905 retval = chmod(path.data, mode);
906 if (retval < 0) {
907 retval = -errno;
908 }
909 }
910 v9fs_string_free(&path);
911 break;
912 case T_CHOWN:
913 v9fs_string_init(&path);
914 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
915 &uid, &gid);
916 if (retval > 0) {
917 retval = lchown(path.data, uid, gid);
918 if (retval < 0) {
919 retval = -errno;
920 }
921 }
922 v9fs_string_free(&path);
923 break;
924 case T_TRUNCATE:
925 v9fs_string_init(&path);
926 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
927 &path, &offset);
928 if (retval > 0) {
929 retval = truncate(path.data, offset);
930 if (retval < 0) {
931 retval = -errno;
932 }
933 }
934 v9fs_string_free(&path);
935 break;
936 case T_UTIME:
937 v9fs_string_init(&path);
938 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
939 &spec[0].tv_sec, &spec[0].tv_nsec,
940 &spec[1].tv_sec, &spec[1].tv_nsec);
941 if (retval > 0) {
942 retval = qemu_utimens(path.data, spec);
943 if (retval < 0) {
944 retval = -errno;
945 }
946 }
947 v9fs_string_free(&path);
948 break;
949 case T_RENAME:
950 v9fs_string_init(&path);
951 v9fs_string_init(&oldpath);
952 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
953 "ss", &oldpath, &path);
954 if (retval > 0) {
955 retval = rename(oldpath.data, path.data);
956 if (retval < 0) {
957 retval = -errno;
958 }
959 }
960 v9fs_string_free(&oldpath);
961 v9fs_string_free(&path);
962 break;
963 case T_REMOVE:
964 v9fs_string_init(&path);
965 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
966 if (retval > 0) {
967 retval = remove(path.data);
968 if (retval < 0) {
969 retval = -errno;
970 }
971 }
972 v9fs_string_free(&path);
973 break;
974 case T_LGETXATTR:
975 case T_LLISTXATTR:
976 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
977 break;
978 case T_LSETXATTR:
979 v9fs_string_init(&path);
980 v9fs_string_init(&name);
981 v9fs_string_init(&value);
982 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
983 &name, &value, &size, &flags);
984 if (retval > 0) {
985 retval = lsetxattr(path.data,
986 name.data, value.data, size, flags);
987 if (retval < 0) {
988 retval = -errno;
989 }
990 }
991 v9fs_string_free(&path);
992 v9fs_string_free(&name);
993 v9fs_string_free(&value);
994 break;
995 case T_LREMOVEXATTR:
996 v9fs_string_init(&path);
997 v9fs_string_init(&name);
998 retval = proxy_unmarshal(&in_iovec,
999 PROXY_HDR_SZ, "ss", &path, &name);
1000 if (retval > 0) {
1001 retval = lremovexattr(path.data, name.data);
1002 if (retval < 0) {
1003 retval = -errno;
1004 }
1005 }
1006 v9fs_string_free(&path);
1007 v9fs_string_free(&name);
1008 break;
1009 case T_GETVERSION:
1010 retval = do_getversion(&in_iovec, &out_iovec);
1011 break;
1012 default:
1013 goto err_out;
1014 break;
1015 }
1016
1017 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1018 goto err_out;
1019 }
1020 }
1021 err_out:
1022 g_free(in_iovec.iov_base);
1023 g_free(out_iovec.iov_base);
1024 return -1;
1025 }
1026
1027 int main(int argc, char **argv)
1028 {
1029 int sock;
1030 uid_t own_u;
1031 gid_t own_g;
1032 char *rpath = NULL;
1033 char *sock_name = NULL;
1034 struct stat stbuf;
1035 int c, option_index;
1036 #ifdef FS_IOC_GETVERSION
1037 int retval;
1038 struct statfs st_fs;
1039 #endif
1040
1041 is_daemon = true;
1042 sock = -1;
1043 own_u = own_g = -1;
1044 while (1) {
1045 option_index = 0;
1046 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1047 &option_index);
1048 if (c == -1) {
1049 break;
1050 }
1051 switch (c) {
1052 case 'p':
1053 rpath = g_strdup(optarg);
1054 break;
1055 case 'n':
1056 is_daemon = false;
1057 break;
1058 case 'f':
1059 sock = atoi(optarg);
1060 break;
1061 case 's':
1062 sock_name = g_strdup(optarg);
1063 break;
1064 case 'u':
1065 own_u = atoi(optarg);
1066 break;
1067 case 'g':
1068 own_g = atoi(optarg);
1069 break;
1070 case '?':
1071 case 'h':
1072 default:
1073 usage(argv[0]);
1074 exit(EXIT_FAILURE);
1075 }
1076 }
1077
1078 /* Parameter validation */
1079 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1080 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1081 usage(argv[0]);
1082 return -1;
1083 }
1084
1085 if (sock_name && sock != -1) {
1086 fprintf(stderr, "both named socket and socket descriptor specified\n");
1087 usage(argv[0]);
1088 exit(EXIT_FAILURE);
1089 }
1090
1091 if (sock_name && (own_u == -1 || own_g == -1)) {
1092 fprintf(stderr, "owner uid:gid not specified, ");
1093 fprintf(stderr,
1094 "owner uid:gid specifies who can access the socket file\n");
1095 usage(argv[0]);
1096 exit(EXIT_FAILURE);
1097 }
1098
1099 if (lstat(rpath, &stbuf) < 0) {
1100 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1101 rpath, strerror(errno));
1102 exit(EXIT_FAILURE);
1103 }
1104
1105 if (!S_ISDIR(stbuf.st_mode)) {
1106 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1107 exit(EXIT_FAILURE);
1108 }
1109
1110 if (is_daemon) {
1111 if (daemon(0, 0) < 0) {
1112 fprintf(stderr, "daemon call failed\n");
1113 exit(EXIT_FAILURE);
1114 }
1115 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1116 }
1117
1118 do_log(LOG_INFO, "Started\n");
1119 if (sock_name) {
1120 sock = proxy_socket(sock_name, own_u, own_g);
1121 if (sock < 0) {
1122 goto error;
1123 }
1124 }
1125
1126 get_version = false;
1127 #ifdef FS_IOC_GETVERSION
1128 /* check whether underlying FS support IOC_GETVERSION */
1129 retval = statfs(rpath, &st_fs);
1130 if (!retval) {
1131 switch (st_fs.f_type) {
1132 case EXT2_SUPER_MAGIC:
1133 case BTRFS_SUPER_MAGIC:
1134 case REISERFS_SUPER_MAGIC:
1135 case XFS_SUPER_MAGIC:
1136 get_version = true;
1137 break;
1138 }
1139 }
1140 #endif
1141
1142 if (chdir("/") < 0) {
1143 do_perror("chdir");
1144 goto error;
1145 }
1146 if (chroot(rpath) < 0) {
1147 do_perror("chroot");
1148 goto error;
1149 }
1150 umask(0);
1151
1152 if (init_capabilities() < 0) {
1153 goto error;
1154 }
1155
1156 process_requests(sock);
1157 error:
1158 do_log(LOG_INFO, "Done\n");
1159 closelog();
1160 return 0;
1161 }