konfd.c 9.3 KB

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