ktpd_session.c 13 KB

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