/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno */
66
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
66
#include <time.h>               /* nanosleep(), time() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
69
69
                                   if_nametoindex(), IF_NAMESIZE */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
74
                                   getuid(), getgid(), seteuid(),
75
75
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
79
79
                                   argp_state, struct argp,
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 {
423
419
  }
424
420
  
425
421
  /* OpenPGP credentials */
426
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
422
  gnutls_certificate_allocate_credentials(&mc.cred);
427
423
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
424
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
425
                                                    from
 
426
                                                    -Wunreachable-code
 
427
                                                 */
429
428
            safer_gnutls_strerror(ret));
430
429
    gnutls_global_deinit();
431
430
    return -1;
553
552
  gnutls_session_t session;
554
553
  int pf;                       /* Protocol family */
555
554
  
556
 
  errno = 0;
557
 
  
558
555
  if(quit_now){
559
 
    errno = EINTR;
560
556
    return -1;
561
557
  }
562
558
  
569
565
    break;
570
566
  default:
571
567
    fprintf(stderr, "Bad address family: %d\n", af);
572
 
    errno = EINVAL;
573
568
    return -1;
574
569
  }
575
570
  
585
580
  
586
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
587
582
  if(tcp_sd < 0){
588
 
    int e = errno;
589
583
    perror("socket");
590
 
    errno = e;
591
584
    goto mandos_end;
592
585
  }
593
586
  
594
587
  if(quit_now){
595
 
    errno = EINTR;
596
588
    goto mandos_end;
597
589
  }
598
590
  
605
597
    ret = inet_pton(af, ip, &to.in.sin_addr);
606
598
  }
607
599
  if(ret < 0 ){
608
 
    int e = errno;
609
600
    perror("inet_pton");
610
 
    errno = e;
611
601
    goto mandos_end;
612
602
  }
613
603
  if(ret == 0){
614
 
    int e = errno;
615
604
    fprintf(stderr, "Bad address: %s\n", ip);
616
 
    errno = e;
617
605
    goto mandos_end;
618
606
  }
619
607
  if(af == AF_INET6){
627
615
      if(if_index == AVAHI_IF_UNSPEC){
628
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
629
617
                " without a network interface\n");
630
 
        errno = EINVAL;
631
618
        goto mandos_end;
632
619
      }
633
620
      /* Set the network interface number as scope */
640
627
  }
641
628
  
642
629
  if(quit_now){
643
 
    errno = EINTR;
644
630
    goto mandos_end;
645
631
  }
646
632
  
677
663
  }
678
664
  
679
665
  if(quit_now){
680
 
    errno = EINTR;
681
666
    goto mandos_end;
682
667
  }
683
668
  
687
672
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
688
673
  }
689
674
  if(ret < 0){
690
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
691
 
      int e = errno;
692
 
      perror("connect");
693
 
      errno = e;
694
 
    }
 
675
    perror("connect");
695
676
    goto mandos_end;
696
677
  }
697
678
  
698
679
  if(quit_now){
699
 
    errno = EINTR;
700
680
    goto mandos_end;
701
681
  }
702
682
  
707
687
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
708
688
                                   out_size - written));
709
689
    if(ret == -1){
710
 
      int e = errno;
711
690
      perror("write");
712
 
      errno = e;
713
691
      goto mandos_end;
714
692
    }
715
693
    written += (size_t)ret;
725
703
    }
726
704
  
727
705
    if(quit_now){
728
 
      errno = EINTR;
729
706
      goto mandos_end;
730
707
    }
731
708
  }
735
712
  }
736
713
  
737
714
  if(quit_now){
738
 
    errno = EINTR;
739
715
    goto mandos_end;
740
716
  }
741
717
  
742
718
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
743
719
  
744
720
  if(quit_now){
745
 
    errno = EINTR;
746
721
    goto mandos_end;
747
722
  }
748
723
  
749
724
  do {
750
725
    ret = gnutls_handshake(session);
751
726
    if(quit_now){
752
 
      errno = EINTR;
753
727
      goto mandos_end;
754
728
    }
755
729
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
759
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
760
734
      gnutls_perror(ret);
761
735
    }
762
 
    errno = EPROTO;
763
736
    goto mandos_end;
764
737
  }
765
738
  
773
746
  while(true){
774
747
    
775
748
    if(quit_now){
776
 
      errno = EINTR;
777
749
      goto mandos_end;
778
750
    }
779
751
    
780
752
    buffer_capacity = incbuffer(&buffer, buffer_length,
781
753
                                   buffer_capacity);
782
754
    if(buffer_capacity == 0){
783
 
      int e = errno;
784
755
      perror("incbuffer");
785
 
      errno = e;
786
756
      goto mandos_end;
787
757
    }
788
758
    
789
759
    if(quit_now){
790
 
      errno = EINTR;
791
760
      goto mandos_end;
792
761
    }
793
762
    
806
775
          ret = gnutls_handshake(session);
807
776
          
808
777
          if(quit_now){
809
 
            errno = EINTR;
810
778
            goto mandos_end;
811
779
          }
812
780
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
813
781
        if(ret < 0){
814
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
815
783
          gnutls_perror(ret);
816
 
          errno = EPROTO;
817
784
          goto mandos_end;
818
785
        }
819
786
        break;
821
788
        fprintf(stderr, "Unknown error while reading data from"
822
789
                " encrypted session with Mandos server\n");
823
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
824
 
        errno = EIO;
825
791
        goto mandos_end;
826
792
      }
827
793
    } else {
834
800
  }
835
801
  
836
802
  if(quit_now){
837
 
    errno = EINTR;
838
803
    goto mandos_end;
839
804
  }
840
805
  
841
806
  do {
842
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
843
808
    if(quit_now){
844
 
      errno = EINTR;
845
809
      goto mandos_end;
846
810
    }
847
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
856
820
      written = 0;
857
821
      while(written < (size_t) decrypted_buffer_size){
858
822
        if(quit_now){
859
 
          errno = EINTR;
860
823
          goto mandos_end;
861
824
        }
862
825
        
864
827
                          (size_t)decrypted_buffer_size - written,
865
828
                          stdout);
866
829
        if(ret == 0 and ferror(stdout)){
867
 
          int e = errno;
868
830
          if(debug){
869
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
870
832
                    strerror(errno));
871
833
          }
872
 
          errno = e;
873
834
          goto mandos_end;
874
835
        }
875
836
        written += (size_t)ret;
881
842
  /* Shutdown procedure */
882
843
  
883
844
 mandos_end:
884
 
  {
885
 
    int e = errno;
886
 
    free(decrypted_buffer);
887
 
    free(buffer);
888
 
    if(tcp_sd >= 0){
889
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
890
 
    }
891
 
    if(ret == -1){
892
 
      if(e == 0){
893
 
        e = errno;
894
 
      }
895
 
      perror("close");
896
 
    }
897
 
    gnutls_deinit(session);
898
 
    if(quit_now){
899
 
      e = EINTR;
900
 
      retval = -1;
901
 
    }
902
 
    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;
903
856
  }
904
857
  return retval;
905
858
}
1021
974
  errno = old_errno;
1022
975
}
1023
976
 
1024
 
/* 
1025
 
 * This function determines if a directory entry in /sys/class/net
1026
 
 * corresponds to an acceptable network device.
1027
 
 * (This function is passed to scandir(3) as a filter function.)
1028
 
 */
1029
 
int good_interface(const struct dirent *if_entry){
1030
 
  ssize_t ssret;
1031
 
  char *flagname = NULL;
1032
 
  if(if_entry->d_name[0] == '.'){
1033
 
    return 0;
1034
 
  }
1035
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
 
                     if_entry->d_name);
1037
 
  if(ret < 0){
1038
 
    perror("asprintf");
1039
 
    return 0;
1040
 
  }
1041
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1042
 
  if(flags_fd == -1){
1043
 
    perror("open");
1044
 
    free(flagname);
1045
 
    return 0;
1046
 
  }
1047
 
  free(flagname);
1048
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1049
 
  /* read line from flags_fd */
1050
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1051
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1052
 
  flagstring[(size_t)to_read] = '\0';
1053
 
  if(flagstring == NULL){
1054
 
    perror("malloc");
1055
 
    close(flags_fd);
1056
 
    return 0;
1057
 
  }
1058
 
  while(to_read > 0){
1059
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1060
 
                                             (size_t)to_read));
1061
 
    if(ssret == -1){
1062
 
      perror("read");
1063
 
      free(flagstring);
1064
 
      close(flags_fd);
1065
 
      return 0;
1066
 
    }
1067
 
    to_read -= ssret;
1068
 
    if(ssret == 0){
1069
 
      break;
1070
 
    }
1071
 
  }
1072
 
  close(flags_fd);
1073
 
  intmax_t tmpmax;
1074
 
  char *tmp;
1075
 
  errno = 0;
1076
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1077
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1078
 
                                         and not (isspace(*tmp)))
1079
 
     or tmpmax != (ifreq_flags)tmpmax){
1080
 
    if(debug){
1081
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1082
 
              flagstring, if_entry->d_name);
1083
 
    }
1084
 
    free(flagstring);
1085
 
    return 0;
1086
 
  }
1087
 
  free(flagstring);
1088
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1089
 
  /* Reject the loopback device */
1090
 
  if(flags & IFF_LOOPBACK){
1091
 
    if(debug){
1092
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1093
 
              if_entry->d_name);
1094
 
    }
1095
 
    return 0;
1096
 
  }
1097
 
  /* Accept point-to-point devices only if connect_to is specified */
1098
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1099
 
    if(debug){
1100
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1101
 
              if_entry->d_name);
1102
 
    }
1103
 
    return 1;
1104
 
  }
1105
 
  /* Otherwise, reject non-broadcast-capable devices */
1106
 
  if(not (flags & IFF_BROADCAST)){
1107
 
    if(debug){
1108
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1109
 
              if_entry->d_name);
1110
 
    }
1111
 
    return 0;
1112
 
  }
1113
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1114
 
  if(flags & IFF_NOARP){
1115
 
    if(debug){
1116
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1117
 
              if_entry->d_name);
1118
 
    }
1119
 
    return 0;
1120
 
  }
1121
 
  /* Accept this device */
1122
 
  if(debug){
1123
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1124
 
            if_entry->d_name);
1125
 
  }
1126
 
  return 1;
1127
 
}
1128
 
 
1129
977
int main(int argc, char *argv[]){
1130
978
  AvahiSServiceBrowser *sb = NULL;
1131
979
  int error;
1133
981
  intmax_t tmpmax;
1134
982
  char *tmp;
1135
983
  int exitcode = EXIT_SUCCESS;
1136
 
  const char *interface = "";
 
984
  const char *interface = "eth0";
1137
985
  struct ifreq network;
1138
986
  int sd = -1;
1139
987
  bool take_down_interface = false;
1140
988
  uid_t uid;
1141
989
  gid_t gid;
 
990
  char *connect_to = NULL;
1142
991
  char tempdir[] = "/tmp/mandosXXXXXX";
1143
992
  bool tempdir_created = false;
1144
993
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1207
1056
        .arg = "SECONDS",
1208
1057
        .doc = "Maximum delay to wait for interface startup",
1209
1058
        .group = 2 },
1210
 
      /*
1211
 
       * These reproduce what we would get without ARGP_NO_HELP
1212
 
       */
1213
 
      { .name = "help", .key = '?',
1214
 
        .doc = "Give this help list", .group = -1 },
1215
 
      { .name = "usage", .key = -3,
1216
 
        .doc = "Give a short usage message", .group = -1 },
1217
 
      { .name = "version", .key = 'V',
1218
 
        .doc = "Print program version", .group = -1 },
1219
1059
      { .name = NULL }
1220
1060
    };
1221
1061
    
1222
1062
    error_t parse_opt(int key, char *arg,
1223
1063
                      struct argp_state *state){
1224
 
      errno = 0;
1225
1064
      switch(key){
1226
1065
      case 128:                 /* --debug */
1227
1066
        debug = true;
1243
1082
        tmpmax = strtoimax(arg, &tmp, 10);
1244
1083
        if(errno != 0 or tmp == arg or *tmp != '\0'
1245
1084
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1246
 
          argp_error(state, "Bad number of DH bits");
 
1085
          fprintf(stderr, "Bad number of DH bits\n");
 
1086
          exit(EXIT_FAILURE);
1247
1087
        }
1248
1088
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1249
1089
        break;
1254
1094
        errno = 0;
1255
1095
        delay = strtof(arg, &tmp);
1256
1096
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1257
 
          argp_error(state, "Bad delay");
 
1097
          fprintf(stderr, "Bad delay\n");
 
1098
          exit(EXIT_FAILURE);
1258
1099
        }
1259
1100
        break;
1260
 
        /*
1261
 
         * These reproduce what we would get without ARGP_NO_HELP
1262
 
         */
1263
 
      case '?':                 /* --help */
1264
 
        argp_state_help(state, state->out_stream,
1265
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1266
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1267
 
      case -3:                  /* --usage */
1268
 
        argp_state_help(state, state->out_stream,
1269
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1270
 
      case 'V':                 /* --version */
1271
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1272
 
        exit(argp_err_exit_status);
 
1101
      case ARGP_KEY_ARG:
 
1102
        argp_usage(state);
 
1103
      case ARGP_KEY_END:
1273
1104
        break;
1274
1105
      default:
1275
1106
        return ARGP_ERR_UNKNOWN;
1276
1107
      }
1277
 
      return errno;
 
1108
      return 0;
1278
1109
    }
1279
1110
    
1280
1111
    struct argp argp = { .options = options, .parser = parse_opt,
1281
1112
                         .args_doc = "",
1282
1113
                         .doc = "Mandos client -- Get and decrypt"
1283
1114
                         " passwords from a Mandos server" };
1284
 
    ret = argp_parse(&argp, argc, argv,
1285
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1286
 
    switch(ret){
1287
 
    case 0:
1288
 
      break;
1289
 
    case ENOMEM:
1290
 
    default:
1291
 
      errno = ret;
1292
 
      perror("argp_parse");
1293
 
      exitcode = EX_OSERR;
1294
 
      goto end;
1295
 
    case EINVAL:
1296
 
      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;
1297
1119
      goto end;
1298
1120
    }
1299
1121
  }
1301
1123
  if(not debug){
1302
1124
    avahi_set_log_function(empty_log);
1303
1125
  }
1304
 
 
1305
 
  if(interface[0] == '\0'){
1306
 
    struct dirent **direntries;
1307
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1308
 
                  alphasort);
1309
 
    if(ret >= 1){
1310
 
      /* Pick the first good interface */
1311
 
      interface = strdup(direntries[0]->d_name);
1312
 
      if(debug){
1313
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1314
 
      }
1315
 
      if(interface == NULL){
1316
 
        perror("malloc");
1317
 
        free(direntries);
1318
 
        exitcode = EXIT_FAILURE;
1319
 
        goto end;
1320
 
      }
1321
 
      free(direntries);
1322
 
    } else {
1323
 
      free(direntries);
1324
 
      fprintf(stderr, "Could not find a network interface\n");
1325
 
      exitcode = EXIT_FAILURE;
1326
 
      goto end;
1327
 
    }
1328
 
  }
1329
1126
  
1330
1127
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1331
1128
     from the signal handler */
1334
1131
  mc.simple_poll = avahi_simple_poll_new();
1335
1132
  if(mc.simple_poll == NULL){
1336
1133
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1337
 
    exitcode = EX_UNAVAILABLE;
 
1134
    exitcode = EXIT_FAILURE;
1338
1135
    goto end;
1339
1136
  }
1340
1137
  
1342
1139
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1343
1140
  if(ret == -1){
1344
1141
    perror("sigaddset");
1345
 
    exitcode = EX_OSERR;
 
1142
    exitcode = EXIT_FAILURE;
1346
1143
    goto end;
1347
1144
  }
1348
1145
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1349
1146
  if(ret == -1){
1350
1147
    perror("sigaddset");
1351
 
    exitcode = EX_OSERR;
 
1148
    exitcode = EXIT_FAILURE;
1352
1149
    goto end;
1353
1150
  }
1354
1151
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1355
1152
  if(ret == -1){
1356
1153
    perror("sigaddset");
1357
 
    exitcode = EX_OSERR;
 
1154
    exitcode = EXIT_FAILURE;
1358
1155
    goto end;
1359
1156
  }
1360
1157
  /* Need to check if the handler is SIG_IGN before handling:
1364
1161
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1365
1162
  if(ret == -1){
1366
1163
    perror("sigaction");
1367
 
    return EX_OSERR;
 
1164
    return EXIT_FAILURE;
1368
1165
  }
1369
1166
  if(old_sigterm_action.sa_handler != SIG_IGN){
1370
1167
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1371
1168
    if(ret == -1){
1372
1169
      perror("sigaction");
1373
 
      exitcode = EX_OSERR;
 
1170
      exitcode = EXIT_FAILURE;
1374
1171
      goto end;
1375
1172
    }
1376
1173
  }
1377
1174
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1378
1175
  if(ret == -1){
1379
1176
    perror("sigaction");
1380
 
    return EX_OSERR;
 
1177
    return EXIT_FAILURE;
1381
1178
  }
1382
1179
  if(old_sigterm_action.sa_handler != SIG_IGN){
1383
1180
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1384
1181
    if(ret == -1){
1385
1182
      perror("sigaction");
1386
 
      exitcode = EX_OSERR;
 
1183
      exitcode = EXIT_FAILURE;
1387
1184
      goto end;
1388
1185
    }
1389
1186
  }
1390
1187
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1391
1188
  if(ret == -1){
1392
1189
    perror("sigaction");
1393
 
    return EX_OSERR;
 
1190
    return EXIT_FAILURE;
1394
1191
  }
1395
1192
  if(old_sigterm_action.sa_handler != SIG_IGN){
1396
1193
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1397
1194
    if(ret == -1){
1398
1195
      perror("sigaction");
1399
 
      exitcode = EX_OSERR;
 
1196
      exitcode = EXIT_FAILURE;
1400
1197
      goto end;
1401
1198
    }
1402
1199
  }
1403
1200
  
1404
1201
  /* If the interface is down, bring it up */
1405
 
  if(strcmp(interface, "none") != 0){
 
1202
  if(interface[0] != '\0'){
1406
1203
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1407
1204
    if(if_index == 0){
1408
1205
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1409
 
      exitcode = EX_UNAVAILABLE;
 
1206
      exitcode = EXIT_FAILURE;
1410
1207
      goto end;
1411
1208
    }
1412
1209
    
1423
1220
    
1424
1221
#ifdef __linux__
1425
1222
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1426
 
       messages about the network interface to mess up the prompt */
 
1223
       messages to mess up the prompt */
1427
1224
    ret = klogctl(8, NULL, 5);
1428
1225
    bool restore_loglevel = true;
1429
1226
    if(ret == -1){
1435
1232
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1436
1233
    if(sd < 0){
1437
1234
      perror("socket");
1438
 
      exitcode = EX_OSERR;
 
1235
      exitcode = EXIT_FAILURE;
1439
1236
#ifdef __linux__
1440
1237
      if(restore_loglevel){
1441
1238
        ret = klogctl(7, NULL, 0);
1464
1261
        }
1465
1262
      }
1466
1263
#endif  /* __linux__ */
1467
 
      exitcode = EX_OSERR;
 
1264
      exitcode = EXIT_FAILURE;
1468
1265
      /* Lower privileges */
1469
1266
      errno = 0;
1470
1267
      ret = seteuid(uid);
1479
1276
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1480
1277
      if(ret == -1){
1481
1278
        take_down_interface = false;
1482
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1483
 
        exitcode = EX_OSERR;
 
1279
        perror("ioctl SIOCSIFFLAGS");
 
1280
        exitcode = EXIT_FAILURE;
1484
1281
#ifdef __linux__
1485
1282
        if(restore_loglevel){
1486
1283
          ret = klogctl(7, NULL, 0);
1552
1349
  ret = init_gnutls_global(pubkey, seckey);
1553
1350
  if(ret == -1){
1554
1351
    fprintf(stderr, "init_gnutls_global failed\n");
1555
 
    exitcode = EX_UNAVAILABLE;
 
1352
    exitcode = EXIT_FAILURE;
1556
1353
    goto end;
1557
1354
  } else {
1558
1355
    gnutls_initialized = true;
1575
1372
  
1576
1373
  if(not init_gpgme(pubkey, seckey, tempdir)){
1577
1374
    fprintf(stderr, "init_gpgme failed\n");
1578
 
    exitcode = EX_UNAVAILABLE;
 
1375
    exitcode = EXIT_FAILURE;
1579
1376
    goto end;
1580
1377
  } else {
1581
1378
    gpgme_initialized = true;
1591
1388
    char *address = strrchr(connect_to, ':');
1592
1389
    if(address == NULL){
1593
1390
      fprintf(stderr, "No colon in address\n");
1594
 
      exitcode = EX_USAGE;
 
1391
      exitcode = EXIT_FAILURE;
1595
1392
      goto end;
1596
1393
    }
1597
1394
    
1605
1402
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1606
1403
       or tmpmax != (uint16_t)tmpmax){
1607
1404
      fprintf(stderr, "Bad port number\n");
1608
 
      exitcode = EX_USAGE;
 
1405
      exitcode = EXIT_FAILURE;
1609
1406
      goto end;
1610
1407
    }
1611
1408
  
1627
1424
    if(quit_now){
1628
1425
      goto end;
1629
1426
    }
1630
 
 
1631
 
    while(not quit_now){
1632
 
      ret = start_mandos_communication(address, port, if_index, af);
1633
 
      if(quit_now or ret == 0){
1634
 
        break;
1635
 
      }
1636
 
      sleep(15);
1637
 
    };
1638
 
 
1639
 
    if (not quit_now){
 
1427
    
 
1428
    ret = start_mandos_communication(address, port, if_index, af);
 
1429
    if(ret < 0){
 
1430
      exitcode = EXIT_FAILURE;
 
1431
    } else {
1640
1432
      exitcode = EXIT_SUCCESS;
1641
1433
    }
1642
 
 
1643
1434
    goto end;
1644
1435
  }
1645
1436
  
1669
1460
  if(mc.server == NULL){
1670
1461
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1671
1462
            avahi_strerror(error));
1672
 
    exitcode = EX_UNAVAILABLE;
 
1463
    exitcode = EXIT_FAILURE;
1673
1464
    goto end;
1674
1465
  }
1675
1466
  
1684
1475
  if(sb == NULL){
1685
1476
    fprintf(stderr, "Failed to create service browser: %s\n",
1686
1477
            avahi_strerror(avahi_server_errno(mc.server)));
1687
 
    exitcode = EX_UNAVAILABLE;
 
1478
    exitcode = EXIT_FAILURE;
1688
1479
    goto end;
1689
1480
  }
1690
1481
  
1739
1530
      if(ret == -1){
1740
1531
        perror("ioctl SIOCGIFFLAGS");
1741
1532
      } else if(network.ifr_flags & IFF_UP) {
1742
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1533
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1743
1534
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1744
1535
        if(ret == -1){
1745
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1536
          perror("ioctl SIOCSIFFLAGS");
1746
1537
        }
1747
1538
      }
1748
1539
      ret = (int)TEMP_FAILURE_RETRY(close(sd));