konfd.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * clish_config_callback.c
  3. *
  4. *
  5. * Callback hook to execute config operations.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <errno.h>
  13. #include <assert.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #include <string.h>
  17. #include <sys/select.h>
  18. #include <signal.h>
  19. #include "clish/private.h"
  20. #include "konf/tree.h"
  21. #include "konf/query.h"
  22. #include "konf/buf.h"
  23. #include "lub/argv.h"
  24. #include "lub/string.h"
  25. #define KONFD_SOCKET_PATH "/tmp/konfd.socket"
  26. #define KONFD_CONFIG_PATH "/tmp/running-config"
  27. #ifndef UNIX_PATH_MAX
  28. #define UNIX_PATH_MAX 108
  29. #endif
  30. #define MAXMSG 1024
  31. /* Global signal vars */
  32. static volatile int sigterm = 0;
  33. static void sighandler(int signo);
  34. static char * process_query(int sock, konf_tree_t * conf, char *str);
  35. int answer_send(int sock, char *command);
  36. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query);
  37. /*--------------------------------------------------------- */
  38. int main(int argc, char **argv)
  39. {
  40. int retval = 0;
  41. unsigned i;
  42. char *str;
  43. konf_tree_t *conf;
  44. lub_bintree_t bufs;
  45. konf_buf_t *tbuf;
  46. /* Network vars */
  47. int sock;
  48. struct sockaddr_un laddr;
  49. struct sockaddr_un raddr;
  50. fd_set active_fd_set, read_fd_set;
  51. /* Signal vars */
  52. struct sigaction sig_act;
  53. sigset_t sig_set;
  54. /* Set signal handler */
  55. sigemptyset(&sig_set);
  56. sigaddset(&sig_set, SIGTERM);
  57. sigaddset(&sig_set, SIGINT);
  58. sigaddset(&sig_set, SIGQUIT);
  59. sig_act.sa_flags = 0;
  60. sig_act.sa_mask = sig_set;
  61. sig_act.sa_handler = &sighandler;
  62. sigaction(SIGTERM, &sig_act, NULL);
  63. sigaction(SIGINT, &sig_act, NULL);
  64. sigaction(SIGQUIT, &sig_act, NULL);
  65. /* Configuration tree */
  66. conf = konf_tree_new("", 0);
  67. /* Initialize the tree of buffers */
  68. lub_bintree_init(&bufs,
  69. konf_buf_bt_offset(),
  70. konf_buf_bt_compare, konf_buf_bt_getkey);
  71. /* Create listen socket */
  72. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  73. fprintf(stderr, "Cannot create socket: %s\n", strerror(errno));
  74. /* syslog(LOG_ERR, "Cannot create socket: %s\n", strerror(errno));
  75. */ return -1;
  76. }
  77. unlink(KONFD_SOCKET_PATH);
  78. laddr.sun_family = AF_UNIX;
  79. strncpy(laddr.sun_path, KONFD_SOCKET_PATH, UNIX_PATH_MAX);
  80. laddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  81. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  82. fprintf(stderr, "Can't bind()\n");
  83. /* syslog(LOG_ERR, "Can't bind()\n");
  84. */ return -1;
  85. }
  86. listen(sock, 5);
  87. /* Initialize the set of active sockets. */
  88. FD_ZERO(&active_fd_set);
  89. FD_SET(sock, &active_fd_set);
  90. /* Main loop */
  91. while (!sigterm) {
  92. int num;
  93. /* Block until input arrives on one or more active sockets. */
  94. read_fd_set = active_fd_set;
  95. num = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
  96. if (num < 0) {
  97. if (EINTR == errno)
  98. continue;
  99. break;
  100. }
  101. if (0 == num)
  102. continue;
  103. /* Get string from stdout */
  104. /* if (!fgets(str, sizeof(str), stdin)) {
  105. enough = 1;
  106. continue;
  107. }
  108. */
  109. /* Service all the sockets with input pending. */
  110. for (i = 0; i < FD_SETSIZE; ++i) {
  111. if (FD_ISSET(i, &read_fd_set)) {
  112. if (i == sock) {
  113. /* Connection request on original socket. */
  114. int new;
  115. socklen_t size;
  116. size = sizeof(raddr);
  117. new = accept(sock, (struct sockaddr *)
  118. &raddr, &size);
  119. if (new < 0) {
  120. fprintf(stderr, "accept");
  121. continue;
  122. }
  123. fprintf(stderr, "Server: connect %u\n", new);
  124. konf_buftree_remove(&bufs, new);
  125. tbuf = konf_buf_new(new);
  126. /* insert it into the binary tree for this conf */
  127. lub_bintree_insert(&bufs, tbuf);
  128. FD_SET(new, &active_fd_set);
  129. } else {
  130. int nbytes;
  131. /* Data arriving on an already-connected socket. */
  132. if ((nbytes = konf_buftree_read(&bufs, i)) <= 0) {
  133. close(i);
  134. FD_CLR(i, &active_fd_set);
  135. konf_buftree_remove(&bufs, i);
  136. continue;
  137. }
  138. while ((str = konf_buftree_parse(&bufs, i))) {
  139. char *answer;
  140. if (!(answer = process_query(i, conf, str)))
  141. answer = lub_string_dup("-e");
  142. lub_string_free(str);
  143. answer_send(i, answer);
  144. lub_string_free(answer);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. /* Free resources */
  151. konf_tree_delete(conf);
  152. /* delete each buf */
  153. while ((tbuf = lub_bintree_findfirst(&bufs))) {
  154. /* remove the buf from the tree */
  155. lub_bintree_remove(&bufs, tbuf);
  156. /* release the instance */
  157. konf_buf_delete(tbuf);
  158. }
  159. return retval;
  160. }
  161. static char * process_query(int sock, konf_tree_t * conf, char *str)
  162. {
  163. unsigned i;
  164. int res;
  165. konf_tree_t *iconf;
  166. konf_tree_t *tmpconf;
  167. konf_query_t *query;
  168. char *retval = NULL;
  169. konf_query_op_t ret;
  170. /* Parse query */
  171. query = konf_query_new();
  172. res = konf_query_parse_str(query, str);
  173. if (res < 0) {
  174. konf_query_free(query);
  175. return NULL;
  176. }
  177. #ifdef DEBUG
  178. fprintf(stderr, "----------------------\n");
  179. fprintf(stderr, "REQUEST: %s\n", str);
  180. /* konf_query_dump(query);
  181. */
  182. #endif
  183. /* Go through the pwd */
  184. iconf = conf;
  185. for (i = 0; i < konf_query__get_pwdc(query); i++) {
  186. if (!
  187. (iconf =
  188. konf_tree_find_conf(iconf, konf_query__get_pwd(query, i), 0, 0))) {
  189. iconf = NULL;
  190. break;
  191. }
  192. }
  193. if (!iconf) {
  194. fprintf(stderr, "Unknown path\n");
  195. konf_query_free(query);
  196. return NULL;
  197. }
  198. switch (konf_query__get_op(query)) {
  199. case KONF_QUERY_OP_SET:
  200. if (konf_query__get_unique(query)) {
  201. if (konf_tree_find_conf(iconf,
  202. konf_query__get_line(query), 0, 0)) {
  203. ret = KONF_QUERY_OP_OK;
  204. break;
  205. }
  206. konf_tree_del_pattern(iconf,
  207. konf_query__get_pattern(query),
  208. konf_query__get_priority(query),
  209. konf_query__get_seq(query),
  210. konf_query__get_seq_num(query));
  211. }
  212. tmpconf = konf_tree_new_conf(iconf,
  213. konf_query__get_line(query), konf_query__get_priority(query),
  214. konf_query__get_seq(query), konf_query__get_seq_num(query));
  215. if (!tmpconf) {
  216. ret = KONF_QUERY_OP_ERROR;
  217. break;
  218. }
  219. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  220. ret = KONF_QUERY_OP_OK;
  221. break;
  222. case KONF_QUERY_OP_UNSET:
  223. konf_tree_del_pattern(iconf,
  224. konf_query__get_pattern(query), konf_query__get_priority(query),
  225. konf_query__get_seq(query), konf_query__get_seq_num(query));
  226. ret = KONF_QUERY_OP_OK;
  227. break;
  228. case KONF_QUERY_OP_DUMP:
  229. if (dump_running_config(sock, iconf, query))
  230. ret = KONF_QUERY_OP_ERROR;
  231. else
  232. ret = KONF_QUERY_OP_OK;
  233. break;
  234. default:
  235. ret = KONF_QUERY_OP_ERROR;
  236. break;
  237. }
  238. #ifdef DEBUG
  239. /* Print whole tree */
  240. konf_tree_fprintf(conf, stderr, NULL, -1, BOOL_TRUE, 0);
  241. #endif
  242. /* Free resources */
  243. konf_query_free(query);
  244. switch (ret) {
  245. case KONF_QUERY_OP_OK:
  246. lub_string_cat(&retval, "-o");
  247. break;
  248. case KONF_QUERY_OP_ERROR:
  249. lub_string_cat(&retval, "-e");
  250. break;
  251. default:
  252. lub_string_cat(&retval, "-e");
  253. break;
  254. };
  255. return retval;
  256. }
  257. /*
  258. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  259. */
  260. static void sighandler(int signo)
  261. {
  262. sigterm = 1;
  263. }
  264. int answer_send(int sock, char *command)
  265. {
  266. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  267. }
  268. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  269. {
  270. FILE *fd;
  271. char *filename;
  272. int dupsock = -1;
  273. if ((filename = konf_query__get_path(query))) {
  274. if (!(fd = fopen(filename, "w")))
  275. return -1;
  276. } else {
  277. if ((dupsock = dup(sock)) < 0)
  278. return -1;
  279. fd = fdopen(dupsock, "w");
  280. }
  281. if (!filename) {
  282. fprintf(fd, "-t\n");
  283. #ifdef DEBUG
  284. fprintf(stderr, "ANSWER: -t\n");
  285. #endif
  286. }
  287. konf_tree_fprintf(conf,
  288. fd,
  289. konf_query__get_pattern(query),
  290. konf_query__get_pwdc(query) - 1,
  291. konf_query__get_seq(query),
  292. 0);
  293. if (!filename) {
  294. fprintf(fd, "\n");
  295. #ifdef DEBUG
  296. fprintf(stderr, "SEND DATA: \n");
  297. #endif
  298. }
  299. fclose(fd);
  300. return 0;
  301. }