qrexec-fork-server.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2015 Marek Marczykowski-Górecki <marmarek@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 <stdio.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <sys/socket.h>
  29. #include <sys/un.h>
  30. #include "qrexec.h"
  31. #include <libvchan.h>
  32. #include "libqrexec-utils.h"
  33. #include "qrexec-agent.h"
  34. void do_exec(const char *cmd)
  35. {
  36. char *shell;
  37. char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(RPC_REQUEST_COMMAND) + 1];
  38. /* replace magic RPC cmd with RPC multiplexer path */
  39. if (strncmp(cmd, RPC_REQUEST_COMMAND " ", strlen(RPC_REQUEST_COMMAND)+1)==0) {
  40. strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
  41. strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), cmd + strlen(RPC_REQUEST_COMMAND));
  42. cmd = buf;
  43. }
  44. signal(SIGCHLD, SIG_DFL);
  45. signal(SIGPIPE, SIG_DFL);
  46. shell = getenv("SHELL");
  47. if (!shell)
  48. shell = "/bin/sh";
  49. execl(shell, basename(shell), "-c", cmd, NULL);
  50. perror("execl");
  51. exit(1);
  52. }
  53. void handle_vchan_error(const char *op)
  54. {
  55. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  56. exit(1);
  57. }
  58. void handle_single_command(int fd, struct qrexec_cmd_info *info) {
  59. char cmdline[info->cmdline_len+1];
  60. if (!read_all(fd, cmdline, info->cmdline_len))
  61. return;
  62. cmdline[info->cmdline_len] = 0;
  63. handle_new_process(info->type, info->connect_domain,
  64. info->connect_port,
  65. cmdline, info->cmdline_len);
  66. }
  67. int main(int argc, char **argv) {
  68. int s, fd;
  69. char *socket_path;
  70. struct qrexec_cmd_info info;
  71. struct sockaddr_un peer;
  72. unsigned int addrlen;
  73. if (argc == 2) {
  74. socket_path = argv[1];
  75. } else if (argc == 1) {
  76. /* this will be leaked, but we don't care as the process will then terminate */
  77. if (asprintf(&socket_path, QREXEC_FORK_SERVER_SOCKET, getenv("USER")) < 0) {
  78. fprintf(stderr, "Memory allocation failed\n");
  79. exit(1);
  80. }
  81. } else {
  82. fprintf(stderr, "Usage: %s [socket path]\n", argv[0]);
  83. exit(1);
  84. }
  85. s = get_server_socket(socket_path);
  86. if (fcntl(s, F_SETFD, O_CLOEXEC) < 0) {
  87. perror("fcntl");
  88. exit(1);
  89. }
  90. /* fork into background */
  91. switch (fork()) {
  92. case -1:
  93. perror("fork");
  94. exit(1);
  95. case 0:
  96. break;
  97. default:
  98. exit(0);
  99. }
  100. signal(SIGCHLD, SIG_IGN);
  101. register_exec_func(do_exec);
  102. while ((fd = accept(s, (struct sockaddr *) &peer, &addrlen)) >= 0) {
  103. if (read_all(fd, &info, sizeof(info))) {
  104. handle_single_command(fd, &info);
  105. }
  106. close(fd);
  107. addrlen = sizeof(peer);
  108. }
  109. close(s);
  110. unlink(socket_path);
  111. return 0;
  112. }