ktpd_session.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <sys/socket.h>
  11. #include <sys/un.h>
  12. #include <syslog.h>
  13. #include <poll.h>
  14. #include <sys/wait.h>
  15. #include <faux/str.h>
  16. #include <faux/async.h>
  17. #include <faux/msg.h>
  18. #include <faux/eloop.h>
  19. #include <klish/ksession.h>
  20. #include <klish/ksession_parse.h>
  21. #include <klish/ktp.h>
  22. #include <klish/ktp_session.h>
  23. typedef enum {
  24. KTPD_SESSION_STATE_DISCONNECTED = 'd',
  25. KTPD_SESSION_STATE_UNAUTHORIZED = 'a',
  26. KTPD_SESSION_STATE_IDLE = 'i',
  27. KTPD_SESSION_STATE_WAIT_FOR_PROCESS = 'p',
  28. } ktpd_session_state_e;
  29. struct ktpd_session_s {
  30. ksession_t *session;
  31. ktpd_session_state_e state;
  32. uid_t uid;
  33. gid_t gid;
  34. char *user;
  35. faux_async_t *async;
  36. faux_hdr_t *hdr; // Engine will receive header and then msg
  37. faux_eloop_t *eloop; // External link, dont's free()
  38. kexec_t *exec;
  39. };
  40. // Static declarations
  41. static bool_t ktpd_session_read_cb(faux_async_t *async,
  42. faux_buf_t *buf, size_t len, void *user_data);
  43. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  44. void *associated_data, void *user_data);
  45. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  46. int *retcode, faux_error_t *error, bool_t dry_run);
  47. ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
  48. const char *start_entry, faux_eloop_t *eloop)
  49. {
  50. ktpd_session_t *ktpd = NULL;
  51. if (sock < 0)
  52. return NULL;
  53. if (!eloop)
  54. return NULL;
  55. ktpd = faux_zmalloc(sizeof(*ktpd));
  56. assert(ktpd);
  57. if (!ktpd)
  58. return NULL;
  59. // Init
  60. ktpd->state = KTPD_SESSION_STATE_IDLE;
  61. ktpd->eloop = eloop;
  62. ktpd->session = ksession_new(scheme, start_entry);
  63. assert(ktpd->session);
  64. // Async object
  65. ktpd->async = faux_async_new(sock);
  66. assert(ktpd->async);
  67. // Receive message header first
  68. faux_async_set_read_limits(ktpd->async,
  69. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  70. faux_async_set_read_cb(ktpd->async, ktpd_session_read_cb, ktpd);
  71. ktpd->hdr = NULL;
  72. faux_async_set_stall_cb(ktpd->async, ktp_stall_cb, ktpd->eloop);
  73. // Eloop callbacks
  74. faux_eloop_add_fd(ktpd->eloop, ktpd_session_fd(ktpd), POLLIN,
  75. ktp_peer_ev, ktpd->async);
  76. faux_eloop_add_signal(ktpd->eloop, SIGCHLD, wait_for_actions_ev, ktpd);
  77. return ktpd;
  78. }
  79. void ktpd_session_free(ktpd_session_t *ktpd)
  80. {
  81. if (!ktpd)
  82. return;
  83. kexec_free(ktpd->exec);
  84. ksession_free(ktpd->session);
  85. faux_free(ktpd->hdr);
  86. close(ktpd_session_fd(ktpd));
  87. faux_async_free(ktpd->async);
  88. faux_free(ktpd);
  89. }
  90. static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
  91. {
  92. char *line = NULL;
  93. int retcode = -1;
  94. ktp_cmd_e cmd = KTP_CMD_ACK;
  95. faux_error_t *error = NULL;
  96. bool_t rc = BOOL_FALSE;
  97. bool_t dry_run = BOOL_FALSE;
  98. assert(ktpd);
  99. assert(msg);
  100. // Get line from message
  101. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  102. ktp_send_error(ktpd->async, cmd, "The line is not specified");
  103. return BOOL_FALSE;
  104. }
  105. // Get dry-run flag from message
  106. if (KTP_STATUS_IS_DRY_RUN(faux_msg_get_status(msg)))
  107. dry_run = BOOL_TRUE;
  108. error = faux_error_new();
  109. rc = ktpd_session_exec(ktpd, line, &retcode, error, dry_run);
  110. faux_str_free(line);
  111. // Command is scheduled. Eloop will wait for ACTION completion.
  112. // So inform client about it and about command features like
  113. // interactive/non-interactive.
  114. if (ktpd->exec) {
  115. faux_msg_t *ack = NULL;
  116. ktp_status_e status = KTP_STATUS_INCOMPLETED;
  117. ack = ktp_msg_preform(cmd, status);
  118. faux_msg_send_async(ack, ktpd->async);
  119. faux_msg_free(ack);
  120. faux_error_free(error);
  121. return BOOL_TRUE; // Continue and wait for ACTION
  122. }
  123. // Session status can be changed while parsing
  124. if (ksession_done(ktpd->session)) {
  125. ktp_send_error(ktpd->async, cmd, "Interrupted by system");
  126. faux_error_free(error);
  127. return BOOL_FALSE;
  128. }
  129. if (rc) {
  130. uint8_t retcode8bit = 0;
  131. faux_msg_t *ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  132. retcode8bit = (uint8_t)(retcode & 0xff);
  133. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  134. faux_msg_send_async(ack, ktpd->async);
  135. faux_msg_free(ack);
  136. } else {
  137. char *err = faux_error_cstr(error);
  138. ktp_send_error(ktpd->async, cmd, err);
  139. faux_str_free(err);
  140. return BOOL_FALSE;
  141. }
  142. faux_error_free(error);
  143. return BOOL_TRUE;
  144. }
  145. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  146. {
  147. char *line = NULL;
  148. faux_msg_t *ack = NULL;
  149. kpargv_t *pargv = NULL;
  150. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  151. assert(ktpd);
  152. assert(msg);
  153. // Get line from message
  154. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  155. ktp_send_error(ktpd->async, cmd, NULL);
  156. return BOOL_FALSE;
  157. }
  158. // Parsing
  159. pargv = ksession_parse_for_completion(ktpd->session, line);
  160. faux_str_free(line);
  161. if (!pargv) {
  162. ktp_send_error(ktpd->async, cmd, NULL);
  163. return BOOL_FALSE;
  164. }
  165. kpargv_debug(pargv);
  166. kpargv_free(pargv);
  167. // Send ACK message
  168. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  169. faux_msg_send_async(ack, ktpd->async);
  170. faux_msg_free(ack);
  171. return BOOL_TRUE;
  172. }
  173. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  174. {
  175. char *line = NULL;
  176. faux_msg_t *ack = NULL;
  177. // kpargv_t *pargv = NULL;
  178. ktp_cmd_e cmd = KTP_HELP_ACK;
  179. assert(ktpd);
  180. assert(msg);
  181. // Get line from message
  182. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  183. ktp_send_error(ktpd->async, cmd, NULL);
  184. return BOOL_FALSE;
  185. }
  186. /* // Parsing
  187. pargv = ksession_parse_line(ktpd->session, line, KPURPOSE_HELP);
  188. faux_str_free(line);
  189. kpargv_free(pargv);
  190. */
  191. // Send ACK message
  192. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  193. faux_msg_send_async(ack, ktpd->async);
  194. faux_msg_free(ack);
  195. return BOOL_TRUE;
  196. }
  197. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  198. {
  199. uint16_t cmd = 0;
  200. assert(ktpd);
  201. if (!ktpd)
  202. return BOOL_FALSE;
  203. assert(msg);
  204. if (!msg)
  205. return BOOL_FALSE;
  206. cmd = faux_msg_get_cmd(msg);
  207. switch (cmd) {
  208. case KTP_CMD:
  209. ktpd_session_process_cmd(ktpd, msg);
  210. break;
  211. case KTP_COMPLETION:
  212. ktpd_session_process_completion(ktpd, msg);
  213. break;
  214. case KTP_HELP:
  215. ktpd_session_process_help(ktpd, msg);
  216. break;
  217. default:
  218. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  219. break;
  220. }
  221. return BOOL_TRUE;
  222. }
  223. /** @brief Low-level function to receive KTP message.
  224. *
  225. * Firstly function gets the header of message. Then it checks and parses
  226. * header and find out the length of whole message. Then it receives the rest
  227. * of message.
  228. */
  229. static bool_t ktpd_session_read_cb(faux_async_t *async,
  230. faux_buf_t *buf, size_t len, void *user_data)
  231. {
  232. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  233. faux_msg_t *completed_msg = NULL;
  234. char *data = NULL;
  235. assert(async);
  236. assert(buf);
  237. assert(ktpd);
  238. // Linearize buffer
  239. data = malloc(len);
  240. faux_buf_read(buf, data, len);
  241. // Receive header
  242. if (!ktpd->hdr) {
  243. size_t whole_len = 0;
  244. size_t msg_wo_hdr = 0;
  245. ktpd->hdr = (faux_hdr_t *)data;
  246. // Check for broken header
  247. if (!ktp_check_header(ktpd->hdr)) {
  248. faux_free(ktpd->hdr);
  249. ktpd->hdr = NULL;
  250. return BOOL_FALSE;
  251. }
  252. whole_len = faux_hdr_len(ktpd->hdr);
  253. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  254. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  255. // Plan to receive message body
  256. if (msg_wo_hdr > 0) {
  257. faux_async_set_read_limits(async,
  258. msg_wo_hdr, msg_wo_hdr);
  259. return BOOL_TRUE;
  260. }
  261. // Here message is completed (msg body has zero length)
  262. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  263. // Receive message body
  264. } else {
  265. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  266. faux_free(data);
  267. }
  268. // Plan to receive msg header
  269. faux_async_set_read_limits(ktpd->async,
  270. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  271. faux_free(ktpd->hdr);
  272. ktpd->hdr = NULL; // Ready to recv new header
  273. // Here message is completed
  274. ktpd_session_dispatch(ktpd, completed_msg);
  275. faux_msg_free(completed_msg);
  276. // Session status can be changed while parsing
  277. if (ksession_done(ktpd->session))
  278. return BOOL_FALSE;
  279. return BOOL_TRUE;
  280. }
  281. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  282. {
  283. assert(ktpd);
  284. if (!ktpd)
  285. return BOOL_FALSE;
  286. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  287. return BOOL_FALSE;
  288. return BOOL_TRUE;
  289. }
  290. int ktpd_session_fd(const ktpd_session_t *ktpd)
  291. {
  292. assert(ktpd);
  293. if (!ktpd)
  294. return BOOL_FALSE;
  295. return faux_async_fd(ktpd->async);
  296. }
  297. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  298. void *associated_data, void *user_data)
  299. {
  300. int wstatus = 0;
  301. pid_t child_pid = -1;
  302. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  303. int retcode = -1;
  304. uint8_t retcode8bit = 0;
  305. faux_msg_t *ack = NULL;
  306. ktp_cmd_e cmd = KTP_CMD_ACK;
  307. if (!ktpd)
  308. return BOOL_FALSE;
  309. // Wait for any child process. Doesn't block.
  310. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  311. if (ktpd->exec)
  312. kexec_continue_command_execution(ktpd->exec, child_pid,
  313. wstatus);
  314. }
  315. if (!ktpd->exec)
  316. return BOOL_TRUE;
  317. // Check if kexec is done now
  318. if (!kexec_retcode(ktpd->exec, &retcode))
  319. return BOOL_TRUE; // Continue
  320. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  321. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  322. kexec_free(ktpd->exec);
  323. ktpd->exec = NULL;
  324. ktpd->state = KTPD_SESSION_STATE_IDLE;
  325. // Send ACK message
  326. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  327. retcode8bit = (uint8_t)(retcode & 0xff);
  328. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  329. faux_msg_send_async(ack, ktpd->async);
  330. faux_msg_free(ack);
  331. type = type; // Happy compiler
  332. associated_data = associated_data; // Happy compiler
  333. return BOOL_TRUE;
  334. }
  335. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  336. void *associated_data, void *user_data)
  337. {
  338. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  339. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  340. ssize_t r = -1;
  341. faux_buf_t *faux_buf = NULL;
  342. char *buf = NULL;
  343. ssize_t len = 0;
  344. faux_msg_t *ack = NULL;
  345. // Some errors or fd is closed so remove it from polling
  346. if (!(info->revents & POLLIN)) {
  347. faux_eloop_del_fd(eloop, info->fd);
  348. return BOOL_TRUE;
  349. }
  350. if (!ktpd)
  351. return BOOL_TRUE;
  352. if (!ktpd->exec)
  353. return BOOL_TRUE;
  354. faux_buf = kexec_bufout(ktpd->exec);
  355. assert(faux_buf);
  356. do {
  357. void *linear_buf = NULL;
  358. ssize_t really_readed = 0;
  359. ssize_t linear_len =
  360. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  361. // Non-blocked read. The fd became non-blocked while
  362. // kexec_prepare().
  363. r = read(info->fd, linear_buf, linear_len);
  364. if (r > 0)
  365. really_readed = r;
  366. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  367. } while (r > 0);
  368. len = faux_buf_len(faux_buf);
  369. if (0 == len)
  370. return BOOL_TRUE;
  371. buf = malloc(len);
  372. faux_buf_read(faux_buf, buf, len);
  373. // Create KTP_STDOUT message to send to client
  374. ack = ktp_msg_preform(KTP_STDOUT, KTP_STATUS_NONE);
  375. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  376. faux_msg_send_async(ack, ktpd->async);
  377. faux_msg_free(ack);
  378. free(buf);
  379. // Happy compiler
  380. eloop = eloop;
  381. type = type;
  382. return BOOL_TRUE;
  383. }
  384. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  385. void *associated_data, void *user_data)
  386. {
  387. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  388. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  389. ssize_t r = -1;
  390. faux_buf_t *faux_buf = NULL;
  391. char *buf = NULL;
  392. ssize_t len = 0;
  393. faux_msg_t *ack = NULL;
  394. // Some errors or fd is closed so remove it from polling
  395. if (!(info->revents & POLLIN)) {
  396. faux_eloop_del_fd(eloop, info->fd);
  397. return BOOL_TRUE;
  398. }
  399. if (!ktpd)
  400. return BOOL_TRUE;
  401. if (!ktpd->exec)
  402. return BOOL_TRUE;
  403. faux_buf = kexec_buferr(ktpd->exec);
  404. assert(faux_buf);
  405. do {
  406. void *linear_buf = NULL;
  407. ssize_t really_readed = 0;
  408. ssize_t linear_len =
  409. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  410. // Non-blocked read. The fd became non-blocked while
  411. // kexec_prepare().
  412. r = read(info->fd, linear_buf, linear_len);
  413. if (r > 0)
  414. really_readed = r;
  415. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  416. } while (r > 0);
  417. len = faux_buf_len(faux_buf);
  418. if (0 == len)
  419. return BOOL_TRUE;
  420. buf = malloc(len);
  421. faux_buf_read(faux_buf, buf, len);
  422. // Create KTP_STDERR message to send to client
  423. ack = ktp_msg_preform(KTP_STDERR, KTP_STATUS_NONE);
  424. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  425. faux_msg_send_async(ack, ktpd->async);
  426. faux_msg_free(ack);
  427. free(buf);
  428. // Happy compiler
  429. eloop = eloop;
  430. type = type;
  431. return BOOL_TRUE;
  432. }
  433. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  434. int *retcode, faux_error_t *error, bool_t dry_run)
  435. {
  436. kexec_t *exec = NULL;
  437. assert(ktpd);
  438. if (!ktpd)
  439. return BOOL_FALSE;
  440. // Parsing
  441. exec = ksession_parse_for_exec(ktpd->session, line, error);
  442. if (!exec)
  443. return BOOL_FALSE;
  444. // Set dry-run flag
  445. kexec_set_dry_run(exec, dry_run);
  446. // Session status can be changed while parsing
  447. if (ksession_done(ktpd->session)) {
  448. kexec_free(exec);
  449. return BOOL_FALSE; // Because action is not completed
  450. }
  451. // Execute kexec and then wait for completion using global Eloop
  452. if (!kexec_exec(exec)) {
  453. kexec_free(exec);
  454. return BOOL_FALSE; // Something went wrong
  455. }
  456. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  457. // we don't need event loop and can return here.
  458. if (kexec_retcode(exec, retcode)) {
  459. kexec_free(exec);
  460. return BOOL_TRUE;
  461. }
  462. // Save kexec pointer to use later
  463. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  464. ktpd->exec = exec;
  465. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  466. action_stdout_ev, ktpd);
  467. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  468. action_stderr_ev, ktpd);
  469. return BOOL_TRUE;
  470. }
  471. #if 0
  472. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  473. {
  474. assert(ktpd);
  475. if (!ktpd)
  476. return;
  477. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  478. }
  479. #endif