/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 at bsnet
  • Date: 2010-08-16 04:30:16 UTC
  • mto: This revision was merged to the branch mainline in revision 419.
  • Revision ID: teddy@fukt.bsnet.se-20100816043016-6wzjp3avb84gaejz
* mandos (main): Bug fix: Don't try to bind to an empty string
                 interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
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>";
 
128
static const char sys_class_net[] = "/sys/class/net";
 
129
char *connect_to = NULL;
130
130
 
131
131
/* Used for passing in values through the Avahi callback functions */
132
132
typedef struct {
554
554
  gnutls_session_t session;
555
555
  int pf;                       /* Protocol family */
556
556
  
557
 
  errno = 0;
558
 
  
559
557
  if(quit_now){
560
 
    errno = EINTR;
561
558
    return -1;
562
559
  }
563
560
  
570
567
    break;
571
568
  default:
572
569
    fprintf(stderr, "Bad address family: %d\n", af);
573
 
    errno = EINVAL;
574
570
    return -1;
575
571
  }
576
572
  
586
582
  
587
583
  tcp_sd = socket(pf, SOCK_STREAM, 0);
588
584
  if(tcp_sd < 0){
589
 
    int e = errno;
590
585
    perror("socket");
591
 
    errno = e;
592
586
    goto mandos_end;
593
587
  }
594
588
  
595
589
  if(quit_now){
596
 
    errno = EINTR;
597
590
    goto mandos_end;
598
591
  }
599
592
  
606
599
    ret = inet_pton(af, ip, &to.in.sin_addr);
607
600
  }
608
601
  if(ret < 0 ){
609
 
    int e = errno;
610
602
    perror("inet_pton");
611
 
    errno = e;
612
603
    goto mandos_end;
613
604
  }
614
605
  if(ret == 0){
615
 
    int e = errno;
616
606
    fprintf(stderr, "Bad address: %s\n", ip);
617
 
    errno = e;
618
607
    goto mandos_end;
619
608
  }
620
609
  if(af == AF_INET6){
628
617
      if(if_index == AVAHI_IF_UNSPEC){
629
618
        fprintf(stderr, "An IPv6 link-local address is incomplete"
630
619
                " without a network interface\n");
631
 
        errno = EINVAL;
632
620
        goto mandos_end;
633
621
      }
634
622
      /* Set the network interface number as scope */
641
629
  }
642
630
  
643
631
  if(quit_now){
644
 
    errno = EINTR;
645
632
    goto mandos_end;
646
633
  }
647
634
  
678
665
  }
679
666
  
680
667
  if(quit_now){
681
 
    errno = EINTR;
682
668
    goto mandos_end;
683
669
  }
684
670
  
688
674
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
689
675
  }
690
676
  if(ret < 0){
691
 
    int e = errno;
692
677
    perror("connect");
693
 
    errno = e;
694
678
    goto mandos_end;
695
679
  }
696
680
  
697
681
  if(quit_now){
698
 
    errno = EINTR;
699
682
    goto mandos_end;
700
683
  }
701
684
  
706
689
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
707
690
                                   out_size - written));
708
691
    if(ret == -1){
709
 
      int e = errno;
710
692
      perror("write");
711
 
      errno = e;
712
693
      goto mandos_end;
713
694
    }
714
695
    written += (size_t)ret;
724
705
    }
725
706
  
726
707
    if(quit_now){
727
 
      errno = EINTR;
728
708
      goto mandos_end;
729
709
    }
730
710
  }
734
714
  }
735
715
  
736
716
  if(quit_now){
737
 
    errno = EINTR;
738
717
    goto mandos_end;
739
718
  }
740
719
  
741
720
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
742
721
  
743
722
  if(quit_now){
744
 
    errno = EINTR;
745
723
    goto mandos_end;
746
724
  }
747
725
  
748
726
  do {
749
727
    ret = gnutls_handshake(session);
750
728
    if(quit_now){
751
 
      errno = EINTR;
752
729
      goto mandos_end;
753
730
    }
754
731
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
758
735
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
759
736
      gnutls_perror(ret);
760
737
    }
761
 
    errno = EPROTO;
762
738
    goto mandos_end;
763
739
  }
764
740
  
772
748
  while(true){
773
749
    
774
750
    if(quit_now){
775
 
      errno = EINTR;
776
751
      goto mandos_end;
777
752
    }
778
753
    
779
754
    buffer_capacity = incbuffer(&buffer, buffer_length,
780
755
                                   buffer_capacity);
781
756
    if(buffer_capacity == 0){
782
 
      int e = errno;
783
757
      perror("incbuffer");
784
 
      errno = e;
785
758
      goto mandos_end;
786
759
    }
787
760
    
788
761
    if(quit_now){
789
 
      errno = EINTR;
790
762
      goto mandos_end;
791
763
    }
792
764
    
805
777
          ret = gnutls_handshake(session);
806
778
          
807
779
          if(quit_now){
808
 
            errno = EINTR;
809
780
            goto mandos_end;
810
781
          }
811
782
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
812
783
        if(ret < 0){
813
784
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
814
785
          gnutls_perror(ret);
815
 
          errno = EPROTO;
816
786
          goto mandos_end;
817
787
        }
818
788
        break;
820
790
        fprintf(stderr, "Unknown error while reading data from"
821
791
                " encrypted session with Mandos server\n");
822
792
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
823
 
        errno = EIO;
824
793
        goto mandos_end;
825
794
      }
826
795
    } else {
833
802
  }
834
803
  
835
804
  if(quit_now){
836
 
    errno = EINTR;
837
805
    goto mandos_end;
838
806
  }
839
807
  
840
808
  do {
841
809
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
842
810
    if(quit_now){
843
 
      errno = EINTR;
844
811
      goto mandos_end;
845
812
    }
846
813
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
855
822
      written = 0;
856
823
      while(written < (size_t) decrypted_buffer_size){
857
824
        if(quit_now){
858
 
          errno = EINTR;
859
825
          goto mandos_end;
860
826
        }
861
827
        
863
829
                          (size_t)decrypted_buffer_size - written,
864
830
                          stdout);
865
831
        if(ret == 0 and ferror(stdout)){
866
 
          int e = errno;
867
832
          if(debug){
868
833
            fprintf(stderr, "Error writing encrypted data: %s\n",
869
834
                    strerror(errno));
870
835
          }
871
 
          errno = e;
872
836
          goto mandos_end;
873
837
        }
874
838
        written += (size_t)ret;
880
844
  /* Shutdown procedure */
881
845
  
882
846
 mandos_end:
883
 
  {
884
 
    int e = errno;
885
 
    free(decrypted_buffer);
886
 
    free(buffer);
887
 
    if(tcp_sd >= 0){
888
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
889
 
    }
890
 
    if(ret == -1){
891
 
      if(e == 0){
892
 
        e = errno;
893
 
      }
894
 
      perror("close");
895
 
    }
896
 
    gnutls_deinit(session);
897
 
    if(quit_now){
898
 
      e = EINTR;
899
 
      retval = -1;
900
 
    }
901
 
    errno = e;
 
847
  free(decrypted_buffer);
 
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;
902
858
  }
903
859
  return retval;
904
860
}
1020
976
  errno = old_errno;
1021
977
}
1022
978
 
 
979
/* 
 
980
 * This function determines if a directory entry in /sys/class/net
 
981
 * corresponds to an acceptable network device.
 
982
 * (This function is passed to scandir(3) as a filter function.)
 
983
 */
 
984
int good_interface(const struct dirent *if_entry){
 
985
  ssize_t ssret;
 
986
  char *flagname = NULL;
 
987
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
988
                     if_entry->d_name);
 
989
  if(ret < 0){
 
990
    perror("asprintf");
 
991
    return 0;
 
992
  }
 
993
  if(if_entry->d_name[0] == '.'){
 
994
    return 0;
 
995
  }
 
996
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
 
997
  if(flags_fd == -1){
 
998
    perror("open");
 
999
    return 0;
 
1000
  }
 
1001
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
 
1002
  /* read line from flags_fd */
 
1003
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1004
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1005
  flagstring[(size_t)to_read] = '\0';
 
1006
  if(flagstring == NULL){
 
1007
    perror("malloc");
 
1008
    close(flags_fd);
 
1009
    return 0;
 
1010
  }
 
1011
  while(to_read > 0){
 
1012
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1013
                                             (size_t)to_read));
 
1014
    if(ssret == -1){
 
1015
      perror("read");
 
1016
      free(flagstring);
 
1017
      close(flags_fd);
 
1018
      return 0;
 
1019
    }
 
1020
    to_read -= ssret;
 
1021
    if(ssret == 0){
 
1022
      break;
 
1023
    }
 
1024
  }
 
1025
  close(flags_fd);
 
1026
  intmax_t tmpmax;
 
1027
  char *tmp;
 
1028
  errno = 0;
 
1029
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1030
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1031
                                         and not (isspace(*tmp)))
 
1032
     or tmpmax != (ifreq_flags)tmpmax){
 
1033
    if(debug){
 
1034
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1035
              flagstring, if_entry->d_name);
 
1036
    }
 
1037
    free(flagstring);
 
1038
    return 0;
 
1039
  }
 
1040
  free(flagstring);
 
1041
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1042
  /* Reject the loopback device */
 
1043
  if(flags & IFF_LOOPBACK){
 
1044
    if(debug){
 
1045
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1046
              if_entry->d_name);
 
1047
    }
 
1048
    return 0;
 
1049
  }
 
1050
  /* Accept point-to-point devices only if connect_to is specified */
 
1051
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1052
    if(debug){
 
1053
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1054
              if_entry->d_name);
 
1055
    }
 
1056
    return 1;
 
1057
  }
 
1058
  /* Otherwise, reject non-broadcast-capable devices */
 
1059
  if(not (flags & IFF_BROADCAST)){
 
1060
    if(debug){
 
1061
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1062
              if_entry->d_name);
 
1063
    }
 
1064
    return 0;
 
1065
  }
 
1066
  /* Accept this device */
 
1067
  if(debug){
 
1068
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1069
            if_entry->d_name);
 
1070
  }
 
1071
  return 1;
 
1072
}
 
1073
 
1023
1074
int main(int argc, char *argv[]){
1024
1075
  AvahiSServiceBrowser *sb = NULL;
1025
1076
  int error;
1027
1078
  intmax_t tmpmax;
1028
1079
  char *tmp;
1029
1080
  int exitcode = EXIT_SUCCESS;
1030
 
  const char *interface = "eth0";
 
1081
  const char *interface = "";
1031
1082
  struct ifreq network;
1032
1083
  int sd = -1;
1033
1084
  bool take_down_interface = false;
1034
1085
  uid_t uid;
1035
1086
  gid_t gid;
1036
 
  char *connect_to = NULL;
1037
1087
  char tempdir[] = "/tmp/mandosXXXXXX";
1038
1088
  bool tempdir_created = false;
1039
1089
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1102
1152
        .arg = "SECONDS",
1103
1153
        .doc = "Maximum delay to wait for interface startup",
1104
1154
        .group = 2 },
1105
 
      /*
1106
 
       * These reproduce what we would get without ARGP_NO_HELP
1107
 
       */
1108
 
      { .name = "help", .key = '?',
1109
 
        .doc = "Give this help list", .group = -1 },
1110
 
      { .name = "usage", .key = -3,
1111
 
        .doc = "Give a short usage message", .group = -1 },
1112
 
      { .name = "version", .key = 'V',
1113
 
        .doc = "Print program version", .group = -1 },
1114
1155
      { .name = NULL }
1115
1156
    };
1116
1157
    
1117
1158
    error_t parse_opt(int key, char *arg,
1118
1159
                      struct argp_state *state){
1119
 
      errno = 0;
1120
1160
      switch(key){
1121
1161
      case 128:                 /* --debug */
1122
1162
        debug = true;
1138
1178
        tmpmax = strtoimax(arg, &tmp, 10);
1139
1179
        if(errno != 0 or tmp == arg or *tmp != '\0'
1140
1180
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1141
 
          argp_error(state, "Bad number of DH bits");
 
1181
          fprintf(stderr, "Bad number of DH bits\n");
 
1182
          exit(EXIT_FAILURE);
1142
1183
        }
1143
1184
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1144
1185
        break;
1149
1190
        errno = 0;
1150
1191
        delay = strtof(arg, &tmp);
1151
1192
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1152
 
          argp_error(state, "Bad delay");
 
1193
          fprintf(stderr, "Bad delay\n");
 
1194
          exit(EXIT_FAILURE);
1153
1195
        }
1154
1196
        break;
1155
 
        /*
1156
 
         * These reproduce what we would get without ARGP_NO_HELP
1157
 
         */
1158
 
      case '?':                 /* --help */
1159
 
        argp_state_help(state, state->out_stream,
1160
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1161
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1162
 
      case -3:                  /* --usage */
1163
 
        argp_state_help(state, state->out_stream,
1164
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1165
 
      case 'V':                 /* --version */
1166
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1167
 
        exit(argp_err_exit_status);
 
1197
      case ARGP_KEY_ARG:
 
1198
        argp_usage(state);
 
1199
      case ARGP_KEY_END:
1168
1200
        break;
1169
1201
      default:
1170
1202
        return ARGP_ERR_UNKNOWN;
1171
1203
      }
1172
 
      return errno;
 
1204
      return 0;
1173
1205
    }
1174
1206
    
1175
1207
    struct argp argp = { .options = options, .parser = parse_opt,
1176
1208
                         .args_doc = "",
1177
1209
                         .doc = "Mandos client -- Get and decrypt"
1178
1210
                         " passwords from a Mandos server" };
1179
 
    ret = argp_parse(&argp, argc, argv,
1180
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1181
 
    switch(ret){
1182
 
    case 0:
1183
 
      break;
1184
 
    case ENOMEM:
1185
 
    default:
1186
 
      errno = ret;
1187
 
      perror("argp_parse");
1188
 
      exitcode = EX_OSERR;
1189
 
      goto end;
1190
 
    case EINVAL:
1191
 
      exitcode = EX_USAGE;
 
1211
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1212
    if(ret == ARGP_ERR_UNKNOWN){
 
1213
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1214
      exitcode = EXIT_FAILURE;
1192
1215
      goto end;
1193
1216
    }
1194
1217
  }
1196
1219
  if(not debug){
1197
1220
    avahi_set_log_function(empty_log);
1198
1221
  }
 
1222
 
 
1223
  if(interface[0] == '\0'){
 
1224
    struct dirent **direntries;
 
1225
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1226
                  alphasort);
 
1227
    if(ret >= 1){
 
1228
      /* Pick the first good interface */
 
1229
      interface = strdup(direntries[0]->d_name);
 
1230
      if(debug){
 
1231
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1232
      }
 
1233
      if(interface == NULL){
 
1234
        perror("malloc");
 
1235
        free(direntries);
 
1236
        exitcode = EXIT_FAILURE;
 
1237
        goto end;
 
1238
      }
 
1239
      free(direntries);
 
1240
    } else {
 
1241
      free(direntries);
 
1242
      fprintf(stderr, "Could not find a network interface\n");
 
1243
      exitcode = EXIT_FAILURE;
 
1244
      goto end;
 
1245
    }
 
1246
  }
1199
1247
  
1200
1248
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1201
1249
     from the signal handler */
1204
1252
  mc.simple_poll = avahi_simple_poll_new();
1205
1253
  if(mc.simple_poll == NULL){
1206
1254
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1207
 
    exitcode = EX_UNAVAILABLE;
 
1255
    exitcode = EXIT_FAILURE;
1208
1256
    goto end;
1209
1257
  }
1210
1258
  
1212
1260
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1213
1261
  if(ret == -1){
1214
1262
    perror("sigaddset");
1215
 
    exitcode = EX_OSERR;
 
1263
    exitcode = EXIT_FAILURE;
1216
1264
    goto end;
1217
1265
  }
1218
1266
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1219
1267
  if(ret == -1){
1220
1268
    perror("sigaddset");
1221
 
    exitcode = EX_OSERR;
 
1269
    exitcode = EXIT_FAILURE;
1222
1270
    goto end;
1223
1271
  }
1224
1272
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1225
1273
  if(ret == -1){
1226
1274
    perror("sigaddset");
1227
 
    exitcode = EX_OSERR;
 
1275
    exitcode = EXIT_FAILURE;
1228
1276
    goto end;
1229
1277
  }
1230
1278
  /* Need to check if the handler is SIG_IGN before handling:
1234
1282
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1235
1283
  if(ret == -1){
1236
1284
    perror("sigaction");
1237
 
    return EX_OSERR;
 
1285
    return EXIT_FAILURE;
1238
1286
  }
1239
1287
  if(old_sigterm_action.sa_handler != SIG_IGN){
1240
1288
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1241
1289
    if(ret == -1){
1242
1290
      perror("sigaction");
1243
 
      exitcode = EX_OSERR;
 
1291
      exitcode = EXIT_FAILURE;
1244
1292
      goto end;
1245
1293
    }
1246
1294
  }
1247
1295
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1248
1296
  if(ret == -1){
1249
1297
    perror("sigaction");
1250
 
    return EX_OSERR;
 
1298
    return EXIT_FAILURE;
1251
1299
  }
1252
1300
  if(old_sigterm_action.sa_handler != SIG_IGN){
1253
1301
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1254
1302
    if(ret == -1){
1255
1303
      perror("sigaction");
1256
 
      exitcode = EX_OSERR;
 
1304
      exitcode = EXIT_FAILURE;
1257
1305
      goto end;
1258
1306
    }
1259
1307
  }
1260
1308
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1261
1309
  if(ret == -1){
1262
1310
    perror("sigaction");
1263
 
    return EX_OSERR;
 
1311
    return EXIT_FAILURE;
1264
1312
  }
1265
1313
  if(old_sigterm_action.sa_handler != SIG_IGN){
1266
1314
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1267
1315
    if(ret == -1){
1268
1316
      perror("sigaction");
1269
 
      exitcode = EX_OSERR;
 
1317
      exitcode = EXIT_FAILURE;
1270
1318
      goto end;
1271
1319
    }
1272
1320
  }
1273
1321
  
1274
1322
  /* If the interface is down, bring it up */
1275
 
  if(interface[0] != '\0'){
 
1323
  if(strcmp(interface, "none") != 0){
1276
1324
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1277
1325
    if(if_index == 0){
1278
1326
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1279
 
      exitcode = EX_UNAVAILABLE;
 
1327
      exitcode = EXIT_FAILURE;
1280
1328
      goto end;
1281
1329
    }
1282
1330
    
1293
1341
    
1294
1342
#ifdef __linux__
1295
1343
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1296
 
       messages about the network interface to mess up the prompt */
 
1344
       messages to mess up the prompt */
1297
1345
    ret = klogctl(8, NULL, 5);
1298
1346
    bool restore_loglevel = true;
1299
1347
    if(ret == -1){
1305
1353
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1306
1354
    if(sd < 0){
1307
1355
      perror("socket");
1308
 
      exitcode = EX_OSERR;
 
1356
      exitcode = EXIT_FAILURE;
1309
1357
#ifdef __linux__
1310
1358
      if(restore_loglevel){
1311
1359
        ret = klogctl(7, NULL, 0);
1334
1382
        }
1335
1383
      }
1336
1384
#endif  /* __linux__ */
1337
 
      exitcode = EX_OSERR;
 
1385
      exitcode = EXIT_FAILURE;
1338
1386
      /* Lower privileges */
1339
1387
      errno = 0;
1340
1388
      ret = seteuid(uid);
1349
1397
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1350
1398
      if(ret == -1){
1351
1399
        take_down_interface = false;
1352
 
        perror("ioctl SIOCSIFFLAGS");
1353
 
        exitcode = EX_OSERR;
 
1400
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1401
        exitcode = EXIT_FAILURE;
1354
1402
#ifdef __linux__
1355
1403
        if(restore_loglevel){
1356
1404
          ret = klogctl(7, NULL, 0);
1422
1470
  ret = init_gnutls_global(pubkey, seckey);
1423
1471
  if(ret == -1){
1424
1472
    fprintf(stderr, "init_gnutls_global failed\n");
1425
 
    exitcode = EX_UNAVAILABLE;
 
1473
    exitcode = EXIT_FAILURE;
1426
1474
    goto end;
1427
1475
  } else {
1428
1476
    gnutls_initialized = true;
1445
1493
  
1446
1494
  if(not init_gpgme(pubkey, seckey, tempdir)){
1447
1495
    fprintf(stderr, "init_gpgme failed\n");
1448
 
    exitcode = EX_UNAVAILABLE;
 
1496
    exitcode = EXIT_FAILURE;
1449
1497
    goto end;
1450
1498
  } else {
1451
1499
    gpgme_initialized = true;
1461
1509
    char *address = strrchr(connect_to, ':');
1462
1510
    if(address == NULL){
1463
1511
      fprintf(stderr, "No colon in address\n");
1464
 
      exitcode = EX_USAGE;
 
1512
      exitcode = EXIT_FAILURE;
1465
1513
      goto end;
1466
1514
    }
1467
1515
    
1475
1523
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1476
1524
       or tmpmax != (uint16_t)tmpmax){
1477
1525
      fprintf(stderr, "Bad port number\n");
1478
 
      exitcode = EX_USAGE;
 
1526
      exitcode = EXIT_FAILURE;
1479
1527
      goto end;
1480
1528
    }
1481
1529
  
1500
1548
    
1501
1549
    ret = start_mandos_communication(address, port, if_index, af);
1502
1550
    if(ret < 0){
1503
 
      switch(errno){
1504
 
      case ENETUNREACH:
1505
 
      case EHOSTDOWN:
1506
 
      case EHOSTUNREACH:
1507
 
        exitcode = EX_NOHOST;
1508
 
        break;
1509
 
      case EINVAL:
1510
 
        exitcode = EX_USAGE;
1511
 
        break;
1512
 
      case EIO:
1513
 
        exitcode = EX_IOERR;
1514
 
        break;
1515
 
      case EPROTO:
1516
 
        exitcode = EX_PROTOCOL;
1517
 
        break;
1518
 
      default:
1519
 
        exitcode = EX_OSERR;
1520
 
        break;
1521
 
      }
 
1551
      exitcode = EXIT_FAILURE;
1522
1552
    } else {
1523
1553
      exitcode = EXIT_SUCCESS;
1524
1554
    }
1551
1581
  if(mc.server == NULL){
1552
1582
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1553
1583
            avahi_strerror(error));
1554
 
    exitcode = EX_UNAVAILABLE;
 
1584
    exitcode = EXIT_FAILURE;
1555
1585
    goto end;
1556
1586
  }
1557
1587
  
1566
1596
  if(sb == NULL){
1567
1597
    fprintf(stderr, "Failed to create service browser: %s\n",
1568
1598
            avahi_strerror(avahi_server_errno(mc.server)));
1569
 
    exitcode = EX_UNAVAILABLE;
 
1599
    exitcode = EXIT_FAILURE;
1570
1600
    goto end;
1571
1601
  }
1572
1602
  
1621
1651
      if(ret == -1){
1622
1652
        perror("ioctl SIOCGIFFLAGS");
1623
1653
      } else if(network.ifr_flags & IFF_UP) {
1624
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1654
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1625
1655
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1626
1656
        if(ret == -1){
1627
 
          perror("ioctl SIOCSIFFLAGS");
 
1657
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1628
1658
        }
1629
1659
      }
1630
1660
      ret = (int)TEMP_FAILURE_RETRY(close(sd));