ktpd_session.c 28 KB

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