/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: 2016-10-01 13:56:09 UTC
  • mfrom: (237.7.423 trunk)
  • Revision ID: teddy@recompile.se-20161001135609-y5q39hvf2d95utwf
Merge from trunk

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
  
755
752
 globalfail:
756
753
  
757
754
  gnutls_certificate_free_credentials(mc->cred);
758
 
  gnutls_global_deinit();
759
755
  gnutls_dh_params_deinit(mc->dh_params);
760
756
  return -1;
761
757
}
822
818
 
823
819
/* Set effective uid to 0, return errno */
824
820
__attribute__((warn_unused_result))
825
 
error_t raise_privileges(void){
826
 
  error_t old_errno = errno;
827
 
  error_t ret_errno = 0;
 
821
int raise_privileges(void){
 
822
  int old_errno = errno;
 
823
  int ret = 0;
828
824
  if(seteuid(0) == -1){
829
 
    ret_errno = errno;
 
825
    ret = errno;
830
826
  }
831
827
  errno = old_errno;
832
 
  return ret_errno;
 
828
  return ret;
833
829
}
834
830
 
835
831
/* Set effective and real user ID to 0.  Return errno. */
836
832
__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){
 
833
int raise_privileges_permanently(void){
 
834
  int old_errno = errno;
 
835
  int ret = raise_privileges();
 
836
  if(ret != 0){
841
837
    errno = old_errno;
842
 
    return ret_errno;
 
838
    return ret;
843
839
  }
844
840
  if(setuid(0) == -1){
845
 
    ret_errno = errno;
 
841
    ret = errno;
846
842
  }
847
843
  errno = old_errno;
848
 
  return ret_errno;
 
844
  return ret;
849
845
}
850
846
 
851
847
/* Set effective user ID to unprivileged saved user ID */
852
848
__attribute__((warn_unused_result))
853
 
error_t lower_privileges(void){
854
 
  error_t old_errno = errno;
855
 
  error_t ret_errno = 0;
 
849
int lower_privileges(void){
 
850
  int old_errno = errno;
 
851
  int ret = 0;
856
852
  if(seteuid(uid) == -1){
857
 
    ret_errno = errno;
 
853
    ret = errno;
858
854
  }
859
855
  errno = old_errno;
860
 
  return ret_errno;
 
856
  return ret;
861
857
}
862
858
 
863
859
/* Lower privileges permanently */
864
860
__attribute__((warn_unused_result))
865
 
error_t lower_privileges_permanently(void){
866
 
  error_t old_errno = errno;
867
 
  error_t ret_errno = 0;
 
861
int lower_privileges_permanently(void){
 
862
  int old_errno = errno;
 
863
  int ret = 0;
868
864
  if(setuid(uid) == -1){
869
 
    ret_errno = errno;
 
865
    ret = errno;
870
866
  }
871
867
  errno = old_errno;
872
 
  return ret_errno;
 
868
  return ret;
873
869
}
874
870
 
875
871
/* Helper function to add_local_route() and delete_local_route() */
931
927
      perror_plus("dup2(devnull, STDIN_FILENO)");
932
928
      _exit(EX_OSERR);
933
929
    }
934
 
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
930
    ret = close(devnull);
935
931
    if(ret == -1){
936
932
      perror_plus("close");
937
933
      _exit(EX_OSERR);
954
950
                                                   helper, O_RDONLY));
955
951
    if(helper_fd == -1){
956
952
      perror_plus("openat");
 
953
      close(helperdir_fd);
957
954
      _exit(EX_UNAVAILABLE);
958
955
    }
959
 
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
956
    close(helperdir_fd);
960
957
#ifdef __GNUC__
961
958
#pragma GCC diagnostic push
962
959
#pragma GCC diagnostic ignored "-Wcast-qual"
1130
1127
    goto mandos_end;
1131
1128
  }
1132
1129
  
1133
 
  memset(&to, 0, sizeof(to));
1134
1130
  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);
 
1131
    struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to;
 
1132
    *to6 = (struct sockaddr_in6){ .sin6_family = (sa_family_t)af };
 
1133
    ret = inet_pton(af, ip, &to6->sin6_addr);
1137
1134
  } 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);
 
1135
    struct sockaddr_in *to4 = (struct sockaddr_in *)&to;
 
1136
    *to4 = (struct sockaddr_in){ .sin_family = (sa_family_t)af };
 
1137
    ret = inet_pton(af, ip, &to4->sin_addr);
1140
1138
  }
1141
1139
  if(ret < 0 ){
1142
1140
    int e = errno;
1221
1219
                    sizeof(struct sockaddr_in));
1222
1220
    }
1223
1221
    if(ret < 0){
1224
 
      if(errno == ENETUNREACH
 
1222
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1225
1223
         and if_index != AVAHI_IF_UNSPEC
1226
1224
         and connect_to == NULL
1227
1225
         and not route_added and
1240
1238
           with an explicit route added with the server's address.
1241
1239
           
1242
1240
           Avahi bug reference:
1243
 
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
1241
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1244
1242
           https://bugs.debian.org/587961
1245
1243
        */
1246
1244
        if(debug){
1426
1424
                                               &decrypted_buffer, mc);
1427
1425
    if(decrypted_buffer_size >= 0){
1428
1426
      
 
1427
      clearerr(stdout);
1429
1428
      written = 0;
1430
1429
      while(written < (size_t) decrypted_buffer_size){
1431
1430
        if(quit_now){
1447
1446
        }
1448
1447
        written += (size_t)ret;
1449
1448
      }
 
1449
      ret = fflush(stdout);
 
1450
      if(ret != 0){
 
1451
        int e = errno;
 
1452
        if(debug){
 
1453
          fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
1454
                       strerror(errno));
 
1455
        }
 
1456
        errno = e;
 
1457
        goto mandos_end;
 
1458
      }
1450
1459
      retval = 0;
1451
1460
    }
1452
1461
  }
1465
1474
    free(decrypted_buffer);
1466
1475
    free(buffer);
1467
1476
    if(tcp_sd >= 0){
1468
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
1477
      ret = close(tcp_sd);
1469
1478
    }
1470
1479
    if(ret == -1){
1471
1480
      if(e == 0){
1626
1635
__attribute__((nonnull, warn_unused_result))
1627
1636
bool get_flags(const char *ifname, struct ifreq *ifr){
1628
1637
  int ret;
1629
 
  error_t ret_errno;
 
1638
  int old_errno;
1630
1639
  
1631
1640
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1632
1641
  if(s < 0){
1633
 
    ret_errno = errno;
 
1642
    old_errno = errno;
1634
1643
    perror_plus("socket");
1635
 
    errno = ret_errno;
 
1644
    errno = old_errno;
1636
1645
    return false;
1637
1646
  }
1638
 
  strcpy(ifr->ifr_name, ifname);
 
1647
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1648
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1639
1649
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1640
1650
  if(ret == -1){
1641
1651
    if(debug){
1642
 
      ret_errno = errno;
 
1652
      old_errno = errno;
1643
1653
      perror_plus("ioctl SIOCGIFFLAGS");
1644
 
      errno = ret_errno;
 
1654
      errno = old_errno;
1645
1655
    }
1646
1656
    return false;
1647
1657
  }
1911
1921
      return;
1912
1922
    }
1913
1923
  }
1914
 
#ifdef __GLIBC__
1915
 
#if __GLIBC_PREREQ(2, 15)
1916
1924
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1917
1925
                           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
1926
  if(numhooks == -1){
1927
1927
    perror_plus("scandir");
1928
1928
    return;
2010
2010
        perror_plus("openat");
2011
2011
        _exit(EXIT_FAILURE);
2012
2012
      }
2013
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2013
      if(close(hookdir_fd) == -1){
2014
2014
        perror_plus("close");
2015
2015
        _exit(EXIT_FAILURE);
2016
2016
      }
2019
2019
        perror_plus("dup2(devnull, STDIN_FILENO)");
2020
2020
        _exit(EX_OSERR);
2021
2021
      }
2022
 
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
2022
      ret = close(devnull);
2023
2023
      if(ret == -1){
2024
2024
        perror_plus("close");
2025
2025
        _exit(EX_OSERR);
2074
2074
    free(direntry);
2075
2075
  }
2076
2076
  free(direntries);
2077
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2077
  if(close(hookdir_fd) == -1){
2078
2078
    perror_plus("close");
2079
2079
  } else {
2080
2080
    hookdir_fd = -1;
2083
2083
}
2084
2084
 
2085
2085
__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;
 
2086
int bring_up_interface(const char *const interface,
 
2087
                       const float delay){
 
2088
  int old_errno = errno;
2089
2089
  int ret;
2090
2090
  struct ifreq network;
2091
2091
  unsigned int if_index = if_nametoindex(interface);
2101
2101
  }
2102
2102
  
2103
2103
  if(not interface_is_up(interface)){
2104
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2104
    int ret_errno = 0;
 
2105
    int ioctl_errno = 0;
2105
2106
    if(not get_flags(interface, &network)){
2106
2107
      ret_errno = errno;
2107
2108
      fprintf_plus(stderr, "Failed to get flags for interface "
2120
2121
    }
2121
2122
    
2122
2123
    if(quit_now){
2123
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2124
      ret = close(sd);
2124
2125
      if(ret == -1){
2125
2126
        perror_plus("close");
2126
2127
      }
2176
2177
    }
2177
2178
    
2178
2179
    /* Close the socket */
2179
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2180
    ret = close(sd);
2180
2181
    if(ret == -1){
2181
2182
      perror_plus("close");
2182
2183
    }
2210
2211
}
2211
2212
 
2212
2213
__attribute__((nonnull, warn_unused_result))
2213
 
error_t take_down_interface(const char *const interface){
2214
 
  error_t old_errno = errno;
 
2214
int take_down_interface(const char *const interface){
 
2215
  int old_errno = errno;
2215
2216
  struct ifreq network;
2216
2217
  unsigned int if_index = if_nametoindex(interface);
2217
2218
  if(if_index == 0){
2220
2221
    return ENXIO;
2221
2222
  }
2222
2223
  if(interface_is_up(interface)){
2223
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
2224
    int ret_errno = 0;
 
2225
    int ioctl_errno = 0;
2224
2226
    if(not get_flags(interface, &network) and debug){
2225
2227
      ret_errno = errno;
2226
2228
      fprintf_plus(stderr, "Failed to get flags for interface "
2264
2266
    }
2265
2267
    
2266
2268
    /* Close the socket */
2267
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2269
    int ret = close(sd);
2268
2270
    if(ret == -1){
2269
2271
      perror_plus("close");
2270
2272
    }
2286
2288
 
2287
2289
int main(int argc, char *argv[]){
2288
2290
  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 };
 
2291
                        .priority = "SECURE256:!CTYPE-X.509"
 
2292
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
 
2293
                        .current_server = NULL, .interfaces = NULL,
 
2294
                        .interfaces_size = 0 };
2292
2295
  AvahiSServiceBrowser *sb = NULL;
2293
2296
  error_t ret_errno;
2294
2297
  int ret;
2475
2478
                         .args_doc = "",
2476
2479
                         .doc = "Mandos client -- Get and decrypt"
2477
2480
                         " passwords from a Mandos server" };
2478
 
    ret = argp_parse(&argp, argc, argv,
2479
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2480
 
    switch(ret){
 
2481
    ret_errno = argp_parse(&argp, argc, argv,
 
2482
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2483
    switch(ret_errno){
2481
2484
    case 0:
2482
2485
      break;
2483
2486
    case ENOMEM:
2484
2487
    default:
2485
 
      errno = ret;
 
2488
      errno = ret_errno;
2486
2489
      perror_plus("argp_parse");
2487
2490
      exitcode = EX_OSERR;
2488
2491
      goto end;
2494
2497
  
2495
2498
  {
2496
2499
    /* Work around Debian bug #633582:
2497
 
       <http://bugs.debian.org/633582> */
 
2500
       <https://bugs.debian.org/633582> */
2498
2501
    
2499
2502
    /* Re-raise privileges */
2500
 
    ret_errno = raise_privileges();
2501
 
    if(ret_errno != 0){
2502
 
      errno = ret_errno;
 
2503
    ret = raise_privileges();
 
2504
    if(ret != 0){
 
2505
      errno = ret;
2503
2506
      perror_plus("Failed to raise privileges");
2504
2507
    } else {
2505
2508
      struct stat st;
2521
2524
              }
2522
2525
            }
2523
2526
          }
2524
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
2527
          close(seckey_fd);
2525
2528
        }
2526
2529
      }
2527
2530
      
2542
2545
              }
2543
2546
            }
2544
2547
          }
2545
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
2548
          close(pubkey_fd);
 
2549
        }
 
2550
      }
 
2551
      
 
2552
      if(dh_params_file != NULL
 
2553
         and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
 
2554
        int dhparams_fd = open(dh_params_file, O_RDONLY);
 
2555
        if(dhparams_fd == -1){
 
2556
          perror_plus("open");
 
2557
        } else {
 
2558
          ret = (int)TEMP_FAILURE_RETRY(fstat(dhparams_fd, &st));
 
2559
          if(ret == -1){
 
2560
            perror_plus("fstat");
 
2561
          } else {
 
2562
            if(S_ISREG(st.st_mode)
 
2563
               and st.st_uid == 0 and st.st_gid == 0){
 
2564
              ret = fchown(dhparams_fd, uid, gid);
 
2565
              if(ret == -1){
 
2566
                perror_plus("fchown");
 
2567
              }
 
2568
            }
 
2569
          }
 
2570
          close(dhparams_fd);
2546
2571
        }
2547
2572
      }
2548
2573
      
2549
2574
      /* Lower privileges */
2550
 
      ret_errno = lower_privileges();
2551
 
      if(ret_errno != 0){
2552
 
        errno = ret_errno;
 
2575
      ret = lower_privileges();
 
2576
      if(ret != 0){
 
2577
        errno = ret;
2553
2578
        perror_plus("Failed to lower privileges");
2554
2579
      }
2555
2580
    }
2725
2750
      errno = bring_up_interface(interface, delay);
2726
2751
      if(not interface_was_up){
2727
2752
        if(errno != 0){
2728
 
          perror_plus("Failed to bring up interface");
 
2753
          fprintf_plus(stderr, "Failed to bring up interface \"%s\":"
 
2754
                       " %s\n", interface, strerror(errno));
2729
2755
        } else {
2730
2756
          errno = argz_add(&interfaces_to_take_down,
2731
2757
                           &interfaces_to_take_down_size,
2882
2908
    
2883
2909
    /* Allocate a new server */
2884
2910
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2885
 
                                 &config, NULL, NULL, &ret_errno);
 
2911
                                 &config, NULL, NULL, &ret);
2886
2912
    
2887
2913
    /* Free the Avahi configuration data */
2888
2914
    avahi_server_config_free(&config);
2891
2917
  /* Check if creating the Avahi server object succeeded */
2892
2918
  if(mc.server == NULL){
2893
2919
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2894
 
                 avahi_strerror(ret_errno));
 
2920
                 avahi_strerror(ret));
2895
2921
    exitcode = EX_UNAVAILABLE;
2896
2922
    goto end;
2897
2923
  }
2932
2958
 end:
2933
2959
  
2934
2960
  if(debug){
2935
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2961
    if(signal_received){
 
2962
      fprintf_plus(stderr, "%s exiting due to signal %d: %s\n",
 
2963
                   argv[0], signal_received,
 
2964
                   strsignal(signal_received));
 
2965
    } else {
 
2966
      fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2967
    }
2936
2968
  }
2937
2969
  
2938
2970
  /* Cleanup things */
2949
2981
  
2950
2982
  if(gnutls_initialized){
2951
2983
    gnutls_certificate_free_credentials(mc.cred);
2952
 
    gnutls_global_deinit();
2953
2984
    gnutls_dh_params_deinit(mc.dh_params);
2954
2985
  }
2955
2986
  
2978
3009
  
2979
3010
  /* Re-raise privileges */
2980
3011
  {
2981
 
    ret_errno = raise_privileges();
2982
 
    if(ret_errno != 0){
2983
 
      errno = ret_errno;
 
3012
    ret = raise_privileges();
 
3013
    if(ret != 0){
 
3014
      errno = ret;
2984
3015
      perror_plus("Failed to raise privileges");
2985
3016
    } else {
2986
3017
      
2994
3025
        while((interface=argz_next(interfaces_to_take_down,
2995
3026
                                   interfaces_to_take_down_size,
2996
3027
                                   interface))){
2997
 
          ret_errno = take_down_interface(interface);
2998
 
          if(ret_errno != 0){
2999
 
            errno = ret_errno;
 
3028
          ret = take_down_interface(interface);
 
3029
          if(ret != 0){
 
3030
            errno = ret;
3000
3031
            perror_plus("Failed to take down interface");
3001
3032
          }
3002
3033
        }
3007
3038
      }
3008
3039
    }
3009
3040
    
3010
 
    ret_errno = lower_privileges_permanently();
3011
 
    if(ret_errno != 0){
3012
 
      errno = ret_errno;
 
3041
    ret = lower_privileges_permanently();
 
3042
    if(ret != 0){
 
3043
      errno = ret;
3013
3044
      perror_plus("Failed to lower privileges permanently");
3014
3045
    }
3015
3046
  }
3017
3048
  free(interfaces_to_take_down);
3018
3049
  free(interfaces_hooks);
3019
3050
  
 
3051
  void clean_dir_at(int base, const char * const dirname,
 
3052
                    uintmax_t level){
 
3053
    struct dirent **direntries = NULL;
 
3054
    int dret;
 
3055
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
 
3056
                                                O_RDONLY
 
3057
                                                | O_NOFOLLOW
 
3058
                                                | O_DIRECTORY
 
3059
                                                | O_PATH));
 
3060
    if(dir_fd == -1){
 
3061
      perror_plus("open");
 
3062
    }
 
3063
    int numentries = scandirat(dir_fd, ".", &direntries,
 
3064
                               notdotentries, alphasort);
 
3065
    if(numentries >= 0){
 
3066
      for(int i = 0; i < numentries; i++){
 
3067
        if(debug){
 
3068
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
 
3069
                       dirname, direntries[i]->d_name);
 
3070
        }
 
3071
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
 
3072
        if(dret == -1){
 
3073
          if(errno == EISDIR){
 
3074
              dret = unlinkat(dir_fd, direntries[i]->d_name,
 
3075
                              AT_REMOVEDIR);
 
3076
          }         
 
3077
          if((dret == -1) and (errno == ENOTEMPTY)
 
3078
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
 
3079
                  == 0) and (level == 0)){
 
3080
            /* Recurse only in this special case */
 
3081
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
 
3082
            dret = 0;
 
3083
          }
 
3084
          if(dret == -1){
 
3085
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
 
3086
                         direntries[i]->d_name, strerror(errno));
 
3087
          }
 
3088
        }
 
3089
        free(direntries[i]);
 
3090
      }
 
3091
      
 
3092
      /* need to clean even if 0 because man page doesn't specify */
 
3093
      free(direntries);
 
3094
      if(numentries == -1){
 
3095
        perror_plus("scandirat");
 
3096
      }
 
3097
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
 
3098
      if(dret == -1 and errno != ENOENT){
 
3099
        perror_plus("rmdir");
 
3100
      }
 
3101
    } else {
 
3102
      perror_plus("scandirat");
 
3103
    }
 
3104
    close(dir_fd);
 
3105
  }
 
3106
  
3020
3107
  /* Removes the GPGME temp directory and all files inside */
3021
3108
  if(tempdir != NULL){
3022
 
    struct dirent **direntries = NULL;
3023
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3024
 
                                                  | O_NOFOLLOW
3025
 
                                                  | O_DIRECTORY
3026
 
                                                  | O_PATH));
3027
 
    if(tempdir_fd == -1){
3028
 
      perror_plus("open");
3029
 
    } else {
3030
 
#ifdef __GLIBC__
3031
 
#if __GLIBC_PREREQ(2, 15)
3032
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
3033
 
                                 notdotentries, alphasort);
3034
 
#else  /* not __GLIBC_PREREQ(2, 15) */
3035
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3036
 
                               alphasort);
3037
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
3038
 
#else   /* not __GLIBC__ */
3039
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
3040
 
                               alphasort);
3041
 
#endif  /* not __GLIBC__ */
3042
 
      if(numentries >= 0){
3043
 
        for(int i = 0; i < numentries; i++){
3044
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3045
 
          if(ret == -1){
3046
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3047
 
                         " \"%s\", 0): %s\n", tempdir,
3048
 
                         direntries[i]->d_name, strerror(errno));
3049
 
          }
3050
 
          free(direntries[i]);
3051
 
        }
3052
 
        
3053
 
        /* need to clean even if 0 because man page doesn't specify */
3054
 
        free(direntries);
3055
 
        if(numentries == -1){
3056
 
          perror_plus("scandir");
3057
 
        }
3058
 
        ret = rmdir(tempdir);
3059
 
        if(ret == -1 and errno != ENOENT){
3060
 
          perror_plus("rmdir");
3061
 
        }
3062
 
      }
3063
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
3064
 
    }
 
3109
    clean_dir_at(-1, tempdir, 0);
3065
3110
  }
3066
3111
  
3067
3112
  if(quit_now){