ktpd_session.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <faux/str.h>
  15. #include <faux/async.h>
  16. #include <faux/msg.h>
  17. #include <faux/eloop.h>
  18. #include <klish/ksession.h>
  19. #include <klish/ktp.h>
  20. #include <klish/ktp_session.h>
  21. typedef enum {
  22. KTPD_SESSION_STATE_DISCONNECTED = 'd',
  23. KTPD_SESSION_STATE_NOT_AUTHORIZED = 'a',
  24. KTPD_SESSION_STATE_IDLE = 'i',
  25. KTPD_SESSION_STATE_WAIT_FOR_PROCESS = 'p',
  26. } ktpd_session_state_e;
  27. struct ktpd_session_s {
  28. ksession_t *ksession;
  29. ktpd_session_state_e state;
  30. uid_t uid;
  31. gid_t gid;
  32. char *user;
  33. faux_async_t *async;
  34. faux_hdr_t *hdr; // Engine will receive header and then msg
  35. faux_eloop_t *eloop;
  36. };
  37. // Static declarations
  38. static bool_t ktpd_session_read_cb(faux_async_t *async,
  39. void *data, size_t len, void *user_data);
  40. static bool_t ktpd_session_stall_cb(faux_async_t *async,
  41. size_t len, void *user_data);
  42. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  43. void *associated_data, void *user_data);
  44. ktpd_session_t *ktpd_session_new(int sock, const kscheme_t *scheme,
  45. const char *start_entry, faux_eloop_t *eloop)
  46. {
  47. ktpd_session_t *session = NULL;
  48. if (sock < 0)
  49. return NULL;
  50. if (!eloop)
  51. return NULL;
  52. session = faux_zmalloc(sizeof(*session));
  53. assert(session);
  54. if (!session)
  55. return NULL;
  56. // Init
  57. session->state = KTPD_SESSION_STATE_NOT_AUTHORIZED;
  58. session->eloop = eloop;
  59. session->ksession = ksession_new(scheme, start_entry);
  60. assert(session->ksession);
  61. // Async object
  62. session->async = faux_async_new(sock);
  63. assert(session->async);
  64. // Receive message header first
  65. faux_async_set_read_limits(session->async,
  66. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  67. faux_async_set_read_cb(session->async, ktpd_session_read_cb, session);
  68. session->hdr = NULL;
  69. faux_async_set_stall_cb(session->async, ktpd_session_stall_cb, session);
  70. // Eloop callbacks
  71. faux_eloop_add_fd(session->eloop, ktpd_session_fd(session), POLLIN,
  72. client_ev, session);
  73. return session;
  74. }
  75. void ktpd_session_free(ktpd_session_t *session)
  76. {
  77. if (!session)
  78. return;
  79. ksession_free(session->ksession);
  80. faux_free(session->hdr);
  81. close(ktpd_session_fd(session));
  82. faux_async_free(session->async);
  83. faux_free(session);
  84. }
  85. static bool_t check_ktp_header(faux_hdr_t *hdr)
  86. {
  87. assert(hdr);
  88. if (!hdr)
  89. return BOOL_FALSE;
  90. if (faux_hdr_magic(hdr) != KTP_MAGIC)
  91. return BOOL_FALSE;
  92. if (faux_hdr_major(hdr) != KTP_MAJOR)
  93. return BOOL_FALSE;
  94. if (faux_hdr_minor(hdr) != KTP_MINOR)
  95. return BOOL_FALSE;
  96. if (faux_hdr_len(hdr) < (int)sizeof(*hdr))
  97. return BOOL_FALSE;
  98. return BOOL_TRUE;
  99. }
  100. static bool_t ktpd_session_send_error(ktpd_session_t *session,
  101. ktp_cmd_e cmd, const char *error)
  102. {
  103. faux_msg_t *msg = NULL;
  104. assert(session);
  105. if (!session)
  106. return BOOL_FALSE;
  107. msg = ktp_msg_preform(cmd, KTP_STATUS_ERROR);
  108. if (error)
  109. faux_msg_add_param(msg, KTP_PARAM_ERROR, error, strlen(error));
  110. faux_msg_send_async(msg, session->async);
  111. faux_msg_free(msg);
  112. return BOOL_TRUE;
  113. }
  114. static bool_t ktpd_session_process_cmd(ktpd_session_t *session, faux_msg_t *msg)
  115. {
  116. char *line = NULL;
  117. faux_msg_t *ack = NULL;
  118. // kpargv_t *pargv = NULL;
  119. ktp_cmd_e cmd = KTP_CMD_ACK;
  120. kexec_t *exec = NULL;
  121. faux_error_t *error = NULL;
  122. assert(session);
  123. assert(msg);
  124. // Get line from message
  125. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  126. ktpd_session_send_error(session, cmd,
  127. "The line is not specified");
  128. return BOOL_FALSE;
  129. }
  130. // Parsing
  131. /* error = faux_error_new();
  132. exec = ksession_parse_for_exec(session->ksession, line, error);
  133. faux_str_free(line);
  134. if (exec) {
  135. kexec_contexts_node_t *iter = kexec_contexts_iter(exec);
  136. kcontext_t *context = NULL;
  137. while ((context = kexec_contexts_each(&iter))) {
  138. kpargv_debug(kcontext_pargv(context));
  139. }
  140. } else {
  141. char *err = faux_error_cstr(error);
  142. ktpd_session_send_error(session, cmd, err);
  143. faux_str_free(err);
  144. return BOOL_FALSE;
  145. }
  146. kexec_exec(exec);
  147. */
  148. {
  149. int retcode = 0;
  150. bool_t r = BOOL_FALSE;
  151. r = ksession_exec_locally(session->ksession, line, &retcode, error);
  152. faux_str_free(line);
  153. if (!r)
  154. printf("ksession_exec_locally() return value is false\n");
  155. printf("kexec retcode is %d\n", retcode);
  156. }
  157. // ktpd_session_exec(session, exec);
  158. // kpargv_debug(pargv);
  159. // if (kpargv_status(pargv) != KPARSE_OK) {
  160. // char *error = NULL;
  161. // error = faux_str_sprintf("Can't parse line: %s",
  162. // kpargv_status_str(pargv));
  163. // kpargv_free(pargv);
  164. // ktpd_session_send_error(session, cmd, error);
  165. // return BOOL_FALSE;
  166. // }
  167. //
  168. // kpargv_free(pargv);
  169. kexec_free(exec);
  170. faux_error_free(error);
  171. // Send ACK message
  172. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  173. faux_msg_send_async(ack, session->async);
  174. faux_msg_free(ack);
  175. return BOOL_TRUE;
  176. }
  177. static bool_t ktpd_session_process_completion(ktpd_session_t *session, faux_msg_t *msg)
  178. {
  179. char *line = NULL;
  180. faux_msg_t *ack = NULL;
  181. kpargv_t *pargv = NULL;
  182. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  183. assert(session);
  184. assert(msg);
  185. // Get line from message
  186. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  187. ktpd_session_send_error(session, cmd, NULL);
  188. return BOOL_FALSE;
  189. }
  190. // Parsing
  191. pargv = ksession_parse_for_completion(session->ksession, line);
  192. faux_str_free(line);
  193. if (!pargv) {
  194. ktpd_session_send_error(session, cmd, NULL);
  195. return BOOL_FALSE;
  196. }
  197. kpargv_debug(pargv);
  198. kpargv_free(pargv);
  199. // Send ACK message
  200. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  201. faux_msg_send_async(ack, session->async);
  202. faux_msg_free(ack);
  203. return BOOL_TRUE;
  204. }
  205. static bool_t ktpd_session_process_help(ktpd_session_t *session, faux_msg_t *msg)
  206. {
  207. char *line = NULL;
  208. faux_msg_t *ack = NULL;
  209. // kpargv_t *pargv = NULL;
  210. ktp_cmd_e cmd = KTP_HELP_ACK;
  211. assert(session);
  212. assert(msg);
  213. // Get line from message
  214. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  215. ktpd_session_send_error(session, cmd, NULL);
  216. return BOOL_FALSE;
  217. }
  218. /* // Parsing
  219. pargv = ksession_parse_line(session->ksession, line, KPURPOSE_HELP);
  220. faux_str_free(line);
  221. kpargv_free(pargv);
  222. */
  223. // Send ACK message
  224. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  225. faux_msg_send_async(ack, session->async);
  226. faux_msg_free(ack);
  227. return BOOL_TRUE;
  228. }
  229. static bool_t ktpd_session_dispatch(ktpd_session_t *session, faux_msg_t *msg)
  230. {
  231. uint16_t cmd = 0;
  232. assert(session);
  233. if (!session)
  234. return BOOL_FALSE;
  235. assert(msg);
  236. if (!msg)
  237. return BOOL_FALSE;
  238. cmd = faux_msg_get_cmd(msg);
  239. switch (cmd) {
  240. case KTP_CMD:
  241. ktpd_session_process_cmd(session, msg);
  242. break;
  243. case KTP_COMPLETION:
  244. ktpd_session_process_completion(session, msg);
  245. break;
  246. case KTP_HELP:
  247. ktpd_session_process_help(session, msg);
  248. break;
  249. default:
  250. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  251. break;
  252. }
  253. return BOOL_TRUE;
  254. }
  255. /** @brief Low-level function to receive KTP message.
  256. *
  257. * Firstly function gets the header of message. Then it checks and parses
  258. * header and find out the length of whole message. Then it receives the rest
  259. * of message.
  260. */
  261. static bool_t ktpd_session_read_cb(faux_async_t *async,
  262. void *data, size_t len, void *user_data)
  263. {
  264. ktpd_session_t *session = (ktpd_session_t *)user_data;
  265. faux_msg_t *completed_msg = NULL;
  266. assert(async);
  267. assert(data);
  268. assert(session);
  269. // Receive header
  270. if (!session->hdr) {
  271. size_t whole_len = 0;
  272. size_t msg_wo_hdr = 0;
  273. session->hdr = (faux_hdr_t *)data;
  274. // Check for broken header
  275. if (!check_ktp_header(session->hdr)) {
  276. faux_free(session->hdr);
  277. session->hdr = NULL;
  278. return BOOL_FALSE;
  279. }
  280. whole_len = faux_hdr_len(session->hdr);
  281. // msg_wo_hdr >= 0 because check_ktp_header() validates whole_len
  282. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  283. // Plan to receive message body
  284. if (msg_wo_hdr > 0) {
  285. faux_async_set_read_limits(async,
  286. msg_wo_hdr, msg_wo_hdr);
  287. return BOOL_TRUE;
  288. }
  289. // Here message is completed (msg body has zero length)
  290. completed_msg = faux_msg_deserialize_parts(session->hdr, NULL, 0);
  291. // Receive message body
  292. } else {
  293. completed_msg = faux_msg_deserialize_parts(session->hdr, data, len);
  294. faux_free(data);
  295. }
  296. // Plan to receive msg header
  297. faux_async_set_read_limits(session->async,
  298. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  299. faux_free(session->hdr);
  300. session->hdr = NULL; // Ready to recv new header
  301. // Here message is completed
  302. ktpd_session_dispatch(session, completed_msg);
  303. faux_msg_free(completed_msg);
  304. return BOOL_TRUE;
  305. }
  306. static bool_t ktpd_session_stall_cb(faux_async_t *async,
  307. size_t len, void *user_data)
  308. {
  309. ktpd_session_t *session = (ktpd_session_t *)user_data;
  310. assert(async);
  311. assert(session);
  312. assert(session->eloop);
  313. faux_eloop_include_fd_event(session->eloop, ktpd_session_fd(session), POLLOUT);
  314. async = async; // Happy compiler
  315. len = len; // Happy compiler
  316. return BOOL_TRUE;
  317. }
  318. bool_t ktpd_session_connected(ktpd_session_t *session)
  319. {
  320. assert(session);
  321. if (!session)
  322. return BOOL_FALSE;
  323. if (KTPD_SESSION_STATE_DISCONNECTED == session->state)
  324. return BOOL_FALSE;
  325. return BOOL_TRUE;
  326. }
  327. int ktpd_session_fd(const ktpd_session_t *session)
  328. {
  329. assert(session);
  330. if (!session)
  331. return BOOL_FALSE;
  332. return faux_async_fd(session->async);
  333. }
  334. bool_t ktpd_session_async_in(ktpd_session_t *session)
  335. {
  336. assert(session);
  337. if (!session)
  338. return BOOL_FALSE;
  339. if (!ktpd_session_connected(session))
  340. return BOOL_FALSE;
  341. if (faux_async_in(session->async) < 0)
  342. return BOOL_FALSE;
  343. return BOOL_TRUE;
  344. }
  345. bool_t ktpd_session_async_out(ktpd_session_t *session)
  346. {
  347. assert(session);
  348. if (!session)
  349. return BOOL_FALSE;
  350. if (!ktpd_session_connected(session))
  351. return BOOL_FALSE;
  352. if (faux_async_out(session->async) < 0)
  353. return BOOL_FALSE;
  354. return BOOL_TRUE;
  355. }
  356. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  357. void *associated_data, void *user_data)
  358. {
  359. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  360. ktpd_session_t *ktpd_session = (ktpd_session_t *)user_data;
  361. assert(ktpd_session);
  362. // Write data
  363. if (info->revents & POLLOUT) {
  364. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  365. if (!ktpd_session_async_out(ktpd_session)) {
  366. // Someting went wrong
  367. faux_eloop_del_fd(eloop, info->fd);
  368. syslog(LOG_ERR, "Problem with async output");
  369. return BOOL_FALSE; // Stop event loop
  370. }
  371. }
  372. // Read data
  373. if (info->revents & POLLIN) {
  374. if (!ktpd_session_async_in(ktpd_session)) {
  375. // Someting went wrong
  376. faux_eloop_del_fd(eloop, info->fd);
  377. syslog(LOG_ERR, "Problem with async input");
  378. return BOOL_FALSE; // Stop event loop
  379. }
  380. }
  381. // EOF
  382. if (info->revents & POLLHUP) {
  383. faux_eloop_del_fd(eloop, info->fd);
  384. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  385. return BOOL_FALSE; // Stop event loop
  386. }
  387. type = type; // Happy compiler
  388. return BOOL_TRUE;
  389. }
  390. bool_t ktpd_session_terminated_action(ktpd_session_t *session,
  391. pid_t pid, int wstatus)
  392. {
  393. assert(session);
  394. if (!session)
  395. return BOOL_FALSE;
  396. syslog(LOG_ERR, "ACTION process %d was terminated: %d",
  397. pid, WEXITSTATUS(wstatus));
  398. return BOOL_TRUE;
  399. }
  400. #if 0
  401. static void ktpd_session_bad_socket(ktpd_session_t *session)
  402. {
  403. assert(session);
  404. if (!session)
  405. return;
  406. session->state = KTPD_SESSION_STATE_DISCONNECTED;
  407. }
  408. #endif