/mandos/release

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2017-01-25 19:38:31 UTC
  • Revision ID: teddy@recompile.se-20170125193831-rz39tr8c5yg8yy7f
Tags: version-1.7.14-1
* Makefile (version): Change to 1.7.14.
* NEWS (Version 1.7.14): Add new entry.
* debian/changelog (1.7.14-1): - '' -

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-2015 Teddy Hogeborn
13
 
 * Copyright © 2008-2015 Björn Påhlsson
 
12
 * Copyright © 2008-2016 Teddy Hogeborn
 
13
 * Copyright © 2008-2016 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
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
 
#include <string.h>             /* memset(), strcmp(), strlen(),
50
 
                                   strerror(), asprintf(), strcpy() */
 
49
#include <string.h>             /* strcmp(), strlen(), strerror(),
 
50
                                   asprintf(), strncpy(), strsignal()
 
51
                                */
51
52
#include <sys/ioctl.h>          /* ioctl */
52
53
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
54
                                   sockaddr_in6, PF_INET6,
57
58
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
59
                                   inet_pton(), connect(),
59
60
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
61
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
61
62
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
63
                                 */
63
64
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
65
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno,
 
66
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
 
67
                                   EAI_SYSTEM, ENETUNREACH,
 
68
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
 
69
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
 
70
                                   ENOTEMPTY,
66
71
                                   program_invocation_short_name */
67
72
#include <time.h>               /* nanosleep(), time(), sleep() */
68
73
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
305
310
      return false;
306
311
    }
307
312
    
308
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
313
    ret = close(fd);
309
314
    if(ret == -1){
310
315
      perror_plus("close");
311
316
    }
513
518
  fprintf_plus(stderr, "GnuTLS: %s", string);
514
519
}
515
520
 
516
 
__attribute__((nonnull, warn_unused_result))
 
521
__attribute__((nonnull(1, 2, 4), warn_unused_result))
517
522
static int init_gnutls_global(const char *pubkeyfilename,
518
523
                              const char *seckeyfilename,
519
524
                              const char *dhparamsfilename,
525
530
    fprintf_plus(stderr, "Initializing GnuTLS\n");
526
531
  }
527
532
  
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
 
  
535
533
  if(debug){
536
534
    /* "Use a log level over 10 to enable all debugging options."
537
535
     * - GnuTLS manual
545
543
  if(ret != GNUTLS_E_SUCCESS){
546
544
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
547
545
                 safer_gnutls_strerror(ret));
548
 
    gnutls_global_deinit();
549
546
    return -1;
550
547
  }
551
548
  
629
626
                     safer_gnutls_strerror(ret));
630
627
        dhparamsfilename = NULL;
631
628
      }
 
629
      free(params.data);
632
630
    } while(false);
633
631
  }
634
632
  if(dhparamsfilename == NULL){
755
753
 globalfail:
756
754
  
757
755
  gnutls_certificate_free_credentials(mc->cred);
758
 
  gnutls_global_deinit();
759
756
  gnutls_dh_params_deinit(mc->dh_params);
760
757
  return -1;
761
758
}
822
819
 
823
820
/* Set effective uid to 0, return errno */
824
821
__attribute__((warn_unused_result))
825
 
error_t raise_privileges(void){
826
 
  error_t old_errno = errno;
827
 
  error_t ret_errno = 0;
 
822
int raise_privileges(void){
 
823
  int old_errno = errno;
 
824
  int ret = 0;
828
825
  if(seteuid(0) == -1){
829
 
    ret_errno = errno;
 
826
    ret = errno;
830
827
  }
831
828
  errno = old_errno;
832
 
  return ret_errno;
 
829
  return ret;
833
830
}
834
831
 
835
832
/* Set effective and real user ID to 0.  Return errno. */
836
833
__attribute__((warn_unused_result))
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){
 
834
int raise_privileges_permanently(void){
 
835
  int old_errno = errno;
 
836
  int ret = raise_privileges();
 
837
  if(ret != 0){
841
838
    errno = old_errno;
842
 
    return ret_errno;
 
839
    return ret;
843
840
  }
844
841
  if(setuid(0) == -1){
845
 
    ret_errno = errno;
 
842
    ret = errno;
846
843
  }
847
844
  errno = old_errno;
848
 
  return ret_errno;
 
845
  return ret;
849
846
}
850
847
 
851
848
/* Set effective user ID to unprivileged saved user ID */
852
849
__attribute__((warn_unused_result))
853
 
error_t lower_privileges(void){
854
 
  error_t old_errno = errno;
855
 
  error_t ret_errno = 0;
 
850
int lower_privileges(void){
 
851
  int old_errno = errno;
 
852
  int ret = 0;
856
853
  if(seteuid(uid) == -1){
857
 
    ret_errno = errno;
 
854
    ret = errno;
858
855
  }
859
856
  errno = old_errno;
860
 
  return ret_errno;
 
857
  return ret;
861
858
}
862
859
 
863
860
/* Lower privileges permanently */
864
861
__attribute__((warn_unused_result))
865
 
error_t lower_privileges_permanently(void){
866
 
  error_t old_errno = errno;
867
 
  error_t ret_errno = 0;
 
862
int lower_privileges_permanently(void){
 
863
  int old_errno = errno;
 
864
  int ret = 0;
868
865
  if(setuid(uid) == -1){
869
 
    ret_errno = errno;
 
866
    ret = errno;
870
867
  }
871
868
  errno = old_errno;
872
 
  return ret_errno;
 
869
  return ret;
873
870
}
874
871
 
875
872
/* Helper function to add_local_route() and delete_local_route() */
931
928
      perror_plus("dup2(devnull, STDIN_FILENO)");
932
929
      _exit(EX_OSERR);
933
930
    }
934
 
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
931
    ret = close(devnull);
935
932
    if(ret == -1){
936
933
      perror_plus("close");
937
934
      _exit(EX_OSERR);
954
951
                                                   helper, O_RDONLY));
955
952
    if(helper_fd == -1){
956
953
      perror_plus("openat");
 
954
      close(helperdir_fd);
957
955
      _exit(EX_UNAVAILABLE);
958
956
    }
959
 
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
957
    close(helperdir_fd);
960
958
#ifdef __GNUC__
961
959
#pragma GCC diagnostic push
962
960
#pragma GCC diagnostic ignored "-Wcast-qual"
1081
1079
    bool match = false;
1082
1080
    {
1083
1081
      char *interface = NULL;
1084
 
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
1085
 
                                 interface))){
 
1082
      while((interface = argz_next(mc->interfaces,
 
1083
                                   mc->interfaces_size,
 
1084
                                   interface))){
1086
1085
        if(if_nametoindex(interface) == (unsigned int)if_index){
1087
1086
          match = true;
1088
1087
          break;
1130
1129
    goto mandos_end;
1131
1130
  }
1132
1131
  
1133
 
  memset(&to, 0, sizeof(to));
1134
1132
  if(af == AF_INET6){
1135
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
1136
 
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
 
1133
    struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to;
 
1134
    *to6 = (struct sockaddr_in6){ .sin6_family = (sa_family_t)af };
 
1135
    ret = inet_pton(af, ip, &to6->sin6_addr);
1137
1136
  } else {                      /* IPv4 */
1138
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
1139
 
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
 
1137
    struct sockaddr_in *to4 = (struct sockaddr_in *)&to;
 
1138
    *to4 = (struct sockaddr_in){ .sin_family = (sa_family_t)af };
 
1139
    ret = inet_pton(af, ip, &to4->sin_addr);
1140
1140
  }
1141
1141
  if(ret < 0 ){
1142
1142
    int e = errno;
1221
1221
                    sizeof(struct sockaddr_in));
1222
1222
    }
1223
1223
    if(ret < 0){
1224
 
      if(errno == ENETUNREACH
 
1224
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1225
1225
         and if_index != AVAHI_IF_UNSPEC
1226
1226
         and connect_to == NULL
1227
1227
         and not route_added and
1240
1240
           with an explicit route added with the server's address.
1241
1241
           
1242
1242
           Avahi bug reference:
1243
 
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1243
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1244
1244
           https://bugs.debian.org/587961
1245
1245
        */
1246
1246
        if(debug){
1426
1426
                                               &decrypted_buffer, mc);
1427
1427
    if(decrypted_buffer_size >= 0){
1428
1428
      
 
1429
      clearerr(stdout);
1429
1430
      written = 0;
1430
1431
      while(written < (size_t) decrypted_buffer_size){
1431
1432
        if(quit_now){
1447
1448
        }
1448
1449
        written += (size_t)ret;
1449
1450
      }
 
1451
      ret = fflush(stdout);
 
1452
      if(ret != 0){
 
1453
        int e = errno;
 
1454
        if(debug){
 
1455
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
1456
                       strerror(errno));
 
1457
        }
 
1458
        errno = e;
 
1459
        goto mandos_end;
 
1460
      }
1450
1461
      retval = 0;
1451
1462
    }
1452
1463
  }
1465
1476
    free(decrypted_buffer);
1466
1477
    free(buffer);
1467
1478
    if(tcp_sd >= 0){
1468
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
1479
      ret = close(tcp_sd);
1469
1480
    }
1470
1481
    if(ret == -1){
1471
1482
      if(e == 0){
1626
1637
__attribute__((nonnull, warn_unused_result))
1627
1638
bool get_flags(const char *ifname, struct ifreq *ifr){
1628
1639
  int ret;
1629
 
  error_t ret_errno;
 
1640
  int old_errno;
1630
1641
  
1631
1642
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1632
1643
  if(s < 0){
1633
 
    ret_errno = errno;
 
1644
    old_errno = errno;
1634
1645
    perror_plus("socket");
1635
 
    errno = ret_errno;
 
1646
    errno = old_errno;
1636
1647
    return false;
1637
1648
  }
1638
 
  strcpy(ifr->ifr_name, ifname);
 
1649
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1650
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1639
1651
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1640
1652
  if(ret == -1){
1641
1653
    if(debug){
1642
 
      ret_errno = errno;
 
1654
      old_errno = errno;
1643
1655
      perror_plus("ioctl SIOCGIFFLAGS");
1644
 
      errno = ret_errno;
 
1656
      errno = old_errno;
1645
1657
    }
1646
1658
    return false;
1647
1659
  }
1911
1923
      return;
1912
1924
    }
1913
1925
  }
1914
 
#ifdef __GLIBC__
1915
 
#if __GLIBC_PREREQ(2, 15)
1916
1926
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1917
1927
                           runnable_hook, alphasort);
1918
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1919
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1920
 
                         alphasort);
1921
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1922
 
#else   /* not __GLIBC__ */
1923
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1924
 
                         alphasort);
1925
 
#endif  /* not __GLIBC__ */
1926
1928
  if(numhooks == -1){
1927
1929
    perror_plus("scandir");
1928
1930
    return;
2010
2012
        perror_plus("openat");
2011
2013
        _exit(EXIT_FAILURE);
2012
2014
      }
2013
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2015
      if(close(hookdir_fd) == -1){
2014
2016
        perror_plus("close");
2015
2017
        _exit(EXIT_FAILURE);
2016
2018
      }
2019
2021
        perror_plus("dup2(devnull, STDIN_FILENO)");
2020
2022
        _exit(EX_OSERR);
2021
2023
      }
2022
 
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
2024
      ret = close(devnull);
2023
2025
      if(ret == -1){
2024
2026
        perror_plus("close");
2025
2027
        _exit(EX_OSERR);
2074
2076
    free(direntry);
2075
2077
  }
2076
2078
  free(direntries);
2077
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2079
  if(close(hookdir_fd) == -1){
2078
2080
    perror_plus("close");
2079
2081
  } else {
2080
2082
    hookdir_fd = -1;
2083
2085
}
2084
2086
 
2085
2087
__attribute__((nonnull, warn_unused_result))
2086
 
error_t bring_up_interface(const char *const interface,
2087
 
                           const float delay){
2088
 
  error_t old_errno = errno;
 
2088
int bring_up_interface(const char *const interface,
 
2089
                       const float delay){
 
2090
  int old_errno = errno;
2089
2091
  int ret;
2090
2092
  struct ifreq network;
2091
2093
  unsigned int if_index = if_nametoindex(interface);
2101
2103
  }
2102
2104
  
2103
2105
  if(not interface_is_up(interface)){
2104
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2106
    int ret_errno = 0;
 
2107
    int ioctl_errno = 0;
2105
2108
    if(not get_flags(interface, &network)){
2106
2109
      ret_errno = errno;
2107
2110
      fprintf_plus(stderr, "Failed to get flags for interface "
2120
2123
    }
2121
2124
    
2122
2125
    if(quit_now){
2123
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2126
      ret = close(sd);
2124
2127
      if(ret == -1){
2125
2128
        perror_plus("close");
2126
2129
      }
2176
2179
    }
2177
2180
    
2178
2181
    /* Close the socket */
2179
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2182
    ret = close(sd);
2180
2183
    if(ret == -1){
2181
2184
      perror_plus("close");
2182
2185
    }
2194
2197
  
2195
2198
  /* Sleep checking until interface is running.
2196
2199
     Check every 0.25s, up to total time of delay */
2197
 
  for(int i=0; i < delay * 4; i++){
 
2200
  for(int i = 0; i < delay * 4; i++){
2198
2201
    if(interface_is_running(interface)){
2199
2202
      break;
2200
2203
    }
2210
2213
}
2211
2214
 
2212
2215
__attribute__((nonnull, warn_unused_result))
2213
 
error_t take_down_interface(const char *const interface){
2214
 
  error_t old_errno = errno;
 
2216
int take_down_interface(const char *const interface){
 
2217
  int old_errno = errno;
2215
2218
  struct ifreq network;
2216
2219
  unsigned int if_index = if_nametoindex(interface);
2217
2220
  if(if_index == 0){
2220
2223
    return ENXIO;
2221
2224
  }
2222
2225
  if(interface_is_up(interface)){
2223
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2226
    int ret_errno = 0;
 
2227
    int ioctl_errno = 0;
2224
2228
    if(not get_flags(interface, &network) and debug){
2225
2229
      ret_errno = errno;
2226
2230
      fprintf_plus(stderr, "Failed to get flags for interface "
2264
2268
    }
2265
2269
    
2266
2270
    /* Close the socket */
2267
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2271
    int ret = close(sd);
2268
2272
    if(ret == -1){
2269
2273
      perror_plus("close");
2270
2274
    }
2286
2290
 
2287
2291
int main(int argc, char *argv[]){
2288
2292
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2289
 
                        .priority = "SECURE256:!CTYPE-X.509:"
2290
 
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2291
 
                        .interfaces = NULL, .interfaces_size = 0 };
 
2293
                        .priority = "SECURE256:!CTYPE-X.509"
 
2294
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
 
2295
                        .current_server = NULL, .interfaces = NULL,
 
2296
                        .interfaces_size = 0 };
2292
2297
  AvahiSServiceBrowser *sb = NULL;
2293
2298
  error_t ret_errno;
2294
2299
  int ret;
2475
2480
                         .args_doc = "",
2476
2481
                         .doc = "Mandos client -- Get and decrypt"
2477
2482
                         " passwords from a Mandos server" };
2478
 
    ret = argp_parse(&argp, argc, argv,
2479
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2480
 
    switch(ret){
 
2483
    ret_errno = argp_parse(&argp, argc, argv,
 
2484
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2485
    switch(ret_errno){
2481
2486
    case 0:
2482
2487
      break;
2483
2488
    case ENOMEM:
2484
2489
    default:
2485
 
      errno = ret;
 
2490
      errno = ret_errno;
2486
2491
      perror_plus("argp_parse");
2487
2492
      exitcode = EX_OSERR;
2488
2493
      goto end;
2494
2499
  
2495
2500
  {
2496
2501
    /* Work around Debian bug #633582:
2497
 
       <http://bugs.debian.org/633582> */
 
2502
       <https://bugs.debian.org/633582> */
2498
2503
    
2499
2504
    /* Re-raise privileges */
2500
 
    ret_errno = raise_privileges();
2501
 
    if(ret_errno != 0){
2502
 
      errno = ret_errno;
 
2505
    ret = raise_privileges();
 
2506
    if(ret != 0){
 
2507
      errno = ret;
2503
2508
      perror_plus("Failed to raise privileges");
2504
2509
    } else {
2505
2510
      struct stat st;
2521
2526
              }
2522
2527
            }
2523
2528
          }
2524
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
2529
          close(seckey_fd);
2525
2530
        }
2526
2531
      }
2527
2532
      
2542
2547
              }
2543
2548
            }
2544
2549
          }
2545
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
2550
          close(pubkey_fd);
2546
2551
        }
2547
2552
      }
2548
2553
      
2549
 
      if(strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
 
2554
      if(dh_params_file != NULL
 
2555
         and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
2550
2556
        int dhparams_fd = open(dh_params_file, O_RDONLY);
2551
2557
        if(dhparams_fd == -1){
2552
2558
          perror_plus("open");
2563
2569
              }
2564
2570
            }
2565
2571
          }
2566
 
          TEMP_FAILURE_RETRY(close(dhparams_fd));
 
2572
          close(dhparams_fd);
2567
2573
        }
2568
2574
      }
2569
2575
      
2570
2576
      /* Lower privileges */
2571
 
      ret_errno = lower_privileges();
2572
 
      if(ret_errno != 0){
2573
 
        errno = ret_errno;
 
2577
      ret = lower_privileges();
 
2578
      if(ret != 0){
 
2579
        errno = ret;
2574
2580
        perror_plus("Failed to lower privileges");
2575
2581
      }
2576
2582
    }
2904
2910
    
2905
2911
    /* Allocate a new server */
2906
2912
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2907
 
                                 &config, NULL, NULL, &ret_errno);
 
2913
                                 &config, NULL, NULL, &ret);
2908
2914
    
2909
2915
    /* Free the Avahi configuration data */
2910
2916
    avahi_server_config_free(&config);
2913
2919
  /* Check if creating the Avahi server object succeeded */
2914
2920
  if(mc.server == NULL){
2915
2921
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2916
 
                 avahi_strerror(ret_errno));
 
2922
                 avahi_strerror(ret));
2917
2923
    exitcode = EX_UNAVAILABLE;
2918
2924
    goto end;
2919
2925
  }
2954
2960
 end:
2955
2961
  
2956
2962
  if(debug){
2957
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2963
    if(signal_received){
 
2964
      fprintf_plus(stderr, "%s exiting due to signal %d: %s\n",
 
2965
                   argv[0], signal_received,
 
2966
                   strsignal(signal_received));
 
2967
    } else {
 
2968
      fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2969
    }
2958
2970
  }
2959
2971
  
2960
2972
  /* Cleanup things */
2971
2983
  
2972
2984
  if(gnutls_initialized){
2973
2985
    gnutls_certificate_free_credentials(mc.cred);
2974
 
    gnutls_global_deinit();
2975
2986
    gnutls_dh_params_deinit(mc.dh_params);
2976
2987
  }
2977
2988
  
3000
3011
  
3001
3012
  /* Re-raise privileges */
3002
3013
  {
3003
 
    ret_errno = raise_privileges();
3004
 
    if(ret_errno != 0){
3005
 
      errno = ret_errno;
 
3014
    ret = raise_privileges();
 
3015
    if(ret != 0){
 
3016
      errno = ret;
3006
3017
      perror_plus("Failed to raise privileges");
3007
3018
    } else {
3008
3019
      
3013
3024
      /* Take down the network interfaces which were brought up */
3014
3025
      {
3015
3026
        char *interface = NULL;
3016
 
        while((interface=argz_next(interfaces_to_take_down,
3017
 
                                   interfaces_to_take_down_size,
3018
 
                                   interface))){
3019
 
          ret_errno = take_down_interface(interface);
3020
 
          if(ret_errno != 0){
3021
 
            errno = ret_errno;
 
3027
        while((interface = argz_next(interfaces_to_take_down,
 
3028
                                     interfaces_to_take_down_size,
 
3029
                                     interface))){
 
3030
          ret = take_down_interface(interface);
 
3031
          if(ret != 0){
 
3032
            errno = ret;
3022
3033
            perror_plus("Failed to take down interface");
3023
3034
          }
3024
3035
        }
3029
3040
      }
3030
3041
    }
3031
3042
    
3032
 
    ret_errno = lower_privileges_permanently();
3033
 
    if(ret_errno != 0){
3034
 
      errno = ret_errno;
 
3043
    ret = lower_privileges_permanently();
 
3044
    if(ret != 0){
 
3045
      errno = ret;
3035
3046
      perror_plus("Failed to lower privileges permanently");
3036
3047
    }
3037
3048
  }
3039
3050
  free(interfaces_to_take_down);
3040
3051
  free(interfaces_hooks);
3041
3052
  
 
3053
  void clean_dir_at(int base, const char * const dirname,
 
3054
                    uintmax_t level){
 
3055
    struct dirent **direntries = NULL;
 
3056
    int dret;
 
3057
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
 
3058
                                                O_RDONLY
 
3059
                                                | O_NOFOLLOW
 
3060
                                                | O_DIRECTORY
 
3061
                                                | O_PATH));
 
3062
    if(dir_fd == -1){
 
3063
      perror_plus("open");
 
3064
    }
 
3065
    int numentries = scandirat(dir_fd, ".", &direntries,
 
3066
                               notdotentries, alphasort);
 
3067
    if(numentries >= 0){
 
3068
      for(int i = 0; i < numentries; i++){
 
3069
        if(debug){
 
3070
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
 
3071
                       dirname, direntries[i]->d_name);
 
3072
        }
 
3073
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
 
3074
        if(dret == -1){
 
3075
          if(errno == EISDIR){
 
3076
              dret = unlinkat(dir_fd, direntries[i]->d_name,
 
3077
                              AT_REMOVEDIR);
 
3078
          }         
 
3079
          if((dret == -1) and (errno == ENOTEMPTY)
 
3080
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
 
3081
                  == 0) and (level == 0)){
 
3082
            /* Recurse only in this special case */
 
3083
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
 
3084
            dret = 0;
 
3085
          }
 
3086
          if(dret == -1){
 
3087
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
 
3088
                         direntries[i]->d_name, strerror(errno));
 
3089
          }
 
3090
        }
 
3091
        free(direntries[i]);
 
3092
      }
 
3093
      
 
3094
      /* need to clean even if 0 because man page doesn't specify */
 
3095
      free(direntries);
 
3096
      if(numentries == -1){
 
3097
        perror_plus("scandirat");
 
3098
      }
 
3099
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
 
3100
      if(dret == -1 and errno != ENOENT){
 
3101
        perror_plus("rmdir");
 
3102
      }
 
3103
    } else {
 
3104
      perror_plus("scandirat");
 
3105
    }
 
3106
    close(dir_fd);
 
3107
  }
 
3108
  
3042
3109
  /* Removes the GPGME temp directory and all files inside */
3043
3110
  if(tempdir != NULL){
3044
 
    struct dirent **direntries = NULL;
3045
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3046
 
                                                  | O_NOFOLLOW
3047
 
                                                  | O_DIRECTORY
3048
 
                                                  | O_PATH));
3049
 
    if(tempdir_fd == -1){
3050
 
      perror_plus("open");
3051
 
    } else {
3052
 
#ifdef __GLIBC__
3053
 
#if __GLIBC_PREREQ(2, 15)
3054
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
3055
 
                                 notdotentries, alphasort);
3056
 
#else  /* not __GLIBC_PREREQ(2, 15) */
3057
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3058
 
                               alphasort);
3059
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
3060
 
#else   /* not __GLIBC__ */
3061
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3062
 
                               alphasort);
3063
 
#endif  /* not __GLIBC__ */
3064
 
      if(numentries >= 0){
3065
 
        for(int i = 0; i < numentries; i++){
3066
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3067
 
          if(ret == -1){
3068
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3069
 
                         " \"%s\", 0): %s\n", tempdir,
3070
 
                         direntries[i]->d_name, strerror(errno));
3071
 
          }
3072
 
          free(direntries[i]);
3073
 
        }
3074
 
        
3075
 
        /* need to clean even if 0 because man page doesn't specify */
3076
 
        free(direntries);
3077
 
        if(numentries == -1){
3078
 
          perror_plus("scandir");
3079
 
        }
3080
 
        ret = rmdir(tempdir);
3081
 
        if(ret == -1 and errno != ENOENT){
3082
 
          perror_plus("rmdir");
3083
 
        }
3084
 
      }
3085
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
3086
 
    }
 
3111
    clean_dir_at(-1, tempdir, 0);
3087
3112
  }
3088
3113
  
3089
3114
  if(quit_now){