konfd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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_list_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. bufs = lub_list_new(konf_buf_compare, konf_buf_delete);
  171. /* Set signal handler */
  172. sigemptyset(&sig_set);
  173. sigaddset(&sig_set, SIGTERM);
  174. sigaddset(&sig_set, SIGINT);
  175. sigaddset(&sig_set, SIGQUIT);
  176. sig_act.sa_flags = 0;
  177. sig_act.sa_mask = sig_set;
  178. sig_act.sa_handler = &sighandler;
  179. sigaction(SIGTERM, &sig_act, NULL);
  180. sigaction(SIGINT, &sig_act, NULL);
  181. sigaction(SIGQUIT, &sig_act, NULL);
  182. /* Ignore SIGPIPE */
  183. sigemptyset(&sigpipe_set);
  184. sigaddset(&sigpipe_set, SIGPIPE);
  185. sigpipe_act.sa_flags = 0;
  186. sigpipe_act.sa_mask = sigpipe_set;
  187. sigpipe_act.sa_handler = SIG_IGN;
  188. sigaction(SIGPIPE, &sigpipe_act, NULL);
  189. /* Initialize the set of active sockets. */
  190. FD_ZERO(&active_fd_set);
  191. FD_SET(sock, &active_fd_set);
  192. if (ro_sock >= 0)
  193. FD_SET(ro_sock, &active_fd_set);
  194. /* Main loop */
  195. while (!sigterm) {
  196. int num;
  197. /* Block until input arrives on one or more active sockets. */
  198. read_fd_set = active_fd_set;
  199. num = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
  200. if (num < 0) {
  201. if (EINTR == errno)
  202. continue;
  203. break;
  204. }
  205. if (0 == num)
  206. continue;
  207. /* Service all the sockets with input pending. */
  208. for (i = 0; i < FD_SETSIZE; ++i) {
  209. if (!FD_ISSET(i, &read_fd_set))
  210. continue;
  211. /* Connection request on listen socket. */
  212. if ((i == sock) || (i == ro_sock)) {
  213. int new;
  214. socklen_t size = sizeof(raddr);
  215. new = accept(i, (struct sockaddr *)&raddr, &size);
  216. if (new < 0)
  217. continue;
  218. #ifdef DEBUG
  219. fprintf(stderr, "------------------------------\n");
  220. fprintf(stderr, "Connection established %u\n", new);
  221. #endif
  222. konf_buftree_remove(bufs, new);
  223. tbuf = konf_buf_new(new);
  224. lub_list_add(bufs, tbuf);
  225. /* In a case of RW socket we use buf's data pointer
  226. to indicate RW or RO socket. NULL=RO, not-NULL=RW */
  227. if (i == sock)
  228. konf_buf__set_data(tbuf, (void *)1);
  229. FD_SET(new, &active_fd_set);
  230. } else {
  231. int nbytes;
  232. tbuf = konf_buftree_find(bufs, i);
  233. /* Data arriving on an already-connected socket. */
  234. if ((nbytes = 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 = 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. #else
  625. fprintf(stderr, "The --user option is not supported.\n");
  626. return -1;
  627. #endif
  628. break;
  629. }
  630. case 'g': {
  631. #ifdef HAVE_GRP_H
  632. struct group *grp = getgrnam(optarg);
  633. if (!grp) {
  634. fprintf(stderr, "Can't identify group \"%s\"\n",
  635. optarg);
  636. return -1;
  637. }
  638. opts->gid = grp->gr_gid;
  639. #else
  640. fprintf(stderr, "The --group option is not supported.\n");
  641. return -1;
  642. #endif
  643. break;
  644. }
  645. case 'O':
  646. if (lub_log_facility(optarg, &(opts->log_facility))) {
  647. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  648. help(-1, argv[0]);
  649. exit(-1);
  650. }
  651. break;
  652. case 'h':
  653. help(0, argv[0]);
  654. exit(0);
  655. break;
  656. case 'v':
  657. version(VERSION);
  658. exit(0);
  659. break;
  660. default:
  661. help(-1, argv[0]);
  662. exit(-1);
  663. break;
  664. }
  665. }
  666. return 0;
  667. }
  668. /*--------------------------------------------------------- */
  669. /* Print help message */
  670. static void help(int status, const char *argv0)
  671. {
  672. const char *name = NULL;
  673. if (!argv0)
  674. return;
  675. /* Find the basename */
  676. name = strrchr(argv0, '/');
  677. if (name)
  678. name++;
  679. else
  680. name = argv0;
  681. if (status != 0) {
  682. fprintf(stderr, "Try `%s -h' for more information.\n",
  683. name);
  684. } else {
  685. printf("Usage: %s [options]\n", name);
  686. printf("Daemon to store user configuration (i.e. commands). "
  687. "The part of the klish project.\n");
  688. printf("Options:\n");
  689. printf("\t-v, --version\tPrint version.\n");
  690. printf("\t-h, --help\tPrint this help.\n");
  691. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  692. printf("\t-s <path>, --socket=<path>\tSpecify the UNIX socket "
  693. "filesystem path to listen on.\n");
  694. printf("\t-p <path>, --pid=<path>\tFile to save daemon's PID to.\n");
  695. printf("\t-r <path>, --chroot=<path>\tDirectory to chroot.\n");
  696. printf("\t-u <user>, --user=<user>\tExecute process as"
  697. " specified user.\n");
  698. printf("\t-g <group>, --group=<group>\tExecute process as"
  699. " specified group.\n");
  700. printf("\t-O, --facility\tSyslog facility. Default is DAEMON.\n");
  701. }
  702. }