1
0

konfd.c 15 KB

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