qrexec-agent.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include <sys/select.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <sys/wait.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include <pwd.h>
  31. #include <grp.h>
  32. #include <sys/stat.h>
  33. #include <assert.h>
  34. #include "qrexec.h"
  35. #include <libvchan.h>
  36. #include "libqrexec-utils.h"
  37. #include "qrexec-agent.h"
  38. struct _connection_info {
  39. int pid;
  40. int connect_domain;
  41. int connect_port;
  42. };
  43. int max_process_fd = -1;
  44. /* */
  45. struct _connection_info connection_info[MAX_FDS];
  46. libvchan_t *ctrl_vchan;
  47. int trigger_fd;
  48. int passfd_socket;
  49. int meminfo_write_started = 0;
  50. void do_exec(const char *cmd);
  51. void handle_vchan_error(const char *op)
  52. {
  53. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  54. exit(1);
  55. }
  56. int handle_handshake(libvchan_t *ctrl)
  57. {
  58. struct msg_header hdr;
  59. struct peer_info info;
  60. /* send own HELLO */
  61. hdr.type = MSG_HELLO;
  62. hdr.len = sizeof(info);
  63. info.version = QREXEC_PROTOCOL_VERSION;
  64. if (libvchan_send(ctrl, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  65. fprintf(stderr, "Failed to send HELLO hdr to agent\n");
  66. return -1;
  67. }
  68. if (libvchan_send(ctrl, &info, sizeof(info)) != sizeof(info)) {
  69. fprintf(stderr, "Failed to send HELLO hdr to agent\n");
  70. return -1;
  71. }
  72. /* receive MSG_HELLO from remote */
  73. if (libvchan_recv(ctrl, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  74. fprintf(stderr, "Failed to read agent HELLO hdr\n");
  75. return -1;
  76. }
  77. if (hdr.type != MSG_HELLO || hdr.len != sizeof(info)) {
  78. fprintf(stderr, "Invalid HELLO packet received: type %d, len %d\n", hdr.type, hdr.len);
  79. return -1;
  80. }
  81. if (libvchan_recv(ctrl, &info, sizeof(info)) != sizeof(info)) {
  82. fprintf(stderr, "Failed to read agent HELLO body\n");
  83. return -1;
  84. }
  85. if (info.version != QREXEC_PROTOCOL_VERSION) {
  86. fprintf(stderr, "Incompatible agent protocol version (remote %d, local %d)\n", info.version, QREXEC_PROTOCOL_VERSION);
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. void init()
  92. {
  93. /* FIXME: This 0 is remote domain ID */
  94. ctrl_vchan = libvchan_server_init(0, VCHAN_BASE_PORT, 4096, 4096);
  95. if (!ctrl_vchan)
  96. handle_vchan_error("server_init");
  97. if (handle_handshake(ctrl_vchan) < 0)
  98. exit(1);
  99. umask(0);
  100. mkfifo(QREXEC_AGENT_TRIGGER_PATH, 0666);
  101. passfd_socket = get_server_socket(QREXEC_AGENT_FDPASS_PATH);
  102. umask(077);
  103. trigger_fd =
  104. open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
  105. register_exec_func(do_exec);
  106. /* wait for qrexec daemon */
  107. while (!libvchan_is_open(ctrl_vchan))
  108. libvchan_wait(ctrl_vchan);
  109. }
  110. void wake_meminfo_writer()
  111. {
  112. FILE *f;
  113. int pid;
  114. if (meminfo_write_started)
  115. /* wake meminfo-writer only once */
  116. return;
  117. f = fopen(MEMINFO_WRITER_PIDFILE, "r");
  118. if (f == NULL) {
  119. /* no meminfo-writer found, ignoring */
  120. return;
  121. }
  122. if (fscanf(f, "%d", &pid) < 1) {
  123. fclose(f);
  124. /* no meminfo-writer found, ignoring */
  125. return;
  126. }
  127. fclose(f);
  128. if (pid <= 1 || pid > 0xffff) {
  129. /* check within acceptable range */
  130. return;
  131. }
  132. if (kill(pid, SIGUSR1) < 0) {
  133. /* Can't send signal */
  134. return;
  135. }
  136. meminfo_write_started = 1;
  137. }
  138. void register_vchan_connection(pid_t pid, int domain, int port)
  139. {
  140. int i;
  141. for (i = 0; i < MAX_FDS; i++) {
  142. if (connection_info[i].pid == 0) {
  143. connection_info[i].pid = pid;
  144. connection_info[i].connect_domain = domain;
  145. connection_info[i].connect_port = port;
  146. return;
  147. }
  148. }
  149. fprintf(stderr, "No free slot for child %d (connection to %d:%d)\n", pid, domain, port);
  150. }
  151. void handle_server_exec_request(struct msg_header *hdr)
  152. {
  153. struct exec_params params;
  154. char buf[hdr->len-sizeof(params)];
  155. pid_t child_agent;
  156. assert(hdr->len >= sizeof(params));
  157. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  158. handle_vchan_error("read exec params");
  159. if (libvchan_recv(ctrl_vchan, buf, hdr->len-sizeof(params)) < 0)
  160. handle_vchan_error("read exec cmd");
  161. child_agent = handle_new_process(hdr->type,
  162. params.connect_domain, params.connect_port,
  163. buf, hdr->len-sizeof(params));
  164. register_vchan_connection(child_agent,
  165. params.connect_domain, params.connect_port);
  166. }
  167. void handle_service_refused(struct msg_header *hdr)
  168. {
  169. struct service_params params;
  170. int stdin_fd, stdout_fd, stderr_fd;
  171. if (hdr->len != sizeof(params)) {
  172. fprintf(stderr, "Invalid msg 0x%x length (%d)\n", MSG_SERVICE_REFUSED, hdr->len);
  173. exit(1);
  174. }
  175. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  176. handle_vchan_error("read exec params");
  177. sscanf(params.ident, "%d %d %d", &stdin_fd, &stdout_fd, &stderr_fd);
  178. /* TODO: send some signal? some response? */
  179. close(stdin_fd);
  180. close(stdout_fd);
  181. close(stderr_fd);
  182. }
  183. void handle_server_cmd()
  184. {
  185. struct msg_header s_hdr;
  186. if (libvchan_recv(ctrl_vchan, &s_hdr, sizeof(s_hdr)) < 0)
  187. handle_vchan_error("read s_hdr");
  188. // fprintf(stderr, "got %x %x %x\n", s_hdr.type, s_hdr.client_id,
  189. // s_hdr.len);
  190. switch (s_hdr.type) {
  191. case MSG_EXEC_CMDLINE:
  192. case MSG_JUST_EXEC:
  193. case MSG_SERVICE_CONNECT:
  194. wake_meminfo_writer();
  195. handle_server_exec_request(&s_hdr);
  196. break;
  197. case MSG_SERVICE_REFUSED:
  198. handle_service_refused(&s_hdr);
  199. break;
  200. default:
  201. fprintf(stderr, "msg type from daemon is %d ?\n",
  202. s_hdr.type);
  203. exit(1);
  204. }
  205. }
  206. volatile int child_exited;
  207. void sigchld_handler(int x __attribute__((__unused__)))
  208. {
  209. child_exited = 1;
  210. signal(SIGCHLD, sigchld_handler);
  211. }
  212. int find_connection(int pid)
  213. {
  214. int i;
  215. for (i = 0; i < MAX_FDS; i++)
  216. if (connection_info[i].pid == pid)
  217. return i;
  218. return -1;
  219. }
  220. void reap_children()
  221. {
  222. int status;
  223. int pid;
  224. int id;
  225. struct msg_header hdr;
  226. struct exec_params params;
  227. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  228. id = find_connection(pid);
  229. if (id < 0)
  230. continue;
  231. hdr.type = MSG_CONNECTION_TERMINATED;
  232. hdr.len = sizeof(struct exec_params);
  233. params.connect_domain = connection_info[id].connect_domain;
  234. params.connect_port = connection_info[id].connect_port;
  235. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  236. handle_vchan_error("send");
  237. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  238. handle_vchan_error("send");
  239. connection_info[id].pid = 0;
  240. }
  241. child_exited = 0;
  242. }
  243. int fill_fds_for_select(fd_set * rdset, fd_set * wrset)
  244. {
  245. int max = -1;
  246. FD_ZERO(rdset);
  247. FD_ZERO(wrset);
  248. FD_SET(trigger_fd, rdset);
  249. if (trigger_fd > max)
  250. max = trigger_fd;
  251. FD_SET(passfd_socket, rdset);
  252. if (passfd_socket > max)
  253. max = passfd_socket;
  254. return max;
  255. }
  256. void handle_new_passfd()
  257. {
  258. int fd = do_accept(passfd_socket);
  259. if (fd >= MAX_FDS) {
  260. fprintf(stderr, "too many clients ?\n");
  261. exit(1);
  262. }
  263. // let client know what fd has been allocated
  264. if (write(fd, &fd, sizeof(fd)) != sizeof(fd)) {
  265. perror("write to client");
  266. }
  267. }
  268. void handle_trigger_io()
  269. {
  270. struct msg_header hdr;
  271. struct trigger_service_params params;
  272. int ret;
  273. hdr.len = sizeof(params);
  274. ret = read(trigger_fd, &params, sizeof(params));
  275. if (ret == sizeof(params)) {
  276. hdr.type = MSG_TRIGGER_SERVICE;
  277. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  278. handle_vchan_error("write hdr");
  279. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  280. handle_vchan_error("write params");
  281. }
  282. // trigger_fd is nonblock - so no need to reopen
  283. // not really, need to reopen at EOF
  284. if (ret <= 0) {
  285. close(trigger_fd);
  286. trigger_fd =
  287. open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
  288. }
  289. }
  290. int main()
  291. {
  292. fd_set rdset, wrset;
  293. int max;
  294. sigset_t chld_set;
  295. init();
  296. signal(SIGCHLD, sigchld_handler);
  297. signal(SIGPIPE, SIG_IGN);
  298. sigemptyset(&chld_set);
  299. sigaddset(&chld_set, SIGCHLD);
  300. for (;;) {
  301. sigprocmask(SIG_BLOCK, &chld_set, NULL);
  302. if (child_exited)
  303. reap_children();
  304. max = fill_fds_for_select(&rdset, &wrset);
  305. if (libvchan_buffer_space(ctrl_vchan) <=
  306. (int)sizeof(struct msg_header))
  307. FD_ZERO(&rdset);
  308. wait_for_vchan_or_argfd(ctrl_vchan, max, &rdset, &wrset);
  309. sigprocmask(SIG_UNBLOCK, &chld_set, NULL);
  310. if (FD_ISSET(passfd_socket, &rdset))
  311. handle_new_passfd();
  312. while (libvchan_data_ready(ctrl_vchan))
  313. handle_server_cmd();
  314. if (FD_ISSET(trigger_fd, &rdset))
  315. handle_trigger_io();
  316. }
  317. }