284
297
if ((ret = gnutls_global_init ())
285
298
!= GNUTLS_E_SUCCESS) {
286
fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
299
fprintf (stderr, "GnuTLS global_init: %s\n",
300
safer_gnutls_strerror(ret));
305
/* "Use a log level over 10 to enable all debugging options."
291
308
gnutls_global_set_log_level(11);
292
309
gnutls_global_set_log_function(debuggnutls);
295
/* openpgp credentials */
312
/* OpenPGP credentials */
296
313
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
297
314
!= GNUTLS_E_SUCCESS) {
298
fprintf (stderr, "memory error: %s\n",
315
fprintf (stderr, "GnuTLS memory error: %s\n",
299
316
safer_gnutls_strerror(ret));
309
326
ret = gnutls_certificate_set_openpgp_key_file
310
327
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
311
328
if (ret != GNUTLS_E_SUCCESS) {
313
(stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
315
ret, pubkeyfile, seckeyfile);
316
fprintf(stdout, "The Error is: %s\n",
330
"Error[%d] while reading the OpenPGP key pair ('%s',"
331
" '%s')\n", ret, pubkeyfile, seckeyfile);
332
fprintf(stdout, "The GnuTLS error is: %s\n",
317
333
safer_gnutls_strerror(ret));
321
//GnuTLS server initialization
322
if ((ret = gnutls_dh_params_init(dh_params))
323
!= GNUTLS_E_SUCCESS) {
324
fprintf (stderr, "Error in dh parameter initialization: %s\n",
325
safer_gnutls_strerror(ret));
329
if ((ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits))
330
!= GNUTLS_E_SUCCESS) {
331
fprintf (stderr, "Error in prime generation: %s\n",
332
safer_gnutls_strerror(ret));
336
gnutls_certificate_set_dh_params(mc->cred, *dh_params);
338
// GnuTLS session creation
339
if ((ret = gnutls_init(session, GNUTLS_SERVER))
340
!= GNUTLS_E_SUCCESS){
337
/* GnuTLS server initialization */
338
ret = gnutls_dh_params_init(&mc->dh_params);
339
if (ret != GNUTLS_E_SUCCESS) {
340
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
341
" %s\n", safer_gnutls_strerror(ret));
344
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
345
if (ret != GNUTLS_E_SUCCESS) {
346
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
347
safer_gnutls_strerror(ret));
351
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
356
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
358
/* GnuTLS session creation */
359
ret = gnutls_init(session, GNUTLS_SERVER);
360
if (ret != GNUTLS_E_SUCCESS){
341
361
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
342
362
safer_gnutls_strerror(ret));
345
if ((ret = gnutls_priority_set_direct(*session, mc->priority, &err))
346
!= GNUTLS_E_SUCCESS) {
347
fprintf(stderr, "Syntax error at: %s\n", err);
348
fprintf(stderr, "GnuTLS error: %s\n",
349
safer_gnutls_strerror(ret));
367
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
368
if (ret != GNUTLS_E_SUCCESS) {
369
fprintf(stderr, "Syntax error at: %s\n", err);
370
fprintf(stderr, "GnuTLS error: %s\n",
371
safer_gnutls_strerror(ret));
353
if ((ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
355
!= GNUTLS_E_SUCCESS) {
356
fprintf(stderr, "Error setting a credentials set: %s\n",
376
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
378
if (ret != GNUTLS_E_SUCCESS) {
379
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
357
380
safer_gnutls_strerror(ret));
438
470
perror("connect");
442
ret = initgnutls (mc, &session, &dh_params);
474
const char *out = mandos_protocol_version;
477
size_t out_size = strlen(out);
478
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
479
out_size - written));
485
written += (size_t)ret;
486
if(written < out_size){
489
if (out == mandos_protocol_version){
499
fprintf(stderr, "Establishing TLS session with %s\n", ip);
448
502
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
451
fprintf(stderr, "Establishing TLS session with %s\n", ip);
454
504
ret = gnutls_handshake (session);
456
506
if (ret != GNUTLS_E_SUCCESS){
458
fprintf(stderr, "\n*** Handshake failed ***\n");
508
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
459
509
gnutls_perror (ret);
465
//Retrieve OpenPGP packet that contains the wanted password
515
/* Read OpenPGP packet that contains the wanted password */
468
518
fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
492
540
case GNUTLS_E_REHANDSHAKE:
493
541
ret = gnutls_handshake (session);
495
fprintf(stderr, "\n*** Handshake failed ***\n");
543
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
496
544
gnutls_perror (ret);
502
550
fprintf(stderr, "Unknown error while reading data from"
503
" encrypted session with mandos server\n");
551
" encrypted session with Mandos server\n");
505
553
gnutls_bye (session, GNUTLS_SHUT_RDWR);
509
557
buffer_length += (size_t) ret;
562
fprintf(stderr, "Closing TLS session\n");
565
gnutls_bye (session, GNUTLS_SHUT_RDWR);
513
567
if (buffer_length > 0){
514
568
decrypted_buffer_size = pgp_packet_decrypt(buffer,
516
570
&decrypted_buffer,
518
572
if (decrypted_buffer_size >= 0){
519
574
while(written < (size_t) decrypted_buffer_size){
520
575
ret = (int)fwrite (decrypted_buffer + written, 1,
521
576
(size_t)decrypted_buffer_size - written,
618
668
case AVAHI_BROWSER_FAILURE:
620
fprintf(stderr, "(Browser) %s\n",
670
fprintf(stderr, "(Avahi browser) %s\n",
621
671
avahi_strerror(avahi_server_errno(mc->server)));
622
672
avahi_simple_poll_quit(mc->simple_poll);
625
675
case AVAHI_BROWSER_NEW:
626
/* We ignore the returned resolver object. In the callback
627
function we free it. If the server is terminated before
628
the callback function is called the server will free
629
the resolver for us. */
676
/* We ignore the returned Avahi resolver object. In the callback
677
function we free it. If the Avahi server is terminated before
678
the callback function is called the Avahi server will free the
631
681
if (!(avahi_s_service_resolver_new(mc->server, interface,
632
682
protocol, name, type, domain,
633
683
AVAHI_PROTO_INET6, 0,
634
684
resolve_callback, mc)))
635
fprintf(stderr, "Failed to resolve service '%s': %s\n", name,
636
avahi_strerror(avahi_server_errno(mc->server)));
685
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
686
name, avahi_strerror(avahi_server_errno(mc->server)));
639
689
case AVAHI_BROWSER_REMOVE:
669
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
670
AvahiServerConfig config;
722
int main(int argc, char *argv[]){
671
723
AvahiSServiceBrowser *sb = NULL;
675
int returncode = EXIT_SUCCESS;
726
int exitcode = EXIT_SUCCESS;
676
727
const char *interface = "eth0";
677
728
struct ifreq network;
679
732
char *connect_to = NULL;
680
733
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
681
734
mandos_context mc = { .simple_poll = NULL, .server = NULL,
682
735
.dh_bits = 1024, .priority = "SECURE256"};
684
debug_int = debug ? 1 : 0;
686
struct option long_options[] = {
687
{"debug", no_argument, &debug_int, 1},
688
{"connect", required_argument, NULL, 'c'},
689
{"interface", required_argument, NULL, 'i'},
690
{"keydir", required_argument, NULL, 'd'},
691
{"seckey", required_argument, NULL, 's'},
692
{"pubkey", required_argument, NULL, 'p'},
693
{"dh-bits", required_argument, NULL, 'D'},
694
{"priority", required_argument, NULL, 'P'},
697
int option_index = 0;
698
ret = getopt_long (argc, argv, "i:", long_options,
725
mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
738
/* Temporary int to get the address of for getopt_long */
739
int debug_int = debug ? 1 : 0;
741
struct option long_options[] = {
742
{"debug", no_argument, &debug_int, 1},
743
{"connect", required_argument, NULL, 'c'},
744
{"interface", required_argument, NULL, 'i'},
745
{"keydir", required_argument, NULL, 'd'},
746
{"seckey", required_argument, NULL, 's'},
747
{"pubkey", required_argument, NULL, 'p'},
748
{"dh-bits", required_argument, NULL, 'D'},
749
{"priority", required_argument, NULL, 'P'},
752
int option_index = 0;
753
ret = getopt_long (argc, argv, "i:", long_options,
780
mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
787
mc.priority = optarg;
791
/* getopt_long() has already printed a message about the
792
unrcognized option, so just exit. */
728
793
exit(EXIT_FAILURE);
732
mc.priority = optarg;
796
/* Set the global debug flag from the temporary int */
797
debug = debug_int ? true : false;
739
debug = debug_int ? true : false;
741
800
pubkeyfile = combinepath(keydir, pubkeyfile);
742
801
if (pubkeyfile == NULL){
743
802
perror("combinepath");
744
returncode = EXIT_FAILURE;
803
exitcode = EXIT_FAILURE;
748
807
seckeyfile = combinepath(keydir, seckeyfile);
749
808
if (seckeyfile == NULL){
750
809
perror("combinepath");
813
ret = init_gnutls_global(&mc);
815
fprintf(stderr, "init_gnutls_global\n");
754
832
if_index = (AvahiIfIndex) if_nametoindex(interface);
763
841
char *address = strrchr(connect_to, ':');
764
842
if(address == NULL){
765
843
fprintf(stderr, "No colon in address\n");
844
exitcode = EXIT_FAILURE;
769
848
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
771
850
perror("Bad port number");
851
exitcode = EXIT_FAILURE;
775
855
address = connect_to;
776
856
ret = start_mandos_communication(address, port, if_index, &mc);
858
exitcode = EXIT_FAILURE;
860
exitcode = EXIT_SUCCESS;
784
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
787
returncode = EXIT_FAILURE;
790
strcpy(network.ifr_name, interface); /* Spurious warning */
791
ret = ioctl(sd, SIOCGIFFLAGS, &network);
794
perror("ioctl SIOCGIFFLAGS");
795
returncode = EXIT_FAILURE;
798
if((network.ifr_flags & IFF_UP) == 0){
799
network.ifr_flags |= IFF_UP;
800
ret = ioctl(sd, SIOCSIFFLAGS, &network);
865
/* If the interface is down, bring it up */
867
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
870
exitcode = EXIT_FAILURE;
873
strcpy(network.ifr_name, interface); /* Spurious warning */
874
ret = ioctl(sd, SIOCGIFFLAGS, &network);
802
perror("ioctl SIOCSIFFLAGS");
803
returncode = EXIT_FAILURE;
876
perror("ioctl SIOCGIFFLAGS");
877
exitcode = EXIT_FAILURE;
880
if((network.ifr_flags & IFF_UP) == 0){
881
network.ifr_flags |= IFF_UP;
882
ret = ioctl(sd, SIOCSIFFLAGS, &network);
884
perror("ioctl SIOCSIFFLAGS");
885
exitcode = EXIT_FAILURE;
810
893
avahi_set_log_function(empty_log);
813
/* Initialize the psuedo-RNG */
896
/* Initialize the pseudo-RNG for Avahi */
814
897
srand((unsigned int) time(NULL));
816
/* Allocate main loop object */
817
if (!(mc.simple_poll = avahi_simple_poll_new())) {
818
fprintf(stderr, "Failed to create simple poll object.\n");
819
returncode = EXIT_FAILURE;
823
/* Do not publish any local records */
824
avahi_server_config_init(&config);
825
config.publish_hinfo = 0;
826
config.publish_addresses = 0;
827
config.publish_workstation = 0;
828
config.publish_domain = 0;
830
/* Allocate a new server */
831
mc.server=avahi_server_new(avahi_simple_poll_get(mc.simple_poll),
832
&config, NULL, NULL, &error);
834
/* Free the configuration data */
835
avahi_server_config_free(&config);
837
/* Check if creating the server object succeeded */
839
fprintf(stderr, "Failed to create server: %s\n",
899
/* Allocate main Avahi loop object */
900
mc.simple_poll = avahi_simple_poll_new();
901
if (mc.simple_poll == NULL) {
902
fprintf(stderr, "Avahi: Failed to create simple poll"
904
exitcode = EXIT_FAILURE;
909
AvahiServerConfig config;
910
/* Do not publish any local Zeroconf records */
911
avahi_server_config_init(&config);
912
config.publish_hinfo = 0;
913
config.publish_addresses = 0;
914
config.publish_workstation = 0;
915
config.publish_domain = 0;
917
/* Allocate a new server */
918
mc.server = avahi_server_new(avahi_simple_poll_get
919
(mc.simple_poll), &config, NULL,
922
/* Free the Avahi configuration data */
923
avahi_server_config_free(&config);
926
/* Check if creating the Avahi server object succeeded */
927
if (mc.server == NULL) {
928
fprintf(stderr, "Failed to create Avahi server: %s\n",
840
929
avahi_strerror(error));
841
returncode = EXIT_FAILURE;
930
exitcode = EXIT_FAILURE;
845
/* Create the service browser */
934
/* Create the Avahi service browser */
846
935
sb = avahi_s_service_browser_new(mc.server, if_index,
847
936
AVAHI_PROTO_INET6,
848
937
"_mandos._tcp", NULL, 0,
849
938
browse_callback, &mc);
851
940
fprintf(stderr, "Failed to create service browser: %s\n",
852
941
avahi_strerror(avahi_server_errno(mc.server)));
853
returncode = EXIT_FAILURE;
942
exitcode = EXIT_FAILURE;
857
946
/* Run the main loop */
860
fprintf(stderr, "Starting avahi loop search\n");
949
fprintf(stderr, "Starting Avahi loop search\n");
863
952
avahi_simple_poll_loop(mc.simple_poll);
868
957
fprintf(stderr, "%s exiting\n", argv[0]);
871
960
/* Cleanup things */
873
962
avahi_s_service_browser_free(sb);
964
if (mc.server != NULL)
876
965
avahi_server_free(mc.server);
967
if (mc.simple_poll != NULL)
879
968
avahi_simple_poll_free(mc.simple_poll);
880
969
free(pubkeyfile);
881
970
free(seckeyfile);