1
0

konfd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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/private.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 = lub_string_dup("-e");
  249. lub_string_free(str);
  250. answer_send(i, answer);
  251. lub_string_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;
  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. if (konf_tree_find_conf(iconf,
  325. konf_query__get_line(query), 0, 0)) {
  326. ret = KONF_QUERY_OP_OK;
  327. break;
  328. }
  329. konf_tree_del_pattern(iconf,
  330. konf_query__get_pattern(query),
  331. konf_query__get_priority(query),
  332. konf_query__get_seq(query),
  333. konf_query__get_seq_num(query));
  334. }
  335. tmpconf = konf_tree_new_conf(iconf,
  336. konf_query__get_line(query), konf_query__get_priority(query),
  337. konf_query__get_seq(query), konf_query__get_seq_num(query));
  338. if (!tmpconf) {
  339. ret = KONF_QUERY_OP_ERROR;
  340. break;
  341. }
  342. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  343. konf_tree__set_depth(tmpconf, konf_query__get_pwdc(query));
  344. ret = KONF_QUERY_OP_OK;
  345. break;
  346. case KONF_QUERY_OP_UNSET:
  347. konf_tree_del_pattern(iconf,
  348. konf_query__get_pattern(query), konf_query__get_priority(query),
  349. konf_query__get_seq(query), konf_query__get_seq_num(query));
  350. ret = KONF_QUERY_OP_OK;
  351. break;
  352. case KONF_QUERY_OP_DUMP:
  353. if (dump_running_config(sock, iconf, query))
  354. ret = KONF_QUERY_OP_ERROR;
  355. else
  356. ret = KONF_QUERY_OP_OK;
  357. break;
  358. default:
  359. ret = KONF_QUERY_OP_ERROR;
  360. break;
  361. }
  362. #ifdef DEBUG
  363. /* Print whole tree */
  364. konf_tree_fprintf(conf, stderr, NULL, -1, BOOL_TRUE, 0);
  365. #endif
  366. /* Free resources */
  367. konf_query_free(query);
  368. switch (ret) {
  369. case KONF_QUERY_OP_OK:
  370. lub_string_cat(&retval, "-o");
  371. break;
  372. case KONF_QUERY_OP_ERROR:
  373. lub_string_cat(&retval, "-e");
  374. break;
  375. default:
  376. lub_string_cat(&retval, "-e");
  377. break;
  378. };
  379. return retval;
  380. }
  381. /*--------------------------------------------------------- */
  382. /*
  383. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  384. */
  385. static void sighandler(int signo)
  386. {
  387. sigterm = 1;
  388. }
  389. /*--------------------------------------------------------- */
  390. int answer_send(int sock, char *command)
  391. {
  392. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  393. }
  394. /*--------------------------------------------------------- */
  395. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  396. {
  397. FILE *fd;
  398. char *filename;
  399. int dupsock = -1;
  400. if ((filename = konf_query__get_path(query))) {
  401. if (!(fd = fopen(filename, "w")))
  402. return -1;
  403. } else {
  404. if ((dupsock = dup(sock)) < 0)
  405. return -1;
  406. fd = fdopen(dupsock, "w");
  407. }
  408. if (!filename) {
  409. fprintf(fd, "-t\n");
  410. #ifdef DEBUG
  411. fprintf(stderr, "ANSWER: -t\n");
  412. #endif
  413. }
  414. konf_tree_fprintf(conf,
  415. fd,
  416. konf_query__get_pattern(query),
  417. konf_query__get_pwdc(query) - 1,
  418. konf_query__get_seq(query),
  419. 0);
  420. if (!filename) {
  421. fprintf(fd, "\n");
  422. #ifdef DEBUG
  423. fprintf(stderr, "SEND DATA: \n");
  424. #endif
  425. }
  426. fclose(fd);
  427. return 0;
  428. }
  429. /*--------------------------------------------------------- */
  430. /* Implement own simple daemon() to don't use Non-POSIX */
  431. int daemonize(int nochdir, int noclose)
  432. {
  433. int fd;
  434. int pid;
  435. pid = fork();
  436. if (-1 == pid)
  437. return -1;
  438. if (pid > 0)
  439. _exit(0); /* Exit parent */
  440. if (setsid() == -1)
  441. return -1;
  442. if (!nochdir)
  443. chdir("/");
  444. if (!noclose) {
  445. fd = open("/dev/null", O_RDWR, 0);
  446. if (fd < 0)
  447. return -1;
  448. dup2(fd, STDIN_FILENO);
  449. dup2(fd, STDOUT_FILENO);
  450. dup2(fd, STDERR_FILENO);
  451. if (fd > 2)
  452. close(fd);
  453. }
  454. return 0;
  455. }
  456. /*--------------------------------------------------------- */
  457. /* Initialize option structure by defaults */
  458. struct options *opts_init(void)
  459. {
  460. struct options *opts = NULL;
  461. opts = malloc(sizeof(*opts));
  462. assert(opts);
  463. opts->debug = 0; /* daemonize by default */
  464. opts->socket_path = lub_string_dup(KONFD_SOCKET_PATH);
  465. opts->pidfile = lub_string_dup(KONFD_PIDFILE);
  466. opts->chroot = NULL;
  467. opts->uid = getuid();
  468. opts->gid = getgid();
  469. return opts;
  470. }
  471. /*--------------------------------------------------------- */
  472. /* Free option structure */
  473. void opts_free(struct options *opts)
  474. {
  475. if (opts->socket_path)
  476. lub_string_free(opts->socket_path);
  477. if (opts->pidfile)
  478. lub_string_free(opts->pidfile);
  479. if (opts->chroot)
  480. lub_string_free(opts->chroot);
  481. free(opts);
  482. }
  483. /*--------------------------------------------------------- */
  484. /* Parse command line options */
  485. static int opts_parse(int argc, char *argv[], struct options *opts)
  486. {
  487. static const char *shortopts = "hvs:p:u:g:dr:";
  488. #ifdef HAVE_GETOPT_H
  489. static const struct option longopts[] = {
  490. {"help", 0, NULL, 'h'},
  491. {"version", 0, NULL, 'v'},
  492. {"socket", 1, NULL, 's'},
  493. {"pid", 1, NULL, 'p'},
  494. {"user", 1, NULL, 'u'},
  495. {"group", 1, NULL, 'g'},
  496. {"debug", 0, NULL, 'd'},
  497. {"chroot", 1, NULL, 'r'},
  498. {NULL, 0, NULL, 0}
  499. };
  500. #endif
  501. optind = 0;
  502. while(1) {
  503. int opt;
  504. #ifdef HAVE_GETOPT_H
  505. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  506. #else
  507. opt = getopt(argc, argv, shortopts);
  508. #endif
  509. if (-1 == opt)
  510. break;
  511. switch (opt) {
  512. case 's':
  513. if (opts->socket_path)
  514. lub_string_free(opts->socket_path);
  515. opts->socket_path = lub_string_dup(optarg);
  516. break;
  517. case 'p':
  518. if (opts->pidfile)
  519. lub_string_free(opts->pidfile);
  520. opts->pidfile = lub_string_dup(optarg);
  521. break;
  522. case 'r':
  523. #ifdef HAVE_CHROOT
  524. if (opts->chroot)
  525. lub_string_free(opts->chroot);
  526. opts->chroot = lub_string_dup(optarg);
  527. #else
  528. syslog(LOG_ERR, "The --chroot option is not supported\n");
  529. return -1;
  530. #endif
  531. break;
  532. case 'd':
  533. opts->debug = 1;
  534. break;
  535. case 'u': {
  536. #ifdef HAVE_PWD_H
  537. struct passwd *pwd = getpwnam(optarg);
  538. if (!pwd) {
  539. syslog(LOG_ERR, "Can't identify user \"%s\"\n",
  540. optarg);
  541. return -1;
  542. }
  543. opts->uid = pwd->pw_uid;
  544. #else
  545. syslog(LOG_ERR, "The --user option is not supported\n");
  546. return -1;
  547. #endif
  548. break;
  549. }
  550. case 'g': {
  551. #ifdef HAVE_GRP_H
  552. struct group *grp = getgrnam(optarg);
  553. if (!grp) {
  554. syslog(LOG_ERR, "Can't identify group \"%s\"\n",
  555. optarg);
  556. return -1;
  557. }
  558. opts->gid = grp->gr_gid;
  559. #else
  560. syslog(LOG_ERR, "The --group option is not supported\n");
  561. return -1;
  562. #endif
  563. break;
  564. }
  565. case 'h':
  566. help(0, argv[0]);
  567. exit(0);
  568. break;
  569. case 'v':
  570. version(VERSION);
  571. exit(0);
  572. break;
  573. default:
  574. help(-1, argv[0]);
  575. exit(-1);
  576. break;
  577. }
  578. }
  579. return 0;
  580. }
  581. /*--------------------------------------------------------- */
  582. /* Print help message */
  583. static void help(int status, const char *argv0)
  584. {
  585. const char *name = NULL;
  586. if (!argv0)
  587. return;
  588. /* Find the basename */
  589. name = strrchr(argv0, '/');
  590. if (name)
  591. name++;
  592. else
  593. name = argv0;
  594. if (status != 0) {
  595. fprintf(stderr, "Try `%s -h' for more information.\n",
  596. name);
  597. } else {
  598. printf("Usage: %s [options]\n", name);
  599. printf("Daemon to store user configuration (i.e. commands). "
  600. "The part of the klish project.\n");
  601. printf("Options:\n");
  602. printf("\t-v, --version\tPrint version.\n");
  603. printf("\t-h, --help\tPrint this help.\n");
  604. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  605. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  606. "filesystem path to listen on.\n");
  607. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  608. printf("\t-r <path>, --chroot=<path>\tDirectory to chroot.\n");
  609. printf("\t-u <user>, --user=<user>\tExecute process as"
  610. " specified user.\n");
  611. printf("\t-g <group>, --group=<group>\tExecute process as"
  612. " specified group.\n");
  613. }
  614. }