vm-file-editor.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <sys/stat.h>
  2. #include <sys/wait.h>
  3. #include <sys/time.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <libqubes-rpc-filecopy.h>
  11. #include "dvm2.h"
  12. #define USER_HOME "/home/user"
  13. #define TMP_LOC "/tmp/qopen/"
  14. // #define DEBUG
  15. static const char *cleanup_filename = NULL;
  16. static const char *cleanup_dirname = NULL;
  17. static void cleanup_file(void)
  18. {
  19. if (cleanup_filename) {
  20. if (unlink(cleanup_filename) < 0)
  21. fprintf(stderr, "Failed to remove file at exit\n");
  22. cleanup_filename = NULL;
  23. }
  24. if (cleanup_dirname) {
  25. if (rmdir(cleanup_dirname) < 0)
  26. fprintf(stderr, "Failed to remove directory at exit\n");
  27. cleanup_dirname = NULL;
  28. }
  29. }
  30. const char *gettime(void)
  31. {
  32. static char retbuf[60];
  33. struct timeval tv;
  34. gettimeofday(&tv, NULL);
  35. snprintf(retbuf, sizeof(retbuf), "%lld.%06lld",
  36. (long long) tv.tv_sec, (long long) tv.tv_usec);
  37. return retbuf;
  38. }
  39. static char *get_directory(void)
  40. {
  41. const char *remote_domain;
  42. char *dir;
  43. size_t len;
  44. char *ret;
  45. remote_domain = getenv("QREXEC_REMOTE_DOMAIN");
  46. if (!remote_domain) {
  47. fprintf(stderr, "Cannot get remote domain name\n");
  48. exit(1);
  49. }
  50. if (!*remote_domain || index(remote_domain, '/'))
  51. goto fail;
  52. if (!strcmp(remote_domain, ".") || !strcmp(remote_domain, ".."))
  53. goto fail;
  54. len = strlen("/tmp/-XXXXXX")+strlen(remote_domain)+1;
  55. dir = malloc(len);
  56. if (!dir) {
  57. fprintf(stderr, "Cannot allocate memory\n");
  58. exit(1);
  59. }
  60. snprintf(dir, len, "/tmp/%s-XXXXXX", remote_domain);
  61. ret = mkdtemp(dir);
  62. if (ret == NULL) {
  63. perror("mkdtemp");
  64. exit(1);
  65. }
  66. cleanup_dirname = strdup(ret);
  67. return ret;
  68. fail:
  69. fprintf(stderr, "Invalid remote domain name: %s\n", remote_domain);
  70. exit(1);
  71. }
  72. char *get_filename(int *view_only)
  73. {
  74. char buf[DVM_FILENAME_SIZE];
  75. char *fname = buf;
  76. static char *retname;
  77. int i;
  78. char *directory;
  79. size_t len;
  80. directory = get_directory();
  81. if (!read_all(0, buf, sizeof(buf)))
  82. exit(1);
  83. buf[DVM_FILENAME_SIZE-1] = 0;
  84. if (index(buf, '/')) {
  85. fprintf(stderr, "filename contains /");
  86. exit(1);
  87. }
  88. for (i=0; buf[i]!=0; i++) {
  89. // replace some characters with _ (eg mimeopen have problems with some of them)
  90. if (index(" !?\"#$%^&*()[]<>;`~|", buf[i]))
  91. buf[i]='_';
  92. }
  93. if (strncmp(buf, DVM_VIEW_ONLY_PREFIX, strlen(DVM_VIEW_ONLY_PREFIX)) == 0) {
  94. *view_only = 1;
  95. fname += strlen(DVM_VIEW_ONLY_PREFIX);
  96. }
  97. len = strlen(directory)+1+strlen(fname)+1;
  98. retname = malloc(len);
  99. if (!retname) {
  100. fprintf(stderr, "Cannot allocate memory\n");
  101. exit(1);
  102. }
  103. snprintf(retname, len, "%s/%s", directory, fname);
  104. free(directory);
  105. return retname;
  106. }
  107. void copy_file_by_name(const char *filename)
  108. {
  109. int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
  110. if (fd < 0) {
  111. perror("open file");
  112. exit(1);
  113. }
  114. /* we now have created a new file, ensure we delete it at the end */
  115. cleanup_filename = strdup(filename);
  116. atexit(cleanup_file);
  117. if (!copy_fd_all(fd, 0))
  118. exit(1);
  119. close(fd);
  120. }
  121. void send_file_back(const char * filename)
  122. {
  123. int fd = open(filename, O_RDONLY);
  124. if (fd < 0) {
  125. perror("open file");
  126. exit(1);
  127. }
  128. if (!copy_fd_all(1, fd))
  129. exit(1);
  130. close(fd);
  131. close(1);
  132. }
  133. int
  134. main()
  135. {
  136. struct stat stat_pre, stat_post, session_stat;
  137. int view_only = 0;
  138. char *filename = get_filename(&view_only);
  139. int child, status, log_fd, null_fd;
  140. FILE *waiter_pidfile;
  141. copy_file_by_name(filename);
  142. if (view_only) {
  143. // mark file as read-only so applications will signal it to the user
  144. chmod(filename, 0400);
  145. }
  146. if (stat(filename, &stat_pre)) {
  147. perror("stat pre");
  148. exit(1);
  149. }
  150. #ifdef DEBUG
  151. fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime());
  152. #endif
  153. // wait for X server to starts (especially in DispVM)
  154. if (stat("/tmp/qubes-session-env", &session_stat)) {
  155. switch (child = fork()) {
  156. case -1:
  157. perror("fork");
  158. exit(1);
  159. case 0:
  160. waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
  161. if (waiter_pidfile == NULL) {
  162. perror("fopen waiter_pidfile");
  163. exit(1);
  164. }
  165. fprintf(waiter_pidfile, "%d\n", getpid());
  166. fclose(waiter_pidfile);
  167. // check the second time, to prevent race
  168. if (stat("/tmp/qubes-session-env", &session_stat)) {
  169. // wait for qubes-session notify
  170. pause();
  171. }
  172. exit(0);
  173. default:
  174. waitpid(child, &status, 0);
  175. if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
  176. //propagate exit code from child
  177. exit(WEXITSTATUS(status));
  178. }
  179. }
  180. }
  181. #ifdef DEBUG
  182. fprintf(stderr, "time=%s, starting editor\n", gettime());
  183. #endif
  184. switch (child = fork()) {
  185. case -1:
  186. perror("fork");
  187. exit(1);
  188. case 0:
  189. null_fd = open("/dev/null", O_RDONLY);
  190. dup2(null_fd, 0);
  191. close(null_fd);
  192. log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
  193. if (log_fd == -1) {
  194. perror("open /tmp/mimeopen.log");
  195. exit(1);
  196. }
  197. dup2(log_fd, 1);
  198. close(log_fd);
  199. setenv("HOME", USER_HOME, 1);
  200. setenv("DISPLAY", ":0", 1);
  201. execl("/usr/bin/qubes-open", "qubes-open", filename, (char*)NULL);
  202. perror("execl");
  203. exit(1);
  204. default:
  205. waitpid(child, &status, 0);
  206. if (status != 0) {
  207. char cmd[512];
  208. #ifdef USE_KDIALOG
  209. snprintf(cmd, sizeof(cmd),
  210. "HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
  211. ("HOME=/home/user DISPLAY=:0 /usr/bin/kdialog --sorry 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
  212. #else
  213. snprintf(cmd, sizeof(cmd),
  214. "HOME=/home/user DISPLAY=:0 /usr/bin/zenity --error --text 'Unable to handle mimetype of the requested file (exit status: %d)!' > /tmp/kdialog.log 2>&1 </dev/null", status);
  215. #endif
  216. status = system(cmd);
  217. }
  218. }
  219. if (stat(filename, &stat_post)) {
  220. perror("stat post");
  221. exit(1);
  222. }
  223. if (stat_pre.st_mtime != stat_post.st_mtime)
  224. send_file_back(filename);
  225. free(filename);
  226. return 0;
  227. }