qrexec-agent.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/select.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <signal.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <sys/wait.h>
  31. #include <fcntl.h>
  32. #include <string.h>
  33. #include <pwd.h>
  34. #include <grp.h>
  35. #include <sys/stat.h>
  36. #include <assert.h>
  37. #include "qrexec.h"
  38. #include <libvchan.h>
  39. #include "libqrexec-utils.h"
  40. #include "qrexec-agent.h"
  41. struct _connection_info {
  42. int pid; /* pid of child process handling the data */
  43. int fd; /* socket to process handling the data (wait for EOF here) */
  44. int connect_domain;
  45. int connect_port;
  46. };
  47. int max_process_fd = -1;
  48. /* */
  49. struct _connection_info connection_info[MAX_FDS];
  50. libvchan_t *ctrl_vchan;
  51. int trigger_fd;
  52. int meminfo_write_started = 0;
  53. void no_colon_in_cmd()
  54. {
  55. fprintf(stderr,
  56. "cmdline is supposed to be in user:command form\n");
  57. exit(1);
  58. }
  59. void do_exec(const char *cmd)
  60. {
  61. char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(RPC_REQUEST_COMMAND) + 1];
  62. char *realcmd = index(cmd, ':');
  63. if (!realcmd)
  64. no_colon_in_cmd();
  65. /* mark end of username and move to command */
  66. *realcmd = 0;
  67. realcmd++;
  68. /* ignore "nogui:" prefix in linux agent */
  69. if (strncmp(realcmd, "nogui:", 6) == 0)
  70. realcmd+=6;
  71. /* replace magic RPC cmd with RPC multiplexer path */
  72. if (strncmp(realcmd, RPC_REQUEST_COMMAND " ", strlen(RPC_REQUEST_COMMAND)+1)==0) {
  73. strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
  74. strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), realcmd + strlen(RPC_REQUEST_COMMAND));
  75. realcmd = buf;
  76. }
  77. signal(SIGCHLD, SIG_DFL);
  78. signal(SIGPIPE, SIG_DFL);
  79. execl("/bin/su", "su", "-", cmd, "-c", realcmd, NULL);
  80. perror("execl");
  81. exit(1);
  82. }
  83. void handle_vchan_error(const char *op)
  84. {
  85. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  86. exit(1);
  87. }
  88. void init()
  89. {
  90. mode_t old_umask;
  91. /* FIXME: This 0 is remote domain ID */
  92. ctrl_vchan = libvchan_server_init(0, VCHAN_BASE_PORT, 4096, 4096);
  93. if (!ctrl_vchan)
  94. handle_vchan_error("server_init");
  95. if (handle_handshake(ctrl_vchan) < 0)
  96. exit(1);
  97. old_umask = umask(0);
  98. trigger_fd = get_server_socket(QREXEC_AGENT_TRIGGER_PATH);
  99. umask(old_umask);
  100. register_exec_func(do_exec);
  101. /* wait for qrexec daemon */
  102. while (!libvchan_is_open(ctrl_vchan))
  103. libvchan_wait(ctrl_vchan);
  104. }
  105. void wake_meminfo_writer()
  106. {
  107. FILE *f;
  108. int pid;
  109. if (meminfo_write_started)
  110. /* wake meminfo-writer only once */
  111. return;
  112. f = fopen(MEMINFO_WRITER_PIDFILE, "r");
  113. if (f == NULL) {
  114. /* no meminfo-writer found, ignoring */
  115. return;
  116. }
  117. if (fscanf(f, "%d", &pid) < 1) {
  118. fclose(f);
  119. /* no meminfo-writer found, ignoring */
  120. return;
  121. }
  122. fclose(f);
  123. if (pid <= 1 || pid > 0xffff) {
  124. /* check within acceptable range */
  125. return;
  126. }
  127. if (kill(pid, SIGUSR1) < 0) {
  128. /* Can't send signal */
  129. return;
  130. }
  131. meminfo_write_started = 1;
  132. }
  133. int try_fork_server(int type, int connect_domain, int connect_port,
  134. char *cmdline, int cmdline_len) {
  135. char username[cmdline_len];
  136. char *colon;
  137. char *fork_server_socket_path;
  138. int s, len;
  139. struct sockaddr_un remote;
  140. struct qrexec_cmd_info info;
  141. strncpy(username, cmdline, cmdline_len);
  142. colon = index(username, ':');
  143. if (!colon)
  144. return -1;
  145. *colon = '\0';
  146. if (asprintf(&fork_server_socket_path, QREXEC_FORK_SERVER_SOCKET, username) < 0) {
  147. fprintf(stderr, "Memory allocation failed\n");
  148. return -1;
  149. }
  150. remote.sun_family = AF_UNIX;
  151. strncpy(remote.sun_path, fork_server_socket_path,
  152. sizeof(remote.sun_path));
  153. free(fork_server_socket_path);
  154. if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  155. perror("socket");
  156. return -1;
  157. }
  158. len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  159. if (connect(s, (struct sockaddr *) &remote, len) == -1) {
  160. if (errno != ECONNREFUSED && errno != ENOENT)
  161. perror("connect");
  162. close(s);
  163. return -1;
  164. }
  165. info.type = type;
  166. info.connect_domain = connect_domain;
  167. info.connect_port = connect_port;
  168. info.cmdline_len = cmdline_len-(strlen(username)+1);
  169. if (!write_all(s, &info, sizeof(info))) {
  170. perror("write");
  171. close(s);
  172. return -1;
  173. }
  174. if (!write_all(s, colon+1, info.cmdline_len)) {
  175. perror("write");
  176. close(s);
  177. return -1;
  178. }
  179. return s;
  180. }
  181. void register_vchan_connection(pid_t pid, int fd, int domain, int port)
  182. {
  183. int i;
  184. for (i = 0; i < MAX_FDS; i++) {
  185. if (connection_info[i].pid == 0) {
  186. connection_info[i].pid = pid;
  187. connection_info[i].fd = fd;
  188. connection_info[i].connect_domain = domain;
  189. connection_info[i].connect_port = port;
  190. return;
  191. }
  192. }
  193. fprintf(stderr, "No free slot for child %d (connection to %d:%d)\n", pid, domain, port);
  194. }
  195. void handle_server_exec_request(struct msg_header *hdr)
  196. {
  197. struct exec_params params;
  198. int buf_len = hdr->len-sizeof(params);
  199. char buf[buf_len];
  200. pid_t child_agent;
  201. int client_fd;
  202. assert(hdr->len >= sizeof(params));
  203. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  204. handle_vchan_error("read exec params");
  205. if (libvchan_recv(ctrl_vchan, buf, buf_len) < 0)
  206. handle_vchan_error("read exec cmd");
  207. if ((hdr->type == MSG_EXEC_CMDLINE || hdr->type == MSG_JUST_EXEC) &&
  208. !strstr(buf, ":nogui:")) {
  209. int child_socket = try_fork_server(hdr->type,
  210. params.connect_domain, params.connect_port,
  211. buf, buf_len);
  212. if (child_socket >= 0) {
  213. register_vchan_connection(-1, child_socket,
  214. params.connect_domain, params.connect_port);
  215. return;
  216. }
  217. }
  218. if (hdr->type == MSG_SERVICE_CONNECT && sscanf(buf, "SOCKET%d", &client_fd)) {
  219. /* FIXME: Maybe add some check if client_fd is really FD to some
  220. * qrexec-client-vm process; but this data comes from qrexec-daemon
  221. * (which sends back what it got from us earlier), so it isn't critical.
  222. */
  223. if (write(client_fd, &params, sizeof(params)) < 0) {
  224. /* ignore */
  225. }
  226. /* No need to send request_id (buf) - the client don't need it, there
  227. * is only meaningless (for the client) socket FD */
  228. /* Register connection even if there was an error sending params to
  229. * qrexec-client-vm. This way the mainloop will clean the things up
  230. * (close socket, send MSG_CONNECTION_TERMINATED) when qrexec-client-vm
  231. * will close the socket (terminate itself). */
  232. register_vchan_connection(-1, client_fd,
  233. params.connect_domain, params.connect_port);
  234. return;
  235. }
  236. /* No fork server case */
  237. child_agent = handle_new_process(hdr->type,
  238. params.connect_domain, params.connect_port,
  239. buf, buf_len);
  240. register_vchan_connection(child_agent, -1,
  241. params.connect_domain, params.connect_port);
  242. }
  243. void handle_service_refused(struct msg_header *hdr)
  244. {
  245. struct service_params params;
  246. int socket_fd;
  247. if (hdr->len != sizeof(params)) {
  248. fprintf(stderr, "Invalid msg 0x%x length (%d)\n", MSG_SERVICE_REFUSED, hdr->len);
  249. exit(1);
  250. }
  251. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  252. handle_vchan_error("read exec params");
  253. if (sscanf(params.ident, "SOCKET%d", &socket_fd))
  254. close(socket_fd);
  255. else
  256. fprintf(stderr, "Received REFUSED for unknown service request '%s'\n", params.ident);
  257. }
  258. void handle_server_cmd()
  259. {
  260. struct msg_header s_hdr;
  261. if (libvchan_recv(ctrl_vchan, &s_hdr, sizeof(s_hdr)) < 0)
  262. handle_vchan_error("read s_hdr");
  263. // fprintf(stderr, "got %x %x %x\n", s_hdr.type, s_hdr.client_id,
  264. // s_hdr.len);
  265. switch (s_hdr.type) {
  266. case MSG_EXEC_CMDLINE:
  267. case MSG_JUST_EXEC:
  268. case MSG_SERVICE_CONNECT:
  269. wake_meminfo_writer();
  270. handle_server_exec_request(&s_hdr);
  271. break;
  272. case MSG_SERVICE_REFUSED:
  273. handle_service_refused(&s_hdr);
  274. break;
  275. default:
  276. fprintf(stderr, "msg type from daemon is %d ?\n",
  277. s_hdr.type);
  278. exit(1);
  279. }
  280. }
  281. volatile int child_exited;
  282. void sigchld_handler(int x __attribute__((__unused__)))
  283. {
  284. child_exited = 1;
  285. signal(SIGCHLD, sigchld_handler);
  286. }
  287. int find_connection(int pid)
  288. {
  289. int i;
  290. for (i = 0; i < MAX_FDS; i++)
  291. if (connection_info[i].pid == pid)
  292. return i;
  293. return -1;
  294. }
  295. void release_connection(int id) {
  296. struct msg_header hdr;
  297. struct exec_params params;
  298. hdr.type = MSG_CONNECTION_TERMINATED;
  299. hdr.len = sizeof(struct exec_params);
  300. params.connect_domain = connection_info[id].connect_domain;
  301. params.connect_port = connection_info[id].connect_port;
  302. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  303. handle_vchan_error("send");
  304. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  305. handle_vchan_error("send");
  306. connection_info[id].pid = 0;
  307. }
  308. void reap_children()
  309. {
  310. int status;
  311. int pid;
  312. int id;
  313. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  314. id = find_connection(pid);
  315. if (id < 0)
  316. continue;
  317. release_connection(id);
  318. }
  319. child_exited = 0;
  320. }
  321. int fill_fds_for_select(fd_set * rdset, fd_set * wrset)
  322. {
  323. int max = -1;
  324. int i;
  325. FD_ZERO(rdset);
  326. FD_ZERO(wrset);
  327. FD_SET(trigger_fd, rdset);
  328. if (trigger_fd > max)
  329. max = trigger_fd;
  330. for (i = 0; i < MAX_FDS; i++) {
  331. if (connection_info[i].pid != 0 && connection_info[i].fd != -1) {
  332. FD_SET(connection_info[i].fd, rdset);
  333. if (connection_info[i].fd > max)
  334. max = connection_info[i].fd;
  335. }
  336. }
  337. return max;
  338. }
  339. void handle_trigger_io()
  340. {
  341. struct msg_header hdr;
  342. struct trigger_service_params params;
  343. int ret;
  344. int client_fd;
  345. client_fd = do_accept(trigger_fd);
  346. if (client_fd < 0)
  347. return;
  348. hdr.len = sizeof(params);
  349. ret = read(client_fd, &params, sizeof(params));
  350. if (ret == sizeof(params)) {
  351. hdr.type = MSG_TRIGGER_SERVICE;
  352. snprintf(params.request_id.ident, sizeof(params.request_id), "SOCKET%d", client_fd);
  353. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  354. handle_vchan_error("write hdr");
  355. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  356. handle_vchan_error("write params");
  357. }
  358. if (ret <= 0) {
  359. close(client_fd);
  360. }
  361. /* do not close client_fd - we'll need it to send the connection details
  362. * later (when dom0 accepts the request) */
  363. }
  364. void handle_terminated_fork_client(fd_set *rdset) {
  365. int i, ret;
  366. char buf[2];
  367. for (i = 0; i < MAX_FDS; i++) {
  368. if (connection_info[i].pid && connection_info[i].fd >= 0 &&
  369. FD_ISSET(connection_info[i].fd, rdset)) {
  370. ret = read(connection_info[i].fd, buf, sizeof(buf));
  371. if (ret == 0 || (ret == -1 && errno == ECONNRESET)) {
  372. close(connection_info[i].fd);
  373. release_connection(i);
  374. } else {
  375. fprintf(stderr, "Unexpected read on fork-server connection: %d(%s)\n", ret, strerror(errno));
  376. close(connection_info[i].fd);
  377. release_connection(i);
  378. }
  379. }
  380. }
  381. }
  382. int main()
  383. {
  384. fd_set rdset, wrset;
  385. int max;
  386. sigset_t chld_set;
  387. init();
  388. signal(SIGCHLD, sigchld_handler);
  389. signal(SIGPIPE, SIG_IGN);
  390. sigemptyset(&chld_set);
  391. sigaddset(&chld_set, SIGCHLD);
  392. for (;;) {
  393. sigprocmask(SIG_BLOCK, &chld_set, NULL);
  394. if (child_exited)
  395. reap_children();
  396. max = fill_fds_for_select(&rdset, &wrset);
  397. if (libvchan_buffer_space(ctrl_vchan) <=
  398. (int)sizeof(struct msg_header))
  399. FD_ZERO(&rdset);
  400. wait_for_vchan_or_argfd(ctrl_vchan, max, &rdset, &wrset);
  401. sigprocmask(SIG_UNBLOCK, &chld_set, NULL);
  402. while (libvchan_data_ready(ctrl_vchan))
  403. handle_server_cmd();
  404. if (FD_ISSET(trigger_fd, &rdset))
  405. handle_trigger_io();
  406. handle_terminated_fork_client(&rdset);
  407. }
  408. }