konfd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. #ifdef HAVE_GETOPT_H
  26. #include <getopt.h>
  27. #endif
  28. #ifdef HAVE_PWD_H
  29. #include <pwd.h>
  30. #endif
  31. #ifdef HAVE_GRP_H
  32. #include <grp.h>
  33. #endif
  34. #include "konf/tree.h"
  35. #include "konf/query.h"
  36. #include "konf/buf.h"
  37. #include "konf/net.h"
  38. #include "lub/argv.h"
  39. #include "lub/string.h"
  40. #include "lub/log.h"
  41. #ifndef VERSION
  42. #define VERSION 1.2.2
  43. #endif
  44. #define QUOTE(t) #t
  45. #define version(v) printf("%s\n", v)
  46. #define KONFD_PIDFILE "/var/run/konfd.pid"
  47. /* UNIX socket path */
  48. /* Don't use UNIX_PATH_MAX due to portability issues */
  49. #define USOCK_PATH_MAX sizeof(((struct sockaddr_un *)0)->sun_path)
  50. /* OpenBSD has no MSG_NOSIGNAL flag */
  51. #ifndef MSG_NOSIGNAL
  52. #define MSG_NOSIGNAL 0
  53. #endif
  54. /* Global signal vars */
  55. static volatile int sigterm = 0;
  56. static void sighandler(int signo);
  57. static void help(int status, const char *argv0);
  58. static char * process_query(konf_buf_t *tbuf, konf_tree_t * conf, char *str);
  59. int answer_send(int sock, char *command);
  60. static int dump_running_config(int sock, konf_tree_t *conf, konf_query_t *query);
  61. int daemonize(int nochdir, int noclose);
  62. struct options *opts_init(void);
  63. void opts_free(struct options *opts);
  64. static int opts_parse(int argc, char *argv[], struct options *opts);
  65. static int create_listen_socket(const char *path,
  66. uid_t uid, gid_t gid, mode_t mode);
  67. /* Command line options */
  68. struct options {
  69. char *socket_path;
  70. char *ro_path;
  71. char *pidfile;
  72. char *chroot;
  73. int debug; /* Don't daemonize in debug mode */
  74. uid_t uid;
  75. gid_t gid;
  76. int log_facility;
  77. };
  78. /*--------------------------------------------------------- */
  79. int main(int argc, char **argv)
  80. {
  81. int retval = -1;
  82. unsigned int i;
  83. char *str;
  84. konf_tree_t *conf;
  85. lub_bintree_t bufs;
  86. konf_buf_t *tbuf;
  87. struct options *opts = NULL;
  88. int pidfd = -1;
  89. /* Network vars */
  90. int sock = -1;
  91. int ro_sock = -1;
  92. struct sockaddr_un raddr;
  93. fd_set active_fd_set, read_fd_set;
  94. /* Signal vars */
  95. struct sigaction sig_act, sigpipe_act;
  96. sigset_t sig_set, sigpipe_set;
  97. /* Parse command line options */
  98. opts = opts_init();
  99. if (opts_parse(argc, argv, opts))
  100. goto err;
  101. /* Initialize syslog */
  102. openlog(argv[0], LOG_CONS, opts->log_facility);
  103. syslog(LOG_ERR, "Start daemon.\n");
  104. /* Fork the daemon */
  105. if (!opts->debug) {
  106. /* Daemonize */
  107. if (daemonize(0, 0) < 0) {
  108. syslog(LOG_ERR, "Can't daemonize\n");
  109. goto err;
  110. }
  111. /* Write pidfile */
  112. if ((pidfd = open(opts->pidfile,
  113. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  114. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  115. syslog(LOG_WARNING, "Can't open pidfile %s: %s",
  116. opts->pidfile, strerror(errno));
  117. } else {
  118. char str[20];
  119. snprintf(str, sizeof(str), "%u\n", getpid());
  120. if (write(pidfd, str, strlen(str)) < 0)
  121. syslog(LOG_WARNING, "Can't write to %s: %s",
  122. opts->pidfile, strerror(errno));
  123. close(pidfd);
  124. }
  125. }
  126. /* Create RW listen socket */
  127. if ((sock = create_listen_socket(opts->socket_path,
  128. opts->uid, opts->gid, 0660)) == -1) {
  129. goto err;
  130. }
  131. /* Create RO listen socket */
  132. if (opts->ro_path && (ro_sock = create_listen_socket(opts->ro_path,
  133. opts->uid, opts->gid, 0666)) == -1) {
  134. goto err;
  135. }
  136. /* Change GID */
  137. if (opts->gid != getgid()) {
  138. if (setgid(opts->gid)) {
  139. syslog(LOG_ERR, "Can't set GID to %u: %s",
  140. opts->gid, strerror(errno));
  141. goto err;
  142. }
  143. }
  144. #ifdef HAVE_CHROOT
  145. /* Chroot */
  146. if (opts->chroot) {
  147. if (chroot(opts->chroot) < 0) {
  148. syslog(LOG_ERR, "Can't chroot to %s: %s",
  149. opts->chroot, strerror(errno));
  150. goto err;
  151. }
  152. }
  153. #endif
  154. /* Change UID */
  155. if (opts->uid != getuid()) {
  156. if (setuid(opts->uid)) {
  157. syslog(LOG_ERR, "Can't set UID to %u: %s",
  158. opts->uid, strerror(errno));
  159. goto err;
  160. }
  161. }
  162. /* Create configuration tree */
  163. conf = konf_tree_new("", 0);
  164. /* Initialize the tree of buffers */
  165. lub_bintree_init(&bufs,
  166. konf_buf_bt_offset(),
  167. konf_buf_bt_compare, konf_buf_bt_getkey);
  168. /* Set signal handler */
  169. sigemptyset(&sig_set);
  170. sigaddset(&sig_set, SIGTERM);
  171. sigaddset(&sig_set, SIGINT);
  172. sigaddset(&sig_set, SIGQUIT);
  173. sig_act.sa_flags = 0;
  174. sig_act.sa_mask = sig_set;
  175. sig_act.sa_handler = &sighandler;
  176. sigaction(SIGTERM, &sig_act, NULL);
  177. sigaction(SIGINT, &sig_act, NULL);
  178. sigaction(SIGQUIT, &sig_act, NULL);
  179. /* Ignore SIGPIPE */
  180. sigemptyset(&sigpipe_set);
  181. sigaddset(&sigpipe_set, SIGPIPE);
  182. sigpipe_act.sa_flags = 0;
  183. sigpipe_act.sa_mask = sigpipe_set;
  184. sigpipe_act.sa_handler = SIG_IGN;
  185. sigaction(SIGPIPE, &sigpipe_act, NULL);
  186. /* Initialize the set of active sockets. */
  187. FD_ZERO(&active_fd_set);
  188. FD_SET(sock, &active_fd_set);
  189. if (ro_sock >= 0)
  190. FD_SET(ro_sock, &active_fd_set);
  191. /* Main loop */
  192. while (!sigterm) {
  193. int num;
  194. /* Block until input arrives on one or more active sockets. */
  195. read_fd_set = active_fd_set;
  196. num = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
  197. if (num < 0) {
  198. if (EINTR == errno)
  199. continue;
  200. break;
  201. }
  202. if (0 == num)
  203. continue;
  204. /* Service all the sockets with input pending. */
  205. for (i = 0; i < FD_SETSIZE; ++i) {
  206. if (!FD_ISSET(i, &read_fd_set))
  207. continue;
  208. /* Connection request on listen socket. */
  209. if ((i == sock) || (i == ro_sock)) {
  210. int new;
  211. socklen_t size = sizeof(raddr);
  212. new = accept(i,
  213. (struct sockaddr *)&raddr, &size);
  214. if (new < 0) {
  215. continue;
  216. }
  217. #ifdef DEBUG
  218. fprintf(stderr, "------------------------------\n");
  219. fprintf(stderr, "Connection established %u\n", new);
  220. #endif
  221. konf_buftree_remove(&bufs, new);
  222. tbuf = konf_buf_new(new);
  223. /* Insert it into the binary tree */
  224. lub_bintree_insert(&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 each buf */
  257. while ((tbuf = lub_bintree_findfirst(&bufs))) {
  258. /* remove the buf from the tree */
  259. lub_bintree_remove(&bufs, tbuf);
  260. /* release the instance */
  261. konf_buf_delete(tbuf);
  262. }
  263. retval = 0;
  264. err:
  265. /* Close RW socket */
  266. if (sock >= 0) {
  267. close(sock);
  268. unlink(opts->socket_path);
  269. }
  270. /* Close RO socket */
  271. if (ro_sock >= 0) {
  272. close(ro_sock);
  273. unlink(opts->ro_path);
  274. }
  275. /* Remove pidfile */
  276. if (pidfd >= 0) {
  277. if (unlink(opts->pidfile) < 0) {
  278. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  279. opts->pidfile, strerror(errno));
  280. }
  281. }
  282. /* Free command line options */
  283. opts_free(opts);
  284. syslog(LOG_ERR, "Stop daemon.\n");
  285. return retval;
  286. }
  287. /*--------- Create listen socket--------------------------- */
  288. static int create_listen_socket(const char *path,
  289. uid_t uid, gid_t gid, mode_t mode)
  290. {
  291. int sock = -1;
  292. struct sockaddr_un laddr;
  293. const int reuseaddr = 1;
  294. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  295. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  296. goto err1;
  297. }
  298. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  299. &reuseaddr, sizeof(reuseaddr))) {
  300. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  301. goto err1;
  302. }
  303. laddr.sun_family = AF_UNIX;
  304. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  305. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  306. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  307. syslog(LOG_ERR, "Can't bind socket: %s\n", strerror(errno));
  308. goto err1;
  309. }
  310. if (chown(path, uid, gid)) {
  311. syslog(LOG_ERR, "Can't chown socket: %s\n", strerror(errno));
  312. goto err2;
  313. }
  314. if (chmod(path, mode)) {
  315. syslog(LOG_ERR, "Can't chmod socket: %s\n", strerror(errno));
  316. goto err2;
  317. }
  318. if (listen(sock, 10)) {
  319. syslog(LOG_ERR, "Can't listen socket: %s\n", strerror(errno));
  320. goto err2;
  321. }
  322. return sock;
  323. err2:
  324. unlink(path);
  325. err1:
  326. if (sock >= 0)
  327. close(sock);
  328. return -1;
  329. }
  330. /*--------------------------------------------------------- */
  331. static char * process_query(konf_buf_t *tbuf, konf_tree_t * conf, char *str)
  332. {
  333. unsigned int i;
  334. int res;
  335. konf_tree_t *iconf;
  336. konf_tree_t *tmpconf;
  337. konf_query_t *query;
  338. char *retval = NULL;
  339. konf_query_op_t ret = KONF_QUERY_OP_ERROR;
  340. #ifdef DEBUG
  341. fprintf(stderr, "REQUEST: %s\n", str);
  342. #endif
  343. /* Parse query */
  344. query = konf_query_new();
  345. res = konf_query_parse_str(query, str);
  346. if (res < 0) {
  347. konf_query_free(query);
  348. return NULL;
  349. }
  350. #ifdef DEBUG
  351. konf_query_dump(query);
  352. #endif
  353. /* Restrict RO socket for non-DUMP operation */
  354. if (!konf_buf__get_data(tbuf) &&
  355. (konf_query__get_op(query) != KONF_QUERY_OP_DUMP)) {
  356. #ifdef DEBUG
  357. fprintf(stderr, "Permission denied. Read-only socket.\n");
  358. #endif
  359. konf_query_free(query);
  360. return NULL;
  361. }
  362. /* Go through the pwd */
  363. iconf = conf;
  364. for (i = 0; i < konf_query__get_pwdc(query); i++) {
  365. if (!(iconf = konf_tree_find_conf(iconf,
  366. konf_query__get_pwd(query, i), 0, 0))) {
  367. iconf = NULL;
  368. break;
  369. }
  370. }
  371. if (!iconf) {
  372. #ifdef DEBUG
  373. fprintf(stderr, "Unknown path.\n");
  374. #endif
  375. konf_query_free(query);
  376. return NULL;
  377. }
  378. switch (konf_query__get_op(query)) {
  379. case KONF_QUERY_OP_SET:
  380. if (konf_query__get_unique(query)) {
  381. int exist = 0;
  382. exist = konf_tree_del_pattern(iconf,
  383. konf_query__get_line(query),
  384. konf_query__get_unique(query),
  385. konf_query__get_pattern(query),
  386. konf_query__get_priority(query),
  387. konf_query__get_seq(query),
  388. konf_query__get_seq_num(query));
  389. if (exist < 0)
  390. break;
  391. if (exist > 0) {
  392. ret = KONF_QUERY_OP_OK;
  393. break;
  394. }
  395. }
  396. tmpconf = konf_tree_new_conf(iconf,
  397. konf_query__get_line(query), konf_query__get_priority(query),
  398. konf_query__get_seq(query), konf_query__get_seq_num(query));
  399. if (!tmpconf)
  400. break;
  401. konf_tree__set_splitter(tmpconf, konf_query__get_splitter(query));
  402. konf_tree__set_depth(tmpconf, konf_query__get_pwdc(query));
  403. ret = KONF_QUERY_OP_OK;
  404. break;
  405. case KONF_QUERY_OP_UNSET:
  406. if (konf_tree_del_pattern(iconf,
  407. NULL,
  408. BOOL_TRUE,
  409. konf_query__get_pattern(query),
  410. konf_query__get_priority(query),
  411. konf_query__get_seq(query),
  412. konf_query__get_seq_num(query)) < 0)
  413. break;
  414. ret = KONF_QUERY_OP_OK;
  415. break;
  416. case KONF_QUERY_OP_DUMP:
  417. if (dump_running_config(konf_buf__get_fd(tbuf), iconf, query))
  418. break;
  419. ret = KONF_QUERY_OP_OK;
  420. break;
  421. default:
  422. break;
  423. }
  424. #ifdef DEBUG
  425. /* Print whole tree */
  426. konf_tree_fprintf(conf, stderr, NULL, -1, -1, BOOL_TRUE, 0);
  427. #endif
  428. /* Free resources */
  429. konf_query_free(query);
  430. switch (ret) {
  431. case KONF_QUERY_OP_OK:
  432. lub_string_cat(&retval, "-o");
  433. break;
  434. case KONF_QUERY_OP_ERROR:
  435. lub_string_cat(&retval, "-e");
  436. break;
  437. default:
  438. lub_string_cat(&retval, "-e");
  439. break;
  440. };
  441. #ifdef DEBUG
  442. fprintf(stderr, "ANSWER: %s\n", retval);
  443. #endif
  444. return retval;
  445. }
  446. /*--------------------------------------------------------- */
  447. /*
  448. * Signal handler for temination signals (like SIGTERM, SIGINT, ...)
  449. */
  450. static void sighandler(int signo)
  451. {
  452. sigterm = 1;
  453. }
  454. /*--------------------------------------------------------- */
  455. int answer_send(int sock, char *command)
  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. 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 > 2)
  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_H
  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_H
  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. }