1
0

konfd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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/stat.h>
  15. #include <fcntl.h>
  16. #include <sys/wait.h>
  17. #include <errno.h>
  18. #include <assert.h>
  19. #include <sys/socket.h>
  20. #include <sys/un.h>
  21. #include <string.h>
  22. #include <sys/select.h>
  23. #include <signal.h>
  24. #include <syslog.h>
  25. #ifdef HAVE_GETOPT_H
  26. #include <getopt.h>
  27. #endif
  28. #ifdef HAVE_PWD_H
  29. #include <pwd.h>
  30. #endif
  31. #ifdef HAVE_GRP_H
  32. #include <grp.h>
  33. #endif
  34. #include "clish/internal.h"
  35. #include "konf/tree.h"
  36. #include "konf/query.h"
  37. #include "konf/buf.h"
  38. #include "lub/argv.h"
  39. #include "lub/string.h"
  40. #ifndef VERSION
  41. #define VERSION 1.2.2
  42. #endif
  43. #define QUOTE(t) #t
  44. #define version(v) printf("%s\n", v)
  45. #define KONFD_PIDFILE "/var/run/konfd.pid"
  46. /* UNIX socket path */
  47. #ifndef UNIX_PATH_MAX
  48. #define UNIX_PATH_MAX 108
  49. #endif
  50. /* OpenBSD has no MSG_NOSIGNAL flag */
  51. #ifndef MSG_NOSIGNAL
  52. #define MSG_NOSIGNAL 0
  53. #endif
  54. #define MAXMSG 1024
  55. /* Global signal vars */
  56. static volatile int sigterm = 0;
  57. static void sighandler(int signo);
  58. static void help(int status, const char *argv0);
  59. static char * process_query(int sock, konf_tree_t * conf, char *str);
  60. int answer_send(int sock, char *command);
  61. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query);
  62. int daemonize(int nochdir, int noclose);
  63. struct options *opts_init(void);
  64. void opts_free(struct options *opts);
  65. static int opts_parse(int argc, char *argv[], struct options *opts);
  66. /* Command line options */
  67. struct options {
  68. char *socket_path;
  69. char *pidfile;
  70. char *chroot;
  71. int debug; /* Don't daemonize in debug mode */
  72. uid_t uid;
  73. gid_t gid;
  74. };
  75. /*--------------------------------------------------------- */
  76. int main(int argc, char **argv)
  77. {
  78. int retval = -1;
  79. unsigned i;
  80. char *str;
  81. konf_tree_t *conf;
  82. lub_bintree_t bufs;
  83. konf_buf_t *tbuf;
  84. struct options *opts = NULL;
  85. int pidfd = -1;
  86. /* Network vars */
  87. int sock = -1;
  88. struct sockaddr_un laddr;
  89. struct sockaddr_un raddr;
  90. fd_set active_fd_set, read_fd_set;
  91. const int reuseaddr = 1;
  92. /* Signal vars */
  93. struct sigaction sig_act, sigpipe_act;
  94. sigset_t sig_set, sigpipe_set;
  95. /* Initialize syslog */
  96. openlog(argv[0], LOG_CONS, LOG_DAEMON);
  97. /* Parse command line options */
  98. opts = opts_init();
  99. if (opts_parse(argc, argv, opts))
  100. goto err;
  101. /* Fork the daemon */
  102. if (!opts->debug) {
  103. /* Daemonize */
  104. if (daemonize(0, 0) < 0) {
  105. syslog(LOG_ERR, "Can't daemonize\n");
  106. goto err;
  107. }
  108. /* Write pidfile */
  109. if ((pidfd = open(opts->pidfile,
  110. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  111. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  112. syslog(LOG_WARNING, "Can't open pidfile %s: %s",
  113. opts->pidfile, strerror(errno));
  114. } else {
  115. char str[20];
  116. snprintf(str, sizeof(str), "%u\n", getpid());
  117. if (write(pidfd, str, strlen(str)) < 0)
  118. syslog(LOG_WARNING, "Can't write to %s: %s",
  119. opts->pidfile, strerror(errno));
  120. close(pidfd);
  121. }
  122. }
  123. /* Create listen socket */
  124. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  125. syslog(LOG_ERR, "Can't create listen socket: %s\n",
  126. strerror(errno));
  127. goto err;
  128. }
  129. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  130. &reuseaddr, sizeof(reuseaddr))) {
  131. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  132. goto err;
  133. }
  134. laddr.sun_family = AF_UNIX;
  135. strncpy(laddr.sun_path, opts->socket_path, UNIX_PATH_MAX);
  136. laddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  137. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  138. syslog(LOG_ERR, "Can't bind socket: %s\n",
  139. strerror(errno));
  140. goto err;
  141. }
  142. if (chown(opts->socket_path, opts->uid, opts->gid)) {
  143. syslog(LOG_ERR, "Can't chown UNIX socket: %s\n",
  144. strerror(errno));
  145. goto err;
  146. }
  147. listen(sock, 5);
  148. /* Change GID */
  149. if (opts->gid != getgid()) {
  150. if (setgid(opts->gid)) {
  151. syslog(LOG_ERR, "Can't set GID to %u: %s",
  152. opts->gid, strerror(errno));
  153. goto err;
  154. }
  155. }
  156. #ifdef HAVE_CHROOT
  157. /* Chroot */
  158. if (opts->chroot) {
  159. if (chroot(opts->chroot) < 0) {
  160. syslog(LOG_ERR, "Can't chroot to %s: %s",
  161. opts->chroot, strerror(errno));
  162. goto err;
  163. }
  164. }
  165. #endif
  166. /* Change UID */
  167. if (opts->uid != getuid()) {
  168. if (setuid(opts->uid)) {
  169. syslog(LOG_ERR, "Can't set UID to %u: %s",
  170. opts->uid, strerror(errno));
  171. goto err;
  172. }
  173. }
  174. /* Create configuration tree */
  175. conf = konf_tree_new("", 0);
  176. /* Initialize the tree of buffers */
  177. lub_bintree_init(&bufs,
  178. konf_buf_bt_offset(),
  179. konf_buf_bt_compare, konf_buf_bt_getkey);
  180. /* Set signal handler */
  181. sigemptyset(&sig_set);
  182. sigaddset(&sig_set, SIGTERM);
  183. sigaddset(&sig_set, SIGINT);
  184. sigaddset(&sig_set, SIGQUIT);
  185. sig_act.sa_flags = 0;
  186. sig_act.sa_mask = sig_set;
  187. sig_act.sa_handler = &sighandler;
  188. sigaction(SIGTERM, &sig_act, NULL);
  189. sigaction(SIGINT, &sig_act, NULL);
  190. sigaction(SIGQUIT, &sig_act, NULL);
  191. /* Ignore SIGPIPE */
  192. sigemptyset(&sigpipe_set);
  193. sigaddset(&sigpipe_set, SIGPIPE);
  194. sigpipe_act.sa_flags = 0;
  195. sigpipe_act.sa_mask = sigpipe_set;
  196. sigpipe_act.sa_handler = SIG_IGN;
  197. sigaction(SIGPIPE, &sigpipe_act, NULL);
  198. /* Initialize the set of active sockets. */
  199. FD_ZERO(&active_fd_set);
  200. FD_SET(sock, &active_fd_set);
  201. /* Main loop */
  202. while (!sigterm) {
  203. int num;
  204. /* Block until input arrives on one or more active sockets. */
  205. read_fd_set = active_fd_set;
  206. num = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
  207. if (num < 0) {
  208. if (EINTR == errno)
  209. continue;
  210. break;
  211. }
  212. if (0 == num)
  213. continue;
  214. /* Service all the sockets with input pending. */
  215. for (i = 0; i < FD_SETSIZE; ++i) {
  216. if (!FD_ISSET(i, &read_fd_set))
  217. continue;
  218. if (i == sock) {
  219. /* Connection request on listen socket. */
  220. int new;
  221. socklen_t size = sizeof(raddr);
  222. new = accept(sock,
  223. (struct sockaddr *)&raddr, &size);
  224. if (new < 0) {
  225. fprintf(stderr, "accept");
  226. continue;
  227. }
  228. #ifdef DEBUG
  229. fprintf(stderr, "Connection established %u\n", new);
  230. #endif
  231. konf_buftree_remove(&bufs, new);
  232. tbuf = konf_buf_new(new);
  233. /* insert it into the binary tree for this conf */
  234. lub_bintree_insert(&bufs, tbuf);
  235. FD_SET(new, &active_fd_set);
  236. } else {
  237. int nbytes;
  238. /* Data arriving on an already-connected socket. */
  239. if ((nbytes = konf_buftree_read(&bufs, i)) <= 0) {
  240. close(i);
  241. FD_CLR(i, &active_fd_set);
  242. konf_buftree_remove(&bufs, i);
  243. continue;
  244. }
  245. while ((str = konf_buftree_parse(&bufs, i))) {
  246. char *answer;
  247. if (!(answer = process_query(i, conf, str)))
  248. answer = strdup("-e");
  249. free(str);
  250. answer_send(i, answer);
  251. free(answer);
  252. }
  253. }
  254. }
  255. }
  256. /* Free resources */
  257. konf_tree_delete(conf);
  258. /* delete each buf */
  259. while ((tbuf = lub_bintree_findfirst(&bufs))) {
  260. /* remove the buf from the tree */
  261. lub_bintree_remove(&bufs, tbuf);
  262. /* release the instance */
  263. konf_buf_delete(tbuf);
  264. }
  265. retval = 0;
  266. err:
  267. /* Close listen socket */
  268. if (sock >= 0) {
  269. close(sock);
  270. unlink(opts->socket_path);
  271. }
  272. /* Remove pidfile */
  273. if (pidfd >= 0) {
  274. if (unlink(opts->pidfile) < 0) {
  275. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  276. opts->pidfile, strerror(errno));
  277. }
  278. }
  279. /* Free command line options */
  280. opts_free(opts);
  281. return retval;
  282. }
  283. /*--------------------------------------------------------- */
  284. static char * process_query(int sock, konf_tree_t * conf, char *str)
  285. {
  286. unsigned i;
  287. int res;
  288. konf_tree_t *iconf;
  289. konf_tree_t *tmpconf;
  290. konf_query_t *query;
  291. char *retval = NULL;
  292. konf_query_op_t ret = KONF_QUERY_OP_ERROR;
  293. #ifdef DEBUG
  294. fprintf(stderr, "----------------------\n");
  295. fprintf(stderr, "REQUEST: %s\n", str);
  296. #endif
  297. /* Parse query */
  298. query = konf_query_new();
  299. res = konf_query_parse_str(query, str);
  300. if (res < 0) {
  301. konf_query_free(query);
  302. return NULL;
  303. }
  304. #ifdef DEBUG
  305. konf_query_dump(query);
  306. #endif
  307. /* Go through the pwd */
  308. iconf = conf;
  309. for (i = 0; i < konf_query__get_pwdc(query); i++) {
  310. if (!(iconf = konf_tree_find_conf(iconf,
  311. konf_query__get_pwd(query, i), 0, 0))) {
  312. iconf = NULL;
  313. break;
  314. }
  315. }
  316. if (!iconf) {
  317. fprintf(stderr, "Unknown path\n");
  318. konf_query_free(query);
  319. return NULL;
  320. }
  321. switch (konf_query__get_op(query)) {
  322. case KONF_QUERY_OP_SET:
  323. if (konf_query__get_unique(query)) {
  324. int exist = 0;
  325. exist = konf_tree_del_pattern(iconf,
  326. konf_query__get_line(query),
  327. konf_query__get_unique(query),
  328. konf_query__get_pattern(query),
  329. konf_query__get_priority(query),
  330. konf_query__get_seq(query),
  331. konf_query__get_seq_num(query));
  332. if (exist < 0)
  333. break;
  334. if (exist > 0) {
  335. ret = KONF_QUERY_OP_OK;
  336. break;
  337. }
  338. }
  339. tmpconf = konf_tree_new_conf(iconf,
  340. konf_query__get_line(query), konf_query__get_priority(query),
  341. konf_query__get_seq(query), konf_query__get_seq_num(query));
  342. if (!tmpconf)
  343. break;
  344. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  345. konf_tree__set_depth(tmpconf, konf_query__get_pwdc(query));
  346. ret = KONF_QUERY_OP_OK;
  347. break;
  348. case KONF_QUERY_OP_UNSET:
  349. if (konf_tree_del_pattern(iconf,
  350. NULL,
  351. BOOL_TRUE,
  352. konf_query__get_pattern(query),
  353. konf_query__get_priority(query),
  354. konf_query__get_seq(query),
  355. konf_query__get_seq_num(query)) < 0)
  356. break;
  357. ret = KONF_QUERY_OP_OK;
  358. break;
  359. case KONF_QUERY_OP_DUMP:
  360. if (dump_running_config(sock, iconf, query))
  361. break;
  362. ret = KONF_QUERY_OP_OK;
  363. break;
  364. default:
  365. break;
  366. }
  367. #ifdef DEBUG
  368. /* Print whole tree */
  369. konf_tree_fprintf(conf, stderr, NULL, -1, BOOL_TRUE, 0);
  370. #endif
  371. /* Free resources */
  372. konf_query_free(query);
  373. switch (ret) {
  374. case KONF_QUERY_OP_OK:
  375. lub_string_cat(&retval, "-o");
  376. break;
  377. case KONF_QUERY_OP_ERROR:
  378. lub_string_cat(&retval, "-e");
  379. break;
  380. default:
  381. lub_string_cat(&retval, "-e");
  382. break;
  383. };
  384. #ifdef DEBUG
  385. fprintf(stderr, "ANSWER: %s\n", retval);
  386. #endif
  387. return retval;
  388. }
  389. /*--------------------------------------------------------- */
  390. /*
  391. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  392. */
  393. static void sighandler(int signo)
  394. {
  395. sigterm = 1;
  396. }
  397. /*--------------------------------------------------------- */
  398. int answer_send(int sock, char *command)
  399. {
  400. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  401. }
  402. /*--------------------------------------------------------- */
  403. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  404. {
  405. FILE *fd;
  406. char *filename;
  407. int dupsock = -1;
  408. if ((filename = konf_query__get_path(query))) {
  409. if (!(fd = fopen(filename, "w")))
  410. return -1;
  411. } else {
  412. if ((dupsock = dup(sock)) < 0)
  413. return -1;
  414. fd = fdopen(dupsock, "w");
  415. }
  416. if (!filename) {
  417. fprintf(fd, "-t\n");
  418. #ifdef DEBUG
  419. fprintf(stderr, "ANSWER: -t\n");
  420. #endif
  421. }
  422. konf_tree_fprintf(conf,
  423. fd,
  424. konf_query__get_pattern(query),
  425. konf_query__get_pwdc(query) - 1,
  426. konf_query__get_seq(query),
  427. 0);
  428. if (!filename) {
  429. fprintf(fd, "\n");
  430. #ifdef DEBUG
  431. fprintf(stderr, "SEND DATA: \n");
  432. #endif
  433. }
  434. fclose(fd);
  435. return 0;
  436. }
  437. /*--------------------------------------------------------- */
  438. /* Implement own simple daemon() to don't use Non-POSIX */
  439. int daemonize(int nochdir, int noclose)
  440. {
  441. int fd;
  442. int pid;
  443. pid = fork();
  444. if (-1 == pid)
  445. return -1;
  446. if (pid > 0)
  447. _exit(0); /* Exit parent */
  448. if (setsid() == -1)
  449. return -1;
  450. if (!nochdir)
  451. chdir("/");
  452. if (!noclose) {
  453. fd = open("/dev/null", O_RDWR, 0);
  454. if (fd < 0)
  455. return -1;
  456. dup2(fd, STDIN_FILENO);
  457. dup2(fd, STDOUT_FILENO);
  458. dup2(fd, STDERR_FILENO);
  459. if (fd > 2)
  460. close(fd);
  461. }
  462. return 0;
  463. }
  464. /*--------------------------------------------------------- */
  465. /* Initialize option structure by defaults */
  466. struct options *opts_init(void)
  467. {
  468. struct options *opts = NULL;
  469. opts = malloc(sizeof(*opts));
  470. assert(opts);
  471. opts->debug = 0; /* daemonize by default */
  472. opts->socket_path = strdup(KONFD_SOCKET_PATH);
  473. opts->pidfile = strdup(KONFD_PIDFILE);
  474. opts->chroot = NULL;
  475. opts->uid = getuid();
  476. opts->gid = getgid();
  477. return opts;
  478. }
  479. /*--------------------------------------------------------- */
  480. /* Free option structure */
  481. void opts_free(struct options *opts)
  482. {
  483. if (opts->socket_path)
  484. free(opts->socket_path);
  485. if (opts->pidfile)
  486. free(opts->pidfile);
  487. if (opts->chroot)
  488. free(opts->chroot);
  489. free(opts);
  490. }
  491. /*--------------------------------------------------------- */
  492. /* Parse command line options */
  493. static int opts_parse(int argc, char *argv[], struct options *opts)
  494. {
  495. static const char *shortopts = "hvs:p:u:g:dr:";
  496. #ifdef HAVE_GETOPT_H
  497. static const struct option longopts[] = {
  498. {"help", 0, NULL, 'h'},
  499. {"version", 0, NULL, 'v'},
  500. {"socket", 1, NULL, 's'},
  501. {"pid", 1, NULL, 'p'},
  502. {"user", 1, NULL, 'u'},
  503. {"group", 1, NULL, 'g'},
  504. {"debug", 0, NULL, 'd'},
  505. {"chroot", 1, NULL, 'r'},
  506. {NULL, 0, NULL, 0}
  507. };
  508. #endif
  509. optind = 0;
  510. while(1) {
  511. int opt;
  512. #ifdef HAVE_GETOPT_H
  513. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  514. #else
  515. opt = getopt(argc, argv, shortopts);
  516. #endif
  517. if (-1 == opt)
  518. break;
  519. switch (opt) {
  520. case 's':
  521. if (opts->socket_path)
  522. free(opts->socket_path);
  523. opts->socket_path = strdup(optarg);
  524. break;
  525. case 'p':
  526. if (opts->pidfile)
  527. free(opts->pidfile);
  528. opts->pidfile = strdup(optarg);
  529. break;
  530. case 'r':
  531. #ifdef HAVE_CHROOT
  532. if (opts->chroot)
  533. free(opts->chroot);
  534. opts->chroot = strdup(optarg);
  535. #else
  536. syslog(LOG_ERR, "The --chroot option is not supported\n");
  537. return -1;
  538. #endif
  539. break;
  540. case 'd':
  541. opts->debug = 1;
  542. break;
  543. case 'u': {
  544. #ifdef HAVE_PWD_H
  545. struct passwd *pwd = getpwnam(optarg);
  546. if (!pwd) {
  547. syslog(LOG_ERR, "Can't identify user \"%s\"\n",
  548. optarg);
  549. return -1;
  550. }
  551. opts->uid = pwd->pw_uid;
  552. #else
  553. syslog(LOG_ERR, "The --user option is not supported\n");
  554. return -1;
  555. #endif
  556. break;
  557. }
  558. case 'g': {
  559. #ifdef HAVE_GRP_H
  560. struct group *grp = getgrnam(optarg);
  561. if (!grp) {
  562. syslog(LOG_ERR, "Can't identify group \"%s\"\n",
  563. optarg);
  564. return -1;
  565. }
  566. opts->gid = grp->gr_gid;
  567. #else
  568. syslog(LOG_ERR, "The --group option is not supported\n");
  569. return -1;
  570. #endif
  571. break;
  572. }
  573. case 'h':
  574. help(0, argv[0]);
  575. exit(0);
  576. break;
  577. case 'v':
  578. version(VERSION);
  579. exit(0);
  580. break;
  581. default:
  582. help(-1, argv[0]);
  583. exit(-1);
  584. break;
  585. }
  586. }
  587. return 0;
  588. }
  589. /*--------------------------------------------------------- */
  590. /* Print help message */
  591. static void help(int status, const char *argv0)
  592. {
  593. const char *name = NULL;
  594. if (!argv0)
  595. return;
  596. /* Find the basename */
  597. name = strrchr(argv0, '/');
  598. if (name)
  599. name++;
  600. else
  601. name = argv0;
  602. if (status != 0) {
  603. fprintf(stderr, "Try `%s -h' for more information.\n",
  604. name);
  605. } else {
  606. printf("Usage: %s [options]\n", name);
  607. printf("Daemon to store user configuration (i.e. commands). "
  608. "The part of the klish project.\n");
  609. printf("Options:\n");
  610. printf("\t-v, --version\tPrint version.\n");
  611. printf("\t-h, --help\tPrint this help.\n");
  612. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  613. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  614. "filesystem path to listen on.\n");
  615. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  616. printf("\t-r <path>, --chroot=<path>\tDirectory to chroot.\n");
  617. printf("\t-u <user>, --user=<user>\tExecute process as"
  618. " specified user.\n");
  619. printf("\t-g <group>, --group=<group>\tExecute process as"
  620. " specified group.\n");
  621. }
  622. }