ktpd_session.c 25 KB

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