vm-file-editor.c 5.5 KB

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