klishd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <signal.h>
  8. #include <syslog.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #include <sys/fsuid.h>
  17. #include <sys/wait.h>
  18. #include <poll.h>
  19. #include <time.h>
  20. #include <faux/faux.h>
  21. #include <faux/str.h>
  22. #include <faux/ini.h>
  23. #include <faux/log.h>
  24. #include <faux/sched.h>
  25. #include <faux/sysdb.h>
  26. #include <faux/net.h>
  27. #include <faux/list.h>
  28. #include <faux/conv.h>
  29. #include <faux/file.h>
  30. #include <faux/eloop.h>
  31. #include <faux/error.h>
  32. #include <klish/ktp.h>
  33. #include <klish/ktp_session.h>
  34. #include <klish/kscheme.h>
  35. #include "private.h"
  36. ischeme_t sch = {
  37. PTYPE_LIST
  38. PTYPE {
  39. .name = "ptype1",
  40. .help = "help1",
  41. ACTION_LIST
  42. ACTION {
  43. .sym = "internal",
  44. .script = "cat /etc/passwd",
  45. },
  46. ACTION {
  47. .sym = "internal",
  48. .script = "cat /etc/group",
  49. },
  50. END_ACTION_LIST,
  51. },
  52. PTYPE {
  53. .name = "ptype2",
  54. .help = "help2",
  55. },
  56. END_PTYPE_LIST,
  57. VIEW_LIST
  58. VIEW {
  59. .name = "view1",
  60. COMMAND_LIST
  61. COMMAND {
  62. .name = "command1",
  63. .help = "help1",
  64. },
  65. COMMAND {
  66. .name = "command2",
  67. .help = "help1",
  68. },
  69. COMMAND {
  70. .name = "command3",
  71. .help = "help1",
  72. },
  73. END_COMMAND_LIST,
  74. },
  75. VIEW {
  76. .name = "view2",
  77. },
  78. VIEW {
  79. .name = "view1",
  80. COMMAND_LIST
  81. COMMAND {
  82. .name = "command4",
  83. .help = "help1",
  84. },
  85. COMMAND {
  86. .name = "command5",
  87. .help = "help1",
  88. },
  89. END_COMMAND_LIST,
  90. },
  91. // VIEW {
  92. // },
  93. END_VIEW_LIST,
  94. };
  95. // Local static functions
  96. static int create_listen_unix_sock(const char *path);
  97. // Main loop events
  98. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  99. void *associated_data, void *user_data);
  100. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  101. void *associated_data, void *user_data);
  102. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  103. void *associated_data, void *user_data);
  104. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  105. void *associated_data, void *user_data);
  106. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  107. void *associated_data, void *user_data);
  108. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  109. void *associated_data, void *user_data);
  110. /** @brief Main function
  111. */
  112. int main(int argc, char **argv)
  113. {
  114. int retval = -1;
  115. struct options *opts = NULL;
  116. int pidfd = -1;
  117. int logoptions = 0;
  118. faux_eloop_t *eloop = NULL;
  119. int listen_unix_sock = -1;
  120. ktpd_clients_t *clients = NULL;
  121. kscheme_t *scheme = NULL;
  122. struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
  123. struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
  124. // Parse command line options
  125. opts = opts_init();
  126. if (opts_parse(argc, argv, opts))
  127. goto err;
  128. // Initialize syslog
  129. logoptions = LOG_CONS;
  130. if (opts->foreground)
  131. logoptions |= LOG_PERROR;
  132. openlog(LOG_NAME, logoptions, opts->log_facility);
  133. if (!opts->verbose)
  134. setlogmask(LOG_UPTO(LOG_INFO));
  135. // Parse config file
  136. syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
  137. if (!access(opts->cfgfile, R_OK)) {
  138. if (config_parse(opts->cfgfile, opts))
  139. goto err;
  140. } else if (opts->cfgfile_userdefined) {
  141. // User defined config must be found
  142. fprintf(stderr, "Error: Can't find config file %s\n",
  143. opts->cfgfile);
  144. goto err;
  145. }
  146. // DEBUG: Show options
  147. opts_show(opts);
  148. syslog(LOG_INFO, "Start daemon.\n");
  149. // Fork the daemon
  150. if (!opts->foreground) {
  151. // Daemonize
  152. syslog(LOG_DEBUG, "Daemonize\n");
  153. if (daemon(0, 0) < 0) {
  154. syslog(LOG_ERR, "Can't daemonize\n");
  155. goto err;
  156. }
  157. // Write pidfile
  158. syslog(LOG_DEBUG, "Write PID file: %s\n", opts->pidfile);
  159. if ((pidfd = open(opts->pidfile,
  160. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  161. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  162. syslog(LOG_WARNING, "Can't open pidfile %s: %s\n",
  163. opts->pidfile, strerror(errno));
  164. } else {
  165. char str[20];
  166. snprintf(str, sizeof(str), "%u\n", getpid());
  167. str[sizeof(str) - 1] = '\0';
  168. if (write(pidfd, str, strlen(str)) < 0)
  169. syslog(LOG_WARNING, "Can't write to %s: %s\n",
  170. opts->pidfile, strerror(errno));
  171. close(pidfd);
  172. }
  173. }
  174. // Load scheme
  175. {
  176. char *txt = NULL;
  177. faux_error_t *error = faux_error_new();
  178. scheme = kscheme_from_ischeme(&sch, error);
  179. if (!scheme) {
  180. fprintf(stderr, "Scheme errors:\n");
  181. faux_error_print(error);
  182. goto err;
  183. }
  184. txt = ischeme_to_text(&sch, 0);
  185. printf("%s\n", txt);
  186. faux_str_free(txt);
  187. }
  188. // Listen socket
  189. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  190. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  191. if (listen_unix_sock < 0)
  192. goto err;
  193. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  194. // Clients sessions DB
  195. clients = ktpd_clients_new();
  196. assert(clients);
  197. if (!clients)
  198. goto err;
  199. // Event loop
  200. eloop = faux_eloop_new(NULL);
  201. // Signals
  202. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  203. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  204. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  205. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  206. // Listen socket. Waiting for new connections
  207. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN, listen_socket_ev, clients);
  208. // Scheduled events
  209. faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  210. faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  211. // Main loop
  212. faux_eloop_loop(eloop);
  213. faux_eloop_free(eloop);
  214. /*
  215. // Non-blocking wait for all children
  216. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
  217. syslog(LOG_DEBUG, "Exit child process %d\n", pid);
  218. }
  219. */
  220. retval = 0;
  221. err:
  222. syslog(LOG_DEBUG, "Cleanup.\n");
  223. ktpd_clients_free(clients);
  224. // Close listen socket
  225. if (listen_unix_sock >= 0)
  226. close(listen_unix_sock);
  227. // Remove pidfile
  228. if (pidfd >= 0) {
  229. if (unlink(opts->pidfile) < 0) {
  230. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  231. opts->pidfile, strerror(errno));
  232. }
  233. }
  234. // Free scheme
  235. kscheme_free(scheme);
  236. // Free command line options
  237. opts_free(opts);
  238. syslog(LOG_INFO, "Stop daemon.\n");
  239. return retval;
  240. }
  241. /** @brief Create listen socket
  242. *
  243. * Previously removes old socket's file from filesystem. Note daemon must check
  244. * for already working daemon to don't duplicate.
  245. *
  246. * @param [in] path Socket path within filesystem.
  247. * @return Socket descriptor of < 0 on error.
  248. */
  249. static int create_listen_unix_sock(const char *path)
  250. {
  251. int sock = -1;
  252. int opt = 1;
  253. struct sockaddr_un laddr = {};
  254. assert(path);
  255. if (!path)
  256. return -1;
  257. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  258. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  259. goto err;
  260. }
  261. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  262. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  263. goto err;
  264. }
  265. // Remove old (lost) socket's file
  266. unlink(path);
  267. laddr.sun_family = AF_UNIX;
  268. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  269. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  270. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  271. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  272. goto err;
  273. }
  274. if (listen(sock, 128)) {
  275. unlink(path);
  276. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  277. goto err;
  278. }
  279. return sock;
  280. err:
  281. if (sock >= 0)
  282. close(sock);
  283. return -1;
  284. }
  285. /** @brief Stop main event loop.
  286. */
  287. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  288. void *associated_data, void *user_data)
  289. {
  290. // Happy compiler
  291. eloop = eloop;
  292. type = type;
  293. associated_data = associated_data;
  294. user_data = user_data;
  295. return BOOL_FALSE; // Stop Event Loop
  296. }
  297. /** @brief Re-read config file.
  298. */
  299. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  300. void *associated_data, void *user_data)
  301. {
  302. struct options *opts = (struct options *)user_data;
  303. if (access(opts->cfgfile, R_OK) == 0) {
  304. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  305. if (config_parse(opts->cfgfile, opts) < 0)
  306. syslog(LOG_ERR, "Error while config file parsing.\n");
  307. } else if (opts->cfgfile_userdefined) {
  308. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  309. }
  310. // Happy compiler
  311. eloop = eloop;
  312. type = type;
  313. associated_data = associated_data;
  314. return BOOL_TRUE;
  315. }
  316. bool_t fd_stall_cb(ktpd_session_t *session, void *user_data)
  317. {
  318. faux_eloop_t *eloop = (faux_eloop_t *)user_data;
  319. assert(session);
  320. assert(eloop);
  321. faux_eloop_include_fd_event(eloop, ktpd_session_fd(session), POLLOUT);
  322. return BOOL_TRUE;
  323. }
  324. /** @brief Event on listen socket. New remote client.
  325. */
  326. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  327. void *associated_data, void *user_data)
  328. {
  329. int new_conn = -1;
  330. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  331. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  332. ktpd_session_t *session = NULL;
  333. assert(clients);
  334. new_conn = accept(info->fd, NULL, NULL);
  335. if (new_conn < 0) {
  336. syslog(LOG_ERR, "Can't accept() new connection");
  337. return BOOL_TRUE;
  338. }
  339. session = ktpd_clients_add(clients, new_conn);
  340. if (!session) {
  341. syslog(LOG_ERR, "Duplicated client fd");
  342. close(new_conn);
  343. return BOOL_TRUE;
  344. }
  345. ktpd_session_set_stall_cb(session, fd_stall_cb, eloop);
  346. faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  347. syslog(LOG_DEBUG, "New connection %d", new_conn);
  348. type = type; // Happy compiler
  349. user_data = user_data; // Happy compiler
  350. return BOOL_TRUE;
  351. }
  352. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  353. void *associated_data, void *user_data)
  354. {
  355. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  356. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  357. ktpd_session_t *session = NULL;
  358. assert(clients);
  359. // Find out session
  360. session = ktpd_clients_find(clients, info->fd);
  361. if (!session) { // Some strange case
  362. syslog(LOG_ERR, "Can't find client session for fd %d", info->fd);
  363. faux_eloop_del_fd(eloop, info->fd);
  364. close(info->fd);
  365. return BOOL_TRUE;
  366. }
  367. // Write data
  368. if (info->revents & POLLOUT) {
  369. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  370. if (!ktpd_session_async_out(session)) {
  371. // Someting went wrong
  372. faux_eloop_del_fd(eloop, info->fd);
  373. ktpd_clients_del(clients, info->fd);
  374. syslog(LOG_ERR, "Problem with async input");
  375. }
  376. }
  377. // Read data
  378. if (info->revents & POLLIN) {
  379. if (!ktpd_session_async_in(session)) {
  380. // Someting went wrong
  381. faux_eloop_del_fd(eloop, info->fd);
  382. ktpd_clients_del(clients, info->fd);
  383. syslog(LOG_ERR, "Problem with async input");
  384. }
  385. }
  386. // EOF
  387. if (info->revents & POLLHUP) {
  388. faux_eloop_del_fd(eloop, info->fd);
  389. ktpd_clients_del(clients, info->fd);
  390. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  391. }
  392. type = type; // Happy compiler
  393. user_data = user_data; // Happy compiler
  394. return BOOL_TRUE;
  395. }
  396. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  397. void *associated_data, void *user_data)
  398. {
  399. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  400. printf("Once %d\n", info->ev_id);
  401. // Happy compiler
  402. eloop = eloop;
  403. type = type;
  404. associated_data = associated_data;
  405. user_data = user_data;
  406. return BOOL_TRUE;
  407. }
  408. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  409. void *associated_data, void *user_data)
  410. {
  411. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  412. printf("Periodic %d\n", info->ev_id);
  413. // Happy compiler
  414. eloop = eloop;
  415. type = type;
  416. associated_data = associated_data;
  417. user_data = user_data;
  418. return BOOL_TRUE;
  419. }