konfd.c 17 KB

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