ktpd_session.c 13 KB

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