1
0

konfd.c 17 KB

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