ktpd_session.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. void *data, 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. void *data, 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. assert(async);
  222. assert(data);
  223. assert(ktpd);
  224. // Receive header
  225. if (!ktpd->hdr) {
  226. size_t whole_len = 0;
  227. size_t msg_wo_hdr = 0;
  228. ktpd->hdr = (faux_hdr_t *)data;
  229. // Check for broken header
  230. if (!ktp_check_header(ktpd->hdr)) {
  231. faux_free(ktpd->hdr);
  232. ktpd->hdr = NULL;
  233. return BOOL_FALSE;
  234. }
  235. whole_len = faux_hdr_len(ktpd->hdr);
  236. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  237. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  238. // Plan to receive message body
  239. if (msg_wo_hdr > 0) {
  240. faux_async_set_read_limits(async,
  241. msg_wo_hdr, msg_wo_hdr);
  242. return BOOL_TRUE;
  243. }
  244. // Here message is completed (msg body has zero length)
  245. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  246. // Receive message body
  247. } else {
  248. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  249. faux_free(data);
  250. }
  251. // Plan to receive msg header
  252. faux_async_set_read_limits(ktpd->async,
  253. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  254. faux_free(ktpd->hdr);
  255. ktpd->hdr = NULL; // Ready to recv new header
  256. // Here message is completed
  257. ktpd_session_dispatch(ktpd, completed_msg);
  258. faux_msg_free(completed_msg);
  259. // Session status can be changed while parsing
  260. if (ksession_done(ktpd->session))
  261. return BOOL_FALSE;
  262. return BOOL_TRUE;
  263. }
  264. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  265. {
  266. assert(ktpd);
  267. if (!ktpd)
  268. return BOOL_FALSE;
  269. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  270. return BOOL_FALSE;
  271. return BOOL_TRUE;
  272. }
  273. int ktpd_session_fd(const ktpd_session_t *ktpd)
  274. {
  275. assert(ktpd);
  276. if (!ktpd)
  277. return BOOL_FALSE;
  278. return faux_async_fd(ktpd->async);
  279. }
  280. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  281. void *associated_data, void *user_data)
  282. {
  283. int wstatus = 0;
  284. pid_t child_pid = -1;
  285. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  286. int retcode = -1;
  287. uint8_t retcode8bit = 0;
  288. faux_msg_t *ack = NULL;
  289. ktp_cmd_e cmd = KTP_CMD_ACK;
  290. if (!ktpd)
  291. return BOOL_FALSE;
  292. // Wait for any child process. Doesn't block.
  293. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  294. if (ktpd->exec)
  295. kexec_continue_command_execution(ktpd->exec, child_pid,
  296. wstatus);
  297. }
  298. if (!ktpd->exec)
  299. return BOOL_TRUE;
  300. // Check if kexec is done now
  301. if (!kexec_retcode(ktpd->exec, &retcode))
  302. return BOOL_TRUE; // Continue
  303. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  304. kexec_free(ktpd->exec);
  305. ktpd->exec = NULL;
  306. ktpd->state = KTPD_SESSION_STATE_IDLE;
  307. // Send ACK message
  308. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  309. retcode8bit = (uint8_t)(retcode & 0xff);
  310. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  311. faux_msg_send_async(ack, ktpd->async);
  312. faux_msg_free(ack);
  313. type = type; // Happy compiler
  314. associated_data = associated_data; // Happy compiler
  315. return BOOL_TRUE;
  316. }
  317. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  318. void *associated_data, void *user_data)
  319. {
  320. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  321. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  322. kexec_t *exec = NULL;
  323. ssize_t r = -1;
  324. faux_buf_t *faux_buf = NULL;
  325. void *linear_buf = NULL;
  326. char *buf = NULL;
  327. ssize_t len = 0;
  328. if (!ktpd)
  329. return BOOL_TRUE;
  330. exec = ktpd->exec;
  331. if (!exec)
  332. return BOOL_TRUE;
  333. faux_buf = kexec_bufout(exec);
  334. assert(faux_buf);
  335. do {
  336. ssize_t really_readed = 0;
  337. ssize_t linear_len =
  338. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  339. // Non-blocked read. The fd became non-blocked while
  340. // kexec_prepare().
  341. r = read(info->fd, linear_buf, linear_len);
  342. if (r > 0)
  343. really_readed = r;
  344. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  345. } while (r > 0);
  346. len = faux_buf_len(faux_buf);
  347. if (0 == len)
  348. return BOOL_TRUE;
  349. buf = malloc(len);
  350. faux_buf_read(faux_buf, buf, len);
  351. write(STDOUT_FILENO, buf, len);
  352. free(buf);
  353. // Happy compiler
  354. eloop = eloop;
  355. type = type;
  356. return BOOL_TRUE;
  357. }
  358. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  359. int *retcode, faux_error_t *error)
  360. {
  361. kexec_t *exec = NULL;
  362. assert(ktpd);
  363. if (!ktpd)
  364. return BOOL_FALSE;
  365. // Parsing
  366. exec = ksession_parse_for_exec(ktpd->session, line, error);
  367. if (!exec)
  368. return BOOL_FALSE;
  369. // Session status can be changed while parsing
  370. if (ksession_done(ktpd->session)) {
  371. kexec_free(exec);
  372. return BOOL_FALSE; // Because action is not completed
  373. }
  374. // Execute kexec and then wait for completion using global Eloop
  375. if (!kexec_exec(exec)) {
  376. kexec_free(exec);
  377. return BOOL_FALSE; // Something went wrong
  378. }
  379. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  380. // we don't need event loop and can return here.
  381. if (kexec_retcode(exec, retcode)) {
  382. kexec_free(exec);
  383. return BOOL_TRUE;
  384. }
  385. // Save kexec pointer to use later
  386. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  387. ktpd->exec = exec;
  388. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  389. action_stdout_ev, ktpd);
  390. return BOOL_TRUE;
  391. }
  392. #if 0
  393. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  394. {
  395. assert(ktpd);
  396. if (!ktpd)
  397. return;
  398. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  399. }
  400. #endif