qrexec-client-vm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <getopt.h>
  31. #include "libqrexec-utils.h"
  32. #include "qrexec.h"
  33. #include "qrexec-agent.h"
  34. void handle_vchan_error(const char *op)
  35. {
  36. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  37. exit(1);
  38. }
  39. void do_exec(char *cmd __attribute__((__unused__))) {
  40. fprintf(stderr, "BUG: do_exec function shouldn't be called!\n");
  41. exit(1);
  42. }
  43. int connect_unix_socket(char *path)
  44. {
  45. int s, len;
  46. struct sockaddr_un remote;
  47. if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  48. perror("socket");
  49. return -1;
  50. }
  51. remote.sun_family = AF_UNIX;
  52. strncpy(remote.sun_path, path,
  53. sizeof(remote.sun_path) - 1);
  54. len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  55. if (connect(s, (struct sockaddr *) &remote, len) == -1) {
  56. perror("connect");
  57. exit(1);
  58. }
  59. return s;
  60. }
  61. char *get_program_name(char *prog)
  62. {
  63. char *basename = rindex(prog, '/');
  64. if (basename)
  65. return basename + 1;
  66. else
  67. return prog;
  68. }
  69. /* Target specification with keyword have changed from $... to @... . Convert
  70. * the argument appropriately, to avoid breaking user tools.
  71. */
  72. void convert_target_name_keyword(char *target)
  73. {
  74. size_t i;
  75. size_t len = strlen(target);
  76. for (i = 0; i < len; i++)
  77. if (target[i] == '$')
  78. target[i] = '@';
  79. }
  80. struct option longopts[] = {
  81. { "buffer-size", required_argument, 0, 'b' },
  82. { NULL, 0, 0, 0},
  83. };
  84. _Noreturn void usage(const char *argv0) {
  85. fprintf(stderr,
  86. "usage: %s [--buffer-size=BUFFER_SIZE] target_vmname program_ident [local_program [local program arguments]]\n",
  87. argv0);
  88. fprintf(stderr, "BUFFER_SIZE is minimum vchan buffer size (default: 64k)\n");
  89. exit(2);
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. int trigger_fd;
  94. struct trigger_service_params params;
  95. struct exec_params exec_params;
  96. int ret, i;
  97. int start_local_process = 0;
  98. char *abs_exec_path;
  99. pid_t child_pid = 0;
  100. int inpipe[2], outpipe[2];
  101. int buffer_size = 0;
  102. int opt;
  103. while (1) {
  104. opt = getopt_long(argc, argv, "+", longopts, NULL);
  105. if (opt == -1)
  106. break;
  107. switch (opt) {
  108. case 'b':
  109. buffer_size = atoi(optarg);
  110. break;
  111. case '?':
  112. usage(argv[0]);
  113. }
  114. }
  115. if (argc - optind < 2) {
  116. usage(argv[0]);
  117. }
  118. if (argc - optind > 2) {
  119. start_local_process = 1;
  120. }
  121. trigger_fd = connect_unix_socket(QREXEC_AGENT_TRIGGER_PATH);
  122. memset(&params, 0, sizeof(params));
  123. strncpy(params.service_name, argv[optind + 1], sizeof(params.service_name) - 1);
  124. convert_target_name_keyword(argv[optind]);
  125. strncpy(params.target_domain, argv[optind],
  126. sizeof(params.target_domain) - 1);
  127. snprintf(params.request_id.ident,
  128. sizeof(params.request_id.ident), "SOCKET");
  129. if (write(trigger_fd, &params, sizeof(params)) < 0) {
  130. perror("write to agent");
  131. exit(1);
  132. }
  133. ret = read(trigger_fd, &exec_params, sizeof(exec_params));
  134. if (ret == 0) {
  135. fprintf(stderr, "Request refused\n");
  136. exit(126);
  137. }
  138. if (ret < 0 || ret != sizeof(exec_params)) {
  139. perror("read");
  140. exit(1);
  141. }
  142. if (start_local_process) {
  143. if (socketpair(AF_UNIX, SOCK_STREAM, 0, inpipe) ||
  144. socketpair(AF_UNIX, SOCK_STREAM, 0, outpipe)) {
  145. perror("socketpair");
  146. exit(1);
  147. }
  148. prepare_child_env();
  149. switch (child_pid = fork()) {
  150. case -1:
  151. perror("fork");
  152. exit(-1);
  153. case 0:
  154. close(inpipe[1]);
  155. close(outpipe[0]);
  156. close(trigger_fd);
  157. for (i = 0; i < 3; i++) {
  158. if (i != 2 || getenv("PASS_LOCAL_STDERR")) {
  159. char *env;
  160. if (asprintf(&env, "SAVED_FD_%d=%d", i, dup(i)) < 0) {
  161. perror("prepare SAVED_FD_");
  162. exit(1);
  163. }
  164. putenv(env);
  165. }
  166. }
  167. dup2(inpipe[0], 0);
  168. dup2(outpipe[1], 1);
  169. close(inpipe[0]);
  170. close(outpipe[1]);
  171. abs_exec_path = strdup(argv[optind + 2]);
  172. argv[optind + 2] = get_program_name(argv[optind + 2]);
  173. execv(abs_exec_path, argv + optind + 2);
  174. perror("execv");
  175. exit(-1);
  176. }
  177. close(inpipe[0]);
  178. close(outpipe[1]);
  179. ret = handle_data_client(MSG_SERVICE_CONNECT,
  180. exec_params.connect_domain, exec_params.connect_port,
  181. inpipe[1], outpipe[0], -1, buffer_size);
  182. } else {
  183. ret = handle_data_client(MSG_SERVICE_CONNECT,
  184. exec_params.connect_domain, exec_params.connect_port,
  185. 1, 0, -1, buffer_size);
  186. }
  187. close(trigger_fd);
  188. if (start_local_process) {
  189. if (waitpid(child_pid, &i, 0) != -1) {
  190. if (WIFSIGNALED(i))
  191. ret = 128 + WTERMSIG(i);
  192. else
  193. ret = WEXITSTATUS(i);
  194. } else {
  195. perror("wait for local process");
  196. }
  197. }
  198. return ret;
  199. }