/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 02:42:49 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090917024249-y6j76xtzz3i2vptv
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
                                                   return immediately
                                                   if interrupted by
                                                   signal.

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() */
 
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() */
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 {
548
544
    struct sockaddr_in6 in6;
549
545
  } to;
550
546
  char *buffer = NULL;
551
 
  char *decrypted_buffer = NULL;
 
547
  char *decrypted_buffer;
552
548
  size_t buffer_length = 0;
553
549
  size_t buffer_capacity = 0;
554
550
  size_t written;
555
 
  int retval = -1;
 
551
  int retval = 0;
556
552
  gnutls_session_t session;
557
553
  int pf;                       /* Protocol family */
558
554
  
559
 
  errno = 0;
560
 
  
561
555
  if(quit_now){
562
 
    errno = EINTR;
563
556
    return -1;
564
557
  }
565
558
  
572
565
    break;
573
566
  default:
574
567
    fprintf(stderr, "Bad address family: %d\n", af);
575
 
    errno = EINVAL;
576
568
    return -1;
577
569
  }
578
570
  
588
580
  
589
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
582
  if(tcp_sd < 0){
591
 
    int e = errno;
592
583
    perror("socket");
593
 
    errno = e;
 
584
    retval = -1;
594
585
    goto mandos_end;
595
586
  }
596
587
  
597
588
  if(quit_now){
598
 
    errno = EINTR;
 
589
    retval = -1;
599
590
    goto mandos_end;
600
591
  }
601
592
  
608
599
    ret = inet_pton(af, ip, &to.in.sin_addr);
609
600
  }
610
601
  if(ret < 0 ){
611
 
    int e = errno;
612
602
    perror("inet_pton");
613
 
    errno = e;
 
603
    retval = -1;
614
604
    goto mandos_end;
615
605
  }
616
606
  if(ret == 0){
617
 
    int e = errno;
618
607
    fprintf(stderr, "Bad address: %s\n", ip);
619
 
    errno = e;
 
608
    retval = -1;
620
609
    goto mandos_end;
621
610
  }
622
611
  if(af == AF_INET6){
630
619
      if(if_index == AVAHI_IF_UNSPEC){
631
620
        fprintf(stderr, "An IPv6 link-local address is incomplete"
632
621
                " without a network interface\n");
633
 
        errno = EINVAL;
 
622
        retval = -1;
634
623
        goto mandos_end;
635
624
      }
636
625
      /* Set the network interface number as scope */
643
632
  }
644
633
  
645
634
  if(quit_now){
646
 
    errno = EINTR;
 
635
    retval = -1;
647
636
    goto mandos_end;
648
637
  }
649
638
  
680
669
  }
681
670
  
682
671
  if(quit_now){
683
 
    errno = EINTR;
 
672
    retval = -1;
684
673
    goto mandos_end;
685
674
  }
686
675
  
690
679
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
680
  }
692
681
  if(ret < 0){
693
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
 
      int e = errno;
695
 
      perror("connect");
696
 
      errno = e;
697
 
    }
 
682
    perror("connect");
 
683
    retval = -1;
698
684
    goto mandos_end;
699
685
  }
700
686
  
701
687
  if(quit_now){
702
 
    errno = EINTR;
 
688
    retval = -1;
703
689
    goto mandos_end;
704
690
  }
705
691
  
710
696
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
711
697
                                   out_size - written));
712
698
    if(ret == -1){
713
 
      int e = errno;
714
699
      perror("write");
715
 
      errno = e;
 
700
      retval = -1;
716
701
      goto mandos_end;
717
702
    }
718
703
    written += (size_t)ret;
728
713
    }
729
714
  
730
715
    if(quit_now){
731
 
      errno = EINTR;
 
716
      retval = -1;
732
717
      goto mandos_end;
733
718
    }
734
719
  }
738
723
  }
739
724
  
740
725
  if(quit_now){
741
 
    errno = EINTR;
 
726
    retval = -1;
742
727
    goto mandos_end;
743
728
  }
744
729
  
745
730
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
746
731
  
747
732
  if(quit_now){
748
 
    errno = EINTR;
 
733
    retval = -1;
749
734
    goto mandos_end;
750
735
  }
751
736
  
752
737
  do {
753
738
    ret = gnutls_handshake(session);
754
739
    if(quit_now){
755
 
      errno = EINTR;
 
740
      retval = -1;
756
741
      goto mandos_end;
757
742
    }
758
743
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
762
747
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
763
748
      gnutls_perror(ret);
764
749
    }
765
 
    errno = EPROTO;
 
750
    retval = -1;
766
751
    goto mandos_end;
767
752
  }
768
753
  
776
761
  while(true){
777
762
    
778
763
    if(quit_now){
779
 
      errno = EINTR;
 
764
      retval = -1;
780
765
      goto mandos_end;
781
766
    }
782
767
    
783
768
    buffer_capacity = incbuffer(&buffer, buffer_length,
784
769
                                   buffer_capacity);
785
770
    if(buffer_capacity == 0){
786
 
      int e = errno;
787
771
      perror("incbuffer");
788
 
      errno = e;
 
772
      retval = -1;
789
773
      goto mandos_end;
790
774
    }
791
775
    
792
776
    if(quit_now){
793
 
      errno = EINTR;
 
777
      retval = -1;
794
778
      goto mandos_end;
795
779
    }
796
780
    
809
793
          ret = gnutls_handshake(session);
810
794
          
811
795
          if(quit_now){
812
 
            errno = EINTR;
 
796
            retval = -1;
813
797
            goto mandos_end;
814
798
          }
815
799
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
816
800
        if(ret < 0){
817
801
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
818
802
          gnutls_perror(ret);
819
 
          errno = EPROTO;
 
803
          retval = -1;
820
804
          goto mandos_end;
821
805
        }
822
806
        break;
823
807
      default:
824
808
        fprintf(stderr, "Unknown error while reading data from"
825
809
                " encrypted session with Mandos server\n");
 
810
        retval = -1;
826
811
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
827
 
        errno = EIO;
828
812
        goto mandos_end;
829
813
      }
830
814
    } else {
837
821
  }
838
822
  
839
823
  if(quit_now){
840
 
    errno = EINTR;
 
824
    retval = -1;
841
825
    goto mandos_end;
842
826
  }
843
827
  
844
828
  do {
845
829
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
846
830
    if(quit_now){
847
 
      errno = EINTR;
 
831
      retval = -1;
848
832
      goto mandos_end;
849
833
    }
850
834
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
859
843
      written = 0;
860
844
      while(written < (size_t) decrypted_buffer_size){
861
845
        if(quit_now){
862
 
          errno = EINTR;
 
846
          retval = -1;
863
847
          goto mandos_end;
864
848
        }
865
849
        
867
851
                          (size_t)decrypted_buffer_size - written,
868
852
                          stdout);
869
853
        if(ret == 0 and ferror(stdout)){
870
 
          int e = errno;
871
854
          if(debug){
872
855
            fprintf(stderr, "Error writing encrypted data: %s\n",
873
856
                    strerror(errno));
874
857
          }
875
 
          errno = e;
876
 
          goto mandos_end;
 
858
          retval = -1;
 
859
          break;
877
860
        }
878
861
        written += (size_t)ret;
879
862
      }
880
 
      retval = 0;
 
863
      free(decrypted_buffer);
 
864
    } else {
 
865
      retval = -1;
881
866
    }
 
867
  } else {
 
868
    retval = -1;
882
869
  }
883
870
  
884
871
  /* Shutdown procedure */
885
872
  
886
873
 mandos_end:
887
 
  {
888
 
    int e = errno;
889
 
    free(decrypted_buffer);
890
 
    free(buffer);
891
 
    if(tcp_sd >= 0){
892
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
893
 
    }
894
 
    if(ret == -1){
895
 
      if(e == 0){
896
 
        e = errno;
897
 
      }
898
 
      perror("close");
899
 
    }
900
 
    gnutls_deinit(session);
901
 
    if(quit_now){
902
 
      e = EINTR;
903
 
      retval = -1;
904
 
    }
905
 
    errno = e;
 
874
  free(buffer);
 
875
  if(tcp_sd >= 0){
 
876
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
877
  }
 
878
  if(ret == -1){
 
879
    perror("close");
 
880
  }
 
881
  gnutls_deinit(session);
 
882
  if(quit_now){
 
883
    retval = -1;
906
884
  }
907
885
  return retval;
908
886
}
1024
1002
  errno = old_errno;
1025
1003
}
1026
1004
 
1027
 
/* 
1028
 
 * This function determines if a directory entry in /sys/class/net
1029
 
 * corresponds to an acceptable network device.
1030
 
 * (This function is passed to scandir(3) as a filter function.)
1031
 
 */
1032
 
int good_interface(const struct dirent *if_entry){
1033
 
  ssize_t ssret;
1034
 
  char *flagname = NULL;
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
 
  if(if_entry->d_name[0] == '.'){
1042
 
    return 0;
1043
 
  }
1044
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1045
 
  if(flags_fd == -1){
1046
 
    perror("open");
1047
 
    return 0;
1048
 
  }
1049
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1050
 
  /* read line from flags_fd */
1051
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
 
  flagstring[(size_t)to_read] = '\0';
1054
 
  if(flagstring == NULL){
1055
 
    perror("malloc");
1056
 
    close(flags_fd);
1057
 
    return 0;
1058
 
  }
1059
 
  while(to_read > 0){
1060
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1061
 
                                             (size_t)to_read));
1062
 
    if(ssret == -1){
1063
 
      perror("read");
1064
 
      free(flagstring);
1065
 
      close(flags_fd);
1066
 
      return 0;
1067
 
    }
1068
 
    to_read -= ssret;
1069
 
    if(ssret == 0){
1070
 
      break;
1071
 
    }
1072
 
  }
1073
 
  close(flags_fd);
1074
 
  intmax_t tmpmax;
1075
 
  char *tmp;
1076
 
  errno = 0;
1077
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1078
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
 
                                         and not (isspace(*tmp)))
1080
 
     or tmpmax != (ifreq_flags)tmpmax){
1081
 
    if(debug){
1082
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
 
              flagstring, if_entry->d_name);
1084
 
    }
1085
 
    free(flagstring);
1086
 
    return 0;
1087
 
  }
1088
 
  free(flagstring);
1089
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1090
 
  /* Reject the loopback device */
1091
 
  if(flags & IFF_LOOPBACK){
1092
 
    if(debug){
1093
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1094
 
              if_entry->d_name);
1095
 
    }
1096
 
    return 0;
1097
 
  }
1098
 
  /* Accept point-to-point devices only if connect_to is specified */
1099
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1100
 
    if(debug){
1101
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1102
 
              if_entry->d_name);
1103
 
    }
1104
 
    return 1;
1105
 
  }
1106
 
  /* Otherwise, reject non-broadcast-capable devices */
1107
 
  if(not (flags & IFF_BROADCAST)){
1108
 
    if(debug){
1109
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1110
 
              if_entry->d_name);
1111
 
    }
1112
 
    return 0;
1113
 
  }
1114
 
  /* Accept this device */
1115
 
  if(debug){
1116
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1117
 
            if_entry->d_name);
1118
 
  }
1119
 
  return 1;
1120
 
}
1121
 
 
1122
1005
int main(int argc, char *argv[]){
1123
1006
  AvahiSServiceBrowser *sb = NULL;
1124
1007
  int error;
1126
1009
  intmax_t tmpmax;
1127
1010
  char *tmp;
1128
1011
  int exitcode = EXIT_SUCCESS;
1129
 
  const char *interface = "";
 
1012
  const char *interface = "eth0";
1130
1013
  struct ifreq network;
1131
1014
  int sd = -1;
1132
1015
  bool take_down_interface = false;
1133
1016
  uid_t uid;
1134
1017
  gid_t gid;
 
1018
  char *connect_to = NULL;
1135
1019
  char tempdir[] = "/tmp/mandosXXXXXX";
1136
1020
  bool tempdir_created = false;
1137
1021
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1200
1084
        .arg = "SECONDS",
1201
1085
        .doc = "Maximum delay to wait for interface startup",
1202
1086
        .group = 2 },
1203
 
      /*
1204
 
       * These reproduce what we would get without ARGP_NO_HELP
1205
 
       */
1206
 
      { .name = "help", .key = '?',
1207
 
        .doc = "Give this help list", .group = -1 },
1208
 
      { .name = "usage", .key = -3,
1209
 
        .doc = "Give a short usage message", .group = -1 },
1210
 
      { .name = "version", .key = 'V',
1211
 
        .doc = "Print program version", .group = -1 },
1212
1087
      { .name = NULL }
1213
1088
    };
1214
1089
    
1215
1090
    error_t parse_opt(int key, char *arg,
1216
1091
                      struct argp_state *state){
1217
 
      errno = 0;
1218
1092
      switch(key){
1219
1093
      case 128:                 /* --debug */
1220
1094
        debug = true;
1236
1110
        tmpmax = strtoimax(arg, &tmp, 10);
1237
1111
        if(errno != 0 or tmp == arg or *tmp != '\0'
1238
1112
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1239
 
          argp_error(state, "Bad number of DH bits");
 
1113
          fprintf(stderr, "Bad number of DH bits\n");
 
1114
          exit(EXIT_FAILURE);
1240
1115
        }
1241
1116
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1242
1117
        break;
1247
1122
        errno = 0;
1248
1123
        delay = strtof(arg, &tmp);
1249
1124
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1250
 
          argp_error(state, "Bad delay");
 
1125
          fprintf(stderr, "Bad delay\n");
 
1126
          exit(EXIT_FAILURE);
1251
1127
        }
1252
1128
        break;
1253
 
        /*
1254
 
         * These reproduce what we would get without ARGP_NO_HELP
1255
 
         */
1256
 
      case '?':                 /* --help */
1257
 
        argp_state_help(state, state->out_stream,
1258
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1259
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1260
 
      case -3:                  /* --usage */
1261
 
        argp_state_help(state, state->out_stream,
1262
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1263
 
      case 'V':                 /* --version */
1264
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1265
 
        exit(argp_err_exit_status);
 
1129
      case ARGP_KEY_ARG:
 
1130
        argp_usage(state);
 
1131
      case ARGP_KEY_END:
1266
1132
        break;
1267
1133
      default:
1268
1134
        return ARGP_ERR_UNKNOWN;
1269
1135
      }
1270
 
      return errno;
 
1136
      return 0;
1271
1137
    }
1272
1138
    
1273
1139
    struct argp argp = { .options = options, .parser = parse_opt,
1274
1140
                         .args_doc = "",
1275
1141
                         .doc = "Mandos client -- Get and decrypt"
1276
1142
                         " passwords from a Mandos server" };
1277
 
    ret = argp_parse(&argp, argc, argv,
1278
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1279
 
    switch(ret){
1280
 
    case 0:
1281
 
      break;
1282
 
    case ENOMEM:
1283
 
    default:
1284
 
      errno = ret;
1285
 
      perror("argp_parse");
1286
 
      exitcode = EX_OSERR;
1287
 
      goto end;
1288
 
    case EINVAL:
1289
 
      exitcode = EX_USAGE;
 
1143
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1144
    if(ret == ARGP_ERR_UNKNOWN){
 
1145
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1146
      exitcode = EXIT_FAILURE;
1290
1147
      goto end;
1291
1148
    }
1292
1149
  }
1294
1151
  if(not debug){
1295
1152
    avahi_set_log_function(empty_log);
1296
1153
  }
1297
 
 
1298
 
  if(interface[0] == '\0'){
1299
 
    struct dirent **direntries;
1300
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1301
 
                  alphasort);
1302
 
    if(ret >= 1){
1303
 
      /* Pick the first good interface */
1304
 
      interface = strdup(direntries[0]->d_name);
1305
 
      if(debug){
1306
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1307
 
      }
1308
 
      if(interface == NULL){
1309
 
        perror("malloc");
1310
 
        free(direntries);
1311
 
        exitcode = EXIT_FAILURE;
1312
 
        goto end;
1313
 
      }
1314
 
      free(direntries);
1315
 
    } else {
1316
 
      free(direntries);
1317
 
      fprintf(stderr, "Could not find a network interface\n");
1318
 
      exitcode = EXIT_FAILURE;
1319
 
      goto end;
1320
 
    }
1321
 
  }
1322
1154
  
1323
1155
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1324
1156
     from the signal handler */
1327
1159
  mc.simple_poll = avahi_simple_poll_new();
1328
1160
  if(mc.simple_poll == NULL){
1329
1161
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1330
 
    exitcode = EX_UNAVAILABLE;
 
1162
    exitcode = EXIT_FAILURE;
1331
1163
    goto end;
1332
1164
  }
1333
1165
  
1335
1167
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1336
1168
  if(ret == -1){
1337
1169
    perror("sigaddset");
1338
 
    exitcode = EX_OSERR;
 
1170
    exitcode = EXIT_FAILURE;
1339
1171
    goto end;
1340
1172
  }
1341
1173
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1342
1174
  if(ret == -1){
1343
1175
    perror("sigaddset");
1344
 
    exitcode = EX_OSERR;
 
1176
    exitcode = EXIT_FAILURE;
1345
1177
    goto end;
1346
1178
  }
1347
1179
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1348
1180
  if(ret == -1){
1349
1181
    perror("sigaddset");
1350
 
    exitcode = EX_OSERR;
 
1182
    exitcode = EXIT_FAILURE;
1351
1183
    goto end;
1352
1184
  }
1353
1185
  /* Need to check if the handler is SIG_IGN before handling:
1357
1189
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1358
1190
  if(ret == -1){
1359
1191
    perror("sigaction");
1360
 
    return EX_OSERR;
 
1192
    return EXIT_FAILURE;
1361
1193
  }
1362
1194
  if(old_sigterm_action.sa_handler != SIG_IGN){
1363
1195
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1364
1196
    if(ret == -1){
1365
1197
      perror("sigaction");
1366
 
      exitcode = EX_OSERR;
 
1198
      exitcode = EXIT_FAILURE;
1367
1199
      goto end;
1368
1200
    }
1369
1201
  }
1370
1202
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1371
1203
  if(ret == -1){
1372
1204
    perror("sigaction");
1373
 
    return EX_OSERR;
 
1205
    return EXIT_FAILURE;
1374
1206
  }
1375
1207
  if(old_sigterm_action.sa_handler != SIG_IGN){
1376
1208
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1377
1209
    if(ret == -1){
1378
1210
      perror("sigaction");
1379
 
      exitcode = EX_OSERR;
 
1211
      exitcode = EXIT_FAILURE;
1380
1212
      goto end;
1381
1213
    }
1382
1214
  }
1383
1215
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1384
1216
  if(ret == -1){
1385
1217
    perror("sigaction");
1386
 
    return EX_OSERR;
 
1218
    return EXIT_FAILURE;
1387
1219
  }
1388
1220
  if(old_sigterm_action.sa_handler != SIG_IGN){
1389
1221
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1390
1222
    if(ret == -1){
1391
1223
      perror("sigaction");
1392
 
      exitcode = EX_OSERR;
 
1224
      exitcode = EXIT_FAILURE;
1393
1225
      goto end;
1394
1226
    }
1395
1227
  }
1396
1228
  
1397
1229
  /* If the interface is down, bring it up */
1398
 
  if(strcmp(interface, "none") != 0){
 
1230
  if(interface[0] != '\0'){
1399
1231
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1400
1232
    if(if_index == 0){
1401
1233
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1402
 
      exitcode = EX_UNAVAILABLE;
 
1234
      exitcode = EXIT_FAILURE;
1403
1235
      goto end;
1404
1236
    }
1405
1237
    
1416
1248
    
1417
1249
#ifdef __linux__
1418
1250
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1419
 
       messages about the network interface to mess up the prompt */
 
1251
       messages to mess up the prompt */
1420
1252
    ret = klogctl(8, NULL, 5);
1421
1253
    bool restore_loglevel = true;
1422
1254
    if(ret == -1){
1428
1260
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1429
1261
    if(sd < 0){
1430
1262
      perror("socket");
1431
 
      exitcode = EX_OSERR;
 
1263
      exitcode = EXIT_FAILURE;
1432
1264
#ifdef __linux__
1433
1265
      if(restore_loglevel){
1434
1266
        ret = klogctl(7, NULL, 0);
1457
1289
        }
1458
1290
      }
1459
1291
#endif  /* __linux__ */
1460
 
      exitcode = EX_OSERR;
 
1292
      exitcode = EXIT_FAILURE;
1461
1293
      /* Lower privileges */
1462
1294
      errno = 0;
1463
1295
      ret = seteuid(uid);
1472
1304
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1473
1305
      if(ret == -1){
1474
1306
        take_down_interface = false;
1475
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1476
 
        exitcode = EX_OSERR;
 
1307
        perror("ioctl SIOCSIFFLAGS");
 
1308
        exitcode = EXIT_FAILURE;
1477
1309
#ifdef __linux__
1478
1310
        if(restore_loglevel){
1479
1311
          ret = klogctl(7, NULL, 0);
1545
1377
  ret = init_gnutls_global(pubkey, seckey);
1546
1378
  if(ret == -1){
1547
1379
    fprintf(stderr, "init_gnutls_global failed\n");
1548
 
    exitcode = EX_UNAVAILABLE;
 
1380
    exitcode = EXIT_FAILURE;
1549
1381
    goto end;
1550
1382
  } else {
1551
1383
    gnutls_initialized = true;
1568
1400
  
1569
1401
  if(not init_gpgme(pubkey, seckey, tempdir)){
1570
1402
    fprintf(stderr, "init_gpgme failed\n");
1571
 
    exitcode = EX_UNAVAILABLE;
 
1403
    exitcode = EXIT_FAILURE;
1572
1404
    goto end;
1573
1405
  } else {
1574
1406
    gpgme_initialized = true;
1584
1416
    char *address = strrchr(connect_to, ':');
1585
1417
    if(address == NULL){
1586
1418
      fprintf(stderr, "No colon in address\n");
1587
 
      exitcode = EX_USAGE;
 
1419
      exitcode = EXIT_FAILURE;
1588
1420
      goto end;
1589
1421
    }
1590
1422
    
1598
1430
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1599
1431
       or tmpmax != (uint16_t)tmpmax){
1600
1432
      fprintf(stderr, "Bad port number\n");
1601
 
      exitcode = EX_USAGE;
 
1433
      exitcode = EXIT_FAILURE;
1602
1434
      goto end;
1603
1435
    }
1604
1436
  
1620
1452
    if(quit_now){
1621
1453
      goto end;
1622
1454
    }
1623
 
 
1624
 
    while(not quit_now){
1625
 
      ret = start_mandos_communication(address, port, if_index, af);
1626
 
      if(quit_now or ret == 0){
1627
 
        break;
1628
 
      }
1629
 
      sleep(15);
1630
 
    };
1631
 
 
1632
 
    if (not quit_now){
 
1455
    
 
1456
    ret = start_mandos_communication(address, port, if_index, af);
 
1457
    if(ret < 0){
 
1458
      exitcode = EXIT_FAILURE;
 
1459
    } else {
1633
1460
      exitcode = EXIT_SUCCESS;
1634
1461
    }
1635
 
 
1636
1462
    goto end;
1637
1463
  }
1638
1464
  
1662
1488
  if(mc.server == NULL){
1663
1489
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1664
1490
            avahi_strerror(error));
1665
 
    exitcode = EX_UNAVAILABLE;
 
1491
    exitcode = EXIT_FAILURE;
1666
1492
    goto end;
1667
1493
  }
1668
1494
  
1677
1503
  if(sb == NULL){
1678
1504
    fprintf(stderr, "Failed to create service browser: %s\n",
1679
1505
            avahi_strerror(avahi_server_errno(mc.server)));
1680
 
    exitcode = EX_UNAVAILABLE;
 
1506
    exitcode = EXIT_FAILURE;
1681
1507
    goto end;
1682
1508
  }
1683
1509
  
1732
1558
      if(ret == -1){
1733
1559
        perror("ioctl SIOCGIFFLAGS");
1734
1560
      } else if(network.ifr_flags & IFF_UP) {
1735
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1561
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1736
1562
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1737
1563
        if(ret == -1){
1738
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1564
          perror("ioctl SIOCSIFFLAGS");
1739
1565
        }
1740
1566
      }
1741
1567
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1798
1624
  if(quit_now){
1799
1625
    sigemptyset(&old_sigterm_action.sa_mask);
1800
1626
    old_sigterm_action.sa_handler = SIG_DFL;
1801
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1802
 
                                            &old_sigterm_action,
1803
 
                                            NULL));
 
1627
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1804
1628
    if(ret == -1){
1805
1629
      perror("sigaction");
1806
1630
    }
1807
 
    do {
1808
 
      ret = raise(signal_received);
1809
 
    } while(ret != 0 and errno == EINTR);
1810
 
    if(ret != 0){
1811
 
      perror("raise");
1812
 
      abort();
1813
 
    }
1814
 
    TEMP_FAILURE_RETRY(pause());
 
1631
    raise(signal_received);
1815
1632
  }
1816
1633
  
1817
1634
  return exitcode;