/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

* mandos: Added ClientDBus.approve_pending property.  Exposed
          approved_by_default, approved_delay, approved_duration as
          D-Bus properties.

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>";
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
  
557
559
  if(quit_now){
 
560
    errno = EINTR;
558
561
    return -1;
559
562
  }
560
563
  
567
570
    break;
568
571
  default:
569
572
    fprintf(stderr, "Bad address family: %d\n", af);
 
573
    errno = EINVAL;
570
574
    return -1;
571
575
  }
572
576
  
582
586
  
583
587
  tcp_sd = socket(pf, SOCK_STREAM, 0);
584
588
  if(tcp_sd < 0){
 
589
    int e = errno;
585
590
    perror("socket");
 
591
    errno = e;
586
592
    goto mandos_end;
587
593
  }
588
594
  
589
595
  if(quit_now){
 
596
    errno = EINTR;
590
597
    goto mandos_end;
591
598
  }
592
599
  
599
606
    ret = inet_pton(af, ip, &to.in.sin_addr);
600
607
  }
601
608
  if(ret < 0 ){
 
609
    int e = errno;
602
610
    perror("inet_pton");
 
611
    errno = e;
603
612
    goto mandos_end;
604
613
  }
605
614
  if(ret == 0){
 
615
    int e = errno;
606
616
    fprintf(stderr, "Bad address: %s\n", ip);
 
617
    errno = e;
607
618
    goto mandos_end;
608
619
  }
609
620
  if(af == AF_INET6){
617
628
      if(if_index == AVAHI_IF_UNSPEC){
618
629
        fprintf(stderr, "An IPv6 link-local address is incomplete"
619
630
                " without a network interface\n");
 
631
        errno = EINVAL;
620
632
        goto mandos_end;
621
633
      }
622
634
      /* Set the network interface number as scope */
629
641
  }
630
642
  
631
643
  if(quit_now){
 
644
    errno = EINTR;
632
645
    goto mandos_end;
633
646
  }
634
647
  
665
678
  }
666
679
  
667
680
  if(quit_now){
 
681
    errno = EINTR;
668
682
    goto mandos_end;
669
683
  }
670
684
  
674
688
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
675
689
  }
676
690
  if(ret < 0){
 
691
    int e = errno;
677
692
    perror("connect");
 
693
    errno = e;
678
694
    goto mandos_end;
679
695
  }
680
696
  
681
697
  if(quit_now){
 
698
    errno = EINTR;
682
699
    goto mandos_end;
683
700
  }
684
701
  
689
706
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
690
707
                                   out_size - written));
691
708
    if(ret == -1){
 
709
      int e = errno;
692
710
      perror("write");
 
711
      errno = e;
693
712
      goto mandos_end;
694
713
    }
695
714
    written += (size_t)ret;
705
724
    }
706
725
  
707
726
    if(quit_now){
 
727
      errno = EINTR;
708
728
      goto mandos_end;
709
729
    }
710
730
  }
714
734
  }
715
735
  
716
736
  if(quit_now){
 
737
    errno = EINTR;
717
738
    goto mandos_end;
718
739
  }
719
740
  
720
741
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
721
742
  
722
743
  if(quit_now){
 
744
    errno = EINTR;
723
745
    goto mandos_end;
724
746
  }
725
747
  
726
748
  do {
727
749
    ret = gnutls_handshake(session);
728
750
    if(quit_now){
 
751
      errno = EINTR;
729
752
      goto mandos_end;
730
753
    }
731
754
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
735
758
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
736
759
      gnutls_perror(ret);
737
760
    }
 
761
    errno = EPROTO;
738
762
    goto mandos_end;
739
763
  }
740
764
  
748
772
  while(true){
749
773
    
750
774
    if(quit_now){
 
775
      errno = EINTR;
751
776
      goto mandos_end;
752
777
    }
753
778
    
754
779
    buffer_capacity = incbuffer(&buffer, buffer_length,
755
780
                                   buffer_capacity);
756
781
    if(buffer_capacity == 0){
 
782
      int e = errno;
757
783
      perror("incbuffer");
 
784
      errno = e;
758
785
      goto mandos_end;
759
786
    }
760
787
    
761
788
    if(quit_now){
 
789
      errno = EINTR;
762
790
      goto mandos_end;
763
791
    }
764
792
    
777
805
          ret = gnutls_handshake(session);
778
806
          
779
807
          if(quit_now){
 
808
            errno = EINTR;
780
809
            goto mandos_end;
781
810
          }
782
811
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
783
812
        if(ret < 0){
784
813
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
785
814
          gnutls_perror(ret);
 
815
          errno = EPROTO;
786
816
          goto mandos_end;
787
817
        }
788
818
        break;
790
820
        fprintf(stderr, "Unknown error while reading data from"
791
821
                " encrypted session with Mandos server\n");
792
822
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
823
        errno = EIO;
793
824
        goto mandos_end;
794
825
      }
795
826
    } else {
802
833
  }
803
834
  
804
835
  if(quit_now){
 
836
    errno = EINTR;
805
837
    goto mandos_end;
806
838
  }
807
839
  
808
840
  do {
809
841
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
810
842
    if(quit_now){
 
843
      errno = EINTR;
811
844
      goto mandos_end;
812
845
    }
813
846
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
822
855
      written = 0;
823
856
      while(written < (size_t) decrypted_buffer_size){
824
857
        if(quit_now){
 
858
          errno = EINTR;
825
859
          goto mandos_end;
826
860
        }
827
861
        
829
863
                          (size_t)decrypted_buffer_size - written,
830
864
                          stdout);
831
865
        if(ret == 0 and ferror(stdout)){
 
866
          int e = errno;
832
867
          if(debug){
833
868
            fprintf(stderr, "Error writing encrypted data: %s\n",
834
869
                    strerror(errno));
835
870
          }
 
871
          errno = e;
836
872
          goto mandos_end;
837
873
        }
838
874
        written += (size_t)ret;
844
880
  /* Shutdown procedure */
845
881
  
846
882
 mandos_end:
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;
 
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;
858
902
  }
859
903
  return retval;
860
904
}
976
1020
  errno = old_errno;
977
1021
}
978
1022
 
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
 
 
1074
1023
int main(int argc, char *argv[]){
1075
1024
  AvahiSServiceBrowser *sb = NULL;
1076
1025
  int error;
1078
1027
  intmax_t tmpmax;
1079
1028
  char *tmp;
1080
1029
  int exitcode = EXIT_SUCCESS;
1081
 
  const char *interface = "";
 
1030
  const char *interface = "eth0";
1082
1031
  struct ifreq network;
1083
1032
  int sd = -1;
1084
1033
  bool take_down_interface = false;
1085
1034
  uid_t uid;
1086
1035
  gid_t gid;
 
1036
  char *connect_to = NULL;
1087
1037
  char tempdir[] = "/tmp/mandosXXXXXX";
1088
1038
  bool tempdir_created = false;
1089
1039
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1152
1102
        .arg = "SECONDS",
1153
1103
        .doc = "Maximum delay to wait for interface startup",
1154
1104
        .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 },
1155
1114
      { .name = NULL }
1156
1115
    };
1157
1116
    
1158
1117
    error_t parse_opt(int key, char *arg,
1159
1118
                      struct argp_state *state){
 
1119
      errno = 0;
1160
1120
      switch(key){
1161
1121
      case 128:                 /* --debug */
1162
1122
        debug = true;
1178
1138
        tmpmax = strtoimax(arg, &tmp, 10);
1179
1139
        if(errno != 0 or tmp == arg or *tmp != '\0'
1180
1140
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1181
 
          fprintf(stderr, "Bad number of DH bits\n");
1182
 
          exit(EXIT_FAILURE);
 
1141
          argp_error(state, "Bad number of DH bits");
1183
1142
        }
1184
1143
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1185
1144
        break;
1190
1149
        errno = 0;
1191
1150
        delay = strtof(arg, &tmp);
1192
1151
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1193
 
          fprintf(stderr, "Bad delay\n");
1194
 
          exit(EXIT_FAILURE);
 
1152
          argp_error(state, "Bad delay");
1195
1153
        }
1196
1154
        break;
1197
 
      case ARGP_KEY_ARG:
1198
 
        argp_usage(state);
1199
 
      case ARGP_KEY_END:
 
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);
1200
1168
        break;
1201
1169
      default:
1202
1170
        return ARGP_ERR_UNKNOWN;
1203
1171
      }
1204
 
      return 0;
 
1172
      return errno;
1205
1173
    }
1206
1174
    
1207
1175
    struct argp argp = { .options = options, .parser = parse_opt,
1208
1176
                         .args_doc = "",
1209
1177
                         .doc = "Mandos client -- Get and decrypt"
1210
1178
                         " passwords from a Mandos server" };
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;
 
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;
1215
1192
      goto end;
1216
1193
    }
1217
1194
  }
1219
1196
  if(not debug){
1220
1197
    avahi_set_log_function(empty_log);
1221
1198
  }
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
 
  }
1247
1199
  
1248
1200
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1249
1201
     from the signal handler */
1252
1204
  mc.simple_poll = avahi_simple_poll_new();
1253
1205
  if(mc.simple_poll == NULL){
1254
1206
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1255
 
    exitcode = EXIT_FAILURE;
 
1207
    exitcode = EX_UNAVAILABLE;
1256
1208
    goto end;
1257
1209
  }
1258
1210
  
1260
1212
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1261
1213
  if(ret == -1){
1262
1214
    perror("sigaddset");
1263
 
    exitcode = EXIT_FAILURE;
 
1215
    exitcode = EX_OSERR;
1264
1216
    goto end;
1265
1217
  }
1266
1218
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1267
1219
  if(ret == -1){
1268
1220
    perror("sigaddset");
1269
 
    exitcode = EXIT_FAILURE;
 
1221
    exitcode = EX_OSERR;
1270
1222
    goto end;
1271
1223
  }
1272
1224
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1273
1225
  if(ret == -1){
1274
1226
    perror("sigaddset");
1275
 
    exitcode = EXIT_FAILURE;
 
1227
    exitcode = EX_OSERR;
1276
1228
    goto end;
1277
1229
  }
1278
1230
  /* Need to check if the handler is SIG_IGN before handling:
1282
1234
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1283
1235
  if(ret == -1){
1284
1236
    perror("sigaction");
1285
 
    return EXIT_FAILURE;
 
1237
    return EX_OSERR;
1286
1238
  }
1287
1239
  if(old_sigterm_action.sa_handler != SIG_IGN){
1288
1240
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1289
1241
    if(ret == -1){
1290
1242
      perror("sigaction");
1291
 
      exitcode = EXIT_FAILURE;
 
1243
      exitcode = EX_OSERR;
1292
1244
      goto end;
1293
1245
    }
1294
1246
  }
1295
1247
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1296
1248
  if(ret == -1){
1297
1249
    perror("sigaction");
1298
 
    return EXIT_FAILURE;
 
1250
    return EX_OSERR;
1299
1251
  }
1300
1252
  if(old_sigterm_action.sa_handler != SIG_IGN){
1301
1253
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1302
1254
    if(ret == -1){
1303
1255
      perror("sigaction");
1304
 
      exitcode = EXIT_FAILURE;
 
1256
      exitcode = EX_OSERR;
1305
1257
      goto end;
1306
1258
    }
1307
1259
  }
1308
1260
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1309
1261
  if(ret == -1){
1310
1262
    perror("sigaction");
1311
 
    return EXIT_FAILURE;
 
1263
    return EX_OSERR;
1312
1264
  }
1313
1265
  if(old_sigterm_action.sa_handler != SIG_IGN){
1314
1266
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1315
1267
    if(ret == -1){
1316
1268
      perror("sigaction");
1317
 
      exitcode = EXIT_FAILURE;
 
1269
      exitcode = EX_OSERR;
1318
1270
      goto end;
1319
1271
    }
1320
1272
  }
1321
1273
  
1322
1274
  /* If the interface is down, bring it up */
1323
 
  if(strcmp(interface, "none") != 0){
 
1275
  if(interface[0] != '\0'){
1324
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1325
1277
    if(if_index == 0){
1326
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1327
 
      exitcode = EXIT_FAILURE;
 
1279
      exitcode = EX_UNAVAILABLE;
1328
1280
      goto end;
1329
1281
    }
1330
1282
    
1341
1293
    
1342
1294
#ifdef __linux__
1343
1295
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1344
 
       messages to mess up the prompt */
 
1296
       messages about the network interface to mess up the prompt */
1345
1297
    ret = klogctl(8, NULL, 5);
1346
1298
    bool restore_loglevel = true;
1347
1299
    if(ret == -1){
1353
1305
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1354
1306
    if(sd < 0){
1355
1307
      perror("socket");
1356
 
      exitcode = EXIT_FAILURE;
 
1308
      exitcode = EX_OSERR;
1357
1309
#ifdef __linux__
1358
1310
      if(restore_loglevel){
1359
1311
        ret = klogctl(7, NULL, 0);
1382
1334
        }
1383
1335
      }
1384
1336
#endif  /* __linux__ */
1385
 
      exitcode = EXIT_FAILURE;
 
1337
      exitcode = EX_OSERR;
1386
1338
      /* Lower privileges */
1387
1339
      errno = 0;
1388
1340
      ret = seteuid(uid);
1397
1349
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1398
1350
      if(ret == -1){
1399
1351
        take_down_interface = false;
1400
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1401
 
        exitcode = EXIT_FAILURE;
 
1352
        perror("ioctl SIOCSIFFLAGS");
 
1353
        exitcode = EX_OSERR;
1402
1354
#ifdef __linux__
1403
1355
        if(restore_loglevel){
1404
1356
          ret = klogctl(7, NULL, 0);
1470
1422
  ret = init_gnutls_global(pubkey, seckey);
1471
1423
  if(ret == -1){
1472
1424
    fprintf(stderr, "init_gnutls_global failed\n");
1473
 
    exitcode = EXIT_FAILURE;
 
1425
    exitcode = EX_UNAVAILABLE;
1474
1426
    goto end;
1475
1427
  } else {
1476
1428
    gnutls_initialized = true;
1493
1445
  
1494
1446
  if(not init_gpgme(pubkey, seckey, tempdir)){
1495
1447
    fprintf(stderr, "init_gpgme failed\n");
1496
 
    exitcode = EXIT_FAILURE;
 
1448
    exitcode = EX_UNAVAILABLE;
1497
1449
    goto end;
1498
1450
  } else {
1499
1451
    gpgme_initialized = true;
1509
1461
    char *address = strrchr(connect_to, ':');
1510
1462
    if(address == NULL){
1511
1463
      fprintf(stderr, "No colon in address\n");
1512
 
      exitcode = EXIT_FAILURE;
 
1464
      exitcode = EX_USAGE;
1513
1465
      goto end;
1514
1466
    }
1515
1467
    
1523
1475
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1524
1476
       or tmpmax != (uint16_t)tmpmax){
1525
1477
      fprintf(stderr, "Bad port number\n");
1526
 
      exitcode = EXIT_FAILURE;
 
1478
      exitcode = EX_USAGE;
1527
1479
      goto end;
1528
1480
    }
1529
1481
  
1548
1500
    
1549
1501
    ret = start_mandos_communication(address, port, if_index, af);
1550
1502
    if(ret < 0){
1551
 
      exitcode = EXIT_FAILURE;
 
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
      }
1552
1522
    } else {
1553
1523
      exitcode = EXIT_SUCCESS;
1554
1524
    }
1581
1551
  if(mc.server == NULL){
1582
1552
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1583
1553
            avahi_strerror(error));
1584
 
    exitcode = EXIT_FAILURE;
 
1554
    exitcode = EX_UNAVAILABLE;
1585
1555
    goto end;
1586
1556
  }
1587
1557
  
1596
1566
  if(sb == NULL){
1597
1567
    fprintf(stderr, "Failed to create service browser: %s\n",
1598
1568
            avahi_strerror(avahi_server_errno(mc.server)));
1599
 
    exitcode = EXIT_FAILURE;
 
1569
    exitcode = EX_UNAVAILABLE;
1600
1570
    goto end;
1601
1571
  }
1602
1572
  
1651
1621
      if(ret == -1){
1652
1622
        perror("ioctl SIOCGIFFLAGS");
1653
1623
      } else if(network.ifr_flags & IFF_UP) {
1654
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1655
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1656
1626
        if(ret == -1){
1657
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1627
          perror("ioctl SIOCSIFFLAGS");
1658
1628
        }
1659
1629
      }
1660
1630
      ret = (int)TEMP_FAILURE_RETRY(close(sd));