/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: 2009-09-04 16:32:22 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090904163222-l9wp25ng1e5ym0yq
* plugin-runner.c (main): When a plugin is killed by a signal, show
                          the signal name, not just the number, in the
                          debug log message.

* plugins.d/password-prompt.c (termination_handler): Store received
                                                     signal in
                                                     "signal_received".
  (main): If exiting due to signal, re-raise signal received instead
          of returning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid(), pause() */
 
74
                                   getuid(), getgid(), setuid(),
 
75
                                   setgid() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
83
                                   sigaction(), SIGTERM, sig_atomic_t,
84
84
                                   raise() */
85
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
86
 
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
87
85
 
88
86
#ifdef __linux__
89
87
#include <sys/klog.h>           /* klogctl() */
144
142
                      .dh_bits = 1024, .priority = "SECURE256"
145
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
146
144
 
147
 
sig_atomic_t quit_now = 0;
148
 
int signal_received = 0;
149
 
 
150
145
/*
151
146
 * Make additional room in "buffer" for at least BUFFER_SIZE more
152
147
 * bytes. "buffer_capacity" is how much is currently allocated,
169
164
 */
170
165
static bool init_gpgme(const char *seckey,
171
166
                       const char *pubkey, const char *tempdir){
 
167
  int ret;
172
168
  gpgme_error_t rc;
173
169
  gpgme_engine_info_t engine_info;
174
170
  
177
173
   * Helper function to insert pub and seckey to the engine keyring.
178
174
   */
179
175
  bool import_key(const char *filename){
180
 
    int ret;
181
176
    int fd;
182
177
    gpgme_data_t pgp_data;
183
178
    
479
474
static int init_gnutls_session(gnutls_session_t *session){
480
475
  int ret;
481
476
  /* GnuTLS session creation */
482
 
  do {
483
 
    ret = gnutls_init(session, GNUTLS_SERVER);
484
 
    if(quit_now){
485
 
      return -1;
486
 
    }
487
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
477
  ret = gnutls_init(session, GNUTLS_SERVER);
488
478
  if(ret != GNUTLS_E_SUCCESS){
489
479
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
490
480
            safer_gnutls_strerror(ret));
492
482
  
493
483
  {
494
484
    const char *err;
495
 
    do {
496
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
497
 
      if(quit_now){
498
 
        gnutls_deinit(*session);
499
 
        return -1;
500
 
      }
501
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
485
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
502
486
    if(ret != GNUTLS_E_SUCCESS){
503
487
      fprintf(stderr, "Syntax error at: %s\n", err);
504
488
      fprintf(stderr, "GnuTLS error: %s\n",
508
492
    }
509
493
  }
510
494
  
511
 
  do {
512
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
513
 
                                 mc.cred);
514
 
    if(quit_now){
515
 
      gnutls_deinit(*session);
516
 
      return -1;
517
 
    }
518
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
495
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
496
                               mc.cred);
519
497
  if(ret != GNUTLS_E_SUCCESS){
520
498
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
521
499
            safer_gnutls_strerror(ret));
524
502
  }
525
503
  
526
504
  /* ignore client certificate if any. */
527
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
505
  gnutls_certificate_server_set_request(*session,
 
506
                                        GNUTLS_CERT_IGNORE);
528
507
  
529
508
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
530
509
  
535
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
536
515
                      __attribute__((unused)) const char *txt){}
537
516
 
 
517
sig_atomic_t quit_now = 0;
 
518
int signal_received = 0;
 
519
 
538
520
/* Called when a Mandos server is found */
539
521
static int start_mandos_communication(const char *ip, uint16_t port,
540
522
                                      AvahiIfIndex if_index,
541
523
                                      int af){
542
 
  int ret, tcp_sd = -1;
 
524
  int ret, tcp_sd;
543
525
  ssize_t sret;
544
526
  union {
545
527
    struct sockaddr_in in;
546
528
    struct sockaddr_in6 in6;
547
529
  } to;
548
530
  char *buffer = NULL;
549
 
  char *decrypted_buffer = NULL;
 
531
  char *decrypted_buffer;
550
532
  size_t buffer_length = 0;
551
533
  size_t buffer_capacity = 0;
 
534
  ssize_t decrypted_buffer_size;
552
535
  size_t written;
553
 
  int retval = -1;
 
536
  int retval = 0;
554
537
  gnutls_session_t session;
555
538
  int pf;                       /* Protocol family */
556
539
  
557
 
  errno = 0;
558
 
  
559
 
  if(quit_now){
560
 
    errno = EINTR;
561
 
    return -1;
562
 
  }
563
 
  
564
540
  switch(af){
565
541
  case AF_INET6:
566
542
    pf = PF_INET6;
570
546
    break;
571
547
  default:
572
548
    fprintf(stderr, "Bad address family: %d\n", af);
573
 
    errno = EINVAL;
574
549
    return -1;
575
550
  }
576
551
  
586
561
  
587
562
  tcp_sd = socket(pf, SOCK_STREAM, 0);
588
563
  if(tcp_sd < 0){
589
 
    int e = errno;
590
564
    perror("socket");
591
 
    errno = e;
592
 
    goto mandos_end;
593
 
  }
594
 
  
595
 
  if(quit_now){
596
 
    errno = EINTR;
597
 
    goto mandos_end;
 
565
    return -1;
598
566
  }
599
567
  
600
568
  memset(&to, 0, sizeof(to));
606
574
    ret = inet_pton(af, ip, &to.in.sin_addr);
607
575
  }
608
576
  if(ret < 0 ){
609
 
    int e = errno;
610
577
    perror("inet_pton");
611
 
    errno = e;
612
 
    goto mandos_end;
 
578
    return -1;
613
579
  }
614
580
  if(ret == 0){
615
 
    int e = errno;
616
581
    fprintf(stderr, "Bad address: %s\n", ip);
617
 
    errno = e;
618
 
    goto mandos_end;
 
582
    return -1;
619
583
  }
620
584
  if(af == AF_INET6){
621
585
    to.in6.sin6_port = htons(port); /* Spurious warnings from
628
592
      if(if_index == AVAHI_IF_UNSPEC){
629
593
        fprintf(stderr, "An IPv6 link-local address is incomplete"
630
594
                " without a network interface\n");
631
 
        errno = EINVAL;
632
 
        goto mandos_end;
 
595
        return -1;
633
596
      }
634
597
      /* Set the network interface number as scope */
635
598
      to.in6.sin6_scope_id = (uint32_t)if_index;
640
603
                                     -Wunreachable-code */
641
604
  }
642
605
  
643
 
  if(quit_now){
644
 
    errno = EINTR;
645
 
    goto mandos_end;
646
 
  }
647
 
  
648
606
  if(debug){
649
607
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
650
608
      char interface[IF_NAMESIZE];
677
635
    }
678
636
  }
679
637
  
680
 
  if(quit_now){
681
 
    errno = EINTR;
682
 
    goto mandos_end;
683
 
  }
684
 
  
685
638
  if(af == AF_INET6){
686
639
    ret = connect(tcp_sd, &to.in6, sizeof(to));
687
640
  } else {
688
641
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
689
642
  }
690
643
  if(ret < 0){
691
 
    int e = errno;
692
644
    perror("connect");
693
 
    errno = e;
694
 
    goto mandos_end;
695
 
  }
696
 
  
697
 
  if(quit_now){
698
 
    errno = EINTR;
699
 
    goto mandos_end;
 
645
    return -1;
700
646
  }
701
647
  
702
648
  const char *out = mandos_protocol_version;
706
652
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
707
653
                                   out_size - written));
708
654
    if(ret == -1){
709
 
      int e = errno;
710
655
      perror("write");
711
 
      errno = e;
 
656
      retval = -1;
712
657
      goto mandos_end;
713
658
    }
714
659
    written += (size_t)ret;
722
667
        break;
723
668
      }
724
669
    }
725
 
  
726
 
    if(quit_now){
727
 
      errno = EINTR;
728
 
      goto mandos_end;
729
 
    }
730
670
  }
731
671
  
732
672
  if(debug){
733
673
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
734
674
  }
735
675
  
736
 
  if(quit_now){
737
 
    errno = EINTR;
738
 
    goto mandos_end;
739
 
  }
740
 
  
741
676
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
742
677
  
743
 
  if(quit_now){
744
 
    errno = EINTR;
745
 
    goto mandos_end;
746
 
  }
747
 
  
748
 
  do {
 
678
  do{
749
679
    ret = gnutls_handshake(session);
750
 
    if(quit_now){
751
 
      errno = EINTR;
752
 
      goto mandos_end;
753
 
    }
754
680
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
755
681
  
756
682
  if(ret != GNUTLS_E_SUCCESS){
758
684
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
759
685
      gnutls_perror(ret);
760
686
    }
761
 
    errno = EPROTO;
 
687
    retval = -1;
762
688
    goto mandos_end;
763
689
  }
764
690
  
770
696
  }
771
697
  
772
698
  while(true){
773
 
    
774
 
    if(quit_now){
775
 
      errno = EINTR;
776
 
      goto mandos_end;
777
 
    }
778
 
    
779
699
    buffer_capacity = incbuffer(&buffer, buffer_length,
780
700
                                   buffer_capacity);
781
701
    if(buffer_capacity == 0){
782
 
      int e = errno;
783
702
      perror("incbuffer");
784
 
      errno = e;
785
 
      goto mandos_end;
786
 
    }
787
 
    
788
 
    if(quit_now){
789
 
      errno = EINTR;
 
703
      retval = -1;
790
704
      goto mandos_end;
791
705
    }
792
706
    
801
715
      case GNUTLS_E_AGAIN:
802
716
        break;
803
717
      case GNUTLS_E_REHANDSHAKE:
804
 
        do {
 
718
        do{
805
719
          ret = gnutls_handshake(session);
806
 
          
807
 
          if(quit_now){
808
 
            errno = EINTR;
809
 
            goto mandos_end;
810
 
          }
811
720
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
812
721
        if(ret < 0){
813
722
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
814
723
          gnutls_perror(ret);
815
 
          errno = EPROTO;
 
724
          retval = -1;
816
725
          goto mandos_end;
817
726
        }
818
727
        break;
819
728
      default:
820
729
        fprintf(stderr, "Unknown error while reading data from"
821
730
                " encrypted session with Mandos server\n");
 
731
        retval = -1;
822
732
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
823
 
        errno = EIO;
824
733
        goto mandos_end;
825
734
      }
826
735
    } else {
832
741
    fprintf(stderr, "Closing TLS session\n");
833
742
  }
834
743
  
835
 
  if(quit_now){
836
 
    errno = EINTR;
837
 
    goto mandos_end;
838
 
  }
839
 
  
840
 
  do {
841
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
842
 
    if(quit_now){
843
 
      errno = EINTR;
844
 
      goto mandos_end;
845
 
    }
846
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
744
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
847
745
  
848
746
  if(buffer_length > 0){
849
 
    ssize_t decrypted_buffer_size;
850
747
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
851
748
                                               buffer_length,
852
749
                                               &decrypted_buffer);
853
750
    if(decrypted_buffer_size >= 0){
854
 
      
855
751
      written = 0;
856
752
      while(written < (size_t) decrypted_buffer_size){
857
 
        if(quit_now){
858
 
          errno = EINTR;
859
 
          goto mandos_end;
860
 
        }
861
 
        
862
753
        ret = (int)fwrite(decrypted_buffer + written, 1,
863
754
                          (size_t)decrypted_buffer_size - written,
864
755
                          stdout);
865
756
        if(ret == 0 and ferror(stdout)){
866
 
          int e = errno;
867
757
          if(debug){
868
758
            fprintf(stderr, "Error writing encrypted data: %s\n",
869
759
                    strerror(errno));
870
760
          }
871
 
          errno = e;
872
 
          goto mandos_end;
 
761
          retval = -1;
 
762
          break;
873
763
        }
874
764
        written += (size_t)ret;
875
765
      }
876
 
      retval = 0;
 
766
      free(decrypted_buffer);
 
767
    } else {
 
768
      retval = -1;
877
769
    }
 
770
  } else {
 
771
    retval = -1;
878
772
  }
879
773
  
880
774
  /* Shutdown procedure */
881
775
  
882
776
 mandos_end:
883
 
  {
884
 
    int e = errno;
885
 
    free(decrypted_buffer);
886
 
    free(buffer);
887
 
    if(tcp_sd >= 0){
888
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
889
 
    }
890
 
    if(ret == -1){
891
 
      if(e == 0){
892
 
        e = errno;
893
 
      }
894
 
      perror("close");
895
 
    }
896
 
    gnutls_deinit(session);
897
 
    if(quit_now){
898
 
      e = EINTR;
899
 
      retval = -1;
900
 
    }
901
 
    errno = e;
 
777
  free(buffer);
 
778
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
779
  if(ret == -1){
 
780
    perror("close");
902
781
  }
 
782
  gnutls_deinit(session);
903
783
  return retval;
904
784
}
905
785
 
968
848
  /* Called whenever a new services becomes available on the LAN or
969
849
     is removed from the LAN */
970
850
  
971
 
  if(quit_now){
972
 
    return;
973
 
  }
974
 
  
975
851
  switch(event){
976
852
  default:
977
853
  case AVAHI_BROWSER_FAILURE:
1044
920
  bool gpgme_initialized = false;
1045
921
  float delay = 2.5f;
1046
922
  
1047
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
923
  struct sigaction old_sigterm_action;
1048
924
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1049
925
  
1050
 
  uid = getuid();
1051
 
  gid = getgid();
1052
 
  
1053
 
  /* Lower any group privileges we might have, just to be safe */
1054
 
  errno = 0;
1055
 
  ret = setgid(gid);
1056
 
  if(ret == -1){
1057
 
    perror("setgid");
1058
 
  }
1059
 
  
1060
 
  /* Lower user privileges (temporarily) */
1061
 
  errno = 0;
1062
 
  ret = seteuid(uid);
1063
 
  if(ret == -1){
1064
 
    perror("seteuid");
1065
 
  }
1066
 
  
1067
 
  if(quit_now){
1068
 
    goto end;
1069
 
  }
1070
 
  
1071
926
  {
1072
927
    struct argp_option options[] = {
1073
928
      { .name = "debug", .key = 128,
1102
957
        .arg = "SECONDS",
1103
958
        .doc = "Maximum delay to wait for interface startup",
1104
959
        .group = 2 },
1105
 
      /*
1106
 
       * These reproduce what we would get without ARGP_NO_HELP
1107
 
       */
1108
 
      { .name = "help", .key = '?',
1109
 
        .doc = "Give this help list", .group = -1 },
1110
 
      { .name = "usage", .key = -3,
1111
 
        .doc = "Give a short usage message", .group = -1 },
1112
 
      { .name = "version", .key = 'V',
1113
 
        .doc = "Print program version", .group = -1 },
1114
960
      { .name = NULL }
1115
961
    };
1116
962
    
1117
963
    error_t parse_opt(int key, char *arg,
1118
964
                      struct argp_state *state){
1119
 
      errno = 0;
1120
965
      switch(key){
1121
966
      case 128:                 /* --debug */
1122
967
        debug = true;
1138
983
        tmpmax = strtoimax(arg, &tmp, 10);
1139
984
        if(errno != 0 or tmp == arg or *tmp != '\0'
1140
985
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1141
 
          argp_error(state, "Bad number of DH bits");
 
986
          fprintf(stderr, "Bad number of DH bits\n");
 
987
          exit(EXIT_FAILURE);
1142
988
        }
1143
989
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1144
990
        break;
1149
995
        errno = 0;
1150
996
        delay = strtof(arg, &tmp);
1151
997
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1152
 
          argp_error(state, "Bad delay");
 
998
          fprintf(stderr, "Bad delay\n");
 
999
          exit(EXIT_FAILURE);
1153
1000
        }
1154
1001
        break;
1155
 
        /*
1156
 
         * These reproduce what we would get without ARGP_NO_HELP
1157
 
         */
1158
 
      case '?':                 /* --help */
1159
 
        argp_state_help(state, state->out_stream,
1160
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1161
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1162
 
      case -3:                  /* --usage */
1163
 
        argp_state_help(state, state->out_stream,
1164
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1165
 
      case 'V':                 /* --version */
1166
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1167
 
        exit(argp_err_exit_status);
 
1002
      case ARGP_KEY_ARG:
 
1003
        argp_usage(state);
 
1004
      case ARGP_KEY_END:
1168
1005
        break;
1169
1006
      default:
1170
1007
        return ARGP_ERR_UNKNOWN;
1171
1008
      }
1172
 
      return errno;
 
1009
      return 0;
1173
1010
    }
1174
1011
    
1175
1012
    struct argp argp = { .options = options, .parser = parse_opt,
1176
1013
                         .args_doc = "",
1177
1014
                         .doc = "Mandos client -- Get and decrypt"
1178
1015
                         " passwords from a Mandos server" };
1179
 
    ret = argp_parse(&argp, argc, argv,
1180
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1181
 
    switch(ret){
1182
 
    case 0:
1183
 
      break;
1184
 
    case ENOMEM:
1185
 
    default:
1186
 
      errno = ret;
1187
 
      perror("argp_parse");
1188
 
      exitcode = EX_OSERR;
1189
 
      goto end;
1190
 
    case EINVAL:
1191
 
      exitcode = EX_USAGE;
 
1016
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1017
    if(ret == ARGP_ERR_UNKNOWN){
 
1018
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1019
      exitcode = EXIT_FAILURE;
1192
1020
      goto end;
1193
1021
    }
1194
1022
  }
1204
1032
  mc.simple_poll = avahi_simple_poll_new();
1205
1033
  if(mc.simple_poll == NULL){
1206
1034
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1207
 
    exitcode = EX_UNAVAILABLE;
 
1035
    exitcode = EXIT_FAILURE;
1208
1036
    goto end;
1209
1037
  }
1210
1038
  
1212
1040
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1213
1041
  if(ret == -1){
1214
1042
    perror("sigaddset");
1215
 
    exitcode = EX_OSERR;
 
1043
    exitcode = EXIT_FAILURE;
1216
1044
    goto end;
1217
1045
  }
1218
1046
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1219
1047
  if(ret == -1){
1220
1048
    perror("sigaddset");
1221
 
    exitcode = EX_OSERR;
 
1049
    exitcode = EXIT_FAILURE;
1222
1050
    goto end;
1223
1051
  }
1224
1052
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1225
1053
  if(ret == -1){
1226
1054
    perror("sigaddset");
1227
 
    exitcode = EX_OSERR;
1228
 
    goto end;
1229
 
  }
1230
 
  /* Need to check if the handler is SIG_IGN before handling:
1231
 
     | [[info:libc:Initial Signal Actions]] |
1232
 
     | [[info:libc:Basic Signal Handling]]  |
1233
 
  */
1234
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1235
 
  if(ret == -1){
1236
 
    perror("sigaction");
1237
 
    return EX_OSERR;
1238
 
  }
1239
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1240
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1241
 
    if(ret == -1){
1242
 
      perror("sigaction");
1243
 
      exitcode = EX_OSERR;
1244
 
      goto end;
1245
 
    }
1246
 
  }
1247
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1248
 
  if(ret == -1){
1249
 
    perror("sigaction");
1250
 
    return EX_OSERR;
1251
 
  }
1252
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1253
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1254
 
    if(ret == -1){
1255
 
      perror("sigaction");
1256
 
      exitcode = EX_OSERR;
1257
 
      goto end;
1258
 
    }
1259
 
  }
1260
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1261
 
  if(ret == -1){
1262
 
    perror("sigaction");
1263
 
    return EX_OSERR;
1264
 
  }
1265
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1266
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1267
 
    if(ret == -1){
1268
 
      perror("sigaction");
1269
 
      exitcode = EX_OSERR;
1270
 
      goto end;
1271
 
    }
 
1055
    exitcode = EXIT_FAILURE;
 
1056
    goto end;
 
1057
  }
 
1058
  ret = sigaction(SIGINT, &sigterm_action, &old_sigterm_action);
 
1059
  if(ret == -1){
 
1060
    perror("sigaction");
 
1061
    exitcode = EXIT_FAILURE;
 
1062
    goto end;
 
1063
  }
 
1064
  ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1065
  if(ret == -1){
 
1066
    perror("sigaction");
 
1067
    exitcode = EXIT_FAILURE;
 
1068
    goto end;
 
1069
  }
 
1070
  ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1071
  if(ret == -1){
 
1072
    perror("sigaction");
 
1073
    exitcode = EXIT_FAILURE;
 
1074
    goto end;
1272
1075
  }
1273
1076
  
1274
1077
  /* If the interface is down, bring it up */
1276
1079
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1277
1080
    if(if_index == 0){
1278
1081
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1279
 
      exitcode = EX_UNAVAILABLE;
 
1082
      exitcode = EXIT_FAILURE;
1280
1083
      goto end;
1281
1084
    }
1282
1085
    
1284
1087
      goto end;
1285
1088
    }
1286
1089
    
1287
 
    /* Re-raise priviliges */
1288
 
    errno = 0;
1289
 
    ret = seteuid(0);
1290
 
    if(ret == -1){
1291
 
      perror("seteuid");
1292
 
    }
1293
 
    
1294
1090
#ifdef __linux__
1295
1091
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1296
 
       messages about the network interface to mess up the prompt */
 
1092
       messages to mess up the prompt */
1297
1093
    ret = klogctl(8, NULL, 5);
1298
1094
    bool restore_loglevel = true;
1299
1095
    if(ret == -1){
1305
1101
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1306
1102
    if(sd < 0){
1307
1103
      perror("socket");
1308
 
      exitcode = EX_OSERR;
 
1104
      exitcode = EXIT_FAILURE;
1309
1105
#ifdef __linux__
1310
1106
      if(restore_loglevel){
1311
1107
        ret = klogctl(7, NULL, 0);
1314
1110
        }
1315
1111
      }
1316
1112
#endif  /* __linux__ */
1317
 
      /* Lower privileges */
1318
 
      errno = 0;
1319
 
      ret = seteuid(uid);
1320
 
      if(ret == -1){
1321
 
        perror("seteuid");
1322
 
      }
1323
1113
      goto end;
1324
1114
    }
1325
1115
    strcpy(network.ifr_name, interface);
1334
1124
        }
1335
1125
      }
1336
1126
#endif  /* __linux__ */
1337
 
      exitcode = EX_OSERR;
1338
 
      /* Lower privileges */
1339
 
      errno = 0;
1340
 
      ret = seteuid(uid);
1341
 
      if(ret == -1){
1342
 
        perror("seteuid");
1343
 
      }
 
1127
      exitcode = EXIT_FAILURE;
1344
1128
      goto end;
1345
1129
    }
1346
1130
    if((network.ifr_flags & IFF_UP) == 0){
1350
1134
      if(ret == -1){
1351
1135
        take_down_interface = false;
1352
1136
        perror("ioctl SIOCSIFFLAGS");
1353
 
        exitcode = EX_OSERR;
 
1137
        exitcode = EXIT_FAILURE;
1354
1138
#ifdef __linux__
1355
1139
        if(restore_loglevel){
1356
1140
          ret = klogctl(7, NULL, 0);
1359
1143
          }
1360
1144
        }
1361
1145
#endif  /* __linux__ */
1362
 
        /* Lower privileges */
1363
 
        errno = 0;
1364
 
        ret = seteuid(uid);
1365
 
        if(ret == -1){
1366
 
          perror("seteuid");
1367
 
        }
1368
1146
        goto end;
1369
1147
      }
1370
1148
    }
1398
1176
      }
1399
1177
    }
1400
1178
#endif  /* __linux__ */
1401
 
    /* Lower privileges */
1402
 
    errno = 0;
1403
 
    if(take_down_interface){
1404
 
      /* Lower privileges */
1405
 
      ret = seteuid(uid);
1406
 
      if(ret == -1){
1407
 
        perror("seteuid");
1408
 
      }
1409
 
    } else {
1410
 
      /* Lower privileges permanently */
1411
 
      ret = setuid(uid);
1412
 
      if(ret == -1){
1413
 
        perror("setuid");
1414
 
      }
1415
 
    }
 
1179
  }
 
1180
  
 
1181
  if(quit_now){
 
1182
    goto end;
 
1183
  }
 
1184
  
 
1185
  uid = getuid();
 
1186
  gid = getgid();
 
1187
  
 
1188
  errno = 0;
 
1189
  setgid(gid);
 
1190
  if(ret == -1){
 
1191
    perror("setgid");
 
1192
  }
 
1193
  
 
1194
  ret = setuid(uid);
 
1195
  if(ret == -1){
 
1196
    perror("setuid");
1416
1197
  }
1417
1198
  
1418
1199
  if(quit_now){
1422
1203
  ret = init_gnutls_global(pubkey, seckey);
1423
1204
  if(ret == -1){
1424
1205
    fprintf(stderr, "init_gnutls_global failed\n");
1425
 
    exitcode = EX_UNAVAILABLE;
 
1206
    exitcode = EXIT_FAILURE;
1426
1207
    goto end;
1427
1208
  } else {
1428
1209
    gnutls_initialized = true;
1445
1226
  
1446
1227
  if(not init_gpgme(pubkey, seckey, tempdir)){
1447
1228
    fprintf(stderr, "init_gpgme failed\n");
1448
 
    exitcode = EX_UNAVAILABLE;
 
1229
    exitcode = EXIT_FAILURE;
1449
1230
    goto end;
1450
1231
  } else {
1451
1232
    gpgme_initialized = true;
1461
1242
    char *address = strrchr(connect_to, ':');
1462
1243
    if(address == NULL){
1463
1244
      fprintf(stderr, "No colon in address\n");
1464
 
      exitcode = EX_USAGE;
 
1245
      exitcode = EXIT_FAILURE;
1465
1246
      goto end;
1466
1247
    }
1467
1248
    
1475
1256
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1476
1257
       or tmpmax != (uint16_t)tmpmax){
1477
1258
      fprintf(stderr, "Bad port number\n");
1478
 
      exitcode = EX_USAGE;
 
1259
      exitcode = EXIT_FAILURE;
1479
1260
      goto end;
1480
1261
    }
1481
1262
  
1500
1281
    
1501
1282
    ret = start_mandos_communication(address, port, if_index, af);
1502
1283
    if(ret < 0){
1503
 
      switch(errno){
1504
 
      case ENETUNREACH:
1505
 
      case EHOSTDOWN:
1506
 
      case EHOSTUNREACH:
1507
 
        exitcode = EX_NOHOST;
1508
 
        break;
1509
 
      case EINVAL:
1510
 
        exitcode = EX_USAGE;
1511
 
        break;
1512
 
      case EIO:
1513
 
        exitcode = EX_IOERR;
1514
 
        break;
1515
 
      case EPROTO:
1516
 
        exitcode = EX_PROTOCOL;
1517
 
        break;
1518
 
      default:
1519
 
        exitcode = EX_OSERR;
1520
 
        break;
1521
 
      }
 
1284
      exitcode = EXIT_FAILURE;
1522
1285
    } else {
1523
1286
      exitcode = EXIT_SUCCESS;
1524
1287
    }
1551
1314
  if(mc.server == NULL){
1552
1315
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1553
1316
            avahi_strerror(error));
1554
 
    exitcode = EX_UNAVAILABLE;
 
1317
    exitcode = EXIT_FAILURE;
1555
1318
    goto end;
1556
1319
  }
1557
1320
  
1566
1329
  if(sb == NULL){
1567
1330
    fprintf(stderr, "Failed to create service browser: %s\n",
1568
1331
            avahi_strerror(avahi_server_errno(mc.server)));
1569
 
    exitcode = EX_UNAVAILABLE;
 
1332
    exitcode = EXIT_FAILURE;
1570
1333
    goto end;
1571
1334
  }
1572
1335
  
1610
1373
  
1611
1374
  /* Take down the network interface */
1612
1375
  if(take_down_interface){
1613
 
    /* Re-raise priviliges */
1614
 
    errno = 0;
1615
 
    ret = seteuid(0);
 
1376
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1616
1377
    if(ret == -1){
1617
 
      perror("seteuid");
 
1378
      perror("ioctl SIOCGIFFLAGS");
 
1379
    } else if(network.ifr_flags & IFF_UP) {
 
1380
      network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1381
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1382
      if(ret == -1){
 
1383
        perror("ioctl SIOCSIFFLAGS");
 
1384
      }
1618
1385
    }
1619
 
    if(geteuid() == 0){
1620
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1621
 
      if(ret == -1){
1622
 
        perror("ioctl SIOCGIFFLAGS");
1623
 
      } else if(network.ifr_flags & IFF_UP) {
1624
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1625
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1626
 
        if(ret == -1){
1627
 
          perror("ioctl SIOCSIFFLAGS");
1628
 
        }
1629
 
      }
1630
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1631
 
      if(ret == -1){
1632
 
        perror("close");
1633
 
      }
1634
 
      /* Lower privileges permanently */
1635
 
      errno = 0;
1636
 
      ret = setuid(uid);
1637
 
      if(ret == -1){
1638
 
        perror("setuid");
1639
 
      }
 
1386
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1387
    if(ret == -1){
 
1388
      perror("close");
1640
1389
    }
1641
1390
  }
1642
1391
  
1685
1434
  }
1686
1435
  
1687
1436
  if(quit_now){
1688
 
    sigemptyset(&old_sigterm_action.sa_mask);
1689
 
    old_sigterm_action.sa_handler = SIG_DFL;
1690
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1691
 
                                            &old_sigterm_action,
1692
 
                                            NULL));
 
1437
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1693
1438
    if(ret == -1){
1694
1439
      perror("sigaction");
1695
1440
    }
1696
 
    do {
1697
 
      ret = raise(signal_received);
1698
 
    } while(ret != 0 and errno == EINTR);
1699
 
    if(ret != 0){
1700
 
      perror("raise");
1701
 
      abort();
1702
 
    }
1703
 
    TEMP_FAILURE_RETRY(pause());
 
1441
    raise(signal_received);
1704
1442
  }
1705
1443
  
1706
1444
  return exitcode;