/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-17 01:21:27 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090917012127-4kwzquwuvudwucuv
* debian/control (Standards-Version): Updated to "2.8.3".

* mandos-client.conf.xml (OPTIONS/timeout): Add that a successful
                                            client request will also
                                            reset the timeout.
* mandos.xml (CHECKING): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
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() */
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno */
66
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
66
#include <time.h>               /* nanosleep(), time() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
69
69
                                   if_nametoindex(), IF_NAMESIZE */
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
74
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
75
                                   setgid() */
 
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
79
79
                                   argp_state, struct argp,
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() */
127
125
static const char mandos_protocol_version[] = "1";
128
126
const char *argp_program_version = "mandos-client " VERSION;
129
127
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;
132
128
 
133
129
/* Used for passing in values through the Avahi callback functions */
134
130
typedef struct {
146
142
                      .dh_bits = 1024, .priority = "SECURE256"
147
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
148
144
 
149
 
sig_atomic_t quit_now = 0;
150
 
int signal_received = 0;
151
 
 
152
145
/*
153
146
 * Make additional room in "buffer" for at least BUFFER_SIZE more
154
147
 * bytes. "buffer_capacity" is how much is currently allocated,
423
416
  }
424
417
  
425
418
  /* OpenPGP credentials */
426
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
419
  gnutls_certificate_allocate_credentials(&mc.cred);
427
420
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
421
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
422
                                                    from
 
423
                                                    -Wunreachable-code
 
424
                                                 */
429
425
            safer_gnutls_strerror(ret));
430
426
    gnutls_global_deinit();
431
427
    return -1;
480
476
  /* GnuTLS session creation */
481
477
  do {
482
478
    ret = gnutls_init(session, GNUTLS_SERVER);
483
 
    if(quit_now){
484
 
      return -1;
485
 
    }
486
479
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
487
480
  if(ret != GNUTLS_E_SUCCESS){
488
481
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
493
486
    const char *err;
494
487
    do {
495
488
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
496
 
      if(quit_now){
497
 
        gnutls_deinit(*session);
498
 
        return -1;
499
 
      }
500
489
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
501
490
    if(ret != GNUTLS_E_SUCCESS){
502
491
      fprintf(stderr, "Syntax error at: %s\n", err);
510
499
  do {
511
500
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
512
501
                                 mc.cred);
513
 
    if(quit_now){
514
 
      gnutls_deinit(*session);
515
 
      return -1;
516
 
    }
517
502
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
518
503
  if(ret != GNUTLS_E_SUCCESS){
519
504
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
534
519
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
535
520
                      __attribute__((unused)) const char *txt){}
536
521
 
 
522
sig_atomic_t quit_now = 0;
 
523
int signal_received = 0;
 
524
 
537
525
/* Called when a Mandos server is found */
538
526
static int start_mandos_communication(const char *ip, uint16_t port,
539
527
                                      AvahiIfIndex if_index,
545
533
    struct sockaddr_in6 in6;
546
534
  } to;
547
535
  char *buffer = NULL;
548
 
  char *decrypted_buffer = NULL;
 
536
  char *decrypted_buffer;
549
537
  size_t buffer_length = 0;
550
538
  size_t buffer_capacity = 0;
551
539
  size_t written;
552
 
  int retval = -1;
 
540
  int retval = 0;
553
541
  gnutls_session_t session;
554
542
  int pf;                       /* Protocol family */
555
543
  
556
 
  errno = 0;
557
 
  
558
544
  if(quit_now){
559
 
    errno = EINTR;
560
545
    return -1;
561
546
  }
562
547
  
569
554
    break;
570
555
  default:
571
556
    fprintf(stderr, "Bad address family: %d\n", af);
572
 
    errno = EINVAL;
573
557
    return -1;
574
558
  }
575
559
  
585
569
  
586
570
  tcp_sd = socket(pf, SOCK_STREAM, 0);
587
571
  if(tcp_sd < 0){
588
 
    int e = errno;
589
572
    perror("socket");
590
 
    errno = e;
 
573
    retval = -1;
591
574
    goto mandos_end;
592
575
  }
593
576
  
594
577
  if(quit_now){
595
 
    errno = EINTR;
596
578
    goto mandos_end;
597
579
  }
598
580
  
605
587
    ret = inet_pton(af, ip, &to.in.sin_addr);
606
588
  }
607
589
  if(ret < 0 ){
608
 
    int e = errno;
609
590
    perror("inet_pton");
610
 
    errno = e;
 
591
    retval = -1;
611
592
    goto mandos_end;
612
593
  }
613
594
  if(ret == 0){
614
 
    int e = errno;
615
595
    fprintf(stderr, "Bad address: %s\n", ip);
616
 
    errno = e;
 
596
    retval = -1;
617
597
    goto mandos_end;
618
598
  }
619
599
  if(af == AF_INET6){
627
607
      if(if_index == AVAHI_IF_UNSPEC){
628
608
        fprintf(stderr, "An IPv6 link-local address is incomplete"
629
609
                " without a network interface\n");
630
 
        errno = EINVAL;
 
610
        retval = -1;
631
611
        goto mandos_end;
632
612
      }
633
613
      /* Set the network interface number as scope */
640
620
  }
641
621
  
642
622
  if(quit_now){
643
 
    errno = EINTR;
644
623
    goto mandos_end;
645
624
  }
646
625
  
677
656
  }
678
657
  
679
658
  if(quit_now){
680
 
    errno = EINTR;
681
659
    goto mandos_end;
682
660
  }
683
661
  
687
665
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
688
666
  }
689
667
  if(ret < 0){
690
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
691
 
      int e = errno;
692
 
      perror("connect");
693
 
      errno = e;
694
 
    }
 
668
    perror("connect");
 
669
    retval = -1;
695
670
    goto mandos_end;
696
671
  }
697
672
  
698
673
  if(quit_now){
699
 
    errno = EINTR;
700
674
    goto mandos_end;
701
675
  }
702
676
  
707
681
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
708
682
                                   out_size - written));
709
683
    if(ret == -1){
710
 
      int e = errno;
711
684
      perror("write");
712
 
      errno = e;
 
685
      retval = -1;
713
686
      goto mandos_end;
714
687
    }
715
688
    written += (size_t)ret;
725
698
    }
726
699
  
727
700
    if(quit_now){
728
 
      errno = EINTR;
729
701
      goto mandos_end;
730
702
    }
731
703
  }
735
707
  }
736
708
  
737
709
  if(quit_now){
738
 
    errno = EINTR;
739
710
    goto mandos_end;
740
711
  }
741
712
  
742
713
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
743
714
  
744
715
  if(quit_now){
745
 
    errno = EINTR;
746
716
    goto mandos_end;
747
717
  }
748
718
  
749
719
  do {
750
720
    ret = gnutls_handshake(session);
751
721
    if(quit_now){
752
 
      errno = EINTR;
753
722
      goto mandos_end;
754
723
    }
755
724
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
759
728
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
760
729
      gnutls_perror(ret);
761
730
    }
762
 
    errno = EPROTO;
 
731
    retval = -1;
763
732
    goto mandos_end;
764
733
  }
765
734
  
773
742
  while(true){
774
743
    
775
744
    if(quit_now){
776
 
      errno = EINTR;
777
745
      goto mandos_end;
778
746
    }
779
747
    
780
748
    buffer_capacity = incbuffer(&buffer, buffer_length,
781
749
                                   buffer_capacity);
782
750
    if(buffer_capacity == 0){
783
 
      int e = errno;
784
751
      perror("incbuffer");
785
 
      errno = e;
 
752
      retval = -1;
786
753
      goto mandos_end;
787
754
    }
788
755
    
789
756
    if(quit_now){
790
 
      errno = EINTR;
791
757
      goto mandos_end;
792
758
    }
793
759
    
806
772
          ret = gnutls_handshake(session);
807
773
          
808
774
          if(quit_now){
809
 
            errno = EINTR;
810
775
            goto mandos_end;
811
776
          }
812
777
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
813
778
        if(ret < 0){
814
779
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
815
780
          gnutls_perror(ret);
816
 
          errno = EPROTO;
 
781
          retval = -1;
817
782
          goto mandos_end;
818
783
        }
819
784
        break;
820
785
      default:
821
786
        fprintf(stderr, "Unknown error while reading data from"
822
787
                " encrypted session with Mandos server\n");
 
788
        retval = -1;
823
789
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
824
 
        errno = EIO;
825
790
        goto mandos_end;
826
791
      }
827
792
    } else {
834
799
  }
835
800
  
836
801
  if(quit_now){
837
 
    errno = EINTR;
838
 
    goto mandos_end;
839
 
  }
840
 
  
841
 
  do {
842
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
843
 
    if(quit_now){
844
 
      errno = EINTR;
845
 
      goto mandos_end;
846
 
    }
847
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
802
    goto mandos_end;
 
803
  }
 
804
  
 
805
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
806
  
 
807
  if(quit_now){
 
808
    goto mandos_end;
 
809
  }
848
810
  
849
811
  if(buffer_length > 0){
850
812
    ssize_t decrypted_buffer_size;
856
818
      written = 0;
857
819
      while(written < (size_t) decrypted_buffer_size){
858
820
        if(quit_now){
859
 
          errno = EINTR;
860
821
          goto mandos_end;
861
822
        }
862
823
        
864
825
                          (size_t)decrypted_buffer_size - written,
865
826
                          stdout);
866
827
        if(ret == 0 and ferror(stdout)){
867
 
          int e = errno;
868
828
          if(debug){
869
829
            fprintf(stderr, "Error writing encrypted data: %s\n",
870
830
                    strerror(errno));
871
831
          }
872
 
          errno = e;
873
 
          goto mandos_end;
 
832
          retval = -1;
 
833
          break;
874
834
        }
875
835
        written += (size_t)ret;
876
836
      }
877
 
      retval = 0;
 
837
      free(decrypted_buffer);
 
838
    } else {
 
839
      retval = -1;
878
840
    }
 
841
  } else {
 
842
    retval = -1;
879
843
  }
880
844
  
881
845
  /* Shutdown procedure */
882
846
  
883
847
 mandos_end:
884
 
  {
885
 
    int e = errno;
886
 
    free(decrypted_buffer);
887
 
    free(buffer);
888
 
    if(tcp_sd >= 0){
889
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
890
 
    }
891
 
    if(ret == -1){
892
 
      if(e == 0){
893
 
        e = errno;
894
 
      }
895
 
      perror("close");
896
 
    }
897
 
    gnutls_deinit(session);
898
 
    if(quit_now){
899
 
      e = EINTR;
900
 
      retval = -1;
901
 
    }
902
 
    errno = e;
 
848
  free(buffer);
 
849
  if(tcp_sd >= 0){
 
850
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
851
  }
 
852
  if(ret == -1){
 
853
    perror("close");
 
854
  }
 
855
  gnutls_deinit(session);
 
856
  if(quit_now){
 
857
    retval = -1;
903
858
  }
904
859
  return retval;
905
860
}
1021
976
  errno = old_errno;
1022
977
}
1023
978
 
1024
 
/* 
1025
 
 * This function determines if a directory entry in /sys/class/net
1026
 
 * corresponds to an acceptable network device.
1027
 
 * (This function is passed to scandir(3) as a filter function.)
1028
 
 */
1029
 
int good_interface(const struct dirent *if_entry){
1030
 
  ssize_t ssret;
1031
 
  char *flagname = NULL;
1032
 
  if(if_entry->d_name[0] == '.'){
1033
 
    return 0;
1034
 
  }
1035
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
 
                     if_entry->d_name);
1037
 
  if(ret < 0){
1038
 
    perror("asprintf");
1039
 
    return 0;
1040
 
  }
1041
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1042
 
  if(flags_fd == -1){
1043
 
    perror("open");
1044
 
    free(flagname);
1045
 
    return 0;
1046
 
  }
1047
 
  free(flagname);
1048
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1049
 
  /* read line from flags_fd */
1050
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1051
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1052
 
  flagstring[(size_t)to_read] = '\0';
1053
 
  if(flagstring == NULL){
1054
 
    perror("malloc");
1055
 
    close(flags_fd);
1056
 
    return 0;
1057
 
  }
1058
 
  while(to_read > 0){
1059
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1060
 
                                             (size_t)to_read));
1061
 
    if(ssret == -1){
1062
 
      perror("read");
1063
 
      free(flagstring);
1064
 
      close(flags_fd);
1065
 
      return 0;
1066
 
    }
1067
 
    to_read -= ssret;
1068
 
    if(ssret == 0){
1069
 
      break;
1070
 
    }
1071
 
  }
1072
 
  close(flags_fd);
1073
 
  intmax_t tmpmax;
1074
 
  char *tmp;
1075
 
  errno = 0;
1076
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1077
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1078
 
                                         and not (isspace(*tmp)))
1079
 
     or tmpmax != (ifreq_flags)tmpmax){
1080
 
    if(debug){
1081
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1082
 
              flagstring, if_entry->d_name);
1083
 
    }
1084
 
    free(flagstring);
1085
 
    return 0;
1086
 
  }
1087
 
  free(flagstring);
1088
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1089
 
  /* Reject the loopback device */
1090
 
  if(flags & IFF_LOOPBACK){
1091
 
    if(debug){
1092
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1093
 
              if_entry->d_name);
1094
 
    }
1095
 
    return 0;
1096
 
  }
1097
 
  /* Accept point-to-point devices only if connect_to is specified */
1098
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1099
 
    if(debug){
1100
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1101
 
              if_entry->d_name);
1102
 
    }
1103
 
    return 1;
1104
 
  }
1105
 
  /* Otherwise, reject non-broadcast-capable devices */
1106
 
  if(not (flags & IFF_BROADCAST)){
1107
 
    if(debug){
1108
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1109
 
              if_entry->d_name);
1110
 
    }
1111
 
    return 0;
1112
 
  }
1113
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1114
 
  if(flags & IFF_NOARP){
1115
 
    if(debug){
1116
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1117
 
              if_entry->d_name);
1118
 
    }
1119
 
    return 0;
1120
 
  }
1121
 
  /* Accept this device */
1122
 
  if(debug){
1123
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1124
 
            if_entry->d_name);
1125
 
  }
1126
 
  return 1;
1127
 
}
1128
 
 
1129
979
int main(int argc, char *argv[]){
1130
980
  AvahiSServiceBrowser *sb = NULL;
1131
981
  int error;
1133
983
  intmax_t tmpmax;
1134
984
  char *tmp;
1135
985
  int exitcode = EXIT_SUCCESS;
1136
 
  const char *interface = "";
 
986
  const char *interface = "eth0";
1137
987
  struct ifreq network;
1138
988
  int sd = -1;
1139
989
  bool take_down_interface = false;
1140
990
  uid_t uid;
1141
991
  gid_t gid;
 
992
  char *connect_to = NULL;
1142
993
  char tempdir[] = "/tmp/mandosXXXXXX";
1143
994
  bool tempdir_created = false;
1144
995
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1207
1058
        .arg = "SECONDS",
1208
1059
        .doc = "Maximum delay to wait for interface startup",
1209
1060
        .group = 2 },
1210
 
      /*
1211
 
       * These reproduce what we would get without ARGP_NO_HELP
1212
 
       */
1213
 
      { .name = "help", .key = '?',
1214
 
        .doc = "Give this help list", .group = -1 },
1215
 
      { .name = "usage", .key = -3,
1216
 
        .doc = "Give a short usage message", .group = -1 },
1217
 
      { .name = "version", .key = 'V',
1218
 
        .doc = "Print program version", .group = -1 },
1219
1061
      { .name = NULL }
1220
1062
    };
1221
1063
    
1222
1064
    error_t parse_opt(int key, char *arg,
1223
1065
                      struct argp_state *state){
1224
 
      errno = 0;
1225
1066
      switch(key){
1226
1067
      case 128:                 /* --debug */
1227
1068
        debug = true;
1243
1084
        tmpmax = strtoimax(arg, &tmp, 10);
1244
1085
        if(errno != 0 or tmp == arg or *tmp != '\0'
1245
1086
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1246
 
          argp_error(state, "Bad number of DH bits");
 
1087
          fprintf(stderr, "Bad number of DH bits\n");
 
1088
          exit(EXIT_FAILURE);
1247
1089
        }
1248
1090
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1249
1091
        break;
1254
1096
        errno = 0;
1255
1097
        delay = strtof(arg, &tmp);
1256
1098
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1257
 
          argp_error(state, "Bad delay");
 
1099
          fprintf(stderr, "Bad delay\n");
 
1100
          exit(EXIT_FAILURE);
1258
1101
        }
1259
1102
        break;
1260
 
        /*
1261
 
         * These reproduce what we would get without ARGP_NO_HELP
1262
 
         */
1263
 
      case '?':                 /* --help */
1264
 
        argp_state_help(state, state->out_stream,
1265
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1266
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1267
 
      case -3:                  /* --usage */
1268
 
        argp_state_help(state, state->out_stream,
1269
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1270
 
      case 'V':                 /* --version */
1271
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1272
 
        exit(argp_err_exit_status);
 
1103
      case ARGP_KEY_ARG:
 
1104
        argp_usage(state);
 
1105
      case ARGP_KEY_END:
1273
1106
        break;
1274
1107
      default:
1275
1108
        return ARGP_ERR_UNKNOWN;
1276
1109
      }
1277
 
      return errno;
 
1110
      return 0;
1278
1111
    }
1279
1112
    
1280
1113
    struct argp argp = { .options = options, .parser = parse_opt,
1281
1114
                         .args_doc = "",
1282
1115
                         .doc = "Mandos client -- Get and decrypt"
1283
1116
                         " passwords from a Mandos server" };
1284
 
    ret = argp_parse(&argp, argc, argv,
1285
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1286
 
    switch(ret){
1287
 
    case 0:
1288
 
      break;
1289
 
    case ENOMEM:
1290
 
    default:
1291
 
      errno = ret;
1292
 
      perror("argp_parse");
1293
 
      exitcode = EX_OSERR;
1294
 
      goto end;
1295
 
    case EINVAL:
1296
 
      exitcode = EX_USAGE;
 
1117
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1118
    if(ret == ARGP_ERR_UNKNOWN){
 
1119
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1120
      exitcode = EXIT_FAILURE;
1297
1121
      goto end;
1298
1122
    }
1299
1123
  }
1301
1125
  if(not debug){
1302
1126
    avahi_set_log_function(empty_log);
1303
1127
  }
1304
 
 
1305
 
  if(interface[0] == '\0'){
1306
 
    struct dirent **direntries;
1307
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1308
 
                  alphasort);
1309
 
    if(ret >= 1){
1310
 
      /* Pick the first good interface */
1311
 
      interface = strdup(direntries[0]->d_name);
1312
 
      if(debug){
1313
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1314
 
      }
1315
 
      if(interface == NULL){
1316
 
        perror("malloc");
1317
 
        free(direntries);
1318
 
        exitcode = EXIT_FAILURE;
1319
 
        goto end;
1320
 
      }
1321
 
      free(direntries);
1322
 
    } else {
1323
 
      free(direntries);
1324
 
      fprintf(stderr, "Could not find a network interface\n");
1325
 
      exitcode = EXIT_FAILURE;
1326
 
      goto end;
1327
 
    }
1328
 
  }
1329
1128
  
1330
1129
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1331
1130
     from the signal handler */
1334
1133
  mc.simple_poll = avahi_simple_poll_new();
1335
1134
  if(mc.simple_poll == NULL){
1336
1135
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1337
 
    exitcode = EX_UNAVAILABLE;
 
1136
    exitcode = EXIT_FAILURE;
1338
1137
    goto end;
1339
1138
  }
1340
1139
  
1342
1141
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1343
1142
  if(ret == -1){
1344
1143
    perror("sigaddset");
1345
 
    exitcode = EX_OSERR;
 
1144
    exitcode = EXIT_FAILURE;
1346
1145
    goto end;
1347
1146
  }
1348
1147
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1349
1148
  if(ret == -1){
1350
1149
    perror("sigaddset");
1351
 
    exitcode = EX_OSERR;
 
1150
    exitcode = EXIT_FAILURE;
1352
1151
    goto end;
1353
1152
  }
1354
1153
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1355
1154
  if(ret == -1){
1356
1155
    perror("sigaddset");
1357
 
    exitcode = EX_OSERR;
 
1156
    exitcode = EXIT_FAILURE;
1358
1157
    goto end;
1359
1158
  }
1360
1159
  /* Need to check if the handler is SIG_IGN before handling:
1364
1163
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1365
1164
  if(ret == -1){
1366
1165
    perror("sigaction");
1367
 
    return EX_OSERR;
 
1166
    return EXIT_FAILURE;
1368
1167
  }
1369
1168
  if(old_sigterm_action.sa_handler != SIG_IGN){
1370
1169
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1371
1170
    if(ret == -1){
1372
1171
      perror("sigaction");
1373
 
      exitcode = EX_OSERR;
 
1172
      exitcode = EXIT_FAILURE;
1374
1173
      goto end;
1375
1174
    }
1376
1175
  }
1377
1176
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1378
1177
  if(ret == -1){
1379
1178
    perror("sigaction");
1380
 
    return EX_OSERR;
 
1179
    return EXIT_FAILURE;
1381
1180
  }
1382
1181
  if(old_sigterm_action.sa_handler != SIG_IGN){
1383
1182
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1384
1183
    if(ret == -1){
1385
1184
      perror("sigaction");
1386
 
      exitcode = EX_OSERR;
 
1185
      exitcode = EXIT_FAILURE;
1387
1186
      goto end;
1388
1187
    }
1389
1188
  }
1390
1189
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1391
1190
  if(ret == -1){
1392
1191
    perror("sigaction");
1393
 
    return EX_OSERR;
 
1192
    return EXIT_FAILURE;
1394
1193
  }
1395
1194
  if(old_sigterm_action.sa_handler != SIG_IGN){
1396
1195
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1397
1196
    if(ret == -1){
1398
1197
      perror("sigaction");
1399
 
      exitcode = EX_OSERR;
 
1198
      exitcode = EXIT_FAILURE;
1400
1199
      goto end;
1401
1200
    }
1402
1201
  }
1403
1202
  
1404
1203
  /* If the interface is down, bring it up */
1405
 
  if(strcmp(interface, "none") != 0){
 
1204
  if(interface[0] != '\0'){
1406
1205
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1407
1206
    if(if_index == 0){
1408
1207
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1409
 
      exitcode = EX_UNAVAILABLE;
 
1208
      exitcode = EXIT_FAILURE;
1410
1209
      goto end;
1411
1210
    }
1412
1211
    
1423
1222
    
1424
1223
#ifdef __linux__
1425
1224
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1426
 
       messages about the network interface to mess up the prompt */
 
1225
       messages to mess up the prompt */
1427
1226
    ret = klogctl(8, NULL, 5);
1428
1227
    bool restore_loglevel = true;
1429
1228
    if(ret == -1){
1435
1234
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1436
1235
    if(sd < 0){
1437
1236
      perror("socket");
1438
 
      exitcode = EX_OSERR;
 
1237
      exitcode = EXIT_FAILURE;
1439
1238
#ifdef __linux__
1440
1239
      if(restore_loglevel){
1441
1240
        ret = klogctl(7, NULL, 0);
1464
1263
        }
1465
1264
      }
1466
1265
#endif  /* __linux__ */
1467
 
      exitcode = EX_OSERR;
 
1266
      exitcode = EXIT_FAILURE;
1468
1267
      /* Lower privileges */
1469
1268
      errno = 0;
1470
1269
      ret = seteuid(uid);
1479
1278
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1480
1279
      if(ret == -1){
1481
1280
        take_down_interface = false;
1482
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1483
 
        exitcode = EX_OSERR;
 
1281
        perror("ioctl SIOCSIFFLAGS");
 
1282
        exitcode = EXIT_FAILURE;
1484
1283
#ifdef __linux__
1485
1284
        if(restore_loglevel){
1486
1285
          ret = klogctl(7, NULL, 0);
1552
1351
  ret = init_gnutls_global(pubkey, seckey);
1553
1352
  if(ret == -1){
1554
1353
    fprintf(stderr, "init_gnutls_global failed\n");
1555
 
    exitcode = EX_UNAVAILABLE;
 
1354
    exitcode = EXIT_FAILURE;
1556
1355
    goto end;
1557
1356
  } else {
1558
1357
    gnutls_initialized = true;
1575
1374
  
1576
1375
  if(not init_gpgme(pubkey, seckey, tempdir)){
1577
1376
    fprintf(stderr, "init_gpgme failed\n");
1578
 
    exitcode = EX_UNAVAILABLE;
 
1377
    exitcode = EXIT_FAILURE;
1579
1378
    goto end;
1580
1379
  } else {
1581
1380
    gpgme_initialized = true;
1591
1390
    char *address = strrchr(connect_to, ':');
1592
1391
    if(address == NULL){
1593
1392
      fprintf(stderr, "No colon in address\n");
1594
 
      exitcode = EX_USAGE;
 
1393
      exitcode = EXIT_FAILURE;
1595
1394
      goto end;
1596
1395
    }
1597
1396
    
1605
1404
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1606
1405
       or tmpmax != (uint16_t)tmpmax){
1607
1406
      fprintf(stderr, "Bad port number\n");
1608
 
      exitcode = EX_USAGE;
 
1407
      exitcode = EXIT_FAILURE;
1609
1408
      goto end;
1610
1409
    }
1611
1410
  
1627
1426
    if(quit_now){
1628
1427
      goto end;
1629
1428
    }
1630
 
 
1631
 
    while(not quit_now){
1632
 
      ret = start_mandos_communication(address, port, if_index, af);
1633
 
      if(quit_now or ret == 0){
1634
 
        break;
1635
 
      }
1636
 
      sleep(15);
1637
 
    };
1638
 
 
1639
 
    if (not quit_now){
 
1429
    
 
1430
    ret = start_mandos_communication(address, port, if_index, af);
 
1431
    if(ret < 0){
 
1432
      exitcode = EXIT_FAILURE;
 
1433
    } else {
1640
1434
      exitcode = EXIT_SUCCESS;
1641
1435
    }
1642
 
 
1643
1436
    goto end;
1644
1437
  }
1645
1438
  
1669
1462
  if(mc.server == NULL){
1670
1463
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1671
1464
            avahi_strerror(error));
1672
 
    exitcode = EX_UNAVAILABLE;
 
1465
    exitcode = EXIT_FAILURE;
1673
1466
    goto end;
1674
1467
  }
1675
1468
  
1684
1477
  if(sb == NULL){
1685
1478
    fprintf(stderr, "Failed to create service browser: %s\n",
1686
1479
            avahi_strerror(avahi_server_errno(mc.server)));
1687
 
    exitcode = EX_UNAVAILABLE;
 
1480
    exitcode = EXIT_FAILURE;
1688
1481
    goto end;
1689
1482
  }
1690
1483
  
1739
1532
      if(ret == -1){
1740
1533
        perror("ioctl SIOCGIFFLAGS");
1741
1534
      } else if(network.ifr_flags & IFF_UP) {
1742
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1535
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1743
1536
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1744
1537
        if(ret == -1){
1745
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1538
          perror("ioctl SIOCSIFFLAGS");
1746
1539
        }
1747
1540
      }
1748
1541
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1805
1598
  if(quit_now){
1806
1599
    sigemptyset(&old_sigterm_action.sa_mask);
1807
1600
    old_sigterm_action.sa_handler = SIG_DFL;
1808
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1809
 
                                            &old_sigterm_action,
1810
 
                                            NULL));
 
1601
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1811
1602
    if(ret == -1){
1812
1603
      perror("sigaction");
1813
1604
    }
1814
 
    do {
1815
 
      ret = raise(signal_received);
1816
 
    } while(ret != 0 and errno == EINTR);
1817
 
    if(ret != 0){
1818
 
      perror("raise");
1819
 
      abort();
1820
 
    }
1821
 
    TEMP_FAILURE_RETRY(pause());
 
1605
    raise(signal_received);
1822
1606
  }
1823
1607
  
1824
1608
  return exitcode;