vm-file-editor.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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(void)
  73. {
  74. char buf[DVM_FILENAME_SIZE];
  75. static char *retname;
  76. int i;
  77. char *directory;
  78. size_t len;
  79. directory = get_directory();
  80. if (!read_all(0, buf, sizeof(buf)))
  81. exit(1);
  82. buf[DVM_FILENAME_SIZE-1] = 0;
  83. if (index(buf, '/')) {
  84. fprintf(stderr, "filename contains /");
  85. exit(1);
  86. }
  87. for (i=0; buf[i]!=0; i++) {
  88. // replace some characters with _ (eg mimeopen have problems with some of them)
  89. if (index(" !?\"#$%^&*()[]<>;`~|", buf[i]))
  90. buf[i]='_';
  91. }
  92. len = strlen(directory)+1+strlen(buf)+1;
  93. retname = malloc(len);
  94. if (!retname) {
  95. fprintf(stderr, "Cannot allocate memory\n");
  96. exit(1);
  97. }
  98. snprintf(retname, len, "%s/%s", directory, buf);
  99. free(directory);
  100. return retname;
  101. }
  102. void copy_file_by_name(const char *filename)
  103. {
  104. int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
  105. if (fd < 0) {
  106. perror("open file");
  107. exit(1);
  108. }
  109. /* we now have created a new file, ensure we delete it at the end */
  110. cleanup_filename = strdup(filename);
  111. atexit(cleanup_file);
  112. if (!copy_fd_all(fd, 0))
  113. exit(1);
  114. close(fd);
  115. }
  116. void send_file_back(const char * filename)
  117. {
  118. int fd = open(filename, O_RDONLY);
  119. if (fd < 0) {
  120. perror("open file");
  121. exit(1);
  122. }
  123. if (!copy_fd_all(1, fd))
  124. exit(1);
  125. close(fd);
  126. close(1);
  127. }
  128. int
  129. main()
  130. {
  131. struct stat stat_pre, stat_post, session_stat;
  132. char *filename = get_filename();
  133. int child, status, log_fd, null_fd;
  134. FILE *waiter_pidfile;
  135. copy_file_by_name(filename);
  136. if (stat(filename, &stat_pre)) {
  137. perror("stat pre");
  138. exit(1);
  139. }
  140. #ifdef DEBUG
  141. fprintf(stderr, "time=%s, waiting for qubes-session\n", gettime());
  142. #endif
  143. // wait for X server to starts (especially in DispVM)
  144. if (stat("/tmp/qubes-session-env", &session_stat)) {
  145. switch (child = fork()) {
  146. case -1:
  147. perror("fork");
  148. exit(1);
  149. case 0:
  150. waiter_pidfile = fopen("/tmp/qubes-session-waiter", "a");
  151. if (waiter_pidfile == NULL) {
  152. perror("fopen waiter_pidfile");
  153. exit(1);
  154. }
  155. fprintf(waiter_pidfile, "%d\n", getpid());
  156. fclose(waiter_pidfile);
  157. // check the second time, to prevent race
  158. if (stat("/tmp/qubes-session-env", &session_stat)) {
  159. // wait for qubes-session notify
  160. pause();
  161. }
  162. exit(0);
  163. default:
  164. waitpid(child, &status, 0);
  165. if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
  166. //propagate exit code from child
  167. exit(WEXITSTATUS(status));
  168. }
  169. }
  170. }
  171. #ifdef DEBUG
  172. fprintf(stderr, "time=%s, starting editor\n", gettime());
  173. #endif
  174. switch (child = fork()) {
  175. case -1:
  176. perror("fork");
  177. exit(1);
  178. case 0:
  179. null_fd = open("/dev/null", O_RDONLY);
  180. dup2(null_fd, 0);
  181. close(null_fd);
  182. log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
  183. if (log_fd == -1) {
  184. perror("open /tmp/mimeopen.log");
  185. exit(1);
  186. }
  187. dup2(log_fd, 1);
  188. close(log_fd);
  189. setenv("HOME", USER_HOME, 1);
  190. setenv("DISPLAY", ":0", 1);
  191. execl("/usr/bin/qubes-open", "qubes-open", filename, (char*)NULL);
  192. perror("execl");
  193. exit(1);
  194. default:
  195. waitpid(child, &status, 0);
  196. if (status != 0) {
  197. char cmd[512];
  198. #ifdef USE_KDIALOG
  199. snprintf(cmd, sizeof(cmd),
  200. "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);
  201. ("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);
  202. #else
  203. snprintf(cmd, sizeof(cmd),
  204. "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);
  205. #endif
  206. status = system(cmd);
  207. }
  208. }
  209. if (stat(filename, &stat_post)) {
  210. perror("stat post");
  211. exit(1);
  212. }
  213. if (stat_pre.st_mtime != stat_post.st_mtime)
  214. send_file_back(filename);
  215. free(filename);
  216. return 0;
  217. }