620
650
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
621
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 delete_local_route() */
 
 
706
__attribute__((nonnull, warn_unused_result))
 
 
707
static bool add_delete_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 debug_flag[] = "--debug";
 
 
715
  char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
 
 
716
  if(pluginhelperdir == NULL){
 
 
718
      fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
 
 
719
                   " variable not set; cannot run helper\n");
 
 
724
  char interface[IF_NAMESIZE];
 
 
725
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
 
726
    perror_plus("if_indextoname");
 
 
730
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
732
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
 
738
    /* Raise privileges */
 
 
739
    errno = raise_privileges_permanently();
 
 
741
      perror_plus("Failed to raise privileges");
 
 
742
      /* _exit(EX_NOPERM); */
 
 
748
        perror_plus("setgid");
 
 
751
      /* Reset supplementary groups */
 
 
753
      ret = setgroups(0, NULL);
 
 
755
        perror_plus("setgroups");
 
 
759
    ret = dup2(devnull, STDIN_FILENO);
 
 
761
      perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
764
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
 
766
      perror_plus("close");
 
 
769
    ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
771
      perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
774
    int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
 
 
779
    if(helperdir_fd == -1){
 
 
781
      _exit(EX_UNAVAILABLE);
 
 
783
    int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
 
 
786
      perror_plus("openat");
 
 
787
      _exit(EX_UNAVAILABLE);
 
 
789
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
 
791
#pragma GCC diagnostic push
 
 
792
#pragma GCC diagnostic ignored "-Wcast-qual"
 
 
794
    if(fexecve(helper_fd, (char *const [])
 
 
795
               { helper, add ? add_arg : delete_arg, (char *)address,
 
 
796
                   interface, debug ? debug_flag : NULL, NULL },
 
 
799
#pragma GCC diagnostic pop
 
 
801
      perror_plus("fexecve");
 
 
813
    pret = waitpid(pid, &status, 0);
 
 
814
    if(pret == -1 and errno == EINTR and quit_now){
 
 
815
      int errno_raising = 0;
 
 
816
      if((errno = raise_privileges()) != 0){
 
 
817
        errno_raising = errno;
 
 
818
        perror_plus("Failed to raise privileges in order to"
 
 
819
                    " kill helper program");
 
 
821
      if(kill(pid, SIGTERM) == -1){
 
 
824
      if((errno_raising == 0) and (errno = lower_privileges()) != 0){
 
 
825
        perror_plus("Failed to lower privileges after killing"
 
 
830
  } while(pret == -1 and errno == EINTR);
 
 
832
    perror_plus("waitpid");
 
 
835
  if(WIFEXITED(status)){
 
 
836
    if(WEXITSTATUS(status) != 0){
 
 
837
      fprintf_plus(stderr, "Error: iprouteadddel exited"
 
 
838
                   " with status %d\n", WEXITSTATUS(status));
 
 
843
  if(WIFSIGNALED(status)){
 
 
844
    fprintf_plus(stderr, "Error: iprouteadddel died by"
 
 
845
                 " signal %d\n", WTERMSIG(status));
 
 
848
  fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
 
 
852
__attribute__((nonnull, warn_unused_result))
 
 
853
static bool add_local_route(const char *address,
 
 
854
                            AvahiIfIndex if_index){
 
 
856
    fprintf_plus(stderr, "Adding route to %s\n", address);
 
 
858
  return add_delete_local_route(true, address, if_index);
 
 
861
__attribute__((nonnull, warn_unused_result))
 
 
862
static bool delete_local_route(const char *address,
 
 
863
                               AvahiIfIndex if_index){
 
 
865
    fprintf_plus(stderr, "Removing route to %s\n", address);
 
 
867
  return add_delete_local_route(false, address, if_index);
 
623
870
/* Called when a Mandos server is found */
 
 
871
__attribute__((nonnull, warn_unused_result))
 
624
872
static int start_mandos_communication(const char *ip, in_port_t port,
 
625
873
                                      AvahiIfIndex if_index,
 
626
874
                                      int af, mandos_context *mc){
 
627
875
  int ret, tcp_sd = -1;
 
630
 
    struct sockaddr_in in;
 
631
 
    struct sockaddr_in6 in6;
 
 
877
  struct sockaddr_storage to;
 
633
878
  char *buffer = NULL;
 
634
879
  char *decrypted_buffer = NULL;
 
635
880
  size_t buffer_length = 0;
 
 
775
1018
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
 
776
1019
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
 
779
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
 
782
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
 
786
 
      perror_plus("inet_ntop");
 
788
 
      if(strcmp(addrstr, ip) != 0){
 
789
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
800
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
802
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
805
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
807
 
      perror_plus("connect");
 
 
1021
      ret = getnameinfo((struct sockaddr *)&to,
 
 
1022
                        sizeof(struct sockaddr_in6),
 
 
1023
                        addrstr, sizeof(addrstr), NULL, 0,
 
 
1026
      ret = getnameinfo((struct sockaddr *)&to,
 
 
1027
                        sizeof(struct sockaddr_in),
 
 
1028
                        addrstr, sizeof(addrstr), NULL, 0,
 
 
1031
    if(ret == EAI_SYSTEM){
 
 
1032
      perror_plus("getnameinfo");
 
 
1033
    } else if(ret != 0) {
 
 
1034
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
 
1035
    } else if(strcmp(addrstr, ip) != 0){
 
 
1036
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
 
1047
      ret = connect(tcp_sd, (struct sockaddr *)&to,
 
 
1048
                    sizeof(struct sockaddr_in6));
 
 
1050
      ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
 
1051
                    sizeof(struct sockaddr_in));
 
 
1054
      if(errno == ENETUNREACH
 
 
1055
         and if_index != AVAHI_IF_UNSPEC
 
 
1056
         and connect_to == NULL
 
 
1057
         and not route_added and
 
 
1058
         ((af == AF_INET6 and not
 
 
1059
           IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
 
 
1061
          or (af == AF_INET and
 
 
1062
              /* Not a a IPv4LL address */
 
 
1063
              (ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
 
 
1064
               & 0xFFFF0000L) != 0xA9FE0000L))){
 
 
1065
        /* Work around Avahi bug - Avahi does not announce link-local
 
 
1066
           addresses if it has a global address, so local hosts with
 
 
1067
           *only* a link-local address (e.g. Mandos clients) cannot
 
 
1068
           connect to a Mandos server announced by Avahi on a server
 
 
1069
           host with a global address.  Work around this by retrying
 
 
1070
           with an explicit route added with the server's address.
 
 
1072
           Avahi bug reference:
 
 
1073
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
 
1074
           https://bugs.debian.org/587961
 
 
1077
          fprintf_plus(stderr, "Mandos server unreachable, trying"
 
 
1081
        route_added = add_local_route(ip, if_index);
 
 
1087
      if(errno != ECONNREFUSED or debug){
 
 
1089
        perror_plus("connect");
 
818
1102
  const char *out = mandos_protocol_version;
 
 
1422
1715
      ret = avahi_simple_poll_iterate(s, (int)block_time);
 
1425
 
      if (ret > 0 or errno != EINTR){
 
 
1718
      if(ret > 0 or errno != EINTR){
 
1426
1719
        return (ret != 1) ? ret : 0;
 
1432
 
/* Set effective uid to 0, return errno */
 
1433
 
error_t raise_privileges(void){
 
1434
 
  error_t old_errno = errno;
 
1435
 
  error_t ret_errno = 0;
 
1436
 
  if(seteuid(0) == -1){
 
1438
 
    perror_plus("seteuid");
 
1444
 
/* Set effective and real user ID to 0.  Return errno. */
 
1445
 
error_t raise_privileges_permanently(void){
 
1446
 
  error_t old_errno = errno;
 
1447
 
  error_t ret_errno = raise_privileges();
 
1452
 
  if(setuid(0) == -1){
 
1454
 
    perror_plus("seteuid");
 
1460
 
/* Set effective user ID to unprivileged saved user ID */
 
1461
 
error_t lower_privileges(void){
 
1462
 
  error_t old_errno = errno;
 
1463
 
  error_t ret_errno = 0;
 
1464
 
  if(seteuid(uid) == -1){
 
1466
 
    perror_plus("seteuid");
 
1472
 
/* Lower privileges permanently */
 
1473
 
error_t lower_privileges_permanently(void){
 
1474
 
  error_t old_errno = errno;
 
1475
 
  error_t ret_errno = 0;
 
1476
 
  if(setuid(uid) == -1){
 
1478
 
    perror_plus("setuid");
 
1484
 
bool run_network_hooks(const char *mode, const char *interface,
 
 
1725
__attribute__((nonnull))
 
 
1726
void run_network_hooks(const char *mode, const char *interface,
 
1485
1727
                       const float delay){
 
1486
 
  struct dirent **direntries;
 
 
1728
  struct dirent **direntries = NULL;
 
 
1729
  if(hookdir_fd == -1){
 
 
1730
    hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
 
 
1732
    if(hookdir_fd == -1){
 
 
1733
      if(errno == ENOENT){
 
 
1735
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
 
1736
                       " found\n", hookdir);
 
 
1739
        perror_plus("open");
 
 
1745
#if __GLIBC_PREREQ(2, 15)
 
 
1746
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
 
1747
                           runnable_hook, alphasort);
 
 
1748
#else  /* not __GLIBC_PREREQ(2, 15) */
 
 
1749
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1751
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
 
1752
#else   /* not __GLIBC__ */
 
 
1753
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1755
#endif  /* not __GLIBC__ */
 
 
1757
    perror_plus("scandir");
 
1487
1760
  struct dirent *direntry;
 
1489
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1492
 
    if(errno == ENOENT){
 
1494
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1495
 
                     " found\n", hookdir);
 
1498
 
      perror_plus("scandir");
 
 
1762
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
1764
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
 
1767
  for(int i = 0; i < numhooks; i++){
 
 
1768
    direntry = direntries[i];
 
 
1770
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1501
 
    int devnull = open("/dev/null", O_RDONLY);
 
1502
 
    for(int i = 0; i < numhooks; i++){
 
1503
 
      direntry = direntries[i];
 
1504
 
      char *fullname = NULL;
 
1505
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
 
1773
    pid_t hook_pid = fork();
 
 
1776
      /* Raise privileges */
 
 
1777
      errno = raise_privileges_permanently();
 
 
1779
        perror_plus("Failed to raise privileges");
 
 
1786
        perror_plus("setgid");
 
 
1789
      /* Reset supplementary groups */
 
 
1791
      ret = setgroups(0, NULL);
 
 
1793
        perror_plus("setgroups");
 
 
1796
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
 
1798
        perror_plus("setenv");
 
 
1801
      ret = setenv("DEVICE", interface, 1);
 
 
1803
        perror_plus("setenv");
 
 
1806
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
 
1808
        perror_plus("setenv");
 
 
1811
      ret = setenv("MODE", mode, 1);
 
 
1813
        perror_plus("setenv");
 
 
1817
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1507
1819
        perror_plus("asprintf");
 
1511
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1514
 
      pid_t hook_pid = fork();
 
1517
 
        /* Raise privileges */
 
1518
 
        raise_privileges_permanently();
 
1523
 
          perror_plus("setgid");
 
1525
 
        /* Reset supplementary groups */
 
1527
 
        ret = setgroups(0, NULL);
 
1529
 
          perror_plus("setgroups");
 
1531
 
        dup2(devnull, STDIN_FILENO);
 
1533
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1534
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1536
 
          perror_plus("setenv");
 
1539
 
        ret = setenv("DEVICE", interface, 1);
 
1541
 
          perror_plus("setenv");
 
1544
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1546
 
          perror_plus("setenv");
 
1549
 
        ret = setenv("MODE", mode, 1);
 
1551
 
          perror_plus("setenv");
 
1555
 
        ret = asprintf(&delaystring, "%f", delay);
 
1557
 
          perror_plus("asprintf");
 
1560
 
        ret = setenv("DELAY", delaystring, 1);
 
1563
 
          perror_plus("setenv");
 
 
1822
      ret = setenv("DELAY", delaystring, 1);
 
1566
1824
        free(delaystring);
 
1567
 
        if(connect_to != NULL){
 
1568
 
          ret = setenv("CONNECT", connect_to, 1);
 
1570
 
            perror_plus("setenv");
 
1574
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1575
 
          perror_plus("execl");
 
1576
 
          _exit(EXIT_FAILURE);
 
 
1825
        perror_plus("setenv");
 
 
1829
      if(connect_to != NULL){
 
 
1830
        ret = setenv("CONNECT", connect_to, 1);
 
 
1832
          perror_plus("setenv");
 
 
1836
      int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
 
 
1840
        perror_plus("openat");
 
 
1841
        _exit(EXIT_FAILURE);
 
 
1843
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
 
1844
        perror_plus("close");
 
 
1845
        _exit(EXIT_FAILURE);
 
 
1847
      ret = dup2(devnull, STDIN_FILENO);
 
 
1849
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
1852
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
 
1854
        perror_plus("close");
 
 
1857
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
1859
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
1862
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
 
1864
        perror_plus("fexecve");
 
 
1865
        _exit(EXIT_FAILURE);
 
 
1869
        perror_plus("fork");
 
 
1874
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
 
1875
        perror_plus("waitpid");
 
 
1879
      if(WIFEXITED(status)){
 
 
1880
        if(WEXITSTATUS(status) != 0){
 
 
1881
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
 
1882
                       " with status %d\n", direntry->d_name,
 
 
1883
                       WEXITSTATUS(status));
 
 
1887
      } else if(WIFSIGNALED(status)){
 
 
1888
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
 
1889
                     " signal %d\n", direntry->d_name,
 
1580
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1581
 
          perror_plus("waitpid");
 
1585
 
        if(WIFEXITED(status)){
 
1586
 
          if(WEXITSTATUS(status) != 0){
 
1587
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1588
 
                         " with status %d\n", direntry->d_name,
 
1589
 
                         WEXITSTATUS(status));
 
1593
 
        } else if(WIFSIGNALED(status)){
 
1594
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1595
 
                       " signal %d\n", direntry->d_name,
 
1600
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1601
 
                       " crashed\n", direntry->d_name);
 
1608
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
1894
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
 
1895
                     " crashed\n", direntry->d_name);
 
 
1901
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
1907
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
 
1908
    perror_plus("close");
 
 
1915
__attribute__((nonnull, warn_unused_result))
 
1617
1916
error_t bring_up_interface(const char *const interface,
 
1618
1917
                           const float delay){
 
1620
1918
  error_t old_errno = errno;
 
1621
 
  error_t ret_errno = 0;
 
1622
 
  int ret, ret_setflags;
 
1623
1920
  struct ifreq network;
 
1624
1921
  unsigned int if_index = if_nametoindex(interface);
 
1625
1922
  if(if_index == 0){
 
 
2437
2784
    mc.current_server->prev->next = NULL;
 
2438
2785
    while(mc.current_server != NULL){
 
2439
2786
      server *next = mc.current_server->next;
 
 
2788
#pragma GCC diagnostic push
 
 
2789
#pragma GCC diagnostic ignored "-Wcast-qual"
 
 
2791
      free((char *)(mc.current_server->ip));
 
 
2793
#pragma GCC diagnostic pop
 
2440
2795
      free(mc.current_server);
 
2441
2796
      mc.current_server = next;
 
2445
 
  /* Re-raise priviliges */
 
 
2800
  /* Re-raise privileges */
 
2449
 
    /* Run network hooks */
 
2450
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2451
 
                      interfaces_hooks : "", delay);
 
2453
 
    /* Take down the network interfaces which were brought up */
 
2455
 
      char *interface = NULL;
 
2456
 
      while((interface=argz_next(interfaces_to_take_down,
 
2457
 
                                 interfaces_to_take_down_size,
 
2459
 
        ret_errno = take_down_interface(interface);
 
2462
 
          perror_plus("Failed to take down interface");
 
2465
 
      if(debug and (interfaces_to_take_down == NULL)){
 
2466
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2471
 
    lower_privileges_permanently();
 
 
2802
    ret_errno = raise_privileges();
 
 
2805
      perror_plus("Failed to raise privileges");
 
 
2808
      /* Run network hooks */
 
 
2809
      run_network_hooks("stop", interfaces_hooks != NULL ?
 
 
2810
                        interfaces_hooks : "", delay);
 
 
2812
      /* Take down the network interfaces which were brought up */
 
 
2814
        char *interface = NULL;
 
 
2815
        while((interface=argz_next(interfaces_to_take_down,
 
 
2816
                                   interfaces_to_take_down_size,
 
 
2818
          ret_errno = take_down_interface(interface);
 
 
2821
            perror_plus("Failed to take down interface");
 
 
2824
        if(debug and (interfaces_to_take_down == NULL)){
 
 
2825
          fprintf_plus(stderr, "No interfaces needed to be taken"
 
 
2831
    ret_errno = lower_privileges_permanently();
 
 
2834
      perror_plus("Failed to lower privileges permanently");
 
2474
2838
  free(interfaces_to_take_down);
 
2475
2839
  free(interfaces_hooks);
 
2477
2841
  /* Removes the GPGME temp directory and all files inside */
 
2478
 
  if(tempdir_created){
 
 
2842
  if(tempdir != NULL){
 
2479
2843
    struct dirent **direntries = NULL;
 
2480
 
    struct dirent *direntry = NULL;
 
2481
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2483
 
    if (numentries > 0){
 
2484
 
      for(int i = 0; i < numentries; i++){
 
2485
 
        direntry = direntries[i];
 
2486
 
        char *fullname = NULL;
 
2487
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2490
 
          perror_plus("asprintf");
 
2493
 
        ret = remove(fullname);
 
2495
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
 
2844
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
 
2848
    if(tempdir_fd == -1){
 
 
2849
      perror_plus("open");
 
 
2852
#if __GLIBC_PREREQ(2, 15)
 
 
2853
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
 
2854
                                 notdotentries, alphasort);
 
 
2855
#else  /* not __GLIBC_PREREQ(2, 15) */
 
 
2856
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2858
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
 
2859
#else   /* not __GLIBC__ */
 
 
2860
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2862
#endif  /* not __GLIBC__ */
 
 
2863
      if(numentries >= 0){
 
 
2864
        for(int i = 0; i < numentries; i++){
 
 
2865
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
 
2867
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
 
2868
                         " \"%s\", 0): %s\n", tempdir,
 
 
2869
                         direntries[i]->d_name, strerror(errno));
 
 
2871
          free(direntries[i]);
 
 
2874
        /* need to clean even if 0 because man page doesn't specify */
 
 
2876
        if(numentries == -1){
 
 
2877
          perror_plus("scandir");
 
 
2879
        ret = rmdir(tempdir);
 
 
2880
        if(ret == -1 and errno != ENOENT){
 
 
2881
          perror_plus("rmdir");
 
2502
 
    /* need to clean even if 0 because man page doesn't specify */
 
2504
 
    if (numentries == -1){
 
2505
 
      perror_plus("scandir");
 
2507
 
    ret = rmdir(tempdir);
 
2508
 
    if(ret == -1 and errno != ENOENT){
 
2509
 
      perror_plus("rmdir");
 
 
2884
      TEMP_FAILURE_RETRY(close(tempdir_fd));