qubes_add_pendrive_script.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include <sys/types.h>
  22. #include <pwd.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/inotify.h>
  26. #include <fcntl.h>
  27. #include <stdlib.h>
  28. #include <xs.h>
  29. #include <syslog.h>
  30. #include <sys/stat.h>
  31. #include <string.h>
  32. #include "dvm.h"
  33. int parse_events(char *buf, int len)
  34. {
  35. int i = 0;
  36. while (i < len) {
  37. struct inotify_event *ev =
  38. (struct inotify_event *) (buf + i);
  39. if ((ev->mask & IN_UNMOUNT) || (ev->mask & IN_IGNORED))
  40. return 1;
  41. i += sizeof(struct inotify_event) + ev->len;
  42. }
  43. return 0;
  44. }
  45. #define BUFLEN 1024
  46. void wait_for_umount(char *name)
  47. {
  48. char buf[BUFLEN];
  49. int fd = inotify_init();
  50. int len;
  51. int ret = inotify_add_watch(fd, name, IN_ATTRIB);
  52. if (ret < 0) {
  53. perror("inotify_add_watch");
  54. return;
  55. }
  56. for (;;) {
  57. len = read(fd, buf, BUFLEN - 1);
  58. if (len <= 0) {
  59. perror("read inotify");
  60. return;
  61. }
  62. if (parse_events(buf, len))
  63. return;
  64. }
  65. }
  66. void background()
  67. {
  68. int i, fd;
  69. for (i = 0; i < 256; i++)
  70. close(i);
  71. fd = open("/dev/null", O_RDWR);
  72. for (i = 0; i <= 2; i++)
  73. dup2(fd, i);
  74. switch (fork()) {
  75. case -1:
  76. exit(1);
  77. case 0:
  78. break;
  79. default:
  80. exit(0);
  81. }
  82. }
  83. int check_legal_filename(char *name)
  84. {
  85. if (index(name, '/')) {
  86. syslog(LOG_DAEMON | LOG_ERR,
  87. "the received filename contains /");
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. void drop_to_user()
  93. {
  94. struct passwd *pw = getpwnam("user");
  95. if (pw)
  96. setuid(pw->pw_uid);
  97. }
  98. int copy_from_xvdh(int destfd, int srcfd, unsigned long long count)
  99. {
  100. int n, size;
  101. char buf[4096];
  102. unsigned long long total = 0;
  103. while (total < count) {
  104. if (count - total > sizeof(buf))
  105. size = sizeof(buf);
  106. else
  107. size = count - total;
  108. n = read(srcfd, buf, size);
  109. if (n != size) {
  110. syslog(LOG_DAEMON | LOG_ERR, "reading xvdh");
  111. return 0;
  112. }
  113. if (write(destfd, buf, size) != size) {
  114. syslog(LOG_DAEMON | LOG_ERR, "writing file");
  115. return 0;
  116. }
  117. total += size;
  118. }
  119. return 1;
  120. }
  121. void redirect_stderr()
  122. {
  123. int fd =
  124. open("/var/log/dvm.log", O_CREAT | O_TRUNC | O_WRONLY, 0600);
  125. if (fd < 0) {
  126. syslog(LOG_DAEMON | LOG_ERR, "open dvm.log");
  127. exit(1);
  128. }
  129. dup2(fd, 2);
  130. }
  131. void suicide(struct xs_handle *xs)
  132. {
  133. xs_write(xs, XBT_NULL, "device/qpen", "killme", 6);
  134. xs_daemon_close(xs);
  135. exit(1);
  136. }
  137. // we are DVM, AppVM sends us a document to work on
  138. // /dev/xvdh contains the dvm_header and document data
  139. void dvm_transaction_request(char *seq, struct xs_handle *xs)
  140. {
  141. char filename[1024], cmdbuf[1024];
  142. struct dvm_header header;
  143. int xvdh_fd, file_fd;
  144. char *src_vm;
  145. unsigned int len;
  146. struct stat stat_pre, stat_post;
  147. xvdh_fd = open("/dev/xvdh", O_RDONLY);
  148. if (read(xvdh_fd, &header, sizeof(header)) != sizeof(header)) {
  149. syslog(LOG_DAEMON | LOG_ERR, "read dvm_header");
  150. suicide(xs);
  151. }
  152. header.name[sizeof(header.name) - 1] = 0;
  153. if (!check_legal_filename(header.name))
  154. suicide(xs);
  155. snprintf(filename, sizeof(filename), "/tmp/%s", header.name);
  156. drop_to_user();
  157. file_fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
  158. if (file_fd < 0) {
  159. syslog(LOG_DAEMON | LOG_ERR, "open file");
  160. suicide(xs);
  161. }
  162. if (!copy_from_xvdh(file_fd, xvdh_fd, header.file_size))
  163. suicide(xs);
  164. close(xvdh_fd);
  165. close(file_fd);
  166. if (stat(filename, &stat_pre)) {
  167. syslog(LOG_DAEMON | LOG_ERR, "stat pre");
  168. suicide(xs);
  169. }
  170. snprintf(cmdbuf, sizeof(cmdbuf),
  171. "HOME=/home/user DISPLAY=:0 /usr/bin/mimeopen -n -M '/tmp/%s' 2>&1 > /tmp/kde-open.log", header.name);
  172. if (system(cmdbuf))
  173. system("HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file!'");
  174. if (stat(filename, &stat_post)) {
  175. syslog(LOG_DAEMON | LOG_ERR, "stat post");
  176. suicide(xs);
  177. }
  178. src_vm = xs_read(xs, XBT_NULL, "qubes_blocksrc", &len);
  179. xs_write(xs, XBT_NULL, "device/qpen", "umount", 6);
  180. if (stat_pre.st_mtime == stat_post.st_mtime)
  181. suicide(xs);
  182. xs_daemon_close(xs);
  183. // if the modify timestamp of the document file has changed, write back the
  184. // modified content to the requestor AppVM
  185. execl("/usr/lib/qubes/qvm-dvm-transfer", "qvm-dvm-transfer", src_vm,
  186. filename, seq, NULL);
  187. syslog(LOG_DAEMON | LOG_ERR, "execl qvm-dvm-transfer");
  188. suicide(xs);
  189. }
  190. // we are AppVM, DVM sends us a modified document
  191. // /dev/xvdh contains the dvm_header and document data
  192. void dvm_transaction_return(char *seq_string, struct xs_handle *xs)
  193. {
  194. int seq = strtoul(seq_string, 0, 10);
  195. char db_name[1024];
  196. char file_name[1024];
  197. int db_fd, file_fd, xvdh_fd;
  198. struct dvm_header header;
  199. xvdh_fd = open("/dev/xvdh", O_RDONLY);
  200. if (xvdh_fd < 0) {
  201. syslog(LOG_DAEMON | LOG_ERR, "open xvdh");
  202. goto out_err;
  203. }
  204. if (read(xvdh_fd, &header, sizeof(header)) != sizeof(header)) {
  205. syslog(LOG_DAEMON | LOG_ERR, "read dvm_header");
  206. goto out_err;
  207. }
  208. drop_to_user();
  209. // read the file name for which the open-in-dvm with transaction=="seq" was started
  210. snprintf(db_name, sizeof(db_name), DBDIR "/%d", seq);
  211. db_fd = open(db_name, O_RDONLY);
  212. if (!db_fd) {
  213. syslog(LOG_DAEMON | LOG_ERR, "open db");
  214. goto out_err;
  215. }
  216. if (read(db_fd, file_name, sizeof(file_name)) < 0) {
  217. syslog(LOG_DAEMON | LOG_ERR, "read db");
  218. goto out_err;
  219. }
  220. close(db_fd);
  221. file_fd = open(file_name, O_WRONLY | O_TRUNC);
  222. if (file_fd < 0) {
  223. syslog(LOG_DAEMON | LOG_ERR, "open filename");
  224. goto out_err;
  225. }
  226. copy_from_xvdh(file_fd, xvdh_fd, header.file_size);
  227. close(xvdh_fd);
  228. close(file_fd);
  229. out_err:
  230. xs_write(xs, XBT_NULL, "device/qpen", "umount", 6);
  231. xs_daemon_close(xs);
  232. }
  233. void dvm_transaction(char *seq, struct xs_handle *xs)
  234. {
  235. struct stat st;
  236. redirect_stderr();
  237. if (stat("/etc/this_is_dvm", &st))
  238. dvm_transaction_return(seq, xs);
  239. else
  240. dvm_transaction_request(seq, xs);
  241. }
  242. #define MOUNTDIR "/mnt/incoming"
  243. int main()
  244. {
  245. struct xs_handle *xs;
  246. char *seq;
  247. unsigned int len;
  248. background();
  249. openlog("qubes_add_pendrive_script", LOG_CONS | LOG_PID,
  250. LOG_DAEMON);
  251. xs = xs_domain_open();
  252. if (!xs) {
  253. syslog(LOG_DAEMON | LOG_ERR, "xs_domain_open");
  254. exit(1);
  255. }
  256. seq = xs_read(xs, XBT_NULL, "qubes_transaction_seq", &len);
  257. if (seq && len > 0 && strcmp(seq, "0")) {
  258. dvm_transaction(seq, xs);
  259. exit(0);
  260. }
  261. if (!system("su - user -c 'mount " MOUNTDIR "'"))
  262. wait_for_umount(MOUNTDIR "/.");
  263. xs_write(xs, XBT_NULL, "device/qpen", "umount", 6);
  264. xs_daemon_close(xs);
  265. return 0;
  266. }