konfd.c 15 KB

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