ktpd_session.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. // ktpd_session_exec(session, exec);
  148. // kpargv_debug(pargv);
  149. // if (kpargv_status(pargv) != KPARSE_OK) {
  150. // char *error = NULL;
  151. // error = faux_str_sprintf("Can't parse line: %s",
  152. // kpargv_status_str(pargv));
  153. // kpargv_free(pargv);
  154. // ktpd_session_send_error(session, cmd, error);
  155. // return BOOL_FALSE;
  156. // }
  157. //
  158. // kpargv_free(pargv);
  159. kexec_free(exec);
  160. faux_error_free(error);
  161. // Send ACK message
  162. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  163. faux_msg_send_async(ack, session->async);
  164. faux_msg_free(ack);
  165. return BOOL_TRUE;
  166. }
  167. static bool_t ktpd_session_process_completion(ktpd_session_t *session, faux_msg_t *msg)
  168. {
  169. char *line = NULL;
  170. faux_msg_t *ack = NULL;
  171. kpargv_t *pargv = NULL;
  172. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  173. assert(session);
  174. assert(msg);
  175. // Get line from message
  176. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  177. ktpd_session_send_error(session, cmd, NULL);
  178. return BOOL_FALSE;
  179. }
  180. // Parsing
  181. pargv = ksession_parse_for_completion(session->ksession, line);
  182. faux_str_free(line);
  183. if (!pargv) {
  184. ktpd_session_send_error(session, cmd, NULL);
  185. return BOOL_FALSE;
  186. }
  187. kpargv_debug(pargv);
  188. kpargv_free(pargv);
  189. // Send ACK message
  190. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  191. faux_msg_send_async(ack, session->async);
  192. faux_msg_free(ack);
  193. return BOOL_TRUE;
  194. }
  195. static bool_t ktpd_session_process_help(ktpd_session_t *session, faux_msg_t *msg)
  196. {
  197. char *line = NULL;
  198. faux_msg_t *ack = NULL;
  199. // kpargv_t *pargv = NULL;
  200. ktp_cmd_e cmd = KTP_HELP_ACK;
  201. assert(session);
  202. assert(msg);
  203. // Get line from message
  204. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  205. ktpd_session_send_error(session, cmd, NULL);
  206. return BOOL_FALSE;
  207. }
  208. /* // Parsing
  209. pargv = ksession_parse_line(session->ksession, line, KPURPOSE_HELP);
  210. faux_str_free(line);
  211. kpargv_free(pargv);
  212. */
  213. // Send ACK message
  214. ack = ktp_msg_preform(cmd, KTP_STATUS_NONE);
  215. faux_msg_send_async(ack, session->async);
  216. faux_msg_free(ack);
  217. return BOOL_TRUE;
  218. }
  219. static bool_t ktpd_session_dispatch(ktpd_session_t *session, faux_msg_t *msg)
  220. {
  221. uint16_t cmd = 0;
  222. assert(session);
  223. if (!session)
  224. return BOOL_FALSE;
  225. assert(msg);
  226. if (!msg)
  227. return BOOL_FALSE;
  228. cmd = faux_msg_get_cmd(msg);
  229. switch (cmd) {
  230. case KTP_CMD:
  231. ktpd_session_process_cmd(session, msg);
  232. break;
  233. case KTP_COMPLETION:
  234. ktpd_session_process_completion(session, msg);
  235. break;
  236. case KTP_HELP:
  237. ktpd_session_process_help(session, msg);
  238. break;
  239. default:
  240. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  241. break;
  242. }
  243. return BOOL_TRUE;
  244. }
  245. /** @brief Low-level function to receive KTP message.
  246. *
  247. * Firstly function gets the header of message. Then it checks and parses
  248. * header and find out the length of whole message. Then it receives the rest
  249. * of message.
  250. */
  251. static bool_t ktpd_session_read_cb(faux_async_t *async,
  252. void *data, size_t len, void *user_data)
  253. {
  254. ktpd_session_t *session = (ktpd_session_t *)user_data;
  255. faux_msg_t *completed_msg = NULL;
  256. assert(async);
  257. assert(data);
  258. assert(session);
  259. // Receive header
  260. if (!session->hdr) {
  261. size_t whole_len = 0;
  262. size_t msg_wo_hdr = 0;
  263. session->hdr = (faux_hdr_t *)data;
  264. // Check for broken header
  265. if (!check_ktp_header(session->hdr)) {
  266. faux_free(session->hdr);
  267. session->hdr = NULL;
  268. return BOOL_FALSE;
  269. }
  270. whole_len = faux_hdr_len(session->hdr);
  271. // msg_wo_hdr >= 0 because check_ktp_header() validates whole_len
  272. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  273. // Plan to receive message body
  274. if (msg_wo_hdr > 0) {
  275. faux_async_set_read_limits(async,
  276. msg_wo_hdr, msg_wo_hdr);
  277. return BOOL_TRUE;
  278. }
  279. // Here message is completed (msg body has zero length)
  280. completed_msg = faux_msg_deserialize_parts(session->hdr, NULL, 0);
  281. // Receive message body
  282. } else {
  283. completed_msg = faux_msg_deserialize_parts(session->hdr, data, len);
  284. faux_free(data);
  285. }
  286. // Plan to receive msg header
  287. faux_async_set_read_limits(session->async,
  288. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  289. faux_free(session->hdr);
  290. session->hdr = NULL; // Ready to recv new header
  291. // Here message is completed
  292. ktpd_session_dispatch(session, completed_msg);
  293. faux_msg_free(completed_msg);
  294. return BOOL_TRUE;
  295. }
  296. static bool_t ktpd_session_stall_cb(faux_async_t *async,
  297. size_t len, void *user_data)
  298. {
  299. ktpd_session_t *session = (ktpd_session_t *)user_data;
  300. assert(async);
  301. assert(session);
  302. assert(session->eloop);
  303. faux_eloop_include_fd_event(session->eloop, ktpd_session_fd(session), POLLOUT);
  304. async = async; // Happy compiler
  305. len = len; // Happy compiler
  306. return BOOL_TRUE;
  307. }
  308. bool_t ktpd_session_connected(ktpd_session_t *session)
  309. {
  310. assert(session);
  311. if (!session)
  312. return BOOL_FALSE;
  313. if (KTPD_SESSION_STATE_DISCONNECTED == session->state)
  314. return BOOL_FALSE;
  315. return BOOL_TRUE;
  316. }
  317. int ktpd_session_fd(const ktpd_session_t *session)
  318. {
  319. assert(session);
  320. if (!session)
  321. return BOOL_FALSE;
  322. return faux_async_fd(session->async);
  323. }
  324. bool_t ktpd_session_async_in(ktpd_session_t *session)
  325. {
  326. assert(session);
  327. if (!session)
  328. return BOOL_FALSE;
  329. if (!ktpd_session_connected(session))
  330. return BOOL_FALSE;
  331. if (faux_async_in(session->async) < 0)
  332. return BOOL_FALSE;
  333. return BOOL_TRUE;
  334. }
  335. bool_t ktpd_session_async_out(ktpd_session_t *session)
  336. {
  337. assert(session);
  338. if (!session)
  339. return BOOL_FALSE;
  340. if (!ktpd_session_connected(session))
  341. return BOOL_FALSE;
  342. if (faux_async_out(session->async) < 0)
  343. return BOOL_FALSE;
  344. return BOOL_TRUE;
  345. }
  346. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  347. void *associated_data, void *user_data)
  348. {
  349. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  350. ktpd_session_t *ktpd_session = (ktpd_session_t *)user_data;
  351. assert(ktpd_session);
  352. // Write data
  353. if (info->revents & POLLOUT) {
  354. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  355. if (!ktpd_session_async_out(ktpd_session)) {
  356. // Someting went wrong
  357. faux_eloop_del_fd(eloop, info->fd);
  358. syslog(LOG_ERR, "Problem with async output");
  359. return BOOL_FALSE; // Stop event loop
  360. }
  361. }
  362. // Read data
  363. if (info->revents & POLLIN) {
  364. if (!ktpd_session_async_in(ktpd_session)) {
  365. // Someting went wrong
  366. faux_eloop_del_fd(eloop, info->fd);
  367. syslog(LOG_ERR, "Problem with async input");
  368. return BOOL_FALSE; // Stop event loop
  369. }
  370. }
  371. // EOF
  372. if (info->revents & POLLHUP) {
  373. faux_eloop_del_fd(eloop, info->fd);
  374. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  375. return BOOL_FALSE; // Stop event loop
  376. }
  377. type = type; // Happy compiler
  378. return BOOL_TRUE;
  379. }
  380. bool_t ktpd_session_terminated_action(ktpd_session_t *session,
  381. pid_t pid, int wstatus)
  382. {
  383. assert(session);
  384. if (!session)
  385. return BOOL_FALSE;
  386. syslog(LOG_ERR, "ACTION process %d was terminated: %d",
  387. pid, WEXITSTATUS(wstatus));
  388. return BOOL_TRUE;
  389. }
  390. #if 0
  391. static void ktpd_session_bad_socket(ktpd_session_t *session)
  392. {
  393. assert(session);
  394. if (!session)
  395. return;
  396. session->state = KTPD_SESSION_STATE_DISCONNECTED;
  397. }
  398. #endif