klishd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 = "command4",
  87. .help = "help1",
  88. PARAM_LIST
  89. PARAM {
  90. .name = "param1",
  91. .help = "helpparam1",
  92. .ptype = "ptype1",
  93. },
  94. PARAM {
  95. .name = "param2",
  96. .help = "helpparam2",
  97. .ptype = "ptype2",
  98. PARAM_LIST
  99. PARAM {
  100. .name = "param3",
  101. .help = "helpparam1",
  102. .ptype = "ptype1",
  103. },
  104. PARAM {
  105. .name = "param4",
  106. .help = "helpparam2",
  107. .ptype = "ptype2",
  108. },
  109. END_PARAM_LIST,
  110. },
  111. END_PARAM_LIST,
  112. ACTION_LIST
  113. ACTION {
  114. .sym = "internal",
  115. .script = "cat /etc/passwd",
  116. },
  117. ACTION {
  118. .sym = "internal",
  119. .script = "cat /etc/group",
  120. },
  121. END_ACTION_LIST,
  122. },
  123. END_COMMAND_LIST,
  124. },
  125. // VIEW {
  126. // },
  127. END_VIEW_LIST,
  128. };
  129. // Local static functions
  130. static int create_listen_unix_sock(const char *path);
  131. // Main loop events
  132. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  133. void *associated_data, void *user_data);
  134. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  135. void *associated_data, void *user_data);
  136. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  137. void *associated_data, void *user_data);
  138. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  139. void *associated_data, void *user_data);
  140. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  141. void *associated_data, void *user_data);
  142. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  143. void *associated_data, void *user_data);
  144. /** @brief Main function
  145. */
  146. int main(int argc, char **argv)
  147. {
  148. int retval = -1;
  149. struct options *opts = NULL;
  150. int pidfd = -1;
  151. int logoptions = 0;
  152. faux_eloop_t *eloop = NULL;
  153. int listen_unix_sock = -1;
  154. ktpd_clients_t *clients = NULL;
  155. kscheme_t *scheme = NULL;
  156. struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
  157. struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
  158. // Parse command line options
  159. opts = opts_init();
  160. if (opts_parse(argc, argv, opts))
  161. goto err;
  162. // Initialize syslog
  163. logoptions = LOG_CONS;
  164. if (opts->foreground)
  165. logoptions |= LOG_PERROR;
  166. openlog(LOG_NAME, logoptions, opts->log_facility);
  167. if (!opts->verbose)
  168. setlogmask(LOG_UPTO(LOG_INFO));
  169. // Parse config file
  170. syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
  171. if (!access(opts->cfgfile, R_OK)) {
  172. if (config_parse(opts->cfgfile, opts))
  173. goto err;
  174. } else if (opts->cfgfile_userdefined) {
  175. // User defined config must be found
  176. fprintf(stderr, "Error: Can't find config file %s\n",
  177. opts->cfgfile);
  178. goto err;
  179. }
  180. // DEBUG: Show options
  181. opts_show(opts);
  182. syslog(LOG_INFO, "Start daemon.\n");
  183. // Fork the daemon
  184. if (!opts->foreground) {
  185. // Daemonize
  186. syslog(LOG_DEBUG, "Daemonize\n");
  187. if (daemon(0, 0) < 0) {
  188. syslog(LOG_ERR, "Can't daemonize\n");
  189. goto err;
  190. }
  191. // Write pidfile
  192. syslog(LOG_DEBUG, "Write PID file: %s\n", opts->pidfile);
  193. if ((pidfd = open(opts->pidfile,
  194. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  195. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  196. syslog(LOG_WARNING, "Can't open pidfile %s: %s\n",
  197. opts->pidfile, strerror(errno));
  198. } else {
  199. char str[20];
  200. snprintf(str, sizeof(str), "%u\n", getpid());
  201. str[sizeof(str) - 1] = '\0';
  202. if (write(pidfd, str, strlen(str)) < 0)
  203. syslog(LOG_WARNING, "Can't write to %s: %s\n",
  204. opts->pidfile, strerror(errno));
  205. close(pidfd);
  206. }
  207. }
  208. // Load scheme
  209. {
  210. char *txt = NULL;
  211. faux_error_t *error = faux_error_new();
  212. scheme = kscheme_from_ischeme(&sch, error);
  213. if (!scheme) {
  214. fprintf(stderr, "Scheme errors:\n");
  215. faux_error_print(error);
  216. faux_error_free(error);
  217. goto err;
  218. }
  219. txt = ischeme_to_text(&sch, 0);
  220. printf("%s\n", txt);
  221. faux_str_free(txt);
  222. faux_error_free(error);
  223. }
  224. // Listen socket
  225. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  226. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  227. if (listen_unix_sock < 0)
  228. goto err;
  229. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  230. // Clients sessions DB
  231. clients = ktpd_clients_new();
  232. assert(clients);
  233. if (!clients)
  234. goto err;
  235. // Event loop
  236. eloop = faux_eloop_new(NULL);
  237. // Signals
  238. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  239. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  240. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  241. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  242. // Listen socket. Waiting for new connections
  243. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN, listen_socket_ev, clients);
  244. // Scheduled events
  245. faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  246. faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  247. // Main loop
  248. faux_eloop_loop(eloop);
  249. faux_eloop_free(eloop);
  250. /*
  251. // Non-blocking wait for all children
  252. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
  253. syslog(LOG_DEBUG, "Exit child process %d\n", pid);
  254. }
  255. */
  256. retval = 0;
  257. err:
  258. syslog(LOG_DEBUG, "Cleanup.\n");
  259. ktpd_clients_free(clients);
  260. // Close listen socket
  261. if (listen_unix_sock >= 0)
  262. close(listen_unix_sock);
  263. // Remove pidfile
  264. if (pidfd >= 0) {
  265. if (unlink(opts->pidfile) < 0) {
  266. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  267. opts->pidfile, strerror(errno));
  268. }
  269. }
  270. // Free scheme
  271. kscheme_free(scheme);
  272. // Free command line options
  273. opts_free(opts);
  274. syslog(LOG_INFO, "Stop daemon.\n");
  275. return retval;
  276. }
  277. /** @brief Create listen socket
  278. *
  279. * Previously removes old socket's file from filesystem. Note daemon must check
  280. * for already working daemon to don't duplicate.
  281. *
  282. * @param [in] path Socket path within filesystem.
  283. * @return Socket descriptor of < 0 on error.
  284. */
  285. static int create_listen_unix_sock(const char *path)
  286. {
  287. int sock = -1;
  288. int opt = 1;
  289. struct sockaddr_un laddr = {};
  290. assert(path);
  291. if (!path)
  292. return -1;
  293. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  294. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  295. goto err;
  296. }
  297. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  298. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  299. goto err;
  300. }
  301. // Remove old (lost) socket's file
  302. unlink(path);
  303. laddr.sun_family = AF_UNIX;
  304. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  305. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  306. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  307. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  308. goto err;
  309. }
  310. if (listen(sock, 128)) {
  311. unlink(path);
  312. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  313. goto err;
  314. }
  315. return sock;
  316. err:
  317. if (sock >= 0)
  318. close(sock);
  319. return -1;
  320. }
  321. /** @brief Stop main event loop.
  322. */
  323. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  324. void *associated_data, void *user_data)
  325. {
  326. // Happy compiler
  327. eloop = eloop;
  328. type = type;
  329. associated_data = associated_data;
  330. user_data = user_data;
  331. return BOOL_FALSE; // Stop Event Loop
  332. }
  333. /** @brief Re-read config file.
  334. */
  335. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  336. void *associated_data, void *user_data)
  337. {
  338. struct options *opts = (struct options *)user_data;
  339. if (access(opts->cfgfile, R_OK) == 0) {
  340. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  341. if (config_parse(opts->cfgfile, opts) < 0)
  342. syslog(LOG_ERR, "Error while config file parsing.\n");
  343. } else if (opts->cfgfile_userdefined) {
  344. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  345. }
  346. // Happy compiler
  347. eloop = eloop;
  348. type = type;
  349. associated_data = associated_data;
  350. return BOOL_TRUE;
  351. }
  352. bool_t fd_stall_cb(ktpd_session_t *session, void *user_data)
  353. {
  354. faux_eloop_t *eloop = (faux_eloop_t *)user_data;
  355. assert(session);
  356. assert(eloop);
  357. faux_eloop_include_fd_event(eloop, ktpd_session_fd(session), POLLOUT);
  358. return BOOL_TRUE;
  359. }
  360. /** @brief Event on listen socket. New remote client.
  361. */
  362. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  363. void *associated_data, void *user_data)
  364. {
  365. int new_conn = -1;
  366. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  367. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  368. ktpd_session_t *session = NULL;
  369. assert(clients);
  370. new_conn = accept(info->fd, NULL, NULL);
  371. if (new_conn < 0) {
  372. syslog(LOG_ERR, "Can't accept() new connection");
  373. return BOOL_TRUE;
  374. }
  375. session = ktpd_clients_add(clients, new_conn);
  376. if (!session) {
  377. syslog(LOG_ERR, "Duplicated client fd");
  378. close(new_conn);
  379. return BOOL_TRUE;
  380. }
  381. ktpd_session_set_stall_cb(session, fd_stall_cb, eloop);
  382. faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  383. syslog(LOG_DEBUG, "New connection %d", new_conn);
  384. type = type; // Happy compiler
  385. user_data = user_data; // Happy compiler
  386. return BOOL_TRUE;
  387. }
  388. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  389. void *associated_data, void *user_data)
  390. {
  391. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  392. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  393. ktpd_session_t *session = NULL;
  394. assert(clients);
  395. // Find out session
  396. session = ktpd_clients_find(clients, info->fd);
  397. if (!session) { // Some strange case
  398. syslog(LOG_ERR, "Can't find client session for fd %d", info->fd);
  399. faux_eloop_del_fd(eloop, info->fd);
  400. close(info->fd);
  401. return BOOL_TRUE;
  402. }
  403. // Write data
  404. if (info->revents & POLLOUT) {
  405. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  406. if (!ktpd_session_async_out(session)) {
  407. // Someting went wrong
  408. faux_eloop_del_fd(eloop, info->fd);
  409. ktpd_clients_del(clients, info->fd);
  410. syslog(LOG_ERR, "Problem with async input");
  411. }
  412. }
  413. // Read data
  414. if (info->revents & POLLIN) {
  415. if (!ktpd_session_async_in(session)) {
  416. // Someting went wrong
  417. faux_eloop_del_fd(eloop, info->fd);
  418. ktpd_clients_del(clients, info->fd);
  419. syslog(LOG_ERR, "Problem with async input");
  420. }
  421. }
  422. // EOF
  423. if (info->revents & POLLHUP) {
  424. faux_eloop_del_fd(eloop, info->fd);
  425. ktpd_clients_del(clients, info->fd);
  426. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  427. }
  428. type = type; // Happy compiler
  429. user_data = user_data; // Happy compiler
  430. return BOOL_TRUE;
  431. }
  432. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  433. void *associated_data, void *user_data)
  434. {
  435. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  436. printf("Once %d\n", info->ev_id);
  437. // Happy compiler
  438. eloop = eloop;
  439. type = type;
  440. associated_data = associated_data;
  441. user_data = user_data;
  442. return BOOL_TRUE;
  443. }
  444. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  445. void *associated_data, void *user_data)
  446. {
  447. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  448. printf("Periodic %d\n", info->ev_id);
  449. // Happy compiler
  450. eloop = eloop;
  451. type = type;
  452. associated_data = associated_data;
  453. user_data = user_data;
  454. return BOOL_TRUE;
  455. }