i386: remove bios_name
[qemu.git] / fsdev / virtfs-proxy-helper.c
blob15c0e79b067b89a6253a680dbf0126c89d74ce47
1 /*
2 * Helper for QEMU Proxy FS Driver
3 * Copyright IBM, Corp. 2011
5 * Authors:
6 * M. Mohan Kumar <mohan@in.ibm.com>
8 * This work is licensed under the terms of the GNU GPL, version 2. See
9 * the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include <sys/resource.h>
14 #include <getopt.h>
15 #include <syslog.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 <cap-ng.h>
24 #include "qemu-common.h"
25 #include "qemu/sockets.h"
26 #include "qemu/xattr.h"
27 #include "9p-iov-marshal.h"
28 #include "hw/9pfs/9p-proxy.h"
29 #include "fsdev/9p-iov-marshal.h"
31 #define PROGNAME "virtfs-proxy-helper"
33 #ifndef XFS_SUPER_MAGIC
34 #define XFS_SUPER_MAGIC 0x58465342
35 #endif
36 #ifndef EXT2_SUPER_MAGIC
37 #define EXT2_SUPER_MAGIC 0xEF53
38 #endif
39 #ifndef REISERFS_SUPER_MAGIC
40 #define REISERFS_SUPER_MAGIC 0x52654973
41 #endif
42 #ifndef BTRFS_SUPER_MAGIC
43 #define BTRFS_SUPER_MAGIC 0x9123683E
44 #endif
46 static const struct option helper_opts[] = {
47 {"fd", required_argument, NULL, 'f'},
48 {"path", required_argument, NULL, 'p'},
49 {"nodaemon", no_argument, NULL, 'n'},
50 {"socket", required_argument, NULL, 's'},
51 {"uid", required_argument, NULL, 'u'},
52 {"gid", required_argument, NULL, 'g'},
53 {},
56 static bool is_daemon;
57 static bool get_version; /* IOC getversion IOCTL supported */
58 static char *prog_name;
60 static void GCC_FMT_ATTR(2, 3) do_log(int loglevel, const char *format, ...)
62 va_list ap;
64 va_start(ap, format);
65 if (is_daemon) {
66 vsyslog(LOG_CRIT, format, ap);
67 } else {
68 vfprintf(stderr, format, ap);
70 va_end(ap);
73 static void do_perror(const char *string)
75 if (is_daemon) {
76 syslog(LOG_CRIT, "%s:%s", string, strerror(errno));
77 } else {
78 fprintf(stderr, "%s:%s\n", string, strerror(errno));
82 static int init_capabilities(void)
84 /* helper needs following capabilities only */
85 int cap_list[] = {
86 CAP_CHOWN,
87 CAP_DAC_OVERRIDE,
88 CAP_FOWNER,
89 CAP_FSETID,
90 CAP_SETGID,
91 CAP_MKNOD,
92 CAP_SETUID,
94 int i;
96 capng_clear(CAPNG_SELECT_BOTH);
97 for (i = 0; i < ARRAY_SIZE(cap_list); i++) {
98 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
99 cap_list[i]) < 0) {
100 do_perror("capng_update");
101 return -1;
104 if (capng_apply(CAPNG_SELECT_BOTH) < 0) {
105 do_perror("capng_apply");
106 return -1;
109 /* Prepare effective set for setugid. */
110 for (i = 0; i < ARRAY_SIZE(cap_list); i++) {
111 if (cap_list[i] == CAP_DAC_OVERRIDE) {
112 continue;
115 if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE,
116 cap_list[i]) < 0) {
117 do_perror("capng_update");
118 return -1;
121 return 0;
124 static int socket_read(int sockfd, void *buff, ssize_t size)
126 ssize_t retval, total = 0;
128 while (size) {
129 retval = read(sockfd, buff, size);
130 if (retval == 0) {
131 return -EIO;
133 if (retval < 0) {
134 if (errno == EINTR) {
135 continue;
137 return -errno;
139 size -= retval;
140 buff += retval;
141 total += retval;
143 return total;
146 static int socket_write(int sockfd, void *buff, ssize_t size)
148 ssize_t retval, total = 0;
150 while (size) {
151 retval = write(sockfd, buff, size);
152 if (retval < 0) {
153 if (errno == EINTR) {
154 continue;
156 return -errno;
158 size -= retval;
159 buff += retval;
160 total += retval;
162 return total;
165 static int read_request(int sockfd, struct iovec *iovec, ProxyHeader *header)
167 int retval;
170 * read the request header.
172 iovec->iov_len = 0;
173 retval = socket_read(sockfd, iovec->iov_base, PROXY_HDR_SZ);
174 if (retval < 0) {
175 return retval;
177 iovec->iov_len = PROXY_HDR_SZ;
178 retval = proxy_unmarshal(iovec, 0, "dd", &header->type, &header->size);
179 if (retval < 0) {
180 return retval;
183 * We can't process message.size > PROXY_MAX_IO_SZ.
184 * Treat it as fatal error
186 if (header->size > PROXY_MAX_IO_SZ) {
187 return -ENOBUFS;
189 retval = socket_read(sockfd, iovec->iov_base + PROXY_HDR_SZ, header->size);
190 if (retval < 0) {
191 return retval;
193 iovec->iov_len += header->size;
194 return 0;
197 static int send_fd(int sockfd, int fd)
199 struct msghdr msg;
200 struct iovec iov;
201 int retval, data;
202 struct cmsghdr *cmsg;
203 union MsgControl msg_control;
205 iov.iov_base = &data;
206 iov.iov_len = sizeof(data);
208 memset(&msg, 0, sizeof(msg));
209 msg.msg_iov = &iov;
210 msg.msg_iovlen = 1;
211 /* No ancillary data on error */
212 if (fd < 0) {
213 /* fd is really negative errno if the request failed */
214 data = fd;
215 } else {
216 data = V9FS_FD_VALID;
217 msg.msg_control = &msg_control;
218 msg.msg_controllen = sizeof(msg_control);
220 cmsg = &msg_control.cmsg;
221 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
222 cmsg->cmsg_level = SOL_SOCKET;
223 cmsg->cmsg_type = SCM_RIGHTS;
224 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
227 do {
228 retval = sendmsg(sockfd, &msg, 0);
229 } while (retval < 0 && errno == EINTR);
230 if (fd >= 0) {
231 close(fd);
233 if (retval < 0) {
234 return retval;
236 return 0;
239 static int send_status(int sockfd, struct iovec *iovec, int status)
241 ProxyHeader header;
242 int retval, msg_size;
244 if (status < 0) {
245 header.type = T_ERROR;
246 } else {
247 header.type = T_SUCCESS;
249 header.size = sizeof(status);
251 * marshal the return status. We don't check error.
252 * because we are sure we have enough space for the status
254 msg_size = proxy_marshal(iovec, 0, "ddd", header.type,
255 header.size, status);
256 if (msg_size < 0) {
257 return msg_size;
259 retval = socket_write(sockfd, iovec->iov_base, msg_size);
260 if (retval < 0) {
261 return retval;
263 return 0;
267 * from man 7 capabilities, section
268 * Effect of User ID Changes on Capabilities:
269 * If the effective user ID is changed from nonzero to 0, then the permitted
270 * set is copied to the effective set. If the effective user ID is changed
271 * from 0 to nonzero, then all capabilities are are cleared from the effective
272 * set.
274 * The setfsuid/setfsgid man pages warn that changing the effective user ID may
275 * expose the program to unwanted signals, but this is not true anymore: for an
276 * unprivileged (without CAP_KILL) program to send a signal, the real or
277 * effective user ID of the sending process must equal the real or saved user
278 * ID of the target process. Even when dropping privileges, it is enough to
279 * keep the saved UID to a "privileged" value and virtfs-proxy-helper won't
280 * be exposed to signals. So just use setresuid/setresgid.
282 static int setugid(int uid, int gid, int *suid, int *sgid)
284 int retval;
286 *suid = geteuid();
287 *sgid = getegid();
289 if (setresgid(-1, gid, *sgid) == -1) {
290 return -errno;
293 if (setresuid(-1, uid, *suid) == -1) {
294 retval = -errno;
295 goto err_sgid;
298 if (uid == 0 && gid == 0) {
299 /* Linux has already copied the permitted set to the effective set. */
300 return 0;
304 * All capabilities have been cleared from the effective set. However
305 * we still need DAC_OVERRIDE because we don't change supplementary
306 * group ids, and hence may be subject to DAC rules. init_capabilities
307 * left the set of capabilities that we want in libcap-ng's state.
309 if (capng_apply(CAPNG_SELECT_CAPS) < 0) {
310 retval = -errno;
311 do_perror("capng_apply");
312 goto err_suid;
314 return 0;
316 err_suid:
317 if (setresuid(-1, *suid, *suid) == -1) {
318 abort();
320 err_sgid:
321 if (setresgid(-1, *sgid, *sgid) == -1) {
322 abort();
324 return retval;
328 * This is used to reset the ugid back with the saved values
329 * There is nothing much we can do checking error values here.
331 static void resetugid(int suid, int sgid)
333 if (setresgid(-1, sgid, sgid) == -1) {
334 abort();
336 if (setresuid(-1, suid, suid) == -1) {
337 abort();
342 * send response in two parts
343 * 1) ProxyHeader
344 * 2) Response or error status
345 * This function should be called with marshaled response
346 * send_response constructs header part and error part only.
347 * send response sends {ProxyHeader,Response} if the request was success
348 * otherwise sends {ProxyHeader,error status}
350 static int send_response(int sock, struct iovec *iovec, int size)
352 int retval;
353 ProxyHeader header;
356 * If response size exceeds available iovec->iov_len,
357 * we return ENOBUFS
359 if (size > PROXY_MAX_IO_SZ) {
360 size = -ENOBUFS;
363 if (size < 0) {
365 * In case of error we would not have got the error encoded
366 * already so encode the error here.
368 header.type = T_ERROR;
369 header.size = sizeof(size);
370 proxy_marshal(iovec, PROXY_HDR_SZ, "d", size);
371 } else {
372 header.type = T_SUCCESS;
373 header.size = size;
375 proxy_marshal(iovec, 0, "dd", header.type, header.size);
376 retval = socket_write(sock, iovec->iov_base, header.size + PROXY_HDR_SZ);
377 if (retval < 0) {
378 return retval;
380 return 0;
384 * gets generation number
385 * returns -errno on failure and sizeof(generation number) on success
387 static int do_getversion(struct iovec *iovec, struct iovec *out_iovec)
389 uint64_t version;
390 int retval = -ENOTTY;
391 #ifdef FS_IOC_GETVERSION
392 int fd;
393 V9fsString path;
394 #endif
397 /* no need to issue ioctl */
398 if (!get_version) {
399 version = 0;
400 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
401 return retval;
403 #ifdef FS_IOC_GETVERSION
404 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
405 if (retval < 0) {
406 return retval;
409 fd = open(path.data, O_RDONLY);
410 if (fd < 0) {
411 retval = -errno;
412 goto err_out;
414 if (ioctl(fd, FS_IOC_GETVERSION, &version) < 0) {
415 retval = -errno;
416 } else {
417 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "q", version);
419 close(fd);
420 err_out:
421 v9fs_string_free(&path);
422 #endif
423 return retval;
426 static int do_getxattr(int type, struct iovec *iovec, struct iovec *out_iovec)
428 int size = 0, offset, retval;
429 V9fsString path, name, xattr;
431 v9fs_string_init(&xattr);
432 v9fs_string_init(&path);
433 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "ds", &size, &path);
434 if (retval < 0) {
435 return retval;
437 offset = PROXY_HDR_SZ + retval;
439 if (size) {
440 xattr.data = g_malloc(size);
441 xattr.size = size;
443 switch (type) {
444 case T_LGETXATTR:
445 v9fs_string_init(&name);
446 retval = proxy_unmarshal(iovec, offset, "s", &name);
447 if (retval > 0) {
448 retval = lgetxattr(path.data, name.data, xattr.data, size);
449 if (retval < 0) {
450 retval = -errno;
451 } else {
452 xattr.size = retval;
455 v9fs_string_free(&name);
456 break;
457 case T_LLISTXATTR:
458 retval = llistxattr(path.data, xattr.data, size);
459 if (retval < 0) {
460 retval = -errno;
461 } else {
462 xattr.size = retval;
464 break;
466 if (retval < 0) {
467 goto err_out;
470 if (!size) {
471 proxy_marshal(out_iovec, PROXY_HDR_SZ, "d", retval);
472 retval = sizeof(retval);
473 } else {
474 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &xattr);
476 err_out:
477 v9fs_string_free(&xattr);
478 v9fs_string_free(&path);
479 return retval;
482 static void stat_to_prstat(ProxyStat *pr_stat, struct stat *stat)
484 memset(pr_stat, 0, sizeof(*pr_stat));
485 pr_stat->st_dev = stat->st_dev;
486 pr_stat->st_ino = stat->st_ino;
487 pr_stat->st_nlink = stat->st_nlink;
488 pr_stat->st_mode = stat->st_mode;
489 pr_stat->st_uid = stat->st_uid;
490 pr_stat->st_gid = stat->st_gid;
491 pr_stat->st_rdev = stat->st_rdev;
492 pr_stat->st_size = stat->st_size;
493 pr_stat->st_blksize = stat->st_blksize;
494 pr_stat->st_blocks = stat->st_blocks;
495 pr_stat->st_atim_sec = stat->st_atim.tv_sec;
496 pr_stat->st_atim_nsec = stat->st_atim.tv_nsec;
497 pr_stat->st_mtim_sec = stat->st_mtim.tv_sec;
498 pr_stat->st_mtim_nsec = stat->st_mtim.tv_nsec;
499 pr_stat->st_ctim_sec = stat->st_ctim.tv_sec;
500 pr_stat->st_ctim_nsec = stat->st_ctim.tv_nsec;
503 static void statfs_to_prstatfs(ProxyStatFS *pr_stfs, struct statfs *stfs)
505 memset(pr_stfs, 0, sizeof(*pr_stfs));
506 pr_stfs->f_type = stfs->f_type;
507 pr_stfs->f_bsize = stfs->f_bsize;
508 pr_stfs->f_blocks = stfs->f_blocks;
509 pr_stfs->f_bfree = stfs->f_bfree;
510 pr_stfs->f_bavail = stfs->f_bavail;
511 pr_stfs->f_files = stfs->f_files;
512 pr_stfs->f_ffree = stfs->f_ffree;
513 pr_stfs->f_fsid[0] = stfs->f_fsid.__val[0];
514 pr_stfs->f_fsid[1] = stfs->f_fsid.__val[1];
515 pr_stfs->f_namelen = stfs->f_namelen;
516 pr_stfs->f_frsize = stfs->f_frsize;
520 * Gets stat/statfs information and packs in out_iovec structure
521 * on success returns number of bytes packed in out_iovec structure
522 * otherwise returns -errno
524 static int do_stat(int type, struct iovec *iovec, struct iovec *out_iovec)
526 int retval;
527 V9fsString path;
528 ProxyStat pr_stat;
529 ProxyStatFS pr_stfs;
530 struct stat st_buf;
531 struct statfs stfs_buf;
533 v9fs_string_init(&path);
534 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "s", &path);
535 if (retval < 0) {
536 return retval;
539 switch (type) {
540 case T_LSTAT:
541 retval = lstat(path.data, &st_buf);
542 if (retval < 0) {
543 retval = -errno;
544 } else {
545 stat_to_prstat(&pr_stat, &st_buf);
546 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
547 "qqqdddqqqqqqqqqq", pr_stat.st_dev,
548 pr_stat.st_ino, pr_stat.st_nlink,
549 pr_stat.st_mode, pr_stat.st_uid,
550 pr_stat.st_gid, pr_stat.st_rdev,
551 pr_stat.st_size, pr_stat.st_blksize,
552 pr_stat.st_blocks,
553 pr_stat.st_atim_sec, pr_stat.st_atim_nsec,
554 pr_stat.st_mtim_sec, pr_stat.st_mtim_nsec,
555 pr_stat.st_ctim_sec, pr_stat.st_ctim_nsec);
557 break;
558 case T_STATFS:
559 retval = statfs(path.data, &stfs_buf);
560 if (retval < 0) {
561 retval = -errno;
562 } else {
563 statfs_to_prstatfs(&pr_stfs, &stfs_buf);
564 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ,
565 "qqqqqqqqqqq", pr_stfs.f_type,
566 pr_stfs.f_bsize, pr_stfs.f_blocks,
567 pr_stfs.f_bfree, pr_stfs.f_bavail,
568 pr_stfs.f_files, pr_stfs.f_ffree,
569 pr_stfs.f_fsid[0], pr_stfs.f_fsid[1],
570 pr_stfs.f_namelen, pr_stfs.f_frsize);
572 break;
574 v9fs_string_free(&path);
575 return retval;
578 static int do_readlink(struct iovec *iovec, struct iovec *out_iovec)
580 char *buffer;
581 int size, retval;
582 V9fsString target, path;
584 v9fs_string_init(&path);
585 retval = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &size);
586 if (retval < 0) {
587 v9fs_string_free(&path);
588 return retval;
590 buffer = g_malloc(size);
591 v9fs_string_init(&target);
592 retval = readlink(path.data, buffer, size - 1);
593 if (retval > 0) {
594 buffer[retval] = '\0';
595 v9fs_string_sprintf(&target, "%s", buffer);
596 retval = proxy_marshal(out_iovec, PROXY_HDR_SZ, "s", &target);
597 } else {
598 retval = -errno;
600 g_free(buffer);
601 v9fs_string_free(&target);
602 v9fs_string_free(&path);
603 return retval;
607 * create other filesystem objects and send 0 on success
608 * return -errno on error
610 static int do_create_others(int type, struct iovec *iovec)
612 dev_t rdev;
613 int retval = 0;
614 int offset = PROXY_HDR_SZ;
615 V9fsString oldpath, path;
616 int mode, uid, gid, cur_uid, cur_gid;
618 v9fs_string_init(&path);
619 v9fs_string_init(&oldpath);
621 retval = proxy_unmarshal(iovec, offset, "dd", &uid, &gid);
622 if (retval < 0) {
623 return retval;
625 offset += retval;
626 retval = setugid(uid, gid, &cur_uid, &cur_gid);
627 if (retval < 0) {
628 goto unmarshal_err_out;
630 switch (type) {
631 case T_MKNOD:
632 retval = proxy_unmarshal(iovec, offset, "sdq", &path, &mode, &rdev);
633 if (retval < 0) {
634 goto err_out;
636 retval = mknod(path.data, mode, rdev);
637 break;
638 case T_MKDIR:
639 retval = proxy_unmarshal(iovec, offset, "sd", &path, &mode);
640 if (retval < 0) {
641 goto err_out;
643 retval = mkdir(path.data, mode);
644 break;
645 case T_SYMLINK:
646 retval = proxy_unmarshal(iovec, offset, "ss", &oldpath, &path);
647 if (retval < 0) {
648 goto err_out;
650 retval = symlink(oldpath.data, path.data);
651 break;
653 if (retval < 0) {
654 retval = -errno;
657 err_out:
658 resetugid(cur_uid, cur_gid);
659 unmarshal_err_out:
660 v9fs_string_free(&path);
661 v9fs_string_free(&oldpath);
662 return retval;
666 * create a file and send fd on success
667 * return -errno on error
669 static int do_create(struct iovec *iovec)
671 int ret;
672 V9fsString path;
673 int flags, mode, uid, gid, cur_uid, cur_gid;
675 v9fs_string_init(&path);
676 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sdddd",
677 &path, &flags, &mode, &uid, &gid);
678 if (ret < 0) {
679 goto unmarshal_err_out;
681 ret = setugid(uid, gid, &cur_uid, &cur_gid);
682 if (ret < 0) {
683 goto unmarshal_err_out;
685 ret = open(path.data, flags, mode);
686 if (ret < 0) {
687 ret = -errno;
690 resetugid(cur_uid, cur_gid);
691 unmarshal_err_out:
692 v9fs_string_free(&path);
693 return ret;
697 * open a file and send fd on success
698 * return -errno on error
700 static int do_open(struct iovec *iovec)
702 int flags, ret;
703 V9fsString path;
705 v9fs_string_init(&path);
706 ret = proxy_unmarshal(iovec, PROXY_HDR_SZ, "sd", &path, &flags);
707 if (ret < 0) {
708 goto err_out;
710 ret = open(path.data, flags);
711 if (ret < 0) {
712 ret = -errno;
714 err_out:
715 v9fs_string_free(&path);
716 return ret;
719 /* create unix domain socket and return the descriptor */
720 static int proxy_socket(const char *path, uid_t uid, gid_t gid)
722 int sock, client;
723 struct sockaddr_un proxy, qemu;
724 socklen_t size;
726 /* requested socket already exists, refuse to start */
727 if (!access(path, F_OK)) {
728 do_log(LOG_CRIT, "socket already exists\n");
729 return -1;
732 if (strlen(path) >= sizeof(proxy.sun_path)) {
733 do_log(LOG_CRIT, "UNIX domain socket path exceeds %zu characters\n",
734 sizeof(proxy.sun_path));
735 return -1;
738 sock = socket(AF_UNIX, SOCK_STREAM, 0);
739 if (sock < 0) {
740 do_perror("socket");
741 return -1;
744 /* mask other part of mode bits */
745 umask(7);
747 proxy.sun_family = AF_UNIX;
748 strcpy(proxy.sun_path, path);
749 if (bind(sock, (struct sockaddr *)&proxy,
750 sizeof(struct sockaddr_un)) < 0) {
751 do_perror("bind");
752 goto error;
754 if (chown(proxy.sun_path, uid, gid) < 0) {
755 do_perror("chown");
756 goto error;
758 if (listen(sock, 1) < 0) {
759 do_perror("listen");
760 goto error;
763 size = sizeof(qemu);
764 client = accept(sock, (struct sockaddr *)&qemu, &size);
765 if (client < 0) {
766 do_perror("accept");
767 goto error;
769 close(sock);
770 return client;
772 error:
773 close(sock);
774 return -1;
777 static void usage(void)
779 fprintf(stderr, "usage: %s\n"
780 " -p|--path <path> 9p path to export\n"
781 " {-f|--fd <socket-descriptor>} socket file descriptor to be used\n"
782 " {-s|--socket <socketname> socket file used for communication\n"
783 " \t-u|--uid <uid> -g|--gid <gid>} - uid:gid combination to give "
784 " access to this socket\n"
785 " \tNote: -s & -f can not be used together\n"
786 " [-n|--nodaemon] Run as a normal program\n",
787 prog_name);
790 static int process_reply(int sock, int type,
791 struct iovec *out_iovec, int retval)
793 switch (type) {
794 case T_OPEN:
795 case T_CREATE:
796 if (send_fd(sock, retval) < 0) {
797 return -1;
799 break;
800 case T_MKNOD:
801 case T_MKDIR:
802 case T_SYMLINK:
803 case T_LINK:
804 case T_CHMOD:
805 case T_CHOWN:
806 case T_TRUNCATE:
807 case T_UTIME:
808 case T_RENAME:
809 case T_REMOVE:
810 case T_LSETXATTR:
811 case T_LREMOVEXATTR:
812 if (send_status(sock, out_iovec, retval) < 0) {
813 return -1;
815 break;
816 case T_LSTAT:
817 case T_STATFS:
818 case T_READLINK:
819 case T_LGETXATTR:
820 case T_LLISTXATTR:
821 case T_GETVERSION:
822 if (send_response(sock, out_iovec, retval) < 0) {
823 return -1;
825 break;
826 default:
827 return -1;
828 break;
830 return 0;
833 static int process_requests(int sock)
835 int flags;
836 int size = 0;
837 int retval = 0;
838 uint64_t offset;
839 ProxyHeader header;
840 int mode, uid, gid;
841 V9fsString name, value;
842 struct timespec spec[2];
843 V9fsString oldpath, path;
844 struct iovec in_iovec, out_iovec;
846 in_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
847 in_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
848 out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
849 out_iovec.iov_len = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
851 while (1) {
853 * initialize the header type, so that we send
854 * response to proper request type.
856 header.type = 0;
857 retval = read_request(sock, &in_iovec, &header);
858 if (retval < 0) {
859 goto err_out;
862 switch (header.type) {
863 case T_OPEN:
864 retval = do_open(&in_iovec);
865 break;
866 case T_CREATE:
867 retval = do_create(&in_iovec);
868 break;
869 case T_MKNOD:
870 case T_MKDIR:
871 case T_SYMLINK:
872 retval = do_create_others(header.type, &in_iovec);
873 break;
874 case T_LINK:
875 v9fs_string_init(&path);
876 v9fs_string_init(&oldpath);
877 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
878 "ss", &oldpath, &path);
879 if (retval > 0) {
880 retval = link(oldpath.data, path.data);
881 if (retval < 0) {
882 retval = -errno;
885 v9fs_string_free(&oldpath);
886 v9fs_string_free(&path);
887 break;
888 case T_LSTAT:
889 case T_STATFS:
890 retval = do_stat(header.type, &in_iovec, &out_iovec);
891 break;
892 case T_READLINK:
893 retval = do_readlink(&in_iovec, &out_iovec);
894 break;
895 case T_CHMOD:
896 v9fs_string_init(&path);
897 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
898 "sd", &path, &mode);
899 if (retval > 0) {
900 retval = chmod(path.data, mode);
901 if (retval < 0) {
902 retval = -errno;
905 v9fs_string_free(&path);
906 break;
907 case T_CHOWN:
908 v9fs_string_init(&path);
909 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sdd", &path,
910 &uid, &gid);
911 if (retval > 0) {
912 retval = lchown(path.data, uid, gid);
913 if (retval < 0) {
914 retval = -errno;
917 v9fs_string_free(&path);
918 break;
919 case T_TRUNCATE:
920 v9fs_string_init(&path);
921 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sq",
922 &path, &offset);
923 if (retval > 0) {
924 retval = truncate(path.data, offset);
925 if (retval < 0) {
926 retval = -errno;
929 v9fs_string_free(&path);
930 break;
931 case T_UTIME:
932 v9fs_string_init(&path);
933 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sqqqq", &path,
934 &spec[0].tv_sec, &spec[0].tv_nsec,
935 &spec[1].tv_sec, &spec[1].tv_nsec);
936 if (retval > 0) {
937 retval = utimensat(AT_FDCWD, path.data, spec,
938 AT_SYMLINK_NOFOLLOW);
939 if (retval < 0) {
940 retval = -errno;
943 v9fs_string_free(&path);
944 break;
945 case T_RENAME:
946 v9fs_string_init(&path);
947 v9fs_string_init(&oldpath);
948 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ,
949 "ss", &oldpath, &path);
950 if (retval > 0) {
951 retval = rename(oldpath.data, path.data);
952 if (retval < 0) {
953 retval = -errno;
956 v9fs_string_free(&oldpath);
957 v9fs_string_free(&path);
958 break;
959 case T_REMOVE:
960 v9fs_string_init(&path);
961 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "s", &path);
962 if (retval > 0) {
963 retval = remove(path.data);
964 if (retval < 0) {
965 retval = -errno;
968 v9fs_string_free(&path);
969 break;
970 case T_LGETXATTR:
971 case T_LLISTXATTR:
972 retval = do_getxattr(header.type, &in_iovec, &out_iovec);
973 break;
974 case T_LSETXATTR:
975 v9fs_string_init(&path);
976 v9fs_string_init(&name);
977 v9fs_string_init(&value);
978 retval = proxy_unmarshal(&in_iovec, PROXY_HDR_SZ, "sssdd", &path,
979 &name, &value, &size, &flags);
980 if (retval > 0) {
981 retval = lsetxattr(path.data,
982 name.data, value.data, size, flags);
983 if (retval < 0) {
984 retval = -errno;
987 v9fs_string_free(&path);
988 v9fs_string_free(&name);
989 v9fs_string_free(&value);
990 break;
991 case T_LREMOVEXATTR:
992 v9fs_string_init(&path);
993 v9fs_string_init(&name);
994 retval = proxy_unmarshal(&in_iovec,
995 PROXY_HDR_SZ, "ss", &path, &name);
996 if (retval > 0) {
997 retval = lremovexattr(path.data, name.data);
998 if (retval < 0) {
999 retval = -errno;
1002 v9fs_string_free(&path);
1003 v9fs_string_free(&name);
1004 break;
1005 case T_GETVERSION:
1006 retval = do_getversion(&in_iovec, &out_iovec);
1007 break;
1008 default:
1009 goto err_out;
1010 break;
1013 if (process_reply(sock, header.type, &out_iovec, retval) < 0) {
1014 goto err_out;
1017 err_out:
1018 g_free(in_iovec.iov_base);
1019 g_free(out_iovec.iov_base);
1020 return -1;
1023 int main(int argc, char **argv)
1025 int sock;
1026 uid_t own_u;
1027 gid_t own_g;
1028 char *rpath = NULL;
1029 char *sock_name = NULL;
1030 struct stat stbuf;
1031 int c, option_index;
1032 #ifdef FS_IOC_GETVERSION
1033 int retval;
1034 struct statfs st_fs;
1035 #endif
1037 prog_name = g_path_get_basename(argv[0]);
1039 is_daemon = true;
1040 sock = -1;
1041 own_u = own_g = -1;
1042 while (1) {
1043 option_index = 0;
1044 c = getopt_long(argc, argv, "p:nh?f:s:u:g:", helper_opts,
1045 &option_index);
1046 if (c == -1) {
1047 break;
1049 switch (c) {
1050 case 'p':
1051 rpath = g_strdup(optarg);
1052 break;
1053 case 'n':
1054 is_daemon = false;
1055 break;
1056 case 'f':
1057 sock = atoi(optarg);
1058 break;
1059 case 's':
1060 sock_name = g_strdup(optarg);
1061 break;
1062 case 'u':
1063 own_u = atoi(optarg);
1064 break;
1065 case 'g':
1066 own_g = atoi(optarg);
1067 break;
1068 case '?':
1069 case 'h':
1070 default:
1071 usage();
1072 exit(EXIT_FAILURE);
1076 /* Parameter validation */
1077 if ((sock_name == NULL && sock == -1) || rpath == NULL) {
1078 fprintf(stderr, "socket, socket descriptor or path not specified\n");
1079 usage();
1080 return -1;
1083 if (sock_name && sock != -1) {
1084 fprintf(stderr, "both named socket and socket descriptor specified\n");
1085 usage();
1086 exit(EXIT_FAILURE);
1089 if (sock_name && (own_u == -1 || own_g == -1)) {
1090 fprintf(stderr, "owner uid:gid not specified, ");
1091 fprintf(stderr,
1092 "owner uid:gid specifies who can access the socket file\n");
1093 usage();
1094 exit(EXIT_FAILURE);
1097 if (lstat(rpath, &stbuf) < 0) {
1098 fprintf(stderr, "invalid path \"%s\" specified, %s\n",
1099 rpath, strerror(errno));
1100 exit(EXIT_FAILURE);
1103 if (!S_ISDIR(stbuf.st_mode)) {
1104 fprintf(stderr, "specified path \"%s\" is not directory\n", rpath);
1105 exit(EXIT_FAILURE);
1108 if (is_daemon) {
1109 if (daemon(0, 0) < 0) {
1110 fprintf(stderr, "daemon call failed\n");
1111 exit(EXIT_FAILURE);
1113 openlog(PROGNAME, LOG_PID, LOG_DAEMON);
1116 do_log(LOG_INFO, "Started\n");
1117 if (sock_name) {
1118 sock = proxy_socket(sock_name, own_u, own_g);
1119 if (sock < 0) {
1120 goto error;
1124 if (chroot(rpath) < 0) {
1125 do_perror("chroot");
1126 goto error;
1128 if (chdir("/") < 0) {
1129 do_perror("chdir");
1130 goto error;
1133 get_version = false;
1134 #ifdef FS_IOC_GETVERSION
1135 /* check whether underlying FS support IOC_GETVERSION */
1136 retval = statfs("/", &st_fs);
1137 if (!retval) {
1138 switch (st_fs.f_type) {
1139 case EXT2_SUPER_MAGIC:
1140 case BTRFS_SUPER_MAGIC:
1141 case REISERFS_SUPER_MAGIC:
1142 case XFS_SUPER_MAGIC:
1143 get_version = true;
1144 break;
1147 #endif
1149 umask(0);
1150 if (init_capabilities() < 0) {
1151 goto error;
1154 process_requests(sock);
1155 error:
1156 g_free(rpath);
1157 g_free(sock_name);
1158 do_log(LOG_INFO, "Done\n");
1159 closelog();
1160 return 0;