ktpd_session.c 24 KB

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