vm-file-editor.c 5.4 KB

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