vm-file-editor.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(int *view_only)
  73. {
  74. char buf[DVM_FILENAME_SIZE];
  75. char *fname = buf;
  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. if (strncmp(buf, DVM_VIEW_ONLY_PREFIX, strlen(DVM_VIEW_ONLY_PREFIX)) == 0) {
  94. *view_only = 1;
  95. fname += strlen(DVM_VIEW_ONLY_PREFIX);
  96. }
  97. len = strlen(directory)+1+strlen(fname)+1;
  98. retname = malloc(len);
  99. if (!retname) {
  100. fprintf(stderr, "Cannot allocate memory\n");
  101. exit(1);
  102. }
  103. snprintf(retname, len, "%s/%s", directory, fname);
  104. free(directory);
  105. return retname;
  106. }
  107. void copy_file_by_name(const char *filename)
  108. {
  109. int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
  110. if (fd < 0) {
  111. perror("open file");
  112. exit(1);
  113. }
  114. /* we now have created a new file, ensure we delete it at the end */
  115. cleanup_filename = strdup(filename);
  116. atexit(cleanup_file);
  117. if (!copy_fd_all(fd, 0))
  118. exit(1);
  119. close(fd);
  120. }
  121. void send_file_back(const char * filename)
  122. {
  123. int fd = open(filename, O_RDONLY);
  124. if (fd < 0) {
  125. perror("open file");
  126. exit(1);
  127. }
  128. if (!copy_fd_all(1, fd))
  129. exit(1);
  130. close(fd);
  131. close(1);
  132. }
  133. int
  134. main()
  135. {
  136. struct stat stat_pre, stat_post;
  137. int view_only = 0;
  138. char *filename = get_filename(&view_only);
  139. int child, status, log_fd, null_fd;
  140. copy_file_by_name(filename);
  141. if (view_only) {
  142. // mark file as read-only so applications will signal it to the user
  143. chmod(filename, 0400);
  144. }
  145. if (stat(filename, &stat_pre)) {
  146. perror("stat pre");
  147. exit(1);
  148. }
  149. #ifdef DEBUG
  150. fprintf(stderr, "time=%s, starting editor\n", gettime());
  151. #endif
  152. switch (child = fork()) {
  153. case -1:
  154. perror("fork");
  155. exit(1);
  156. case 0:
  157. null_fd = open("/dev/null", O_RDONLY);
  158. dup2(null_fd, 0);
  159. close(null_fd);
  160. log_fd = open("/tmp/mimeopen.log", O_CREAT | O_APPEND, 0666);
  161. if (log_fd == -1) {
  162. perror("open /tmp/mimeopen.log");
  163. exit(1);
  164. }
  165. dup2(log_fd, 1);
  166. close(log_fd);
  167. setenv("HOME", USER_HOME, 1);
  168. setenv("DISPLAY", ":0", 1);
  169. execl("/usr/bin/qubes-open", "qubes-open", filename, (char*)NULL);
  170. perror("execl");
  171. exit(1);
  172. default:
  173. waitpid(child, &status, 0);
  174. if (status != 0) {
  175. char cmd[512];
  176. #ifdef USE_KDIALOG
  177. snprintf(cmd, sizeof(cmd),
  178. "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);
  179. ("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);
  180. #else
  181. snprintf(cmd, sizeof(cmd),
  182. "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);
  183. #endif
  184. status = system(cmd);
  185. }
  186. }
  187. if (stat(filename, &stat_post)) {
  188. perror("stat post");
  189. exit(1);
  190. }
  191. if (stat_pre.st_mtime != stat_post.st_mtime)
  192. send_file_back(filename);
  193. free(filename);
  194. return 0;
  195. }