1
0

konfd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. unsigned 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. unsigned 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. sigterm = 1;
  457. }
  458. /*--------------------------------------------------------- */
  459. int answer_send(int sock, char *command)
  460. {
  461. return send(sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  462. }
  463. /*--------------------------------------------------------- */
  464. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query)
  465. {
  466. FILE *fd;
  467. char *filename;
  468. int dupsock = -1;
  469. if ((filename = konf_query__get_path(query))) {
  470. if (!(fd = fopen(filename, "w")))
  471. return -1;
  472. } else {
  473. if ((dupsock = dup(sock)) < 0)
  474. return -1;
  475. fd = fdopen(dupsock, "w");
  476. }
  477. if (!filename) {
  478. fprintf(fd, "-t\n");
  479. #ifdef DEBUG
  480. fprintf(stderr, "ANSWER: -t\n");
  481. #endif
  482. }
  483. konf_tree_fprintf(conf,
  484. fd,
  485. konf_query__get_pattern(query),
  486. konf_query__get_pwdc(query) - 1,
  487. konf_query__get_depth(query),
  488. konf_query__get_seq(query),
  489. 0);
  490. if (!filename) {
  491. fprintf(fd, "\n");
  492. #ifdef DEBUG
  493. fprintf(stderr, "SEND DATA: \n");
  494. #endif
  495. }
  496. fclose(fd);
  497. return 0;
  498. }
  499. /*--------------------------------------------------------- */
  500. /* Implement own simple daemon() to don't use Non-POSIX */
  501. int daemonize(int nochdir, int noclose)
  502. {
  503. int fd;
  504. int pid;
  505. pid = fork();
  506. if (-1 == pid)
  507. return -1;
  508. if (pid > 0)
  509. _exit(0); /* Exit parent */
  510. if (setsid() == -1)
  511. return -1;
  512. if (!nochdir) {
  513. if (chdir("/"))
  514. return -1;
  515. }
  516. if (!noclose) {
  517. fd = open("/dev/null", O_RDWR, 0);
  518. if (fd < 0)
  519. return -1;
  520. dup2(fd, STDIN_FILENO);
  521. dup2(fd, STDOUT_FILENO);
  522. dup2(fd, STDERR_FILENO);
  523. if (fd > 2)
  524. close(fd);
  525. }
  526. return 0;
  527. }
  528. /*--------------------------------------------------------- */
  529. /* Initialize option structure by defaults */
  530. struct options *opts_init(void)
  531. {
  532. struct options *opts = NULL;
  533. opts = malloc(sizeof(*opts));
  534. assert(opts);
  535. opts->debug = 0; /* daemonize by default */
  536. opts->socket_path = strdup(KONFD_SOCKET_PATH);
  537. opts->ro_path = NULL;
  538. opts->pidfile = strdup(KONFD_PIDFILE);
  539. opts->chroot = NULL;
  540. opts->uid = getuid();
  541. opts->gid = getgid();
  542. opts->log_facility = LOG_DAEMON;
  543. return opts;
  544. }
  545. /*--------------------------------------------------------- */
  546. /* Free option structure */
  547. void opts_free(struct options *opts)
  548. {
  549. if (opts->socket_path)
  550. free(opts->socket_path);
  551. if (opts->ro_path)
  552. free(opts->ro_path);
  553. if (opts->pidfile)
  554. free(opts->pidfile);
  555. if (opts->chroot)
  556. free(opts->chroot);
  557. free(opts);
  558. }
  559. /*--------------------------------------------------------- */
  560. /* Parse command line options */
  561. static int opts_parse(int argc, char *argv[], struct options *opts)
  562. {
  563. static const char *shortopts = "hvs:S:p:u:g:dr:O:";
  564. #ifdef HAVE_GETOPT_LONG
  565. static const struct option longopts[] = {
  566. {"help", 0, NULL, 'h'},
  567. {"version", 0, NULL, 'v'},
  568. {"socket", 1, NULL, 's'},
  569. {"ro-socket", 1, NULL, 'S'},
  570. {"pid", 1, NULL, 'p'},
  571. {"user", 1, NULL, 'u'},
  572. {"group", 1, NULL, 'g'},
  573. {"debug", 0, NULL, 'd'},
  574. {"chroot", 1, NULL, 'r'},
  575. {"facility", 1, NULL, 'O'},
  576. {NULL, 0, NULL, 0}
  577. };
  578. #endif
  579. optind = 1;
  580. while(1) {
  581. int opt;
  582. #ifdef HAVE_GETOPT_LONG
  583. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  584. #else
  585. opt = getopt(argc, argv, shortopts);
  586. #endif
  587. if (-1 == opt)
  588. break;
  589. switch (opt) {
  590. case 's':
  591. if (opts->socket_path)
  592. free(opts->socket_path);
  593. opts->socket_path = strdup(optarg);
  594. break;
  595. case 'S':
  596. if (opts->ro_path)
  597. free(opts->ro_path);
  598. opts->ro_path = strdup(optarg);
  599. break;
  600. case 'p':
  601. if (opts->pidfile)
  602. free(opts->pidfile);
  603. opts->pidfile = strdup(optarg);
  604. break;
  605. case 'r':
  606. #ifdef HAVE_CHROOT
  607. if (opts->chroot)
  608. free(opts->chroot);
  609. opts->chroot = strdup(optarg);
  610. #else
  611. fprintf(stderr, "Error: The --chroot option is not supported.\n");
  612. return -1;
  613. #endif
  614. break;
  615. case 'd':
  616. opts->debug = 1;
  617. break;
  618. case 'u': {
  619. #ifdef HAVE_PWD_H
  620. struct passwd *pwd = getpwnam(optarg);
  621. if (!pwd) {
  622. fprintf(stderr, "Error: Can't identify user \"%s\"\n",
  623. optarg);
  624. return -1;
  625. }
  626. opts->uid = pwd->pw_uid;
  627. #else
  628. fprintf(stderr, "The --user option is not supported.\n");
  629. return -1;
  630. #endif
  631. break;
  632. }
  633. case 'g': {
  634. #ifdef HAVE_GRP_H
  635. struct group *grp = getgrnam(optarg);
  636. if (!grp) {
  637. fprintf(stderr, "Can't identify group \"%s\"\n",
  638. optarg);
  639. return -1;
  640. }
  641. opts->gid = grp->gr_gid;
  642. #else
  643. fprintf(stderr, "The --group option is not supported.\n");
  644. return -1;
  645. #endif
  646. break;
  647. }
  648. case 'O':
  649. if (lub_log_facility(optarg, &(opts->log_facility))) {
  650. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  651. help(-1, argv[0]);
  652. exit(-1);
  653. }
  654. break;
  655. case 'h':
  656. help(0, argv[0]);
  657. exit(0);
  658. break;
  659. case 'v':
  660. version(VERSION);
  661. exit(0);
  662. break;
  663. default:
  664. help(-1, argv[0]);
  665. exit(-1);
  666. break;
  667. }
  668. }
  669. return 0;
  670. }
  671. /*--------------------------------------------------------- */
  672. /* Print help message */
  673. static void help(int status, const char *argv0)
  674. {
  675. const char *name = NULL;
  676. if (!argv0)
  677. return;
  678. /* Find the basename */
  679. name = strrchr(argv0, '/');
  680. if (name)
  681. name++;
  682. else
  683. name = argv0;
  684. if (status != 0) {
  685. fprintf(stderr, "Try `%s -h' for more information.\n",
  686. name);
  687. } else {
  688. printf("Usage: %s [options]\n", name);
  689. printf("Daemon to store user configuration (i.e. commands). "
  690. "The part of the klish project.\n");
  691. printf("Options:\n");
  692. printf("\t-v, --version\tPrint version.\n");
  693. printf("\t-h, --help\tPrint this help.\n");
  694. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  695. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  696. "filesystem path to listen on.\n");
  697. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  698. printf("\t-r <path>, --chroot=<path>\tDirectory to chroot.\n");
  699. printf("\t-u <user>, --user=<user>\tExecute process as"
  700. " specified user.\n");
  701. printf("\t-g <group>, --group=<group>\tExecute process as"
  702. " specified group.\n");
  703. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  704. }
  705. }