qrexec-client-vm.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/un.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include "qrexec.h"
  30. int connect_unix_socket()
  31. {
  32. int s, len;
  33. struct sockaddr_un remote;
  34. if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  35. perror("socket");
  36. return -1;
  37. }
  38. remote.sun_family = AF_UNIX;
  39. strncpy(remote.sun_path, QREXEC_AGENT_FDPASS_PATH,
  40. sizeof(remote.sun_path));
  41. len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  42. if (connect(s, (struct sockaddr *) &remote, len) == -1) {
  43. perror("connect");
  44. exit(1);
  45. }
  46. return s;
  47. }
  48. char *get_program_name(char *prog)
  49. {
  50. char *basename = rindex(prog, '/');
  51. if (basename)
  52. return basename + 1;
  53. else
  54. return prog;
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. int trigger_fd;
  59. struct trigger_connect_params params;
  60. int local_fd[3], remote_fd[3];
  61. int i;
  62. char *abs_exec_path;
  63. if (argc < 4) {
  64. fprintf(stderr,
  65. "usage: %s target_vmname program_ident local_program [local program arguments]\n",
  66. argv[0]);
  67. exit(1);
  68. }
  69. trigger_fd = open(QREXEC_AGENT_TRIGGER_PATH, O_WRONLY);
  70. if (trigger_fd < 0) {
  71. perror("open " QREXEC_AGENT_TRIGGER_PATH);
  72. exit(1);
  73. }
  74. for (i = 0; i < 3; i++) {
  75. local_fd[i] = connect_unix_socket();
  76. if (read(local_fd[i], &remote_fd[i], sizeof(remote_fd[i])) != sizeof(remote_fd[i])) {
  77. perror("read client fd");
  78. exit(1);
  79. }
  80. if (i != 2 || getenv("PASS_LOCAL_STDERR")) {
  81. char *env;
  82. if (asprintf(&env, "SAVED_FD_%d=%d", i, dup(i)) < 0) {
  83. perror("prepare SAVED_FD_");
  84. exit(1);
  85. }
  86. putenv(env);
  87. dup2(local_fd[i], i);
  88. close(local_fd[i]);
  89. }
  90. }
  91. memset(&params, 0, sizeof(params));
  92. strncpy(params.exec_index, argv[2], sizeof(params.exec_index));
  93. strncpy(params.target_vmname, argv[1],
  94. sizeof(params.target_vmname));
  95. snprintf(params.process_fds.ident,
  96. sizeof(params.process_fds.ident), "%d %d %d",
  97. remote_fd[0], remote_fd[1], remote_fd[2]);
  98. if (write(trigger_fd, &params, sizeof(params)) < 0) {
  99. if (!getenv("PASS_LOCAL_STDERR"))
  100. perror("write to agent");
  101. exit(1);
  102. }
  103. close(trigger_fd);
  104. abs_exec_path = strdup(argv[3]);
  105. argv[3] = get_program_name(argv[3]);
  106. execv(abs_exec_path, argv + 3);
  107. perror("execv");
  108. return 1;
  109. }