1
0

konfd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * konfd.c
  3. *
  4. * The konfd daemon to store user configuration commands.
  5. *
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif /* HAVE_CONFIG_H */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <errno.h>
  16. #include <assert.h>
  17. #include <sys/socket.h>
  18. #include <sys/un.h>
  19. #include <string.h>
  20. #include <sys/select.h>
  21. #include <signal.h>
  22. #ifdef HAVE_GETOPT_H
  23. #include <getopt.h>
  24. #endif
  25. #include "clish/private.h"
  26. #include "konf/tree.h"
  27. #include "konf/query.h"
  28. #include "konf/buf.h"
  29. #include "lub/argv.h"
  30. #include "lub/string.h"
  31. #ifndef VERSION
  32. #define VERSION 1.2.2
  33. #endif
  34. #define QUOTE(t) #t
  35. #define version(v) printf("%s\n", v)
  36. #define KONFD_CONFIG_PATH "/tmp/running-config"
  37. /* UNIX socket path */
  38. #ifndef UNIX_PATH_MAX
  39. #define UNIX_PATH_MAX 108
  40. #endif
  41. /* OpenBSD has no MSG_NOSIGNAL flag */
  42. #ifndef MSG_NOSIGNAL
  43. #define MSG_NOSIGNAL 0
  44. #endif
  45. #define MAXMSG 1024
  46. /* Global signal vars */
  47. static volatile int sigterm = 0;
  48. static void sighandler(int signo);
  49. static void help(int status, const char *argv0);
  50. static char * process_query(int sock, konf_tree_t * conf, char *str);
  51. int answer_send(int sock, char *command);
  52. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query);
  53. /*--------------------------------------------------------- */
  54. int main(int argc, char **argv)
  55. {
  56. int retval = 0;
  57. unsigned i;
  58. char *str;
  59. konf_tree_t *conf;
  60. lub_bintree_t bufs;
  61. konf_buf_t *tbuf;
  62. /* Network vars */
  63. int sock;
  64. struct sockaddr_un laddr;
  65. struct sockaddr_un raddr;
  66. fd_set active_fd_set, read_fd_set;
  67. const char *socket_path = KONFD_SOCKET_PATH;
  68. /* Signal vars */
  69. struct sigaction sig_act, sigpipe_act;
  70. sigset_t sig_set, sigpipe_set;
  71. /* Command line options */
  72. static const char *shortopts = "hvs:";
  73. #ifdef HAVE_GETOPT_H
  74. static const struct option longopts[] = {
  75. {"help", 0, NULL, 'h'},
  76. {"version", 0, NULL, 'v'},
  77. {"socket", 1, NULL, 's'},
  78. {NULL, 0, NULL, 0}
  79. };
  80. #endif
  81. /* Parse command line options */
  82. optind = 0;
  83. while(1) {
  84. int opt;
  85. #ifdef HAVE_GETOPT_H
  86. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  87. #else
  88. opt = getopt(argc, argv, shortopts);
  89. #endif
  90. if (-1 == opt)
  91. break;
  92. switch (opt) {
  93. case 's':
  94. socket_path = optarg;
  95. break;
  96. case 'h':
  97. help(0, argv[0]);
  98. exit(0);
  99. break;
  100. case 'v':
  101. version(VERSION);
  102. exit(0);
  103. break;
  104. default:
  105. help(-1, argv[0]);
  106. exit(-1);
  107. break;
  108. }
  109. }
  110. /* Set signal handler */
  111. sigemptyset(&sig_set);
  112. sigaddset(&sig_set, SIGTERM);
  113. sigaddset(&sig_set, SIGINT);
  114. sigaddset(&sig_set, SIGQUIT);
  115. sig_act.sa_flags = 0;
  116. sig_act.sa_mask = sig_set;
  117. sig_act.sa_handler = &sighandler;
  118. sigaction(SIGTERM, &sig_act, NULL);
  119. sigaction(SIGINT, &sig_act, NULL);
  120. sigaction(SIGQUIT, &sig_act, NULL);
  121. /* Ignore SIGPIPE */
  122. sigemptyset(&sigpipe_set);
  123. sigaddset(&sigpipe_set, SIGPIPE);
  124. sigpipe_act.sa_flags = 0;
  125. sigpipe_act.sa_mask = sigpipe_set;
  126. sigpipe_act.sa_handler = SIG_IGN;
  127. sigaction(SIGPIPE, &sigpipe_act, NULL);
  128. /* Create listen socket */
  129. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  130. fprintf(stderr, "Cannot create socket: %s\n", strerror(errno));
  131. /* syslog(LOG_ERR, "Cannot create socket: %s\n", strerror(errno)); */
  132. return -1;
  133. }
  134. laddr.sun_family = AF_UNIX;
  135. strncpy(laddr.sun_path, socket_path, UNIX_PATH_MAX);
  136. laddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  137. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  138. fprintf(stderr, "Can't bind()\n");
  139. /* syslog(LOG_ERR, "Can't bind()\n"); */
  140. close(sock);
  141. return -1;
  142. }
  143. listen(sock, 5);
  144. /* Create configuration tree */
  145. conf = konf_tree_new("", 0);
  146. /* Initialize the tree of buffers */
  147. lub_bintree_init(&bufs,
  148. konf_buf_bt_offset(),
  149. konf_buf_bt_compare, konf_buf_bt_getkey);
  150. /* Initialize the set of active sockets. */
  151. FD_ZERO(&active_fd_set);
  152. FD_SET(sock, &active_fd_set);
  153. /* Main loop */
  154. while (!sigterm) {
  155. int num;
  156. /* Block until input arrives on one or more active sockets. */
  157. read_fd_set = active_fd_set;
  158. num = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
  159. if (num < 0) {
  160. if (EINTR == errno)
  161. continue;
  162. break;
  163. }
  164. if (0 == num)
  165. continue;
  166. /* Service all the sockets with input pending. */
  167. for (i = 0; i < FD_SETSIZE; ++i) {
  168. if (FD_ISSET(i, &read_fd_set)) {
  169. if (i == sock) {
  170. /* Connection request on original socket. */
  171. int new;
  172. socklen_t size;
  173. size = sizeof(raddr);
  174. new = accept(sock, (struct sockaddr *)
  175. &raddr, &size);
  176. if (new < 0) {
  177. fprintf(stderr, "accept");
  178. continue;
  179. }
  180. fprintf(stderr, "Server: connect %u\n", new);
  181. konf_buftree_remove(&bufs, new);
  182. tbuf = konf_buf_new(new);
  183. /* insert it into the binary tree for this conf */
  184. lub_bintree_insert(&bufs, tbuf);
  185. FD_SET(new, &active_fd_set);
  186. } else {
  187. int nbytes;
  188. /* Data arriving on an already-connected socket. */
  189. if ((nbytes = konf_buftree_read(&bufs, i)) <= 0) {
  190. close(i);
  191. FD_CLR(i, &active_fd_set);
  192. konf_buftree_remove(&bufs, i);
  193. continue;
  194. }
  195. while ((str = konf_buftree_parse(&bufs, i))) {
  196. char *answer;
  197. if (!(answer = process_query(i, conf, str)))
  198. answer = lub_string_dup("-e");
  199. lub_string_free(str);
  200. answer_send(i, answer);
  201. lub_string_free(answer);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. /* Free resources */
  208. konf_tree_delete(conf);
  209. /* delete each buf */
  210. while ((tbuf = lub_bintree_findfirst(&bufs))) {
  211. /* remove the buf from the tree */
  212. lub_bintree_remove(&bufs, tbuf);
  213. /* release the instance */
  214. konf_buf_delete(tbuf);
  215. }
  216. /* Close listen socket */
  217. close(sock);
  218. unlink(socket_path);
  219. return retval;
  220. }
  221. /*--------------------------------------------------------- */
  222. static char * process_query(int sock, konf_tree_t * conf, char *str)
  223. {
  224. unsigned i;
  225. int res;
  226. konf_tree_t *iconf;
  227. konf_tree_t *tmpconf;
  228. konf_query_t *query;
  229. char *retval = NULL;
  230. konf_query_op_t ret;
  231. #ifdef DEBUG
  232. fprintf(stderr, "----------------------\n");
  233. fprintf(stderr, "REQUEST: %s\n", str);
  234. #endif
  235. /* Parse query */
  236. query = konf_query_new();
  237. res = konf_query_parse_str(query, str);
  238. if (res < 0) {
  239. konf_query_free(query);
  240. return NULL;
  241. }
  242. #ifdef DEBUG
  243. konf_query_dump(query);
  244. #endif
  245. /* Go through the pwd */
  246. iconf = conf;
  247. for (i = 0; i < konf_query__get_pwdc(query); i++) {
  248. if (!
  249. (iconf =
  250. konf_tree_find_conf(iconf, konf_query__get_pwd(query, i), 0, 0))) {
  251. iconf = NULL;
  252. break;
  253. }
  254. }
  255. if (!iconf) {
  256. fprintf(stderr, "Unknown path\n");
  257. konf_query_free(query);
  258. return NULL;
  259. }
  260. switch (konf_query__get_op(query)) {
  261. case KONF_QUERY_OP_SET:
  262. if (konf_query__get_unique(query)) {
  263. if (konf_tree_find_conf(iconf,
  264. konf_query__get_line(query), 0, 0)) {
  265. ret = KONF_QUERY_OP_OK;
  266. break;
  267. }
  268. konf_tree_del_pattern(iconf,
  269. konf_query__get_pattern(query),
  270. konf_query__get_priority(query),
  271. konf_query__get_seq(query),
  272. konf_query__get_seq_num(query));
  273. }
  274. tmpconf = konf_tree_new_conf(iconf,
  275. konf_query__get_line(query), konf_query__get_priority(query),
  276. konf_query__get_seq(query), konf_query__get_seq_num(query));
  277. if (!tmpconf) {
  278. ret = KONF_QUERY_OP_ERROR;
  279. break;
  280. }
  281. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  282. konf_tree__set_depth(tmpconf, konf_query__get_pwdc(query));
  283. ret = KONF_QUERY_OP_OK;
  284. break;
  285. case KONF_QUERY_OP_UNSET:
  286. konf_tree_del_pattern(iconf,
  287. konf_query__get_pattern(query), konf_query__get_priority(query),
  288. konf_query__get_seq(query), konf_query__get_seq_num(query));
  289. ret = KONF_QUERY_OP_OK;
  290. break;
  291. case KONF_QUERY_OP_DUMP:
  292. if (dump_running_config(sock, iconf, query))
  293. ret = KONF_QUERY_OP_ERROR;
  294. else
  295. ret = KONF_QUERY_OP_OK;
  296. break;
  297. default:
  298. ret = KONF_QUERY_OP_ERROR;
  299. break;
  300. }
  301. #ifdef DEBUG
  302. /* Print whole tree */
  303. konf_tree_fprintf(conf, stderr, NULL, -1, BOOL_TRUE, 0);
  304. #endif
  305. /* Free resources */
  306. konf_query_free(query);
  307. switch (ret) {
  308. case KONF_QUERY_OP_OK:
  309. lub_string_cat(&retval, "-o");
  310. break;
  311. case KONF_QUERY_OP_ERROR:
  312. lub_string_cat(&retval, "-e");
  313. break;
  314. default:
  315. lub_string_cat(&retval, "-e");
  316. break;
  317. };
  318. return retval;
  319. }
  320. /*--------------------------------------------------------- */
  321. /*
  322. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  323. */
  324. static void sighandler(int signo)
  325. {
  326. sigterm = 1;
  327. }
  328. /*--------------------------------------------------------- */
  329. int answer_send(int sock, char *command)
  330. {
  331. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  332. }
  333. /*--------------------------------------------------------- */
  334. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  335. {
  336. FILE *fd;
  337. char *filename;
  338. int dupsock = -1;
  339. if ((filename = konf_query__get_path(query))) {
  340. if (!(fd = fopen(filename, "w")))
  341. return -1;
  342. } else {
  343. if ((dupsock = dup(sock)) < 0)
  344. return -1;
  345. fd = fdopen(dupsock, "w");
  346. }
  347. if (!filename) {
  348. fprintf(fd, "-t\n");
  349. #ifdef DEBUG
  350. fprintf(stderr, "ANSWER: -t\n");
  351. #endif
  352. }
  353. konf_tree_fprintf(conf,
  354. fd,
  355. konf_query__get_pattern(query),
  356. konf_query__get_pwdc(query) - 1,
  357. konf_query__get_seq(query),
  358. 0);
  359. if (!filename) {
  360. fprintf(fd, "\n");
  361. #ifdef DEBUG
  362. fprintf(stderr, "SEND DATA: \n");
  363. #endif
  364. }
  365. fclose(fd);
  366. return 0;
  367. }
  368. /*--------------------------------------------------------- */
  369. /* Print help message */
  370. static void help(int status, const char *argv0)
  371. {
  372. const char *name = NULL;
  373. if (!argv0)
  374. return;
  375. /* Find the basename */
  376. name = strrchr(argv0, '/');
  377. if (name)
  378. name++;
  379. else
  380. name = argv0;
  381. if (status != 0) {
  382. fprintf(stderr, "Try `%s -h' for more information.\n",
  383. name);
  384. } else {
  385. printf("Usage: %s [options]\n", name);
  386. printf("Daemon to store user configuration (i.e. commands). "
  387. "The part of the klish project.\n");
  388. printf("Options:\n");
  389. printf("\t-v, --version\tPrint version.\n");
  390. printf("\t-h, --help\tPrint this help.\n");
  391. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  392. "filesystem path to listen on.\n");
  393. }
  394. }