ktpd_session.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. kexec_free(ktpd->exec);
  322. ktpd->exec = NULL;
  323. ktpd->state = KTPD_SESSION_STATE_IDLE;
  324. // Send ACK message
  325. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  326. retcode8bit = (uint8_t)(retcode & 0xff);
  327. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  328. faux_msg_send_async(ack, ktpd->async);
  329. faux_msg_free(ack);
  330. type = type; // Happy compiler
  331. associated_data = associated_data; // Happy compiler
  332. return BOOL_TRUE;
  333. }
  334. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  335. void *associated_data, void *user_data)
  336. {
  337. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  338. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  339. ssize_t r = -1;
  340. faux_buf_t *faux_buf = NULL;
  341. char *buf = NULL;
  342. ssize_t len = 0;
  343. faux_msg_t *ack = NULL;
  344. if (!ktpd)
  345. return BOOL_TRUE;
  346. if (!ktpd->exec)
  347. return BOOL_TRUE;
  348. faux_buf = kexec_bufout(ktpd->exec);
  349. assert(faux_buf);
  350. do {
  351. void *linear_buf = NULL;
  352. ssize_t really_readed = 0;
  353. ssize_t linear_len =
  354. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  355. // Non-blocked read. The fd became non-blocked while
  356. // kexec_prepare().
  357. r = read(info->fd, linear_buf, linear_len);
  358. if (r > 0)
  359. really_readed = r;
  360. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  361. } while (r > 0);
  362. len = faux_buf_len(faux_buf);
  363. if (0 == len)
  364. return BOOL_TRUE;
  365. buf = malloc(len);
  366. faux_buf_read(faux_buf, buf, len);
  367. // Create KTP_STDOUT message to send to client
  368. ack = ktp_msg_preform(KTP_STDOUT, KTP_STATUS_NONE);
  369. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  370. faux_msg_send_async(ack, ktpd->async);
  371. faux_msg_free(ack);
  372. free(buf);
  373. // Happy compiler
  374. eloop = eloop;
  375. type = type;
  376. return BOOL_TRUE;
  377. }
  378. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  379. void *associated_data, void *user_data)
  380. {
  381. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  382. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  383. ssize_t r = -1;
  384. faux_buf_t *faux_buf = NULL;
  385. char *buf = NULL;
  386. ssize_t len = 0;
  387. faux_msg_t *ack = NULL;
  388. if (!ktpd)
  389. return BOOL_TRUE;
  390. if (!ktpd->exec)
  391. return BOOL_TRUE;
  392. faux_buf = kexec_buferr(ktpd->exec);
  393. assert(faux_buf);
  394. do {
  395. void *linear_buf = NULL;
  396. ssize_t really_readed = 0;
  397. ssize_t linear_len =
  398. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  399. // Non-blocked read. The fd became non-blocked while
  400. // kexec_prepare().
  401. r = read(info->fd, linear_buf, linear_len);
  402. if (r > 0)
  403. really_readed = r;
  404. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  405. } while (r > 0);
  406. len = faux_buf_len(faux_buf);
  407. if (0 == len)
  408. return BOOL_TRUE;
  409. buf = malloc(len);
  410. faux_buf_read(faux_buf, buf, len);
  411. // Create KTP_STDERR message to send to client
  412. ack = ktp_msg_preform(KTP_STDERR, KTP_STATUS_NONE);
  413. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  414. faux_msg_send_async(ack, ktpd->async);
  415. faux_msg_free(ack);
  416. free(buf);
  417. // Happy compiler
  418. eloop = eloop;
  419. type = type;
  420. return BOOL_TRUE;
  421. }
  422. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  423. int *retcode, faux_error_t *error, bool_t dry_run)
  424. {
  425. kexec_t *exec = NULL;
  426. assert(ktpd);
  427. if (!ktpd)
  428. return BOOL_FALSE;
  429. // Parsing
  430. exec = ksession_parse_for_exec(ktpd->session, line, error);
  431. if (!exec)
  432. return BOOL_FALSE;
  433. // Set dry-run flag
  434. kexec_set_dry_run(exec, dry_run);
  435. // Session status can be changed while parsing
  436. if (ksession_done(ktpd->session)) {
  437. kexec_free(exec);
  438. return BOOL_FALSE; // Because action is not completed
  439. }
  440. // Execute kexec and then wait for completion using global Eloop
  441. if (!kexec_exec(exec)) {
  442. kexec_free(exec);
  443. return BOOL_FALSE; // Something went wrong
  444. }
  445. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  446. // we don't need event loop and can return here.
  447. if (kexec_retcode(exec, retcode)) {
  448. kexec_free(exec);
  449. return BOOL_TRUE;
  450. }
  451. // Save kexec pointer to use later
  452. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  453. ktpd->exec = exec;
  454. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  455. action_stdout_ev, ktpd);
  456. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  457. action_stderr_ev, ktpd);
  458. return BOOL_TRUE;
  459. }
  460. #if 0
  461. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  462. {
  463. assert(ktpd);
  464. if (!ktpd)
  465. return;
  466. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  467. }
  468. #endif