ktpd_session.c 11 KB

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