ktpd_session.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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(key);
  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. ack = ktp_msg_preform(cmd, status);
  255. faux_msg_send_async(ack, ktpd->async);
  256. faux_msg_free(ack);
  257. faux_error_free(error);
  258. return BOOL_TRUE; // Continue and wait for ACTION
  259. }
  260. // Here we don't need to wait for the action. We have retcode already.
  261. if (ksession_done(ktpd->session)) {
  262. ktpd->exit = BOOL_TRUE;
  263. status |= KTP_STATUS_EXIT;
  264. }
  265. // Prepare ACK message
  266. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  267. if (rc) {
  268. uint8_t retcode8bit = 0;
  269. retcode8bit = (uint8_t)(retcode & 0xff);
  270. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  271. } else {
  272. faux_msg_set_status(ack, KTP_STATUS_ERROR);
  273. char *err = faux_error_cstr(error);
  274. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  275. faux_str_free(err);
  276. ret = BOOL_FALSE;
  277. }
  278. // Generate prompt
  279. prompt = generate_prompt(ktpd);
  280. if (prompt) {
  281. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  282. faux_str_free(prompt);
  283. }
  284. // Add hotkeys
  285. if (view_was_changed)
  286. add_hotkeys_to_msg(ktpd, ack);
  287. faux_msg_send_async(ack, ktpd->async);
  288. faux_msg_free(ack);
  289. faux_error_free(error);
  290. return ret;
  291. }
  292. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  293. int *retcode, faux_error_t *error,
  294. bool_t dry_run, bool_t *view_was_changed_p)
  295. {
  296. kexec_t *exec = NULL;
  297. assert(ktpd);
  298. if (!ktpd)
  299. return BOOL_FALSE;
  300. // Parsing
  301. exec = ksession_parse_for_exec(ktpd->session, line, error);
  302. if (!exec)
  303. return BOOL_FALSE;
  304. // Set dry-run flag
  305. kexec_set_dry_run(exec, dry_run);
  306. // Session status can be changed while parsing
  307. // NOTE: kexec_t is atomic now
  308. // if (ksession_done(ktpd->session)) {
  309. // kexec_free(exec);
  310. // return BOOL_FALSE; // Because action is not completed
  311. // }
  312. // Execute kexec and then wait for completion using global Eloop
  313. if (!kexec_exec(exec)) {
  314. kexec_free(exec);
  315. return BOOL_FALSE; // Something went wrong
  316. }
  317. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  318. // we don't need event loop and can return here.
  319. if (kexec_retcode(exec, retcode)) {
  320. if (view_was_changed_p)
  321. *view_was_changed_p = !kpath_is_equal(
  322. ksession_path(ktpd->session),
  323. kexec_saved_path(exec));
  324. kexec_free(exec);
  325. return BOOL_TRUE;
  326. }
  327. // Save kexec pointer to use later
  328. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  329. ktpd->exec = exec;
  330. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  331. action_stdout_ev, ktpd);
  332. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  333. action_stderr_ev, ktpd);
  334. return BOOL_TRUE;
  335. }
  336. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  337. void *associated_data, void *user_data)
  338. {
  339. int wstatus = 0;
  340. pid_t child_pid = -1;
  341. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  342. int retcode = -1;
  343. uint8_t retcode8bit = 0;
  344. faux_msg_t *ack = NULL;
  345. ktp_cmd_e cmd = KTP_CMD_ACK;
  346. uint32_t status = KTP_STATUS_NONE;
  347. char *prompt = NULL;
  348. bool_t view_was_changed = BOOL_FALSE;
  349. if (!ktpd)
  350. return BOOL_FALSE;
  351. // Wait for any child process. Doesn't block.
  352. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  353. if (ktpd->exec)
  354. kexec_continue_command_execution(ktpd->exec, child_pid,
  355. wstatus);
  356. }
  357. if (!ktpd->exec)
  358. return BOOL_TRUE;
  359. // Check if kexec is done now
  360. if (!kexec_retcode(ktpd->exec, &retcode))
  361. return BOOL_TRUE; // Continue
  362. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  363. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  364. view_was_changed = !kpath_is_equal(
  365. ksession_path(ktpd->session), kexec_saved_path(ktpd->exec));
  366. kexec_free(ktpd->exec);
  367. ktpd->exec = NULL;
  368. ktpd->state = KTPD_SESSION_STATE_IDLE;
  369. // All kexec_t actions are done so can break the loop if needed.
  370. if (ksession_done(ktpd->session)) {
  371. ktpd->exit = BOOL_TRUE;
  372. status |= KTP_STATUS_EXIT; // Notify client about exiting
  373. }
  374. // Send ACK message
  375. ack = ktp_msg_preform(cmd, status);
  376. retcode8bit = (uint8_t)(retcode & 0xff);
  377. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  378. // Generate prompt
  379. prompt = generate_prompt(ktpd);
  380. if (prompt) {
  381. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  382. faux_str_free(prompt);
  383. }
  384. // Add hotkeys
  385. if (view_was_changed)
  386. add_hotkeys_to_msg(ktpd, ack);
  387. faux_msg_send_async(ack, ktpd->async);
  388. faux_msg_free(ack);
  389. type = type; // Happy compiler
  390. associated_data = associated_data; // Happy compiler
  391. if (ktpd->exit)
  392. return BOOL_FALSE;
  393. return BOOL_TRUE;
  394. }
  395. static int compl_compare(const void *first, const void *second)
  396. {
  397. const char *f = (const char *)first;
  398. const char *s = (const char *)second;
  399. return strcmp(f, s);
  400. }
  401. static int compl_kcompare(const void *key, const void *list_item)
  402. {
  403. const char *f = (const char *)key;
  404. const char *s = (const char *)list_item;
  405. return strcmp(f, s);
  406. }
  407. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  408. {
  409. char *line = NULL;
  410. faux_msg_t *ack = NULL;
  411. kpargv_t *pargv = NULL;
  412. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  413. uint32_t status = KTP_STATUS_NONE;
  414. const char *prefix = NULL;
  415. size_t prefix_len = 0;
  416. assert(ktpd);
  417. assert(msg);
  418. // Get line from message
  419. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  420. ktp_send_error(ktpd->async, cmd, NULL);
  421. return BOOL_FALSE;
  422. }
  423. // Parsing
  424. pargv = ksession_parse_for_completion(ktpd->session, line);
  425. faux_str_free(line);
  426. if (!pargv) {
  427. ktp_send_error(ktpd->async, cmd, NULL);
  428. return BOOL_FALSE;
  429. }
  430. kpargv_debug(pargv);
  431. if (ksession_done(ktpd->session)) {
  432. ktpd->exit = BOOL_TRUE;
  433. status |= KTP_STATUS_EXIT; // Notify client about exiting
  434. }
  435. // Prepare ACK message
  436. ack = ktp_msg_preform(cmd, status);
  437. // Last unfinished word. Common prefix for all completions
  438. prefix = kpargv_last_arg(pargv);
  439. if (!faux_str_is_empty(prefix)) {
  440. prefix_len = strlen(prefix);
  441. faux_msg_add_param(ack, KTP_PARAM_PREFIX, prefix, prefix_len);
  442. }
  443. // Fill msg with possible completions
  444. if (!kpargv_completions_is_empty(pargv)) {
  445. const kentry_t *candidate = NULL;
  446. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  447. faux_list_node_t *compl_iter = NULL;
  448. faux_list_t *completions = NULL;
  449. char *compl_str = NULL;
  450. completions = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  451. compl_compare, compl_kcompare,
  452. (void (*)(void *))faux_str_free);
  453. while ((candidate = kpargv_completions_each(&citer))) {
  454. const kentry_t *completion = NULL;
  455. kparg_t *parg = NULL;
  456. int rc = -1;
  457. char *out = NULL;
  458. bool_t res = BOOL_FALSE;
  459. char *l = NULL; // One line of completion
  460. const char *str = NULL;
  461. // Get completion entry from candidate entry
  462. completion = kentry_nested_by_purpose(candidate,
  463. KENTRY_PURPOSE_COMPLETION);
  464. // If candidate entry doesn't contain completion then try
  465. // to get completion from entry's PTYPE
  466. if (!completion) {
  467. const kentry_t *ptype = NULL;
  468. ptype = kentry_nested_by_purpose(candidate,
  469. KENTRY_PURPOSE_PTYPE);
  470. if (!ptype)
  471. continue;
  472. completion = kentry_nested_by_purpose(ptype,
  473. KENTRY_PURPOSE_COMPLETION);
  474. }
  475. if (!completion)
  476. continue;
  477. parg = kparg_new(candidate, prefix);
  478. kpargv_set_candidate_parg(pargv, parg);
  479. res = ksession_exec_locally(ktpd->session, completion,
  480. pargv, &rc, &out);
  481. kparg_free(parg);
  482. if (!res || (rc < 0) || !out) {
  483. if (out)
  484. faux_str_free(out);
  485. continue;
  486. }
  487. // Get all completions one by one
  488. str = out;
  489. while ((l = faux_str_getline(str, &str))) {
  490. // Compare prefix
  491. if ((prefix_len > 0) &&
  492. (faux_str_cmpn(prefix, l, prefix_len) != 0)) {
  493. faux_str_free(l);
  494. continue;
  495. }
  496. compl_str = l + prefix_len;
  497. faux_list_add(completions, faux_str_dup(compl_str));
  498. faux_str_free(l);
  499. }
  500. faux_str_free(out);
  501. }
  502. // Put completion list to message
  503. compl_iter = faux_list_head(completions);
  504. while ((compl_str = faux_list_each(&compl_iter))) {
  505. faux_msg_add_param(ack, KTP_PARAM_LINE,
  506. compl_str, strlen(compl_str));
  507. }
  508. faux_list_free(completions);
  509. }
  510. faux_msg_send_async(ack, ktpd->async);
  511. faux_msg_free(ack);
  512. kpargv_free(pargv);
  513. return BOOL_TRUE;
  514. }
  515. // The most priority source of help is candidate's help ACTION output. Next
  516. // source is candidate's PTYPE help ACTION output.
  517. // Function generates two lines for one resulting help line. The first
  518. // component is a 'prefix' and the second component is 'text'.
  519. // The 'prefix' can be something like 'ip', 'filter' i.e.
  520. // subcommand or '3..89', '<STRING>' i.e. description of type. The 'text'
  521. // field is description of current parameter. For example 'Interface IP
  522. // address'. So the full help can be:
  523. // AAA.BBB.CCC.DDD Interface IP address
  524. // [ first field ] [ second field ]
  525. //
  526. // If not candidate parameter nor PTYPE contains the help functions the engine
  527. // tries to construct help itself.
  528. //
  529. // It uses the following sources for 'prefix':
  530. // * 'help' field of PTYPE
  531. // * 'value' field of PTYPE
  532. // * 'name' field of PTYPE
  533. // * 'value' field of parameter
  534. // * 'name' field of parameter
  535. //
  536. // Engine uses the following sources for 'text':
  537. // * 'help' field of parameter
  538. // * 'value' field of parameter
  539. // * 'name' field of parameter
  540. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  541. {
  542. char *line = NULL;
  543. faux_msg_t *ack = NULL;
  544. kpargv_t *pargv = NULL;
  545. ktp_cmd_e cmd = KTP_HELP_ACK;
  546. uint32_t status = KTP_STATUS_NONE;
  547. const char *prefix = NULL;
  548. assert(ktpd);
  549. assert(msg);
  550. // Get line from message
  551. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  552. ktp_send_error(ktpd->async, cmd, NULL);
  553. return BOOL_FALSE;
  554. }
  555. // Parsing
  556. pargv = ksession_parse_for_completion(ktpd->session, line);
  557. faux_str_free(line);
  558. if (!pargv) {
  559. ktp_send_error(ktpd->async, cmd, NULL);
  560. return BOOL_FALSE;
  561. }
  562. if (ksession_done(ktpd->session)) {
  563. ktpd->exit = BOOL_TRUE;
  564. status |= KTP_STATUS_EXIT; // Notify client about exiting
  565. }
  566. // Prepare ACK message
  567. ack = ktp_msg_preform(cmd, status);
  568. // Last unfinished word. Common prefix for all entries
  569. prefix = kpargv_last_arg(pargv);
  570. // Fill msg with possible completions
  571. if (!kpargv_completions_is_empty(pargv)) {
  572. const kentry_t *candidate = NULL;
  573. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  574. faux_list_node_t *help_iter = NULL;
  575. faux_list_t *help_list = NULL;
  576. help_t *help_struct = NULL;
  577. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  578. help_compare, help_kcompare, help_free);
  579. while ((candidate = kpargv_completions_each(&citer))) {
  580. const kentry_t *help = NULL;
  581. const kentry_t *ptype = NULL;
  582. bool_t help_added = BOOL_FALSE;
  583. // Get PTYPE of parameter
  584. ptype = kentry_nested_by_purpose(candidate,
  585. KENTRY_PURPOSE_PTYPE);
  586. // Try to get help fn from parameter itself
  587. help = kentry_nested_by_purpose(candidate,
  588. KENTRY_PURPOSE_HELP);
  589. if (!help && ptype)
  590. help = kentry_nested_by_purpose(ptype,
  591. KENTRY_PURPOSE_HELP);
  592. // Generate help with found ACTION
  593. if (help) {
  594. char *out = NULL;
  595. const char *str = NULL;
  596. kparg_t *parg = NULL;
  597. int rc = -1;
  598. char *prefix_str = NULL;
  599. char *line_str = NULL;
  600. parg = kparg_new(candidate, prefix);
  601. kpargv_set_candidate_parg(pargv, parg);
  602. ksession_exec_locally(ktpd->session,
  603. help, pargv, &rc, &out);
  604. kparg_free(parg);
  605. str = out;
  606. do {
  607. prefix_str = faux_str_getline(str, &str);
  608. if (!prefix_str)
  609. break;
  610. line_str = faux_str_getline(str, &str);
  611. if (!line_str) {
  612. faux_str_free(prefix_str);
  613. break;
  614. }
  615. help_struct = help_new(prefix_str, line_str);
  616. faux_list_add(help_list, help_struct);
  617. help_added = BOOL_TRUE;
  618. } while (line_str);
  619. faux_str_free(out);
  620. }
  621. // Generate help with available information
  622. if (!help_added) {
  623. const char *prefix_str = NULL;
  624. const char *line_str = NULL;
  625. // Prefix_str
  626. if (ptype) {
  627. prefix_str = kentry_help(ptype);
  628. if (!prefix_str)
  629. prefix_str = kentry_value(ptype);
  630. if (!prefix_str)
  631. prefix_str = kentry_name(ptype);
  632. } else {
  633. prefix_str = kentry_value(candidate);
  634. if (!prefix_str)
  635. prefix_str = kentry_name(candidate);
  636. }
  637. assert(prefix_str);
  638. // Line_str
  639. line_str = kentry_help(candidate);
  640. if (!line_str)
  641. line_str = kentry_value(candidate);
  642. if (!line_str)
  643. line_str = kentry_name(candidate);
  644. assert(line_str);
  645. help_struct = help_new(
  646. faux_str_dup(prefix_str),
  647. faux_str_dup(line_str));
  648. faux_list_add(help_list, help_struct);
  649. help_added = BOOL_TRUE;
  650. }
  651. }
  652. // Put help list to message
  653. help_iter = faux_list_head(help_list);
  654. while ((help_struct = (help_t *)faux_list_each(&help_iter))) {
  655. faux_msg_add_param(ack, KTP_PARAM_PREFIX,
  656. help_struct->prefix, strlen(help_struct->prefix));
  657. faux_msg_add_param(ack, KTP_PARAM_LINE,
  658. help_struct->line, strlen(help_struct->line));
  659. }
  660. faux_list_free(help_list);
  661. }
  662. faux_msg_send_async(ack, ktpd->async);
  663. faux_msg_free(ack);
  664. kpargv_free(pargv);
  665. return BOOL_TRUE;
  666. }
  667. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  668. {
  669. uint16_t cmd = 0;
  670. assert(ktpd);
  671. if (!ktpd)
  672. return BOOL_FALSE;
  673. assert(msg);
  674. if (!msg)
  675. return BOOL_FALSE;
  676. cmd = faux_msg_get_cmd(msg);
  677. switch (cmd) {
  678. case KTP_AUTH:
  679. ktpd_session_process_auth(ktpd, msg);
  680. break;
  681. case KTP_CMD:
  682. ktpd_session_process_cmd(ktpd, msg);
  683. break;
  684. case KTP_COMPLETION:
  685. ktpd_session_process_completion(ktpd, msg);
  686. break;
  687. case KTP_HELP:
  688. ktpd_session_process_help(ktpd, msg);
  689. break;
  690. default:
  691. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  692. break;
  693. }
  694. return BOOL_TRUE;
  695. }
  696. /** @brief Low-level function to receive KTP message.
  697. *
  698. * Firstly function gets the header of message. Then it checks and parses
  699. * header and find out the length of whole message. Then it receives the rest
  700. * of message.
  701. */
  702. static bool_t ktpd_session_read_cb(faux_async_t *async,
  703. faux_buf_t *buf, size_t len, void *user_data)
  704. {
  705. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  706. faux_msg_t *completed_msg = NULL;
  707. char *data = NULL;
  708. assert(async);
  709. assert(buf);
  710. assert(ktpd);
  711. // Linearize buffer
  712. data = malloc(len);
  713. faux_buf_read(buf, data, len);
  714. // Receive header
  715. if (!ktpd->hdr) {
  716. size_t whole_len = 0;
  717. size_t msg_wo_hdr = 0;
  718. ktpd->hdr = (faux_hdr_t *)data;
  719. // Check for broken header
  720. if (!ktp_check_header(ktpd->hdr)) {
  721. faux_free(ktpd->hdr);
  722. ktpd->hdr = NULL;
  723. return BOOL_FALSE;
  724. }
  725. whole_len = faux_hdr_len(ktpd->hdr);
  726. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  727. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  728. // Plan to receive message body
  729. if (msg_wo_hdr > 0) {
  730. faux_async_set_read_limits(async,
  731. msg_wo_hdr, msg_wo_hdr);
  732. return BOOL_TRUE;
  733. }
  734. // Here message is completed (msg body has zero length)
  735. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  736. // Receive message body
  737. } else {
  738. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  739. faux_free(data);
  740. }
  741. // Plan to receive msg header
  742. faux_async_set_read_limits(ktpd->async,
  743. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  744. faux_free(ktpd->hdr);
  745. ktpd->hdr = NULL; // Ready to recv new header
  746. // Here message is completed
  747. ktpd_session_dispatch(ktpd, completed_msg);
  748. faux_msg_free(completed_msg);
  749. return BOOL_TRUE;
  750. }
  751. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  752. {
  753. assert(ktpd);
  754. if (!ktpd)
  755. return BOOL_FALSE;
  756. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  757. return BOOL_FALSE;
  758. return BOOL_TRUE;
  759. }
  760. int ktpd_session_fd(const ktpd_session_t *ktpd)
  761. {
  762. assert(ktpd);
  763. if (!ktpd)
  764. return BOOL_FALSE;
  765. return faux_async_fd(ktpd->async);
  766. }
  767. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  768. void *associated_data, void *user_data)
  769. {
  770. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  771. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  772. ssize_t r = -1;
  773. faux_buf_t *faux_buf = NULL;
  774. char *buf = NULL;
  775. ssize_t len = 0;
  776. faux_msg_t *ack = NULL;
  777. // Some errors or fd is closed so remove it from polling
  778. if (!(info->revents & POLLIN)) {
  779. faux_eloop_del_fd(eloop, info->fd);
  780. return BOOL_TRUE;
  781. }
  782. if (!ktpd)
  783. return BOOL_TRUE;
  784. if (!ktpd->exec)
  785. return BOOL_TRUE;
  786. faux_buf = kexec_bufout(ktpd->exec);
  787. assert(faux_buf);
  788. do {
  789. void *linear_buf = NULL;
  790. ssize_t really_readed = 0;
  791. ssize_t linear_len =
  792. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  793. // Non-blocked read. The fd became non-blocked while
  794. // kexec_prepare().
  795. r = read(info->fd, linear_buf, linear_len);
  796. if (r > 0)
  797. really_readed = r;
  798. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  799. } while (r > 0);
  800. len = faux_buf_len(faux_buf);
  801. if (0 == len)
  802. return BOOL_TRUE;
  803. buf = malloc(len);
  804. faux_buf_read(faux_buf, buf, len);
  805. // Create KTP_STDOUT message to send to client
  806. ack = ktp_msg_preform(KTP_STDOUT, KTP_STATUS_NONE);
  807. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  808. faux_msg_send_async(ack, ktpd->async);
  809. faux_msg_free(ack);
  810. free(buf);
  811. // Happy compiler
  812. eloop = eloop;
  813. type = type;
  814. return BOOL_TRUE;
  815. }
  816. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  817. void *associated_data, void *user_data)
  818. {
  819. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  820. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  821. ssize_t r = -1;
  822. faux_buf_t *faux_buf = NULL;
  823. char *buf = NULL;
  824. ssize_t len = 0;
  825. faux_msg_t *ack = NULL;
  826. // Some errors or fd is closed so remove it from polling
  827. if (!(info->revents & POLLIN)) {
  828. faux_eloop_del_fd(eloop, info->fd);
  829. return BOOL_TRUE;
  830. }
  831. if (!ktpd)
  832. return BOOL_TRUE;
  833. if (!ktpd->exec)
  834. return BOOL_TRUE;
  835. faux_buf = kexec_buferr(ktpd->exec);
  836. assert(faux_buf);
  837. do {
  838. void *linear_buf = NULL;
  839. ssize_t really_readed = 0;
  840. ssize_t linear_len =
  841. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  842. // Non-blocked read. The fd became non-blocked while
  843. // kexec_prepare().
  844. r = read(info->fd, linear_buf, linear_len);
  845. if (r > 0)
  846. really_readed = r;
  847. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  848. } while (r > 0);
  849. len = faux_buf_len(faux_buf);
  850. if (0 == len)
  851. return BOOL_TRUE;
  852. buf = malloc(len);
  853. faux_buf_read(faux_buf, buf, len);
  854. // Create KTP_STDERR message to send to client
  855. ack = ktp_msg_preform(KTP_STDERR, KTP_STATUS_NONE);
  856. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  857. faux_msg_send_async(ack, ktpd->async);
  858. faux_msg_free(ack);
  859. free(buf);
  860. // Happy compiler
  861. eloop = eloop;
  862. type = type;
  863. return BOOL_TRUE;
  864. }
  865. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  866. void *associated_data, void *user_data)
  867. {
  868. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  869. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  870. faux_async_t *async = ktpd->async;
  871. assert(async);
  872. // Write data
  873. if (info->revents & POLLOUT) {
  874. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  875. if (faux_async_out(async) < 0) {
  876. // Someting went wrong
  877. faux_eloop_del_fd(eloop, info->fd);
  878. syslog(LOG_ERR, "Problem with async output");
  879. return BOOL_FALSE; // Stop event loop
  880. }
  881. }
  882. // Read data
  883. if (info->revents & POLLIN) {
  884. if (faux_async_in(async) < 0) {
  885. // Someting went wrong
  886. faux_eloop_del_fd(eloop, info->fd);
  887. syslog(LOG_ERR, "Problem with async input");
  888. return BOOL_FALSE; // Stop event loop
  889. }
  890. }
  891. // EOF
  892. if (info->revents & POLLHUP) {
  893. faux_eloop_del_fd(eloop, info->fd);
  894. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  895. return BOOL_FALSE; // Stop event loop
  896. }
  897. // POLLERR
  898. if (info->revents & POLLERR) {
  899. faux_eloop_del_fd(eloop, info->fd);
  900. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  901. return BOOL_FALSE; // Stop event loop
  902. }
  903. // POLLNVAL
  904. if (info->revents & POLLNVAL) {
  905. faux_eloop_del_fd(eloop, info->fd);
  906. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  907. return BOOL_FALSE; // Stop event loop
  908. }
  909. type = type; // Happy compiler
  910. // Session can be really finished here. Note KTPD session can't be
  911. // stopped immediately so it's only two places within code to really
  912. // break the loop. This one and within wait_for_action_ev().
  913. if (ktpd->exit)
  914. return BOOL_FALSE;
  915. return BOOL_TRUE;
  916. }
  917. #if 0
  918. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  919. {
  920. assert(ktpd);
  921. if (!ktpd)
  922. return;
  923. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  924. }
  925. #endif