qrexec-client-vm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. char pid_s[10];
  79. if (argc < 3) {
  80. fprintf(stderr,
  81. "usage: %s target_vmname program_ident [local_program [local program arguments]]\n",
  82. argv[0]);
  83. exit(1);
  84. }
  85. if (argc > 3) {
  86. start_local_process = 1;
  87. }
  88. trigger_fd = connect_unix_socket(QREXEC_AGENT_TRIGGER_PATH);
  89. memset(&params, 0, sizeof(params));
  90. strncpy(params.service_name, argv[2], sizeof(params.service_name));
  91. strncpy(params.target_domain, argv[1],
  92. sizeof(params.target_domain));
  93. snprintf(params.request_id.ident,
  94. sizeof(params.request_id.ident), "SOCKET");
  95. if (write(trigger_fd, &params, sizeof(params)) < 0) {
  96. perror("write to agent");
  97. exit(1);
  98. }
  99. ret = read(trigger_fd, &exec_params, sizeof(exec_params));
  100. if (ret == 0) {
  101. fprintf(stderr, "Request refused\n");
  102. exit(1);
  103. }
  104. if (ret < 0 || ret != sizeof(exec_params)) {
  105. perror("read");
  106. exit(1);
  107. }
  108. if (start_local_process) {
  109. if (socketpair(AF_UNIX, SOCK_STREAM, 0, inpipe) ||
  110. socketpair(AF_UNIX, SOCK_STREAM, 0, outpipe)) {
  111. perror("socketpair");
  112. exit(1);
  113. }
  114. snprintf(pid_s, sizeof(pid_s), "%d", getpid());
  115. setenv("QREXEC_AGENT_PID", pid_s, 1);
  116. switch (child_pid = fork()) {
  117. case -1:
  118. perror("fork");
  119. exit(-1);
  120. case 0:
  121. close(inpipe[1]);
  122. close(outpipe[0]);
  123. close(trigger_fd);
  124. for (i = 0; i < 3; i++) {
  125. if (i != 2 || getenv("PASS_LOCAL_STDERR")) {
  126. char *env;
  127. if (asprintf(&env, "SAVED_FD_%d=%d", i, dup(i)) < 0) {
  128. perror("prepare SAVED_FD_");
  129. exit(1);
  130. }
  131. putenv(env);
  132. }
  133. }
  134. dup2(inpipe[0], 0);
  135. dup2(outpipe[1], 1);
  136. close(inpipe[0]);
  137. close(outpipe[1]);
  138. abs_exec_path = strdup(argv[3]);
  139. argv[3] = get_program_name(argv[3]);
  140. execv(abs_exec_path, argv + 3);
  141. perror("execv");
  142. exit(-1);
  143. }
  144. close(inpipe[0]);
  145. close(outpipe[1]);
  146. ret = handle_data_client(MSG_SERVICE_CONNECT,
  147. exec_params.connect_domain, exec_params.connect_port,
  148. inpipe[1], outpipe[0], -1);
  149. } else {
  150. ret = handle_data_client(MSG_SERVICE_CONNECT,
  151. exec_params.connect_domain, exec_params.connect_port,
  152. 1, 0, -1);
  153. }
  154. close(trigger_fd);
  155. if (start_local_process)
  156. waitpid(child_pid, &i, 0);
  157. return ret;
  158. }