9
9
 * "browse_callback", and parts of "main".
 
11
11
 * Everything else is
 
12
 
 * Copyright © 2008-2013 Teddy Hogeborn
 
13
 
 * Copyright © 2008-2013 Björn Påhlsson
 
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.
 
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
 
 
15
 * This file is part of Mandos.
 
 
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.
 
 
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.
 
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/>.
 
29
30
 * Contact the authors at <mandos@recompile.se>.
 
 
32
33
/* Needed by GPGME, specifically gpgme_data_seek() */
 
33
34
#ifndef _LARGEFILE_SOURCE
 
34
35
#define _LARGEFILE_SOURCE
 
 
36
#endif  /* not _LARGEFILE_SOURCE */
 
36
37
#ifndef _FILE_OFFSET_BITS
 
37
38
#define _FILE_OFFSET_BITS 64
 
 
39
#endif  /* not _FILE_OFFSET_BITS */
 
40
41
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
42
43
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
 
43
 
                                   stdout, ferror(), remove() */
 
44
45
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
 
45
46
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
46
47
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
47
48
                                   strtof(), abort() */
 
48
49
#include <stdbool.h>            /* bool, false, true */
 
49
 
#include <string.h>             /* memset(), strcmp(), strlen(),
 
50
 
                                   strerror(), asprintf(), strcpy() */
 
 
50
#include <string.h>             /* strcmp(), strlen(), strerror(),
 
 
51
                                   asprintf(), strncpy(), strsignal()
 
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(),
 
60
 
#include <fcntl.h>              /* open() */
 
 
62
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
 
61
63
#include <dirent.h>             /* opendir(), struct dirent, readdir()
 
63
65
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
 
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,
 
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,
 
 
555
574
                 safer_gnutls_strerror(ret));
 
558
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
559
 
  if(ret != GNUTLS_E_SUCCESS){
 
560
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
 
561
 
                 safer_gnutls_strerror(ret));
 
 
577
  /* If a Diffie-Hellman parameters file was given, try to use it */
 
 
578
  if(dhparamsfilename != NULL){
 
 
579
    gnutls_datum_t params = { .data = NULL, .size = 0 };
 
 
581
      int dhpfile = open(dhparamsfilename, O_RDONLY);
 
 
584
        dhparamsfilename = NULL;
 
 
587
      size_t params_capacity = 0;
 
 
589
        params_capacity = incbuffer((char **)¶ms.data,
 
 
591
                                    (size_t)params_capacity);
 
 
592
        if(params_capacity == 0){
 
 
593
          perror_plus("incbuffer");
 
 
596
          dhparamsfilename = NULL;
 
 
599
        ssize_t bytes_read = read(dhpfile,
 
 
600
                                  params.data + params.size,
 
 
606
        /* check bytes_read for failure */
 
 
611
          dhparamsfilename = NULL;
 
 
614
        params.size += (unsigned int)bytes_read;
 
 
616
      ret = close(dhpfile);
 
 
618
        perror_plus("close");
 
 
620
      if(params.data == NULL){
 
 
621
        dhparamsfilename = NULL;
 
 
623
      if(dhparamsfilename == NULL){
 
 
626
      ret = gnutls_dh_params_import_pkcs3(mc->dh_params, ¶ms,
 
 
627
                                          GNUTLS_X509_FMT_PEM);
 
 
628
      if(ret != GNUTLS_E_SUCCESS){
 
 
629
        fprintf_plus(stderr, "Failed to parse DH parameters in file"
 
 
630
                     " \"%s\": %s\n", dhparamsfilename,
 
 
631
                     safer_gnutls_strerror(ret));
 
 
632
        dhparamsfilename = NULL;
 
 
637
  if(dhparamsfilename == NULL){
 
 
638
    if(mc->dh_bits == 0){
 
 
639
      /* Find out the optimal number of DH bits */
 
 
640
      /* Try to read the private key file */
 
 
641
      gnutls_datum_t buffer = { .data = NULL, .size = 0 };
 
 
643
        int secfile = open(seckeyfilename, O_RDONLY);
 
 
648
        size_t buffer_capacity = 0;
 
 
650
          buffer_capacity = incbuffer((char **)&buffer.data,
 
 
652
                                      (size_t)buffer_capacity);
 
 
653
          if(buffer_capacity == 0){
 
 
654
            perror_plus("incbuffer");
 
 
659
          ssize_t bytes_read = read(secfile,
 
 
660
                                    buffer.data + buffer.size,
 
 
666
          /* check bytes_read for failure */
 
 
673
          buffer.size += (unsigned int)bytes_read;
 
 
677
      /* If successful, use buffer to parse private key */
 
 
678
      gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
 
 
679
      if(buffer.data != NULL){
 
 
681
          gnutls_openpgp_privkey_t privkey = NULL;
 
 
682
          ret = gnutls_openpgp_privkey_init(&privkey);
 
 
683
          if(ret != GNUTLS_E_SUCCESS){
 
 
684
            fprintf_plus(stderr, "Error initializing OpenPGP key"
 
 
686
                         safer_gnutls_strerror(ret));
 
 
690
            ret = gnutls_openpgp_privkey_import
 
 
691
              (privkey, &buffer, GNUTLS_OPENPGP_FMT_BASE64, "", 0);
 
 
692
            if(ret != GNUTLS_E_SUCCESS){
 
 
693
              fprintf_plus(stderr, "Error importing OpenPGP key : %s",
 
 
694
                           safer_gnutls_strerror(ret));
 
 
700
              /* Use private key to suggest an appropriate
 
 
702
              sec_param = gnutls_openpgp_privkey_sec_param(privkey);
 
 
703
              gnutls_openpgp_privkey_deinit(privkey);
 
 
705
                fprintf_plus(stderr, "This OpenPGP key implies using"
 
 
706
                             " a GnuTLS security parameter \"%s\".\n",
 
 
707
                             safe_string(gnutls_sec_param_get_name
 
 
713
        if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
 
 
714
          /* Err on the side of caution */
 
 
715
          sec_param = GNUTLS_SEC_PARAM_ULTRA;
 
 
717
            fprintf_plus(stderr, "Falling back to security parameter"
 
 
719
                         safe_string(gnutls_sec_param_get_name
 
 
724
      uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
 
 
728
          fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
 
 
729
                       " implies %u DH bits; using that.\n",
 
 
730
                       safe_string(gnutls_sec_param_get_name
 
 
735
        fprintf_plus(stderr, "Failed to get implied number of DH"
 
 
736
                     " bits for security parameter \"%s\"): %s\n",
 
 
737
                     safe_string(gnutls_sec_param_get_name
 
 
739
                     safer_gnutls_strerror(ret));
 
 
743
      fprintf_plus(stderr, "DH bits explicitly set to %u\n",
 
 
746
    ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
 
747
    if(ret != GNUTLS_E_SUCCESS){
 
 
748
      fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
 
 
749
                   " bits): %s\n", mc->dh_bits,
 
 
750
                   safer_gnutls_strerror(ret));
 
565
754
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
 
 
636
822
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
637
823
                      __attribute__((unused)) const char *txt){}
 
 
825
/* Set effective uid to 0, return errno */
 
 
826
__attribute__((warn_unused_result))
 
 
827
int raise_privileges(void){
 
 
828
  int old_errno = errno;
 
 
830
  if(seteuid(0) == -1){
 
 
837
/* Set effective and real user ID to 0.  Return errno. */
 
 
838
__attribute__((warn_unused_result))
 
 
839
int raise_privileges_permanently(void){
 
 
840
  int old_errno = errno;
 
 
841
  int ret = raise_privileges();
 
 
853
/* Set effective user ID to unprivileged saved user ID */
 
 
854
__attribute__((warn_unused_result))
 
 
855
int lower_privileges(void){
 
 
856
  int old_errno = errno;
 
 
858
  if(seteuid(uid) == -1){
 
 
865
/* Lower privileges permanently */
 
 
866
__attribute__((warn_unused_result))
 
 
867
int lower_privileges_permanently(void){
 
 
868
  int old_errno = errno;
 
 
870
  if(setuid(uid) == -1){
 
 
877
/* Helper function to add_local_route() and delete_local_route() */
 
 
878
__attribute__((nonnull, warn_unused_result))
 
 
879
static bool add_delete_local_route(const bool add,
 
 
881
                                   AvahiIfIndex if_index){
 
 
883
  char helper[] = "mandos-client-iprouteadddel";
 
 
884
  char add_arg[] = "add";
 
 
885
  char delete_arg[] = "delete";
 
 
886
  char debug_flag[] = "--debug";
 
 
887
  char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
 
 
888
  if(pluginhelperdir == NULL){
 
 
890
      fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
 
 
891
                   " variable not set; cannot run helper\n");
 
 
896
  char interface[IF_NAMESIZE];
 
 
897
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
 
898
    perror_plus("if_indextoname");
 
 
902
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
904
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
 
910
    /* Raise privileges */
 
 
911
    errno = raise_privileges_permanently();
 
 
913
      perror_plus("Failed to raise privileges");
 
 
914
      /* _exit(EX_NOPERM); */
 
 
920
        perror_plus("setgid");
 
 
923
      /* Reset supplementary groups */
 
 
925
      ret = setgroups(0, NULL);
 
 
927
        perror_plus("setgroups");
 
 
931
    ret = dup2(devnull, STDIN_FILENO);
 
 
933
      perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
936
    ret = close(devnull);
 
 
938
      perror_plus("close");
 
 
941
    ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
943
      perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
946
    int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
 
 
951
    if(helperdir_fd == -1){
 
 
953
      _exit(EX_UNAVAILABLE);
 
 
955
    int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
 
 
958
      perror_plus("openat");
 
 
960
      _exit(EX_UNAVAILABLE);
 
 
964
#pragma GCC diagnostic push
 
 
965
#pragma GCC diagnostic ignored "-Wcast-qual"
 
 
967
    if(fexecve(helper_fd, (char *const [])
 
 
968
               { helper, add ? add_arg : delete_arg, (char *)address,
 
 
969
                   interface, debug ? debug_flag : NULL, NULL },
 
 
972
#pragma GCC diagnostic pop
 
 
974
      perror_plus("fexecve");
 
 
986
    pret = waitpid(pid, &status, 0);
 
 
987
    if(pret == -1 and errno == EINTR and quit_now){
 
 
988
      int errno_raising = 0;
 
 
989
      if((errno = raise_privileges()) != 0){
 
 
990
        errno_raising = errno;
 
 
991
        perror_plus("Failed to raise privileges in order to"
 
 
992
                    " kill helper program");
 
 
994
      if(kill(pid, SIGTERM) == -1){
 
 
997
      if((errno_raising == 0) and (errno = lower_privileges()) != 0){
 
 
998
        perror_plus("Failed to lower privileges after killing"
 
 
1003
  } while(pret == -1 and errno == EINTR);
 
 
1005
    perror_plus("waitpid");
 
 
1008
  if(WIFEXITED(status)){
 
 
1009
    if(WEXITSTATUS(status) != 0){
 
 
1010
      fprintf_plus(stderr, "Error: iprouteadddel exited"
 
 
1011
                   " with status %d\n", WEXITSTATUS(status));
 
 
1016
  if(WIFSIGNALED(status)){
 
 
1017
    fprintf_plus(stderr, "Error: iprouteadddel died by"
 
 
1018
                 " signal %d\n", WTERMSIG(status));
 
 
1021
  fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
 
 
1025
__attribute__((nonnull, warn_unused_result))
 
 
1026
static bool add_local_route(const char *address,
 
 
1027
                            AvahiIfIndex if_index){
 
 
1029
    fprintf_plus(stderr, "Adding route to %s\n", address);
 
 
1031
  return add_delete_local_route(true, address, if_index);
 
 
1034
__attribute__((nonnull, warn_unused_result))
 
 
1035
static bool delete_local_route(const char *address,
 
 
1036
                               AvahiIfIndex if_index){
 
 
1038
    fprintf_plus(stderr, "Removing route to %s\n", address);
 
 
1040
  return add_delete_local_route(false, address, if_index);
 
639
1043
/* Called when a Mandos server is found */
 
640
1044
__attribute__((nonnull, warn_unused_result))
 
641
1045
static int start_mandos_communication(const char *ip, in_port_t port,
 
 
810
1217
    goto mandos_end;
 
814
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
815
 
                  sizeof(struct sockaddr_in6));
 
817
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
818
 
                  sizeof(struct sockaddr_in));
 
821
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
823
 
      perror_plus("connect");
 
 
1222
      ret = connect(tcp_sd, (struct sockaddr *)&to,
 
 
1223
                    sizeof(struct sockaddr_in6));
 
 
1225
      ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
 
1226
                    sizeof(struct sockaddr_in));
 
 
1229
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
 
 
1230
         and if_index != AVAHI_IF_UNSPEC
 
 
1231
         and connect_to == NULL
 
 
1232
         and not route_added and
 
 
1233
         ((af == AF_INET6 and not
 
 
1234
           IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
 
 
1236
          or (af == AF_INET and
 
 
1237
              /* Not a a IPv4LL address */
 
 
1238
              (ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
 
 
1239
               & 0xFFFF0000L) != 0xA9FE0000L))){
 
 
1240
        /* Work around Avahi bug - Avahi does not announce link-local
 
 
1241
           addresses if it has a global address, so local hosts with
 
 
1242
           *only* a link-local address (e.g. Mandos clients) cannot
 
 
1243
           connect to a Mandos server announced by Avahi on a server
 
 
1244
           host with a global address.  Work around this by retrying
 
 
1245
           with an explicit route added with the server's address.
 
 
1247
           Avahi bug reference:
 
 
1248
           https://lists.freedesktop.org/archives/avahi/2010-February/001833.html
 
 
1249
           https://bugs.debian.org/587961
 
 
1252
          fprintf_plus(stderr, "Mandos server unreachable, trying"
 
 
1256
        route_added = add_local_route(ip, if_index);
 
 
1262
      if(errno != ECONNREFUSED or debug){
 
 
1264
        perror_plus("connect");
 
834
1277
  const char *out = mandos_protocol_version;
 
 
1457
 
/* Set effective uid to 0, return errno */
 
1458
 
__attribute__((warn_unused_result))
 
1459
 
error_t raise_privileges(void){
 
1460
 
  error_t old_errno = errno;
 
1461
 
  error_t ret_errno = 0;
 
1462
 
  if(seteuid(0) == -1){
 
1464
 
    perror_plus("seteuid");
 
1470
 
/* Set effective and real user ID to 0.  Return errno. */
 
1471
 
__attribute__((warn_unused_result))
 
1472
 
error_t raise_privileges_permanently(void){
 
1473
 
  error_t old_errno = errno;
 
1474
 
  error_t ret_errno = raise_privileges();
 
1479
 
  if(setuid(0) == -1){
 
1481
 
    perror_plus("seteuid");
 
1487
 
/* Set effective user ID to unprivileged saved user ID */
 
1488
 
__attribute__((warn_unused_result))
 
1489
 
error_t lower_privileges(void){
 
1490
 
  error_t old_errno = errno;
 
1491
 
  error_t ret_errno = 0;
 
1492
 
  if(seteuid(uid) == -1){
 
1494
 
    perror_plus("seteuid");
 
1500
 
/* Lower privileges permanently */
 
1501
 
__attribute__((warn_unused_result))
 
1502
 
error_t lower_privileges_permanently(void){
 
1503
 
  error_t old_errno = errno;
 
1504
 
  error_t ret_errno = 0;
 
1505
 
  if(setuid(uid) == -1){
 
1507
 
    perror_plus("setuid");
 
1513
1921
__attribute__((nonnull))
 
1514
1922
void run_network_hooks(const char *mode, const char *interface,
 
1515
1923
                       const float delay){
 
1516
 
  struct dirent **direntries;
 
1517
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1924
  struct dirent **direntries = NULL;
 
 
1925
  if(hookdir_fd == -1){
 
 
1926
    hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
 
 
1928
    if(hookdir_fd == -1){
 
 
1929
      if(errno == ENOENT){
 
 
1931
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
 
1932
                       " found\n", hookdir);
 
 
1935
        perror_plus("open");
 
 
1940
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
1942
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
 
1945
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
 
1946
                           runnable_hook, alphasort);
 
1519
1947
  if(numhooks == -1){
 
1520
 
    if(errno == ENOENT){
 
1522
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1523
 
                     " found\n", hookdir);
 
1526
 
      perror_plus("scandir");
 
 
1948
    perror_plus("scandir");
 
 
1952
  struct dirent *direntry;
 
 
1954
  for(int i = 0; i < numhooks; i++){
 
 
1955
    direntry = direntries[i];
 
 
1957
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1529
 
    struct dirent *direntry;
 
1531
 
    int devnull = open("/dev/null", O_RDONLY);
 
1532
 
    for(int i = 0; i < numhooks; i++){
 
1533
 
      direntry = direntries[i];
 
1534
 
      char *fullname = NULL;
 
1535
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
 
1960
    pid_t hook_pid = fork();
 
 
1963
      /* Raise privileges */
 
 
1964
      errno = raise_privileges_permanently();
 
 
1966
        perror_plus("Failed to raise privileges");
 
 
1973
        perror_plus("setgid");
 
 
1976
      /* Reset supplementary groups */
 
 
1978
      ret = setgroups(0, NULL);
 
 
1980
        perror_plus("setgroups");
 
 
1983
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
 
1985
        perror_plus("setenv");
 
 
1988
      ret = setenv("DEVICE", interface, 1);
 
 
1990
        perror_plus("setenv");
 
 
1993
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
 
1995
        perror_plus("setenv");
 
 
1998
      ret = setenv("MODE", mode, 1);
 
 
2000
        perror_plus("setenv");
 
 
2004
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1537
2006
        perror_plus("asprintf");
 
1541
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1544
 
      pid_t hook_pid = fork();
 
1547
 
        /* Raise privileges */
 
1548
 
        if(raise_privileges_permanently() != 0){
 
1549
 
          perror_plus("Failed to raise privileges");
 
1556
 
          perror_plus("setgid");
 
1559
 
        /* Reset supplementary groups */
 
1561
 
        ret = setgroups(0, NULL);
 
1563
 
          perror_plus("setgroups");
 
1566
 
        ret = dup2(devnull, STDIN_FILENO);
 
1568
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
1571
 
        ret = close(devnull);
 
1573
 
          perror_plus("close");
 
1576
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1578
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1581
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1583
 
          perror_plus("setenv");
 
1586
 
        ret = setenv("DEVICE", interface, 1);
 
1588
 
          perror_plus("setenv");
 
1591
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1593
 
          perror_plus("setenv");
 
1596
 
        ret = setenv("MODE", mode, 1);
 
1598
 
          perror_plus("setenv");
 
1602
 
        ret = asprintf(&delaystring, "%f", (double)delay);
 
1604
 
          perror_plus("asprintf");
 
1607
 
        ret = setenv("DELAY", delaystring, 1);
 
1610
 
          perror_plus("setenv");
 
 
2009
      ret = setenv("DELAY", delaystring, 1);
 
1613
2011
        free(delaystring);
 
1614
 
        if(connect_to != NULL){
 
1615
 
          ret = setenv("CONNECT", connect_to, 1);
 
1617
 
            perror_plus("setenv");
 
1621
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1622
 
          perror_plus("execl");
 
1623
 
          _exit(EXIT_FAILURE);
 
 
2012
        perror_plus("setenv");
 
 
2016
      if(connect_to != NULL){
 
 
2017
        ret = setenv("CONNECT", connect_to, 1);
 
 
2019
          perror_plus("setenv");
 
 
2023
      int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
 
 
2027
        perror_plus("openat");
 
 
2028
        _exit(EXIT_FAILURE);
 
 
2030
      if(close(hookdir_fd) == -1){
 
 
2031
        perror_plus("close");
 
 
2032
        _exit(EXIT_FAILURE);
 
 
2034
      ret = dup2(devnull, STDIN_FILENO);
 
 
2036
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
2039
      ret = close(devnull);
 
 
2041
        perror_plus("close");
 
 
2044
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
2046
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
2049
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
 
2051
        perror_plus("fexecve");
 
 
2052
        _exit(EXIT_FAILURE);
 
 
2056
        perror_plus("fork");
 
 
2061
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
 
2062
        perror_plus("waitpid");
 
 
2066
      if(WIFEXITED(status)){
 
 
2067
        if(WEXITSTATUS(status) != 0){
 
 
2068
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
 
2069
                       " with status %d\n", direntry->d_name,
 
 
2070
                       WEXITSTATUS(status));
 
 
2074
      } else if(WIFSIGNALED(status)){
 
 
2075
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
 
2076
                     " signal %d\n", direntry->d_name,
 
1627
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1628
 
          perror_plus("waitpid");
 
1632
 
        if(WIFEXITED(status)){
 
1633
 
          if(WEXITSTATUS(status) != 0){
 
1634
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1635
 
                         " with status %d\n", direntry->d_name,
 
1636
 
                         WEXITSTATUS(status));
 
1640
 
        } else if(WIFSIGNALED(status)){
 
1641
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1642
 
                       " signal %d\n", direntry->d_name,
 
1647
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1648
 
                       " crashed\n", direntry->d_name);
 
1655
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
2081
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
 
2082
                     " crashed\n", direntry->d_name);
 
 
2088
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
2094
  if(close(hookdir_fd) == -1){
 
 
2095
    perror_plus("close");
 
1663
2102
__attribute__((nonnull, warn_unused_result))
 
1664
 
error_t bring_up_interface(const char *const interface,
 
1666
 
  error_t old_errno = errno;
 
 
2103
int bring_up_interface(const char *const interface,
 
 
2105
  int old_errno = errno;
 
1668
2107
  struct ifreq network;
 
1669
2108
  unsigned int if_index = if_nametoindex(interface);
 
 
2558
3065
  free(interfaces_to_take_down);
 
2559
3066
  free(interfaces_hooks);
 
 
3068
  void clean_dir_at(int base, const char * const dirname,
 
 
3070
    struct dirent **direntries = NULL;
 
 
3072
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
 
 
3078
      perror_plus("open");
 
 
3081
    int numentries = scandirat(dir_fd, ".", &direntries,
 
 
3082
                               notdotentries, alphasort);
 
 
3083
    if(numentries >= 0){
 
 
3084
      for(int i = 0; i < numentries; i++){
 
 
3086
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
 
 
3087
                       dirname, direntries[i]->d_name);
 
 
3089
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
 
 
3091
          if(errno == EISDIR){
 
 
3092
              dret = unlinkat(dir_fd, direntries[i]->d_name,
 
 
3095
          if((dret == -1) and (errno == ENOTEMPTY)
 
 
3096
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
 
 
3097
                  == 0) and (level == 0)){
 
 
3098
            /* Recurse only in this special case */
 
 
3099
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
 
 
3102
          if((dret == -1) and (errno != ENOENT)){
 
 
3103
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
 
 
3104
                         direntries[i]->d_name, strerror(errno));
 
 
3107
        free(direntries[i]);
 
 
3110
      /* need to clean even if 0 because man page doesn't specify */
 
 
3112
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
 
 
3113
      if(dret == -1 and errno != ENOENT){
 
 
3114
        perror_plus("rmdir");
 
 
3117
      perror_plus("scandirat");
 
2561
3122
  /* Removes the GPGME temp directory and all files inside */
 
2562
 
  if(tempdir_created){
 
2563
 
    struct dirent **direntries = NULL;
 
2564
 
    struct dirent *direntry = NULL;
 
2565
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2568
 
      for(int i = 0; i < numentries; i++){
 
2569
 
        direntry = direntries[i];
 
2570
 
        char *fullname = NULL;
 
2571
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2574
 
          perror_plus("asprintf");
 
2577
 
        ret = remove(fullname);
 
2579
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2586
 
    /* need to clean even if 0 because man page doesn't specify */
 
2588
 
    if(numentries == -1){
 
2589
 
      perror_plus("scandir");
 
2591
 
    ret = rmdir(tempdir);
 
2592
 
    if(ret == -1 and errno != ENOENT){
 
2593
 
      perror_plus("rmdir");
 
 
3123
  if(tempdir != NULL){
 
 
3124
    clean_dir_at(-1, tempdir, 0);