1
0

konfd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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, -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_depth(query),
  427. konf_query__get_seq(query),
  428. 0);
  429. if (!filename) {
  430. fprintf(fd, "\n");
  431. #ifdef DEBUG
  432. fprintf(stderr, "SEND DATA: \n");
  433. #endif
  434. }
  435. fclose(fd);
  436. return 0;
  437. }
  438. /*--------------------------------------------------------- */
  439. /* Implement own simple daemon() to don't use Non-POSIX */
  440. int daemonize(int nochdir, int noclose)
  441. {
  442. int fd;
  443. int pid;
  444. pid = fork();
  445. if (-1 == pid)
  446. return -1;
  447. if (pid > 0)
  448. _exit(0); /* Exit parent */
  449. if (setsid() == -1)
  450. return -1;
  451. if (!nochdir)
  452. chdir("/");
  453. if (!noclose) {
  454. fd = open("/dev/null", O_RDWR, 0);
  455. if (fd < 0)
  456. return -1;
  457. dup2(fd, STDIN_FILENO);
  458. dup2(fd, STDOUT_FILENO);
  459. dup2(fd, STDERR_FILENO);
  460. if (fd > 2)
  461. close(fd);
  462. }
  463. return 0;
  464. }
  465. /*--------------------------------------------------------- */
  466. /* Initialize option structure by defaults */
  467. struct options *opts_init(void)
  468. {
  469. struct options *opts = NULL;
  470. opts = malloc(sizeof(*opts));
  471. assert(opts);
  472. opts->debug = 0; /* daemonize by default */
  473. opts->socket_path = strdup(KONFD_SOCKET_PATH);
  474. opts->pidfile = strdup(KONFD_PIDFILE);
  475. opts->chroot = NULL;
  476. opts->uid = getuid();
  477. opts->gid = getgid();
  478. return opts;
  479. }
  480. /*--------------------------------------------------------- */
  481. /* Free option structure */
  482. void opts_free(struct options *opts)
  483. {
  484. if (opts->socket_path)
  485. free(opts->socket_path);
  486. if (opts->pidfile)
  487. free(opts->pidfile);
  488. if (opts->chroot)
  489. free(opts->chroot);
  490. free(opts);
  491. }
  492. /*--------------------------------------------------------- */
  493. /* Parse command line options */
  494. static int opts_parse(int argc, char *argv[], struct options *opts)
  495. {
  496. static const char *shortopts = "hvs:p:u:g:dr:";
  497. #ifdef HAVE_GETOPT_H
  498. static const struct option longopts[] = {
  499. {"help", 0, NULL, 'h'},
  500. {"version", 0, NULL, 'v'},
  501. {"socket", 1, NULL, 's'},
  502. {"pid", 1, NULL, 'p'},
  503. {"user", 1, NULL, 'u'},
  504. {"group", 1, NULL, 'g'},
  505. {"debug", 0, NULL, 'd'},
  506. {"chroot", 1, NULL, 'r'},
  507. {NULL, 0, NULL, 0}
  508. };
  509. #endif
  510. optind = 0;
  511. while(1) {
  512. int opt;
  513. #ifdef HAVE_GETOPT_H
  514. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  515. #else
  516. opt = getopt(argc, argv, shortopts);
  517. #endif
  518. if (-1 == opt)
  519. break;
  520. switch (opt) {
  521. case 's':
  522. if (opts->socket_path)
  523. free(opts->socket_path);
  524. opts->socket_path = strdup(optarg);
  525. break;
  526. case 'p':
  527. if (opts->pidfile)
  528. free(opts->pidfile);
  529. opts->pidfile = strdup(optarg);
  530. break;
  531. case 'r':
  532. #ifdef HAVE_CHROOT
  533. if (opts->chroot)
  534. free(opts->chroot);
  535. opts->chroot = strdup(optarg);
  536. #else
  537. syslog(LOG_ERR, "The --chroot option is not supported\n");
  538. return -1;
  539. #endif
  540. break;
  541. case 'd':
  542. opts->debug = 1;
  543. break;
  544. case 'u': {
  545. #ifdef HAVE_PWD_H
  546. struct passwd *pwd = getpwnam(optarg);
  547. if (!pwd) {
  548. syslog(LOG_ERR, "Can't identify user \"%s\"\n",
  549. optarg);
  550. return -1;
  551. }
  552. opts->uid = pwd->pw_uid;
  553. #else
  554. syslog(LOG_ERR, "The --user option is not supported\n");
  555. return -1;
  556. #endif
  557. break;
  558. }
  559. case 'g': {
  560. #ifdef HAVE_GRP_H
  561. struct group *grp = getgrnam(optarg);
  562. if (!grp) {
  563. syslog(LOG_ERR, "Can't identify group \"%s\"\n",
  564. optarg);
  565. return -1;
  566. }
  567. opts->gid = grp->gr_gid;
  568. #else
  569. syslog(LOG_ERR, "The --group option is not supported\n");
  570. return -1;
  571. #endif
  572. break;
  573. }
  574. case 'h':
  575. help(0, argv[0]);
  576. exit(0);
  577. break;
  578. case 'v':
  579. version(VERSION);
  580. exit(0);
  581. break;
  582. default:
  583. help(-1, argv[0]);
  584. exit(-1);
  585. break;
  586. }
  587. }
  588. return 0;
  589. }
  590. /*--------------------------------------------------------- */
  591. /* Print help message */
  592. static void help(int status, const char *argv0)
  593. {
  594. const char *name = NULL;
  595. if (!argv0)
  596. return;
  597. /* Find the basename */
  598. name = strrchr(argv0, '/');
  599. if (name)
  600. name++;
  601. else
  602. name = argv0;
  603. if (status != 0) {
  604. fprintf(stderr, "Try `%s -h' for more information.\n",
  605. name);
  606. } else {
  607. printf("Usage: %s [options]\n", name);
  608. printf("Daemon to store user configuration (i.e. commands). "
  609. "The part of the klish project.\n");
  610. printf("Options:\n");
  611. printf("\t-v, --version\tPrint version.\n");
  612. printf("\t-h, --help\tPrint this help.\n");
  613. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  614. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  615. "filesystem path to listen on.\n");
  616. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  617. printf("\t-r <path>, --chroot=<path>\tDirectory to chroot.\n");
  618. printf("\t-u <user>, --user=<user>\tExecute process as"
  619. " specified user.\n");
  620. printf("\t-g <group>, --group=<group>\tExecute process as"
  621. " specified group.\n");
  622. }
  623. }