geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

650 lines
16 KiB

  1. /*-
  2. * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD$
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <sys/param.h>
  34. #include <sys/queue.h>
  35. #include <sys/endian.h>
  36. #include <sys/socket.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/stat.h>
  39. #include <sys/disk.h>
  40. #include <sys/bio.h>
  41. #include <netinet/in.h>
  42. #include <netinet/tcp.h>
  43. #include <arpa/inet.h>
  44. #include <signal.h>
  45. #include <err.h>
  46. #include <errno.h>
  47. #include <string.h>
  48. #include <libgen.h>
  49. #include <syslog.h>
  50. #include <stdarg.h>
  51. #include <geom/gate/g_gate.h>
  52. #include "ggate.h"
  53. #define G_GATED_EXPORT_FILE "/etc/gg.exports"
  54. #define G_GATED_DEBUG(...) \
  55. if (g_gate_verbose) { \
  56. printf(__VA_ARGS__); \
  57. printf("\n"); \
  58. }
  59. static const char *exports = G_GATED_EXPORT_FILE;
  60. static int got_sighup = 0;
  61. static int nagle = 1;
  62. static unsigned rcvbuf = G_GATE_RCVBUF;
  63. static unsigned sndbuf = G_GATE_SNDBUF;
  64. struct export {
  65. char *e_path; /* path to device/file */
  66. in_addr_t e_ip; /* remote IP address */
  67. in_addr_t e_mask; /* IP mask */
  68. unsigned e_flags; /* flags (RO/RW) */
  69. SLIST_ENTRY(export) e_next;
  70. };
  71. static SLIST_HEAD(, export) exports_list =
  72. SLIST_HEAD_INITIALIZER(&exports_list);
  73. static void
  74. usage(void)
  75. {
  76. fprintf(stderr, "usage: %s [-nv] [-a address] [-p port] [-R rcvbuf] "
  77. "[-S sndbuf] [exports file]\n", getprogname());
  78. exit(EXIT_FAILURE);
  79. }
  80. static char *
  81. ip2str(in_addr_t ip)
  82. {
  83. static char sip[16];
  84. snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
  85. ((ip >> 24) & 0xff),
  86. ((ip >> 16) & 0xff),
  87. ((ip >> 8) & 0xff),
  88. (ip & 0xff));
  89. return (sip);
  90. }
  91. static in_addr_t
  92. countmask(unsigned m)
  93. {
  94. in_addr_t mask;
  95. if (m == 0) {
  96. mask = 0x0;
  97. } else {
  98. mask = 1 << (32 - m);
  99. mask--;
  100. mask = ~mask;
  101. }
  102. return (mask);
  103. }
  104. static void
  105. line_parse(char *line, unsigned lineno)
  106. {
  107. struct export *ex;
  108. char *word, *path, *sflags;
  109. unsigned flags, i, vmask;
  110. in_addr_t ip, mask;
  111. ip = mask = flags = vmask = 0;
  112. path = NULL;
  113. sflags = NULL;
  114. for (i = 0, word = strtok(line, " \t"); word != NULL;
  115. i++, word = strtok(NULL, " \t")) {
  116. switch (i) {
  117. case 0: /* IP address or host name */
  118. ip = g_gate_str2ip(strsep(&word, "/"));
  119. if (ip == INADDR_NONE) {
  120. g_gate_xlog("Invalid IP/host name at line %u.",
  121. lineno);
  122. }
  123. ip = ntohl(ip);
  124. if (word == NULL)
  125. vmask = 32;
  126. else {
  127. errno = 0;
  128. vmask = strtoul(word, NULL, 10);
  129. if (vmask == 0 && errno != 0) {
  130. g_gate_xlog("Invalid IP mask value at "
  131. "line %u.", lineno);
  132. }
  133. if ((unsigned)vmask > 32) {
  134. g_gate_xlog("Invalid IP mask value at line %u.",
  135. lineno);
  136. }
  137. }
  138. mask = countmask(vmask);
  139. break;
  140. case 1: /* flags */
  141. if (strcasecmp("rd", word) == 0 ||
  142. strcasecmp("ro", word) == 0) {
  143. flags = O_RDONLY;
  144. } else if (strcasecmp("wo", word) == 0) {
  145. flags = O_WRONLY;
  146. } else if (strcasecmp("rw", word) == 0) {
  147. flags = O_RDWR;
  148. } else {
  149. g_gate_xlog("Invalid value in flags field at "
  150. "line %u.", lineno);
  151. }
  152. sflags = word;
  153. break;
  154. case 2: /* path */
  155. if (strlen(word) >= MAXPATHLEN) {
  156. g_gate_xlog("Path too long at line %u. ",
  157. lineno);
  158. }
  159. path = word;
  160. break;
  161. default:
  162. g_gate_xlog("Too many arguments at line %u. ", lineno);
  163. }
  164. }
  165. if (i != 3)
  166. g_gate_xlog("Too few arguments at line %u.", lineno);
  167. ex = malloc(sizeof(*ex));
  168. if (ex == NULL)
  169. g_gate_xlog("No enough memory.");
  170. ex->e_path = strdup(path);
  171. if (ex->e_path == NULL)
  172. g_gate_xlog("No enough memory.");
  173. /* Made 'and' here. */
  174. ex->e_ip = (ip & mask);
  175. ex->e_mask = mask;
  176. ex->e_flags = flags;
  177. SLIST_INSERT_HEAD(&exports_list, ex, e_next);
  178. g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
  179. ip2str(ex->e_ip), vmask, path, sflags);
  180. }
  181. static void
  182. exports_clear(void)
  183. {
  184. struct export *ex;
  185. while (!SLIST_EMPTY(&exports_list)) {
  186. ex = SLIST_FIRST(&exports_list);
  187. SLIST_REMOVE_HEAD(&exports_list, e_next);
  188. free(ex);
  189. }
  190. }
  191. #define EXPORTS_LINE_SIZE 2048
  192. static void
  193. exports_get(void)
  194. {
  195. char buf[EXPORTS_LINE_SIZE], *line;
  196. unsigned lineno = 0, objs = 0, len;
  197. FILE *fd;
  198. exports_clear();
  199. fd = fopen(exports, "r");
  200. if (fd == NULL) {
  201. g_gate_xlog("Cannot open exports file (%s): %s.", exports,
  202. strerror(errno));
  203. }
  204. g_gate_log(LOG_INFO, "Reading exports file (%s).", exports);
  205. for (;;) {
  206. if (fgets(buf, sizeof(buf), fd) == NULL) {
  207. if (feof(fd))
  208. break;
  209. g_gate_xlog("Error while reading exports file: %s.",
  210. strerror(errno));
  211. }
  212. /* Increase line count. */
  213. lineno++;
  214. /* Skip spaces and tabs. */
  215. for (line = buf; *line == ' ' || *line == '\t'; ++line)
  216. ;
  217. /* Empty line, comment or empty line at the end of file. */
  218. if (*line == '\n' || *line == '#' || *line == '\0')
  219. continue;
  220. len = strlen(line);
  221. if (line[len - 1] == '\n') {
  222. /* Remove new line char. */
  223. line[len - 1] = '\0';
  224. } else {
  225. if (!feof(fd))
  226. g_gate_xlog("Line %u too long.", lineno);
  227. }
  228. line_parse(line, lineno);
  229. objs++;
  230. }
  231. fclose(fd);
  232. if (objs == 0)
  233. g_gate_xlog("There are no objects to export.");
  234. g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
  235. }
  236. static struct export *
  237. exports_find(struct sockaddr *s, const char *path)
  238. {
  239. struct export *ex;
  240. in_addr_t ip;
  241. ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
  242. SLIST_FOREACH(ex, &exports_list, e_next) {
  243. if ((ip & ex->e_mask) != ex->e_ip)
  244. continue;
  245. if (path != NULL && strcmp(path, ex->e_path) != 0)
  246. continue;
  247. g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
  248. return (ex);
  249. }
  250. g_gate_log(LOG_INFO, "Unauthorized connection from: %s.", ip2str(ip));
  251. return (NULL);
  252. }
  253. static void
  254. sendfail(int sfd, int error, const char *fmt, ...)
  255. {
  256. struct g_gate_sinit sinit;
  257. va_list ap;
  258. int data;
  259. sinit.gs_error = error;
  260. g_gate_swap2n_sinit(&sinit);
  261. data = send(sfd, &sinit, sizeof(sinit), 0);
  262. g_gate_swap2h_sinit(&sinit);
  263. if (data == -1) {
  264. g_gate_xlog("Error while sending initial packet: %s.",
  265. strerror(errno));
  266. }
  267. if (fmt != NULL) {
  268. va_start(ap, fmt);
  269. g_gate_xvlog(fmt, ap);
  270. /* NOTREACHED */
  271. va_end(ap);
  272. }
  273. exit(EXIT_FAILURE);
  274. }
  275. static void
  276. serve(int sfd, struct sockaddr *s)
  277. {
  278. struct g_gate_cinit cinit;
  279. struct g_gate_sinit sinit;
  280. struct g_gate_hdr hdr;
  281. struct export *ex;
  282. char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
  283. size_t bufsize;
  284. int32_t error;
  285. int fd, flags;
  286. ssize_t data;
  287. char *buf;
  288. g_gate_log(LOG_DEBUG, "Receiving initial packet.");
  289. data = recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
  290. g_gate_swap2h_cinit(&cinit);
  291. if (data == -1) {
  292. g_gate_xlog("Error while receiving initial packet: %s.",
  293. strerror(errno));
  294. }
  295. ex = exports_find(s, cinit.gc_path);
  296. if (ex == NULL) {
  297. sendfail(sfd, EINVAL, "Requested path isn't exported: %s.",
  298. strerror(errno));
  299. }
  300. error = 0;
  301. strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
  302. strlcat(ipmask, "/", sizeof(ipmask));
  303. strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
  304. if ((cinit.gc_flags & G_GATE_FLAG_READONLY) != 0) {
  305. if (ex->e_flags == O_WRONLY) {
  306. g_gate_log(LOG_ERR, "Read-only access requested, but "
  307. "%s (%s) is exported write-only.", ex->e_path,
  308. ipmask);
  309. error = EPERM;
  310. } else {
  311. sinit.gs_flags = G_GATE_FLAG_READONLY;
  312. }
  313. } else if ((cinit.gc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
  314. if (ex->e_flags == O_RDONLY) {
  315. g_gate_log(LOG_ERR, "Write-only access requested, but "
  316. "%s (%s) is exported read-only.", ex->e_path,
  317. ipmask);
  318. error = EPERM;
  319. } else {
  320. sinit.gs_flags = G_GATE_FLAG_WRITEONLY;
  321. }
  322. } else {
  323. if (ex->e_flags == O_RDONLY) {
  324. g_gate_log(LOG_ERR, "Read-write access requested, but "
  325. "%s (%s) is exported read-only.", ex->e_path,
  326. ipmask);
  327. error = EPERM;
  328. } else if (ex->e_flags == O_WRONLY) {
  329. g_gate_log(LOG_ERR, "Read-write access requested, but "
  330. "%s (%s) is exported write-only.", ex->e_path,
  331. ipmask);
  332. error = EPERM;
  333. } else {
  334. sinit.gs_flags = 0;
  335. }
  336. }
  337. if (error != 0)
  338. sendfail(sfd, error, NULL);
  339. flags = g_gate_openflags(sinit.gs_flags);;
  340. fd = open(ex->e_path, flags);
  341. if (fd < 0) {
  342. sendfail(sfd, errno, "Error while opening %s: %s.", ex->e_path,
  343. strerror(errno));
  344. }
  345. g_gate_log(LOG_DEBUG, "Sending initial packet.");
  346. /*
  347. * This field isn't used by ggc(8) for now.
  348. * It should be used in future when user don't give device size.
  349. */
  350. sinit.gs_mediasize = g_gate_mediasize(fd);
  351. sinit.gs_sectorsize = g_gate_sectorsize(fd);
  352. sinit.gs_error = 0;
  353. g_gate_swap2n_sinit(&sinit);
  354. data = send(sfd, &sinit, sizeof(sinit), 0);
  355. g_gate_swap2h_sinit(&sinit);
  356. if (data == -1) {
  357. sendfail(sfd, errno, "Error while sending initial packet: %s.",
  358. strerror(errno));
  359. }
  360. bufsize = G_GATE_BUFSIZE_START;
  361. buf = malloc(bufsize);
  362. if (buf == NULL)
  363. g_gate_xlog("No enough memory.");
  364. g_gate_log(LOG_DEBUG, "New process: %u.", getpid());
  365. for (;;) {
  366. /*
  367. * Receive request.
  368. */
  369. data = recv(sfd, &hdr, sizeof(hdr), MSG_WAITALL);
  370. if (data == 0) {
  371. g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
  372. exit(EXIT_SUCCESS);
  373. } else if (data == -1) {
  374. g_gate_xlog("Error while receiving hdr packet: %s.",
  375. strerror(errno));
  376. } else if (data != sizeof(hdr)) {
  377. g_gate_xlog("Malformed hdr packet received.");
  378. }
  379. g_gate_log(LOG_DEBUG, "Received hdr packet.");
  380. g_gate_swap2h_hdr(&hdr);
  381. /*
  382. * Increase buffer if there is need to.
  383. */
  384. if (hdr.gh_length > bufsize) {
  385. bufsize = hdr.gh_length;
  386. g_gate_log(LOG_DEBUG, "Increasing buffer to %u.",
  387. bufsize);
  388. buf = realloc(buf, bufsize);
  389. if (buf == NULL)
  390. g_gate_xlog("No enough memory.");
  391. }
  392. if (hdr.gh_cmd == BIO_READ) {
  393. if (pread(fd, buf, hdr.gh_length,
  394. hdr.gh_offset) == -1) {
  395. error = errno;
  396. g_gate_log(LOG_ERR, "Error while reading data "
  397. "(offset=%ju, size=%zu): %s.",
  398. (uintmax_t)hdr.gh_offset,
  399. (size_t)hdr.gh_length, strerror(error));
  400. } else {
  401. error = 0;
  402. }
  403. hdr.gh_error = error;
  404. g_gate_swap2n_hdr(&hdr);
  405. if (send(sfd, &hdr, sizeof(hdr), 0) == -1) {
  406. g_gate_xlog("Error while sending status: %s.",
  407. strerror(errno));
  408. }
  409. g_gate_swap2h_hdr(&hdr);
  410. /* Send data only if there was no error while pread(). */
  411. if (error == 0) {
  412. data = send(sfd, buf, hdr.gh_length, 0);
  413. if (data == -1) {
  414. g_gate_xlog("Error while sending data: "
  415. "%s.", strerror(errno));
  416. }
  417. g_gate_log(LOG_DEBUG, "Sent %d bytes "
  418. "(offset=%ju, size=%zu).", data,
  419. (uintmax_t)hdr.gh_offset,
  420. (size_t)hdr.gh_length);
  421. }
  422. } else /* if (hdr.gh_cmd == BIO_WRITE) */ {
  423. g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
  424. hdr.gh_length);
  425. data = recv(sfd, buf, hdr.gh_length, MSG_WAITALL);
  426. if (data == -1) {
  427. g_gate_xlog("Error while receiving data: %s.",
  428. strerror(errno));
  429. }
  430. if (pwrite(fd, buf, hdr.gh_length, hdr.gh_offset) == -1) {
  431. error = errno;
  432. g_gate_log(LOG_ERR, "Error while writing data "
  433. "(offset=%llu, size=%u): %s.",
  434. hdr.gh_offset, hdr.gh_length,
  435. strerror(error));
  436. } else {
  437. error = 0;
  438. }
  439. hdr.gh_error = error;
  440. g_gate_swap2n_hdr(&hdr);
  441. if (send(sfd, &hdr, sizeof(hdr), 0) == -1) {
  442. g_gate_xlog("Error while sending status: %s.",
  443. strerror(errno));
  444. }
  445. g_gate_swap2h_hdr(&hdr);
  446. g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%llu, "
  447. "size=%u).", data, hdr.gh_offset, hdr.gh_length);
  448. }
  449. g_gate_log(LOG_DEBUG, "Tick.");
  450. }
  451. }
  452. static void
  453. huphandler(int sig __unused)
  454. {
  455. got_sighup = 1;
  456. }
  457. int
  458. main(int argc, char *argv[])
  459. {
  460. struct sockaddr_in serv;
  461. struct sockaddr from;
  462. in_addr_t bindaddr;
  463. socklen_t fromlen;
  464. struct timeval tv;
  465. int on, sfd, tmpsfd;
  466. pid_t childpid;
  467. unsigned bsize, port;
  468. bindaddr = htonl(INADDR_ANY);
  469. port = G_GATE_PORT;
  470. for (;;) {
  471. int ch;
  472. ch = getopt(argc, argv, "a:hnp:R:S:v");
  473. if (ch == -1)
  474. break;
  475. switch (ch) {
  476. case 'a':
  477. bindaddr = g_gate_str2ip(optarg);
  478. if (bindaddr == INADDR_NONE) {
  479. errx(EXIT_FAILURE,
  480. "Invalid IP/host name to bind to.");
  481. }
  482. break;
  483. case 'n':
  484. nagle = 0;
  485. break;
  486. case 'p':
  487. errno = 0;
  488. port = strtoul(optarg, NULL, 10);
  489. if (port == 0 && errno != 0)
  490. errx(EXIT_FAILURE, "Invalid port.");
  491. break;
  492. case 'R':
  493. errno = 0;
  494. rcvbuf = strtoul(optarg, NULL, 10);
  495. if (rcvbuf == 0 && errno != 0)
  496. errx(EXIT_FAILURE, "Invalid rcvbuf.");
  497. break;
  498. case 'S':
  499. errno = 0;
  500. sndbuf = strtoul(optarg, NULL, 10);
  501. if (sndbuf == 0 && errno != 0)
  502. errx(EXIT_FAILURE, "Invalid sndbuf.");
  503. break;
  504. case 'v':
  505. g_gate_verbose++;
  506. break;
  507. case 'h':
  508. default:
  509. usage();
  510. }
  511. }
  512. argc -= optind;
  513. argv += optind;
  514. if (argv[0] != NULL)
  515. exports = argv[0];
  516. exports_get();
  517. if (!g_gate_verbose) {
  518. /* Run in daemon mode. */
  519. if (daemon(0, 0) < 0)
  520. g_gate_xlog("Can't daemonize: %s", strerror(errno));
  521. }
  522. signal(SIGCHLD, SIG_IGN);
  523. sfd = socket(AF_INET, SOCK_STREAM, 0);
  524. if (sfd < 0)
  525. g_gate_xlog("Can't open stream socket: %s.", strerror(errno));
  526. bzero(&serv, sizeof(serv));
  527. serv.sin_family = AF_INET;
  528. serv.sin_addr.s_addr = bindaddr;
  529. serv.sin_port = htons(port);
  530. on = 1;
  531. if (nagle) {
  532. if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &on,
  533. sizeof(on)) < 0) {
  534. g_gate_xlog("setsockopt() error: %s.", strerror(errno));
  535. }
  536. }
  537. if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
  538. g_gate_xlog("setsockopt(): %s.", strerror(errno));
  539. bsize = rcvbuf;
  540. if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(bsize)) < 0)
  541. g_gate_xlog("setsockopt(): %s.", strerror(errno));
  542. bsize = sndbuf;
  543. if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &bsize, sizeof(bsize)) < 0)
  544. g_gate_xlog("setsockopt(): %s.", strerror(errno));
  545. tv.tv_sec = 10;
  546. tv.tv_usec = 0;
  547. if (setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) ||
  548. setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
  549. g_gate_xlog("setsockopt() error: %s.", strerror(errno));
  550. }
  551. if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) < 0)
  552. g_gate_xlog("bind(): %s.", strerror(errno));
  553. if (listen(sfd, 5) < 0)
  554. g_gate_xlog("listen(): %s.", strerror(errno));
  555. g_gate_log(LOG_INFO, "Listen on port: %d.", port);
  556. signal(SIGHUP, huphandler);
  557. for (;;) {
  558. fromlen = sizeof(from);
  559. tmpsfd = accept(sfd, &from, &fromlen);
  560. if (tmpsfd < 0)
  561. g_gate_xlog("accept(): %s.", strerror(errno));
  562. if (got_sighup) {
  563. got_sighup = 0;
  564. exports_get();
  565. }
  566. if (exports_find(&from, NULL) == NULL) {
  567. close(tmpsfd);
  568. continue;
  569. }
  570. childpid = fork();
  571. if (childpid < 0) {
  572. g_gate_xlog("Cannot create child process: %s.",
  573. strerror(errno));
  574. } else if (childpid == 0) {
  575. close(sfd);
  576. serve(tmpsfd, &from);
  577. /* NOTREACHED */
  578. }
  579. close(tmpsfd);
  580. }
  581. close(sfd);
  582. exit(EXIT_SUCCESS);
  583. }