/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

* Makefile: Merge branch adding warning messages to "run-*" targets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
47
                                   strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
83
                                   sigaction(), SIGTERM, sig_atomic_t,
84
84
                                   raise() */
 
85
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
 
86
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
85
87
 
86
88
#ifdef __linux__
87
89
#include <sys/klog.h>           /* klogctl() */
125
127
static const char mandos_protocol_version[] = "1";
126
128
const char *argp_program_version = "mandos-client " VERSION;
127
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
130
static const char sys_class_net[] = "/sys/class/net";
 
131
char *connect_to = NULL;
128
132
 
129
133
/* Used for passing in values through the Avahi callback functions */
130
134
typedef struct {
552
556
  gnutls_session_t session;
553
557
  int pf;                       /* Protocol family */
554
558
  
 
559
  errno = 0;
 
560
  
555
561
  if(quit_now){
 
562
    errno = EINTR;
556
563
    return -1;
557
564
  }
558
565
  
565
572
    break;
566
573
  default:
567
574
    fprintf(stderr, "Bad address family: %d\n", af);
 
575
    errno = EINVAL;
568
576
    return -1;
569
577
  }
570
578
  
580
588
  
581
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
582
590
  if(tcp_sd < 0){
 
591
    int e = errno;
583
592
    perror("socket");
 
593
    errno = e;
584
594
    goto mandos_end;
585
595
  }
586
596
  
587
597
  if(quit_now){
 
598
    errno = EINTR;
588
599
    goto mandos_end;
589
600
  }
590
601
  
597
608
    ret = inet_pton(af, ip, &to.in.sin_addr);
598
609
  }
599
610
  if(ret < 0 ){
 
611
    int e = errno;
600
612
    perror("inet_pton");
 
613
    errno = e;
601
614
    goto mandos_end;
602
615
  }
603
616
  if(ret == 0){
 
617
    int e = errno;
604
618
    fprintf(stderr, "Bad address: %s\n", ip);
 
619
    errno = e;
605
620
    goto mandos_end;
606
621
  }
607
622
  if(af == AF_INET6){
615
630
      if(if_index == AVAHI_IF_UNSPEC){
616
631
        fprintf(stderr, "An IPv6 link-local address is incomplete"
617
632
                " without a network interface\n");
 
633
        errno = EINVAL;
618
634
        goto mandos_end;
619
635
      }
620
636
      /* Set the network interface number as scope */
627
643
  }
628
644
  
629
645
  if(quit_now){
 
646
    errno = EINTR;
630
647
    goto mandos_end;
631
648
  }
632
649
  
663
680
  }
664
681
  
665
682
  if(quit_now){
 
683
    errno = EINTR;
666
684
    goto mandos_end;
667
685
  }
668
686
  
672
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
673
691
  }
674
692
  if(ret < 0){
 
693
    int e = errno;
675
694
    perror("connect");
 
695
    errno = e;
676
696
    goto mandos_end;
677
697
  }
678
698
  
679
699
  if(quit_now){
 
700
    errno = EINTR;
680
701
    goto mandos_end;
681
702
  }
682
703
  
687
708
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
688
709
                                   out_size - written));
689
710
    if(ret == -1){
 
711
      int e = errno;
690
712
      perror("write");
 
713
      errno = e;
691
714
      goto mandos_end;
692
715
    }
693
716
    written += (size_t)ret;
703
726
    }
704
727
  
705
728
    if(quit_now){
 
729
      errno = EINTR;
706
730
      goto mandos_end;
707
731
    }
708
732
  }
712
736
  }
713
737
  
714
738
  if(quit_now){
 
739
    errno = EINTR;
715
740
    goto mandos_end;
716
741
  }
717
742
  
718
743
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
719
744
  
720
745
  if(quit_now){
 
746
    errno = EINTR;
721
747
    goto mandos_end;
722
748
  }
723
749
  
724
750
  do {
725
751
    ret = gnutls_handshake(session);
726
752
    if(quit_now){
 
753
      errno = EINTR;
727
754
      goto mandos_end;
728
755
    }
729
756
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
733
760
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
734
761
      gnutls_perror(ret);
735
762
    }
 
763
    errno = EPROTO;
736
764
    goto mandos_end;
737
765
  }
738
766
  
746
774
  while(true){
747
775
    
748
776
    if(quit_now){
 
777
      errno = EINTR;
749
778
      goto mandos_end;
750
779
    }
751
780
    
752
781
    buffer_capacity = incbuffer(&buffer, buffer_length,
753
782
                                   buffer_capacity);
754
783
    if(buffer_capacity == 0){
 
784
      int e = errno;
755
785
      perror("incbuffer");
 
786
      errno = e;
756
787
      goto mandos_end;
757
788
    }
758
789
    
759
790
    if(quit_now){
 
791
      errno = EINTR;
760
792
      goto mandos_end;
761
793
    }
762
794
    
775
807
          ret = gnutls_handshake(session);
776
808
          
777
809
          if(quit_now){
 
810
            errno = EINTR;
778
811
            goto mandos_end;
779
812
          }
780
813
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
781
814
        if(ret < 0){
782
815
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
783
816
          gnutls_perror(ret);
 
817
          errno = EPROTO;
784
818
          goto mandos_end;
785
819
        }
786
820
        break;
788
822
        fprintf(stderr, "Unknown error while reading data from"
789
823
                " encrypted session with Mandos server\n");
790
824
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
825
        errno = EIO;
791
826
        goto mandos_end;
792
827
      }
793
828
    } else {
800
835
  }
801
836
  
802
837
  if(quit_now){
 
838
    errno = EINTR;
803
839
    goto mandos_end;
804
840
  }
805
841
  
806
842
  do {
807
843
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
808
844
    if(quit_now){
 
845
      errno = EINTR;
809
846
      goto mandos_end;
810
847
    }
811
848
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
820
857
      written = 0;
821
858
      while(written < (size_t) decrypted_buffer_size){
822
859
        if(quit_now){
 
860
          errno = EINTR;
823
861
          goto mandos_end;
824
862
        }
825
863
        
827
865
                          (size_t)decrypted_buffer_size - written,
828
866
                          stdout);
829
867
        if(ret == 0 and ferror(stdout)){
 
868
          int e = errno;
830
869
          if(debug){
831
870
            fprintf(stderr, "Error writing encrypted data: %s\n",
832
871
                    strerror(errno));
833
872
          }
 
873
          errno = e;
834
874
          goto mandos_end;
835
875
        }
836
876
        written += (size_t)ret;
842
882
  /* Shutdown procedure */
843
883
  
844
884
 mandos_end:
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;
 
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;
856
904
  }
857
905
  return retval;
858
906
}
974
1022
  errno = old_errno;
975
1023
}
976
1024
 
 
1025
/* 
 
1026
 * This function determines if a directory entry in /sys/class/net
 
1027
 * corresponds to an acceptable network device.
 
1028
 * (This function is passed to scandir(3) as a filter function.)
 
1029
 */
 
1030
int good_interface(const struct dirent *if_entry){
 
1031
  ssize_t ssret;
 
1032
  char *flagname = NULL;
 
1033
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
1034
                     if_entry->d_name);
 
1035
  if(ret < 0){
 
1036
    perror("asprintf");
 
1037
    return 0;
 
1038
  }
 
1039
  if(if_entry->d_name[0] == '.'){
 
1040
    return 0;
 
1041
  }
 
1042
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
 
1043
  if(flags_fd == -1){
 
1044
    perror("open");
 
1045
    return 0;
 
1046
  }
 
1047
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
 
1048
  /* read line from flags_fd */
 
1049
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1050
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1051
  flagstring[(size_t)to_read] = '\0';
 
1052
  if(flagstring == NULL){
 
1053
    perror("malloc");
 
1054
    close(flags_fd);
 
1055
    return 0;
 
1056
  }
 
1057
  while(to_read > 0){
 
1058
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1059
                                             (size_t)to_read));
 
1060
    if(ssret == -1){
 
1061
      perror("read");
 
1062
      free(flagstring);
 
1063
      close(flags_fd);
 
1064
      return 0;
 
1065
    }
 
1066
    to_read -= ssret;
 
1067
    if(ssret == 0){
 
1068
      break;
 
1069
    }
 
1070
  }
 
1071
  close(flags_fd);
 
1072
  intmax_t tmpmax;
 
1073
  char *tmp;
 
1074
  errno = 0;
 
1075
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1076
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1077
                                         and not (isspace(*tmp)))
 
1078
     or tmpmax != (ifreq_flags)tmpmax){
 
1079
    if(debug){
 
1080
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1081
              flagstring, if_entry->d_name);
 
1082
    }
 
1083
    free(flagstring);
 
1084
    return 0;
 
1085
  }
 
1086
  free(flagstring);
 
1087
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1088
  /* Reject the loopback device */
 
1089
  if(flags & IFF_LOOPBACK){
 
1090
    if(debug){
 
1091
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1092
              if_entry->d_name);
 
1093
    }
 
1094
    return 0;
 
1095
  }
 
1096
  /* Accept point-to-point devices only if connect_to is specified */
 
1097
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1098
    if(debug){
 
1099
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1100
              if_entry->d_name);
 
1101
    }
 
1102
    return 1;
 
1103
  }
 
1104
  /* Otherwise, reject non-broadcast-capable devices */
 
1105
  if(not (flags & IFF_BROADCAST)){
 
1106
    if(debug){
 
1107
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1108
              if_entry->d_name);
 
1109
    }
 
1110
    return 0;
 
1111
  }
 
1112
  /* Accept this device */
 
1113
  if(debug){
 
1114
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1115
            if_entry->d_name);
 
1116
  }
 
1117
  return 1;
 
1118
}
 
1119
 
977
1120
int main(int argc, char *argv[]){
978
1121
  AvahiSServiceBrowser *sb = NULL;
979
1122
  int error;
981
1124
  intmax_t tmpmax;
982
1125
  char *tmp;
983
1126
  int exitcode = EXIT_SUCCESS;
984
 
  const char *interface = "eth0";
 
1127
  const char *interface = "";
985
1128
  struct ifreq network;
986
1129
  int sd = -1;
987
1130
  bool take_down_interface = false;
988
1131
  uid_t uid;
989
1132
  gid_t gid;
990
 
  char *connect_to = NULL;
991
1133
  char tempdir[] = "/tmp/mandosXXXXXX";
992
1134
  bool tempdir_created = false;
993
1135
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1056
1198
        .arg = "SECONDS",
1057
1199
        .doc = "Maximum delay to wait for interface startup",
1058
1200
        .group = 2 },
 
1201
      /*
 
1202
       * These reproduce what we would get without ARGP_NO_HELP
 
1203
       */
 
1204
      { .name = "help", .key = '?',
 
1205
        .doc = "Give this help list", .group = -1 },
 
1206
      { .name = "usage", .key = -3,
 
1207
        .doc = "Give a short usage message", .group = -1 },
 
1208
      { .name = "version", .key = 'V',
 
1209
        .doc = "Print program version", .group = -1 },
1059
1210
      { .name = NULL }
1060
1211
    };
1061
1212
    
1062
1213
    error_t parse_opt(int key, char *arg,
1063
1214
                      struct argp_state *state){
 
1215
      errno = 0;
1064
1216
      switch(key){
1065
1217
      case 128:                 /* --debug */
1066
1218
        debug = true;
1082
1234
        tmpmax = strtoimax(arg, &tmp, 10);
1083
1235
        if(errno != 0 or tmp == arg or *tmp != '\0'
1084
1236
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1085
 
          fprintf(stderr, "Bad number of DH bits\n");
1086
 
          exit(EXIT_FAILURE);
 
1237
          argp_error(state, "Bad number of DH bits");
1087
1238
        }
1088
1239
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1089
1240
        break;
1094
1245
        errno = 0;
1095
1246
        delay = strtof(arg, &tmp);
1096
1247
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1097
 
          fprintf(stderr, "Bad delay\n");
1098
 
          exit(EXIT_FAILURE);
 
1248
          argp_error(state, "Bad delay");
1099
1249
        }
1100
1250
        break;
1101
 
      case ARGP_KEY_ARG:
1102
 
        argp_usage(state);
1103
 
      case ARGP_KEY_END:
 
1251
        /*
 
1252
         * These reproduce what we would get without ARGP_NO_HELP
 
1253
         */
 
1254
      case '?':                 /* --help */
 
1255
        argp_state_help(state, state->out_stream,
 
1256
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1257
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1258
      case -3:                  /* --usage */
 
1259
        argp_state_help(state, state->out_stream,
 
1260
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1261
      case 'V':                 /* --version */
 
1262
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1263
        exit(argp_err_exit_status);
1104
1264
        break;
1105
1265
      default:
1106
1266
        return ARGP_ERR_UNKNOWN;
1107
1267
      }
1108
 
      return 0;
 
1268
      return errno;
1109
1269
    }
1110
1270
    
1111
1271
    struct argp argp = { .options = options, .parser = parse_opt,
1112
1272
                         .args_doc = "",
1113
1273
                         .doc = "Mandos client -- Get and decrypt"
1114
1274
                         " passwords from a Mandos server" };
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;
 
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;
1119
1288
      goto end;
1120
1289
    }
1121
1290
  }
1123
1292
  if(not debug){
1124
1293
    avahi_set_log_function(empty_log);
1125
1294
  }
 
1295
 
 
1296
  if(interface[0] == '\0'){
 
1297
    struct dirent **direntries;
 
1298
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1299
                  alphasort);
 
1300
    if(ret >= 1){
 
1301
      /* Pick the first good interface */
 
1302
      interface = strdup(direntries[0]->d_name);
 
1303
      if(debug){
 
1304
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1305
      }
 
1306
      if(interface == NULL){
 
1307
        perror("malloc");
 
1308
        free(direntries);
 
1309
        exitcode = EXIT_FAILURE;
 
1310
        goto end;
 
1311
      }
 
1312
      free(direntries);
 
1313
    } else {
 
1314
      free(direntries);
 
1315
      fprintf(stderr, "Could not find a network interface\n");
 
1316
      exitcode = EXIT_FAILURE;
 
1317
      goto end;
 
1318
    }
 
1319
  }
1126
1320
  
1127
1321
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1128
1322
     from the signal handler */
1131
1325
  mc.simple_poll = avahi_simple_poll_new();
1132
1326
  if(mc.simple_poll == NULL){
1133
1327
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1134
 
    exitcode = EXIT_FAILURE;
 
1328
    exitcode = EX_UNAVAILABLE;
1135
1329
    goto end;
1136
1330
  }
1137
1331
  
1139
1333
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1140
1334
  if(ret == -1){
1141
1335
    perror("sigaddset");
1142
 
    exitcode = EXIT_FAILURE;
 
1336
    exitcode = EX_OSERR;
1143
1337
    goto end;
1144
1338
  }
1145
1339
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1146
1340
  if(ret == -1){
1147
1341
    perror("sigaddset");
1148
 
    exitcode = EXIT_FAILURE;
 
1342
    exitcode = EX_OSERR;
1149
1343
    goto end;
1150
1344
  }
1151
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1152
1346
  if(ret == -1){
1153
1347
    perror("sigaddset");
1154
 
    exitcode = EXIT_FAILURE;
 
1348
    exitcode = EX_OSERR;
1155
1349
    goto end;
1156
1350
  }
1157
1351
  /* Need to check if the handler is SIG_IGN before handling:
1161
1355
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1162
1356
  if(ret == -1){
1163
1357
    perror("sigaction");
1164
 
    return EXIT_FAILURE;
 
1358
    return EX_OSERR;
1165
1359
  }
1166
1360
  if(old_sigterm_action.sa_handler != SIG_IGN){
1167
1361
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1168
1362
    if(ret == -1){
1169
1363
      perror("sigaction");
1170
 
      exitcode = EXIT_FAILURE;
 
1364
      exitcode = EX_OSERR;
1171
1365
      goto end;
1172
1366
    }
1173
1367
  }
1174
1368
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1175
1369
  if(ret == -1){
1176
1370
    perror("sigaction");
1177
 
    return EXIT_FAILURE;
 
1371
    return EX_OSERR;
1178
1372
  }
1179
1373
  if(old_sigterm_action.sa_handler != SIG_IGN){
1180
1374
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1181
1375
    if(ret == -1){
1182
1376
      perror("sigaction");
1183
 
      exitcode = EXIT_FAILURE;
 
1377
      exitcode = EX_OSERR;
1184
1378
      goto end;
1185
1379
    }
1186
1380
  }
1187
1381
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1188
1382
  if(ret == -1){
1189
1383
    perror("sigaction");
1190
 
    return EXIT_FAILURE;
 
1384
    return EX_OSERR;
1191
1385
  }
1192
1386
  if(old_sigterm_action.sa_handler != SIG_IGN){
1193
1387
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1194
1388
    if(ret == -1){
1195
1389
      perror("sigaction");
1196
 
      exitcode = EXIT_FAILURE;
 
1390
      exitcode = EX_OSERR;
1197
1391
      goto end;
1198
1392
    }
1199
1393
  }
1200
1394
  
1201
1395
  /* If the interface is down, bring it up */
1202
 
  if(interface[0] != '\0'){
 
1396
  if(strcmp(interface, "none") != 0){
1203
1397
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1204
1398
    if(if_index == 0){
1205
1399
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1206
 
      exitcode = EXIT_FAILURE;
 
1400
      exitcode = EX_UNAVAILABLE;
1207
1401
      goto end;
1208
1402
    }
1209
1403
    
1220
1414
    
1221
1415
#ifdef __linux__
1222
1416
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1223
 
       messages to mess up the prompt */
 
1417
       messages about the network interface to mess up the prompt */
1224
1418
    ret = klogctl(8, NULL, 5);
1225
1419
    bool restore_loglevel = true;
1226
1420
    if(ret == -1){
1232
1426
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1233
1427
    if(sd < 0){
1234
1428
      perror("socket");
1235
 
      exitcode = EXIT_FAILURE;
 
1429
      exitcode = EX_OSERR;
1236
1430
#ifdef __linux__
1237
1431
      if(restore_loglevel){
1238
1432
        ret = klogctl(7, NULL, 0);
1261
1455
        }
1262
1456
      }
1263
1457
#endif  /* __linux__ */
1264
 
      exitcode = EXIT_FAILURE;
 
1458
      exitcode = EX_OSERR;
1265
1459
      /* Lower privileges */
1266
1460
      errno = 0;
1267
1461
      ret = seteuid(uid);
1276
1470
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1277
1471
      if(ret == -1){
1278
1472
        take_down_interface = false;
1279
 
        perror("ioctl SIOCSIFFLAGS");
1280
 
        exitcode = EXIT_FAILURE;
 
1473
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1474
        exitcode = EX_OSERR;
1281
1475
#ifdef __linux__
1282
1476
        if(restore_loglevel){
1283
1477
          ret = klogctl(7, NULL, 0);
1349
1543
  ret = init_gnutls_global(pubkey, seckey);
1350
1544
  if(ret == -1){
1351
1545
    fprintf(stderr, "init_gnutls_global failed\n");
1352
 
    exitcode = EXIT_FAILURE;
 
1546
    exitcode = EX_UNAVAILABLE;
1353
1547
    goto end;
1354
1548
  } else {
1355
1549
    gnutls_initialized = true;
1372
1566
  
1373
1567
  if(not init_gpgme(pubkey, seckey, tempdir)){
1374
1568
    fprintf(stderr, "init_gpgme failed\n");
1375
 
    exitcode = EXIT_FAILURE;
 
1569
    exitcode = EX_UNAVAILABLE;
1376
1570
    goto end;
1377
1571
  } else {
1378
1572
    gpgme_initialized = true;
1388
1582
    char *address = strrchr(connect_to, ':');
1389
1583
    if(address == NULL){
1390
1584
      fprintf(stderr, "No colon in address\n");
1391
 
      exitcode = EXIT_FAILURE;
 
1585
      exitcode = EX_USAGE;
1392
1586
      goto end;
1393
1587
    }
1394
1588
    
1402
1596
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1403
1597
       or tmpmax != (uint16_t)tmpmax){
1404
1598
      fprintf(stderr, "Bad port number\n");
1405
 
      exitcode = EXIT_FAILURE;
 
1599
      exitcode = EX_USAGE;
1406
1600
      goto end;
1407
1601
    }
1408
1602
  
1427
1621
    
1428
1622
    ret = start_mandos_communication(address, port, if_index, af);
1429
1623
    if(ret < 0){
1430
 
      exitcode = EXIT_FAILURE;
 
1624
      switch(errno){
 
1625
      case ENETUNREACH:
 
1626
      case EHOSTDOWN:
 
1627
      case EHOSTUNREACH:
 
1628
        exitcode = EX_NOHOST;
 
1629
        break;
 
1630
      case EINVAL:
 
1631
        exitcode = EX_USAGE;
 
1632
        break;
 
1633
      case EIO:
 
1634
        exitcode = EX_IOERR;
 
1635
        break;
 
1636
      case EPROTO:
 
1637
        exitcode = EX_PROTOCOL;
 
1638
        break;
 
1639
      default:
 
1640
        exitcode = EX_OSERR;
 
1641
        break;
 
1642
      }
1431
1643
    } else {
1432
1644
      exitcode = EXIT_SUCCESS;
1433
1645
    }
1460
1672
  if(mc.server == NULL){
1461
1673
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1462
1674
            avahi_strerror(error));
1463
 
    exitcode = EXIT_FAILURE;
 
1675
    exitcode = EX_UNAVAILABLE;
1464
1676
    goto end;
1465
1677
  }
1466
1678
  
1475
1687
  if(sb == NULL){
1476
1688
    fprintf(stderr, "Failed to create service browser: %s\n",
1477
1689
            avahi_strerror(avahi_server_errno(mc.server)));
1478
 
    exitcode = EXIT_FAILURE;
 
1690
    exitcode = EX_UNAVAILABLE;
1479
1691
    goto end;
1480
1692
  }
1481
1693
  
1530
1742
      if(ret == -1){
1531
1743
        perror("ioctl SIOCGIFFLAGS");
1532
1744
      } else if(network.ifr_flags & IFF_UP) {
1533
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1534
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1535
1747
        if(ret == -1){
1536
 
          perror("ioctl SIOCSIFFLAGS");
 
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1537
1749
        }
1538
1750
      }
1539
1751
      ret = (int)TEMP_FAILURE_RETRY(close(sd));