/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: 2015-07-07 15:49:49 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 325.
  • Revision ID: teddy@recompile.se-20150707154949-vbuj4pc6wf3o1vgz
mandos.service: Use Type=dbus (implicitly).

mandos.service ([Service]/Type): Removed.
               ([Service]/BusName): Uncommented; set to
                                    "se.recompile.Mandos".

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-2016 Teddy Hogeborn
13
 
 * Copyright © 2008-2016 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() */
 
49
#include <string.h>             /* memset(), strcmp(), strlen(),
 
50
                                   strerror(), asprintf(), strcpy() */
51
51
#include <sys/ioctl.h>          /* ioctl */
52
52
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
53
                                   sockaddr_in6, PF_INET6,
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
64
64
                                   strtoimax() */
65
 
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
66
 
                                   EAI_SYSTEM, ENETUNREACH,
67
 
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
68
 
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
69
 
                                   ENOTEMPTY,
 
65
#include <errno.h>              /* perror(), errno,
70
66
                                   program_invocation_short_name */
71
67
#include <time.h>               /* nanosleep(), time(), sleep() */
72
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
309
305
      return false;
310
306
    }
311
307
    
312
 
    ret = close(fd);
 
308
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
313
309
    if(ret == -1){
314
310
      perror_plus("close");
315
311
    }
517
513
  fprintf_plus(stderr, "GnuTLS: %s", string);
518
514
}
519
515
 
520
 
__attribute__((nonnull(1, 2, 4), warn_unused_result))
 
516
__attribute__((nonnull, warn_unused_result))
521
517
static int init_gnutls_global(const char *pubkeyfilename,
522
518
                              const char *seckeyfilename,
523
 
                              const char *dhparamsfilename,
524
519
                              mandos_context *mc){
525
520
  int ret;
526
521
  unsigned int uret;
529
524
    fprintf_plus(stderr, "Initializing GnuTLS\n");
530
525
  }
531
526
  
 
527
  ret = gnutls_global_init();
 
528
  if(ret != GNUTLS_E_SUCCESS){
 
529
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
530
                 safer_gnutls_strerror(ret));
 
531
    return -1;
 
532
  }
 
533
  
532
534
  if(debug){
533
535
    /* "Use a log level over 10 to enable all debugging options."
534
536
     * - GnuTLS manual
542
544
  if(ret != GNUTLS_E_SUCCESS){
543
545
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
544
546
                 safer_gnutls_strerror(ret));
 
547
    gnutls_global_deinit();
545
548
    return -1;
546
549
  }
547
550
  
572
575
                 safer_gnutls_strerror(ret));
573
576
    goto globalfail;
574
577
  }
575
 
  /* If a Diffie-Hellman parameters file was given, try to use it */
576
 
  if(dhparamsfilename != NULL){
577
 
    gnutls_datum_t params = { .data = NULL, .size = 0 };
578
 
    do {
579
 
      int dhpfile = open(dhparamsfilename, O_RDONLY);
580
 
      if(dhpfile == -1){
581
 
        perror_plus("open");
582
 
        dhparamsfilename = NULL;
583
 
        break;
584
 
      }
585
 
      size_t params_capacity = 0;
 
578
  if(mc->dh_bits == 0){
 
579
    /* Find out the optimal number of DH bits */
 
580
    /* Try to read the private key file */
 
581
    gnutls_datum_t buffer = { .data = NULL, .size = 0 };
 
582
    {
 
583
      int secfile = open(seckeyfilename, O_RDONLY);
 
584
      size_t buffer_capacity = 0;
586
585
      while(true){
587
 
        params_capacity = incbuffer((char **)&params.data,
588
 
                                    (size_t)params.size,
589
 
                                    (size_t)params_capacity);
590
 
        if(params_capacity == 0){
 
586
        buffer_capacity = incbuffer((char **)&buffer.data,
 
587
                                    (size_t)buffer.size,
 
588
                                    (size_t)buffer_capacity);
 
589
        if(buffer_capacity == 0){
591
590
          perror_plus("incbuffer");
592
 
          free(params.data);
593
 
          params.data = NULL;
594
 
          dhparamsfilename = NULL;
 
591
          free(buffer.data);
 
592
          buffer.data = NULL;
595
593
          break;
596
594
        }
597
 
        ssize_t bytes_read = read(dhpfile,
598
 
                                  params.data + params.size,
 
595
        ssize_t bytes_read = read(secfile, buffer.data + buffer.size,
599
596
                                  BUFFER_SIZE);
600
597
        /* EOF */
601
598
        if(bytes_read == 0){
604
601
        /* check bytes_read for failure */
605
602
        if(bytes_read < 0){
606
603
          perror_plus("read");
607
 
          free(params.data);
608
 
          params.data = NULL;
609
 
          dhparamsfilename = NULL;
610
 
          break;
611
 
        }
612
 
        params.size += (unsigned int)bytes_read;
613
 
      }
614
 
      if(params.data == NULL){
615
 
        dhparamsfilename = NULL;
616
 
      }
617
 
      if(dhparamsfilename == NULL){
618
 
        break;
619
 
      }
620
 
      ret = gnutls_dh_params_import_pkcs3(mc->dh_params, &params,
621
 
                                          GNUTLS_X509_FMT_PEM);
622
 
      if(ret != GNUTLS_E_SUCCESS){
623
 
        fprintf_plus(stderr, "Failed to parse DH parameters in file"
624
 
                     " \"%s\": %s\n", dhparamsfilename,
625
 
                     safer_gnutls_strerror(ret));
626
 
        dhparamsfilename = NULL;
627
 
      }
628
 
    } while(false);
629
 
  }
630
 
  if(dhparamsfilename == NULL){
631
 
    if(mc->dh_bits == 0){
632
 
      /* Find out the optimal number of DH bits */
633
 
      /* Try to read the private key file */
634
 
      gnutls_datum_t buffer = { .data = NULL, .size = 0 };
635
 
      do {
636
 
        int secfile = open(seckeyfilename, O_RDONLY);
637
 
        if(secfile == -1){
638
 
          perror_plus("open");
639
 
          break;
640
 
        }
641
 
        size_t buffer_capacity = 0;
642
 
        while(true){
643
 
          buffer_capacity = incbuffer((char **)&buffer.data,
644
 
                                      (size_t)buffer.size,
645
 
                                      (size_t)buffer_capacity);
646
 
          if(buffer_capacity == 0){
647
 
            perror_plus("incbuffer");
648
 
            free(buffer.data);
649
 
            buffer.data = NULL;
650
 
            break;
651
 
          }
652
 
          ssize_t bytes_read = read(secfile,
653
 
                                    buffer.data + buffer.size,
654
 
                                    BUFFER_SIZE);
655
 
          /* EOF */
656
 
          if(bytes_read == 0){
657
 
            break;
658
 
          }
659
 
          /* check bytes_read for failure */
660
 
          if(bytes_read < 0){
661
 
            perror_plus("read");
662
 
            free(buffer.data);
663
 
            buffer.data = NULL;
664
 
            break;
665
 
          }
666
 
          buffer.size += (unsigned int)bytes_read;
667
 
        }
668
 
        close(secfile);
669
 
      } while(false);
670
 
      /* If successful, use buffer to parse private key */
671
 
      gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
672
 
      if(buffer.data != NULL){
673
 
        {
674
 
          gnutls_openpgp_privkey_t privkey = NULL;
675
 
          ret = gnutls_openpgp_privkey_init(&privkey);
 
604
          free(buffer.data);
 
605
          buffer.data = NULL;
 
606
          break;
 
607
        }
 
608
        buffer.size += (unsigned int)bytes_read;
 
609
      }
 
610
      close(secfile);
 
611
    }
 
612
    /* If successful, use buffer to parse private key */
 
613
    gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
 
614
    if(buffer.data != NULL){
 
615
      {
 
616
        gnutls_openpgp_privkey_t privkey = NULL;
 
617
        ret = gnutls_openpgp_privkey_init(&privkey);
 
618
        if(ret != GNUTLS_E_SUCCESS){
 
619
          fprintf_plus(stderr, "Error initializing OpenPGP key"
 
620
                       " structure: %s", safer_gnutls_strerror(ret));
 
621
          free(buffer.data);
 
622
          buffer.data = NULL;
 
623
        } else {
 
624
          ret = gnutls_openpgp_privkey_import(privkey, &buffer,
 
625
                                            GNUTLS_OPENPGP_FMT_BASE64,
 
626
                                              "", 0);
676
627
          if(ret != GNUTLS_E_SUCCESS){
677
 
            fprintf_plus(stderr, "Error initializing OpenPGP key"
678
 
                         " structure: %s",
 
628
            fprintf_plus(stderr, "Error importing OpenPGP key : %s",
679
629
                         safer_gnutls_strerror(ret));
680
 
            free(buffer.data);
681
 
            buffer.data = NULL;
682
 
          } else {
683
 
            ret = gnutls_openpgp_privkey_import
684
 
              (privkey, &buffer, GNUTLS_OPENPGP_FMT_BASE64, "", 0);
685
 
            if(ret != GNUTLS_E_SUCCESS){
686
 
              fprintf_plus(stderr, "Error importing OpenPGP key : %s",
687
 
                           safer_gnutls_strerror(ret));
688
 
              privkey = NULL;
689
 
            }
690
 
            free(buffer.data);
691
 
            buffer.data = NULL;
692
 
            if(privkey != NULL){
693
 
              /* Use private key to suggest an appropriate
694
 
                 sec_param */
695
 
              sec_param = gnutls_openpgp_privkey_sec_param(privkey);
696
 
              gnutls_openpgp_privkey_deinit(privkey);
697
 
              if(debug){
698
 
                fprintf_plus(stderr, "This OpenPGP key implies using"
699
 
                             " a GnuTLS security parameter \"%s\".\n",
700
 
                             safe_string(gnutls_sec_param_get_name
701
 
                                         (sec_param)));
702
 
              }
703
 
            }
 
630
            privkey = NULL;
704
631
          }
705
 
        }
706
 
        if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
707
 
          /* Err on the side of caution */
708
 
          sec_param = GNUTLS_SEC_PARAM_ULTRA;
709
 
          if(debug){
710
 
            fprintf_plus(stderr, "Falling back to security parameter"
711
 
                         " \"%s\"\n",
712
 
                         safe_string(gnutls_sec_param_get_name
713
 
                                     (sec_param)));
 
632
          free(buffer.data);
 
633
          buffer.data = NULL;
 
634
          if(privkey != NULL){
 
635
            /* Use private key to suggest an appropriate sec_param */
 
636
            sec_param = gnutls_openpgp_privkey_sec_param(privkey);
 
637
            gnutls_openpgp_privkey_deinit(privkey);
 
638
            if(debug){
 
639
              fprintf_plus(stderr, "This OpenPGP key implies using a"
 
640
                           " GnuTLS security parameter \"%s\".\n",
 
641
                           safe_string(gnutls_sec_param_get_name
 
642
                                       (sec_param)));
 
643
            }
714
644
          }
715
645
        }
716
646
      }
717
 
      uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
718
 
      if(uret != 0){
719
 
        mc->dh_bits = uret;
 
647
      if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
 
648
        /* Err on the side of caution */
 
649
        sec_param = GNUTLS_SEC_PARAM_ULTRA;
720
650
        if(debug){
721
 
          fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
722
 
                       " implies %u DH bits; using that.\n",
 
651
          fprintf_plus(stderr, "Falling back to security parameter"
 
652
                       " \"%s\"\n",
723
653
                       safe_string(gnutls_sec_param_get_name
724
 
                                   (sec_param)),
725
 
                       mc->dh_bits);
 
654
                                   (sec_param)));
726
655
        }
727
 
      } else {
728
 
        fprintf_plus(stderr, "Failed to get implied number of DH"
729
 
                     " bits for security parameter \"%s\"): %s\n",
 
656
      }
 
657
    }
 
658
    uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
 
659
    if(uret != 0){
 
660
      mc->dh_bits = uret;
 
661
      if(debug){
 
662
        fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
 
663
                     " implies %u DH bits; using that.\n",
730
664
                     safe_string(gnutls_sec_param_get_name
731
665
                                 (sec_param)),
732
 
                     safer_gnutls_strerror(ret));
733
 
        goto globalfail;
 
666
                     mc->dh_bits);
734
667
      }
735
 
    } else if(debug){
736
 
      fprintf_plus(stderr, "DH bits explicitly set to %u\n",
737
 
                   mc->dh_bits);
738
 
    }
739
 
    ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
740
 
    if(ret != GNUTLS_E_SUCCESS){
741
 
      fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
742
 
                   " bits): %s\n", mc->dh_bits,
 
668
    } else {
 
669
      fprintf_plus(stderr, "Failed to get implied number of DH"
 
670
                   " bits for security parameter \"%s\"): %s\n",
 
671
                   safe_string(gnutls_sec_param_get_name(sec_param)),
743
672
                   safer_gnutls_strerror(ret));
744
673
      goto globalfail;
745
674
    }
746
 
  }
 
675
  } else if(debug){
 
676
    fprintf_plus(stderr, "DH bits explicitly set to %u\n",
 
677
                 mc->dh_bits);
 
678
  }
 
679
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
680
  if(ret != GNUTLS_E_SUCCESS){
 
681
    fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
 
682
                 " %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
 
683
    goto globalfail;
 
684
  }
 
685
  
747
686
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
748
687
  
749
688
  return 0;
751
690
 globalfail:
752
691
  
753
692
  gnutls_certificate_free_credentials(mc->cred);
 
693
  gnutls_global_deinit();
754
694
  gnutls_dh_params_deinit(mc->dh_params);
755
695
  return -1;
756
696
}
808
748
  /* ignore client certificate if any. */
809
749
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
810
750
  
 
751
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
 
752
  
811
753
  return 0;
812
754
}
813
755
 
926
868
      perror_plus("dup2(devnull, STDIN_FILENO)");
927
869
      _exit(EX_OSERR);
928
870
    }
929
 
    ret = close(devnull);
 
871
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
930
872
    if(ret == -1){
931
873
      perror_plus("close");
932
874
      _exit(EX_OSERR);
949
891
                                                   helper, O_RDONLY));
950
892
    if(helper_fd == -1){
951
893
      perror_plus("openat");
952
 
      close(helperdir_fd);
953
894
      _exit(EX_UNAVAILABLE);
954
895
    }
955
 
    close(helperdir_fd);
 
896
    TEMP_FAILURE_RETRY(close(helperdir_fd));
956
897
#ifdef __GNUC__
957
898
#pragma GCC diagnostic push
958
899
#pragma GCC diagnostic ignored "-Wcast-qual"
1126
1067
    goto mandos_end;
1127
1068
  }
1128
1069
  
 
1070
  memset(&to, 0, sizeof(to));
1129
1071
  if(af == AF_INET6){
1130
 
    struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to;
1131
 
    *to6 = (struct sockaddr_in6){ .sin6_family = (sa_family_t)af };
1132
 
    ret = inet_pton(af, ip, &to6->sin6_addr);
 
1072
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
1073
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
1133
1074
  } else {                      /* IPv4 */
1134
 
    struct sockaddr_in *to4 = (struct sockaddr_in *)&to;
1135
 
    *to4 = (struct sockaddr_in){ .sin_family = (sa_family_t)af };
1136
 
    ret = inet_pton(af, ip, &to4->sin_addr);
 
1075
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
1076
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
1137
1077
  }
1138
1078
  if(ret < 0 ){
1139
1079
    int e = errno;
1218
1158
                    sizeof(struct sockaddr_in));
1219
1159
    }
1220
1160
    if(ret < 0){
1221
 
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
 
1161
      if(errno == ENETUNREACH
1222
1162
         and if_index != AVAHI_IF_UNSPEC
1223
1163
         and connect_to == NULL
1224
1164
         and not route_added and
1462
1402
    free(decrypted_buffer);
1463
1403
    free(buffer);
1464
1404
    if(tcp_sd >= 0){
1465
 
      ret = close(tcp_sd);
 
1405
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
1466
1406
    }
1467
1407
    if(ret == -1){
1468
1408
      if(e == 0){
1632
1572
    errno = ret_errno;
1633
1573
    return false;
1634
1574
  }
1635
 
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1636
 
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
 
1575
  strcpy(ifr->ifr_name, ifname);
1637
1576
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1638
1577
  if(ret == -1){
1639
1578
    if(debug){
1909
1848
      return;
1910
1849
    }
1911
1850
  }
 
1851
#ifdef __GLIBC__
 
1852
#if __GLIBC_PREREQ(2, 15)
1912
1853
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1913
1854
                           runnable_hook, alphasort);
 
1855
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1856
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1857
                         alphasort);
 
1858
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1859
#else   /* not __GLIBC__ */
 
1860
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1861
                         alphasort);
 
1862
#endif  /* not __GLIBC__ */
1914
1863
  if(numhooks == -1){
1915
1864
    perror_plus("scandir");
1916
1865
    return;
1998
1947
        perror_plus("openat");
1999
1948
        _exit(EXIT_FAILURE);
2000
1949
      }
2001
 
      if(close(hookdir_fd) == -1){
 
1950
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
2002
1951
        perror_plus("close");
2003
1952
        _exit(EXIT_FAILURE);
2004
1953
      }
2007
1956
        perror_plus("dup2(devnull, STDIN_FILENO)");
2008
1957
        _exit(EX_OSERR);
2009
1958
      }
2010
 
      ret = close(devnull);
 
1959
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
2011
1960
      if(ret == -1){
2012
1961
        perror_plus("close");
2013
1962
        _exit(EX_OSERR);
2062
2011
    free(direntry);
2063
2012
  }
2064
2013
  free(direntries);
2065
 
  if(close(hookdir_fd) == -1){
 
2014
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
2066
2015
    perror_plus("close");
2067
2016
  } else {
2068
2017
    hookdir_fd = -1;
2108
2057
    }
2109
2058
    
2110
2059
    if(quit_now){
2111
 
      ret = close(sd);
 
2060
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2112
2061
      if(ret == -1){
2113
2062
        perror_plus("close");
2114
2063
      }
2164
2113
    }
2165
2114
    
2166
2115
    /* Close the socket */
2167
 
    ret = close(sd);
 
2116
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
2168
2117
    if(ret == -1){
2169
2118
      perror_plus("close");
2170
2119
    }
2252
2201
    }
2253
2202
    
2254
2203
    /* Close the socket */
2255
 
    int ret = close(sd);
 
2204
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
2256
2205
    if(ret == -1){
2257
2206
      perror_plus("close");
2258
2207
    }
2274
2223
 
2275
2224
int main(int argc, char *argv[]){
2276
2225
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2277
 
                        .priority = "SECURE256:!CTYPE-X.509"
2278
 
                        ":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
2279
 
                        .current_server = NULL, .interfaces = NULL,
2280
 
                        .interfaces_size = 0 };
 
2226
                        .priority = "SECURE256:!CTYPE-X.509:"
 
2227
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
 
2228
                        .interfaces = NULL, .interfaces_size = 0 };
2281
2229
  AvahiSServiceBrowser *sb = NULL;
2282
2230
  error_t ret_errno;
2283
2231
  int ret;
2292
2240
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
2293
2241
  const char *seckey = PATHDIR "/" SECKEY;
2294
2242
  const char *pubkey = PATHDIR "/" PUBKEY;
2295
 
  const char *dh_params_file = NULL;
2296
2243
  char *interfaces_hooks = NULL;
2297
2244
  
2298
2245
  bool gnutls_initialized = false;
2351
2298
        .doc = "Bit length of the prime number used in the"
2352
2299
        " Diffie-Hellman key exchange",
2353
2300
        .group = 2 },
2354
 
      { .name = "dh-params", .key = 134,
2355
 
        .arg = "FILE",
2356
 
        .doc = "PEM-encoded PKCS#3 file with pre-generated parameters"
2357
 
        " for the Diffie-Hellman key exchange",
2358
 
        .group = 2 },
2359
2301
      { .name = "priority", .key = 130,
2360
2302
        .arg = "STRING",
2361
2303
        .doc = "GnuTLS priority string for the TLS handshake",
2416
2358
        }
2417
2359
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
2418
2360
        break;
2419
 
      case 134:                 /* --dh-params */
2420
 
        dh_params_file = arg;
2421
 
        break;
2422
2361
      case 130:                 /* --priority */
2423
2362
        mc.priority = arg;
2424
2363
        break;
2510
2449
              }
2511
2450
            }
2512
2451
          }
2513
 
          close(seckey_fd);
 
2452
          TEMP_FAILURE_RETRY(close(seckey_fd));
2514
2453
        }
2515
2454
      }
2516
2455
      
2531
2470
              }
2532
2471
            }
2533
2472
          }
2534
 
          close(pubkey_fd);
2535
 
        }
2536
 
      }
2537
 
      
2538
 
      if(dh_params_file != NULL
2539
 
         and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
2540
 
        int dhparams_fd = open(dh_params_file, O_RDONLY);
2541
 
        if(dhparams_fd == -1){
2542
 
          perror_plus("open");
2543
 
        } else {
2544
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(dhparams_fd, &st));
2545
 
          if(ret == -1){
2546
 
            perror_plus("fstat");
2547
 
          } else {
2548
 
            if(S_ISREG(st.st_mode)
2549
 
               and st.st_uid == 0 and st.st_gid == 0){
2550
 
              ret = fchown(dhparams_fd, uid, gid);
2551
 
              if(ret == -1){
2552
 
                perror_plus("fchown");
2553
 
              }
2554
 
            }
2555
 
          }
2556
 
          close(dhparams_fd);
 
2473
          TEMP_FAILURE_RETRY(close(pubkey_fd));
2557
2474
        }
2558
2475
      }
2559
2476
      
2736
2653
      errno = bring_up_interface(interface, delay);
2737
2654
      if(not interface_was_up){
2738
2655
        if(errno != 0){
2739
 
          fprintf_plus(stderr, "Failed to bring up interface \"%s\":"
2740
 
                       " %s\n", interface, strerror(errno));
 
2656
          perror_plus("Failed to bring up interface");
2741
2657
        } else {
2742
2658
          errno = argz_add(&interfaces_to_take_down,
2743
2659
                           &interfaces_to_take_down_size,
2766
2682
    goto end;
2767
2683
  }
2768
2684
  
2769
 
  ret = init_gnutls_global(pubkey, seckey, dh_params_file, &mc);
 
2685
  ret = init_gnutls_global(pubkey, seckey, &mc);
2770
2686
  if(ret == -1){
2771
2687
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2772
2688
    exitcode = EX_UNAVAILABLE;
2961
2877
  
2962
2878
  if(gnutls_initialized){
2963
2879
    gnutls_certificate_free_credentials(mc.cred);
 
2880
    gnutls_global_deinit();
2964
2881
    gnutls_dh_params_deinit(mc.dh_params);
2965
2882
  }
2966
2883
  
3028
2945
  free(interfaces_to_take_down);
3029
2946
  free(interfaces_hooks);
3030
2947
  
3031
 
  void clean_dir_at(int base, const char * const dirname,
3032
 
                    uintmax_t level){
3033
 
    struct dirent **direntries = NULL;
3034
 
    int dret;
3035
 
    int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
3036
 
                                                O_RDONLY
3037
 
                                                | O_NOFOLLOW
3038
 
                                                | O_DIRECTORY
3039
 
                                                | O_PATH));
3040
 
    if(dir_fd == -1){
3041
 
      perror_plus("open");
3042
 
    }
3043
 
    int numentries = scandirat(dir_fd, ".", &direntries,
3044
 
                               notdotentries, alphasort);
3045
 
    if(numentries >= 0){
3046
 
      for(int i = 0; i < numentries; i++){
3047
 
        if(debug){
3048
 
          fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
3049
 
                       dirname, direntries[i]->d_name);
3050
 
        }
3051
 
        dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
3052
 
        if(dret == -1){
3053
 
          if(errno == EISDIR){
3054
 
              dret = unlinkat(dir_fd, direntries[i]->d_name,
3055
 
                              AT_REMOVEDIR);
3056
 
          }         
3057
 
          if((dret == -1) and (errno == ENOTEMPTY)
3058
 
             and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
3059
 
                  == 0) and (level == 0)){
3060
 
            /* Recurse only in this special case */
3061
 
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3062
 
            dret = 0;
3063
 
          }
3064
 
          if(dret == -1){
3065
 
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3066
 
                         direntries[i]->d_name, strerror(errno));
3067
 
          }
3068
 
        }
3069
 
        free(direntries[i]);
3070
 
      }
3071
 
      
3072
 
      /* need to clean even if 0 because man page doesn't specify */
3073
 
      free(direntries);
3074
 
      if(numentries == -1){
3075
 
        perror_plus("scandirat");
3076
 
      }
3077
 
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
3078
 
      if(dret == -1 and errno != ENOENT){
3079
 
        perror_plus("rmdir");
3080
 
      }
3081
 
    } else {
3082
 
      perror_plus("scandirat");
3083
 
    }
3084
 
    close(dir_fd);
3085
 
  }
3086
 
  
3087
2948
  /* Removes the GPGME temp directory and all files inside */
3088
2949
  if(tempdir != NULL){
3089
 
    clean_dir_at(-1, tempdir, 0);
 
2950
    struct dirent **direntries = NULL;
 
2951
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
 
2952
                                                  | O_NOFOLLOW
 
2953
                                                  | O_DIRECTORY
 
2954
                                                  | O_PATH));
 
2955
    if(tempdir_fd == -1){
 
2956
      perror_plus("open");
 
2957
    } else {
 
2958
#ifdef __GLIBC__
 
2959
#if __GLIBC_PREREQ(2, 15)
 
2960
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
2961
                                 notdotentries, alphasort);
 
2962
#else  /* not __GLIBC_PREREQ(2, 15) */
 
2963
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2964
                               alphasort);
 
2965
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
2966
#else   /* not __GLIBC__ */
 
2967
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2968
                               alphasort);
 
2969
#endif  /* not __GLIBC__ */
 
2970
      if(numentries >= 0){
 
2971
        for(int i = 0; i < numentries; i++){
 
2972
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2973
          if(ret == -1){
 
2974
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2975
                         " \"%s\", 0): %s\n", tempdir,
 
2976
                         direntries[i]->d_name, strerror(errno));
 
2977
          }
 
2978
          free(direntries[i]);
 
2979
        }
 
2980
        
 
2981
        /* need to clean even if 0 because man page doesn't specify */
 
2982
        free(direntries);
 
2983
        if(numentries == -1){
 
2984
          perror_plus("scandir");
 
2985
        }
 
2986
        ret = rmdir(tempdir);
 
2987
        if(ret == -1 and errno != ENOENT){
 
2988
          perror_plus("rmdir");
 
2989
        }
 
2990
      }
 
2991
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2992
    }
3090
2993
  }
3091
2994
  
3092
2995
  if(quit_now){