qrexec-client-vm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(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. /* Target specification with keyword have changed from $... to @... . Convert
  69. * the argument appropriately, to avoid breaking user tools.
  70. */
  71. void convert_target_name_keyword(char *target)
  72. {
  73. size_t i;
  74. size_t len = strlen(target);
  75. for (i = 0; i < len; i++)
  76. if (target[i] == '$')
  77. target[i] = '@';
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. int trigger_fd;
  82. struct trigger_service_params params;
  83. struct exec_params exec_params;
  84. int ret, i;
  85. int start_local_process = 0;
  86. char *abs_exec_path;
  87. pid_t child_pid = 0;
  88. int inpipe[2], outpipe[2];
  89. if (argc < 3) {
  90. fprintf(stderr,
  91. "usage: %s target_vmname program_ident [local_program [local program arguments]]\n",
  92. argv[0]);
  93. exit(1);
  94. }
  95. if (argc > 3) {
  96. start_local_process = 1;
  97. }
  98. trigger_fd = connect_unix_socket(QREXEC_AGENT_TRIGGER_PATH);
  99. memset(&params, 0, sizeof(params));
  100. strncpy(params.service_name, argv[2], sizeof(params.service_name));
  101. convert_target_name_keyword(argv[1]);
  102. strncpy(params.target_domain, argv[1],
  103. sizeof(params.target_domain));
  104. snprintf(params.request_id.ident,
  105. sizeof(params.request_id.ident), "SOCKET");
  106. if (write(trigger_fd, &params, sizeof(params)) < 0) {
  107. perror("write to agent");
  108. exit(1);
  109. }
  110. ret = read(trigger_fd, &exec_params, sizeof(exec_params));
  111. if (ret == 0) {
  112. fprintf(stderr, "Request refused\n");
  113. exit(126);
  114. }
  115. if (ret < 0 || ret != sizeof(exec_params)) {
  116. perror("read");
  117. exit(1);
  118. }
  119. if (start_local_process) {
  120. if (socketpair(AF_UNIX, SOCK_STREAM, 0, inpipe) ||
  121. socketpair(AF_UNIX, SOCK_STREAM, 0, outpipe)) {
  122. perror("socketpair");
  123. exit(1);
  124. }
  125. prepare_child_env();
  126. switch (child_pid = fork()) {
  127. case -1:
  128. perror("fork");
  129. exit(-1);
  130. case 0:
  131. close(inpipe[1]);
  132. close(outpipe[0]);
  133. close(trigger_fd);
  134. for (i = 0; i < 3; i++) {
  135. if (i != 2 || getenv("PASS_LOCAL_STDERR")) {
  136. char *env;
  137. if (asprintf(&env, "SAVED_FD_%d=%d", i, dup(i)) < 0) {
  138. perror("prepare SAVED_FD_");
  139. exit(1);
  140. }
  141. putenv(env);
  142. }
  143. }
  144. dup2(inpipe[0], 0);
  145. dup2(outpipe[1], 1);
  146. close(inpipe[0]);
  147. close(outpipe[1]);
  148. abs_exec_path = strdup(argv[3]);
  149. argv[3] = get_program_name(argv[3]);
  150. execv(abs_exec_path, argv + 3);
  151. perror("execv");
  152. exit(-1);
  153. }
  154. close(inpipe[0]);
  155. close(outpipe[1]);
  156. ret = handle_data_client(MSG_SERVICE_CONNECT,
  157. exec_params.connect_domain, exec_params.connect_port,
  158. inpipe[1], outpipe[0], -1);
  159. } else {
  160. ret = handle_data_client(MSG_SERVICE_CONNECT,
  161. exec_params.connect_domain, exec_params.connect_port,
  162. 1, 0, -1);
  163. }
  164. close(trigger_fd);
  165. if (start_local_process) {
  166. if (waitpid(child_pid, &i, 0) != -1) {
  167. if (WIFSIGNALED(i))
  168. ret = 128 + WTERMSIG(i);
  169. else
  170. ret = WEXITSTATUS(i);
  171. } else {
  172. perror("wait for local process");
  173. }
  174. }
  175. return ret;
  176. }