konfd.c 7.3 KB

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