qrexec-agent.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 "qrexec.h"
  34. #include "libqrexec-utils.h"
  35. enum fdtype {
  36. FDTYPE_INVALID,
  37. FDTYPE_STDOUT,
  38. FDTYPE_STDERR
  39. };
  40. struct _process_fd {
  41. int client_id;
  42. int type;
  43. int is_blocked;
  44. };
  45. struct _client_info {
  46. int stdin_fd;
  47. int stdout_fd;
  48. int stderr_fd;
  49. int exit_status;
  50. int is_exited;
  51. int pid;
  52. int is_blocked;
  53. int is_close_after_flush_needed;
  54. struct buffer buffer;
  55. };
  56. int max_process_fd = -1;
  57. /* indexed by file descriptor */
  58. struct _process_fd process_fd[MAX_FDS];
  59. /* indexed by client id, which is descriptor number of a client in daemon */
  60. struct _client_info client_info[MAX_FDS];
  61. int trigger_fd;
  62. int passfd_socket;
  63. int meminfo_write_started = 0;
  64. void init()
  65. {
  66. peer_server_init(REXEC_PORT);
  67. umask(0);
  68. mkfifo(QREXEC_AGENT_TRIGGER_PATH, 0666);
  69. passfd_socket = get_server_socket(QREXEC_AGENT_FDPASS_PATH);
  70. umask(077);
  71. trigger_fd =
  72. open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
  73. }
  74. void wake_meminfo_writer() {
  75. FILE *f;
  76. pid_t pid;
  77. if (meminfo_write_started)
  78. /* wake meminfo-writer only once */
  79. return;
  80. f = fopen(MEMINFO_WRITER_PIDFILE, "r");
  81. if (f == NULL) {
  82. /* no meminfo-writer found, ignoring */
  83. return;
  84. }
  85. if (fscanf(f, "%d", &pid) < 1) {
  86. /* no meminfo-writer found, ignoring */
  87. return;
  88. }
  89. fclose(f);
  90. kill(pid, SIGUSR1);
  91. meminfo_write_started = 1;
  92. }
  93. void no_colon_in_cmd()
  94. {
  95. fprintf(stderr,
  96. "cmdline is supposed to be in user:command form\n");
  97. exit(1);
  98. }
  99. void do_exec(const char *cmd)
  100. {
  101. char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(QUBES_RPC_MAGIC_CMD) + 1];
  102. char *realcmd = index(cmd, ':'), *user;
  103. if (!realcmd)
  104. no_colon_in_cmd();
  105. /* mark end of username and move to command */
  106. user=strndup(cmd,realcmd-cmd);
  107. realcmd++;
  108. /* ignore "nogui:" prefix in linux agent */
  109. if (strncmp(realcmd, "nogui:", 6) == 0)
  110. realcmd+=6;
  111. /* replace magic RPC cmd with RPC multiplexer path */
  112. if (strncmp(realcmd, QUBES_RPC_MAGIC_CMD " ", strlen(QUBES_RPC_MAGIC_CMD)+1)==0) {
  113. strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
  114. strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), realcmd + strlen(QUBES_RPC_MAGIC_CMD));
  115. realcmd = buf;
  116. }
  117. signal(SIGCHLD, SIG_DFL);
  118. signal(SIGPIPE, SIG_DFL);
  119. execl("/bin/su", "su", "-", user, "-c", realcmd, NULL);
  120. perror("execl");
  121. exit(1);
  122. }
  123. void handle_just_exec(int client_id, int len)
  124. {
  125. char buf[len];
  126. int fdn, pid;
  127. read_all_vchan_ext(buf, len);
  128. switch (pid = fork()) {
  129. case -1:
  130. perror("fork");
  131. exit(1);
  132. case 0:
  133. fdn = open("/dev/null", O_RDWR);
  134. fix_fds(fdn, fdn, fdn);
  135. do_exec(buf);
  136. perror("execl");
  137. exit(1);
  138. default:;
  139. }
  140. fprintf(stderr, "executed (nowait) %s pid %d\n", buf, pid);
  141. }
  142. void create_info_about_client(int client_id, int pid, int stdin_fd,
  143. int stdout_fd, int stderr_fd)
  144. {
  145. process_fd[stdout_fd].client_id = client_id;
  146. process_fd[stdout_fd].type = FDTYPE_STDOUT;
  147. process_fd[stdout_fd].is_blocked = 0;
  148. process_fd[stderr_fd].client_id = client_id;
  149. process_fd[stderr_fd].type = FDTYPE_STDERR;
  150. process_fd[stderr_fd].is_blocked = 0;
  151. if (stderr_fd > max_process_fd)
  152. max_process_fd = stderr_fd;
  153. if (stdout_fd > max_process_fd)
  154. max_process_fd = stdout_fd;
  155. set_nonblock(stdin_fd);
  156. client_info[client_id].stdin_fd = stdin_fd;
  157. client_info[client_id].stdout_fd = stdout_fd;
  158. client_info[client_id].stderr_fd = stderr_fd;
  159. client_info[client_id].exit_status = 0;
  160. client_info[client_id].is_exited = 0;
  161. client_info[client_id].pid = pid;
  162. client_info[client_id].is_blocked = 0;
  163. client_info[client_id].is_close_after_flush_needed = 0;
  164. buffer_init(&client_info[client_id].buffer);
  165. }
  166. void handle_exec(int client_id, int len)
  167. {
  168. char buf[len];
  169. int pid, stdin_fd, stdout_fd, stderr_fd;
  170. read_all_vchan_ext(buf, len);
  171. do_fork_exec(buf, &pid, &stdin_fd, &stdout_fd, &stderr_fd);
  172. create_info_about_client(client_id, pid, stdin_fd, stdout_fd,
  173. stderr_fd);
  174. fprintf(stderr, "executed %s pid %d\n", buf, pid);
  175. }
  176. void handle_connect_existing(int client_id, int len)
  177. {
  178. int stdin_fd, stdout_fd, stderr_fd;
  179. char buf[len];
  180. read_all_vchan_ext(buf, len);
  181. sscanf(buf, "%d %d %d", &stdin_fd, &stdout_fd, &stderr_fd);
  182. create_info_about_client(client_id, -1, stdin_fd, stdout_fd,
  183. stderr_fd);
  184. client_info[client_id].is_exited = 1; //do not wait for SIGCHLD
  185. }
  186. void update_max_process_fd()
  187. {
  188. int i;
  189. for (i = max_process_fd;
  190. process_fd[i].type == FDTYPE_INVALID && i >= 0; i--);
  191. max_process_fd = i;
  192. }
  193. void send_exit_code(int client_id, int status)
  194. {
  195. struct server_header s_hdr;
  196. s_hdr.type = MSG_AGENT_TO_SERVER_EXIT_CODE;
  197. s_hdr.client_id = client_id;
  198. s_hdr.len = sizeof status;
  199. write_all_vchan_ext(&s_hdr, sizeof s_hdr);
  200. write_all_vchan_ext(&status, sizeof(status));
  201. fprintf(stderr, "send exit code %d for client_id %d pid %d\n",
  202. status, client_id, client_info[client_id].pid);
  203. }
  204. // erase process data structures, possibly forced by remote
  205. void remove_process(int client_id, int status)
  206. {
  207. int i;
  208. if (!client_info[client_id].pid)
  209. return;
  210. if (client_info[client_id].stdin_fd >= 0)
  211. fork_and_flush_stdin(client_info[client_id].stdin_fd,
  212. &client_info[client_id].buffer);
  213. #if 0
  214. // let's let it die by itself, possibly after it has received buffered stdin
  215. kill(client_info[client_id].pid, SIGKILL);
  216. #endif
  217. if (status != -1)
  218. send_exit_code(client_id, status);
  219. close(client_info[client_id].stdin_fd);
  220. client_info[client_id].pid = 0;
  221. client_info[client_id].stdin_fd = -1;
  222. client_info[client_id].is_blocked = 0;
  223. buffer_free(&client_info[client_id].buffer);
  224. for (i = 0; i <= max_process_fd; i++)
  225. if (process_fd[i].type != FDTYPE_INVALID
  226. && process_fd[i].client_id == client_id) {
  227. process_fd[i].type = FDTYPE_INVALID;
  228. process_fd[i].client_id = -1;
  229. process_fd[i].is_blocked = 0;
  230. close(i);
  231. }
  232. update_max_process_fd();
  233. }
  234. // remove process not immediately after it has exited, but after its stdout and stderr has been drained
  235. // previous method implemented in flush_out_err was broken - it cannot work when peer signalled it is blocked
  236. void possibly_remove_process(int client_id)
  237. {
  238. if (client_info[client_id].stdout_fd == -1 &&
  239. client_info[client_id].stderr_fd == -1 &&
  240. client_info[client_id].is_exited)
  241. remove_process(client_id,
  242. client_info[client_id].exit_status);
  243. }
  244. void handle_input(int client_id, int len)
  245. {
  246. char buf[len];
  247. read_all_vchan_ext(buf, len);
  248. if (!client_info[client_id].pid || client_info[client_id].stdin_fd == -1)
  249. return;
  250. if (len == 0) {
  251. if (client_info[client_id].is_blocked)
  252. client_info[client_id].is_close_after_flush_needed
  253. = 1;
  254. else {
  255. close(client_info[client_id].stdin_fd);
  256. client_info[client_id].stdin_fd = -1;
  257. }
  258. return;
  259. }
  260. switch (write_stdin
  261. (client_info[client_id].stdin_fd, client_id, buf, len,
  262. &client_info[client_id].buffer)) {
  263. case WRITE_STDIN_OK:
  264. break;
  265. case WRITE_STDIN_BUFFERED:
  266. client_info[client_id].is_blocked = 1;
  267. break;
  268. case WRITE_STDIN_ERROR:
  269. // do not remove process, as it still can write data to stdout
  270. close(client_info[client_id].stdin_fd);
  271. client_info[client_id].stdin_fd = -1;
  272. client_info[client_id].is_blocked = 0;
  273. break;
  274. default:
  275. fprintf(stderr, "unknown write_stdin?\n");
  276. exit(1);
  277. }
  278. }
  279. void set_blocked_outerr(int client_id, int val)
  280. {
  281. process_fd[client_info[client_id].stdout_fd].is_blocked = val;
  282. process_fd[client_info[client_id].stderr_fd].is_blocked = val;
  283. }
  284. void handle_server_data()
  285. {
  286. struct server_header s_hdr;
  287. read_all_vchan_ext(&s_hdr, sizeof s_hdr);
  288. // fprintf(stderr, "got %x %x %x\n", s_hdr.type, s_hdr.client_id,
  289. // s_hdr.len);
  290. switch (s_hdr.type) {
  291. case MSG_XON:
  292. set_blocked_outerr(s_hdr.client_id, 0);
  293. break;
  294. case MSG_XOFF:
  295. set_blocked_outerr(s_hdr.client_id, 1);
  296. break;
  297. case MSG_SERVER_TO_AGENT_CONNECT_EXISTING:
  298. handle_connect_existing(s_hdr.client_id, s_hdr.len);
  299. break;
  300. case MSG_SERVER_TO_AGENT_EXEC_CMDLINE:
  301. wake_meminfo_writer();
  302. handle_exec(s_hdr.client_id, s_hdr.len);
  303. break;
  304. case MSG_SERVER_TO_AGENT_JUST_EXEC:
  305. wake_meminfo_writer();
  306. handle_just_exec(s_hdr.client_id, s_hdr.len);
  307. break;
  308. case MSG_SERVER_TO_AGENT_INPUT:
  309. handle_input(s_hdr.client_id, s_hdr.len);
  310. break;
  311. case MSG_SERVER_TO_AGENT_CLIENT_END:
  312. remove_process(s_hdr.client_id, -1);
  313. break;
  314. default:
  315. fprintf(stderr, "msg type from daemon is %d ?\n",
  316. s_hdr.type);
  317. exit(1);
  318. }
  319. }
  320. void handle_process_data(int fd)
  321. {
  322. struct server_header s_hdr;
  323. char buf[MAX_DATA_CHUNK];
  324. int ret;
  325. int len;
  326. len = buffer_space_vchan_ext();
  327. if (len <= sizeof s_hdr)
  328. return;
  329. ret = read(fd, buf, len - sizeof s_hdr);
  330. s_hdr.client_id = process_fd[fd].client_id;
  331. if (process_fd[fd].type == FDTYPE_STDOUT)
  332. s_hdr.type = MSG_AGENT_TO_SERVER_STDOUT;
  333. else if (process_fd[fd].type == FDTYPE_STDERR)
  334. s_hdr.type = MSG_AGENT_TO_SERVER_STDERR;
  335. else {
  336. fprintf(stderr, "fd=%d, client_id=%d, type=%d ?\n", fd,
  337. process_fd[fd].client_id, process_fd[fd].type);
  338. exit(1);
  339. }
  340. s_hdr.len = ret;
  341. if (ret >= 0) {
  342. write_all_vchan_ext(&s_hdr, sizeof s_hdr);
  343. write_all_vchan_ext(buf, ret);
  344. }
  345. if (ret == 0) {
  346. int client_id = process_fd[fd].client_id;
  347. if (process_fd[fd].type == FDTYPE_STDOUT)
  348. client_info[client_id].stdout_fd = -1;
  349. else
  350. client_info[client_id].stderr_fd = -1;
  351. process_fd[fd].type = FDTYPE_INVALID;
  352. process_fd[fd].client_id = -1;
  353. process_fd[fd].is_blocked = 0;
  354. close(fd);
  355. update_max_process_fd();
  356. possibly_remove_process(client_id);
  357. }
  358. if (ret < 0)
  359. remove_process(process_fd[fd].client_id, 127);
  360. }
  361. volatile int child_exited;
  362. void sigchld_handler(int x)
  363. {
  364. child_exited = 1;
  365. signal(SIGCHLD, sigchld_handler);
  366. }
  367. int find_info(int pid)
  368. {
  369. int i;
  370. for (i = 0; i < MAX_FDS; i++)
  371. if (client_info[i].pid == pid)
  372. return i;
  373. return -1;
  374. }
  375. void handle_process_data_all(fd_set * select_fds)
  376. {
  377. int i;
  378. for (i = 0; i <= max_process_fd; i++)
  379. if (process_fd[i].type != FDTYPE_INVALID
  380. && FD_ISSET(i, select_fds))
  381. handle_process_data(i);
  382. }
  383. void reap_children()
  384. {
  385. int status;
  386. int pid;
  387. int client_id;
  388. while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
  389. client_id = find_info(pid);
  390. if (client_id < 0)
  391. continue;
  392. client_info[client_id].is_exited = 1;
  393. client_info[client_id].exit_status = status;
  394. possibly_remove_process(client_id);
  395. }
  396. child_exited = 0;
  397. }
  398. int fill_fds_for_select(fd_set * rdset, fd_set * wrset)
  399. {
  400. int max = -1;
  401. int fd, i;
  402. FD_ZERO(rdset);
  403. FD_ZERO(wrset);
  404. for (i = 0; i <= max_process_fd; i++)
  405. if (process_fd[i].type != FDTYPE_INVALID
  406. && !process_fd[i].is_blocked) {
  407. FD_SET(i, rdset);
  408. max = i;
  409. }
  410. FD_SET(trigger_fd, rdset);
  411. if (trigger_fd > max)
  412. max = trigger_fd;
  413. FD_SET(passfd_socket, rdset);
  414. if (passfd_socket > max)
  415. max = passfd_socket;
  416. for (i = 0; i < MAX_FDS; i++)
  417. if (client_info[i].pid && client_info[i].is_blocked) {
  418. fd = client_info[i].stdin_fd;
  419. FD_SET(fd, wrset);
  420. if (fd > max)
  421. max = fd;
  422. }
  423. return max;
  424. }
  425. void flush_client_data_agent(int client_id)
  426. {
  427. struct _client_info *info = &client_info[client_id];
  428. switch (flush_client_data
  429. (info->stdin_fd, client_id, &info->buffer)) {
  430. case WRITE_STDIN_OK:
  431. info->is_blocked = 0;
  432. if (info->is_close_after_flush_needed) {
  433. close(info->stdin_fd);
  434. info->stdin_fd = -1;
  435. info->is_close_after_flush_needed = 0;
  436. }
  437. break;
  438. case WRITE_STDIN_ERROR:
  439. // do not remove process, as it still can write data to stdout
  440. info->is_blocked = 0;
  441. close(info->stdin_fd);
  442. info->stdin_fd = -1;
  443. info->is_close_after_flush_needed = 0;
  444. break;
  445. case WRITE_STDIN_BUFFERED:
  446. break;
  447. default:
  448. fprintf(stderr, "unknown flush_client_data?\n");
  449. exit(1);
  450. }
  451. }
  452. void handle_new_passfd()
  453. {
  454. int fd = do_accept(passfd_socket);
  455. if (fd >= MAX_FDS) {
  456. fprintf(stderr, "too many clients ?\n");
  457. exit(1);
  458. }
  459. // let client know what fd has been allocated
  460. write(fd, &fd, sizeof(fd));
  461. }
  462. void handle_trigger_io()
  463. {
  464. struct server_header s_hdr;
  465. struct trigger_connect_params params;
  466. int ret;
  467. s_hdr.client_id = 0;
  468. s_hdr.len = 0;
  469. ret = read(trigger_fd, &params, sizeof(params));
  470. if (ret == sizeof(params)) {
  471. s_hdr.type = MSG_AGENT_TO_SERVER_TRIGGER_CONNECT_EXISTING;
  472. write_all_vchan_ext(&s_hdr, sizeof s_hdr);
  473. write_all_vchan_ext(&params, sizeof params);
  474. }
  475. // trigger_fd is nonblock - so no need to reopen
  476. // not really, need to reopen at EOF
  477. if (ret <= 0) {
  478. close(trigger_fd);
  479. trigger_fd =
  480. open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
  481. }
  482. }
  483. int main()
  484. {
  485. fd_set rdset, wrset;
  486. int max;
  487. int i;
  488. sigset_t chld_set;
  489. init();
  490. signal(SIGCHLD, sigchld_handler);
  491. signal(SIGPIPE, SIG_IGN);
  492. sigemptyset(&chld_set);
  493. sigaddset(&chld_set, SIGCHLD);
  494. for (;;) {
  495. sigprocmask(SIG_BLOCK, &chld_set, NULL);
  496. if (child_exited)
  497. reap_children();
  498. max = fill_fds_for_select(&rdset, &wrset);
  499. if (buffer_space_vchan_ext() <=
  500. sizeof(struct server_header))
  501. FD_ZERO(&rdset);
  502. wait_for_vchan_or_argfd(max, &rdset, &wrset);
  503. sigprocmask(SIG_UNBLOCK, &chld_set, NULL);
  504. if (FD_ISSET(passfd_socket, &rdset))
  505. handle_new_passfd();
  506. while (read_ready_vchan_ext())
  507. handle_server_data();
  508. if (FD_ISSET(trigger_fd, &rdset))
  509. handle_trigger_io();
  510. handle_process_data_all(&rdset);
  511. for (i = 0; i <= MAX_FDS; i++)
  512. if (client_info[i].pid
  513. && client_info[i].is_blocked
  514. && FD_ISSET(client_info[i].stdin_fd, &wrset))
  515. flush_client_data_agent(i);
  516. }
  517. }