ktpd_session.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. bool_t exit;
  40. };
  41. // Static declarations
  42. static bool_t ktpd_session_read_cb(faux_async_t *async,
  43. faux_buf_t *buf, size_t len, void *user_data);
  44. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  45. void *associated_data, void *user_data);
  46. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  47. void *associated_data, void *user_data);
  48. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  49. int *retcode, faux_error_t *error, bool_t dry_run);
  50. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  51. void *associated_data, void *user_data);
  52. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  53. void *associated_data, void *user_data);
  54. ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
  55. const char *start_entry, faux_eloop_t *eloop)
  56. {
  57. ktpd_session_t *ktpd = NULL;
  58. if (sock < 0)
  59. return NULL;
  60. if (!eloop)
  61. return NULL;
  62. ktpd = faux_zmalloc(sizeof(*ktpd));
  63. assert(ktpd);
  64. if (!ktpd)
  65. return NULL;
  66. // Init
  67. ktpd->state = KTPD_SESSION_STATE_IDLE;
  68. ktpd->eloop = eloop;
  69. ktpd->session = ksession_new(scheme, start_entry);
  70. assert(ktpd->session);
  71. ktpd->exec = NULL;
  72. // Exit flag. It differs from ksession done flag because KTPD session
  73. // can't exit immediately. It must finish current command processing
  74. // before really stop the event loop. Note: User defined plugin
  75. // function must use ksession done flag. This exit flag is internal
  76. // feature of KTPD session.
  77. ktpd->exit = BOOL_FALSE;
  78. // Async object
  79. ktpd->async = faux_async_new(sock);
  80. assert(ktpd->async);
  81. // Receive message header first
  82. faux_async_set_read_limits(ktpd->async,
  83. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  84. faux_async_set_read_cb(ktpd->async, ktpd_session_read_cb, ktpd);
  85. ktpd->hdr = NULL;
  86. faux_async_set_stall_cb(ktpd->async, ktp_stall_cb, ktpd->eloop);
  87. // Eloop callbacks
  88. faux_eloop_add_fd(ktpd->eloop, ktpd_session_fd(ktpd), POLLIN,
  89. client_ev, ktpd);
  90. faux_eloop_add_signal(ktpd->eloop, SIGCHLD, wait_for_actions_ev, ktpd);
  91. return ktpd;
  92. }
  93. void ktpd_session_free(ktpd_session_t *ktpd)
  94. {
  95. if (!ktpd)
  96. return;
  97. kexec_free(ktpd->exec);
  98. ksession_free(ktpd->session);
  99. faux_free(ktpd->hdr);
  100. close(ktpd_session_fd(ktpd));
  101. faux_async_free(ktpd->async);
  102. faux_free(ktpd);
  103. }
  104. static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
  105. {
  106. char *line = NULL;
  107. int retcode = -1;
  108. ktp_cmd_e cmd = KTP_CMD_ACK;
  109. faux_error_t *error = NULL;
  110. bool_t rc = BOOL_FALSE;
  111. bool_t dry_run = BOOL_FALSE;
  112. uint32_t status = KTP_STATUS_NONE;
  113. bool_t ret = BOOL_TRUE;
  114. assert(ktpd);
  115. assert(msg);
  116. // Get line from message
  117. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  118. ktp_send_error(ktpd->async, cmd, "The line is not specified");
  119. return BOOL_FALSE;
  120. }
  121. // Get dry-run flag from message
  122. if (KTP_STATUS_IS_DRY_RUN(faux_msg_get_status(msg)))
  123. dry_run = BOOL_TRUE;
  124. error = faux_error_new();
  125. ktpd->exec = NULL;
  126. rc = ktpd_session_exec(ktpd, line, &retcode, error, dry_run);
  127. faux_str_free(line);
  128. // Command is scheduled. Eloop will wait for ACTION completion.
  129. // So inform client about it and about command features like
  130. // interactive/non-interactive.
  131. if (ktpd->exec) {
  132. faux_msg_t *ack = NULL;
  133. ktp_status_e status = KTP_STATUS_INCOMPLETED;
  134. ack = ktp_msg_preform(cmd, status);
  135. faux_msg_send_async(ack, ktpd->async);
  136. faux_msg_free(ack);
  137. faux_error_free(error);
  138. return BOOL_TRUE; // Continue and wait for ACTION
  139. }
  140. // Here we don't need to wait for the action. We have retcode already.
  141. if (ksession_done(ktpd->session)) {
  142. ktpd->exit = BOOL_TRUE;
  143. status |= KTP_STATUS_EXIT;
  144. }
  145. if (rc) {
  146. uint8_t retcode8bit = 0;
  147. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  148. retcode8bit = (uint8_t)(retcode & 0xff);
  149. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  150. faux_msg_send_async(ack, ktpd->async);
  151. faux_msg_free(ack);
  152. } else {
  153. char *err = faux_error_cstr(error);
  154. ktp_send_error(ktpd->async, cmd, err);
  155. faux_str_free(err);
  156. ret = BOOL_FALSE;
  157. }
  158. faux_error_free(error);
  159. return ret;
  160. }
  161. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  162. int *retcode, faux_error_t *error, bool_t dry_run)
  163. {
  164. kexec_t *exec = NULL;
  165. assert(ktpd);
  166. if (!ktpd)
  167. return BOOL_FALSE;
  168. // Parsing
  169. exec = ksession_parse_for_exec(ktpd->session, line, error);
  170. if (!exec)
  171. return BOOL_FALSE;
  172. // Set dry-run flag
  173. kexec_set_dry_run(exec, dry_run);
  174. // Session status can be changed while parsing
  175. // NOTE: kexec_t is atomic now
  176. // if (ksession_done(ktpd->session)) {
  177. // kexec_free(exec);
  178. // return BOOL_FALSE; // Because action is not completed
  179. // }
  180. // Execute kexec and then wait for completion using global Eloop
  181. if (!kexec_exec(exec)) {
  182. kexec_free(exec);
  183. return BOOL_FALSE; // Something went wrong
  184. }
  185. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  186. // we don't need event loop and can return here.
  187. if (kexec_retcode(exec, retcode)) {
  188. kexec_free(exec);
  189. return BOOL_TRUE;
  190. }
  191. // Save kexec pointer to use later
  192. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  193. ktpd->exec = exec;
  194. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  195. action_stdout_ev, ktpd);
  196. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  197. action_stderr_ev, ktpd);
  198. return BOOL_TRUE;
  199. }
  200. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  201. void *associated_data, void *user_data)
  202. {
  203. int wstatus = 0;
  204. pid_t child_pid = -1;
  205. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  206. int retcode = -1;
  207. uint8_t retcode8bit = 0;
  208. faux_msg_t *ack = NULL;
  209. ktp_cmd_e cmd = KTP_CMD_ACK;
  210. uint32_t status = KTP_STATUS_NONE;
  211. if (!ktpd)
  212. return BOOL_FALSE;
  213. // Wait for any child process. Doesn't block.
  214. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  215. if (ktpd->exec)
  216. kexec_continue_command_execution(ktpd->exec, child_pid,
  217. wstatus);
  218. }
  219. if (!ktpd->exec)
  220. return BOOL_TRUE;
  221. // Check if kexec is done now
  222. if (!kexec_retcode(ktpd->exec, &retcode))
  223. return BOOL_TRUE; // Continue
  224. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  225. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  226. kexec_free(ktpd->exec);
  227. ktpd->exec = NULL;
  228. ktpd->state = KTPD_SESSION_STATE_IDLE;
  229. // All kexec_t actions are done so can break the loop if needed.
  230. if (ksession_done(ktpd->session)) {
  231. ktpd->exit = BOOL_TRUE;
  232. status |= KTP_STATUS_EXIT; // Notify client about exiting
  233. }
  234. // Send ACK message
  235. ack = ktp_msg_preform(cmd, status);
  236. retcode8bit = (uint8_t)(retcode & 0xff);
  237. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  238. faux_msg_send_async(ack, ktpd->async);
  239. faux_msg_free(ack);
  240. type = type; // Happy compiler
  241. associated_data = associated_data; // Happy compiler
  242. if (ktpd->exit)
  243. return BOOL_FALSE;
  244. return BOOL_TRUE;
  245. }
  246. static int compl_compare(const void *first, const void *second)
  247. {
  248. const char *f = (const char *)first;
  249. const char *s = (const char *)second;
  250. return strcmp(f, s);
  251. }
  252. static int compl_kcompare(const void *key, const void *list_item)
  253. {
  254. const char *f = (const char *)key;
  255. const char *s = (const char *)list_item;
  256. return strcmp(f, s);
  257. }
  258. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  259. {
  260. char *line = NULL;
  261. faux_msg_t *ack = NULL;
  262. kpargv_t *pargv = NULL;
  263. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  264. uint32_t status = KTP_STATUS_NONE;
  265. const char *prefix = NULL;
  266. size_t prefix_len = 0;
  267. assert(ktpd);
  268. assert(msg);
  269. // Get line from message
  270. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  271. ktp_send_error(ktpd->async, cmd, NULL);
  272. return BOOL_FALSE;
  273. }
  274. // Parsing
  275. pargv = ksession_parse_for_completion(ktpd->session, line);
  276. faux_str_free(line);
  277. if (!pargv) {
  278. ktp_send_error(ktpd->async, cmd, NULL);
  279. return BOOL_FALSE;
  280. }
  281. kpargv_debug(pargv);
  282. if (ksession_done(ktpd->session)) {
  283. ktpd->exit = BOOL_TRUE;
  284. status |= KTP_STATUS_EXIT; // Notify client about exiting
  285. }
  286. // Prepare ACK message
  287. ack = ktp_msg_preform(cmd, status);
  288. // Last unfinished word. Common prefix for all completions
  289. prefix = kpargv_last_arg(pargv);
  290. if (!faux_str_is_empty(prefix)) {
  291. prefix_len = strlen(prefix);
  292. faux_msg_add_param(ack, KTP_PARAM_PREFIX, prefix, prefix_len);
  293. }
  294. // Fill msg with possible completions
  295. if (!kpargv_completions_is_empty(pargv)) {
  296. const kentry_t *candidate = NULL;
  297. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  298. faux_list_node_t *compl_iter = NULL;
  299. faux_list_t *completions = NULL;
  300. char *compl_str = NULL;
  301. completions = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  302. compl_compare, compl_kcompare,
  303. (void (*)(void *))faux_str_free);
  304. while ((candidate = kpargv_completions_each(&citer))) {
  305. const kentry_t *completion = NULL;
  306. kparg_t *parg = NULL;
  307. int rc = -1;
  308. char *out = NULL;
  309. bool_t res = BOOL_FALSE;
  310. char *l = NULL; // One line of completion
  311. const char *str = NULL;
  312. // Get completion entry from candidate entry
  313. completion = kentry_nested_by_purpose(candidate,
  314. KENTRY_PURPOSE_COMPLETION);
  315. // If candidate entry doesn't contain completion then try
  316. // to get completion from entry's PTYPE
  317. if (!completion) {
  318. const kentry_t *ptype = NULL;
  319. ptype = kentry_nested_by_purpose(candidate,
  320. KENTRY_PURPOSE_PTYPE);
  321. if (!ptype)
  322. continue;
  323. completion = kentry_nested_by_purpose(ptype,
  324. KENTRY_PURPOSE_COMPLETION);
  325. }
  326. if (!completion)
  327. continue;
  328. parg = kparg_new(candidate, prefix);
  329. kpargv_set_candidate_parg(pargv, parg);
  330. res = ksession_exec_locally(ktpd->session, completion,
  331. pargv, &rc, &out);
  332. kparg_free(parg);
  333. if (!res || (rc < 0) || !out) {
  334. if (out)
  335. faux_str_free(out);
  336. continue;
  337. }
  338. // Get all completions one by one
  339. str = out;
  340. while ((l = faux_str_getline(str, &str))) {
  341. // Compare prefix
  342. if ((prefix_len > 0) &&
  343. (faux_str_cmpn(prefix, l, prefix_len) != 0)) {
  344. faux_str_free(l);
  345. continue;
  346. }
  347. compl_str = l + prefix_len;
  348. faux_list_add(completions, faux_str_dup(compl_str));
  349. faux_str_free(l);
  350. }
  351. faux_str_free(out);
  352. }
  353. // Put completion list to message
  354. compl_iter = faux_list_head(completions);
  355. while ((compl_str = faux_list_each(&compl_iter))) {
  356. faux_msg_add_param(ack, KTP_PARAM_LINE,
  357. compl_str, strlen(compl_str));
  358. }
  359. faux_list_free(completions);
  360. }
  361. faux_msg_send_async(ack, ktpd->async);
  362. faux_msg_free(ack);
  363. kpargv_free(pargv);
  364. return BOOL_TRUE;
  365. }
  366. // The most priority source of help is candidate's help ACTION output. Next
  367. // source is candidate's PTYPE help ACTION output.
  368. // Function generates two lines for one resulting help line. The first
  369. // component is a 'prefix' and the second component is 'text'.
  370. // The 'prefix' can be something like 'ip', 'filter' i.e.
  371. // subcommand or '3..89', '<STRING>' i.e. description of type. The 'text'
  372. // field is description of current parameter. For example 'Interface IP
  373. // address'. So the full help can be:
  374. // AAA.BBB.CCC.DDD Interface IP address
  375. // [ first field ] [ second field ]
  376. //
  377. // If not candidate parameter nor PTYPE contains the help functions the engine
  378. // tries to construct help itself.
  379. //
  380. // It uses the following sources for 'prefix':
  381. // * 'help' field of PTYPE
  382. // * 'value' field of PTYPE
  383. // * 'name' field of PTYPE
  384. // * 'value' field of parameter
  385. // * 'name' field of parameter
  386. //
  387. // Engine uses the following sources for 'text':
  388. // * 'help' field of parameter
  389. // * 'value' field of parameter
  390. // * 'name' field of parameter
  391. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  392. {
  393. char *line = NULL;
  394. faux_msg_t *ack = NULL;
  395. kpargv_t *pargv = NULL;
  396. ktp_cmd_e cmd = KTP_HELP_ACK;
  397. uint32_t status = KTP_STATUS_NONE;
  398. const char *prefix = NULL;
  399. assert(ktpd);
  400. assert(msg);
  401. // Get line from message
  402. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  403. ktp_send_error(ktpd->async, cmd, NULL);
  404. return BOOL_FALSE;
  405. }
  406. // Parsing
  407. pargv = ksession_parse_for_completion(ktpd->session, line);
  408. faux_str_free(line);
  409. if (!pargv) {
  410. ktp_send_error(ktpd->async, cmd, NULL);
  411. return BOOL_FALSE;
  412. }
  413. if (ksession_done(ktpd->session)) {
  414. ktpd->exit = BOOL_TRUE;
  415. status |= KTP_STATUS_EXIT; // Notify client about exiting
  416. }
  417. // Prepare ACK message
  418. ack = ktp_msg_preform(cmd, status);
  419. // Last unfinished word. Common prefix for all entries
  420. prefix = kpargv_last_arg(pargv);
  421. // Fill msg with possible completions
  422. if (!kpargv_completions_is_empty(pargv)) {
  423. const kentry_t *candidate = NULL;
  424. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  425. faux_list_node_t *help_iter = NULL;
  426. faux_list_t *help_list = NULL;
  427. help_t *help_struct = NULL;
  428. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  429. help_compare, help_kcompare, help_free);
  430. while ((candidate = kpargv_completions_each(&citer))) {
  431. const kentry_t *help = NULL;
  432. const kentry_t *ptype = NULL;
  433. bool_t help_added = BOOL_FALSE;
  434. // Get PTYPE of parameter
  435. ptype = kentry_nested_by_purpose(candidate,
  436. KENTRY_PURPOSE_PTYPE);
  437. // Try to get help fn from parameter itself
  438. help = kentry_nested_by_purpose(candidate,
  439. KENTRY_PURPOSE_HELP);
  440. if (!help && ptype)
  441. help = kentry_nested_by_purpose(ptype,
  442. KENTRY_PURPOSE_HELP);
  443. // Generate help with found ACTION
  444. if (help) {
  445. char *out = NULL;
  446. const char *str = NULL;
  447. kparg_t *parg = NULL;
  448. int rc = -1;
  449. char *prefix_str = NULL;
  450. char *line_str = NULL;
  451. parg = kparg_new(candidate, prefix);
  452. kpargv_set_candidate_parg(pargv, parg);
  453. ksession_exec_locally(ktpd->session,
  454. help, pargv, &rc, &out);
  455. kparg_free(parg);
  456. str = out;
  457. do {
  458. prefix_str = faux_str_getline(str, &str);
  459. if (!prefix_str)
  460. break;
  461. line_str = faux_str_getline(str, &str);
  462. if (!line_str) {
  463. faux_str_free(prefix_str);
  464. break;
  465. }
  466. help_struct = help_new(prefix_str, line_str);
  467. faux_list_add(help_list, help_struct);
  468. help_added = BOOL_TRUE;
  469. } while (line_str);
  470. faux_str_free(out);
  471. }
  472. // Generate help with available information
  473. if (!help_added) {
  474. const char *prefix_str = NULL;
  475. const char *line_str = NULL;
  476. // Prefix_str
  477. if (ptype) {
  478. prefix_str = kentry_help(ptype);
  479. if (!prefix_str)
  480. prefix_str = kentry_value(ptype);
  481. if (!prefix_str)
  482. prefix_str = kentry_name(ptype);
  483. } else {
  484. prefix_str = kentry_value(candidate);
  485. if (!prefix_str)
  486. prefix_str = kentry_name(candidate);
  487. }
  488. assert(prefix_str);
  489. // Line_str
  490. line_str = kentry_help(candidate);
  491. if (!line_str)
  492. line_str = kentry_value(candidate);
  493. if (!line_str)
  494. line_str = kentry_name(candidate);
  495. assert(line_str);
  496. help_struct = help_new(
  497. faux_str_dup(prefix_str),
  498. faux_str_dup(line_str));
  499. faux_list_add(help_list, help_struct);
  500. help_added = BOOL_TRUE;
  501. }
  502. }
  503. // Put help list to message
  504. help_iter = faux_list_head(help_list);
  505. while ((help_struct = (help_t *)faux_list_each(&help_iter))) {
  506. faux_msg_add_param(ack, KTP_PARAM_PREFIX,
  507. help_struct->prefix, strlen(help_struct->prefix));
  508. faux_msg_add_param(ack, KTP_PARAM_LINE,
  509. help_struct->line, strlen(help_struct->line));
  510. }
  511. faux_list_free(help_list);
  512. }
  513. faux_msg_send_async(ack, ktpd->async);
  514. faux_msg_free(ack);
  515. kpargv_free(pargv);
  516. return BOOL_TRUE;
  517. }
  518. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  519. {
  520. uint16_t cmd = 0;
  521. assert(ktpd);
  522. if (!ktpd)
  523. return BOOL_FALSE;
  524. assert(msg);
  525. if (!msg)
  526. return BOOL_FALSE;
  527. cmd = faux_msg_get_cmd(msg);
  528. switch (cmd) {
  529. case KTP_CMD:
  530. ktpd_session_process_cmd(ktpd, msg);
  531. break;
  532. case KTP_COMPLETION:
  533. ktpd_session_process_completion(ktpd, msg);
  534. break;
  535. case KTP_HELP:
  536. ktpd_session_process_help(ktpd, msg);
  537. break;
  538. default:
  539. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  540. break;
  541. }
  542. return BOOL_TRUE;
  543. }
  544. /** @brief Low-level function to receive KTP message.
  545. *
  546. * Firstly function gets the header of message. Then it checks and parses
  547. * header and find out the length of whole message. Then it receives the rest
  548. * of message.
  549. */
  550. static bool_t ktpd_session_read_cb(faux_async_t *async,
  551. faux_buf_t *buf, size_t len, void *user_data)
  552. {
  553. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  554. faux_msg_t *completed_msg = NULL;
  555. char *data = NULL;
  556. assert(async);
  557. assert(buf);
  558. assert(ktpd);
  559. // Linearize buffer
  560. data = malloc(len);
  561. faux_buf_read(buf, data, len);
  562. // Receive header
  563. if (!ktpd->hdr) {
  564. size_t whole_len = 0;
  565. size_t msg_wo_hdr = 0;
  566. ktpd->hdr = (faux_hdr_t *)data;
  567. // Check for broken header
  568. if (!ktp_check_header(ktpd->hdr)) {
  569. faux_free(ktpd->hdr);
  570. ktpd->hdr = NULL;
  571. return BOOL_FALSE;
  572. }
  573. whole_len = faux_hdr_len(ktpd->hdr);
  574. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  575. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  576. // Plan to receive message body
  577. if (msg_wo_hdr > 0) {
  578. faux_async_set_read_limits(async,
  579. msg_wo_hdr, msg_wo_hdr);
  580. return BOOL_TRUE;
  581. }
  582. // Here message is completed (msg body has zero length)
  583. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  584. // Receive message body
  585. } else {
  586. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  587. faux_free(data);
  588. }
  589. // Plan to receive msg header
  590. faux_async_set_read_limits(ktpd->async,
  591. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  592. faux_free(ktpd->hdr);
  593. ktpd->hdr = NULL; // Ready to recv new header
  594. // Here message is completed
  595. ktpd_session_dispatch(ktpd, completed_msg);
  596. faux_msg_free(completed_msg);
  597. return BOOL_TRUE;
  598. }
  599. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  600. {
  601. assert(ktpd);
  602. if (!ktpd)
  603. return BOOL_FALSE;
  604. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  605. return BOOL_FALSE;
  606. return BOOL_TRUE;
  607. }
  608. int ktpd_session_fd(const ktpd_session_t *ktpd)
  609. {
  610. assert(ktpd);
  611. if (!ktpd)
  612. return BOOL_FALSE;
  613. return faux_async_fd(ktpd->async);
  614. }
  615. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  616. void *associated_data, void *user_data)
  617. {
  618. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  619. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  620. ssize_t r = -1;
  621. faux_buf_t *faux_buf = NULL;
  622. char *buf = NULL;
  623. ssize_t len = 0;
  624. faux_msg_t *ack = NULL;
  625. // Some errors or fd is closed so remove it from polling
  626. if (!(info->revents & POLLIN)) {
  627. faux_eloop_del_fd(eloop, info->fd);
  628. return BOOL_TRUE;
  629. }
  630. if (!ktpd)
  631. return BOOL_TRUE;
  632. if (!ktpd->exec)
  633. return BOOL_TRUE;
  634. faux_buf = kexec_bufout(ktpd->exec);
  635. assert(faux_buf);
  636. do {
  637. void *linear_buf = NULL;
  638. ssize_t really_readed = 0;
  639. ssize_t linear_len =
  640. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  641. // Non-blocked read. The fd became non-blocked while
  642. // kexec_prepare().
  643. r = read(info->fd, linear_buf, linear_len);
  644. if (r > 0)
  645. really_readed = r;
  646. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  647. } while (r > 0);
  648. len = faux_buf_len(faux_buf);
  649. if (0 == len)
  650. return BOOL_TRUE;
  651. buf = malloc(len);
  652. faux_buf_read(faux_buf, buf, len);
  653. // Create KTP_STDOUT message to send to client
  654. ack = ktp_msg_preform(KTP_STDOUT, KTP_STATUS_NONE);
  655. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  656. faux_msg_send_async(ack, ktpd->async);
  657. faux_msg_free(ack);
  658. free(buf);
  659. // Happy compiler
  660. eloop = eloop;
  661. type = type;
  662. return BOOL_TRUE;
  663. }
  664. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  665. void *associated_data, void *user_data)
  666. {
  667. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  668. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  669. ssize_t r = -1;
  670. faux_buf_t *faux_buf = NULL;
  671. char *buf = NULL;
  672. ssize_t len = 0;
  673. faux_msg_t *ack = NULL;
  674. // Some errors or fd is closed so remove it from polling
  675. if (!(info->revents & POLLIN)) {
  676. faux_eloop_del_fd(eloop, info->fd);
  677. return BOOL_TRUE;
  678. }
  679. if (!ktpd)
  680. return BOOL_TRUE;
  681. if (!ktpd->exec)
  682. return BOOL_TRUE;
  683. faux_buf = kexec_buferr(ktpd->exec);
  684. assert(faux_buf);
  685. do {
  686. void *linear_buf = NULL;
  687. ssize_t really_readed = 0;
  688. ssize_t linear_len =
  689. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  690. // Non-blocked read. The fd became non-blocked while
  691. // kexec_prepare().
  692. r = read(info->fd, linear_buf, linear_len);
  693. if (r > 0)
  694. really_readed = r;
  695. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  696. } while (r > 0);
  697. len = faux_buf_len(faux_buf);
  698. if (0 == len)
  699. return BOOL_TRUE;
  700. buf = malloc(len);
  701. faux_buf_read(faux_buf, buf, len);
  702. // Create KTP_STDERR message to send to client
  703. ack = ktp_msg_preform(KTP_STDERR, KTP_STATUS_NONE);
  704. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  705. faux_msg_send_async(ack, ktpd->async);
  706. faux_msg_free(ack);
  707. free(buf);
  708. // Happy compiler
  709. eloop = eloop;
  710. type = type;
  711. return BOOL_TRUE;
  712. }
  713. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  714. void *associated_data, void *user_data)
  715. {
  716. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  717. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  718. faux_async_t *async = ktpd->async;
  719. assert(async);
  720. // Write data
  721. if (info->revents & POLLOUT) {
  722. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  723. if (faux_async_out(async) < 0) {
  724. // Someting went wrong
  725. faux_eloop_del_fd(eloop, info->fd);
  726. syslog(LOG_ERR, "Problem with async output");
  727. return BOOL_FALSE; // Stop event loop
  728. }
  729. }
  730. // Read data
  731. if (info->revents & POLLIN) {
  732. if (faux_async_in(async) < 0) {
  733. // Someting went wrong
  734. faux_eloop_del_fd(eloop, info->fd);
  735. syslog(LOG_ERR, "Problem with async input");
  736. return BOOL_FALSE; // Stop event loop
  737. }
  738. }
  739. // EOF
  740. if (info->revents & POLLHUP) {
  741. faux_eloop_del_fd(eloop, info->fd);
  742. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  743. return BOOL_FALSE; // Stop event loop
  744. }
  745. // POLLERR
  746. if (info->revents & POLLERR) {
  747. faux_eloop_del_fd(eloop, info->fd);
  748. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  749. return BOOL_FALSE; // Stop event loop
  750. }
  751. // POLLNVAL
  752. if (info->revents & POLLNVAL) {
  753. faux_eloop_del_fd(eloop, info->fd);
  754. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  755. return BOOL_FALSE; // Stop event loop
  756. }
  757. type = type; // Happy compiler
  758. // Session can be really finished here. Note KTPD session can't be
  759. // stopped immediately so it's only two places within code to really
  760. // break the loop. This one and within wait_for_action_ev().
  761. if (ktpd->exit)
  762. return BOOL_FALSE;
  763. return BOOL_TRUE;
  764. }
  765. #if 0
  766. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  767. {
  768. assert(ktpd);
  769. if (!ktpd)
  770. return;
  771. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  772. }
  773. #endif