/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-03-02 17:35:23 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 335.
  • Revision ID: teddy@recompile.se-20160302173523-rdhzeatfxr582i2c
Use "nogroup" as a fallback group, not "nobody".

* Makefile (GROUP): Use "nogroup" as a fallback group, not "nobody".
* mandos (main): - '' -

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() */
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() */
 
60
#include <fcntl.h>              /* open(), unlinkat(), AT_REMOVEDIR */
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,
 
65
#include <errno.h>              /* perror(), errno, EINTR, EINVAL,
 
66
                                   EAI_SYSTEM, ENETUNREACH,
 
67
                                   EHOSTUNREACH, ECONNREFUSED, EPROTO,
 
68
                                   EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
 
69
                                   ENOTEMPTY,
66
70
                                   program_invocation_short_name */
67
71
#include <time.h>               /* nanosleep(), time(), sleep() */
68
72
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
305
309
      return false;
306
310
    }
307
311
    
308
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
312
    ret = close(fd);
309
313
    if(ret == -1){
310
314
      perror_plus("close");
311
315
    }
493
497
  return plaintext_length;
494
498
}
495
499
 
 
500
__attribute__((warn_unused_result, const))
 
501
static const char *safe_string(const char *str){
 
502
  if(str == NULL)
 
503
    return "(unknown)";
 
504
  return str;
 
505
}
 
506
 
496
507
__attribute__((warn_unused_result))
497
508
static const char *safer_gnutls_strerror(int value){
498
509
  const char *ret = gnutls_strerror(value);
499
 
  if(ret == NULL)
500
 
    ret = "(unknown)";
501
 
  return ret;
 
510
  return safe_string(ret);
502
511
}
503
512
 
504
513
/* GnuTLS log function callback */
508
517
  fprintf_plus(stderr, "GnuTLS: %s", string);
509
518
}
510
519
 
511
 
__attribute__((nonnull, warn_unused_result))
 
520
__attribute__((nonnull(1, 2, 4), warn_unused_result))
512
521
static int init_gnutls_global(const char *pubkeyfilename,
513
522
                              const char *seckeyfilename,
 
523
                              const char *dhparamsfilename,
514
524
                              mandos_context *mc){
515
525
  int ret;
 
526
  unsigned int uret;
516
527
  
517
528
  if(debug){
518
529
    fprintf_plus(stderr, "Initializing GnuTLS\n");
519
530
  }
520
531
  
521
 
  ret = gnutls_global_init();
522
 
  if(ret != GNUTLS_E_SUCCESS){
523
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
524
 
                 safer_gnutls_strerror(ret));
525
 
    return -1;
526
 
  }
527
 
  
528
532
  if(debug){
529
533
    /* "Use a log level over 10 to enable all debugging options."
530
534
     * - GnuTLS manual
538
542
  if(ret != GNUTLS_E_SUCCESS){
539
543
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
540
544
                 safer_gnutls_strerror(ret));
541
 
    gnutls_global_deinit();
542
545
    return -1;
543
546
  }
544
547
  
569
572
                 safer_gnutls_strerror(ret));
570
573
    goto globalfail;
571
574
  }
572
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
573
 
  if(ret != GNUTLS_E_SUCCESS){
574
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
575
 
                 safer_gnutls_strerror(ret));
576
 
    goto globalfail;
577
 
  }
578
 
  
 
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;
 
586
      while(true){
 
587
        params_capacity = incbuffer((char **)&params.data,
 
588
                                    (size_t)params.size,
 
589
                                    (size_t)params_capacity);
 
590
        if(params_capacity == 0){
 
591
          perror_plus("incbuffer");
 
592
          free(params.data);
 
593
          params.data = NULL;
 
594
          dhparamsfilename = NULL;
 
595
          break;
 
596
        }
 
597
        ssize_t bytes_read = read(dhpfile,
 
598
                                  params.data + params.size,
 
599
                                  BUFFER_SIZE);
 
600
        /* EOF */
 
601
        if(bytes_read == 0){
 
602
          break;
 
603
        }
 
604
        /* check bytes_read for failure */
 
605
        if(bytes_read < 0){
 
606
          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);
 
676
          if(ret != GNUTLS_E_SUCCESS){
 
677
            fprintf_plus(stderr, "Error initializing OpenPGP key"
 
678
                         " structure: %s",
 
679
                         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
            }
 
704
          }
 
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)));
 
714
          }
 
715
        }
 
716
      }
 
717
      uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
 
718
      if(uret != 0){
 
719
        mc->dh_bits = uret;
 
720
        if(debug){
 
721
          fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
 
722
                       " implies %u DH bits; using that.\n",
 
723
                       safe_string(gnutls_sec_param_get_name
 
724
                                   (sec_param)),
 
725
                       mc->dh_bits);
 
726
        }
 
727
      } else {
 
728
        fprintf_plus(stderr, "Failed to get implied number of DH"
 
729
                     " bits for security parameter \"%s\"): %s\n",
 
730
                     safe_string(gnutls_sec_param_get_name
 
731
                                 (sec_param)),
 
732
                     safer_gnutls_strerror(ret));
 
733
        goto globalfail;
 
734
      }
 
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,
 
743
                   safer_gnutls_strerror(ret));
 
744
      goto globalfail;
 
745
    }
 
746
  }
579
747
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
580
748
  
581
749
  return 0;
583
751
 globalfail:
584
752
  
585
753
  gnutls_certificate_free_credentials(mc->cred);
586
 
  gnutls_global_deinit();
587
754
  gnutls_dh_params_deinit(mc->dh_params);
588
755
  return -1;
589
756
}
641
808
  /* ignore client certificate if any. */
642
809
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
643
810
  
644
 
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
645
 
  
646
811
  return 0;
647
812
}
648
813
 
702
867
  return ret_errno;
703
868
}
704
869
 
705
 
/* Helper function to add_local_route() and remove_local_route() */
 
870
/* Helper function to add_local_route() and delete_local_route() */
706
871
__attribute__((nonnull, warn_unused_result))
707
 
static bool add_remove_local_route(const bool add,
 
872
static bool add_delete_local_route(const bool add,
708
873
                                   const char *address,
709
874
                                   AvahiIfIndex if_index){
710
875
  int ret;
711
876
  char helper[] = "mandos-client-iprouteadddel";
712
877
  char add_arg[] = "add";
713
878
  char delete_arg[] = "delete";
 
879
  char debug_flag[] = "--debug";
714
880
  char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
715
881
  if(pluginhelperdir == NULL){
716
882
    if(debug){
760
926
      perror_plus("dup2(devnull, STDIN_FILENO)");
761
927
      _exit(EX_OSERR);
762
928
    }
763
 
    ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
929
    ret = close(devnull);
764
930
    if(ret == -1){
765
931
      perror_plus("close");
766
932
      _exit(EX_OSERR);
783
949
                                                   helper, O_RDONLY));
784
950
    if(helper_fd == -1){
785
951
      perror_plus("openat");
 
952
      close(helperdir_fd);
786
953
      _exit(EX_UNAVAILABLE);
787
954
    }
788
 
    TEMP_FAILURE_RETRY(close(helperdir_fd));
 
955
    close(helperdir_fd);
789
956
#ifdef __GNUC__
790
957
#pragma GCC diagnostic push
791
958
#pragma GCC diagnostic ignored "-Wcast-qual"
792
959
#endif
793
960
    if(fexecve(helper_fd, (char *const [])
794
961
               { helper, add ? add_arg : delete_arg, (char *)address,
795
 
                   interface, NULL }, environ) == -1){
 
962
                   interface, debug ? debug_flag : NULL, NULL },
 
963
               environ) == -1){
796
964
#ifdef __GNUC__
797
965
#pragma GCC diagnostic pop
798
966
#endif
850
1018
__attribute__((nonnull, warn_unused_result))
851
1019
static bool add_local_route(const char *address,
852
1020
                            AvahiIfIndex if_index){
853
 
  return add_remove_local_route(true, address, if_index);
 
1021
  if(debug){
 
1022
    fprintf_plus(stderr, "Adding route to %s\n", address);
 
1023
  }
 
1024
  return add_delete_local_route(true, address, if_index);
854
1025
}
855
1026
 
856
1027
__attribute__((nonnull, warn_unused_result))
857
 
static bool remove_local_route(const char *address,
 
1028
static bool delete_local_route(const char *address,
858
1029
                               AvahiIfIndex if_index){
859
 
  return add_remove_local_route(false, address, if_index);
 
1030
  if(debug){
 
1031
    fprintf_plus(stderr, "Removing route to %s\n", address);
 
1032
  }
 
1033
  return add_delete_local_route(false, address, if_index);
860
1034
}
861
1035
 
862
1036
/* Called when a Mandos server is found */
952
1126
    goto mandos_end;
953
1127
  }
954
1128
  
955
 
  memset(&to, 0, sizeof(to));
956
1129
  if(af == AF_INET6){
957
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
958
 
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
 
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);
959
1133
  } else {                      /* IPv4 */
960
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
961
 
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
 
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);
962
1137
  }
963
1138
  if(ret < 0 ){
964
1139
    int e = errno;
1043
1218
                    sizeof(struct sockaddr_in));
1044
1219
    }
1045
1220
    if(ret < 0){
1046
 
      if(errno == ENETUNREACH
 
1221
      if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1047
1222
         and if_index != AVAHI_IF_UNSPEC
1048
1223
         and connect_to == NULL
1049
1224
         and not route_added and
1065
1240
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1066
1241
           https://bugs.debian.org/587961
1067
1242
        */
 
1243
        if(debug){
 
1244
          fprintf_plus(stderr, "Mandos server unreachable, trying"
 
1245
                       " direct route\n");
 
1246
        }
1068
1247
        int e = errno;
1069
1248
        route_added = add_local_route(ip, if_index);
1070
1249
        if(route_added){
1274
1453
 mandos_end:
1275
1454
  {
1276
1455
    if(route_added){
1277
 
      if(not remove_local_route(ip, if_index)){
1278
 
        fprintf_plus(stderr, "Failed to remove local route to %s on"
 
1456
      if(not delete_local_route(ip, if_index)){
 
1457
        fprintf_plus(stderr, "Failed to delete local route to %s on"
1279
1458
                     " interface %d", ip, if_index);
1280
1459
      }
1281
1460
    }
1283
1462
    free(decrypted_buffer);
1284
1463
    free(buffer);
1285
1464
    if(tcp_sd >= 0){
1286
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
1465
      ret = close(tcp_sd);
1287
1466
    }
1288
1467
    if(ret == -1){
1289
1468
      if(e == 0){
1453
1632
    errno = ret_errno;
1454
1633
    return false;
1455
1634
  }
1456
 
  strcpy(ifr->ifr_name, ifname);
 
1635
  strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
 
1636
  ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1457
1637
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1458
1638
  if(ret == -1){
1459
1639
    if(debug){
1729
1909
      return;
1730
1910
    }
1731
1911
  }
1732
 
#ifdef __GLIBC__
1733
 
#if __GLIBC_PREREQ(2, 15)
1734
1912
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1735
1913
                           runnable_hook, alphasort);
1736
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1737
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1738
 
                         alphasort);
1739
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1740
 
#else   /* not __GLIBC__ */
1741
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1742
 
                         alphasort);
1743
 
#endif  /* not __GLIBC__ */
1744
1914
  if(numhooks == -1){
1745
1915
    perror_plus("scandir");
1746
1916
    return;
1828
1998
        perror_plus("openat");
1829
1999
        _exit(EXIT_FAILURE);
1830
2000
      }
1831
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2001
      if(close(hookdir_fd) == -1){
1832
2002
        perror_plus("close");
1833
2003
        _exit(EXIT_FAILURE);
1834
2004
      }
1837
2007
        perror_plus("dup2(devnull, STDIN_FILENO)");
1838
2008
        _exit(EX_OSERR);
1839
2009
      }
1840
 
      ret = (int)TEMP_FAILURE_RETRY(close(devnull));
 
2010
      ret = close(devnull);
1841
2011
      if(ret == -1){
1842
2012
        perror_plus("close");
1843
2013
        _exit(EX_OSERR);
1892
2062
    free(direntry);
1893
2063
  }
1894
2064
  free(direntries);
1895
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
2065
  if(close(hookdir_fd) == -1){
1896
2066
    perror_plus("close");
1897
2067
  } else {
1898
2068
    hookdir_fd = -1;
1938
2108
    }
1939
2109
    
1940
2110
    if(quit_now){
1941
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2111
      ret = close(sd);
1942
2112
      if(ret == -1){
1943
2113
        perror_plus("close");
1944
2114
      }
1994
2164
    }
1995
2165
    
1996
2166
    /* Close the socket */
1997
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2167
    ret = close(sd);
1998
2168
    if(ret == -1){
1999
2169
      perror_plus("close");
2000
2170
    }
2082
2252
    }
2083
2253
    
2084
2254
    /* Close the socket */
2085
 
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2255
    int ret = close(sd);
2086
2256
    if(ret == -1){
2087
2257
      perror_plus("close");
2088
2258
    }
2103
2273
}
2104
2274
 
2105
2275
int main(int argc, char *argv[]){
2106
 
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
2107
 
                        .priority = "SECURE256:!CTYPE-X.509:"
2108
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
2109
 
                        .interfaces = NULL, .interfaces_size = 0 };
 
2276
  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 };
2110
2281
  AvahiSServiceBrowser *sb = NULL;
2111
2282
  error_t ret_errno;
2112
2283
  int ret;
2121
2292
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
2122
2293
  const char *seckey = PATHDIR "/" SECKEY;
2123
2294
  const char *pubkey = PATHDIR "/" PUBKEY;
 
2295
  const char *dh_params_file = NULL;
2124
2296
  char *interfaces_hooks = NULL;
2125
2297
  
2126
2298
  bool gnutls_initialized = false;
2179
2351
        .doc = "Bit length of the prime number used in the"
2180
2352
        " Diffie-Hellman key exchange",
2181
2353
        .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 },
2182
2359
      { .name = "priority", .key = 130,
2183
2360
        .arg = "STRING",
2184
2361
        .doc = "GnuTLS priority string for the TLS handshake",
2239
2416
        }
2240
2417
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
2241
2418
        break;
 
2419
      case 134:                 /* --dh-params */
 
2420
        dh_params_file = arg;
 
2421
        break;
2242
2422
      case 130:                 /* --priority */
2243
2423
        mc.priority = arg;
2244
2424
        break;
2330
2510
              }
2331
2511
            }
2332
2512
          }
2333
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
2513
          close(seckey_fd);
2334
2514
        }
2335
2515
      }
2336
2516
      
2351
2531
              }
2352
2532
            }
2353
2533
          }
2354
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
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);
2355
2557
        }
2356
2558
      }
2357
2559
      
2534
2736
      errno = bring_up_interface(interface, delay);
2535
2737
      if(not interface_was_up){
2536
2738
        if(errno != 0){
2537
 
          perror_plus("Failed to bring up interface");
 
2739
          fprintf_plus(stderr, "Failed to bring up interface \"%s\":"
 
2740
                       " %s\n", interface, strerror(errno));
2538
2741
        } else {
2539
2742
          errno = argz_add(&interfaces_to_take_down,
2540
2743
                           &interfaces_to_take_down_size,
2563
2766
    goto end;
2564
2767
  }
2565
2768
  
2566
 
  ret = init_gnutls_global(pubkey, seckey, &mc);
 
2769
  ret = init_gnutls_global(pubkey, seckey, dh_params_file, &mc);
2567
2770
  if(ret == -1){
2568
2771
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2569
2772
    exitcode = EX_UNAVAILABLE;
2758
2961
  
2759
2962
  if(gnutls_initialized){
2760
2963
    gnutls_certificate_free_credentials(mc.cred);
2761
 
    gnutls_global_deinit();
2762
2964
    gnutls_dh_params_deinit(mc.dh_params);
2763
2965
  }
2764
2966
  
2826
3028
  free(interfaces_to_take_down);
2827
3029
  free(interfaces_hooks);
2828
3030
  
 
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
  
2829
3087
  /* Removes the GPGME temp directory and all files inside */
2830
3088
  if(tempdir != NULL){
2831
 
    struct dirent **direntries = NULL;
2832
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
2833
 
                                                  | O_NOFOLLOW
2834
 
                                                  | O_DIRECTORY
2835
 
                                                  | O_PATH));
2836
 
    if(tempdir_fd == -1){
2837
 
      perror_plus("open");
2838
 
    } else {
2839
 
#ifdef __GLIBC__
2840
 
#if __GLIBC_PREREQ(2, 15)
2841
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2842
 
                                 notdotentries, alphasort);
2843
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2844
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2845
 
                               alphasort);
2846
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2847
 
#else   /* not __GLIBC__ */
2848
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2849
 
                               alphasort);
2850
 
#endif  /* not __GLIBC__ */
2851
 
      if(numentries >= 0){
2852
 
        for(int i = 0; i < numentries; i++){
2853
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2854
 
          if(ret == -1){
2855
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2856
 
                         " \"%s\", 0): %s\n", tempdir,
2857
 
                         direntries[i]->d_name, strerror(errno));
2858
 
          }
2859
 
          free(direntries[i]);
2860
 
        }
2861
 
        
2862
 
        /* need to clean even if 0 because man page doesn't specify */
2863
 
        free(direntries);
2864
 
        if(numentries == -1){
2865
 
          perror_plus("scandir");
2866
 
        }
2867
 
        ret = rmdir(tempdir);
2868
 
        if(ret == -1 and errno != ENOENT){
2869
 
          perror_plus("rmdir");
2870
 
        }
2871
 
      }
2872
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2873
 
    }
 
3089
    clean_dir_at(-1, tempdir, 0);
2874
3090
  }
2875
3091
  
2876
3092
  if(quit_now){