/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: 2018-02-12 14:54:58 UTC
  • Revision ID: teddy@recompile.se-20180212145458-mkcjkw4cd9s18izm
Client bug fix: Revert change to plugin-runner

* plugin-runner.c (free_plugin): Bug fix: Don't free() plugin name.

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
14
 
 * 
15
 
 * This program is free software: you can redistribute it and/or
16
 
 * modify it under the terms of the GNU General Public License as
17
 
 * published by the Free Software Foundation, either version 3 of the
18
 
 * License, or (at your option) any later version.
19
 
 * 
20
 
 * This program is distributed in the hope that it will be useful, but
 
12
 * Copyright © 2008-2018 Teddy Hogeborn
 
13
 * Copyright © 2008-2018 Björn Påhlsson
 
14
 * 
 
15
 * This file is part of Mandos.
 
16
 * 
 
17
 * Mandos is free software: you can redistribute it and/or modify it
 
18
 * under the terms of the GNU General Public License as published by
 
19
 * the Free Software Foundation, either version 3 of the License, or
 
20
 * (at your option) any later version.
 
21
 * 
 
22
 * Mandos is distributed in the hope that it will be useful, but
21
23
 * WITHOUT ANY WARRANTY; without even the implied warranty of
22
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
25
 * General Public License for more details.
24
26
 * 
25
27
 * You should have received a copy of the GNU General Public License
26
 
 * along with this program.  If not, see
27
 
 * <http://www.gnu.org/licenses/>.
 
28
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
28
29
 * 
29
30
 * Contact the authors at <mandos@recompile.se>.
30
31
 */
47
48
                                   strtof(), abort() */
48
49
#include <stdbool.h>            /* bool, false, true */
49
50
#include <string.h>             /* strcmp(), strlen(), strerror(),
50
 
                                   asprintf(), strcpy() */
 
51
                                   asprintf(), strncpy(), strsignal()
 
52
                                */
51
53
#include <sys/ioctl.h>          /* ioctl */
52
54
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
55
                                   sockaddr_in6, PF_INET6,
57
59
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
60
                                   inet_pton(), connect(),
59
61
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
62
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
61
63
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
64
                                 */
63
65
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
66
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno,
 
67
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
 
68
                                   EAI_SYSTEM, ENETUNREACH,
 
69
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
 
70
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
 
71
                                   ENOTEMPTY,
66
72
                                   program_invocation_short_name */
67
73
#include <time.h>               /* nanosleep(), time(), sleep() */
68
74
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
305
311
      return false;
306
312
    }
307
313
    
308
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
314
    ret = close(fd);
309
315
    if(ret == -1){
310
316
      perror_plus("close");
311
317
    }
513
519
  fprintf_plus(stderr, "GnuTLS: %s", string);
514
520
}
515
521
 
516
 
__attribute__((nonnull, warn_unused_result))
 
522
__attribute__((nonnull(1, 2, 4), warn_unused_result))
517
523
static int init_gnutls_global(const char *pubkeyfilename,
518
524
                              const char *seckeyfilename,
519
525
                              const char *dhparamsfilename,
525
531
    fprintf_plus(stderr, "Initializing GnuTLS\n");
526
532
  }
527
533
  
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
534
  if(debug){
536
535
    /* "Use a log level over 10 to enable all debugging options."
537
536
     * - GnuTLS manual
545
544
  if(ret != GNUTLS_E_SUCCESS){
546
545
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
547
546
                 safer_gnutls_strerror(ret));
548
 
    gnutls_global_deinit();
549
547
    return -1;
550
548
  }
551
549
  
629
627
                     safer_gnutls_strerror(ret));
630
628
        dhparamsfilename = NULL;
631
629
      }
 
630
      free(params.data);
632
631
    } while(false);
633
632
  }
634
633
  if(dhparamsfilename == NULL){
755
754
 globalfail:
756
755
  
757
756
  gnutls_certificate_free_credentials(mc->cred);
758
 
  gnutls_global_deinit();
759
757
  gnutls_dh_params_deinit(mc->dh_params);
760
758
  return -1;
761
759
}
822
820
 
823
821
/* Set effective uid to 0, return errno */
824
822
__attribute__((warn_unused_result))
825
 
error_t raise_privileges(void){
826
 
  error_t old_errno = errno;
827
 
  error_t ret_errno = 0;
 
823
int raise_privileges(void){
 
824
  int old_errno = errno;
 
825
  int ret = 0;
828
826
  if(seteuid(0) == -1){
829
 
    ret_errno = errno;
 
827
    ret = errno;
830
828
  }
831
829
  errno = old_errno;
832
 
  return ret_errno;
 
830
  return ret;
833
831
}
834
832
 
835
833
/* Set effective and real user ID to 0.  Return errno. */
836
834
__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){
 
835
int raise_privileges_permanently(void){
 
836
  int old_errno = errno;
 
837
  int ret = raise_privileges();
 
838
  if(ret != 0){
841
839
    errno = old_errno;
842
 
    return ret_errno;
 
840
    return ret;
843
841
  }
844
842
  if(setuid(0) == -1){
845
 
    ret_errno = errno;
 
843
    ret = errno;
846
844
  }
847
845
  errno = old_errno;
848
 
  return ret_errno;
 
846
  return ret;
849
847
}
850
848
 
851
849
/* Set effective user ID to unprivileged saved user ID */
852
850
__attribute__((warn_unused_result))
853
 
error_t lower_privileges(void){
854
 
  error_t old_errno = errno;
855
 
  error_t ret_errno = 0;
 
851
int lower_privileges(void){
 
852
  int old_errno = errno;
 
853
  int ret = 0;
856
854
  if(seteuid(uid) == -1){
857
 
    ret_errno = errno;
 
855
    ret = errno;
858
856
  }
859
857
  errno = old_errno;
860
 
  return ret_errno;
 
858
  return ret;
861
859
}
862
860
 
863
861
/* Lower privileges permanently */
864
862
__attribute__((warn_unused_result))
865
 
error_t lower_privileges_permanently(void){
866
 
  error_t old_errno = errno;
867
 
  error_t ret_errno = 0;
 
863
int lower_privileges_permanently(void){
 
864
  int old_errno = errno;
 
865
  int ret = 0;
868
866
  if(setuid(uid) == -1){
869
 
    ret_errno = errno;
 
867
    ret = errno;
870
868
  }
871
869
  errno = old_errno;
872
 
  return ret_errno;
 
870
  return ret;
873
871
}
874
872
 
875
873
/* Helper function to add_local_route() and delete_local_route() */
931
929
      perror_plus("dup2(devnull, STDIN_FILENO)");
932
930
      _exit(EX_OSERR);
933
931
    }
934
 
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
932
    ret = close(devnull);
935
933
    if(ret == -1){
936
934
      perror_plus("close");
937
935
      _exit(EX_OSERR);
954
952
                                                   helper, O_RDONLY));
955
953
    if(helper_fd == -1){
956
954
      perror_plus("openat");
 
955
      close(helperdir_fd);
957
956
      _exit(EX_UNAVAILABLE);
958
957
    }
959
 
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
958
    close(helperdir_fd);
960
959
#ifdef __GNUC__
961
960
#pragma GCC diagnostic push
962
961
#pragma GCC diagnostic ignored "-Wcast-qual"
1081
1080
    bool match = false;
1082
1081
    {
1083
1082
      char *interface = NULL;
1084
 
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
1085
 
                                 interface))){
 
1083
      while((interface = argz_next(mc->interfaces,
 
1084
                                   mc->interfaces_size,
 
1085
                                   interface))){
1086
1086
        if(if_nametoindex(interface) == (unsigned int)if_index){
1087
1087
          match = true;
1088
1088
          break;
1222
1222
                    sizeof(struct sockaddr_in));
1223
1223
    }
1224
1224
    if(ret < 0){
1225
 
      if(errno == ENETUNREACH
 
1225
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1226
1226
         and if_index != AVAHI_IF_UNSPEC
1227
1227
         and connect_to == NULL
1228
1228
         and not route_added and
1241
1241
           with an explicit route added with the server's address.
1242
1242
           
1243
1243
           Avahi bug reference:
1244
 
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1244
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1245
1245
           https://bugs.debian.org/587961
1246
1246
        */
1247
1247
        if(debug){
1427
1427
                                               &decrypted_buffer, mc);
1428
1428
    if(decrypted_buffer_size >= 0){
1429
1429
      
 
1430
      clearerr(stdout);
1430
1431
      written = 0;
1431
1432
      while(written < (size_t) decrypted_buffer_size){
1432
1433
        if(quit_now){
1448
1449
        }
1449
1450
        written += (size_t)ret;
1450
1451
      }
 
1452
      ret = fflush(stdout);
 
1453
      if(ret != 0){
 
1454
        int e = errno;
 
1455
        if(debug){
 
1456
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
1457
                       strerror(errno));
 
1458
        }
 
1459
        errno = e;
 
1460
        goto mandos_end;
 
1461
      }
1451
1462
      retval = 0;
1452
1463
    }
1453
1464
  }
1466
1477
    free(decrypted_buffer);
1467
1478
    free(buffer);
1468
1479
    if(tcp_sd >= 0){
1469
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
1480
      ret = close(tcp_sd);
1470
1481
    }
1471
1482
    if(ret == -1){
1472
1483
      if(e == 0){
1484
1495
  return retval;
1485
1496
}
1486
1497
 
1487
 
__attribute__((nonnull))
1488
1498
static void resolve_callback(AvahiSServiceResolver *r,
1489
1499
                             AvahiIfIndex interface,
1490
1500
                             AvahiProtocol proto,
1627
1637
__attribute__((nonnull, warn_unused_result))
1628
1638
bool get_flags(const char *ifname, struct ifreq *ifr){
1629
1639
  int ret;
1630
 
  error_t ret_errno;
 
1640
  int old_errno;
1631
1641
  
1632
1642
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1633
1643
  if(s < 0){
1634
 
    ret_errno = errno;
 
1644
    old_errno = errno;
1635
1645
    perror_plus("socket");
1636
 
    errno = ret_errno;
 
1646
    errno = old_errno;
1637
1647
    return false;
1638
1648
  }
1639
 
  strcpy(ifr->ifr_name, ifname);
 
1649
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1650
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1640
1651
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1641
1652
  if(ret == -1){
1642
1653
    if(debug){
1643
 
      ret_errno = errno;
 
1654
      old_errno = errno;
1644
1655
      perror_plus("ioctl SIOCGIFFLAGS");
1645
 
      errno = ret_errno;
 
1656
      errno = old_errno;
1646
1657
    }
1647
1658
    return false;
1648
1659
  }
1912
1923
      return;
1913
1924
    }
1914
1925
  }
1915
 
#ifdef __GLIBC__
1916
 
#if __GLIBC_PREREQ(2, 15)
1917
1926
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1918
1927
                           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__ */
1927
1928
  if(numhooks == -1){
1928
1929
    perror_plus("scandir");
1929
1930
    return;
2011
2012
        perror_plus("openat");
2012
2013
        _exit(EXIT_FAILURE);
2013
2014
      }
2014
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2015
      if(close(hookdir_fd) == -1){
2015
2016
        perror_plus("close");
2016
2017
        _exit(EXIT_FAILURE);
2017
2018
      }
2020
2021
        perror_plus("dup2(devnull, STDIN_FILENO)");
2021
2022
        _exit(EX_OSERR);
2022
2023
      }
2023
 
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
2024
      ret = close(devnull);
2024
2025
      if(ret == -1){
2025
2026
        perror_plus("close");
2026
2027
        _exit(EX_OSERR);
2075
2076
    free(direntry);
2076
2077
  }
2077
2078
  free(direntries);
2078
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2079
  if(close(hookdir_fd) == -1){
2079
2080
    perror_plus("close");
2080
2081
  } else {
2081
2082
    hookdir_fd = -1;
2084
2085
}
2085
2086
 
2086
2087
__attribute__((nonnull, warn_unused_result))
2087
 
error_t bring_up_interface(const char *const interface,
2088
 
                           const float delay){
2089
 
  error_t old_errno = errno;
 
2088
int bring_up_interface(const char *const interface,
 
2089
                       const float delay){
 
2090
  int old_errno = errno;
2090
2091
  int ret;
2091
2092
  struct ifreq network;
2092
2093
  unsigned int if_index = if_nametoindex(interface);
2102
2103
  }
2103
2104
  
2104
2105
  if(not interface_is_up(interface)){
2105
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2106
    int ret_errno = 0;
 
2107
    int ioctl_errno = 0;
2106
2108
    if(not get_flags(interface, &network)){
2107
2109
      ret_errno = errno;
2108
2110
      fprintf_plus(stderr, "Failed to get flags for interface "
2121
2123
    }
2122
2124
    
2123
2125
    if(quit_now){
2124
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2126
      ret = close(sd);
2125
2127
      if(ret == -1){
2126
2128
        perror_plus("close");
2127
2129
      }
2177
2179
    }
2178
2180
    
2179
2181
    /* Close the socket */
2180
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2182
    ret = close(sd);
2181
2183
    if(ret == -1){
2182
2184
      perror_plus("close");
2183
2185
    }
2195
2197
  
2196
2198
  /* Sleep checking until interface is running.
2197
2199
     Check every 0.25s, up to total time of delay */
2198
 
  for(int i=0; i < delay * 4; i++){
 
2200
  for(int i = 0; i < delay * 4; i++){
2199
2201
    if(interface_is_running(interface)){
2200
2202
      break;
2201
2203
    }
2211
2213
}
2212
2214
 
2213
2215
__attribute__((nonnull, warn_unused_result))
2214
 
error_t take_down_interface(const char *const interface){
2215
 
  error_t old_errno = errno;
 
2216
int take_down_interface(const char *const interface){
 
2217
  int old_errno = errno;
2216
2218
  struct ifreq network;
2217
2219
  unsigned int if_index = if_nametoindex(interface);
2218
2220
  if(if_index == 0){
2221
2223
    return ENXIO;
2222
2224
  }
2223
2225
  if(interface_is_up(interface)){
2224
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2226
    int ret_errno = 0;
 
2227
    int ioctl_errno = 0;
2225
2228
    if(not get_flags(interface, &network) and debug){
2226
2229
      ret_errno = errno;
2227
2230
      fprintf_plus(stderr, "Failed to get flags for interface "
2265
2268
    }
2266
2269
    
2267
2270
    /* Close the socket */
2268
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2271
    int ret = close(sd);
2269
2272
    if(ret == -1){
2270
2273
      perror_plus("close");
2271
2274
    }
2287
2290
 
2288
2291
int main(int argc, char *argv[]){
2289
2292
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2290
 
                        .priority = "SECURE256:!CTYPE-X.509:"
2291
 
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2292
 
                        .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 };
2293
2297
  AvahiSServiceBrowser *sb = NULL;
2294
2298
  error_t ret_errno;
2295
2299
  int ret;
2476
2480
                         .args_doc = "",
2477
2481
                         .doc = "Mandos client -- Get and decrypt"
2478
2482
                         " passwords from a Mandos server" };
2479
 
    ret = argp_parse(&argp, argc, argv,
2480
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2481
 
    switch(ret){
 
2483
    ret_errno = argp_parse(&argp, argc, argv,
 
2484
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2485
    switch(ret_errno){
2482
2486
    case 0:
2483
2487
      break;
2484
2488
    case ENOMEM:
2485
2489
    default:
2486
 
      errno = ret;
 
2490
      errno = ret_errno;
2487
2491
      perror_plus("argp_parse");
2488
2492
      exitcode = EX_OSERR;
2489
2493
      goto end;
2495
2499
  
2496
2500
  {
2497
2501
    /* Work around Debian bug #633582:
2498
 
       <http://bugs.debian.org/633582> */
 
2502
       <https://bugs.debian.org/633582> */
2499
2503
    
2500
2504
    /* Re-raise privileges */
2501
 
    ret_errno = raise_privileges();
2502
 
    if(ret_errno != 0){
2503
 
      errno = ret_errno;
 
2505
    ret = raise_privileges();
 
2506
    if(ret != 0){
 
2507
      errno = ret;
2504
2508
      perror_plus("Failed to raise privileges");
2505
2509
    } else {
2506
2510
      struct stat st;
2522
2526
              }
2523
2527
            }
2524
2528
          }
2525
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
2529
          close(seckey_fd);
2526
2530
        }
2527
2531
      }
2528
2532
      
2543
2547
              }
2544
2548
            }
2545
2549
          }
2546
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
2550
          close(pubkey_fd);
2547
2551
        }
2548
2552
      }
2549
2553
      
2550
 
      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){
2551
2556
        int dhparams_fd = open(dh_params_file, O_RDONLY);
2552
2557
        if(dhparams_fd == -1){
2553
2558
          perror_plus("open");
2564
2569
              }
2565
2570
            }
2566
2571
          }
2567
 
          TEMP_FAILURE_RETRY(close(dhparams_fd));
 
2572
          close(dhparams_fd);
2568
2573
        }
2569
2574
      }
2570
2575
      
2571
2576
      /* Lower privileges */
2572
 
      ret_errno = lower_privileges();
2573
 
      if(ret_errno != 0){
2574
 
        errno = ret_errno;
 
2577
      ret = lower_privileges();
 
2578
      if(ret != 0){
 
2579
        errno = ret;
2575
2580
        perror_plus("Failed to lower privileges");
2576
2581
      }
2577
2582
    }
2905
2910
    
2906
2911
    /* Allocate a new server */
2907
2912
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2908
 
                                 &config, NULL, NULL, &ret_errno);
 
2913
                                 &config, NULL, NULL, &ret);
2909
2914
    
2910
2915
    /* Free the Avahi configuration data */
2911
2916
    avahi_server_config_free(&config);
2914
2919
  /* Check if creating the Avahi server object succeeded */
2915
2920
  if(mc.server == NULL){
2916
2921
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2917
 
                 avahi_strerror(ret_errno));
 
2922
                 avahi_strerror(ret));
2918
2923
    exitcode = EX_UNAVAILABLE;
2919
2924
    goto end;
2920
2925
  }
2955
2960
 end:
2956
2961
  
2957
2962
  if(debug){
2958
 
    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
    }
2959
2970
  }
2960
2971
  
2961
2972
  /* Cleanup things */
2972
2983
  
2973
2984
  if(gnutls_initialized){
2974
2985
    gnutls_certificate_free_credentials(mc.cred);
2975
 
    gnutls_global_deinit();
2976
2986
    gnutls_dh_params_deinit(mc.dh_params);
2977
2987
  }
2978
2988
  
3001
3011
  
3002
3012
  /* Re-raise privileges */
3003
3013
  {
3004
 
    ret_errno = raise_privileges();
3005
 
    if(ret_errno != 0){
3006
 
      errno = ret_errno;
 
3014
    ret = raise_privileges();
 
3015
    if(ret != 0){
 
3016
      errno = ret;
3007
3017
      perror_plus("Failed to raise privileges");
3008
3018
    } else {
3009
3019
      
3014
3024
      /* Take down the network interfaces which were brought up */
3015
3025
      {
3016
3026
        char *interface = NULL;
3017
 
        while((interface=argz_next(interfaces_to_take_down,
3018
 
                                   interfaces_to_take_down_size,
3019
 
                                   interface))){
3020
 
          ret_errno = take_down_interface(interface);
3021
 
          if(ret_errno != 0){
3022
 
            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;
3023
3033
            perror_plus("Failed to take down interface");
3024
3034
          }
3025
3035
        }
3030
3040
      }
3031
3041
    }
3032
3042
    
3033
 
    ret_errno = lower_privileges_permanently();
3034
 
    if(ret_errno != 0){
3035
 
      errno = ret_errno;
 
3043
    ret = lower_privileges_permanently();
 
3044
    if(ret != 0){
 
3045
      errno = ret;
3036
3046
      perror_plus("Failed to lower privileges permanently");
3037
3047
    }
3038
3048
  }
3040
3050
  free(interfaces_to_take_down);
3041
3051
  free(interfaces_hooks);
3042
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
  
3043
3109
  /* Removes the GPGME temp directory and all files inside */
3044
3110
  if(tempdir != NULL){
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
 
    }
 
3111
    clean_dir_at(-1, tempdir, 0);
3088
3112
  }
3089
3113
  
3090
3114
  if(quit_now){