konfd.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /* Configuration tree */
  118. conf = konf_tree_new("", 0);
  119. /* Initialize the tree of buffers */
  120. lub_bintree_init(&bufs,
  121. konf_buf_bt_offset(),
  122. konf_buf_bt_compare, konf_buf_bt_getkey);
  123. /* Create listen socket */
  124. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  125. fprintf(stderr, "Cannot create socket: %s\n", strerror(errno));
  126. /* syslog(LOG_ERR, "Cannot create socket: %s\n", strerror(errno));
  127. */ return -1;
  128. }
  129. unlink(socket_path);
  130. laddr.sun_family = AF_UNIX;
  131. strncpy(laddr.sun_path, socket_path, UNIX_PATH_MAX);
  132. laddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  133. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  134. fprintf(stderr, "Can't bind()\n");
  135. /* syslog(LOG_ERR, "Can't bind()\n");
  136. */ return -1;
  137. }
  138. listen(sock, 5);
  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. return retval;
  206. }
  207. /*--------------------------------------------------------- */
  208. static char * process_query(int sock, konf_tree_t * conf, char *str)
  209. {
  210. unsigned i;
  211. int res;
  212. konf_tree_t *iconf;
  213. konf_tree_t *tmpconf;
  214. konf_query_t *query;
  215. char *retval = NULL;
  216. konf_query_op_t ret;
  217. #ifdef DEBUG
  218. fprintf(stderr, "----------------------\n");
  219. fprintf(stderr, "REQUEST: %s\n", str);
  220. #endif
  221. /* Parse query */
  222. query = konf_query_new();
  223. res = konf_query_parse_str(query, str);
  224. if (res < 0) {
  225. konf_query_free(query);
  226. return NULL;
  227. }
  228. #ifdef DEBUG
  229. konf_query_dump(query);
  230. #endif
  231. /* Go through the pwd */
  232. iconf = conf;
  233. for (i = 0; i < konf_query__get_pwdc(query); i++) {
  234. if (!
  235. (iconf =
  236. konf_tree_find_conf(iconf, konf_query__get_pwd(query, i), 0, 0))) {
  237. iconf = NULL;
  238. break;
  239. }
  240. }
  241. if (!iconf) {
  242. fprintf(stderr, "Unknown path\n");
  243. konf_query_free(query);
  244. return NULL;
  245. }
  246. switch (konf_query__get_op(query)) {
  247. case KONF_QUERY_OP_SET:
  248. if (konf_query__get_unique(query)) {
  249. if (konf_tree_find_conf(iconf,
  250. konf_query__get_line(query), 0, 0)) {
  251. ret = KONF_QUERY_OP_OK;
  252. break;
  253. }
  254. konf_tree_del_pattern(iconf,
  255. konf_query__get_pattern(query),
  256. konf_query__get_priority(query),
  257. konf_query__get_seq(query),
  258. konf_query__get_seq_num(query));
  259. }
  260. tmpconf = konf_tree_new_conf(iconf,
  261. konf_query__get_line(query), konf_query__get_priority(query),
  262. konf_query__get_seq(query), konf_query__get_seq_num(query));
  263. if (!tmpconf) {
  264. ret = KONF_QUERY_OP_ERROR;
  265. break;
  266. }
  267. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  268. konf_tree__set_depth(tmpconf, konf_query__get_pwdc(query));
  269. ret = KONF_QUERY_OP_OK;
  270. break;
  271. case KONF_QUERY_OP_UNSET:
  272. konf_tree_del_pattern(iconf,
  273. konf_query__get_pattern(query), konf_query__get_priority(query),
  274. konf_query__get_seq(query), konf_query__get_seq_num(query));
  275. ret = KONF_QUERY_OP_OK;
  276. break;
  277. case KONF_QUERY_OP_DUMP:
  278. if (dump_running_config(sock, iconf, query))
  279. ret = KONF_QUERY_OP_ERROR;
  280. else
  281. ret = KONF_QUERY_OP_OK;
  282. break;
  283. default:
  284. ret = KONF_QUERY_OP_ERROR;
  285. break;
  286. }
  287. #ifdef DEBUG
  288. /* Print whole tree */
  289. konf_tree_fprintf(conf, stderr, NULL, -1, BOOL_TRUE, 0);
  290. #endif
  291. /* Free resources */
  292. konf_query_free(query);
  293. switch (ret) {
  294. case KONF_QUERY_OP_OK:
  295. lub_string_cat(&retval, "-o");
  296. break;
  297. case KONF_QUERY_OP_ERROR:
  298. lub_string_cat(&retval, "-e");
  299. break;
  300. default:
  301. lub_string_cat(&retval, "-e");
  302. break;
  303. };
  304. return retval;
  305. }
  306. /*--------------------------------------------------------- */
  307. /*
  308. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  309. */
  310. static void sighandler(int signo)
  311. {
  312. sigterm = 1;
  313. }
  314. /*--------------------------------------------------------- */
  315. int answer_send(int sock, char *command)
  316. {
  317. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  318. }
  319. /*--------------------------------------------------------- */
  320. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  321. {
  322. FILE *fd;
  323. char *filename;
  324. int dupsock = -1;
  325. if ((filename = konf_query__get_path(query))) {
  326. if (!(fd = fopen(filename, "w")))
  327. return -1;
  328. } else {
  329. if ((dupsock = dup(sock)) < 0)
  330. return -1;
  331. fd = fdopen(dupsock, "w");
  332. }
  333. if (!filename) {
  334. fprintf(fd, "-t\n");
  335. #ifdef DEBUG
  336. fprintf(stderr, "ANSWER: -t\n");
  337. #endif
  338. }
  339. konf_tree_fprintf(conf,
  340. fd,
  341. konf_query__get_pattern(query),
  342. konf_query__get_pwdc(query) - 1,
  343. konf_query__get_seq(query),
  344. 0);
  345. if (!filename) {
  346. fprintf(fd, "\n");
  347. #ifdef DEBUG
  348. fprintf(stderr, "SEND DATA: \n");
  349. #endif
  350. }
  351. fclose(fd);
  352. return 0;
  353. }
  354. /*--------------------------------------------------------- */
  355. /* Print help message */
  356. static void help(int status, const char *argv0)
  357. {
  358. const char *name = NULL;
  359. if (!argv0)
  360. return;
  361. /* Find the basename */
  362. name = strrchr(argv0, '/');
  363. if (name)
  364. name++;
  365. else
  366. name = argv0;
  367. if (status != 0) {
  368. fprintf(stderr, "Try `%s -h' for more information.\n",
  369. name);
  370. } else {
  371. printf("Usage: %s [options]\n", name);
  372. printf("Daemon to store user configuration (i.e. commands). "
  373. "The part of the klish project.\n");
  374. printf("Options:\n");
  375. printf("\t-v, --version\tPrint version.\n");
  376. printf("\t-h, --help\tPrint this help.\n");
  377. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  378. "filesystem path to listen on.\n");
  379. }
  380. }