gnunet-helper-exit.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2011, 2012 Christian Grothoff
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file exit/gnunet-helper-exit.c
  18. *
  19. * @brief the helper for exit nodes. Opens a virtual
  20. * network-interface, sends data received on the if to stdout, sends
  21. * data received on stdin to the interface. The code also enables
  22. * IPv4/IPv6 forwarding and NAT on the current system (the latter on
  23. * an interface specified on the command-line); these changes to the
  24. * network configuration are NOT automatically undone when the program
  25. * is stopped (this is because we cannot be sure that some other
  26. * application didn't enable them before or after us; also, these
  27. * changes should be mostly harmless as it simply turns the system
  28. * into a router).
  29. *
  30. * @author Philipp Tölke
  31. * @author Christian Grothoff
  32. *
  33. * The following list of people have reviewed this code and considered
  34. * it safe since the last modification (if you reviewed it, please
  35. * have your name added to the list):
  36. *
  37. * - Philipp Tölke
  38. */
  39. #include "platform.h"
  40. #ifdef IF_TUN_HDR
  41. #include IF_TUN_HDR
  42. #endif
  43. #if defined(BSD) || defined(SOLARIS)
  44. #define ifr_netmask ifr_ifru.ifru_addr
  45. #define SIOGIFINDEX SIOCGIFINDEX
  46. #endif
  47. /**
  48. * Need 'struct GNUNET_MessageHeader'.
  49. */
  50. #include "gnunet_crypto_lib.h"
  51. #include "gnunet_common.h"
  52. /**
  53. * Need VPN message types.
  54. */
  55. #include "gnunet_protocols.h"
  56. /**
  57. * Should we print (interesting|debug) messages that can happen during
  58. * normal operation?
  59. */
  60. #define DEBUG GNUNET_NO
  61. /**
  62. * Maximum size of a GNUnet message (GNUNET_MAX_MESSAGE_SIZE)
  63. */
  64. #define MAX_SIZE 65536
  65. /**
  66. * Path to 'sysctl' binary.
  67. */
  68. static const char *sbin_sysctl;
  69. /**
  70. * Path to 'iptables' binary.
  71. */
  72. static const char *sbin_iptables;
  73. #if ! defined(__ANDROID__)
  74. #if ! defined(_LINUX_IN6_H) && defined(__linux__)
  75. /**
  76. * This is in linux/include/net/ipv6.h, but not always exported.
  77. */
  78. struct in6_ifreq
  79. {
  80. struct in6_addr ifr6_addr;
  81. uint32_t ifr6_prefixlen; /* __u32 in the original */
  82. int ifr6_ifindex;
  83. };
  84. #endif
  85. #endif
  86. /**
  87. * Open '/dev/null' and make the result the given
  88. * file descriptor.
  89. *
  90. * @param target_fd desired FD to point to /dev/null
  91. * @param flags open flags (O_RDONLY, O_WRONLY)
  92. */
  93. static void
  94. open_dev_null (int target_fd,
  95. int flags)
  96. {
  97. int fd;
  98. fd = open ("/dev/null", flags);
  99. if (-1 == fd)
  100. abort ();
  101. if (fd == target_fd)
  102. return;
  103. if (-1 == dup2 (fd, target_fd))
  104. {
  105. (void) close (fd);
  106. abort ();
  107. }
  108. (void) close (fd);
  109. }
  110. /**
  111. * Run the given command and wait for it to complete.
  112. *
  113. * @param file name of the binary to run
  114. * @param cmd command line arguments (as given to 'execv')
  115. * @return 0 on success, 1 on any error
  116. */
  117. static int
  118. fork_and_exec (const char *file,
  119. char *const cmd[])
  120. {
  121. int status;
  122. pid_t pid;
  123. pid_t ret;
  124. pid = fork ();
  125. if (-1 == pid)
  126. {
  127. fprintf (stderr,
  128. "fork failed: %s\n",
  129. strerror (errno));
  130. return 1;
  131. }
  132. if (0 == pid)
  133. {
  134. /* we are the child process */
  135. /* close stdin/stdout to not cause interference
  136. with the helper's main protocol! */
  137. (void) close (0);
  138. open_dev_null (0, O_RDONLY);
  139. (void) close (1);
  140. open_dev_null (1, O_WRONLY);
  141. (void) execv (file, cmd);
  142. /* can only get here on error */
  143. fprintf (stderr,
  144. "exec `%s' failed: %s\n",
  145. file,
  146. strerror (errno));
  147. _exit (1);
  148. }
  149. /* keep running waitpid as long as the only error we get is 'EINTR' */
  150. while ((-1 == (ret = waitpid (pid, &status, 0))) &&
  151. (errno == EINTR))
  152. ;
  153. if (-1 == ret)
  154. {
  155. fprintf (stderr,
  156. "waitpid failed: %s\n",
  157. strerror (errno));
  158. return 1;
  159. }
  160. if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
  161. return 1;
  162. /* child process completed and returned success, we're happy */
  163. return 0;
  164. }
  165. /**
  166. * Creates a tun-interface called dev;
  167. *
  168. * @param dev is assumed to point to a char[IFNAMSIZ]
  169. * if *dev == '\\0', uses the name supplied by the kernel;
  170. * @return the fd to the tun or -1 on error
  171. */
  172. #ifdef IFF_TUN /* LINUX */
  173. static int
  174. init_tun (char *dev)
  175. {
  176. struct ifreq ifr;
  177. int fd;
  178. if (NULL == dev)
  179. {
  180. errno = EINVAL;
  181. return -1;
  182. }
  183. if (-1 == (fd = open ("/dev/net/tun", O_RDWR)))
  184. {
  185. fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun",
  186. strerror (errno));
  187. return -1;
  188. }
  189. if (fd >= FD_SETSIZE)
  190. {
  191. fprintf (stderr, "File descriptor to large: %d", fd);
  192. (void) close (fd);
  193. return -1;
  194. }
  195. memset (&ifr, 0, sizeof(ifr));
  196. ifr.ifr_flags = IFF_TUN;
  197. if ('\0' != *dev)
  198. strncpy (ifr.ifr_name, dev, IFNAMSIZ);
  199. if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
  200. {
  201. fprintf (stderr,
  202. "Error with ioctl on `%s': %s\n", "/dev/net/tun",
  203. strerror (errno));
  204. (void) close (fd);
  205. return -1;
  206. }
  207. strcpy (dev, ifr.ifr_name);
  208. return fd;
  209. }
  210. #else /* BSD et al, including DARWIN */
  211. #ifdef SIOCIFCREATE
  212. static int
  213. init_tun (char *dev)
  214. {
  215. int fd;
  216. int s;
  217. struct ifreq ifr;
  218. fd = open (dev, O_RDWR);
  219. if (fd == -1)
  220. {
  221. s = socket (AF_INET, SOCK_DGRAM, 0);
  222. if (s < 0)
  223. return -1;
  224. memset (&ifr, 0, sizeof(ifr));
  225. strncpy (ifr.ifr_name, dev + 5, sizeof(ifr.ifr_name) - 1);
  226. if (! ioctl (s, SIOCIFCREATE, &ifr))
  227. fd = open (dev, O_RDWR);
  228. close (s);
  229. }
  230. return fd;
  231. }
  232. #else
  233. #define init_tun(dev) open (dev, O_RDWR)
  234. #endif
  235. #endif /* !IFF_TUN (BSD) */
  236. /**
  237. * @brief Sets the IPv6-Address given in address on the interface dev
  238. *
  239. * @param dev the interface to configure
  240. * @param address the IPv6-Address
  241. * @param prefix_len the length of the network-prefix
  242. */
  243. static void
  244. set_address6 (const char *dev, const char *address, unsigned long prefix_len)
  245. {
  246. struct ifreq ifr;
  247. struct sockaddr_in6 sa6;
  248. int fd;
  249. struct in6_ifreq ifr6;
  250. /*
  251. * parse the new address
  252. */
  253. memset (&sa6, 0, sizeof(struct sockaddr_in6));
  254. sa6.sin6_family = AF_INET6;
  255. if (1 != inet_pton (AF_INET6, address, &sa6.sin6_addr))
  256. {
  257. fprintf (stderr, "Failed to parse address `%s': %s\n", address,
  258. strerror (errno));
  259. exit (1);
  260. }
  261. if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
  262. {
  263. fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
  264. exit (1);
  265. }
  266. memset (&ifr, 0, sizeof(struct ifreq));
  267. /*
  268. * Get the index of the if
  269. */
  270. strncpy (ifr.ifr_name, dev, IFNAMSIZ);
  271. if (-1 == ioctl (fd, SIOGIFINDEX, &ifr))
  272. {
  273. fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
  274. (void) close (fd);
  275. exit (1);
  276. }
  277. memset (&ifr6, 0, sizeof(struct in6_ifreq));
  278. ifr6.ifr6_addr = sa6.sin6_addr;
  279. ifr6.ifr6_ifindex = ifr.ifr_ifindex;
  280. ifr6.ifr6_prefixlen = prefix_len;
  281. /*
  282. * Set the address
  283. */
  284. if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6))
  285. {
  286. fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
  287. strerror (errno));
  288. (void) close (fd);
  289. exit (1);
  290. }
  291. /*
  292. * Get the flags
  293. */
  294. if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
  295. {
  296. fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
  297. strerror (errno));
  298. (void) close (fd);
  299. exit (1);
  300. }
  301. /*
  302. * Add the UP and RUNNING flags
  303. */
  304. ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
  305. if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
  306. {
  307. fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
  308. strerror (errno));
  309. (void) close (fd);
  310. exit (1);
  311. }
  312. if (0 != close (fd))
  313. {
  314. fprintf (stderr, "close failed: %s\n", strerror (errno));
  315. exit (1);
  316. }
  317. }
  318. /**
  319. * @brief Sets the IPv4-Address given in address on the interface dev
  320. *
  321. * @param dev the interface to configure
  322. * @param address the IPv4-Address
  323. * @param mask the netmask
  324. */
  325. static void
  326. set_address4 (const char *dev, const char *address, const char *mask)
  327. {
  328. int fd;
  329. struct sockaddr_in *addr;
  330. struct ifreq ifr;
  331. memset (&ifr, 0, sizeof(struct ifreq));
  332. addr = (struct sockaddr_in *) &(ifr.ifr_addr);
  333. addr->sin_family = AF_INET;
  334. /*
  335. * Parse the address
  336. */
  337. if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
  338. {
  339. fprintf (stderr, "Failed to parse address `%s': %s\n", address,
  340. strerror (errno));
  341. exit (1);
  342. }
  343. if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
  344. {
  345. fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
  346. exit (1);
  347. }
  348. strncpy (ifr.ifr_name, dev, IFNAMSIZ);
  349. /*
  350. * Set the address
  351. */
  352. if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
  353. {
  354. fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
  355. (void) close (fd);
  356. exit (1);
  357. }
  358. /*
  359. * Parse the netmask
  360. */
  361. addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
  362. if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
  363. {
  364. fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
  365. strerror (errno));
  366. (void) close (fd);
  367. exit (1);
  368. }
  369. /*
  370. * Set the netmask
  371. */
  372. if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
  373. {
  374. fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
  375. strerror (errno));
  376. (void) close (fd);
  377. exit (1);
  378. }
  379. /*
  380. * Get the flags
  381. */
  382. if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr))
  383. {
  384. fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
  385. strerror (errno));
  386. (void) close (fd);
  387. exit (1);
  388. }
  389. /*
  390. * Add the UP and RUNNING flags
  391. */
  392. ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
  393. if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr))
  394. {
  395. fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
  396. strerror (errno));
  397. (void) close (fd);
  398. exit (1);
  399. }
  400. if (0 != close (fd))
  401. {
  402. fprintf (stderr, "close failed: %s\n", strerror (errno));
  403. (void) close (fd);
  404. exit (1);
  405. }
  406. }
  407. /**
  408. * Start forwarding to and from the tunnel.
  409. *
  410. * @param fd_tun tunnel FD
  411. */
  412. static void
  413. run (int fd_tun)
  414. {
  415. /*
  416. * The buffer filled by reading from fd_tun
  417. */
  418. unsigned char buftun[MAX_SIZE];
  419. ssize_t buftun_size = 0;
  420. unsigned char *buftun_read = NULL;
  421. /*
  422. * The buffer filled by reading from stdin
  423. */
  424. unsigned char bufin[MAX_SIZE];
  425. ssize_t bufin_size = 0;
  426. size_t bufin_rpos = 0;
  427. unsigned char *bufin_read = NULL;
  428. fd_set fds_w;
  429. fd_set fds_r;
  430. /* read refers to reading from fd_tun, writing to stdout */
  431. int read_open = 1;
  432. /* write refers to reading from stdin, writing to fd_tun */
  433. int write_open = 1;
  434. while ((1 == read_open) && (1 == write_open))
  435. {
  436. FD_ZERO (&fds_w);
  437. FD_ZERO (&fds_r);
  438. /*
  439. * We are supposed to read and the buffer is empty
  440. * -> select on read from tun
  441. */
  442. if (read_open && (0 == buftun_size))
  443. FD_SET (fd_tun, &fds_r);
  444. /*
  445. * We are supposed to read and the buffer is not empty
  446. * -> select on write to stdout
  447. */
  448. if (read_open && (0 != buftun_size))
  449. FD_SET (1, &fds_w);
  450. /*
  451. * We are supposed to write and the buffer is empty
  452. * -> select on read from stdin
  453. */
  454. if (write_open && (NULL == bufin_read))
  455. FD_SET (0, &fds_r);
  456. /*
  457. * We are supposed to write and the buffer is not empty
  458. * -> select on write to tun
  459. */
  460. if (write_open && (NULL != bufin_read))
  461. FD_SET (fd_tun, &fds_w);
  462. int r = select (fd_tun + 1, &fds_r, &fds_w, NULL, NULL);
  463. if (-1 == r)
  464. {
  465. if (EINTR == errno)
  466. continue;
  467. fprintf (stderr, "select failed: %s\n", strerror (errno));
  468. exit (1);
  469. }
  470. if (r > 0)
  471. {
  472. if (FD_ISSET (fd_tun, &fds_r))
  473. {
  474. buftun_size =
  475. read (fd_tun, buftun + sizeof(struct GNUNET_MessageHeader),
  476. MAX_SIZE - sizeof(struct GNUNET_MessageHeader));
  477. if (-1 == buftun_size)
  478. {
  479. fprintf (stderr,
  480. "read-error: %s\n",
  481. strerror (errno));
  482. shutdown (fd_tun, SHUT_RD);
  483. shutdown (1, SHUT_WR);
  484. read_open = 0;
  485. buftun_size = 0;
  486. }
  487. else if (0 == buftun_size)
  488. {
  489. #if DEBUG
  490. fprintf (stderr, "EOF on tun\n");
  491. #endif
  492. shutdown (fd_tun, SHUT_RD);
  493. shutdown (1, SHUT_WR);
  494. read_open = 0;
  495. buftun_size = 0;
  496. }
  497. else
  498. {
  499. buftun_read = buftun;
  500. struct GNUNET_MessageHeader *hdr =
  501. (struct GNUNET_MessageHeader *) buftun;
  502. buftun_size += sizeof(struct GNUNET_MessageHeader);
  503. hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  504. hdr->size = htons (buftun_size);
  505. }
  506. }
  507. else if (FD_ISSET (1, &fds_w))
  508. {
  509. ssize_t written = write (1, buftun_read, buftun_size);
  510. if (-1 == written)
  511. {
  512. #if ! DEBUG
  513. if (errno != EPIPE)
  514. #endif
  515. fprintf (stderr,
  516. "write-error to stdout: %s\n",
  517. strerror (errno));
  518. shutdown (fd_tun, SHUT_RD);
  519. shutdown (1, SHUT_WR);
  520. read_open = 0;
  521. buftun_size = 0;
  522. }
  523. else if (0 == written)
  524. {
  525. fprintf (stderr, "write returned 0!?\n");
  526. exit (1);
  527. }
  528. else
  529. {
  530. buftun_size -= written;
  531. buftun_read += written;
  532. }
  533. }
  534. if (FD_ISSET (0, &fds_r))
  535. {
  536. bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
  537. if (-1 == bufin_size)
  538. {
  539. fprintf (stderr, "read-error: %s\n", strerror (errno));
  540. shutdown (0, SHUT_RD);
  541. shutdown (fd_tun, SHUT_WR);
  542. write_open = 0;
  543. bufin_size = 0;
  544. }
  545. else if (0 == bufin_size)
  546. {
  547. #if DEBUG
  548. fprintf (stderr, "EOF on stdin\n");
  549. #endif
  550. shutdown (0, SHUT_RD);
  551. shutdown (fd_tun, SHUT_WR);
  552. write_open = 0;
  553. bufin_size = 0;
  554. }
  555. else
  556. {
  557. struct GNUNET_MessageHeader *hdr;
  558. PROCESS_BUFFER:
  559. bufin_rpos += bufin_size;
  560. if (bufin_rpos < sizeof(struct GNUNET_MessageHeader))
  561. continue;
  562. hdr = (struct GNUNET_MessageHeader *) bufin;
  563. if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
  564. {
  565. fprintf (stderr, "protocol violation!\n");
  566. exit (1);
  567. }
  568. if (ntohs (hdr->size) > bufin_rpos)
  569. continue;
  570. bufin_read = bufin + sizeof(struct GNUNET_MessageHeader);
  571. bufin_size = ntohs (hdr->size) - sizeof(struct GNUNET_MessageHeader);
  572. bufin_rpos -= bufin_size + sizeof(struct GNUNET_MessageHeader);
  573. }
  574. }
  575. else if (FD_ISSET (fd_tun, &fds_w))
  576. {
  577. ssize_t written = write (fd_tun, bufin_read, bufin_size);
  578. if (-1 == written)
  579. {
  580. fprintf (stderr, "write-error to tun: %s\n", strerror (errno));
  581. shutdown (0, SHUT_RD);
  582. shutdown (fd_tun, SHUT_WR);
  583. write_open = 0;
  584. bufin_size = 0;
  585. }
  586. else if (0 == written)
  587. {
  588. fprintf (stderr, "write returned 0!?\n");
  589. exit (1);
  590. }
  591. else
  592. {
  593. bufin_size -= written;
  594. bufin_read += written;
  595. if (0 == bufin_size)
  596. {
  597. memmove (bufin, bufin_read, bufin_rpos);
  598. bufin_read = NULL; /* start reading again */
  599. bufin_size = 0;
  600. goto PROCESS_BUFFER;
  601. }
  602. }
  603. }
  604. }
  605. }
  606. }
  607. /**
  608. * Open VPN tunnel interface.
  609. *
  610. * @param argc must be 6
  611. * @param argv 0: binary name ("gnunet-helper-exit")
  612. * 1: tunnel interface name ("gnunet-exit")
  613. * 2: "physical" interface name ("eth0"), or "-" to not setup NAT
  614. * and routing
  615. * 3: IPv6 address ("::1"), or "-" to skip IPv6
  616. * 4: IPv6 netmask length in bits ("64") [ignored if #4 is "-"]
  617. * 5: IPv4 address ("1.2.3.4"), or "-" to skip IPv4
  618. * 6: IPv4 netmask ("255.255.0.0") [ignored if #4 is "-"]
  619. */
  620. int
  621. main (int argc, char **argv)
  622. {
  623. char dev[IFNAMSIZ];
  624. int fd_tun;
  625. int global_ret;
  626. if (7 != argc)
  627. {
  628. fprintf (stderr, "Fatal: must supply 6 arguments!\n");
  629. return 1;
  630. }
  631. if ((0 == strcmp (argv[3], "-")) &&
  632. (0 == strcmp (argv[5], "-")))
  633. {
  634. fprintf (stderr, "Fatal: disabling both IPv4 and IPv6 makes no sense.\n");
  635. return 1;
  636. }
  637. if (0 != strcmp (argv[2], "-"))
  638. {
  639. #ifdef IPTABLES
  640. if (0 == access (IPTABLES, X_OK))
  641. sbin_iptables = IPTABLES;
  642. else
  643. #endif
  644. if (0 == access ("/sbin/iptables", X_OK))
  645. sbin_iptables = "/sbin/iptables";
  646. else if (0 == access ("/usr/sbin/iptables", X_OK))
  647. sbin_iptables = "/usr/sbin/iptables";
  648. else
  649. {
  650. fprintf (stderr,
  651. "Fatal: executable iptables not found in approved directories: %s\n",
  652. strerror (errno));
  653. return 1;
  654. }
  655. #ifdef SYSCTL
  656. if (0 == access (SYSCTL, X_OK))
  657. sbin_sysctl = SYSCTL;
  658. else
  659. #endif
  660. if (0 == access ("/sbin/sysctl", X_OK))
  661. sbin_sysctl = "/sbin/sysctl";
  662. else if (0 == access ("/usr/sbin/sysctl", X_OK))
  663. sbin_sysctl = "/usr/sbin/sysctl";
  664. else
  665. {
  666. fprintf (stderr,
  667. "Fatal: executable sysctl not found in approved directories: %s\n",
  668. strerror (errno));
  669. return 1;
  670. }
  671. }
  672. strncpy (dev, argv[1], IFNAMSIZ);
  673. dev[IFNAMSIZ - 1] = '\0';
  674. if (-1 == (fd_tun = init_tun (dev)))
  675. {
  676. fprintf (stderr,
  677. "Fatal: could not initialize tun-interface `%s' with IPv6 %s/%s and IPv4 %s/%s\n",
  678. dev,
  679. argv[3],
  680. argv[4],
  681. argv[5],
  682. argv[6]);
  683. return 1;
  684. }
  685. if (0 != strcmp (argv[3], "-"))
  686. {
  687. {
  688. const char *address = argv[3];
  689. long prefix_len = atol (argv[4]);
  690. if ((prefix_len < 1) || (prefix_len > 127))
  691. {
  692. fprintf (stderr, "Fatal: prefix_len out of range\n");
  693. return 1;
  694. }
  695. set_address6 (dev, address, prefix_len);
  696. }
  697. if (0 != strcmp (argv[2], "-"))
  698. {
  699. char *const sysctl_args[] = {
  700. "sysctl", "-w", "net.ipv6.conf.all.forwarding=1", NULL
  701. };
  702. if (0 != fork_and_exec (sbin_sysctl,
  703. sysctl_args))
  704. {
  705. fprintf (stderr,
  706. "Failed to enable IPv6 forwarding. Will continue anyway.\n");
  707. }
  708. }
  709. }
  710. if (0 != strcmp (argv[5], "-"))
  711. {
  712. {
  713. const char *address = argv[5];
  714. const char *mask = argv[6];
  715. set_address4 (dev, address, mask);
  716. }
  717. if (0 != strcmp (argv[2], "-"))
  718. {
  719. {
  720. char *const sysctl_args[] = {
  721. "sysctl", "-w", "net.ipv4.ip_forward=1", NULL
  722. };
  723. if (0 != fork_and_exec (sbin_sysctl,
  724. sysctl_args))
  725. {
  726. fprintf (stderr,
  727. "Failed to enable IPv4 forwarding. Will continue anyway.\n");
  728. }
  729. }
  730. {
  731. char *const iptables_args[] = {
  732. "iptables", "-t", "nat", "-A", "POSTROUTING", "-o", argv[2], "-j",
  733. "MASQUERADE", NULL
  734. };
  735. if (0 != fork_and_exec (sbin_iptables,
  736. iptables_args))
  737. {
  738. fprintf (stderr,
  739. "Failed to enable IPv4 masquerading (NAT). Will continue anyway.\n");
  740. }
  741. }
  742. }
  743. }
  744. uid_t uid = getuid ();
  745. #ifdef HAVE_SETRESUID
  746. if (0 != setresuid (uid, uid, uid))
  747. {
  748. fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
  749. global_ret = 2;
  750. goto cleanup;
  751. }
  752. #else
  753. if (0 != (setuid (uid) | seteuid (uid)))
  754. {
  755. fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
  756. global_ret = 2;
  757. goto cleanup;
  758. }
  759. #endif
  760. if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
  761. {
  762. fprintf (stderr, "Failed to protect against SIGPIPE: %s\n",
  763. strerror (errno));
  764. /* no exit, we might as well die with SIGPIPE should it ever happen */
  765. }
  766. run (fd_tun);
  767. global_ret = 0;
  768. cleanup:
  769. (void) close (fd_tun);
  770. return global_ret;
  771. }
  772. /* end of gnunet-helper-exit.c */