/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 19:00:59 UTC
  • mto: This revision was merged to the branch mainline in revision 420.
  • Revision ID: teddy@fukt.bsnet.se-20100816190059-69vvi27up95cgbbl
* Makefile (run-server, run-client): Add notices about harmless error
                                     messages.

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>";
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 {
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;
594
584
    goto mandos_end;
595
585
  }
596
586
  
597
587
  if(quit_now){
598
 
    errno = EINTR;
599
588
    goto mandos_end;
600
589
  }
601
590
  
608
597
    ret = inet_pton(af, ip, &to.in.sin_addr);
609
598
  }
610
599
  if(ret < 0 ){
611
 
    int e = errno;
612
600
    perror("inet_pton");
613
 
    errno = e;
614
601
    goto mandos_end;
615
602
  }
616
603
  if(ret == 0){
617
 
    int e = errno;
618
604
    fprintf(stderr, "Bad address: %s\n", ip);
619
 
    errno = e;
620
605
    goto mandos_end;
621
606
  }
622
607
  if(af == AF_INET6){
630
615
      if(if_index == AVAHI_IF_UNSPEC){
631
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
632
617
                " without a network interface\n");
633
 
        errno = EINVAL;
634
618
        goto mandos_end;
635
619
      }
636
620
      /* Set the network interface number as scope */
643
627
  }
644
628
  
645
629
  if(quit_now){
646
 
    errno = EINTR;
647
630
    goto mandos_end;
648
631
  }
649
632
  
680
663
  }
681
664
  
682
665
  if(quit_now){
683
 
    errno = EINTR;
684
666
    goto mandos_end;
685
667
  }
686
668
  
690
672
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
673
  }
692
674
  if(ret < 0){
693
 
    int e = errno;
694
675
    perror("connect");
695
 
    errno = e;
696
676
    goto mandos_end;
697
677
  }
698
678
  
699
679
  if(quit_now){
700
 
    errno = EINTR;
701
680
    goto mandos_end;
702
681
  }
703
682
  
708
687
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
709
688
                                   out_size - written));
710
689
    if(ret == -1){
711
 
      int e = errno;
712
690
      perror("write");
713
 
      errno = e;
714
691
      goto mandos_end;
715
692
    }
716
693
    written += (size_t)ret;
726
703
    }
727
704
  
728
705
    if(quit_now){
729
 
      errno = EINTR;
730
706
      goto mandos_end;
731
707
    }
732
708
  }
736
712
  }
737
713
  
738
714
  if(quit_now){
739
 
    errno = EINTR;
740
715
    goto mandos_end;
741
716
  }
742
717
  
743
718
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
744
719
  
745
720
  if(quit_now){
746
 
    errno = EINTR;
747
721
    goto mandos_end;
748
722
  }
749
723
  
750
724
  do {
751
725
    ret = gnutls_handshake(session);
752
726
    if(quit_now){
753
 
      errno = EINTR;
754
727
      goto mandos_end;
755
728
    }
756
729
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
760
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
761
734
      gnutls_perror(ret);
762
735
    }
763
 
    errno = EPROTO;
764
736
    goto mandos_end;
765
737
  }
766
738
  
774
746
  while(true){
775
747
    
776
748
    if(quit_now){
777
 
      errno = EINTR;
778
749
      goto mandos_end;
779
750
    }
780
751
    
781
752
    buffer_capacity = incbuffer(&buffer, buffer_length,
782
753
                                   buffer_capacity);
783
754
    if(buffer_capacity == 0){
784
 
      int e = errno;
785
755
      perror("incbuffer");
786
 
      errno = e;
787
756
      goto mandos_end;
788
757
    }
789
758
    
790
759
    if(quit_now){
791
 
      errno = EINTR;
792
760
      goto mandos_end;
793
761
    }
794
762
    
807
775
          ret = gnutls_handshake(session);
808
776
          
809
777
          if(quit_now){
810
 
            errno = EINTR;
811
778
            goto mandos_end;
812
779
          }
813
780
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
814
781
        if(ret < 0){
815
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
816
783
          gnutls_perror(ret);
817
 
          errno = EPROTO;
818
784
          goto mandos_end;
819
785
        }
820
786
        break;
822
788
        fprintf(stderr, "Unknown error while reading data from"
823
789
                " encrypted session with Mandos server\n");
824
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
825
 
        errno = EIO;
826
791
        goto mandos_end;
827
792
      }
828
793
    } else {
835
800
  }
836
801
  
837
802
  if(quit_now){
838
 
    errno = EINTR;
839
803
    goto mandos_end;
840
804
  }
841
805
  
842
806
  do {
843
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
844
808
    if(quit_now){
845
 
      errno = EINTR;
846
809
      goto mandos_end;
847
810
    }
848
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
857
820
      written = 0;
858
821
      while(written < (size_t) decrypted_buffer_size){
859
822
        if(quit_now){
860
 
          errno = EINTR;
861
823
          goto mandos_end;
862
824
        }
863
825
        
865
827
                          (size_t)decrypted_buffer_size - written,
866
828
                          stdout);
867
829
        if(ret == 0 and ferror(stdout)){
868
 
          int e = errno;
869
830
          if(debug){
870
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
871
832
                    strerror(errno));
872
833
          }
873
 
          errno = e;
874
834
          goto mandos_end;
875
835
        }
876
836
        written += (size_t)ret;
882
842
  /* Shutdown procedure */
883
843
  
884
844
 mandos_end:
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;
 
845
  free(decrypted_buffer);
 
846
  free(buffer);
 
847
  if(tcp_sd >= 0){
 
848
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
849
  }
 
850
  if(ret == -1){
 
851
    perror("close");
 
852
  }
 
853
  gnutls_deinit(session);
 
854
  if(quit_now){
 
855
    retval = -1;
904
856
  }
905
857
  return retval;
906
858
}
1022
974
  errno = old_errno;
1023
975
}
1024
976
 
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
 
 
1120
977
int main(int argc, char *argv[]){
1121
978
  AvahiSServiceBrowser *sb = NULL;
1122
979
  int error;
1124
981
  intmax_t tmpmax;
1125
982
  char *tmp;
1126
983
  int exitcode = EXIT_SUCCESS;
1127
 
  const char *interface = "";
 
984
  const char *interface = "eth0";
1128
985
  struct ifreq network;
1129
986
  int sd = -1;
1130
987
  bool take_down_interface = false;
1131
988
  uid_t uid;
1132
989
  gid_t gid;
 
990
  char *connect_to = NULL;
1133
991
  char tempdir[] = "/tmp/mandosXXXXXX";
1134
992
  bool tempdir_created = false;
1135
993
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1198
1056
        .arg = "SECONDS",
1199
1057
        .doc = "Maximum delay to wait for interface startup",
1200
1058
        .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 },
1210
1059
      { .name = NULL }
1211
1060
    };
1212
1061
    
1213
1062
    error_t parse_opt(int key, char *arg,
1214
1063
                      struct argp_state *state){
1215
 
      errno = 0;
1216
1064
      switch(key){
1217
1065
      case 128:                 /* --debug */
1218
1066
        debug = true;
1234
1082
        tmpmax = strtoimax(arg, &tmp, 10);
1235
1083
        if(errno != 0 or tmp == arg or *tmp != '\0'
1236
1084
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1237
 
          argp_error(state, "Bad number of DH bits");
 
1085
          fprintf(stderr, "Bad number of DH bits\n");
 
1086
          exit(EXIT_FAILURE);
1238
1087
        }
1239
1088
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1240
1089
        break;
1245
1094
        errno = 0;
1246
1095
        delay = strtof(arg, &tmp);
1247
1096
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1248
 
          argp_error(state, "Bad delay");
 
1097
          fprintf(stderr, "Bad delay\n");
 
1098
          exit(EXIT_FAILURE);
1249
1099
        }
1250
1100
        break;
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);
 
1101
      case ARGP_KEY_ARG:
 
1102
        argp_usage(state);
 
1103
      case ARGP_KEY_END:
1264
1104
        break;
1265
1105
      default:
1266
1106
        return ARGP_ERR_UNKNOWN;
1267
1107
      }
1268
 
      return errno;
 
1108
      return 0;
1269
1109
    }
1270
1110
    
1271
1111
    struct argp argp = { .options = options, .parser = parse_opt,
1272
1112
                         .args_doc = "",
1273
1113
                         .doc = "Mandos client -- Get and decrypt"
1274
1114
                         " passwords from a Mandos server" };
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;
 
1115
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1116
    if(ret == ARGP_ERR_UNKNOWN){
 
1117
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1118
      exitcode = EXIT_FAILURE;
1288
1119
      goto end;
1289
1120
    }
1290
1121
  }
1292
1123
  if(not debug){
1293
1124
    avahi_set_log_function(empty_log);
1294
1125
  }
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
 
  }
1320
1126
  
1321
1127
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1322
1128
     from the signal handler */
1325
1131
  mc.simple_poll = avahi_simple_poll_new();
1326
1132
  if(mc.simple_poll == NULL){
1327
1133
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1328
 
    exitcode = EX_UNAVAILABLE;
 
1134
    exitcode = EXIT_FAILURE;
1329
1135
    goto end;
1330
1136
  }
1331
1137
  
1333
1139
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1334
1140
  if(ret == -1){
1335
1141
    perror("sigaddset");
1336
 
    exitcode = EX_OSERR;
 
1142
    exitcode = EXIT_FAILURE;
1337
1143
    goto end;
1338
1144
  }
1339
1145
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1340
1146
  if(ret == -1){
1341
1147
    perror("sigaddset");
1342
 
    exitcode = EX_OSERR;
 
1148
    exitcode = EXIT_FAILURE;
1343
1149
    goto end;
1344
1150
  }
1345
1151
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1346
1152
  if(ret == -1){
1347
1153
    perror("sigaddset");
1348
 
    exitcode = EX_OSERR;
 
1154
    exitcode = EXIT_FAILURE;
1349
1155
    goto end;
1350
1156
  }
1351
1157
  /* Need to check if the handler is SIG_IGN before handling:
1355
1161
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1356
1162
  if(ret == -1){
1357
1163
    perror("sigaction");
1358
 
    return EX_OSERR;
 
1164
    return EXIT_FAILURE;
1359
1165
  }
1360
1166
  if(old_sigterm_action.sa_handler != SIG_IGN){
1361
1167
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1362
1168
    if(ret == -1){
1363
1169
      perror("sigaction");
1364
 
      exitcode = EX_OSERR;
 
1170
      exitcode = EXIT_FAILURE;
1365
1171
      goto end;
1366
1172
    }
1367
1173
  }
1368
1174
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1369
1175
  if(ret == -1){
1370
1176
    perror("sigaction");
1371
 
    return EX_OSERR;
 
1177
    return EXIT_FAILURE;
1372
1178
  }
1373
1179
  if(old_sigterm_action.sa_handler != SIG_IGN){
1374
1180
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1375
1181
    if(ret == -1){
1376
1182
      perror("sigaction");
1377
 
      exitcode = EX_OSERR;
 
1183
      exitcode = EXIT_FAILURE;
1378
1184
      goto end;
1379
1185
    }
1380
1186
  }
1381
1187
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1382
1188
  if(ret == -1){
1383
1189
    perror("sigaction");
1384
 
    return EX_OSERR;
 
1190
    return EXIT_FAILURE;
1385
1191
  }
1386
1192
  if(old_sigterm_action.sa_handler != SIG_IGN){
1387
1193
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1388
1194
    if(ret == -1){
1389
1195
      perror("sigaction");
1390
 
      exitcode = EX_OSERR;
 
1196
      exitcode = EXIT_FAILURE;
1391
1197
      goto end;
1392
1198
    }
1393
1199
  }
1394
1200
  
1395
1201
  /* If the interface is down, bring it up */
1396
 
  if(strcmp(interface, "none") != 0){
 
1202
  if(interface[0] != '\0'){
1397
1203
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1398
1204
    if(if_index == 0){
1399
1205
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1400
 
      exitcode = EX_UNAVAILABLE;
 
1206
      exitcode = EXIT_FAILURE;
1401
1207
      goto end;
1402
1208
    }
1403
1209
    
1414
1220
    
1415
1221
#ifdef __linux__
1416
1222
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1417
 
       messages about the network interface to mess up the prompt */
 
1223
       messages to mess up the prompt */
1418
1224
    ret = klogctl(8, NULL, 5);
1419
1225
    bool restore_loglevel = true;
1420
1226
    if(ret == -1){
1426
1232
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1427
1233
    if(sd < 0){
1428
1234
      perror("socket");
1429
 
      exitcode = EX_OSERR;
 
1235
      exitcode = EXIT_FAILURE;
1430
1236
#ifdef __linux__
1431
1237
      if(restore_loglevel){
1432
1238
        ret = klogctl(7, NULL, 0);
1455
1261
        }
1456
1262
      }
1457
1263
#endif  /* __linux__ */
1458
 
      exitcode = EX_OSERR;
 
1264
      exitcode = EXIT_FAILURE;
1459
1265
      /* Lower privileges */
1460
1266
      errno = 0;
1461
1267
      ret = seteuid(uid);
1470
1276
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1471
1277
      if(ret == -1){
1472
1278
        take_down_interface = false;
1473
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1474
 
        exitcode = EX_OSERR;
 
1279
        perror("ioctl SIOCSIFFLAGS");
 
1280
        exitcode = EXIT_FAILURE;
1475
1281
#ifdef __linux__
1476
1282
        if(restore_loglevel){
1477
1283
          ret = klogctl(7, NULL, 0);
1543
1349
  ret = init_gnutls_global(pubkey, seckey);
1544
1350
  if(ret == -1){
1545
1351
    fprintf(stderr, "init_gnutls_global failed\n");
1546
 
    exitcode = EX_UNAVAILABLE;
 
1352
    exitcode = EXIT_FAILURE;
1547
1353
    goto end;
1548
1354
  } else {
1549
1355
    gnutls_initialized = true;
1566
1372
  
1567
1373
  if(not init_gpgme(pubkey, seckey, tempdir)){
1568
1374
    fprintf(stderr, "init_gpgme failed\n");
1569
 
    exitcode = EX_UNAVAILABLE;
 
1375
    exitcode = EXIT_FAILURE;
1570
1376
    goto end;
1571
1377
  } else {
1572
1378
    gpgme_initialized = true;
1582
1388
    char *address = strrchr(connect_to, ':');
1583
1389
    if(address == NULL){
1584
1390
      fprintf(stderr, "No colon in address\n");
1585
 
      exitcode = EX_USAGE;
 
1391
      exitcode = EXIT_FAILURE;
1586
1392
      goto end;
1587
1393
    }
1588
1394
    
1596
1402
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1597
1403
       or tmpmax != (uint16_t)tmpmax){
1598
1404
      fprintf(stderr, "Bad port number\n");
1599
 
      exitcode = EX_USAGE;
 
1405
      exitcode = EXIT_FAILURE;
1600
1406
      goto end;
1601
1407
    }
1602
1408
  
1621
1427
    
1622
1428
    ret = start_mandos_communication(address, port, if_index, af);
1623
1429
    if(ret < 0){
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
 
      }
 
1430
      exitcode = EXIT_FAILURE;
1643
1431
    } else {
1644
1432
      exitcode = EXIT_SUCCESS;
1645
1433
    }
1672
1460
  if(mc.server == NULL){
1673
1461
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1674
1462
            avahi_strerror(error));
1675
 
    exitcode = EX_UNAVAILABLE;
 
1463
    exitcode = EXIT_FAILURE;
1676
1464
    goto end;
1677
1465
  }
1678
1466
  
1687
1475
  if(sb == NULL){
1688
1476
    fprintf(stderr, "Failed to create service browser: %s\n",
1689
1477
            avahi_strerror(avahi_server_errno(mc.server)));
1690
 
    exitcode = EX_UNAVAILABLE;
 
1478
    exitcode = EXIT_FAILURE;
1691
1479
    goto end;
1692
1480
  }
1693
1481
  
1742
1530
      if(ret == -1){
1743
1531
        perror("ioctl SIOCGIFFLAGS");
1744
1532
      } else if(network.ifr_flags & IFF_UP) {
1745
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1533
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1746
1534
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1747
1535
        if(ret == -1){
1748
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1536
          perror("ioctl SIOCSIFFLAGS");
1749
1537
        }
1750
1538
      }
1751
1539
      ret = (int)TEMP_FAILURE_RETRY(close(sd));