konfd.c 14 KB

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