/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2015-07-12 01:57:54 UTC
  • Revision ID: teddy@recompile.se-20150712015754-1lq7u2ejo7fdyfvi
* debian/mandos.prerm: Don't run init script, use only invoke-rc.d.

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