/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

Support not using IPv6 in server:

* mandos (AvahiService.__init__): Take new "protocol" parameter.  All
                                  callers changed.
  (IPv6_TCPServer.__init__): Take new "use_ipv6" parameter.  All
                             callers changed.
  (IPv6_TCPServer.server_bind): Create IPv4 socket if not using IPv6.
  (main): New "--no-ipv6" command line option.  New "use_ipv6" config
          option.
* mandos-options.xml ([@id="address"]): Document conditional IPv4
                                        address support.
  ([@id="ipv6"]): New paragraph.
* mandos.conf (use_ipv6): New config option.
* mandos.conf.xml (OPTIONS): Document new "use_dbus" option.
  (EXAMPLE): Changed to use IPv6 link-local address.  Added "use_ipv6"
             option.
* mandos.xml (SYNOPSIS): New "--no-ipv6" option.
  (OPTIONS): Document new "--no-ipv6" option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
 
#ifndef _LARGEFILE_SOURCE
34
33
#define _LARGEFILE_SOURCE
35
 
#endif
36
 
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
38
 
#endif
39
35
 
40
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
37
 
44
40
#include <stdint.h>             /* uint16_t, uint32_t */
45
41
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
42
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof(), abort() */
 
43
                                   srand(), strtof() */
48
44
#include <stdbool.h>            /* bool, false, true */
49
45
#include <string.h>             /* memset(), strcmp(), strlen(),
50
46
                                   strerror(), asprintf(), strcpy() */
71
67
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
68
                                */
73
69
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid(), pause() */
 
70
                                   getuid(), getgid(), setuid(),
 
71
                                   setgid() */
76
72
#include <arpa/inet.h>          /* inet_pton(), htons */
77
73
#include <iso646.h>             /* not, or, and */
78
74
#include <argp.h>               /* struct argp_option, error_t, struct
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
85
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
 
79
                                   sigaction(), SIGTERM, sigaction,
 
80
                                   sig_atomic_t */
86
81
 
87
82
#ifdef __linux__
88
83
#include <sys/klog.h>           /* klogctl() */
143
138
                      .dh_bits = 1024, .priority = "SECURE256"
144
139
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
145
140
 
146
 
sig_atomic_t quit_now = 0;
147
 
int signal_received = 0;
148
 
 
149
141
/*
150
142
 * Make additional room in "buffer" for at least BUFFER_SIZE more
151
143
 * bytes. "buffer_capacity" is how much is currently allocated,
168
160
 */
169
161
static bool init_gpgme(const char *seckey,
170
162
                       const char *pubkey, const char *tempdir){
 
163
  int ret;
171
164
  gpgme_error_t rc;
172
165
  gpgme_engine_info_t engine_info;
173
166
  
176
169
   * Helper function to insert pub and seckey to the engine keyring.
177
170
   */
178
171
  bool import_key(const char *filename){
179
 
    int ret;
180
172
    int fd;
181
173
    gpgme_data_t pgp_data;
182
174
    
253
245
    return false;
254
246
  }
255
247
  
256
 
  return true;
 
248
  return true; 
257
249
}
258
250
 
259
251
/* 
313
305
        }
314
306
        gpgme_recipient_t recipient;
315
307
        recipient = result->recipients;
316
 
        while(recipient != NULL){
317
 
          fprintf(stderr, "Public key algorithm: %s\n",
318
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
319
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
320
 
          fprintf(stderr, "Secret key available: %s\n",
321
 
                  recipient->status == GPG_ERR_NO_SECKEY
322
 
                  ? "No" : "Yes");
323
 
          recipient = recipient->next;
 
308
        if(recipient){
 
309
          while(recipient != NULL){
 
310
            fprintf(stderr, "Public key algorithm: %s\n",
 
311
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
312
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
313
            fprintf(stderr, "Secret key available: %s\n",
 
314
                    recipient->status == GPG_ERR_NO_SECKEY
 
315
                    ? "No" : "Yes");
 
316
            recipient = recipient->next;
 
317
          }
324
318
        }
325
319
      }
326
320
    }
478
472
static int init_gnutls_session(gnutls_session_t *session){
479
473
  int ret;
480
474
  /* GnuTLS session creation */
481
 
  do {
482
 
    ret = gnutls_init(session, GNUTLS_SERVER);
483
 
    if(quit_now){
484
 
      return -1;
485
 
    }
486
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
475
  ret = gnutls_init(session, GNUTLS_SERVER);
487
476
  if(ret != GNUTLS_E_SUCCESS){
488
477
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
489
478
            safer_gnutls_strerror(ret));
491
480
  
492
481
  {
493
482
    const char *err;
494
 
    do {
495
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
496
 
      if(quit_now){
497
 
        gnutls_deinit(*session);
498
 
        return -1;
499
 
      }
500
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
483
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
501
484
    if(ret != GNUTLS_E_SUCCESS){
502
485
      fprintf(stderr, "Syntax error at: %s\n", err);
503
486
      fprintf(stderr, "GnuTLS error: %s\n",
507
490
    }
508
491
  }
509
492
  
510
 
  do {
511
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
512
 
                                 mc.cred);
513
 
    if(quit_now){
514
 
      gnutls_deinit(*session);
515
 
      return -1;
516
 
    }
517
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
493
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
494
                               mc.cred);
518
495
  if(ret != GNUTLS_E_SUCCESS){
519
496
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
520
497
            safer_gnutls_strerror(ret));
523
500
  }
524
501
  
525
502
  /* ignore client certificate if any. */
526
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
503
  gnutls_certificate_server_set_request(*session,
 
504
                                        GNUTLS_CERT_IGNORE);
527
505
  
528
506
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
529
507
  
538
516
static int start_mandos_communication(const char *ip, uint16_t port,
539
517
                                      AvahiIfIndex if_index,
540
518
                                      int af){
541
 
  int ret, tcp_sd = -1;
 
519
  int ret, tcp_sd;
542
520
  ssize_t sret;
543
521
  union {
544
522
    struct sockaddr_in in;
545
523
    struct sockaddr_in6 in6;
546
524
  } to;
547
525
  char *buffer = NULL;
548
 
  char *decrypted_buffer = NULL;
 
526
  char *decrypted_buffer;
549
527
  size_t buffer_length = 0;
550
528
  size_t buffer_capacity = 0;
 
529
  ssize_t decrypted_buffer_size;
551
530
  size_t written;
552
 
  int retval = -1;
 
531
  int retval = 0;
553
532
  gnutls_session_t session;
554
533
  int pf;                       /* Protocol family */
555
534
  
556
 
  if(quit_now){
557
 
    return -1;
558
 
  }
559
 
  
560
535
  switch(af){
561
536
  case AF_INET6:
562
537
    pf = PF_INET6;
582
557
  tcp_sd = socket(pf, SOCK_STREAM, 0);
583
558
  if(tcp_sd < 0){
584
559
    perror("socket");
585
 
    goto mandos_end;
586
 
  }
587
 
  
588
 
  if(quit_now){
589
 
    goto mandos_end;
 
560
    return -1;
590
561
  }
591
562
  
592
563
  memset(&to, 0, sizeof(to));
593
564
  if(af == AF_INET6){
594
 
    to.in6.sin6_family = (sa_family_t)af;
 
565
    to.in6.sin6_family = (uint16_t)af;
595
566
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
596
567
  } else {                      /* IPv4 */
597
568
    to.in.sin_family = (sa_family_t)af;
599
570
  }
600
571
  if(ret < 0 ){
601
572
    perror("inet_pton");
602
 
    goto mandos_end;
 
573
    return -1;
603
574
  }
604
575
  if(ret == 0){
605
576
    fprintf(stderr, "Bad address: %s\n", ip);
606
 
    goto mandos_end;
 
577
    return -1;
607
578
  }
608
579
  if(af == AF_INET6){
609
580
    to.in6.sin6_port = htons(port); /* Spurious warnings from
616
587
      if(if_index == AVAHI_IF_UNSPEC){
617
588
        fprintf(stderr, "An IPv6 link-local address is incomplete"
618
589
                " without a network interface\n");
619
 
        goto mandos_end;
 
590
        return -1;
620
591
      }
621
592
      /* Set the network interface number as scope */
622
593
      to.in6.sin6_scope_id = (uint32_t)if_index;
627
598
                                     -Wunreachable-code */
628
599
  }
629
600
  
630
 
  if(quit_now){
631
 
    goto mandos_end;
632
 
  }
633
 
  
634
601
  if(debug){
635
602
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
636
603
      char interface[IF_NAMESIZE];
663
630
    }
664
631
  }
665
632
  
666
 
  if(quit_now){
667
 
    goto mandos_end;
668
 
  }
669
 
  
670
633
  if(af == AF_INET6){
671
634
    ret = connect(tcp_sd, &to.in6, sizeof(to));
672
635
  } else {
674
637
  }
675
638
  if(ret < 0){
676
639
    perror("connect");
677
 
    goto mandos_end;
678
 
  }
679
 
  
680
 
  if(quit_now){
681
 
    goto mandos_end;
 
640
    return -1;
682
641
  }
683
642
  
684
643
  const char *out = mandos_protocol_version;
689
648
                                   out_size - written));
690
649
    if(ret == -1){
691
650
      perror("write");
 
651
      retval = -1;
692
652
      goto mandos_end;
693
653
    }
694
654
    written += (size_t)ret;
702
662
        break;
703
663
      }
704
664
    }
705
 
  
706
 
    if(quit_now){
707
 
      goto mandos_end;
708
 
    }
709
665
  }
710
666
  
711
667
  if(debug){
712
668
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
713
669
  }
714
670
  
715
 
  if(quit_now){
716
 
    goto mandos_end;
717
 
  }
718
 
  
719
671
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
720
672
  
721
 
  if(quit_now){
722
 
    goto mandos_end;
723
 
  }
724
 
  
725
 
  do {
 
673
  do{
726
674
    ret = gnutls_handshake(session);
727
 
    if(quit_now){
728
 
      goto mandos_end;
729
 
    }
730
675
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
731
676
  
732
677
  if(ret != GNUTLS_E_SUCCESS){
734
679
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
735
680
      gnutls_perror(ret);
736
681
    }
 
682
    retval = -1;
737
683
    goto mandos_end;
738
684
  }
739
685
  
745
691
  }
746
692
  
747
693
  while(true){
748
 
    
749
 
    if(quit_now){
750
 
      goto mandos_end;
751
 
    }
752
 
    
753
694
    buffer_capacity = incbuffer(&buffer, buffer_length,
754
695
                                   buffer_capacity);
755
696
    if(buffer_capacity == 0){
756
697
      perror("incbuffer");
757
 
      goto mandos_end;
758
 
    }
759
 
    
760
 
    if(quit_now){
 
698
      retval = -1;
761
699
      goto mandos_end;
762
700
    }
763
701
    
772
710
      case GNUTLS_E_AGAIN:
773
711
        break;
774
712
      case GNUTLS_E_REHANDSHAKE:
775
 
        do {
 
713
        do{
776
714
          ret = gnutls_handshake(session);
777
 
          
778
 
          if(quit_now){
779
 
            goto mandos_end;
780
 
          }
781
715
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
782
716
        if(ret < 0){
783
717
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
784
718
          gnutls_perror(ret);
 
719
          retval = -1;
785
720
          goto mandos_end;
786
721
        }
787
722
        break;
788
723
      default:
789
724
        fprintf(stderr, "Unknown error while reading data from"
790
725
                " encrypted session with Mandos server\n");
 
726
        retval = -1;
791
727
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
792
728
        goto mandos_end;
793
729
      }
800
736
    fprintf(stderr, "Closing TLS session\n");
801
737
  }
802
738
  
803
 
  if(quit_now){
804
 
    goto mandos_end;
805
 
  }
806
 
  
807
 
  do {
808
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
809
 
    if(quit_now){
810
 
      goto mandos_end;
811
 
    }
812
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
739
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
813
740
  
814
741
  if(buffer_length > 0){
815
 
    ssize_t decrypted_buffer_size;
816
742
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
817
743
                                               buffer_length,
818
744
                                               &decrypted_buffer);
819
745
    if(decrypted_buffer_size >= 0){
820
 
      
821
746
      written = 0;
822
747
      while(written < (size_t) decrypted_buffer_size){
823
 
        if(quit_now){
824
 
          goto mandos_end;
825
 
        }
826
 
        
827
748
        ret = (int)fwrite(decrypted_buffer + written, 1,
828
749
                          (size_t)decrypted_buffer_size - written,
829
750
                          stdout);
832
753
            fprintf(stderr, "Error writing encrypted data: %s\n",
833
754
                    strerror(errno));
834
755
          }
835
 
          goto mandos_end;
 
756
          retval = -1;
 
757
          break;
836
758
        }
837
759
        written += (size_t)ret;
838
760
      }
839
 
      retval = 0;
 
761
      free(decrypted_buffer);
 
762
    } else {
 
763
      retval = -1;
840
764
    }
 
765
  } else {
 
766
    retval = -1;
841
767
  }
842
768
  
843
769
  /* Shutdown procedure */
844
770
  
845
771
 mandos_end:
846
 
  free(decrypted_buffer);
847
772
  free(buffer);
848
 
  if(tcp_sd >= 0){
849
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
850
 
  }
 
773
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
851
774
  if(ret == -1){
852
775
    perror("close");
853
776
  }
854
777
  gnutls_deinit(session);
855
 
  if(quit_now){
856
 
    retval = -1;
857
 
  }
858
778
  return retval;
859
779
}
860
780
 
877
797
  /* Called whenever a service has been resolved successfully or
878
798
     timed out */
879
799
  
880
 
  if(quit_now){
881
 
    return;
882
 
  }
883
 
  
884
800
  switch(event){
885
801
  default:
886
802
  case AVAHI_RESOLVER_FAILURE:
923
839
  /* Called whenever a new services becomes available on the LAN or
924
840
     is removed from the LAN */
925
841
  
926
 
  if(quit_now){
927
 
    return;
928
 
  }
929
 
  
930
842
  switch(event){
931
843
  default:
932
844
  case AVAHI_BROWSER_FAILURE:
961
873
  }
962
874
}
963
875
 
 
876
sig_atomic_t quit_now = 0;
 
877
 
964
878
/* stop main loop after sigterm has been called */
965
 
static void handle_sigterm(int sig){
 
879
static void handle_sigterm(__attribute__((unused)) int sig){
966
880
  if(quit_now){
967
881
    return;
968
882
  }
969
883
  quit_now = 1;
970
 
  signal_received = sig;
971
884
  int old_errno = errno;
972
885
  if(mc.simple_poll != NULL){
973
886
    avahi_simple_poll_quit(mc.simple_poll);
984
897
  int exitcode = EXIT_SUCCESS;
985
898
  const char *interface = "eth0";
986
899
  struct ifreq network;
987
 
  int sd = -1;
988
 
  bool take_down_interface = false;
 
900
  int sd;
989
901
  uid_t uid;
990
902
  gid_t gid;
991
903
  char *connect_to = NULL;
999
911
  bool gpgme_initialized = false;
1000
912
  float delay = 2.5f;
1001
913
  
1002
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
914
  struct sigaction old_sigterm_action;
1003
915
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1004
916
  
1005
 
  uid = getuid();
1006
 
  gid = getgid();
1007
 
  
1008
 
  /* Lower any group privileges we might have, just to be safe */
1009
 
  errno = 0;
1010
 
  ret = setgid(gid);
1011
 
  if(ret == -1){
1012
 
    perror("setgid");
1013
 
  }
1014
 
  
1015
 
  /* Lower user privileges (temporarily) */
1016
 
  errno = 0;
1017
 
  ret = seteuid(uid);
1018
 
  if(ret == -1){
1019
 
    perror("seteuid");
1020
 
  }
1021
 
  
1022
 
  if(quit_now){
1023
 
    goto end;
1024
 
  }
1025
 
  
1026
917
  {
1027
918
    struct argp_option options[] = {
1028
919
      { .name = "debug", .key = 128,
1057
948
        .arg = "SECONDS",
1058
949
        .doc = "Maximum delay to wait for interface startup",
1059
950
        .group = 2 },
1060
 
      /*
1061
 
       * These reproduce what we would get without ARGP_NO_HELP
1062
 
       */
1063
 
      { .name = "help", .key = '?',
1064
 
        .doc = "Give this help list", .group = -1 },
1065
 
      { .name = "usage", .key = -3,
1066
 
        .doc = "Give a short usage message", .group = -1 },
1067
 
      { .name = "version", .key = 'V',
1068
 
        .doc = "Print program version", .group = -1 },
1069
951
      { .name = NULL }
1070
952
    };
1071
953
    
1072
954
    error_t parse_opt(int key, char *arg,
1073
955
                      struct argp_state *state){
1074
 
      errno = 0;
1075
956
      switch(key){
1076
957
      case 128:                 /* --debug */
1077
958
        debug = true;
1093
974
        tmpmax = strtoimax(arg, &tmp, 10);
1094
975
        if(errno != 0 or tmp == arg or *tmp != '\0'
1095
976
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1096
 
          argp_error(state, "Bad number of DH bits");
 
977
          fprintf(stderr, "Bad number of DH bits\n");
 
978
          exit(EXIT_FAILURE);
1097
979
        }
1098
980
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1099
981
        break;
1104
986
        errno = 0;
1105
987
        delay = strtof(arg, &tmp);
1106
988
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1107
 
          argp_error(state, "Bad delay");
 
989
          fprintf(stderr, "Bad delay\n");
 
990
          exit(EXIT_FAILURE);
1108
991
        }
1109
992
        break;
1110
 
        /*
1111
 
         * These reproduce what we would get without ARGP_NO_HELP
1112
 
         */
1113
 
      case '?':                 /* --help */
1114
 
        argp_state_help(state, state->out_stream,
1115
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1116
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1117
 
      case -3:                  /* --usage */
1118
 
        argp_state_help(state, state->out_stream,
1119
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1120
 
      case 'V':                 /* --version */
1121
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1122
 
        exit(argp_err_exit_status);
 
993
      case ARGP_KEY_ARG:
 
994
        argp_usage(state);
 
995
      case ARGP_KEY_END:
1123
996
        break;
1124
997
      default:
1125
998
        return ARGP_ERR_UNKNOWN;
1126
999
      }
1127
 
      return errno;
 
1000
      return 0;
1128
1001
    }
1129
1002
    
1130
1003
    struct argp argp = { .options = options, .parser = parse_opt,
1131
1004
                         .args_doc = "",
1132
1005
                         .doc = "Mandos client -- Get and decrypt"
1133
1006
                         " passwords from a Mandos server" };
1134
 
    ret = argp_parse(&argp, argc, argv,
1135
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1136
 
    switch(ret){
1137
 
    case 0:
1138
 
      break;
1139
 
    case ENOMEM:
1140
 
    default:
1141
 
      errno = ret;
1142
 
      perror("argp_parse");
1143
 
      exitcode = EX_OSERR;
1144
 
      goto end;
1145
 
    case EINVAL:
1146
 
      exitcode = EX_USAGE;
 
1007
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1008
    if(ret == ARGP_ERR_UNKNOWN){
 
1009
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1010
      exitcode = EXIT_FAILURE;
1147
1011
      goto end;
1148
1012
    }
1149
1013
  }
1182
1046
    exitcode = EXIT_FAILURE;
1183
1047
    goto end;
1184
1048
  }
1185
 
  /* Need to check if the handler is SIG_IGN before handling:
1186
 
     | [[info:libc:Initial Signal Actions]] |
1187
 
     | [[info:libc:Basic Signal Handling]]  |
1188
 
  */
1189
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1190
 
  if(ret == -1){
1191
 
    perror("sigaction");
1192
 
    return EXIT_FAILURE;
1193
 
  }
1194
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1195
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1196
 
    if(ret == -1){
1197
 
      perror("sigaction");
1198
 
      exitcode = EXIT_FAILURE;
1199
 
      goto end;
1200
 
    }
1201
 
  }
1202
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1203
 
  if(ret == -1){
1204
 
    perror("sigaction");
1205
 
    return EXIT_FAILURE;
1206
 
  }
1207
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1208
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1209
 
    if(ret == -1){
1210
 
      perror("sigaction");
1211
 
      exitcode = EXIT_FAILURE;
1212
 
      goto end;
1213
 
    }
1214
 
  }
1215
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1216
 
  if(ret == -1){
1217
 
    perror("sigaction");
1218
 
    return EXIT_FAILURE;
1219
 
  }
1220
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1221
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1222
 
    if(ret == -1){
1223
 
      perror("sigaction");
1224
 
      exitcode = EXIT_FAILURE;
1225
 
      goto end;
1226
 
    }
1227
 
  }
 
1049
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1050
  if(ret == -1){
 
1051
    perror("sigaction");
 
1052
    exitcode = EXIT_FAILURE;
 
1053
    goto end;
 
1054
  }  
1228
1055
  
1229
1056
  /* If the interface is down, bring it up */
1230
1057
  if(interface[0] != '\0'){
1231
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1232
 
    if(if_index == 0){
1233
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1234
 
      exitcode = EXIT_FAILURE;
1235
 
      goto end;
1236
 
    }
1237
 
    
1238
 
    if(quit_now){
1239
 
      goto end;
1240
 
    }
1241
 
    
1242
 
    /* Re-raise priviliges */
1243
 
    errno = 0;
1244
 
    ret = seteuid(0);
1245
 
    if(ret == -1){
1246
 
      perror("seteuid");
1247
 
    }
1248
 
    
1249
1058
#ifdef __linux__
1250
1059
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1251
 
       messages about the network interface to mess up the prompt */
 
1060
       messages to mess up the prompt */
1252
1061
    ret = klogctl(8, NULL, 5);
1253
1062
    bool restore_loglevel = true;
1254
1063
    if(ret == -1){
1269
1078
        }
1270
1079
      }
1271
1080
#endif  /* __linux__ */
1272
 
      /* Lower privileges */
1273
 
      errno = 0;
1274
 
      ret = seteuid(uid);
1275
 
      if(ret == -1){
1276
 
        perror("seteuid");
1277
 
      }
1278
1081
      goto end;
1279
1082
    }
1280
1083
    strcpy(network.ifr_name, interface);
1290
1093
      }
1291
1094
#endif  /* __linux__ */
1292
1095
      exitcode = EXIT_FAILURE;
1293
 
      /* Lower privileges */
1294
 
      errno = 0;
1295
 
      ret = seteuid(uid);
1296
 
      if(ret == -1){
1297
 
        perror("seteuid");
1298
 
      }
1299
1096
      goto end;
1300
1097
    }
1301
1098
    if((network.ifr_flags & IFF_UP) == 0){
1302
1099
      network.ifr_flags |= IFF_UP;
1303
 
      take_down_interface = true;
1304
1100
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1305
1101
      if(ret == -1){
1306
 
        take_down_interface = false;
1307
1102
        perror("ioctl SIOCSIFFLAGS");
1308
1103
        exitcode = EXIT_FAILURE;
1309
1104
#ifdef __linux__
1314
1109
          }
1315
1110
        }
1316
1111
#endif  /* __linux__ */
1317
 
        /* Lower privileges */
1318
 
        errno = 0;
1319
 
        ret = seteuid(uid);
1320
 
        if(ret == -1){
1321
 
          perror("seteuid");
1322
 
        }
1323
1112
        goto end;
1324
1113
      }
1325
1114
    }
1337
1126
        perror("nanosleep");
1338
1127
      }
1339
1128
    }
1340
 
    if(not take_down_interface){
1341
 
      /* We won't need the socket anymore */
1342
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1343
 
      if(ret == -1){
1344
 
        perror("close");
1345
 
      }
 
1129
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1130
    if(ret == -1){
 
1131
      perror("close");
1346
1132
    }
1347
1133
#ifdef __linux__
1348
1134
    if(restore_loglevel){
1353
1139
      }
1354
1140
    }
1355
1141
#endif  /* __linux__ */
1356
 
    /* Lower privileges */
1357
 
    errno = 0;
1358
 
    if(take_down_interface){
1359
 
      /* Lower privileges */
1360
 
      ret = seteuid(uid);
1361
 
      if(ret == -1){
1362
 
        perror("seteuid");
1363
 
      }
1364
 
    } else {
1365
 
      /* Lower privileges permanently */
1366
 
      ret = setuid(uid);
1367
 
      if(ret == -1){
1368
 
        perror("setuid");
1369
 
      }
1370
 
    }
1371
 
  }
1372
 
  
1373
 
  if(quit_now){
1374
 
    goto end;
 
1142
  }
 
1143
  
 
1144
  uid = getuid();
 
1145
  gid = getgid();
 
1146
  
 
1147
  errno = 0;
 
1148
  setgid(gid);
 
1149
  if(ret == -1){
 
1150
    perror("setgid");
 
1151
  }
 
1152
  
 
1153
  ret = setuid(uid);
 
1154
  if(ret == -1){
 
1155
    perror("setuid");
1375
1156
  }
1376
1157
  
1377
1158
  ret = init_gnutls_global(pubkey, seckey);
1383
1164
    gnutls_initialized = true;
1384
1165
  }
1385
1166
  
1386
 
  if(quit_now){
1387
 
    goto end;
1388
 
  }
1389
 
  
1390
 
  tempdir_created = true;
1391
1167
  if(mkdtemp(tempdir) == NULL){
1392
 
    tempdir_created = false;
1393
1168
    perror("mkdtemp");
1394
1169
    goto end;
1395
1170
  }
1396
 
  
1397
 
  if(quit_now){
1398
 
    goto end;
1399
 
  }
 
1171
  tempdir_created = true;
1400
1172
  
1401
1173
  if(not init_gpgme(pubkey, seckey, tempdir)){
1402
1174
    fprintf(stderr, "init_gpgme failed\n");
1406
1178
    gpgme_initialized = true;
1407
1179
  }
1408
1180
  
1409
 
  if(quit_now){
1410
 
    goto end;
 
1181
  if(interface[0] != '\0'){
 
1182
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1183
    if(if_index == 0){
 
1184
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1185
      exitcode = EXIT_FAILURE;
 
1186
      goto end;
 
1187
    }
1411
1188
  }
1412
1189
  
1413
1190
  if(connect_to != NULL){
1419
1196
      exitcode = EXIT_FAILURE;
1420
1197
      goto end;
1421
1198
    }
1422
 
    
1423
 
    if(quit_now){
1424
 
      goto end;
1425
 
    }
1426
 
    
1427
1199
    uint16_t port;
1428
1200
    errno = 0;
1429
1201
    tmpmax = strtoimax(address+1, &tmp, 10);
1433
1205
      exitcode = EXIT_FAILURE;
1434
1206
      goto end;
1435
1207
    }
1436
 
  
1437
 
    if(quit_now){
1438
 
      goto end;
1439
 
    }
1440
 
    
1441
1208
    port = (uint16_t)tmpmax;
1442
1209
    *address = '\0';
1443
1210
    address = connect_to;
1448
1215
    } else {
1449
1216
      af = AF_INET;
1450
1217
    }
1451
 
    
1452
 
    if(quit_now){
1453
 
      goto end;
1454
 
    }
1455
 
    
1456
1218
    ret = start_mandos_communication(address, port, if_index, af);
1457
1219
    if(ret < 0){
1458
1220
      exitcode = EXIT_FAILURE;
1461
1223
    }
1462
1224
    goto end;
1463
1225
  }
1464
 
  
1465
 
  if(quit_now){
1466
 
    goto end;
1467
 
  }
1468
 
  
 
1226
    
1469
1227
  {
1470
1228
    AvahiServerConfig config;
1471
1229
    /* Do not publish any local Zeroconf records */
1492
1250
    goto end;
1493
1251
  }
1494
1252
  
1495
 
  if(quit_now){
1496
 
    goto end;
1497
 
  }
1498
 
  
1499
1253
  /* Create the Avahi service browser */
1500
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1501
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1507
1261
    goto end;
1508
1262
  }
1509
1263
  
1510
 
  if(quit_now){
1511
 
    goto end;
1512
 
  }
1513
 
  
1514
1264
  /* Run the main loop */
1515
1265
  
1516
1266
  if(debug){
1545
1295
    gpgme_release(mc.ctx);
1546
1296
  }
1547
1297
  
1548
 
  /* Take down the network interface */
1549
 
  if(take_down_interface){
1550
 
    /* Re-raise priviliges */
1551
 
    errno = 0;
1552
 
    ret = seteuid(0);
1553
 
    if(ret == -1){
1554
 
      perror("seteuid");
1555
 
    }
1556
 
    if(geteuid() == 0){
1557
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1558
 
      if(ret == -1){
1559
 
        perror("ioctl SIOCGIFFLAGS");
1560
 
      } else if(network.ifr_flags & IFF_UP) {
1561
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1562
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1563
 
        if(ret == -1){
1564
 
          perror("ioctl SIOCSIFFLAGS");
1565
 
        }
1566
 
      }
1567
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1568
 
      if(ret == -1){
1569
 
        perror("close");
1570
 
      }
1571
 
      /* Lower privileges permanently */
1572
 
      errno = 0;
1573
 
      ret = setuid(uid);
1574
 
      if(ret == -1){
1575
 
        perror("setuid");
1576
 
      }
1577
 
    }
1578
 
  }
1579
 
  
1580
1298
  /* Removes the temp directory used by GPGME */
1581
1299
  if(tempdir_created){
1582
1300
    DIR *d;
1621
1339
    }
1622
1340
  }
1623
1341
  
1624
 
  if(quit_now){
1625
 
    sigemptyset(&old_sigterm_action.sa_mask);
1626
 
    old_sigterm_action.sa_handler = SIG_DFL;
1627
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1628
 
                                            &old_sigterm_action,
1629
 
                                            NULL));
1630
 
    if(ret == -1){
1631
 
      perror("sigaction");
1632
 
    }
1633
 
    do {
1634
 
      ret = raise(signal_received);
1635
 
    } while(ret != 0 and errno == EINTR);
1636
 
    if(ret != 0){
1637
 
      perror("raise");
1638
 
      abort();
1639
 
    }
1640
 
    TEMP_FAILURE_RETRY(pause());
1641
 
  }
1642
 
  
1643
1342
  return exitcode;
1644
1343
}