konfd.c 9.3 KB

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