qrexec-fork-server.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(RPC_REQUEST_COMMAND) + 1];
  37. /* replace magic RPC cmd with RPC multiplexer path */
  38. if (strncmp(cmd, RPC_REQUEST_COMMAND " ", strlen(RPC_REQUEST_COMMAND)+1)==0) {
  39. strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
  40. strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), cmd + strlen(RPC_REQUEST_COMMAND));
  41. cmd = buf;
  42. }
  43. signal(SIGCHLD, SIG_DFL);
  44. signal(SIGPIPE, SIG_DFL);
  45. execl("/bin/sh", "sh", "-c", cmd, NULL);
  46. perror("execl");
  47. exit(1);
  48. }
  49. void handle_vchan_error(const char *op)
  50. {
  51. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  52. exit(1);
  53. }
  54. void handle_single_command(int fd, struct qrexec_cmd_info *info) {
  55. char cmdline[info->cmdline_len+1];
  56. if (!read_all(fd, cmdline, info->cmdline_len))
  57. return;
  58. cmdline[info->cmdline_len] = 0;
  59. handle_new_process(info->type, info->connect_domain,
  60. info->connect_port,
  61. cmdline, info->cmdline_len);
  62. }
  63. int main(int argc, char **argv) {
  64. int s, fd;
  65. char *socket_path;
  66. struct qrexec_cmd_info info;
  67. struct sockaddr_un peer;
  68. unsigned int addrlen;
  69. if (argc == 2) {
  70. socket_path = argv[1];
  71. } else if (argc == 1) {
  72. /* this will be leaked, but we don't care as the process will then terminate */
  73. if (asprintf(&socket_path, QREXEC_FORK_SERVER_SOCKET, getenv("USER")) < 0) {
  74. fprintf(stderr, "Memory allocation failed\n");
  75. exit(1);
  76. }
  77. } else {
  78. fprintf(stderr, "Usage: %s [socket path]\n", argv[0]);
  79. exit(1);
  80. }
  81. s = get_server_socket(socket_path);
  82. if (fcntl(s, F_SETFD, O_CLOEXEC) < 0) {
  83. perror("fcntl");
  84. exit(1);
  85. }
  86. /* fork into background */
  87. switch (fork()) {
  88. case -1:
  89. perror("fork");
  90. exit(1);
  91. case 0:
  92. break;
  93. default:
  94. exit(0);
  95. }
  96. signal(SIGCHLD, SIG_IGN);
  97. register_exec_func(do_exec);
  98. while ((fd = accept(s, (struct sockaddr *) &peer, &addrlen)) >= 0) {
  99. if (read_all(fd, &info, sizeof(info))) {
  100. handle_single_command(fd, &info);
  101. }
  102. close(fd);
  103. addrlen = sizeof(peer);
  104. }
  105. close(s);
  106. unlink(socket_path);
  107. return 0;
  108. }