konfd.c 17 KB

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