ktpd_session.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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; // Object for data exchange with client (KTP)
  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 ssize_t stdin_out(int fd, faux_buf_t *buf)
  670. {
  671. ssize_t total_written = 0;
  672. assert(buf);
  673. if (!buf)
  674. return -1;
  675. assert(fd >= 0);
  676. while (faux_buf_len(buf) > 0) {
  677. ssize_t data_to_write = 0;
  678. ssize_t bytes_written = 0;
  679. void *data = NULL;
  680. data_to_write = faux_buf_dread_lock_easy(buf, &data);
  681. if (data_to_write <= 0)
  682. return -1;
  683. bytes_written = write(fd, data, data_to_write);
  684. if (bytes_written > 0) {
  685. total_written += bytes_written;
  686. faux_buf_dread_unlock_easy(buf, bytes_written);
  687. } else {
  688. faux_buf_dread_unlock_easy(buf, 0);
  689. }
  690. if (bytes_written < 0) {
  691. if ( // Something went wrong
  692. (errno != EINTR) &&
  693. (errno != EAGAIN) &&
  694. (errno != EWOULDBLOCK)
  695. )
  696. return -1;
  697. // Not whole data block was written
  698. } else if (bytes_written != data_to_write) {
  699. break;
  700. }
  701. }
  702. return total_written;
  703. }
  704. static bool_t action_stdin_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  705. void *associated_data, void *user_data)
  706. {
  707. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  708. faux_buf_t *bufin = NULL;
  709. int fd = -1;
  710. if (!ktpd)
  711. return BOOL_TRUE;
  712. if (!ktpd->exec)
  713. return BOOL_TRUE;
  714. fd = kexec_stdin(ktpd->exec);
  715. if (fd < 0) // Something strange
  716. return BOOL_FALSE;
  717. bufin = kexec_bufin(ktpd->exec);
  718. assert(bufin);
  719. stdin_out(fd, bufin); // Non-blocking write
  720. if (faux_buf_len(bufin) != 0) // Try later
  721. return BOOL_TRUE;
  722. // All data is written
  723. faux_eloop_exclude_fd_event(ktpd->eloop, fd, POLLOUT);
  724. // Happy compiler
  725. eloop = eloop;
  726. type = type;
  727. associated_data = associated_data;
  728. return BOOL_TRUE;
  729. }
  730. static bool_t ktpd_session_process_stdin(ktpd_session_t *ktpd, faux_msg_t *msg)
  731. {
  732. char *line = NULL;
  733. unsigned int len = 0;
  734. faux_buf_t *bufin = NULL;
  735. int fd = -1;
  736. assert(ktpd);
  737. assert(msg);
  738. if (!ktpd->exec)
  739. return BOOL_FALSE;
  740. if (!kexec_interactive(ktpd->exec))
  741. return BOOL_FALSE;
  742. fd = kexec_stdin(ktpd->exec);
  743. if (fd < 0)
  744. return BOOL_FALSE;
  745. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_LINE, (void **)&line, &len))
  746. return BOOL_TRUE; // It's strange but not a bug
  747. if (len == 0)
  748. return BOOL_TRUE;
  749. bufin = kexec_bufin(ktpd->exec);
  750. assert(bufin);
  751. faux_buf_write(bufin, line, len);
  752. stdin_out(fd, bufin); // Non-blocking write
  753. if (faux_buf_len(bufin) == 0)
  754. return BOOL_TRUE;
  755. // Non-blocking write can't write all data so plan to write later
  756. faux_eloop_include_fd_event(ktpd->eloop, fd, POLLOUT);
  757. return BOOL_TRUE;
  758. }
  759. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  760. {
  761. uint16_t cmd = 0;
  762. assert(ktpd);
  763. if (!ktpd)
  764. return BOOL_FALSE;
  765. assert(msg);
  766. if (!msg)
  767. return BOOL_FALSE;
  768. cmd = faux_msg_get_cmd(msg);
  769. switch (cmd) {
  770. case KTP_AUTH:
  771. if ((ktpd->state != KTPD_SESSION_STATE_UNAUTHORIZED) &&
  772. (ktpd->state != KTPD_SESSION_STATE_IDLE))
  773. break;
  774. ktpd_session_process_auth(ktpd, msg);
  775. break;
  776. case KTP_CMD:
  777. if (ktpd->state != KTPD_SESSION_STATE_IDLE)
  778. break;
  779. ktpd_session_process_cmd(ktpd, msg);
  780. break;
  781. case KTP_COMPLETION:
  782. if (ktpd->state != KTPD_SESSION_STATE_IDLE)
  783. break;
  784. ktpd_session_process_completion(ktpd, msg);
  785. break;
  786. case KTP_HELP:
  787. if (ktpd->state != KTPD_SESSION_STATE_IDLE)
  788. break;
  789. ktpd_session_process_help(ktpd, msg);
  790. break;
  791. case KTP_STDIN:
  792. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS)
  793. break;
  794. ktpd_session_process_stdin(ktpd, msg);
  795. break;
  796. default:
  797. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  798. break;
  799. }
  800. return BOOL_TRUE;
  801. }
  802. /** @brief Low-level function to receive KTP message.
  803. *
  804. * Firstly function gets the header of message. Then it checks and parses
  805. * header and find out the length of whole message. Then it receives the rest
  806. * of message.
  807. */
  808. static bool_t ktpd_session_read_cb(faux_async_t *async,
  809. faux_buf_t *buf, size_t len, void *user_data)
  810. {
  811. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  812. faux_msg_t *completed_msg = NULL;
  813. char *data = NULL;
  814. assert(async);
  815. assert(buf);
  816. assert(ktpd);
  817. // Linearize buffer
  818. data = malloc(len);
  819. faux_buf_read(buf, data, len);
  820. // Receive header
  821. if (!ktpd->hdr) {
  822. size_t whole_len = 0;
  823. size_t msg_wo_hdr = 0;
  824. ktpd->hdr = (faux_hdr_t *)data;
  825. // Check for broken header
  826. if (!ktp_check_header(ktpd->hdr)) {
  827. faux_free(ktpd->hdr);
  828. ktpd->hdr = NULL;
  829. return BOOL_FALSE;
  830. }
  831. whole_len = faux_hdr_len(ktpd->hdr);
  832. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  833. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  834. // Plan to receive message body
  835. if (msg_wo_hdr > 0) {
  836. faux_async_set_read_limits(async,
  837. msg_wo_hdr, msg_wo_hdr);
  838. return BOOL_TRUE;
  839. }
  840. // Here message is completed (msg body has zero length)
  841. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  842. // Receive message body
  843. } else {
  844. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  845. faux_free(data);
  846. }
  847. // Plan to receive msg header
  848. faux_async_set_read_limits(ktpd->async,
  849. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  850. faux_free(ktpd->hdr);
  851. ktpd->hdr = NULL; // Ready to recv new header
  852. // Here message is completed
  853. ktpd_session_dispatch(ktpd, completed_msg);
  854. faux_msg_free(completed_msg);
  855. return BOOL_TRUE;
  856. }
  857. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  858. {
  859. assert(ktpd);
  860. if (!ktpd)
  861. return BOOL_FALSE;
  862. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  863. return BOOL_FALSE;
  864. return BOOL_TRUE;
  865. }
  866. int ktpd_session_fd(const ktpd_session_t *ktpd)
  867. {
  868. assert(ktpd);
  869. if (!ktpd)
  870. return BOOL_FALSE;
  871. return faux_async_fd(ktpd->async);
  872. }
  873. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  874. void *associated_data, void *user_data)
  875. {
  876. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  877. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  878. ssize_t r = -1;
  879. faux_buf_t *faux_buf = NULL;
  880. char *buf = NULL;
  881. ssize_t len = 0;
  882. faux_msg_t *ack = NULL;
  883. // Interactive command use these function as callback not only for
  884. // getting stdout but for writing stdin too. Because pseudo-terminal
  885. // uses the same fd for in and out.
  886. if (info->revents & POLLOUT)
  887. return action_stdin_ev(eloop, type, associated_data, user_data);
  888. // Some errors or fd is closed so remove it from polling
  889. if (!(info->revents & POLLIN)) {
  890. faux_eloop_del_fd(eloop, info->fd);
  891. return BOOL_TRUE;
  892. }
  893. if (!ktpd)
  894. return BOOL_TRUE;
  895. if (!ktpd->exec)
  896. return BOOL_TRUE;
  897. faux_buf = kexec_bufout(ktpd->exec);
  898. assert(faux_buf);
  899. do {
  900. void *linear_buf = NULL;
  901. ssize_t really_readed = 0;
  902. ssize_t linear_len =
  903. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  904. // Non-blocked read. The fd became non-blocked while
  905. // kexec_prepare().
  906. r = read(info->fd, linear_buf, linear_len);
  907. if (r > 0)
  908. really_readed = r;
  909. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  910. } while (r > 0);
  911. len = faux_buf_len(faux_buf);
  912. if (0 == len)
  913. return BOOL_TRUE;
  914. buf = malloc(len);
  915. faux_buf_read(faux_buf, buf, len);
  916. // Create KTP_STDOUT message to send to client
  917. ack = ktp_msg_preform(KTP_STDOUT, KTP_STATUS_NONE);
  918. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  919. faux_msg_send_async(ack, ktpd->async);
  920. faux_msg_free(ack);
  921. free(buf);
  922. // Happy compiler
  923. eloop = eloop;
  924. type = type;
  925. return BOOL_TRUE;
  926. }
  927. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  928. void *associated_data, void *user_data)
  929. {
  930. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  931. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  932. ssize_t r = -1;
  933. faux_buf_t *faux_buf = NULL;
  934. char *buf = NULL;
  935. ssize_t len = 0;
  936. faux_msg_t *ack = NULL;
  937. // Some errors or fd is closed so remove it from polling
  938. if (!(info->revents & POLLIN)) {
  939. faux_eloop_del_fd(eloop, info->fd);
  940. return BOOL_TRUE;
  941. }
  942. if (!ktpd)
  943. return BOOL_TRUE;
  944. if (!ktpd->exec)
  945. return BOOL_TRUE;
  946. faux_buf = kexec_buferr(ktpd->exec);
  947. assert(faux_buf);
  948. do {
  949. void *linear_buf = NULL;
  950. ssize_t really_readed = 0;
  951. ssize_t linear_len =
  952. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  953. // Non-blocked read. The fd became non-blocked while
  954. // kexec_prepare().
  955. r = read(info->fd, linear_buf, linear_len);
  956. if (r > 0)
  957. really_readed = r;
  958. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  959. } while (r > 0);
  960. len = faux_buf_len(faux_buf);
  961. if (0 == len)
  962. return BOOL_TRUE;
  963. buf = malloc(len);
  964. faux_buf_read(faux_buf, buf, len);
  965. // Create KTP_STDERR message to send to client
  966. ack = ktp_msg_preform(KTP_STDERR, KTP_STATUS_NONE);
  967. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  968. faux_msg_send_async(ack, ktpd->async);
  969. faux_msg_free(ack);
  970. free(buf);
  971. // Happy compiler
  972. eloop = eloop;
  973. type = type;
  974. return BOOL_TRUE;
  975. }
  976. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  977. void *associated_data, void *user_data)
  978. {
  979. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  980. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  981. faux_async_t *async = ktpd->async;
  982. assert(async);
  983. // Write data
  984. if (info->revents & POLLOUT) {
  985. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  986. if (faux_async_out(async) < 0) {
  987. // Someting went wrong
  988. faux_eloop_del_fd(eloop, info->fd);
  989. syslog(LOG_ERR, "Problem with async output");
  990. return BOOL_FALSE; // Stop event loop
  991. }
  992. }
  993. // Read data
  994. if (info->revents & POLLIN) {
  995. if (faux_async_in(async) < 0) {
  996. // Someting went wrong
  997. faux_eloop_del_fd(eloop, info->fd);
  998. syslog(LOG_ERR, "Problem with async input");
  999. return BOOL_FALSE; // Stop event loop
  1000. }
  1001. }
  1002. // EOF
  1003. if (info->revents & POLLHUP) {
  1004. faux_eloop_del_fd(eloop, info->fd);
  1005. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  1006. return BOOL_FALSE; // Stop event loop
  1007. }
  1008. // POLLERR
  1009. if (info->revents & POLLERR) {
  1010. faux_eloop_del_fd(eloop, info->fd);
  1011. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  1012. return BOOL_FALSE; // Stop event loop
  1013. }
  1014. // POLLNVAL
  1015. if (info->revents & POLLNVAL) {
  1016. faux_eloop_del_fd(eloop, info->fd);
  1017. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  1018. return BOOL_FALSE; // Stop event loop
  1019. }
  1020. type = type; // Happy compiler
  1021. // Session can be really finished here. Note KTPD session can't be
  1022. // stopped immediately so it's only two places within code to really
  1023. // break the loop. This one and within wait_for_action_ev().
  1024. if (ktpd->exit)
  1025. return BOOL_FALSE;
  1026. return BOOL_TRUE;
  1027. }
  1028. #if 0
  1029. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  1030. {
  1031. assert(ktpd);
  1032. if (!ktpd)
  1033. return;
  1034. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  1035. }
  1036. #endif