vm-file-editor.c 5.4 KB

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