638
650
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
639
651
                      __attribute__((unused)) const char *txt){}
 
 
653
/* Set effective uid to 0, return errno */
 
 
654
__attribute__((warn_unused_result))
 
 
655
error_t raise_privileges(void){
 
 
656
  error_t old_errno = errno;
 
 
657
  error_t ret_errno = 0;
 
 
658
  if(seteuid(0) == -1){
 
 
665
/* Set effective and real user ID to 0.  Return errno. */
 
 
666
__attribute__((warn_unused_result))
 
 
667
error_t raise_privileges_permanently(void){
 
 
668
  error_t old_errno = errno;
 
 
669
  error_t ret_errno = raise_privileges();
 
 
681
/* Set effective user ID to unprivileged saved user ID */
 
 
682
__attribute__((warn_unused_result))
 
 
683
error_t lower_privileges(void){
 
 
684
  error_t old_errno = errno;
 
 
685
  error_t ret_errno = 0;
 
 
686
  if(seteuid(uid) == -1){
 
 
693
/* Lower privileges permanently */
 
 
694
__attribute__((warn_unused_result))
 
 
695
error_t lower_privileges_permanently(void){
 
 
696
  error_t old_errno = errno;
 
 
697
  error_t ret_errno = 0;
 
 
698
  if(setuid(uid) == -1){
 
 
705
/* Helper function to add_local_route() and remove_local_route() */
 
 
706
__attribute__((nonnull, warn_unused_result))
 
 
707
static bool add_remove_local_route(const bool add,
 
 
709
                                   AvahiIfIndex if_index){
 
 
711
  char helper[] = "mandos-client-iprouteadddel";
 
 
712
  char add_arg[] = "add";
 
 
713
  char delete_arg[] = "delete";
 
 
714
  char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
 
 
715
  if(pluginhelperdir == NULL){
 
 
717
      fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
 
 
718
                   " variable not set; cannot run helper\n");
 
 
723
  char interface[IF_NAMESIZE];
 
 
724
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
 
725
    perror_plus("if_indextoname");
 
 
729
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
731
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
 
737
    /* Raise privileges */
 
 
738
    errno = raise_privileges_permanently();
 
 
740
      perror_plus("Failed to raise privileges");
 
 
741
      /* _exit(EX_NOPERM); */
 
 
747
        perror_plus("setgid");
 
 
750
      /* Reset supplementary groups */
 
 
752
      ret = setgroups(0, NULL);
 
 
754
        perror_plus("setgroups");
 
 
758
    ret = dup2(devnull, STDIN_FILENO);
 
 
760
      perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
763
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
 
765
      perror_plus("close");
 
 
768
    ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
770
      perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
773
    int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
 
 
778
    if(helperdir_fd == -1){
 
 
780
      _exit(EX_UNAVAILABLE);
 
 
782
    int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
 
 
785
      perror_plus("openat");
 
 
786
      _exit(EX_UNAVAILABLE);
 
 
788
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
 
790
#pragma GCC diagnostic push
 
 
791
#pragma GCC diagnostic ignored "-Wcast-qual"
 
 
793
    if(fexecve(helper_fd, (char *const [])
 
 
794
               { helper, add ? add_arg : delete_arg, (char *)address,
 
 
795
                   interface, NULL }, environ) == -1){
 
 
797
#pragma GCC diagnostic pop
 
 
799
      perror_plus("fexecve");
 
 
811
    pret = waitpid(pid, &status, 0);
 
 
812
    if(pret == -1 and errno == EINTR and quit_now){
 
 
813
      int errno_raising = 0;
 
 
814
      if((errno = raise_privileges()) != 0){
 
 
815
        errno_raising = errno;
 
 
816
        perror_plus("Failed to raise privileges in order to"
 
 
817
                    " kill helper program");
 
 
819
      if(kill(pid, SIGTERM) == -1){
 
 
822
      if((errno_raising == 0) and (errno = lower_privileges()) != 0){
 
 
823
        perror_plus("Failed to lower privileges after killing"
 
 
828
  } while(pret == -1 and errno == EINTR);
 
 
830
    perror_plus("waitpid");
 
 
833
  if(WIFEXITED(status)){
 
 
834
    if(WEXITSTATUS(status) != 0){
 
 
835
      fprintf_plus(stderr, "Error: iprouteadddel exited"
 
 
836
                   " with status %d\n", WEXITSTATUS(status));
 
 
841
  if(WIFSIGNALED(status)){
 
 
842
    fprintf_plus(stderr, "Error: iprouteadddel died by"
 
 
843
                 " signal %d\n", WTERMSIG(status));
 
 
846
  fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
 
 
850
__attribute__((nonnull, warn_unused_result))
 
 
851
static bool add_local_route(const char *address,
 
 
852
                            AvahiIfIndex if_index){
 
 
853
  return add_remove_local_route(true, address, if_index);
 
 
856
__attribute__((nonnull, warn_unused_result))
 
 
857
static bool remove_local_route(const char *address,
 
 
858
                               AvahiIfIndex if_index){
 
 
859
  return add_remove_local_route(false, address, if_index);
 
641
862
/* Called when a Mandos server is found */
 
642
863
__attribute__((nonnull, warn_unused_result))
 
643
864
static int start_mandos_communication(const char *ip, in_port_t port,
 
 
812
1034
    goto mandos_end;
 
816
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
817
 
                  sizeof(struct sockaddr_in6));
 
819
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
820
 
                  sizeof(struct sockaddr_in));
 
823
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
825
 
      perror_plus("connect");
 
 
1039
      ret = connect(tcp_sd, (struct sockaddr *)&to,
 
 
1040
                    sizeof(struct sockaddr_in6));
 
 
1042
      ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
 
1043
                    sizeof(struct sockaddr_in));
 
 
1046
      if(errno == ENETUNREACH
 
 
1047
         and if_index != AVAHI_IF_UNSPEC
 
 
1048
         and connect_to == NULL
 
 
1049
         and not route_added and
 
 
1050
         ((af == AF_INET6 and not
 
 
1051
           IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
 
 
1053
          or (af == AF_INET and
 
 
1054
              /* Not a a IPv4LL address */
 
 
1055
              (ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
 
 
1056
               & 0xFFFF0000L) != 0xA9FE0000L))){
 
 
1057
        /* Work around Avahi bug - Avahi does not announce link-local
 
 
1058
           addresses if it has a global address, so local hosts with
 
 
1059
           *only* a link-local address (e.g. Mandos clients) cannot
 
 
1060
           connect to a Mandos server announced by Avahi on a server
 
 
1061
           host with a global address.  Work around this by retrying
 
 
1062
           with an explicit route added with the server's address.
 
 
1064
           Avahi bug reference:
 
 
1065
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
 
1066
           https://bugs.debian.org/587961
 
 
1069
        route_added = add_local_route(ip, if_index);
 
 
1075
      if(errno != ECONNREFUSED or debug){
 
 
1077
        perror_plus("connect");
 
836
1090
  const char *out = mandos_protocol_version;
 
 
1460
 
/* Set effective uid to 0, return errno */
 
1461
 
__attribute__((warn_unused_result))
 
1462
 
error_t raise_privileges(void){
 
1463
 
  error_t old_errno = errno;
 
1464
 
  error_t ret_errno = 0;
 
1465
 
  if(seteuid(0) == -1){
 
1467
 
    perror_plus("seteuid");
 
1473
 
/* Set effective and real user ID to 0.  Return errno. */
 
1474
 
__attribute__((warn_unused_result))
 
1475
 
error_t raise_privileges_permanently(void){
 
1476
 
  error_t old_errno = errno;
 
1477
 
  error_t ret_errno = raise_privileges();
 
1482
 
  if(setuid(0) == -1){
 
1484
 
    perror_plus("seteuid");
 
1490
 
/* Set effective user ID to unprivileged saved user ID */
 
1491
 
__attribute__((warn_unused_result))
 
1492
 
error_t lower_privileges(void){
 
1493
 
  error_t old_errno = errno;
 
1494
 
  error_t ret_errno = 0;
 
1495
 
  if(seteuid(uid) == -1){
 
1497
 
    perror_plus("seteuid");
 
1503
 
/* Lower privileges permanently */
 
1504
 
__attribute__((warn_unused_result))
 
1505
 
error_t lower_privileges_permanently(void){
 
1506
 
  error_t old_errno = errno;
 
1507
 
  error_t ret_errno = 0;
 
1508
 
  if(setuid(uid) == -1){
 
1510
 
    perror_plus("setuid");
 
1516
1713
__attribute__((nonnull))
 
1517
1714
void run_network_hooks(const char *mode, const char *interface,
 
1518
1715
                       const float delay){
 
1519
 
  struct dirent **direntries;
 
1520
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1716
  struct dirent **direntries = NULL;
 
 
1717
  if(hookdir_fd == -1){
 
 
1718
    hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
 
 
1720
    if(hookdir_fd == -1){
 
 
1721
      if(errno == ENOENT){
 
 
1723
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
 
1724
                       " found\n", hookdir);
 
 
1727
        perror_plus("open");
 
 
1733
#if __GLIBC_PREREQ(2, 15)
 
 
1734
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
 
1735
                           runnable_hook, alphasort);
 
 
1736
#else  /* not __GLIBC_PREREQ(2, 15) */
 
 
1737
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1739
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
 
1740
#else   /* not __GLIBC__ */
 
 
1741
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1743
#endif  /* not __GLIBC__ */
 
1522
1744
  if(numhooks == -1){
 
1523
 
    if(errno == ENOENT){
 
1525
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1526
 
                     " found\n", hookdir);
 
1529
 
      perror_plus("scandir");
 
 
1745
    perror_plus("scandir");
 
 
1748
  struct dirent *direntry;
 
 
1750
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
1752
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
 
1755
  for(int i = 0; i < numhooks; i++){
 
 
1756
    direntry = direntries[i];
 
 
1758
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1532
 
    struct dirent *direntry;
 
1534
 
    int devnull = open("/dev/null", O_RDONLY);
 
1535
 
    for(int i = 0; i < numhooks; i++){
 
1536
 
      direntry = direntries[i];
 
1537
 
      char *fullname = NULL;
 
1538
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
 
1761
    pid_t hook_pid = fork();
 
 
1764
      /* Raise privileges */
 
 
1765
      errno = raise_privileges_permanently();
 
 
1767
        perror_plus("Failed to raise privileges");
 
 
1774
        perror_plus("setgid");
 
 
1777
      /* Reset supplementary groups */
 
 
1779
      ret = setgroups(0, NULL);
 
 
1781
        perror_plus("setgroups");
 
 
1784
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
 
1786
        perror_plus("setenv");
 
 
1789
      ret = setenv("DEVICE", interface, 1);
 
 
1791
        perror_plus("setenv");
 
 
1794
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
 
1796
        perror_plus("setenv");
 
 
1799
      ret = setenv("MODE", mode, 1);
 
 
1801
        perror_plus("setenv");
 
 
1805
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1540
1807
        perror_plus("asprintf");
 
1544
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1547
 
      pid_t hook_pid = fork();
 
1550
 
        /* Raise privileges */
 
1551
 
        if(raise_privileges_permanently() != 0){
 
1552
 
          perror_plus("Failed to raise privileges");
 
1559
 
          perror_plus("setgid");
 
1562
 
        /* Reset supplementary groups */
 
1564
 
        ret = setgroups(0, NULL);
 
1566
 
          perror_plus("setgroups");
 
1569
 
        ret = dup2(devnull, STDIN_FILENO);
 
1571
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
1574
 
        ret = close(devnull);
 
1576
 
          perror_plus("close");
 
1579
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1581
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1584
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1586
 
          perror_plus("setenv");
 
1589
 
        ret = setenv("DEVICE", interface, 1);
 
1591
 
          perror_plus("setenv");
 
1594
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1596
 
          perror_plus("setenv");
 
1599
 
        ret = setenv("MODE", mode, 1);
 
1601
 
          perror_plus("setenv");
 
1605
 
        ret = asprintf(&delaystring, "%f", (double)delay);
 
1607
 
          perror_plus("asprintf");
 
1610
 
        ret = setenv("DELAY", delaystring, 1);
 
1613
 
          perror_plus("setenv");
 
 
1810
      ret = setenv("DELAY", delaystring, 1);
 
1616
1812
        free(delaystring);
 
1617
 
        if(connect_to != NULL){
 
1618
 
          ret = setenv("CONNECT", connect_to, 1);
 
1620
 
            perror_plus("setenv");
 
1624
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1625
 
          perror_plus("execl");
 
1626
 
          _exit(EXIT_FAILURE);
 
 
1813
        perror_plus("setenv");
 
 
1817
      if(connect_to != NULL){
 
 
1818
        ret = setenv("CONNECT", connect_to, 1);
 
 
1820
          perror_plus("setenv");
 
 
1824
      int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
 
 
1828
        perror_plus("openat");
 
 
1829
        _exit(EXIT_FAILURE);
 
 
1831
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
 
1832
        perror_plus("close");
 
 
1833
        _exit(EXIT_FAILURE);
 
 
1835
      ret = dup2(devnull, STDIN_FILENO);
 
 
1837
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
1840
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
 
1842
        perror_plus("close");
 
 
1845
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
1847
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
1850
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
 
1852
        perror_plus("fexecve");
 
 
1853
        _exit(EXIT_FAILURE);
 
 
1857
        perror_plus("fork");
 
 
1862
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
 
1863
        perror_plus("waitpid");
 
 
1867
      if(WIFEXITED(status)){
 
 
1868
        if(WEXITSTATUS(status) != 0){
 
 
1869
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
 
1870
                       " with status %d\n", direntry->d_name,
 
 
1871
                       WEXITSTATUS(status));
 
 
1875
      } else if(WIFSIGNALED(status)){
 
 
1876
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
 
1877
                     " signal %d\n", direntry->d_name,
 
1630
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1631
 
          perror_plus("waitpid");
 
1635
 
        if(WIFEXITED(status)){
 
1636
 
          if(WEXITSTATUS(status) != 0){
 
1637
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1638
 
                         " with status %d\n", direntry->d_name,
 
1639
 
                         WEXITSTATUS(status));
 
1643
 
        } else if(WIFSIGNALED(status)){
 
1644
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1645
 
                       " signal %d\n", direntry->d_name,
 
1650
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1651
 
                       " crashed\n", direntry->d_name);
 
1658
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
1882
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
 
1883
                     " crashed\n", direntry->d_name);
 
 
1889
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
1895
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
 
1896
    perror_plus("close");
 
1666
1903
__attribute__((nonnull, warn_unused_result))
 
 
2576
2829
  /* Removes the GPGME temp directory and all files inside */
 
2577
2830
  if(tempdir != NULL){
 
2578
2831
    struct dirent **direntries = NULL;
 
2579
 
    struct dirent *direntry = NULL;
 
2580
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2583
 
      for(int i = 0; i < numentries; i++){
 
2584
 
        direntry = direntries[i];
 
2585
 
        char *fullname = NULL;
 
2586
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2589
 
          perror_plus("asprintf");
 
2592
 
        ret = remove(fullname);
 
2594
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
 
2832
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
 
2836
    if(tempdir_fd == -1){
 
 
2837
      perror_plus("open");
 
 
2840
#if __GLIBC_PREREQ(2, 15)
 
 
2841
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
 
2842
                                 notdotentries, alphasort);
 
 
2843
#else  /* not __GLIBC_PREREQ(2, 15) */
 
 
2844
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2846
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
 
2847
#else   /* not __GLIBC__ */
 
 
2848
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2850
#endif  /* not __GLIBC__ */
 
 
2851
      if(numentries >= 0){
 
 
2852
        for(int i = 0; i < numentries; i++){
 
 
2853
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
 
2855
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
 
2856
                         " \"%s\", 0): %s\n", tempdir,
 
 
2857
                         direntries[i]->d_name, strerror(errno));
 
 
2859
          free(direntries[i]);
 
 
2862
        /* need to clean even if 0 because man page doesn't specify */
 
 
2864
        if(numentries == -1){
 
 
2865
          perror_plus("scandir");
 
 
2867
        ret = rmdir(tempdir);
 
 
2868
        if(ret == -1 and errno != ENOENT){
 
 
2869
          perror_plus("rmdir");
 
2601
 
    /* need to clean even if 0 because man page doesn't specify */
 
2603
 
    if(numentries == -1){
 
2604
 
      perror_plus("scandir");
 
2606
 
    ret = rmdir(tempdir);
 
2607
 
    if(ret == -1 and errno != ENOENT){
 
2608
 
      perror_plus("rmdir");
 
 
2872
      TEMP_FAILURE_RETRY(close(tempdir_fd));