vm-file-editor.c 5.4 KB

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