qvm-open-in-dvm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #define _GNU_SOURCE
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <xs.h>
  29. #include "dvm.h"
  30. void check_name(unsigned char *s)
  31. {
  32. int c;
  33. for (; *s; s++) {
  34. c = *s;
  35. if (c >= 'a' && c <= 'z')
  36. continue;
  37. if (c >= 'A' && c <= 'Z')
  38. continue;
  39. if (c == '_' || c == '-')
  40. continue;
  41. fprintf(stderr, "invalid string %s\n", s);
  42. exit(1);
  43. }
  44. }
  45. int get_and_set_seq()
  46. {
  47. int seq_fd, seq, n;
  48. mkdir(DBDIR, 0700);
  49. seq_fd = open(DBDIR "/seq", O_CREAT | O_RDWR, 0600);
  50. if (seq_fd < 0) {
  51. perror("open seq_fd");
  52. exit(1);
  53. }
  54. n = read(seq_fd, &seq, sizeof(seq));
  55. if (n < sizeof(seq))
  56. seq = 0;
  57. seq++;
  58. lseek(seq_fd, 0, SEEK_SET);
  59. write(seq_fd, &seq, sizeof(seq));
  60. close(seq_fd);
  61. return seq;
  62. }
  63. /*
  64. Write the filename we are sending to DVM to DBDIR/transaction_seq
  65. When DVM sends us a modified document via transaction with transaction_seq,
  66. we will know that we are supposed to update the document with the
  67. filename at DBDIR/transaction_seq
  68. */
  69. void write_db(char *name, int seq)
  70. {
  71. int db_fd;
  72. char dbname[256];
  73. struct stat st;
  74. if (!stat("/etc/this_is_dvm", &st))
  75. return;
  76. snprintf(dbname, sizeof(dbname), DBDIR "/%d", seq);
  77. db_fd = open(dbname, O_CREAT | O_WRONLY | O_TRUNC, 0600);
  78. if (db_fd < 0) {
  79. perror("open dbfile");
  80. exit(1);
  81. }
  82. if (write(db_fd, name, strlen(name) + 1) != strlen(name) + 1) {
  83. perror("write db");
  84. exit(1);
  85. }
  86. close(db_fd);
  87. }
  88. void copy_file(int xvdg_fd, int file_fd)
  89. {
  90. int n;
  91. char buf[4096];
  92. for (;;) {
  93. n = read(file_fd, buf, sizeof(buf));
  94. if (n < 0) {
  95. perror("read file");
  96. exit(1);
  97. }
  98. if (n == 0)
  99. break;
  100. if (write(xvdg_fd, buf, n) != n) {
  101. perror("write file");
  102. exit(1);
  103. }
  104. }
  105. }
  106. int main(int argc, char **argv)
  107. {
  108. struct dvm_header header = { 0, };
  109. struct stat st;
  110. struct xs_handle *xs;
  111. int seq;
  112. int xvdg_fd, file_fd;
  113. char *abs_filename;
  114. char buf[4096];
  115. if (argc != 3 && argc != 4) {
  116. fprintf(stderr, "usage: %s vmname file\n", argv[0]);
  117. exit(1);
  118. }
  119. check_name((unsigned char *) argv[1]);
  120. if (argv[2][0] == '/')
  121. abs_filename = argv[2];
  122. else {
  123. char cwd[4096];
  124. getcwd(cwd, sizeof(cwd));
  125. asprintf(&abs_filename, "%s/%s", cwd, argv[2]);
  126. }
  127. if (stat(abs_filename, &st)) {
  128. perror("stat file");
  129. exit(1);
  130. }
  131. header.file_size = st.st_size;
  132. strncpy(header.name, rindex(abs_filename, '/') + 1,
  133. sizeof(header.name) - 1);
  134. xs = xs_domain_open();
  135. if (!xs) {
  136. perror("xs_domain_open");
  137. exit(1);
  138. }
  139. // request a new block device at /dev/xvdg from qfileexchgd
  140. if (!xs_write(xs, 0, "device/qpen", "new", 3)) {
  141. perror("xs_write");
  142. exit(1);
  143. }
  144. while (stat("/dev/xvdg", &st))
  145. usleep(100000);
  146. xvdg_fd = open("/dev/xvdg", O_WRONLY);
  147. if (xvdg_fd < 0) {
  148. perror("open xvdg");
  149. exit(1);
  150. }
  151. setuid(getuid());
  152. if (argc == 3)
  153. // we are AppVM; get new seq
  154. seq = get_and_set_seq();
  155. else
  156. // we are DVM; use the cmdline transaction_seq
  157. seq = strtoul(argv[3], 0, 0);
  158. file_fd = open(abs_filename, O_RDONLY);
  159. if (file_fd < 0) {
  160. perror("open file");
  161. exit(1);
  162. }
  163. if (write(xvdg_fd, &header, sizeof(header)) != sizeof(header)) {
  164. perror("write filesize");
  165. exit(1);
  166. }
  167. copy_file(xvdg_fd, file_fd);
  168. close(file_fd);
  169. close(xvdg_fd);
  170. // request qfileexchgd to send our /dev/xvdg to its destination
  171. // either "disposable", which means "create DVM for me"
  172. // or vmname, meaning this is a reply to originator AppVM
  173. snprintf(buf, sizeof(buf), "send %s %d", argv[1], seq);
  174. if (!xs_write(xs, 0, "device/qpen", buf, strlen(buf))) {
  175. perror("xs_write");
  176. exit(1);
  177. }
  178. write_db(abs_filename, seq);
  179. xs_daemon_close(xs);
  180. return 0;
  181. }