/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-06 20:29:34 UTC
  • mfrom: (738.1.6 route-external)
  • Revision ID: teddy@recompile.se-20150706202934-09j06jm2fiw1bn80
Merge change to add local route when network is "unreachable".

Show diffs side-by-side

added added

removed removed

Lines of Context:
493
493
  return plaintext_length;
494
494
}
495
495
 
 
496
__attribute__((warn_unused_result, const))
 
497
static const char *safe_string(const char *str){
 
498
  if(str == NULL)
 
499
    return "(unknown)";
 
500
  return str;
 
501
}
 
502
 
496
503
__attribute__((warn_unused_result))
497
504
static const char *safer_gnutls_strerror(int value){
498
505
  const char *ret = gnutls_strerror(value);
499
 
  if(ret == NULL)
500
 
    ret = "(unknown)";
501
 
  return ret;
 
506
  return safe_string(ret);
502
507
}
503
508
 
504
509
/* GnuTLS log function callback */
513
518
                              const char *seckeyfilename,
514
519
                              mandos_context *mc){
515
520
  int ret;
 
521
  unsigned int uret;
516
522
  
517
523
  if(debug){
518
524
    fprintf_plus(stderr, "Initializing GnuTLS\n");
569
575
                 safer_gnutls_strerror(ret));
570
576
    goto globalfail;
571
577
  }
 
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;
 
585
      while(true){
 
586
        buffer_capacity = incbuffer((char **)&buffer.data,
 
587
                                    (size_t)buffer.size,
 
588
                                    (size_t)buffer_capacity);
 
589
        if(buffer_capacity == 0){
 
590
          perror_plus("incbuffer");
 
591
          free(buffer.data);
 
592
          buffer.data = NULL;
 
593
          break;
 
594
        }
 
595
        ssize_t bytes_read = read(secfile, buffer.data + buffer.size,
 
596
                                  BUFFER_SIZE);
 
597
        /* EOF */
 
598
        if(bytes_read == 0){
 
599
          break;
 
600
        }
 
601
        /* check bytes_read for failure */
 
602
        if(bytes_read < 0){
 
603
          perror_plus("read");
 
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);
 
627
          if(ret != GNUTLS_E_SUCCESS){
 
628
            fprintf_plus(stderr, "Error importing OpenPGP key : %s",
 
629
                         safer_gnutls_strerror(ret));
 
630
            privkey = NULL;
 
631
          }
 
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
            }
 
644
          }
 
645
        }
 
646
      }
 
647
      if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
 
648
        /* Err on the side of caution */
 
649
        sec_param = GNUTLS_SEC_PARAM_ULTRA;
 
650
        if(debug){
 
651
          fprintf_plus(stderr, "Falling back to security parameter"
 
652
                       " \"%s\"\n",
 
653
                       safe_string(gnutls_sec_param_get_name
 
654
                                   (sec_param)));
 
655
        }
 
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",
 
664
                     safe_string(gnutls_sec_param_get_name
 
665
                                 (sec_param)),
 
666
                     mc->dh_bits);
 
667
      }
 
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)),
 
672
                   safer_gnutls_strerror(ret));
 
673
      goto globalfail;
 
674
    }
 
675
  } else if(debug){
 
676
    fprintf_plus(stderr, "DH bits explicitly set to %u\n",
 
677
                 mc->dh_bits);
 
678
  }
572
679
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
573
680
  if(ret != GNUTLS_E_SUCCESS){
574
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
575
 
                 safer_gnutls_strerror(ret));
 
681
    fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
 
682
                 " %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
576
683
    goto globalfail;
577
684
  }
578
685
  
702
809
  return ret_errno;
703
810
}
704
811
 
705
 
/* Helper function to add_local_route() and remove_local_route() */
 
812
/* Helper function to add_local_route() and delete_local_route() */
706
813
__attribute__((nonnull, warn_unused_result))
707
 
static bool add_remove_local_route(const bool add,
 
814
static bool add_delete_local_route(const bool add,
708
815
                                   const char *address,
709
816
                                   AvahiIfIndex if_index){
710
817
  int ret;
711
818
  char helper[] = "mandos-client-iprouteadddel";
712
819
  char add_arg[] = "add";
713
820
  char delete_arg[] = "delete";
 
821
  char debug_flag[] = "--debug";
714
822
  char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
715
823
  if(pluginhelperdir == NULL){
716
824
    if(debug){
792
900
#endif
793
901
    if(fexecve(helper_fd, (char *const [])
794
902
               { helper, add ? add_arg : delete_arg, (char *)address,
795
 
                   interface, NULL }, environ) == -1){
 
903
                   interface, debug ? debug_flag : NULL, NULL },
 
904
               environ) == -1){
796
905
#ifdef __GNUC__
797
906
#pragma GCC diagnostic pop
798
907
#endif
850
959
__attribute__((nonnull, warn_unused_result))
851
960
static bool add_local_route(const char *address,
852
961
                            AvahiIfIndex if_index){
853
 
  return add_remove_local_route(true, address, if_index);
 
962
  if(debug){
 
963
    fprintf_plus(stderr, "Adding route to %s\n", address);
 
964
  }
 
965
  return add_delete_local_route(true, address, if_index);
854
966
}
855
967
 
856
968
__attribute__((nonnull, warn_unused_result))
857
 
static bool remove_local_route(const char *address,
 
969
static bool delete_local_route(const char *address,
858
970
                               AvahiIfIndex if_index){
859
 
  return add_remove_local_route(false, address, if_index);
 
971
  if(debug){
 
972
    fprintf_plus(stderr, "Removing route to %s\n", address);
 
973
  }
 
974
  return add_delete_local_route(false, address, if_index);
860
975
}
861
976
 
862
977
/* Called when a Mandos server is found */
1065
1180
           http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1066
1181
           https://bugs.debian.org/587961
1067
1182
        */
 
1183
        if(debug){
 
1184
          fprintf_plus(stderr, "Mandos server unreachable, trying"
 
1185
                       " direct route\n");
 
1186
        }
1068
1187
        int e = errno;
1069
1188
        route_added = add_local_route(ip, if_index);
1070
1189
        if(route_added){
1274
1393
 mandos_end:
1275
1394
  {
1276
1395
    if(route_added){
1277
 
      if(not remove_local_route(ip, if_index)){
1278
 
        fprintf_plus(stderr, "Failed to remove local route to %s on"
 
1396
      if(not delete_local_route(ip, if_index)){
 
1397
        fprintf_plus(stderr, "Failed to delete local route to %s on"
1279
1398
                     " interface %d", ip, if_index);
1280
1399
      }
1281
1400
    }
2103
2222
}
2104
2223
 
2105
2224
int main(int argc, char *argv[]){
2106
 
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
 
2225
  mandos_context mc = { .server = NULL, .dh_bits = 0,
2107
2226
                        .priority = "SECURE256:!CTYPE-X.509:"
2108
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
2227
                        "+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2109
2228
                        .interfaces = NULL, .interfaces_size = 0 };
2110
2229
  AvahiSServiceBrowser *sb = NULL;
2111
2230
  error_t ret_errno;