1
0

konfd.c 9.4 KB

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