konfd.c 9.5 KB

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