1
0

konfd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. #if WITH_INTERNAL_GETOPT
  26. #include "libc/getopt.h"
  27. #else
  28. #ifdef HAVE_GETOPT_H
  29. #include <getopt.h>
  30. #endif
  31. #endif
  32. #ifdef HAVE_PWD_H
  33. #include <pwd.h>
  34. #endif
  35. #ifdef HAVE_GRP_H
  36. #include <grp.h>
  37. #endif
  38. #include "konf/tree.h"
  39. #include "konf/query.h"
  40. #include "konf/buf.h"
  41. #include "konf/net.h"
  42. #include "lub/argv.h"
  43. #include "lub/string.h"
  44. #include "lub/log.h"
  45. #ifndef VERSION
  46. #define VERSION 1.2.2
  47. #endif
  48. #define QUOTE(t) #t
  49. #define version(v) printf("%s\n", v)
  50. #define KONFD_PIDFILE "/var/run/konfd.pid"
  51. /* UNIX socket path */
  52. /* Don't use UNIX_PATH_MAX due to portability issues */
  53. #define USOCK_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
  54. /* OpenBSD has no MSG_NOSIGNAL flag */
  55. #ifndef MSG_NOSIGNAL
  56. #define MSG_NOSIGNAL 0
  57. #endif
  58. /* Global signal vars */
  59. static volatile int sigterm = 0;
  60. static void sighandler(int signo);
  61. static void help(int status, const char *argv0);
  62. static char * process_query(konf_buf_t *tbuf, konf_tree_t * conf, char *str);
  63. int answer_send(int sock, const char *command);
  64. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query);
  65. int daemonize(int nochdir, int noclose);
  66. struct options *opts_init(void);
  67. void opts_free(struct options *opts);
  68. static int opts_parse(int argc, char *argv[], struct options *opts);
  69. static int create_listen_socket(const char *path,
  70. uid_t uid, gid_t gid, mode_t mode);
  71. /* Command line options */
  72. struct options {
  73. char *socket_path;
  74. char *ro_path;
  75. char *pidfile;
  76. char *chroot;
  77. int debug; /* Don't daemonize in debug mode */
  78. uid_t uid;
  79. gid_t gid;
  80. int log_facility;
  81. };
  82. /*--------------------------------------------------------- */
  83. int main(int argc, char **argv)
  84. {
  85. int retval = -1;
  86. int i;
  87. char *str;
  88. konf_tree_t *conf;
  89. lub_bintree_t bufs;
  90. konf_buf_t *tbuf;
  91. struct options *opts = NULL;
  92. int pidfd = -1;
  93. /* Network vars */
  94. int sock = -1;
  95. int ro_sock = -1;
  96. struct sockaddr_un raddr;
  97. fd_set active_fd_set, read_fd_set;
  98. /* Signal vars */
  99. struct sigaction sig_act, sigpipe_act;
  100. sigset_t sig_set, sigpipe_set;
  101. /* Parse command line options */
  102. opts = opts_init();
  103. if (opts_parse(argc, argv, opts))
  104. goto err;
  105. /* Initialize syslog */
  106. openlog(argv[0], LOG_CONS, opts->log_facility);
  107. syslog(LOG_ERR, "Start daemon.\n");
  108. /* Fork the daemon */
  109. if (!opts->debug) {
  110. /* Daemonize */
  111. if (daemonize(0, 0) < 0) {
  112. syslog(LOG_ERR, "Can't daemonize\n");
  113. goto err;
  114. }
  115. /* Write pidfile */
  116. if ((pidfd = open(opts->pidfile,
  117. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  118. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  119. syslog(LOG_WARNING, "Can't open pidfile %s: %s",
  120. opts->pidfile, strerror(errno));
  121. } else {
  122. char str[20];
  123. snprintf(str, sizeof(str), "%u\n", getpid());
  124. str[sizeof(str) - 1] = '\0';
  125. if (write(pidfd, str, strlen(str)) < 0)
  126. syslog(LOG_WARNING, "Can't write to %s: %s",
  127. opts->pidfile, strerror(errno));
  128. close(pidfd);
  129. }
  130. }
  131. /* Create RW listen socket */
  132. if ((sock = create_listen_socket(opts->socket_path,
  133. opts->uid, opts->gid, 0660)) == -1) {
  134. goto err;
  135. }
  136. /* Create RO listen socket */
  137. if (opts->ro_path && (ro_sock = create_listen_socket(opts->ro_path,
  138. opts->uid, opts->gid, 0666)) == -1) {
  139. goto err;
  140. }
  141. /* Change GID */
  142. if (opts->gid != getgid()) {
  143. if (setgid(opts->gid)) {
  144. syslog(LOG_ERR, "Can't set GID to %u: %s",
  145. opts->gid, strerror(errno));
  146. goto err;
  147. }
  148. }
  149. #ifdef HAVE_CHROOT
  150. /* Chroot */
  151. if (opts->chroot) {
  152. if (chroot(opts->chroot) < 0) {
  153. syslog(LOG_ERR, "Can't chroot to %s: %s",
  154. opts->chroot, strerror(errno));
  155. goto err;
  156. }
  157. }
  158. #endif
  159. /* Change UID */
  160. if (opts->uid != getuid()) {
  161. if (setuid(opts->uid)) {
  162. syslog(LOG_ERR, "Can't set UID to %u: %s",
  163. opts->uid, strerror(errno));
  164. goto err;
  165. }
  166. }
  167. /* Create configuration tree */
  168. conf = konf_tree_new("", 0);
  169. /* Initialize the tree of buffers */
  170. lub_bintree_init(&bufs,
  171. konf_buf_bt_offset(),
  172. konf_buf_bt_compare, konf_buf_bt_getkey);
  173. /* Set signal handler */
  174. sigemptyset(&sig_set);
  175. sigaddset(&sig_set, SIGTERM);
  176. sigaddset(&sig_set, SIGINT);
  177. sigaddset(&sig_set, SIGQUIT);
  178. sig_act.sa_flags = 0;
  179. sig_act.sa_mask = sig_set;
  180. sig_act.sa_handler = &sighandler;
  181. sigaction(SIGTERM, &sig_act, NULL);
  182. sigaction(SIGINT, &sig_act, NULL);
  183. sigaction(SIGQUIT, &sig_act, NULL);
  184. /* Ignore SIGPIPE */
  185. sigemptyset(&sigpipe_set);
  186. sigaddset(&sigpipe_set, SIGPIPE);
  187. sigpipe_act.sa_flags = 0;
  188. sigpipe_act.sa_mask = sigpipe_set;
  189. sigpipe_act.sa_handler = SIG_IGN;
  190. sigaction(SIGPIPE, &sigpipe_act, NULL);
  191. /* Initialize the set of active sockets. */
  192. FD_ZERO(&active_fd_set);
  193. FD_SET(sock, &active_fd_set);
  194. if (ro_sock >= 0)
  195. FD_SET(ro_sock, &active_fd_set);
  196. /* Main loop */
  197. while (!sigterm) {
  198. int num;
  199. /* Block until input arrives on one or more active sockets. */
  200. read_fd_set = active_fd_set;
  201. num = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
  202. if (num < 0) {
  203. if (EINTR == errno)
  204. continue;
  205. break;
  206. }
  207. if (0 == num)
  208. continue;
  209. /* Service all the sockets with input pending. */
  210. for (i = 0; i < FD_SETSIZE; ++i) {
  211. if (!FD_ISSET(i, &read_fd_set))
  212. continue;
  213. /* Connection request on listen socket. */
  214. if ((i == sock) || (i == ro_sock)) {
  215. int new;
  216. socklen_t size = sizeof(raddr);
  217. new = accept(i,
  218. (struct sockaddr *)&raddr, &size);
  219. if (new < 0) {
  220. continue;
  221. }
  222. #ifdef DEBUG
  223. fprintf(stderr, "------------------------------\n");
  224. fprintf(stderr, "Connection established %u\n", new);
  225. #endif
  226. konf_buftree_remove(&bufs, new);
  227. tbuf = konf_buf_new(new);
  228. /* Insert it into the binary tree */
  229. lub_bintree_insert(&bufs, tbuf);
  230. /* In a case of RW socket we use buf's data pointer
  231. to indicate RW or RO socket. NULL=RO, not-NULL=RW */
  232. if (i == sock)
  233. konf_buf__set_data(tbuf, (void *)1);
  234. FD_SET(new, &active_fd_set);
  235. } else {
  236. int nbytes;
  237. tbuf = konf_buftree_find(&bufs, i);
  238. /* Data arriving on an already-connected socket. */
  239. if ((nbytes = konf_buf_read(tbuf)) <= 0) {
  240. close(i);
  241. FD_CLR(i, &active_fd_set);
  242. konf_buftree_remove(&bufs, i);
  243. #ifdef DEBUG
  244. fprintf(stderr, "Connection closed %u\n", i);
  245. #endif
  246. continue;
  247. }
  248. while ((str = konf_buf_parse(tbuf))) {
  249. char *answer;
  250. if (!(answer = process_query(tbuf, conf, str)))
  251. answer = strdup("-e");
  252. free(str);
  253. answer_send(i, answer);
  254. free(answer);
  255. }
  256. }
  257. }
  258. }
  259. /* Free resources */
  260. konf_tree_delete(conf);
  261. /* delete each buf */
  262. while ((tbuf = lub_bintree_findfirst(&bufs))) {
  263. /* remove the buf from the tree */
  264. lub_bintree_remove(&bufs, tbuf);
  265. /* release the instance */
  266. konf_buf_delete(tbuf);
  267. }
  268. retval = 0;
  269. err:
  270. /* Close RW socket */
  271. if (sock >= 0) {
  272. close(sock);
  273. unlink(opts->socket_path);
  274. }
  275. /* Close RO socket */
  276. if (ro_sock >= 0) {
  277. close(ro_sock);
  278. unlink(opts->ro_path);
  279. }
  280. /* Remove pidfile */
  281. if (pidfd >= 0) {
  282. if (unlink(opts->pidfile) < 0) {
  283. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  284. opts->pidfile, strerror(errno));
  285. }
  286. }
  287. /* Free command line options */
  288. opts_free(opts);
  289. syslog(LOG_ERR, "Stop daemon.\n");
  290. return retval;
  291. }
  292. /*--------- Create listen socket--------------------------- */
  293. static int create_listen_socket(const char *path,
  294. uid_t uid, gid_t gid, mode_t mode)
  295. {
  296. int sock = -1;
  297. struct sockaddr_un laddr;
  298. const int reuseaddr = 1;
  299. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  300. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  301. goto err1;
  302. }
  303. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  304. &reuseaddr, sizeof(reuseaddr))) {
  305. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  306. goto err1;
  307. }
  308. laddr.sun_family = AF_UNIX;
  309. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  310. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  311. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  312. syslog(LOG_ERR, "Can't bind socket: %s\n", strerror(errno));
  313. goto err1;
  314. }
  315. if (chown(path, uid, gid)) {
  316. syslog(LOG_ERR, "Can't chown socket: %s\n", strerror(errno));
  317. goto err2;
  318. }
  319. if (chmod(path, mode)) {
  320. syslog(LOG_ERR, "Can't chmod socket: %s\n", strerror(errno));
  321. goto err2;
  322. }
  323. if (listen(sock, 10)) {
  324. syslog(LOG_ERR, "Can't listen socket: %s\n", strerror(errno));
  325. goto err2;
  326. }
  327. return sock;
  328. err2:
  329. unlink(path);
  330. err1:
  331. if (sock >= 0)
  332. close(sock);
  333. return -1;
  334. }
  335. /*--------------------------------------------------------- */
  336. static char * process_query(konf_buf_t *tbuf, konf_tree_t * conf, char *str)
  337. {
  338. int i;
  339. int res;
  340. konf_tree_t *iconf;
  341. konf_tree_t *tmpconf;
  342. konf_query_t *query;
  343. char *retval = NULL;
  344. konf_query_op_e ret = KONF_QUERY_OP_ERROR;
  345. #ifdef DEBUG
  346. fprintf(stderr, "REQUEST: %s\n", str);
  347. #endif
  348. /* Parse query */
  349. query = konf_query_new();
  350. res = konf_query_parse_str(query, str);
  351. if (res < 0) {
  352. konf_query_free(query);
  353. return NULL;
  354. }
  355. #ifdef DEBUG
  356. konf_query_dump(query);
  357. #endif
  358. /* Restrict RO socket for non-DUMP operation */
  359. if (!konf_buf__get_data(tbuf) &&
  360. (konf_query__get_op(query) != KONF_QUERY_OP_DUMP)) {
  361. #ifdef DEBUG
  362. fprintf(stderr, "Permission denied. Read-only socket.\n");
  363. #endif
  364. konf_query_free(query);
  365. return NULL;
  366. }
  367. /* Go through the pwd */
  368. iconf = conf;
  369. for (i = 0; i < konf_query__get_pwdc(query); i++) {
  370. if (!(iconf = konf_tree_find_conf(iconf,
  371. konf_query__get_pwd(query, i), 0, 0))) {
  372. iconf = NULL;
  373. break;
  374. }
  375. }
  376. if (!iconf) {
  377. #ifdef DEBUG
  378. fprintf(stderr, "Unknown path.\n");
  379. #endif
  380. konf_query_free(query);
  381. return NULL;
  382. }
  383. switch (konf_query__get_op(query)) {
  384. case KONF_QUERY_OP_SET:
  385. if (konf_query__get_unique(query)) {
  386. int exist = 0;
  387. exist = konf_tree_del_pattern(iconf,
  388. konf_query__get_line(query),
  389. konf_query__get_unique(query),
  390. konf_query__get_pattern(query),
  391. konf_query__get_priority(query),
  392. konf_query__get_seq(query),
  393. konf_query__get_seq_num(query));
  394. if (exist < 0)
  395. break;
  396. if (exist > 0) {
  397. ret = KONF_QUERY_OP_OK;
  398. break;
  399. }
  400. }
  401. tmpconf = konf_tree_new_conf(iconf,
  402. konf_query__get_line(query), konf_query__get_priority(query),
  403. konf_query__get_seq(query), konf_query__get_seq_num(query));
  404. if (!tmpconf)
  405. break;
  406. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  407. konf_tree__set_depth(tmpconf, konf_query__get_pwdc(query));
  408. ret = KONF_QUERY_OP_OK;
  409. break;
  410. case KONF_QUERY_OP_UNSET:
  411. if (konf_tree_del_pattern(iconf,
  412. NULL,
  413. BOOL_TRUE,
  414. konf_query__get_pattern(query),
  415. konf_query__get_priority(query),
  416. konf_query__get_seq(query),
  417. konf_query__get_seq_num(query)) < 0)
  418. break;
  419. ret = KONF_QUERY_OP_OK;
  420. break;
  421. case KONF_QUERY_OP_DUMP:
  422. if (dump_running_config(konf_buf__get_fd(tbuf), iconf, query))
  423. break;
  424. ret = KONF_QUERY_OP_OK;
  425. break;
  426. default:
  427. break;
  428. }
  429. #ifdef DEBUG
  430. /* Print whole tree */
  431. konf_tree_fprintf(conf, stderr, NULL, -1, -1, BOOL_TRUE, BOOL_TRUE, 0);
  432. #endif
  433. /* Free resources */
  434. konf_query_free(query);
  435. switch (ret) {
  436. case KONF_QUERY_OP_OK:
  437. lub_string_cat(&retval, "-o");
  438. break;
  439. case KONF_QUERY_OP_ERROR:
  440. lub_string_cat(&retval, "-e");
  441. break;
  442. default:
  443. lub_string_cat(&retval, "-e");
  444. break;
  445. };
  446. #ifdef DEBUG
  447. fprintf(stderr, "ANSWER: %s\n", retval);
  448. #endif
  449. return retval;
  450. }
  451. /*--------------------------------------------------------- */
  452. /*
  453. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  454. */
  455. static void sighandler(int signo)
  456. {
  457. signo = signo; /* Happy compiler */
  458. sigterm = 1;
  459. }
  460. /*--------------------------------------------------------- */
  461. int answer_send(int sock, const char *command)
  462. {
  463. if (!command) {
  464. errno = EINVAL;
  465. return -1;
  466. }
  467. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  468. }
  469. /*--------------------------------------------------------- */
  470. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  471. {
  472. FILE *fd;
  473. char *filename;
  474. int dupsock = -1;
  475. if ((filename = konf_query__get_path(query))) {
  476. if (!(fd = fopen(filename, "w")))
  477. return -1;
  478. } else {
  479. if ((dupsock = dup(sock)) < 0)
  480. return -1;
  481. fd = fdopen(dupsock, "w");
  482. }
  483. if (!filename) {
  484. fprintf(fd, "-t\n");
  485. #ifdef DEBUG
  486. fprintf(stderr, "ANSWER: -t\n");
  487. #endif
  488. }
  489. konf_tree_fprintf(conf,
  490. fd,
  491. konf_query__get_pattern(query),
  492. konf_query__get_pwdc(query) - 1,
  493. konf_query__get_depth(query),
  494. konf_query__get_seq(query),
  495. konf_query__get_splitter(query),
  496. 0);
  497. if (!filename) {
  498. fprintf(fd, "\n");
  499. #ifdef DEBUG
  500. fprintf(stderr, "SEND DATA: \n");
  501. #endif
  502. }
  503. fclose(fd);
  504. return 0;
  505. }
  506. /*--------------------------------------------------------- */
  507. /* Implement own simple daemon() to don't use Non-POSIX */
  508. int daemonize(int nochdir, int noclose)
  509. {
  510. int fd;
  511. int pid;
  512. pid = fork();
  513. if (-1 == pid)
  514. return -1;
  515. if (pid > 0)
  516. _exit(0); /* Exit parent */
  517. if (setsid() == -1)
  518. return -1;
  519. if (!nochdir) {
  520. if (chdir("/"))
  521. return -1;
  522. }
  523. if (!noclose) {
  524. fd = open("/dev/null", O_RDWR, 0);
  525. if (fd < 0)
  526. return -1;
  527. dup2(fd, STDIN_FILENO);
  528. dup2(fd, STDOUT_FILENO);
  529. dup2(fd, STDERR_FILENO);
  530. if (fd > STDERR_FILENO)
  531. close(fd);
  532. }
  533. return 0;
  534. }
  535. /*--------------------------------------------------------- */
  536. /* Initialize option structure by defaults */
  537. struct options *opts_init(void)
  538. {
  539. struct options *opts = NULL;
  540. opts = malloc(sizeof(*opts));
  541. assert(opts);
  542. opts->debug = 0; /* daemonize by default */
  543. opts->socket_path = strdup(KONFD_SOCKET_PATH);
  544. opts->ro_path = NULL;
  545. opts->pidfile = strdup(KONFD_PIDFILE);
  546. opts->chroot = NULL;
  547. opts->uid = getuid();
  548. opts->gid = getgid();
  549. opts->log_facility = LOG_DAEMON;
  550. return opts;
  551. }
  552. /*--------------------------------------------------------- */
  553. /* Free option structure */
  554. void opts_free(struct options *opts)
  555. {
  556. if (opts->socket_path)
  557. free(opts->socket_path);
  558. if (opts->ro_path)
  559. free(opts->ro_path);
  560. if (opts->pidfile)
  561. free(opts->pidfile);
  562. if (opts->chroot)
  563. free(opts->chroot);
  564. free(opts);
  565. }
  566. /*--------------------------------------------------------- */
  567. /* Parse command line options */
  568. static int opts_parse(int argc, char *argv[], struct options *opts)
  569. {
  570. static const char *shortopts = "hvs:S:p:u:g:dr:O:";
  571. #ifdef HAVE_GETOPT_LONG
  572. static const struct option longopts[] = {
  573. {"help", 0, NULL, 'h'},
  574. {"version", 0, NULL, 'v'},
  575. {"socket", 1, NULL, 's'},
  576. {"ro-socket", 1, NULL, 'S'},
  577. {"pid", 1, NULL, 'p'},
  578. {"user", 1, NULL, 'u'},
  579. {"group", 1, NULL, 'g'},
  580. {"debug", 0, NULL, 'd'},
  581. {"chroot", 1, NULL, 'r'},
  582. {"facility", 1, NULL, 'O'},
  583. {NULL, 0, NULL, 0}
  584. };
  585. #endif
  586. optind = 1;
  587. while(1) {
  588. int opt;
  589. #ifdef HAVE_GETOPT_LONG
  590. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  591. #else
  592. opt = getopt(argc, argv, shortopts);
  593. #endif
  594. if (-1 == opt)
  595. break;
  596. switch (opt) {
  597. case 's':
  598. if (opts->socket_path)
  599. free(opts->socket_path);
  600. opts->socket_path = strdup(optarg);
  601. break;
  602. case 'S':
  603. if (opts->ro_path)
  604. free(opts->ro_path);
  605. opts->ro_path = strdup(optarg);
  606. break;
  607. case 'p':
  608. if (opts->pidfile)
  609. free(opts->pidfile);
  610. opts->pidfile = strdup(optarg);
  611. break;
  612. case 'r':
  613. #ifdef HAVE_CHROOT
  614. if (opts->chroot)
  615. free(opts->chroot);
  616. opts->chroot = strdup(optarg);
  617. #else
  618. fprintf(stderr, "Error: The --chroot option is not supported.\n");
  619. return -1;
  620. #endif
  621. break;
  622. case 'd':
  623. opts->debug = 1;
  624. break;
  625. case 'u': {
  626. #ifdef HAVE_PWD_H
  627. struct passwd *pwd = getpwnam(optarg);
  628. if (!pwd) {
  629. fprintf(stderr, "Error: Can't identify user \"%s\"\n",
  630. optarg);
  631. return -1;
  632. }
  633. opts->uid = pwd->pw_uid;
  634. #else
  635. fprintf(stderr, "The --user option is not supported.\n");
  636. return -1;
  637. #endif
  638. break;
  639. }
  640. case 'g': {
  641. #ifdef HAVE_GRP_H
  642. struct group *grp = getgrnam(optarg);
  643. if (!grp) {
  644. fprintf(stderr, "Can't identify group \"%s\"\n",
  645. optarg);
  646. return -1;
  647. }
  648. opts->gid = grp->gr_gid;
  649. #else
  650. fprintf(stderr, "The --group option is not supported.\n");
  651. return -1;
  652. #endif
  653. break;
  654. }
  655. case 'O':
  656. if (lub_log_facility(optarg, &(opts->log_facility))) {
  657. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  658. help(-1, argv[0]);
  659. exit(-1);
  660. }
  661. break;
  662. case 'h':
  663. help(0, argv[0]);
  664. exit(0);
  665. break;
  666. case 'v':
  667. version(VERSION);
  668. exit(0);
  669. break;
  670. default:
  671. help(-1, argv[0]);
  672. exit(-1);
  673. break;
  674. }
  675. }
  676. return 0;
  677. }
  678. /*--------------------------------------------------------- */
  679. /* Print help message */
  680. static void help(int status, const char *argv0)
  681. {
  682. const char *name = NULL;
  683. if (!argv0)
  684. return;
  685. /* Find the basename */
  686. name = strrchr(argv0, '/');
  687. if (name)
  688. name++;
  689. else
  690. name = argv0;
  691. if (status != 0) {
  692. fprintf(stderr, "Try `%s -h' for more information.\n",
  693. name);
  694. } else {
  695. printf("Usage: %s [options]\n", name);
  696. printf("Daemon to store user configuration (i.e. commands). "
  697. "The part of the klish project.\n");
  698. printf("Options:\n");
  699. printf("\t-v, --version\tPrint version.\n");
  700. printf("\t-h, --help\tPrint this help.\n");
  701. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  702. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  703. "filesystem path to listen on.\n");
  704. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  705. printf("\t-r <path>, --chroot=<path>\tDirectory to chroot.\n");
  706. printf("\t-u <user>, --user=<user>\tExecute process as"
  707. " specified user.\n");
  708. printf("\t-g <group>, --group=<group>\tExecute process as"
  709. " specified group.\n");
  710. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  711. }
  712. }