575
569
safer_gnutls_strerror(ret));
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 };
583
int secfile = open(seckeyfilename, O_RDONLY);
584
size_t buffer_capacity = 0;
586
buffer_capacity = incbuffer((char **)&buffer.data,
588
(size_t)buffer_capacity);
589
if(buffer_capacity == 0){
590
perror_plus("incbuffer");
595
ssize_t bytes_read = read(secfile, buffer.data + buffer.size,
601
/* check bytes_read for failure */
608
buffer.size += (unsigned int)bytes_read;
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){
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));
624
ret = gnutls_openpgp_privkey_import(privkey, &buffer,
625
GNUTLS_OPENPGP_FMT_BASE64,
627
if(ret != GNUTLS_E_SUCCESS){
628
fprintf_plus(stderr, "Error importing OpenPGP key : %s",
629
safer_gnutls_strerror(ret));
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);
639
fprintf_plus(stderr, "This OpenPGP key implies using a"
640
" GnuTLS security parameter \"%s\".\n",
641
safe_string(gnutls_sec_param_get_name
647
if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
648
/* Err on the side of caution */
649
sec_param = GNUTLS_SEC_PARAM_ULTRA;
651
fprintf_plus(stderr, "Falling back to security parameter"
653
safe_string(gnutls_sec_param_get_name
658
uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
662
fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
663
" implies %u DH bits; using that.\n",
664
safe_string(gnutls_sec_param_get_name
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)),
672
safer_gnutls_strerror(ret));
676
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
679
572
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
680
573
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));
574
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
575
safer_gnutls_strerror(ret));
809
702
return ret_errno;
812
/* Helper function to add_local_route() and delete_local_route() */
705
/* Helper function to add_local_route() and remove_local_route() */
813
706
__attribute__((nonnull, warn_unused_result))
814
static bool add_delete_local_route(const bool add,
707
static bool add_remove_local_route(const bool add,
815
708
const char *address,
816
709
AvahiIfIndex if_index){
818
711
char helper[] = "mandos-client-iprouteadddel";
819
712
char add_arg[] = "add";
820
char delete_arg[] = "delete";
821
char debug_flag[] = "--debug";
713
char remove_arg[] = "remove";
822
714
char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
823
715
if(pluginhelperdir == NULL){
886
if(helperdir_fd == -1){
888
_exit(EX_UNAVAILABLE);
890
778
int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
891
779
helper, O_RDONLY));
893
perror_plus("openat");
894
_exit(EX_UNAVAILABLE);
896
780
TEMP_FAILURE_RETRY(close(helperdir_fd));
898
782
#pragma GCC diagnostic push
899
783
#pragma GCC diagnostic ignored "-Wcast-qual"
901
785
if(fexecve(helper_fd, (char *const [])
902
{ helper, add ? add_arg : delete_arg, (char *)address,
903
interface, debug ? debug_flag : NULL, NULL },
786
{ helper, add ? add_arg : remove_arg, (char *)address,
787
interface, NULL }, environ) == -1){
906
789
#pragma GCC diagnostic pop
959
842
__attribute__((nonnull, warn_unused_result))
960
843
static bool add_local_route(const char *address,
961
844
AvahiIfIndex if_index){
963
fprintf_plus(stderr, "Adding route to %s\n", address);
965
return add_delete_local_route(true, address, if_index);
845
return add_remove_local_route(true, address, if_index);
968
848
__attribute__((nonnull, warn_unused_result))
969
static bool delete_local_route(const char *address,
849
static bool remove_local_route(const char *address,
970
850
AvahiIfIndex if_index){
972
fprintf_plus(stderr, "Removing route to %s\n", address);
974
return add_delete_local_route(false, address, if_index);
851
return add_remove_local_route(false, address, if_index);
977
854
/* Called when a Mandos server is found */
2224
2097
int main(int argc, char *argv[]){
2225
mandos_context mc = { .server = NULL, .dh_bits = 0,
2098
mandos_context mc = { .server = NULL, .dh_bits = 1024,
2226
2099
.priority = "SECURE256:!CTYPE-X.509:"
2227
"+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2100
"+CTYPE-OPENPGP", .current_server = NULL,
2228
2101
.interfaces = NULL, .interfaces_size = 0 };
2229
2102
AvahiSServiceBrowser *sb = NULL;
2230
2103
error_t ret_errno;