qrexec-client-vm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #define _GNU_SOURCE
  22. #include <sys/socket.h>
  23. #include <sys/wait.h>
  24. #include <sys/un.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include "libqrexec-utils.h"
  31. #include "qrexec.h"
  32. #include "qrexec-agent.h"
  33. void handle_vchan_error(const char *op)
  34. {
  35. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  36. exit(1);
  37. }
  38. void do_exec(const char *cmd __attribute__((__unused__))) {
  39. fprintf(stderr, "BUG: do_exec function shouldn't be called!\n");
  40. exit(1);
  41. }
  42. int connect_unix_socket(char *path)
  43. {
  44. int s, len;
  45. struct sockaddr_un remote;
  46. if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  47. perror("socket");
  48. return -1;
  49. }
  50. remote.sun_family = AF_UNIX;
  51. strncpy(remote.sun_path, path,
  52. sizeof(remote.sun_path));
  53. len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  54. if (connect(s, (struct sockaddr *) &remote, len) == -1) {
  55. perror("connect");
  56. exit(1);
  57. }
  58. return s;
  59. }
  60. char *get_program_name(char *prog)
  61. {
  62. char *basename = rindex(prog, '/');
  63. if (basename)
  64. return basename + 1;
  65. else
  66. return prog;
  67. }
  68. int main(int argc, char **argv)
  69. {
  70. int trigger_fd;
  71. struct trigger_service_params params;
  72. struct exec_params exec_params;
  73. int ret, i;
  74. int start_local_process = 0;
  75. char *abs_exec_path;
  76. pid_t child_pid = 0;
  77. int inpipe[2], outpipe[2];
  78. if (argc < 3) {
  79. fprintf(stderr,
  80. "usage: %s target_vmname program_ident [local_program [local program arguments]]\n",
  81. argv[0]);
  82. exit(1);
  83. }
  84. if (argc > 3) {
  85. start_local_process = 1;
  86. }
  87. trigger_fd = connect_unix_socket(QREXEC_AGENT_TRIGGER_PATH);
  88. memset(&params, 0, sizeof(params));
  89. strncpy(params.service_name, argv[2], sizeof(params.service_name));
  90. strncpy(params.target_domain, argv[1],
  91. sizeof(params.target_domain));
  92. snprintf(params.request_id.ident,
  93. sizeof(params.request_id.ident), "SOCKET");
  94. if (write(trigger_fd, &params, sizeof(params)) < 0) {
  95. perror("write to agent");
  96. exit(1);
  97. }
  98. ret = read(trigger_fd, &exec_params, sizeof(exec_params));
  99. if (ret == 0) {
  100. fprintf(stderr, "Request refused\n");
  101. exit(1);
  102. }
  103. if (ret < 0 || ret != sizeof(exec_params)) {
  104. perror("read");
  105. exit(1);
  106. }
  107. if (start_local_process) {
  108. if (socketpair(AF_UNIX, SOCK_STREAM, 0, inpipe) ||
  109. socketpair(AF_UNIX, SOCK_STREAM, 0, outpipe)) {
  110. perror("socketpair");
  111. exit(1);
  112. }
  113. prepare_child_env();
  114. switch (child_pid = fork()) {
  115. case -1:
  116. perror("fork");
  117. exit(-1);
  118. case 0:
  119. close(inpipe[1]);
  120. close(outpipe[0]);
  121. close(trigger_fd);
  122. for (i = 0; i < 3; i++) {
  123. if (i != 2 || getenv("PASS_LOCAL_STDERR")) {
  124. char *env;
  125. if (asprintf(&env, "SAVED_FD_%d=%d", i, dup(i)) < 0) {
  126. perror("prepare SAVED_FD_");
  127. exit(1);
  128. }
  129. putenv(env);
  130. }
  131. }
  132. dup2(inpipe[0], 0);
  133. dup2(outpipe[1], 1);
  134. close(inpipe[0]);
  135. close(outpipe[1]);
  136. abs_exec_path = strdup(argv[3]);
  137. argv[3] = get_program_name(argv[3]);
  138. execv(abs_exec_path, argv + 3);
  139. perror("execv");
  140. exit(-1);
  141. }
  142. close(inpipe[0]);
  143. close(outpipe[1]);
  144. ret = handle_data_client(MSG_SERVICE_CONNECT,
  145. exec_params.connect_domain, exec_params.connect_port,
  146. inpipe[1], outpipe[0], -1);
  147. } else {
  148. ret = handle_data_client(MSG_SERVICE_CONNECT,
  149. exec_params.connect_domain, exec_params.connect_port,
  150. 1, 0, -1);
  151. }
  152. close(trigger_fd);
  153. if (start_local_process)
  154. waitpid(child_pid, &i, 0);
  155. return ret;
  156. }