qrexec-agent.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 passfd_socket;
  53. int meminfo_write_started = 0;
  54. void no_colon_in_cmd()
  55. {
  56. fprintf(stderr,
  57. "cmdline is supposed to be in user:command form\n");
  58. exit(1);
  59. }
  60. void do_exec(const char *cmd)
  61. {
  62. char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(RPC_REQUEST_COMMAND) + 1];
  63. char *realcmd = index(cmd, ':');
  64. if (!realcmd)
  65. no_colon_in_cmd();
  66. /* mark end of username and move to command */
  67. *realcmd = 0;
  68. realcmd++;
  69. /* ignore "nogui:" prefix in linux agent */
  70. if (strncmp(realcmd, "nogui:", 6) == 0)
  71. realcmd+=6;
  72. /* replace magic RPC cmd with RPC multiplexer path */
  73. if (strncmp(realcmd, RPC_REQUEST_COMMAND " ", strlen(RPC_REQUEST_COMMAND)+1)==0) {
  74. strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
  75. strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), realcmd + strlen(RPC_REQUEST_COMMAND));
  76. realcmd = buf;
  77. }
  78. signal(SIGCHLD, SIG_DFL);
  79. signal(SIGPIPE, SIG_DFL);
  80. execl("/bin/su", "su", "-", cmd, "-c", realcmd, NULL);
  81. perror("execl");
  82. exit(1);
  83. }
  84. void handle_vchan_error(const char *op)
  85. {
  86. fprintf(stderr, "Error while vchan %s, exiting\n", op);
  87. exit(1);
  88. }
  89. void init()
  90. {
  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. umask(0);
  98. mkfifo(QREXEC_AGENT_TRIGGER_PATH, 0666);
  99. passfd_socket = get_server_socket(QREXEC_AGENT_FDPASS_PATH);
  100. umask(077);
  101. trigger_fd =
  102. open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
  103. register_exec_func(do_exec);
  104. /* wait for qrexec daemon */
  105. while (!libvchan_is_open(ctrl_vchan))
  106. libvchan_wait(ctrl_vchan);
  107. }
  108. void wake_meminfo_writer()
  109. {
  110. FILE *f;
  111. int pid;
  112. if (meminfo_write_started)
  113. /* wake meminfo-writer only once */
  114. return;
  115. f = fopen(MEMINFO_WRITER_PIDFILE, "r");
  116. if (f == NULL) {
  117. /* no meminfo-writer found, ignoring */
  118. return;
  119. }
  120. if (fscanf(f, "%d", &pid) < 1) {
  121. fclose(f);
  122. /* no meminfo-writer found, ignoring */
  123. return;
  124. }
  125. fclose(f);
  126. if (pid <= 1 || pid > 0xffff) {
  127. /* check within acceptable range */
  128. return;
  129. }
  130. if (kill(pid, SIGUSR1) < 0) {
  131. /* Can't send signal */
  132. return;
  133. }
  134. meminfo_write_started = 1;
  135. }
  136. int try_fork_server(int type, int connect_domain, int connect_port,
  137. char *cmdline, int cmdline_len) {
  138. char username[cmdline_len];
  139. char *colon;
  140. char *fork_server_socket_path;
  141. int s, len;
  142. struct sockaddr_un remote;
  143. struct qrexec_cmd_info info;
  144. strncpy(username, cmdline, cmdline_len);
  145. colon = index(username, ':');
  146. if (!colon)
  147. return -1;
  148. *colon = '\0';
  149. if (asprintf(&fork_server_socket_path, QREXEC_FORK_SERVER_SOCKET, username) < 0) {
  150. fprintf(stderr, "Memory allocation failed\n");
  151. return -1;
  152. }
  153. remote.sun_family = AF_UNIX;
  154. strncpy(remote.sun_path, fork_server_socket_path,
  155. sizeof(remote.sun_path));
  156. free(fork_server_socket_path);
  157. if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  158. perror("socket");
  159. return -1;
  160. }
  161. len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  162. if (connect(s, (struct sockaddr *) &remote, len) == -1) {
  163. if (errno != ECONNREFUSED)
  164. perror("connect");
  165. close(s);
  166. return -1;
  167. }
  168. info.type = type;
  169. info.connect_domain = connect_domain;
  170. info.connect_port = connect_port;
  171. info.cmdline_len = cmdline_len-(strlen(username)+1);
  172. if (!write_all(s, &info, sizeof(info))) {
  173. perror("write");
  174. close(s);
  175. return -1;
  176. }
  177. if (!write_all(s, colon+1, info.cmdline_len)) {
  178. perror("write");
  179. close(s);
  180. return -1;
  181. }
  182. return s;
  183. }
  184. void register_vchan_connection(pid_t pid, int fd, int domain, int port)
  185. {
  186. int i;
  187. for (i = 0; i < MAX_FDS; i++) {
  188. if (connection_info[i].pid == 0) {
  189. connection_info[i].pid = pid;
  190. connection_info[i].fd = fd;
  191. connection_info[i].connect_domain = domain;
  192. connection_info[i].connect_port = port;
  193. return;
  194. }
  195. }
  196. fprintf(stderr, "No free slot for child %d (connection to %d:%d)\n", pid, domain, port);
  197. }
  198. void handle_server_exec_request(struct msg_header *hdr)
  199. {
  200. struct exec_params params;
  201. char buf[hdr->len-sizeof(params)];
  202. pid_t child_agent;
  203. assert(hdr->len >= sizeof(params));
  204. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  205. handle_vchan_error("read exec params");
  206. if (libvchan_recv(ctrl_vchan, buf, hdr->len-sizeof(params)) < 0)
  207. handle_vchan_error("read exec cmd");
  208. if ((hdr->type == MSG_EXEC_CMDLINE || hdr->type == MSG_JUST_EXEC) &&
  209. !strstr(buf, ":nogui:")) {
  210. int child_socket = try_fork_server(hdr->type,
  211. params.connect_domain, params.connect_port,
  212. buf, hdr->len-sizeof(params));
  213. if (child_socket >= 0) {
  214. register_vchan_connection(-1, child_socket,
  215. params.connect_domain, params.connect_port);
  216. return;
  217. }
  218. }
  219. child_agent = handle_new_process(hdr->type,
  220. params.connect_domain, params.connect_port,
  221. buf, hdr->len-sizeof(params));
  222. register_vchan_connection(child_agent, -1,
  223. params.connect_domain, params.connect_port);
  224. }
  225. void handle_service_refused(struct msg_header *hdr)
  226. {
  227. struct service_params params;
  228. int stdin_fd, stdout_fd, stderr_fd;
  229. if (hdr->len != sizeof(params)) {
  230. fprintf(stderr, "Invalid msg 0x%x length (%d)\n", MSG_SERVICE_REFUSED, hdr->len);
  231. exit(1);
  232. }
  233. if (libvchan_recv(ctrl_vchan, &params, sizeof(params)) < 0)
  234. handle_vchan_error("read exec params");
  235. sscanf(params.ident, "%d %d %d", &stdin_fd, &stdout_fd, &stderr_fd);
  236. /* TODO: send some signal? some response? */
  237. close(stdin_fd);
  238. close(stdout_fd);
  239. close(stderr_fd);
  240. }
  241. void handle_server_cmd()
  242. {
  243. struct msg_header s_hdr;
  244. if (libvchan_recv(ctrl_vchan, &s_hdr, sizeof(s_hdr)) < 0)
  245. handle_vchan_error("read s_hdr");
  246. // fprintf(stderr, "got %x %x %x\n", s_hdr.type, s_hdr.client_id,
  247. // s_hdr.len);
  248. switch (s_hdr.type) {
  249. case MSG_EXEC_CMDLINE:
  250. case MSG_JUST_EXEC:
  251. case MSG_SERVICE_CONNECT:
  252. wake_meminfo_writer();
  253. handle_server_exec_request(&s_hdr);
  254. break;
  255. case MSG_SERVICE_REFUSED:
  256. handle_service_refused(&s_hdr);
  257. break;
  258. default:
  259. fprintf(stderr, "msg type from daemon is %d ?\n",
  260. s_hdr.type);
  261. exit(1);
  262. }
  263. }
  264. volatile int child_exited;
  265. void sigchld_handler(int x __attribute__((__unused__)))
  266. {
  267. child_exited = 1;
  268. signal(SIGCHLD, sigchld_handler);
  269. }
  270. int find_connection(int pid)
  271. {
  272. int i;
  273. for (i = 0; i < MAX_FDS; i++)
  274. if (connection_info[i].pid == pid)
  275. return i;
  276. return -1;
  277. }
  278. void release_connection(int id) {
  279. struct msg_header hdr;
  280. struct exec_params params;
  281. hdr.type = MSG_CONNECTION_TERMINATED;
  282. hdr.len = sizeof(struct exec_params);
  283. params.connect_domain = connection_info[id].connect_domain;
  284. params.connect_port = connection_info[id].connect_port;
  285. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  286. handle_vchan_error("send");
  287. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  288. handle_vchan_error("send");
  289. connection_info[id].pid = 0;
  290. }
  291. void reap_children()
  292. {
  293. int status;
  294. int pid;
  295. int id;
  296. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  297. id = find_connection(pid);
  298. if (id < 0)
  299. continue;
  300. release_connection(id);
  301. }
  302. child_exited = 0;
  303. }
  304. int fill_fds_for_select(fd_set * rdset, fd_set * wrset)
  305. {
  306. int max = -1;
  307. int i;
  308. FD_ZERO(rdset);
  309. FD_ZERO(wrset);
  310. FD_SET(trigger_fd, rdset);
  311. if (trigger_fd > max)
  312. max = trigger_fd;
  313. FD_SET(passfd_socket, rdset);
  314. if (passfd_socket > max)
  315. max = passfd_socket;
  316. for (i = 0; i < MAX_FDS; i++) {
  317. if (connection_info[i].pid != 0 && connection_info[i].fd != -1) {
  318. FD_SET(connection_info[i].fd, rdset);
  319. if (connection_info[i].fd > max)
  320. max = connection_info[i].fd;
  321. }
  322. }
  323. return max;
  324. }
  325. void handle_new_passfd()
  326. {
  327. int fd = do_accept(passfd_socket);
  328. if (fd >= MAX_FDS) {
  329. fprintf(stderr, "too many clients ?\n");
  330. exit(1);
  331. }
  332. // let client know what fd has been allocated
  333. if (write(fd, &fd, sizeof(fd)) != sizeof(fd)) {
  334. perror("write to client");
  335. }
  336. }
  337. void handle_trigger_io()
  338. {
  339. struct msg_header hdr;
  340. struct trigger_service_params params;
  341. int ret;
  342. hdr.len = sizeof(params);
  343. ret = read(trigger_fd, &params, sizeof(params));
  344. if (ret == sizeof(params)) {
  345. hdr.type = MSG_TRIGGER_SERVICE;
  346. if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
  347. handle_vchan_error("write hdr");
  348. if (libvchan_send(ctrl_vchan, &params, sizeof(params)) < 0)
  349. handle_vchan_error("write params");
  350. }
  351. // trigger_fd is nonblock - so no need to reopen
  352. // not really, need to reopen at EOF
  353. if (ret <= 0) {
  354. close(trigger_fd);
  355. trigger_fd =
  356. open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
  357. }
  358. }
  359. void handle_terminated_fork_client(fd_set *rdset) {
  360. int i, ret;
  361. char buf[2];
  362. for (i = 0; i < MAX_FDS; i++) {
  363. if (connection_info[i].pid && connection_info[i].fd >= 0 &&
  364. FD_ISSET(connection_info[i].fd, rdset)) {
  365. ret = read(connection_info[i].fd, buf, sizeof(buf));
  366. if (ret == 0 || (ret == -1 && errno == ECONNRESET)) {
  367. close(connection_info[i].fd);
  368. release_connection(i);
  369. } else {
  370. fprintf(stderr, "Unexpected read on fork-server connection: %d(%s)\n", ret, strerror(errno));
  371. }
  372. }
  373. }
  374. }
  375. int main()
  376. {
  377. fd_set rdset, wrset;
  378. int max;
  379. sigset_t chld_set;
  380. init();
  381. signal(SIGCHLD, sigchld_handler);
  382. signal(SIGPIPE, SIG_IGN);
  383. sigemptyset(&chld_set);
  384. sigaddset(&chld_set, SIGCHLD);
  385. for (;;) {
  386. sigprocmask(SIG_BLOCK, &chld_set, NULL);
  387. if (child_exited)
  388. reap_children();
  389. max = fill_fds_for_select(&rdset, &wrset);
  390. if (libvchan_buffer_space(ctrl_vchan) <=
  391. (int)sizeof(struct msg_header))
  392. FD_ZERO(&rdset);
  393. wait_for_vchan_or_argfd(ctrl_vchan, max, &rdset, &wrset);
  394. sigprocmask(SIG_UNBLOCK, &chld_set, NULL);
  395. if (FD_ISSET(passfd_socket, &rdset))
  396. handle_new_passfd();
  397. while (libvchan_data_ready(ctrl_vchan))
  398. handle_server_cmd();
  399. if (FD_ISSET(trigger_fd, &rdset))
  400. handle_trigger_io();
  401. handle_terminated_fork_client(&rdset);
  402. }
  403. }