297
284
if ((ret = gnutls_global_init ())
298
285
!= GNUTLS_E_SUCCESS) {
299
fprintf (stderr, "GnuTLS global_init: %s\n",
300
safer_gnutls_strerror(ret));
286
fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
305
/* "Use a log level over 10 to enable all debugging options."
308
291
gnutls_global_set_log_level(11);
309
292
gnutls_global_set_log_function(debuggnutls);
312
/* OpenPGP credentials */
295
/* openpgp credentials */
313
296
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
314
297
!= GNUTLS_E_SUCCESS) {
315
fprintf (stderr, "GnuTLS memory error: %s\n",
298
fprintf (stderr, "memory error: %s\n",
316
299
safer_gnutls_strerror(ret));
326
309
ret = gnutls_certificate_set_openpgp_key_file
327
310
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
328
311
if (ret != GNUTLS_E_SUCCESS) {
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",
313
(stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
315
ret, pubkeyfile, seckeyfile);
316
fprintf(stdout, "The Error is: %s\n",
333
317
safer_gnutls_strerror(ret));
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){
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){
361
341
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
362
342
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));
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));
376
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
378
if (ret != GNUTLS_E_SUCCESS) {
379
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
353
if ((ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
355
!= GNUTLS_E_SUCCESS) {
356
fprintf(stderr, "Error setting a credentials set: %s\n",
380
357
safer_gnutls_strerror(ret));
470
438
perror("connect");
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){
442
ret = initgnutls (mc, &session, &dh_params);
448
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
499
451
fprintf(stderr, "Establishing TLS session with %s\n", ip);
502
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
504
454
ret = gnutls_handshake (session);
506
456
if (ret != GNUTLS_E_SUCCESS){
508
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
458
fprintf(stderr, "\n*** Handshake failed ***\n");
509
459
gnutls_perror (ret);
515
/* Read OpenPGP packet that contains the wanted password */
465
//Retrieve OpenPGP packet that contains the wanted password
518
468
fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
540
492
case GNUTLS_E_REHANDSHAKE:
541
493
ret = gnutls_handshake (session);
543
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
495
fprintf(stderr, "\n*** Handshake failed ***\n");
544
496
gnutls_perror (ret);
550
502
fprintf(stderr, "Unknown error while reading data from"
551
" encrypted session with Mandos server\n");
503
" encrypted session with mandos server\n");
553
505
gnutls_bye (session, GNUTLS_SHUT_RDWR);
557
509
buffer_length += (size_t) ret;
562
fprintf(stderr, "Closing TLS session\n");
565
gnutls_bye (session, GNUTLS_SHUT_RDWR);
567
513
if (buffer_length > 0){
568
514
decrypted_buffer_size = pgp_packet_decrypt(buffer,
570
516
&decrypted_buffer,
572
518
if (decrypted_buffer_size >= 0){
574
519
while(written < (size_t) decrypted_buffer_size){
575
520
ret = (int)fwrite (decrypted_buffer + written, 1,
576
521
(size_t)decrypted_buffer_size - written,
668
618
case AVAHI_BROWSER_FAILURE:
670
fprintf(stderr, "(Avahi browser) %s\n",
620
fprintf(stderr, "(Browser) %s\n",
671
621
avahi_strerror(avahi_server_errno(mc->server)));
672
622
avahi_simple_poll_quit(mc->simple_poll);
675
625
case AVAHI_BROWSER_NEW:
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
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. */
681
631
if (!(avahi_s_service_resolver_new(mc->server, interface,
682
632
protocol, name, type, domain,
683
633
AVAHI_PROTO_INET6, 0,
684
634
resolve_callback, mc)))
685
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
686
name, avahi_strerror(avahi_server_errno(mc->server)));
635
fprintf(stderr, "Failed to resolve service '%s': %s\n", name,
636
avahi_strerror(avahi_server_errno(mc->server)));
689
639
case AVAHI_BROWSER_REMOVE:
722
int main(int argc, char *argv[]){
669
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
670
AvahiServerConfig config;
723
671
AvahiSServiceBrowser *sb = NULL;
726
int exitcode = EXIT_SUCCESS;
675
int returncode = EXIT_SUCCESS;
727
676
const char *interface = "eth0";
728
677
struct ifreq network;
732
679
char *connect_to = NULL;
733
680
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
734
681
mandos_context mc = { .simple_poll = NULL, .server = NULL,
735
682
.dh_bits = 1024, .priority = "SECURE256"};
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. */
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);
793
728
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;
800
741
pubkeyfile = combinepath(keydir, pubkeyfile);
801
742
if (pubkeyfile == NULL){
802
743
perror("combinepath");
803
exitcode = EXIT_FAILURE;
744
returncode = EXIT_FAILURE;
807
748
seckeyfile = combinepath(keydir, seckeyfile);
808
749
if (seckeyfile == NULL){
809
750
perror("combinepath");
813
ret = init_gnutls_global(&mc);
815
fprintf(stderr, "init_gnutls_global\n");
832
754
if_index = (AvahiIfIndex) if_nametoindex(interface);
841
763
char *address = strrchr(connect_to, ':');
842
764
if(address == NULL){
843
765
fprintf(stderr, "No colon in address\n");
844
exitcode = EXIT_FAILURE;
848
769
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
850
771
perror("Bad port number");
851
exitcode = EXIT_FAILURE;
855
775
address = connect_to;
856
776
ret = start_mandos_communication(address, port, if_index, &mc);
858
exitcode = EXIT_FAILURE;
860
exitcode = EXIT_SUCCESS;
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);
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);
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;
802
perror("ioctl SIOCSIFFLAGS");
803
returncode = EXIT_FAILURE;
893
810
avahi_set_log_function(empty_log);
896
/* Initialize the pseudo-RNG for Avahi */
813
/* Initialize the psuedo-RNG */
897
814
srand((unsigned int) time(NULL));
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",
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",
929
840
avahi_strerror(error));
930
exitcode = EXIT_FAILURE;
841
returncode = EXIT_FAILURE;
934
/* Create the Avahi service browser */
845
/* Create the service browser */
935
846
sb = avahi_s_service_browser_new(mc.server, if_index,
936
847
AVAHI_PROTO_INET6,
937
848
"_mandos._tcp", NULL, 0,
938
849
browse_callback, &mc);
940
851
fprintf(stderr, "Failed to create service browser: %s\n",
941
852
avahi_strerror(avahi_server_errno(mc.server)));
942
exitcode = EXIT_FAILURE;
853
returncode = EXIT_FAILURE;
946
857
/* Run the main loop */
949
fprintf(stderr, "Starting Avahi loop search\n");
860
fprintf(stderr, "Starting avahi loop search\n");
952
863
avahi_simple_poll_loop(mc.simple_poll);
957
868
fprintf(stderr, "%s exiting\n", argv[0]);
960
871
/* Cleanup things */
962
873
avahi_s_service_browser_free(sb);
964
if (mc.server != NULL)
965
876
avahi_server_free(mc.server);
967
if (mc.simple_poll != NULL)
968
879
avahi_simple_poll_free(mc.simple_poll);
969
880
free(pubkeyfile);
970
881
free(seckeyfile);