/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2015-07-16 23:01:02 UTC
  • Revision ID: teddy@recompile.se-20150716230102-62u5uwaguiufc6ac
mandos-client: Don't use memset().

* plugins.d/mandos-client.c (start_mandos_communication): Use compound
                                                          literal
                                                          struct
                                                          assignment
                                                          instead of
                                                          memset().

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2016 Teddy Hogeborn
13
 
 * Copyright © 2008-2016 Björn Påhlsson
 
12
 * Copyright © 2008-2015 Teddy Hogeborn
 
13
 * Copyright © 2008-2015 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
47
47
                                   strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* strcmp(), strlen(), strerror(),
50
 
                                   asprintf(), strncpy() */
 
50
                                   asprintf(), strcpy() */
51
51
#include <sys/ioctl.h>          /* ioctl */
52
52
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
53
                                   sockaddr_in6, PF_INET6,
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
64
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
66
 
                                   EAI_SYSTEM, ENETUNREACH,
67
 
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
68
 
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
69
 
                                   ENOTEMPTY,
 
65
#include <errno.h>              /* perror(), errno,
70
66
                                   program_invocation_short_name */
71
67
#include <time.h>               /* nanosleep(), time(), sleep() */
72
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
309
305
      return false;
310
306
    }
311
307
    
312
 
    ret = close(fd);
 
308
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
313
309
    if(ret == -1){
314
310
      perror_plus("close");
315
311
    }
517
513
  fprintf_plus(stderr, "GnuTLS: %s", string);
518
514
}
519
515
 
520
 
__attribute__((nonnull(1, 2, 4), warn_unused_result))
 
516
__attribute__((nonnull, warn_unused_result))
521
517
static int init_gnutls_global(const char *pubkeyfilename,
522
518
                              const char *seckeyfilename,
523
519
                              const char *dhparamsfilename,
529
525
    fprintf_plus(stderr, "Initializing GnuTLS\n");
530
526
  }
531
527
  
 
528
  ret = gnutls_global_init();
 
529
  if(ret != GNUTLS_E_SUCCESS){
 
530
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
531
                 safer_gnutls_strerror(ret));
 
532
    return -1;
 
533
  }
 
534
  
532
535
  if(debug){
533
536
    /* "Use a log level over 10 to enable all debugging options."
534
537
     * - GnuTLS manual
542
545
  if(ret != GNUTLS_E_SUCCESS){
543
546
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
544
547
                 safer_gnutls_strerror(ret));
 
548
    gnutls_global_deinit();
545
549
    return -1;
546
550
  }
547
551
  
751
755
 globalfail:
752
756
  
753
757
  gnutls_certificate_free_credentials(mc->cred);
 
758
  gnutls_global_deinit();
754
759
  gnutls_dh_params_deinit(mc->dh_params);
755
760
  return -1;
756
761
}
817
822
 
818
823
/* Set effective uid to 0, return errno */
819
824
__attribute__((warn_unused_result))
820
 
int raise_privileges(void){
821
 
  int old_errno = errno;
822
 
  int ret = 0;
 
825
error_t raise_privileges(void){
 
826
  error_t old_errno = errno;
 
827
  error_t ret_errno = 0;
823
828
  if(seteuid(0) == -1){
824
 
    ret = errno;
 
829
    ret_errno = errno;
825
830
  }
826
831
  errno = old_errno;
827
 
  return ret;
 
832
  return ret_errno;
828
833
}
829
834
 
830
835
/* Set effective and real user ID to 0.  Return errno. */
831
836
__attribute__((warn_unused_result))
832
 
int raise_privileges_permanently(void){
833
 
  int old_errno = errno;
834
 
  int ret = raise_privileges();
835
 
  if(ret != 0){
 
837
error_t raise_privileges_permanently(void){
 
838
  error_t old_errno = errno;
 
839
  error_t ret_errno = raise_privileges();
 
840
  if(ret_errno != 0){
836
841
    errno = old_errno;
837
 
    return ret;
 
842
    return ret_errno;
838
843
  }
839
844
  if(setuid(0) == -1){
840
 
    ret = errno;
 
845
    ret_errno = errno;
841
846
  }
842
847
  errno = old_errno;
843
 
  return ret;
 
848
  return ret_errno;
844
849
}
845
850
 
846
851
/* Set effective user ID to unprivileged saved user ID */
847
852
__attribute__((warn_unused_result))
848
 
int lower_privileges(void){
849
 
  int old_errno = errno;
850
 
  int ret = 0;
 
853
error_t lower_privileges(void){
 
854
  error_t old_errno = errno;
 
855
  error_t ret_errno = 0;
851
856
  if(seteuid(uid) == -1){
852
 
    ret = errno;
 
857
    ret_errno = errno;
853
858
  }
854
859
  errno = old_errno;
855
 
  return ret;
 
860
  return ret_errno;
856
861
}
857
862
 
858
863
/* Lower privileges permanently */
859
864
__attribute__((warn_unused_result))
860
 
int lower_privileges_permanently(void){
861
 
  int old_errno = errno;
862
 
  int ret = 0;
 
865
error_t lower_privileges_permanently(void){
 
866
  error_t old_errno = errno;
 
867
  error_t ret_errno = 0;
863
868
  if(setuid(uid) == -1){
864
 
    ret = errno;
 
869
    ret_errno = errno;
865
870
  }
866
871
  errno = old_errno;
867
 
  return ret;
 
872
  return ret_errno;
868
873
}
869
874
 
870
875
/* Helper function to add_local_route() and delete_local_route() */
926
931
      perror_plus("dup2(devnull, STDIN_FILENO)");
927
932
      _exit(EX_OSERR);
928
933
    }
929
 
    ret = close(devnull);
 
934
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
930
935
    if(ret == -1){
931
936
      perror_plus("close");
932
937
      _exit(EX_OSERR);
949
954
                                                   helper, O_RDONLY));
950
955
    if(helper_fd == -1){
951
956
      perror_plus("openat");
952
 
      close(helperdir_fd);
953
957
      _exit(EX_UNAVAILABLE);
954
958
    }
955
 
    close(helperdir_fd);
 
959
    TEMP_FAILURE_RETRY(close(helperdir_fd));
956
960
#ifdef __GNUC__
957
961
#pragma GCC diagnostic push
958
962
#pragma GCC diagnostic ignored "-Wcast-qual"
1218
1222
                    sizeof(struct sockaddr_in));
1219
1223
    }
1220
1224
    if(ret < 0){
1221
 
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
 
1225
      if(errno == ENETUNREACH
1222
1226
         and if_index != AVAHI_IF_UNSPEC
1223
1227
         and connect_to == NULL
1224
1228
         and not route_added and
1462
1466
    free(decrypted_buffer);
1463
1467
    free(buffer);
1464
1468
    if(tcp_sd >= 0){
1465
 
      ret = close(tcp_sd);
 
1469
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
1466
1470
    }
1467
1471
    if(ret == -1){
1468
1472
      if(e == 0){
1623
1627
__attribute__((nonnull, warn_unused_result))
1624
1628
bool get_flags(const char *ifname, struct ifreq *ifr){
1625
1629
  int ret;
1626
 
  int old_errno;
 
1630
  error_t ret_errno;
1627
1631
  
1628
1632
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1629
1633
  if(s < 0){
1630
 
    old_errno = errno;
 
1634
    ret_errno = errno;
1631
1635
    perror_plus("socket");
1632
 
    errno = old_errno;
 
1636
    errno = ret_errno;
1633
1637
    return false;
1634
1638
  }
1635
 
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1636
 
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
 
1639
  strcpy(ifr->ifr_name, ifname);
1637
1640
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1638
1641
  if(ret == -1){
1639
1642
    if(debug){
1640
 
      old_errno = errno;
 
1643
      ret_errno = errno;
1641
1644
      perror_plus("ioctl SIOCGIFFLAGS");
1642
 
      errno = old_errno;
 
1645
      errno = ret_errno;
1643
1646
    }
1644
1647
    return false;
1645
1648
  }
1909
1912
      return;
1910
1913
    }
1911
1914
  }
 
1915
#ifdef __GLIBC__
 
1916
#if __GLIBC_PREREQ(2, 15)
1912
1917
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1913
1918
                           runnable_hook, alphasort);
 
1919
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1920
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1921
                         alphasort);
 
1922
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1923
#else   /* not __GLIBC__ */
 
1924
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1925
                         alphasort);
 
1926
#endif  /* not __GLIBC__ */
1914
1927
  if(numhooks == -1){
1915
1928
    perror_plus("scandir");
1916
1929
    return;
1998
2011
        perror_plus("openat");
1999
2012
        _exit(EXIT_FAILURE);
2000
2013
      }
2001
 
      if(close(hookdir_fd) == -1){
 
2014
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
2002
2015
        perror_plus("close");
2003
2016
        _exit(EXIT_FAILURE);
2004
2017
      }
2007
2020
        perror_plus("dup2(devnull, STDIN_FILENO)");
2008
2021
        _exit(EX_OSERR);
2009
2022
      }
2010
 
      ret = close(devnull);
 
2023
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
2011
2024
      if(ret == -1){
2012
2025
        perror_plus("close");
2013
2026
        _exit(EX_OSERR);
2062
2075
    free(direntry);
2063
2076
  }
2064
2077
  free(direntries);
2065
 
  if(close(hookdir_fd) == -1){
 
2078
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
2066
2079
    perror_plus("close");
2067
2080
  } else {
2068
2081
    hookdir_fd = -1;
2071
2084
}
2072
2085
 
2073
2086
__attribute__((nonnull, warn_unused_result))
2074
 
int bring_up_interface(const char *const interface,
2075
 
                       const float delay){
2076
 
  int old_errno = errno;
 
2087
error_t bring_up_interface(const char *const interface,
 
2088
                           const float delay){
 
2089
  error_t old_errno = errno;
2077
2090
  int ret;
2078
2091
  struct ifreq network;
2079
2092
  unsigned int if_index = if_nametoindex(interface);
2089
2102
  }
2090
2103
  
2091
2104
  if(not interface_is_up(interface)){
2092
 
    int ret_errno = 0;
2093
 
    int ioctl_errno = 0;
 
2105
    error_t ret_errno = 0, ioctl_errno = 0;
2094
2106
    if(not get_flags(interface, &network)){
2095
2107
      ret_errno = errno;
2096
2108
      fprintf_plus(stderr, "Failed to get flags for interface "
2109
2121
    }
2110
2122
    
2111
2123
    if(quit_now){
2112
 
      ret = close(sd);
 
2124
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2113
2125
      if(ret == -1){
2114
2126
        perror_plus("close");
2115
2127
      }
2165
2177
    }
2166
2178
    
2167
2179
    /* Close the socket */
2168
 
    ret = close(sd);
 
2180
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
2169
2181
    if(ret == -1){
2170
2182
      perror_plus("close");
2171
2183
    }
2199
2211
}
2200
2212
 
2201
2213
__attribute__((nonnull, warn_unused_result))
2202
 
int take_down_interface(const char *const interface){
2203
 
  int old_errno = errno;
 
2214
error_t take_down_interface(const char *const interface){
 
2215
  error_t old_errno = errno;
2204
2216
  struct ifreq network;
2205
2217
  unsigned int if_index = if_nametoindex(interface);
2206
2218
  if(if_index == 0){
2209
2221
    return ENXIO;
2210
2222
  }
2211
2223
  if(interface_is_up(interface)){
2212
 
    int ret_errno = 0;
2213
 
    int ioctl_errno = 0;
 
2224
    error_t ret_errno = 0, ioctl_errno = 0;
2214
2225
    if(not get_flags(interface, &network) and debug){
2215
2226
      ret_errno = errno;
2216
2227
      fprintf_plus(stderr, "Failed to get flags for interface "
2254
2265
    }
2255
2266
    
2256
2267
    /* Close the socket */
2257
 
    int ret = close(sd);
 
2268
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
2258
2269
    if(ret == -1){
2259
2270
      perror_plus("close");
2260
2271
    }
2276
2287
 
2277
2288
int main(int argc, char *argv[]){
2278
2289
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2279
 
                        .priority = "SECURE256:!CTYPE-X.509"
2280
 
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
2281
 
                        .current_server = NULL, .interfaces = NULL,
2282
 
                        .interfaces_size = 0 };
 
2290
                        .priority = "SECURE256:!CTYPE-X.509:"
 
2291
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
 
2292
                        .interfaces = NULL, .interfaces_size = 0 };
2283
2293
  AvahiSServiceBrowser *sb = NULL;
2284
2294
  error_t ret_errno;
2285
2295
  int ret;
2466
2476
                         .args_doc = "",
2467
2477
                         .doc = "Mandos client -- Get and decrypt"
2468
2478
                         " passwords from a Mandos server" };
2469
 
    ret_errno = argp_parse(&argp, argc, argv,
2470
 
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2471
 
    switch(ret_errno){
 
2479
    ret = argp_parse(&argp, argc, argv,
 
2480
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2481
    switch(ret){
2472
2482
    case 0:
2473
2483
      break;
2474
2484
    case ENOMEM:
2475
2485
    default:
2476
 
      errno = ret_errno;
 
2486
      errno = ret;
2477
2487
      perror_plus("argp_parse");
2478
2488
      exitcode = EX_OSERR;
2479
2489
      goto end;
2488
2498
       <http://bugs.debian.org/633582> */
2489
2499
    
2490
2500
    /* Re-raise privileges */
2491
 
    ret = raise_privileges();
2492
 
    if(ret != 0){
2493
 
      errno = ret;
 
2501
    ret_errno = raise_privileges();
 
2502
    if(ret_errno != 0){
 
2503
      errno = ret_errno;
2494
2504
      perror_plus("Failed to raise privileges");
2495
2505
    } else {
2496
2506
      struct stat st;
2512
2522
              }
2513
2523
            }
2514
2524
          }
2515
 
          close(seckey_fd);
 
2525
          TEMP_FAILURE_RETRY(close(seckey_fd));
2516
2526
        }
2517
2527
      }
2518
2528
      
2533
2543
              }
2534
2544
            }
2535
2545
          }
2536
 
          close(pubkey_fd);
 
2546
          TEMP_FAILURE_RETRY(close(pubkey_fd));
2537
2547
        }
2538
2548
      }
2539
2549
      
2540
 
      if(dh_params_file != NULL
2541
 
         and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
 
2550
      if(strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
2542
2551
        int dhparams_fd = open(dh_params_file, O_RDONLY);
2543
2552
        if(dhparams_fd == -1){
2544
2553
          perror_plus("open");
2555
2564
              }
2556
2565
            }
2557
2566
          }
2558
 
          close(dhparams_fd);
 
2567
          TEMP_FAILURE_RETRY(close(dhparams_fd));
2559
2568
        }
2560
2569
      }
2561
2570
      
2562
2571
      /* Lower privileges */
2563
 
      ret = lower_privileges();
2564
 
      if(ret != 0){
2565
 
        errno = ret;
 
2572
      ret_errno = lower_privileges();
 
2573
      if(ret_errno != 0){
 
2574
        errno = ret_errno;
2566
2575
        perror_plus("Failed to lower privileges");
2567
2576
      }
2568
2577
    }
2896
2905
    
2897
2906
    /* Allocate a new server */
2898
2907
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2899
 
                                 &config, NULL, NULL, &ret);
 
2908
                                 &config, NULL, NULL, &ret_errno);
2900
2909
    
2901
2910
    /* Free the Avahi configuration data */
2902
2911
    avahi_server_config_free(&config);
2905
2914
  /* Check if creating the Avahi server object succeeded */
2906
2915
  if(mc.server == NULL){
2907
2916
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2908
 
                 avahi_strerror(ret));
 
2917
                 avahi_strerror(ret_errno));
2909
2918
    exitcode = EX_UNAVAILABLE;
2910
2919
    goto end;
2911
2920
  }
2963
2972
  
2964
2973
  if(gnutls_initialized){
2965
2974
    gnutls_certificate_free_credentials(mc.cred);
 
2975
    gnutls_global_deinit();
2966
2976
    gnutls_dh_params_deinit(mc.dh_params);
2967
2977
  }
2968
2978
  
2991
3001
  
2992
3002
  /* Re-raise privileges */
2993
3003
  {
2994
 
    ret = raise_privileges();
2995
 
    if(ret != 0){
2996
 
      errno = ret;
 
3004
    ret_errno = raise_privileges();
 
3005
    if(ret_errno != 0){
 
3006
      errno = ret_errno;
2997
3007
      perror_plus("Failed to raise privileges");
2998
3008
    } else {
2999
3009
      
3007
3017
        while((interface=argz_next(interfaces_to_take_down,
3008
3018
                                   interfaces_to_take_down_size,
3009
3019
                                   interface))){
3010
 
          ret = take_down_interface(interface);
3011
 
          if(ret != 0){
3012
 
            errno = ret;
 
3020
          ret_errno = take_down_interface(interface);
 
3021
          if(ret_errno != 0){
 
3022
            errno = ret_errno;
3013
3023
            perror_plus("Failed to take down interface");
3014
3024
          }
3015
3025
        }
3020
3030
      }
3021
3031
    }
3022
3032
    
3023
 
    ret = lower_privileges_permanently();
3024
 
    if(ret != 0){
3025
 
      errno = ret;
 
3033
    ret_errno = lower_privileges_permanently();
 
3034
    if(ret_errno != 0){
 
3035
      errno = ret_errno;
3026
3036
      perror_plus("Failed to lower privileges permanently");
3027
3037
    }
3028
3038
  }
3030
3040
  free(interfaces_to_take_down);
3031
3041
  free(interfaces_hooks);
3032
3042
  
3033
 
  void clean_dir_at(int base, const char * const dirname,
3034
 
                    uintmax_t level){
3035
 
    struct dirent **direntries = NULL;
3036
 
    int dret;
3037
 
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
3038
 
                                                O_RDONLY
3039
 
                                                | O_NOFOLLOW
3040
 
                                                | O_DIRECTORY
3041
 
                                                | O_PATH));
3042
 
    if(dir_fd == -1){
3043
 
      perror_plus("open");
3044
 
    }
3045
 
    int numentries = scandirat(dir_fd, ".", &direntries,
3046
 
                               notdotentries, alphasort);
3047
 
    if(numentries >= 0){
3048
 
      for(int i = 0; i < numentries; i++){
3049
 
        if(debug){
3050
 
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
3051
 
                       dirname, direntries[i]->d_name);
3052
 
        }
3053
 
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
3054
 
        if(dret == -1){
3055
 
          if(errno == EISDIR){
3056
 
              dret = unlinkat(dir_fd, direntries[i]->d_name,
3057
 
                              AT_REMOVEDIR);
3058
 
          }         
3059
 
          if((dret == -1) and (errno == ENOTEMPTY)
3060
 
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
3061
 
                  == 0) and (level == 0)){
3062
 
            /* Recurse only in this special case */
3063
 
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3064
 
            dret = 0;
3065
 
          }
3066
 
          if(dret == -1){
3067
 
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3068
 
                         direntries[i]->d_name, strerror(errno));
3069
 
          }
3070
 
        }
3071
 
        free(direntries[i]);
3072
 
      }
3073
 
      
3074
 
      /* need to clean even if 0 because man page doesn't specify */
3075
 
      free(direntries);
3076
 
      if(numentries == -1){
3077
 
        perror_plus("scandirat");
3078
 
      }
3079
 
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
3080
 
      if(dret == -1 and errno != ENOENT){
3081
 
        perror_plus("rmdir");
3082
 
      }
3083
 
    } else {
3084
 
      perror_plus("scandirat");
3085
 
    }
3086
 
    close(dir_fd);
3087
 
  }
3088
 
  
3089
3043
  /* Removes the GPGME temp directory and all files inside */
3090
3044
  if(tempdir != NULL){
3091
 
    clean_dir_at(-1, tempdir, 0);
 
3045
    struct dirent **direntries = NULL;
 
3046
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
3047
                                                  | O_NOFOLLOW
 
3048
                                                  | O_DIRECTORY
 
3049
                                                  | O_PATH));
 
3050
    if(tempdir_fd == -1){
 
3051
      perror_plus("open");
 
3052
    } else {
 
3053
#ifdef __GLIBC__
 
3054
#if __GLIBC_PREREQ(2, 15)
 
3055
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
3056
                                 notdotentries, alphasort);
 
3057
#else  /* not __GLIBC_PREREQ(2, 15) */
 
3058
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
3059
                               alphasort);
 
3060
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
3061
#else   /* not __GLIBC__ */
 
3062
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
3063
                               alphasort);
 
3064
#endif  /* not __GLIBC__ */
 
3065
      if(numentries >= 0){
 
3066
        for(int i = 0; i < numentries; i++){
 
3067
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
3068
          if(ret == -1){
 
3069
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
3070
                         " \"%s\", 0): %s\n", tempdir,
 
3071
                         direntries[i]->d_name, strerror(errno));
 
3072
          }
 
3073
          free(direntries[i]);
 
3074
        }
 
3075
        
 
3076
        /* need to clean even if 0 because man page doesn't specify */
 
3077
        free(direntries);
 
3078
        if(numentries == -1){
 
3079
          perror_plus("scandir");
 
3080
        }
 
3081
        ret = rmdir(tempdir);
 
3082
        if(ret == -1 and errno != ENOENT){
 
3083
          perror_plus("rmdir");
 
3084
        }
 
3085
      }
 
3086
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
3087
    }
3092
3088
  }
3093
3089
  
3094
3090
  if(quit_now){