/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

  • Committer: teddy at bsnet
  • Date: 2010-09-10 17:06:26 UTC
  • mto: (237.2.190 mandos)
  • mto: This revision was merged to the branch mainline in revision 270.
  • Revision ID: teddy@fukt.bsnet.se-20100910170626-exo8e7ptkb9ncg29
* Makefile (install-server): Install dbus-mandos.conf as
                             "/etc/dbus-1/system.d/mandos.conf".
  (purge-server): Remove "/etc/dbus-1/system.d/mandos.conf".

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, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
47
                                   strtof(), abort() */
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(), setuid(),
75
 
                                   setgid() */
 
74
                                   getuid(), getgid(), seteuid(),
 
75
                                   setgid(), pause() */
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 */
85
87
 
86
88
#ifdef __linux__
87
89
#include <sys/klog.h>           /* klogctl() */
125
127
static const char mandos_protocol_version[] = "1";
126
128
const char *argp_program_version = "mandos-client " VERSION;
127
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
130
static const char sys_class_net[] = "/sys/class/net";
 
131
char *connect_to = NULL;
128
132
 
129
133
/* Used for passing in values through the Avahi callback functions */
130
134
typedef struct {
142
146
                      .dh_bits = 1024, .priority = "SECURE256"
143
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
148
 
 
149
sig_atomic_t quit_now = 0;
 
150
int signal_received = 0;
 
151
 
145
152
/*
146
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
154
 * bytes. "buffer_capacity" is how much is currently allocated,
474
481
static int init_gnutls_session(gnutls_session_t *session){
475
482
  int ret;
476
483
  /* GnuTLS session creation */
477
 
  ret = gnutls_init(session, GNUTLS_SERVER);
 
484
  do {
 
485
    ret = gnutls_init(session, GNUTLS_SERVER);
 
486
    if(quit_now){
 
487
      return -1;
 
488
    }
 
489
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
478
490
  if(ret != GNUTLS_E_SUCCESS){
479
491
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
480
492
            safer_gnutls_strerror(ret));
482
494
  
483
495
  {
484
496
    const char *err;
485
 
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
497
    do {
 
498
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
499
      if(quit_now){
 
500
        gnutls_deinit(*session);
 
501
        return -1;
 
502
      }
 
503
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
486
504
    if(ret != GNUTLS_E_SUCCESS){
487
505
      fprintf(stderr, "Syntax error at: %s\n", err);
488
506
      fprintf(stderr, "GnuTLS error: %s\n",
492
510
    }
493
511
  }
494
512
  
495
 
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
496
 
                               mc.cred);
 
513
  do {
 
514
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
515
                                 mc.cred);
 
516
    if(quit_now){
 
517
      gnutls_deinit(*session);
 
518
      return -1;
 
519
    }
 
520
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
497
521
  if(ret != GNUTLS_E_SUCCESS){
498
522
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
499
523
            safer_gnutls_strerror(ret));
502
526
  }
503
527
  
504
528
  /* ignore client certificate if any. */
505
 
  gnutls_certificate_server_set_request(*session,
506
 
                                        GNUTLS_CERT_IGNORE);
 
529
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
507
530
  
508
531
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
509
532
  
514
537
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
538
                      __attribute__((unused)) const char *txt){}
516
539
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
540
/* Called when a Mandos server is found */
521
541
static int start_mandos_communication(const char *ip, uint16_t port,
522
542
                                      AvahiIfIndex if_index,
528
548
    struct sockaddr_in6 in6;
529
549
  } to;
530
550
  char *buffer = NULL;
531
 
  char *decrypted_buffer;
 
551
  char *decrypted_buffer = NULL;
532
552
  size_t buffer_length = 0;
533
553
  size_t buffer_capacity = 0;
534
554
  size_t written;
535
 
  int retval = 0;
 
555
  int retval = -1;
536
556
  gnutls_session_t session;
537
557
  int pf;                       /* Protocol family */
538
558
  
 
559
  errno = 0;
 
560
  
539
561
  if(quit_now){
 
562
    errno = EINTR;
540
563
    return -1;
541
564
  }
542
565
  
549
572
    break;
550
573
  default:
551
574
    fprintf(stderr, "Bad address family: %d\n", af);
 
575
    errno = EINVAL;
552
576
    return -1;
553
577
  }
554
578
  
564
588
  
565
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
566
590
  if(tcp_sd < 0){
 
591
    int e = errno;
567
592
    perror("socket");
568
 
    retval = -1;
 
593
    errno = e;
569
594
    goto mandos_end;
570
595
  }
571
596
  
572
597
  if(quit_now){
 
598
    errno = EINTR;
573
599
    goto mandos_end;
574
600
  }
575
601
  
582
608
    ret = inet_pton(af, ip, &to.in.sin_addr);
583
609
  }
584
610
  if(ret < 0 ){
 
611
    int e = errno;
585
612
    perror("inet_pton");
586
 
    retval = -1;
 
613
    errno = e;
587
614
    goto mandos_end;
588
615
  }
589
616
  if(ret == 0){
 
617
    int e = errno;
590
618
    fprintf(stderr, "Bad address: %s\n", ip);
591
 
    retval = -1;
 
619
    errno = e;
592
620
    goto mandos_end;
593
621
  }
594
622
  if(af == AF_INET6){
602
630
      if(if_index == AVAHI_IF_UNSPEC){
603
631
        fprintf(stderr, "An IPv6 link-local address is incomplete"
604
632
                " without a network interface\n");
605
 
        retval = -1;
 
633
        errno = EINVAL;
606
634
        goto mandos_end;
607
635
      }
608
636
      /* Set the network interface number as scope */
615
643
  }
616
644
  
617
645
  if(quit_now){
 
646
    errno = EINTR;
618
647
    goto mandos_end;
619
648
  }
620
649
  
651
680
  }
652
681
  
653
682
  if(quit_now){
 
683
    errno = EINTR;
654
684
    goto mandos_end;
655
685
  }
656
686
  
660
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
661
691
  }
662
692
  if(ret < 0){
 
693
    int e = errno;
663
694
    perror("connect");
664
 
    retval = -1;
 
695
    errno = e;
665
696
    goto mandos_end;
666
697
  }
667
698
  
668
699
  if(quit_now){
 
700
    errno = EINTR;
669
701
    goto mandos_end;
670
702
  }
671
703
  
676
708
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
677
709
                                   out_size - written));
678
710
    if(ret == -1){
 
711
      int e = errno;
679
712
      perror("write");
680
 
      retval = -1;
 
713
      errno = e;
681
714
      goto mandos_end;
682
715
    }
683
716
    written += (size_t)ret;
693
726
    }
694
727
  
695
728
    if(quit_now){
 
729
      errno = EINTR;
696
730
      goto mandos_end;
697
731
    }
698
732
  }
702
736
  }
703
737
  
704
738
  if(quit_now){
 
739
    errno = EINTR;
705
740
    goto mandos_end;
706
741
  }
707
742
  
708
743
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
709
744
  
710
745
  if(quit_now){
 
746
    errno = EINTR;
711
747
    goto mandos_end;
712
748
  }
713
749
  
714
750
  do {
715
751
    ret = gnutls_handshake(session);
716
752
    if(quit_now){
 
753
      errno = EINTR;
717
754
      goto mandos_end;
718
755
    }
719
756
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
723
760
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
724
761
      gnutls_perror(ret);
725
762
    }
726
 
    retval = -1;
 
763
    errno = EPROTO;
727
764
    goto mandos_end;
728
765
  }
729
766
  
737
774
  while(true){
738
775
    
739
776
    if(quit_now){
 
777
      errno = EINTR;
740
778
      goto mandos_end;
741
779
    }
742
780
    
743
781
    buffer_capacity = incbuffer(&buffer, buffer_length,
744
782
                                   buffer_capacity);
745
783
    if(buffer_capacity == 0){
 
784
      int e = errno;
746
785
      perror("incbuffer");
747
 
      retval = -1;
 
786
      errno = e;
748
787
      goto mandos_end;
749
788
    }
750
789
    
751
790
    if(quit_now){
 
791
      errno = EINTR;
752
792
      goto mandos_end;
753
793
    }
754
794
    
767
807
          ret = gnutls_handshake(session);
768
808
          
769
809
          if(quit_now){
 
810
            errno = EINTR;
770
811
            goto mandos_end;
771
812
          }
772
813
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
773
814
        if(ret < 0){
774
815
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
775
816
          gnutls_perror(ret);
776
 
          retval = -1;
 
817
          errno = EPROTO;
777
818
          goto mandos_end;
778
819
        }
779
820
        break;
780
821
      default:
781
822
        fprintf(stderr, "Unknown error while reading data from"
782
823
                " encrypted session with Mandos server\n");
783
 
        retval = -1;
784
824
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
825
        errno = EIO;
785
826
        goto mandos_end;
786
827
      }
787
828
    } else {
794
835
  }
795
836
  
796
837
  if(quit_now){
797
 
    goto mandos_end;
798
 
  }
799
 
  
800
 
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
801
 
  
802
 
  if(quit_now){
803
 
    goto mandos_end;
804
 
  }
 
838
    errno = EINTR;
 
839
    goto mandos_end;
 
840
  }
 
841
  
 
842
  do {
 
843
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
844
    if(quit_now){
 
845
      errno = EINTR;
 
846
      goto mandos_end;
 
847
    }
 
848
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
805
849
  
806
850
  if(buffer_length > 0){
807
851
    ssize_t decrypted_buffer_size;
813
857
      written = 0;
814
858
      while(written < (size_t) decrypted_buffer_size){
815
859
        if(quit_now){
 
860
          errno = EINTR;
816
861
          goto mandos_end;
817
862
        }
818
863
        
820
865
                          (size_t)decrypted_buffer_size - written,
821
866
                          stdout);
822
867
        if(ret == 0 and ferror(stdout)){
 
868
          int e = errno;
823
869
          if(debug){
824
870
            fprintf(stderr, "Error writing encrypted data: %s\n",
825
871
                    strerror(errno));
826
872
          }
827
 
          retval = -1;
828
 
          break;
 
873
          errno = e;
 
874
          goto mandos_end;
829
875
        }
830
876
        written += (size_t)ret;
831
877
      }
832
 
      free(decrypted_buffer);
833
 
    } else {
834
 
      retval = -1;
 
878
      retval = 0;
835
879
    }
836
 
  } else {
837
 
    retval = -1;
838
880
  }
839
881
  
840
882
  /* Shutdown procedure */
841
883
  
842
884
 mandos_end:
843
 
  free(buffer);
844
 
  if(tcp_sd >= 0){
845
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
 
  }
847
 
  if(ret == -1){
848
 
    perror("close");
849
 
  }
850
 
  gnutls_deinit(session);
851
 
  if(quit_now){
852
 
    retval = -1;
 
885
  {
 
886
    int e = errno;
 
887
    free(decrypted_buffer);
 
888
    free(buffer);
 
889
    if(tcp_sd >= 0){
 
890
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
891
    }
 
892
    if(ret == -1){
 
893
      if(e == 0){
 
894
        e = errno;
 
895
      }
 
896
      perror("close");
 
897
    }
 
898
    gnutls_deinit(session);
 
899
    if(quit_now){
 
900
      e = EINTR;
 
901
      retval = -1;
 
902
    }
 
903
    errno = e;
853
904
  }
854
905
  return retval;
855
906
}
971
1022
  errno = old_errno;
972
1023
}
973
1024
 
 
1025
/* 
 
1026
 * This function determines if a directory entry in /sys/class/net
 
1027
 * corresponds to an acceptable network device.
 
1028
 * (This function is passed to scandir(3) as a filter function.)
 
1029
 */
 
1030
int good_interface(const struct dirent *if_entry){
 
1031
  ssize_t ssret;
 
1032
  char *flagname = NULL;
 
1033
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
1034
                     if_entry->d_name);
 
1035
  if(ret < 0){
 
1036
    perror("asprintf");
 
1037
    return 0;
 
1038
  }
 
1039
  if(if_entry->d_name[0] == '.'){
 
1040
    return 0;
 
1041
  }
 
1042
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
 
1043
  if(flags_fd == -1){
 
1044
    perror("open");
 
1045
    return 0;
 
1046
  }
 
1047
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
 
1048
  /* read line from flags_fd */
 
1049
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1050
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1051
  flagstring[(size_t)to_read] = '\0';
 
1052
  if(flagstring == NULL){
 
1053
    perror("malloc");
 
1054
    close(flags_fd);
 
1055
    return 0;
 
1056
  }
 
1057
  while(to_read > 0){
 
1058
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1059
                                             (size_t)to_read));
 
1060
    if(ssret == -1){
 
1061
      perror("read");
 
1062
      free(flagstring);
 
1063
      close(flags_fd);
 
1064
      return 0;
 
1065
    }
 
1066
    to_read -= ssret;
 
1067
    if(ssret == 0){
 
1068
      break;
 
1069
    }
 
1070
  }
 
1071
  close(flags_fd);
 
1072
  intmax_t tmpmax;
 
1073
  char *tmp;
 
1074
  errno = 0;
 
1075
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1076
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1077
                                         and not (isspace(*tmp)))
 
1078
     or tmpmax != (ifreq_flags)tmpmax){
 
1079
    if(debug){
 
1080
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1081
              flagstring, if_entry->d_name);
 
1082
    }
 
1083
    free(flagstring);
 
1084
    return 0;
 
1085
  }
 
1086
  free(flagstring);
 
1087
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1088
  /* Reject the loopback device */
 
1089
  if(flags & IFF_LOOPBACK){
 
1090
    if(debug){
 
1091
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1092
              if_entry->d_name);
 
1093
    }
 
1094
    return 0;
 
1095
  }
 
1096
  /* Accept point-to-point devices only if connect_to is specified */
 
1097
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1098
    if(debug){
 
1099
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1100
              if_entry->d_name);
 
1101
    }
 
1102
    return 1;
 
1103
  }
 
1104
  /* Otherwise, reject non-broadcast-capable devices */
 
1105
  if(not (flags & IFF_BROADCAST)){
 
1106
    if(debug){
 
1107
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1108
              if_entry->d_name);
 
1109
    }
 
1110
    return 0;
 
1111
  }
 
1112
  /* Accept this device */
 
1113
  if(debug){
 
1114
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1115
            if_entry->d_name);
 
1116
  }
 
1117
  return 1;
 
1118
}
 
1119
 
974
1120
int main(int argc, char *argv[]){
975
1121
  AvahiSServiceBrowser *sb = NULL;
976
1122
  int error;
978
1124
  intmax_t tmpmax;
979
1125
  char *tmp;
980
1126
  int exitcode = EXIT_SUCCESS;
981
 
  const char *interface = "eth0";
 
1127
  const char *interface = "";
982
1128
  struct ifreq network;
983
1129
  int sd = -1;
984
1130
  bool take_down_interface = false;
985
1131
  uid_t uid;
986
1132
  gid_t gid;
987
 
  char *connect_to = NULL;
988
1133
  char tempdir[] = "/tmp/mandosXXXXXX";
989
1134
  bool tempdir_created = false;
990
1135
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
995
1140
  bool gpgme_initialized = false;
996
1141
  float delay = 2.5f;
997
1142
  
998
 
  struct sigaction old_sigterm_action;
 
1143
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
999
1144
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1000
1145
  
 
1146
  uid = getuid();
 
1147
  gid = getgid();
 
1148
  
 
1149
  /* Lower any group privileges we might have, just to be safe */
 
1150
  errno = 0;
 
1151
  ret = setgid(gid);
 
1152
  if(ret == -1){
 
1153
    perror("setgid");
 
1154
  }
 
1155
  
 
1156
  /* Lower user privileges (temporarily) */
 
1157
  errno = 0;
 
1158
  ret = seteuid(uid);
 
1159
  if(ret == -1){
 
1160
    perror("seteuid");
 
1161
  }
 
1162
  
 
1163
  if(quit_now){
 
1164
    goto end;
 
1165
  }
 
1166
  
1001
1167
  {
1002
1168
    struct argp_option options[] = {
1003
1169
      { .name = "debug", .key = 128,
1032
1198
        .arg = "SECONDS",
1033
1199
        .doc = "Maximum delay to wait for interface startup",
1034
1200
        .group = 2 },
 
1201
      /*
 
1202
       * These reproduce what we would get without ARGP_NO_HELP
 
1203
       */
 
1204
      { .name = "help", .key = '?',
 
1205
        .doc = "Give this help list", .group = -1 },
 
1206
      { .name = "usage", .key = -3,
 
1207
        .doc = "Give a short usage message", .group = -1 },
 
1208
      { .name = "version", .key = 'V',
 
1209
        .doc = "Print program version", .group = -1 },
1035
1210
      { .name = NULL }
1036
1211
    };
1037
1212
    
1038
1213
    error_t parse_opt(int key, char *arg,
1039
1214
                      struct argp_state *state){
 
1215
      errno = 0;
1040
1216
      switch(key){
1041
1217
      case 128:                 /* --debug */
1042
1218
        debug = true;
1058
1234
        tmpmax = strtoimax(arg, &tmp, 10);
1059
1235
        if(errno != 0 or tmp == arg or *tmp != '\0'
1060
1236
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1061
 
          fprintf(stderr, "Bad number of DH bits\n");
1062
 
          exit(EXIT_FAILURE);
 
1237
          argp_error(state, "Bad number of DH bits");
1063
1238
        }
1064
1239
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1065
1240
        break;
1070
1245
        errno = 0;
1071
1246
        delay = strtof(arg, &tmp);
1072
1247
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1073
 
          fprintf(stderr, "Bad delay\n");
1074
 
          exit(EXIT_FAILURE);
 
1248
          argp_error(state, "Bad delay");
1075
1249
        }
1076
1250
        break;
1077
 
      case ARGP_KEY_ARG:
1078
 
        argp_usage(state);
1079
 
      case ARGP_KEY_END:
 
1251
        /*
 
1252
         * These reproduce what we would get without ARGP_NO_HELP
 
1253
         */
 
1254
      case '?':                 /* --help */
 
1255
        argp_state_help(state, state->out_stream,
 
1256
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1257
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1258
      case -3:                  /* --usage */
 
1259
        argp_state_help(state, state->out_stream,
 
1260
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1261
      case 'V':                 /* --version */
 
1262
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1263
        exit(argp_err_exit_status);
1080
1264
        break;
1081
1265
      default:
1082
1266
        return ARGP_ERR_UNKNOWN;
1083
1267
      }
1084
 
      return 0;
 
1268
      return errno;
1085
1269
    }
1086
1270
    
1087
1271
    struct argp argp = { .options = options, .parser = parse_opt,
1088
1272
                         .args_doc = "",
1089
1273
                         .doc = "Mandos client -- Get and decrypt"
1090
1274
                         " passwords from a Mandos server" };
1091
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1092
 
    if(ret == ARGP_ERR_UNKNOWN){
1093
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
1094
 
      exitcode = EXIT_FAILURE;
 
1275
    ret = argp_parse(&argp, argc, argv,
 
1276
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
1277
    switch(ret){
 
1278
    case 0:
 
1279
      break;
 
1280
    case ENOMEM:
 
1281
    default:
 
1282
      errno = ret;
 
1283
      perror("argp_parse");
 
1284
      exitcode = EX_OSERR;
 
1285
      goto end;
 
1286
    case EINVAL:
 
1287
      exitcode = EX_USAGE;
1095
1288
      goto end;
1096
1289
    }
1097
1290
  }
1099
1292
  if(not debug){
1100
1293
    avahi_set_log_function(empty_log);
1101
1294
  }
 
1295
 
 
1296
  if(interface[0] == '\0'){
 
1297
    struct dirent **direntries;
 
1298
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1299
                  alphasort);
 
1300
    if(ret >= 1){
 
1301
      /* Pick the first good interface */
 
1302
      interface = strdup(direntries[0]->d_name);
 
1303
      if(debug){
 
1304
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1305
      }
 
1306
      if(interface == NULL){
 
1307
        perror("malloc");
 
1308
        free(direntries);
 
1309
        exitcode = EXIT_FAILURE;
 
1310
        goto end;
 
1311
      }
 
1312
      free(direntries);
 
1313
    } else {
 
1314
      free(direntries);
 
1315
      fprintf(stderr, "Could not find a network interface\n");
 
1316
      exitcode = EXIT_FAILURE;
 
1317
      goto end;
 
1318
    }
 
1319
  }
1102
1320
  
1103
1321
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1104
1322
     from the signal handler */
1107
1325
  mc.simple_poll = avahi_simple_poll_new();
1108
1326
  if(mc.simple_poll == NULL){
1109
1327
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1110
 
    exitcode = EXIT_FAILURE;
 
1328
    exitcode = EX_UNAVAILABLE;
1111
1329
    goto end;
1112
1330
  }
1113
1331
  
1115
1333
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1116
1334
  if(ret == -1){
1117
1335
    perror("sigaddset");
1118
 
    exitcode = EXIT_FAILURE;
 
1336
    exitcode = EX_OSERR;
1119
1337
    goto end;
1120
1338
  }
1121
1339
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1122
1340
  if(ret == -1){
1123
1341
    perror("sigaddset");
1124
 
    exitcode = EXIT_FAILURE;
 
1342
    exitcode = EX_OSERR;
1125
1343
    goto end;
1126
1344
  }
1127
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1128
1346
  if(ret == -1){
1129
1347
    perror("sigaddset");
1130
 
    exitcode = EXIT_FAILURE;
 
1348
    exitcode = EX_OSERR;
1131
1349
    goto end;
1132
1350
  }
1133
1351
  /* Need to check if the handler is SIG_IGN before handling:
1137
1355
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1138
1356
  if(ret == -1){
1139
1357
    perror("sigaction");
1140
 
    return EXIT_FAILURE;
 
1358
    return EX_OSERR;
1141
1359
  }
1142
1360
  if(old_sigterm_action.sa_handler != SIG_IGN){
1143
1361
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1144
1362
    if(ret == -1){
1145
1363
      perror("sigaction");
1146
 
      exitcode = EXIT_FAILURE;
 
1364
      exitcode = EX_OSERR;
1147
1365
      goto end;
1148
1366
    }
1149
1367
  }
1150
1368
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1151
1369
  if(ret == -1){
1152
1370
    perror("sigaction");
1153
 
    return EXIT_FAILURE;
 
1371
    return EX_OSERR;
1154
1372
  }
1155
1373
  if(old_sigterm_action.sa_handler != SIG_IGN){
1156
1374
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1157
1375
    if(ret == -1){
1158
1376
      perror("sigaction");
1159
 
      exitcode = EXIT_FAILURE;
 
1377
      exitcode = EX_OSERR;
1160
1378
      goto end;
1161
1379
    }
1162
1380
  }
1163
1381
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1164
1382
  if(ret == -1){
1165
1383
    perror("sigaction");
1166
 
    return EXIT_FAILURE;
 
1384
    return EX_OSERR;
1167
1385
  }
1168
1386
  if(old_sigterm_action.sa_handler != SIG_IGN){
1169
1387
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1170
1388
    if(ret == -1){
1171
1389
      perror("sigaction");
1172
 
      exitcode = EXIT_FAILURE;
 
1390
      exitcode = EX_OSERR;
1173
1391
      goto end;
1174
1392
    }
1175
1393
  }
1176
1394
  
1177
1395
  /* If the interface is down, bring it up */
1178
 
  if(interface[0] != '\0'){
 
1396
  if(strcmp(interface, "none") != 0){
1179
1397
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1180
1398
    if(if_index == 0){
1181
1399
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1182
 
      exitcode = EXIT_FAILURE;
 
1400
      exitcode = EX_UNAVAILABLE;
1183
1401
      goto end;
1184
1402
    }
1185
1403
    
1187
1405
      goto end;
1188
1406
    }
1189
1407
    
 
1408
    /* Re-raise priviliges */
 
1409
    errno = 0;
 
1410
    ret = seteuid(0);
 
1411
    if(ret == -1){
 
1412
      perror("seteuid");
 
1413
    }
 
1414
    
1190
1415
#ifdef __linux__
1191
1416
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
 
       messages to mess up the prompt */
 
1417
       messages about the network interface to mess up the prompt */
1193
1418
    ret = klogctl(8, NULL, 5);
1194
1419
    bool restore_loglevel = true;
1195
1420
    if(ret == -1){
1201
1426
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1202
1427
    if(sd < 0){
1203
1428
      perror("socket");
1204
 
      exitcode = EXIT_FAILURE;
 
1429
      exitcode = EX_OSERR;
1205
1430
#ifdef __linux__
1206
1431
      if(restore_loglevel){
1207
1432
        ret = klogctl(7, NULL, 0);
1210
1435
        }
1211
1436
      }
1212
1437
#endif  /* __linux__ */
 
1438
      /* Lower privileges */
 
1439
      errno = 0;
 
1440
      ret = seteuid(uid);
 
1441
      if(ret == -1){
 
1442
        perror("seteuid");
 
1443
      }
1213
1444
      goto end;
1214
1445
    }
1215
1446
    strcpy(network.ifr_name, interface);
1224
1455
        }
1225
1456
      }
1226
1457
#endif  /* __linux__ */
1227
 
      exitcode = EXIT_FAILURE;
 
1458
      exitcode = EX_OSERR;
 
1459
      /* Lower privileges */
 
1460
      errno = 0;
 
1461
      ret = seteuid(uid);
 
1462
      if(ret == -1){
 
1463
        perror("seteuid");
 
1464
      }
1228
1465
      goto end;
1229
1466
    }
1230
1467
    if((network.ifr_flags & IFF_UP) == 0){
1233
1470
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
1471
      if(ret == -1){
1235
1472
        take_down_interface = false;
1236
 
        perror("ioctl SIOCSIFFLAGS");
1237
 
        exitcode = EXIT_FAILURE;
 
1473
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1474
        exitcode = EX_OSERR;
1238
1475
#ifdef __linux__
1239
1476
        if(restore_loglevel){
1240
1477
          ret = klogctl(7, NULL, 0);
1243
1480
          }
1244
1481
        }
1245
1482
#endif  /* __linux__ */
 
1483
        /* Lower privileges */
 
1484
        errno = 0;
 
1485
        ret = seteuid(uid);
 
1486
        if(ret == -1){
 
1487
          perror("seteuid");
 
1488
        }
1246
1489
        goto end;
1247
1490
      }
1248
1491
    }
1276
1519
      }
1277
1520
    }
1278
1521
#endif  /* __linux__ */
1279
 
  }
1280
 
  
1281
 
  if(quit_now){
1282
 
    goto end;
1283
 
  }
1284
 
  
1285
 
  uid = getuid();
1286
 
  gid = getgid();
1287
 
  
1288
 
  errno = 0;
1289
 
  setgid(gid);
1290
 
  if(ret == -1){
1291
 
    perror("setgid");
1292
 
  }
1293
 
  
1294
 
  ret = setuid(uid);
1295
 
  if(ret == -1){
1296
 
    perror("setuid");
 
1522
    /* Lower privileges */
 
1523
    errno = 0;
 
1524
    if(take_down_interface){
 
1525
      /* Lower privileges */
 
1526
      ret = seteuid(uid);
 
1527
      if(ret == -1){
 
1528
        perror("seteuid");
 
1529
      }
 
1530
    } else {
 
1531
      /* Lower privileges permanently */
 
1532
      ret = setuid(uid);
 
1533
      if(ret == -1){
 
1534
        perror("setuid");
 
1535
      }
 
1536
    }
1297
1537
  }
1298
1538
  
1299
1539
  if(quit_now){
1303
1543
  ret = init_gnutls_global(pubkey, seckey);
1304
1544
  if(ret == -1){
1305
1545
    fprintf(stderr, "init_gnutls_global failed\n");
1306
 
    exitcode = EXIT_FAILURE;
 
1546
    exitcode = EX_UNAVAILABLE;
1307
1547
    goto end;
1308
1548
  } else {
1309
1549
    gnutls_initialized = true;
1326
1566
  
1327
1567
  if(not init_gpgme(pubkey, seckey, tempdir)){
1328
1568
    fprintf(stderr, "init_gpgme failed\n");
1329
 
    exitcode = EXIT_FAILURE;
 
1569
    exitcode = EX_UNAVAILABLE;
1330
1570
    goto end;
1331
1571
  } else {
1332
1572
    gpgme_initialized = true;
1342
1582
    char *address = strrchr(connect_to, ':');
1343
1583
    if(address == NULL){
1344
1584
      fprintf(stderr, "No colon in address\n");
1345
 
      exitcode = EXIT_FAILURE;
 
1585
      exitcode = EX_USAGE;
1346
1586
      goto end;
1347
1587
    }
1348
1588
    
1356
1596
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1357
1597
       or tmpmax != (uint16_t)tmpmax){
1358
1598
      fprintf(stderr, "Bad port number\n");
1359
 
      exitcode = EXIT_FAILURE;
 
1599
      exitcode = EX_USAGE;
1360
1600
      goto end;
1361
1601
    }
1362
1602
  
1381
1621
    
1382
1622
    ret = start_mandos_communication(address, port, if_index, af);
1383
1623
    if(ret < 0){
1384
 
      exitcode = EXIT_FAILURE;
 
1624
      switch(errno){
 
1625
      case ENETUNREACH:
 
1626
      case EHOSTDOWN:
 
1627
      case EHOSTUNREACH:
 
1628
        exitcode = EX_NOHOST;
 
1629
        break;
 
1630
      case EINVAL:
 
1631
        exitcode = EX_USAGE;
 
1632
        break;
 
1633
      case EIO:
 
1634
        exitcode = EX_IOERR;
 
1635
        break;
 
1636
      case EPROTO:
 
1637
        exitcode = EX_PROTOCOL;
 
1638
        break;
 
1639
      default:
 
1640
        exitcode = EX_OSERR;
 
1641
        break;
 
1642
      }
1385
1643
    } else {
1386
1644
      exitcode = EXIT_SUCCESS;
1387
1645
    }
1414
1672
  if(mc.server == NULL){
1415
1673
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1416
1674
            avahi_strerror(error));
1417
 
    exitcode = EXIT_FAILURE;
 
1675
    exitcode = EX_UNAVAILABLE;
1418
1676
    goto end;
1419
1677
  }
1420
1678
  
1429
1687
  if(sb == NULL){
1430
1688
    fprintf(stderr, "Failed to create service browser: %s\n",
1431
1689
            avahi_strerror(avahi_server_errno(mc.server)));
1432
 
    exitcode = EXIT_FAILURE;
 
1690
    exitcode = EX_UNAVAILABLE;
1433
1691
    goto end;
1434
1692
  }
1435
1693
  
1473
1731
  
1474
1732
  /* Take down the network interface */
1475
1733
  if(take_down_interface){
1476
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1734
    /* Re-raise priviliges */
 
1735
    errno = 0;
 
1736
    ret = seteuid(0);
1477
1737
    if(ret == -1){
1478
 
      perror("ioctl SIOCGIFFLAGS");
1479
 
    } else if(network.ifr_flags & IFF_UP) {
1480
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1481
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1482
 
      if(ret == -1){
1483
 
        perror("ioctl SIOCSIFFLAGS");
1484
 
      }
 
1738
      perror("seteuid");
1485
1739
    }
1486
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1487
 
    if(ret == -1){
1488
 
      perror("close");
 
1740
    if(geteuid() == 0){
 
1741
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1742
      if(ret == -1){
 
1743
        perror("ioctl SIOCGIFFLAGS");
 
1744
      } else if(network.ifr_flags & IFF_UP) {
 
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1747
        if(ret == -1){
 
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1749
        }
 
1750
      }
 
1751
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1752
      if(ret == -1){
 
1753
        perror("close");
 
1754
      }
 
1755
      /* Lower privileges permanently */
 
1756
      errno = 0;
 
1757
      ret = setuid(uid);
 
1758
      if(ret == -1){
 
1759
        perror("setuid");
 
1760
      }
1489
1761
    }
1490
1762
  }
1491
1763
  
1536
1808
  if(quit_now){
1537
1809
    sigemptyset(&old_sigterm_action.sa_mask);
1538
1810
    old_sigterm_action.sa_handler = SIG_DFL;
1539
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1811
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
1812
                                            &old_sigterm_action,
 
1813
                                            NULL));
1540
1814
    if(ret == -1){
1541
1815
      perror("sigaction");
1542
1816
    }
1543
 
    raise(signal_received);
 
1817
    do {
 
1818
      ret = raise(signal_received);
 
1819
    } while(ret != 0 and errno == EINTR);
 
1820
    if(ret != 0){
 
1821
      perror("raise");
 
1822
      abort();
 
1823
    }
 
1824
    TEMP_FAILURE_RETRY(pause());
1544
1825
  }
1545
1826
  
1546
1827
  return exitcode;