qrexec-fork-server.c 3.3 KB

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