ktpd_session.c 24 KB

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