/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 Hogeborn
  • Date: 2011-07-13 01:11:12 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110713011112-bgf3tenqq60uoyyb
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
                                      libraries.  Remove "$(COMMON)".
                                      Thanks to Angel Abad
                                      <angelabad@ubuntu.com>.
* initramfs-tools-script: Work around change in initramfs-tools,
                          Debian bug #633582.
* plugins.d/mandos-client.c (init_gnutls_global): Bug fix: check for
                errors from gnutls_certificate_allocate_credentials().

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,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 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, 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() */
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno */
66
 
#include <time.h>               /* nanosleep(), time() */
 
66
#include <time.h>               /* nanosleep(), time(), sleep() */
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 */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
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 */
 
85
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
 
86
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
86
87
 
87
88
#ifdef __linux__
88
89
#include <sys/klog.h>           /* klogctl() */
126
127
static const char mandos_protocol_version[] = "1";
127
128
const char *argp_program_version = "mandos-client " VERSION;
128
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;
129
132
 
130
133
/* Used for passing in values through the Avahi callback functions */
131
134
typedef struct {
420
423
  }
421
424
  
422
425
  /* OpenPGP credentials */
423
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
424
427
  if(ret != GNUTLS_E_SUCCESS){
425
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
426
 
                                                    from
427
 
                                                    -Wunreachable-code
428
 
                                                 */
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n",
429
429
            safer_gnutls_strerror(ret));
430
430
    gnutls_global_deinit();
431
431
    return -1;
553
553
  gnutls_session_t session;
554
554
  int pf;                       /* Protocol family */
555
555
  
 
556
  errno = 0;
 
557
  
556
558
  if(quit_now){
 
559
    errno = EINTR;
557
560
    return -1;
558
561
  }
559
562
  
566
569
    break;
567
570
  default:
568
571
    fprintf(stderr, "Bad address family: %d\n", af);
 
572
    errno = EINVAL;
569
573
    return -1;
570
574
  }
571
575
  
581
585
  
582
586
  tcp_sd = socket(pf, SOCK_STREAM, 0);
583
587
  if(tcp_sd < 0){
 
588
    int e = errno;
584
589
    perror("socket");
 
590
    errno = e;
585
591
    goto mandos_end;
586
592
  }
587
593
  
588
594
  if(quit_now){
 
595
    errno = EINTR;
589
596
    goto mandos_end;
590
597
  }
591
598
  
598
605
    ret = inet_pton(af, ip, &to.in.sin_addr);
599
606
  }
600
607
  if(ret < 0 ){
 
608
    int e = errno;
601
609
    perror("inet_pton");
 
610
    errno = e;
602
611
    goto mandos_end;
603
612
  }
604
613
  if(ret == 0){
 
614
    int e = errno;
605
615
    fprintf(stderr, "Bad address: %s\n", ip);
 
616
    errno = e;
606
617
    goto mandos_end;
607
618
  }
608
619
  if(af == AF_INET6){
616
627
      if(if_index == AVAHI_IF_UNSPEC){
617
628
        fprintf(stderr, "An IPv6 link-local address is incomplete"
618
629
                " without a network interface\n");
 
630
        errno = EINVAL;
619
631
        goto mandos_end;
620
632
      }
621
633
      /* Set the network interface number as scope */
628
640
  }
629
641
  
630
642
  if(quit_now){
 
643
    errno = EINTR;
631
644
    goto mandos_end;
632
645
  }
633
646
  
664
677
  }
665
678
  
666
679
  if(quit_now){
 
680
    errno = EINTR;
667
681
    goto mandos_end;
668
682
  }
669
683
  
673
687
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
674
688
  }
675
689
  if(ret < 0){
676
 
    perror("connect");
 
690
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
691
      int e = errno;
 
692
      perror("connect");
 
693
      errno = e;
 
694
    }
677
695
    goto mandos_end;
678
696
  }
679
697
  
680
698
  if(quit_now){
 
699
    errno = EINTR;
681
700
    goto mandos_end;
682
701
  }
683
702
  
688
707
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
689
708
                                   out_size - written));
690
709
    if(ret == -1){
 
710
      int e = errno;
691
711
      perror("write");
 
712
      errno = e;
692
713
      goto mandos_end;
693
714
    }
694
715
    written += (size_t)ret;
704
725
    }
705
726
  
706
727
    if(quit_now){
 
728
      errno = EINTR;
707
729
      goto mandos_end;
708
730
    }
709
731
  }
713
735
  }
714
736
  
715
737
  if(quit_now){
 
738
    errno = EINTR;
716
739
    goto mandos_end;
717
740
  }
718
741
  
719
742
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
720
743
  
721
744
  if(quit_now){
 
745
    errno = EINTR;
722
746
    goto mandos_end;
723
747
  }
724
748
  
725
749
  do {
726
750
    ret = gnutls_handshake(session);
727
751
    if(quit_now){
 
752
      errno = EINTR;
728
753
      goto mandos_end;
729
754
    }
730
755
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
734
759
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
735
760
      gnutls_perror(ret);
736
761
    }
 
762
    errno = EPROTO;
737
763
    goto mandos_end;
738
764
  }
739
765
  
747
773
  while(true){
748
774
    
749
775
    if(quit_now){
 
776
      errno = EINTR;
750
777
      goto mandos_end;
751
778
    }
752
779
    
753
780
    buffer_capacity = incbuffer(&buffer, buffer_length,
754
781
                                   buffer_capacity);
755
782
    if(buffer_capacity == 0){
 
783
      int e = errno;
756
784
      perror("incbuffer");
 
785
      errno = e;
757
786
      goto mandos_end;
758
787
    }
759
788
    
760
789
    if(quit_now){
 
790
      errno = EINTR;
761
791
      goto mandos_end;
762
792
    }
763
793
    
776
806
          ret = gnutls_handshake(session);
777
807
          
778
808
          if(quit_now){
 
809
            errno = EINTR;
779
810
            goto mandos_end;
780
811
          }
781
812
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
782
813
        if(ret < 0){
783
814
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
784
815
          gnutls_perror(ret);
 
816
          errno = EPROTO;
785
817
          goto mandos_end;
786
818
        }
787
819
        break;
789
821
        fprintf(stderr, "Unknown error while reading data from"
790
822
                " encrypted session with Mandos server\n");
791
823
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
824
        errno = EIO;
792
825
        goto mandos_end;
793
826
      }
794
827
    } else {
801
834
  }
802
835
  
803
836
  if(quit_now){
 
837
    errno = EINTR;
804
838
    goto mandos_end;
805
839
  }
806
840
  
807
841
  do {
808
842
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
809
843
    if(quit_now){
 
844
      errno = EINTR;
810
845
      goto mandos_end;
811
846
    }
812
847
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
821
856
      written = 0;
822
857
      while(written < (size_t) decrypted_buffer_size){
823
858
        if(quit_now){
 
859
          errno = EINTR;
824
860
          goto mandos_end;
825
861
        }
826
862
        
828
864
                          (size_t)decrypted_buffer_size - written,
829
865
                          stdout);
830
866
        if(ret == 0 and ferror(stdout)){
 
867
          int e = errno;
831
868
          if(debug){
832
869
            fprintf(stderr, "Error writing encrypted data: %s\n",
833
870
                    strerror(errno));
834
871
          }
 
872
          errno = e;
835
873
          goto mandos_end;
836
874
        }
837
875
        written += (size_t)ret;
843
881
  /* Shutdown procedure */
844
882
  
845
883
 mandos_end:
846
 
  free(decrypted_buffer);
847
 
  free(buffer);
848
 
  if(tcp_sd >= 0){
849
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
850
 
  }
851
 
  if(ret == -1){
852
 
    perror("close");
853
 
  }
854
 
  gnutls_deinit(session);
855
 
  if(quit_now){
856
 
    retval = -1;
 
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;
857
903
  }
858
904
  return retval;
859
905
}
975
1021
  errno = old_errno;
976
1022
}
977
1023
 
 
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
 
978
1129
int main(int argc, char *argv[]){
979
1130
  AvahiSServiceBrowser *sb = NULL;
980
1131
  int error;
982
1133
  intmax_t tmpmax;
983
1134
  char *tmp;
984
1135
  int exitcode = EXIT_SUCCESS;
985
 
  const char *interface = "eth0";
 
1136
  const char *interface = "";
986
1137
  struct ifreq network;
987
1138
  int sd = -1;
988
1139
  bool take_down_interface = false;
989
1140
  uid_t uid;
990
1141
  gid_t gid;
991
 
  char *connect_to = NULL;
992
1142
  char tempdir[] = "/tmp/mandosXXXXXX";
993
1143
  bool tempdir_created = false;
994
1144
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1151
1301
  if(not debug){
1152
1302
    avahi_set_log_function(empty_log);
1153
1303
  }
 
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
  }
1154
1329
  
1155
1330
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1156
1331
     from the signal handler */
1159
1334
  mc.simple_poll = avahi_simple_poll_new();
1160
1335
  if(mc.simple_poll == NULL){
1161
1336
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1162
 
    exitcode = EXIT_FAILURE;
 
1337
    exitcode = EX_UNAVAILABLE;
1163
1338
    goto end;
1164
1339
  }
1165
1340
  
1167
1342
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1168
1343
  if(ret == -1){
1169
1344
    perror("sigaddset");
1170
 
    exitcode = EXIT_FAILURE;
 
1345
    exitcode = EX_OSERR;
1171
1346
    goto end;
1172
1347
  }
1173
1348
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1174
1349
  if(ret == -1){
1175
1350
    perror("sigaddset");
1176
 
    exitcode = EXIT_FAILURE;
 
1351
    exitcode = EX_OSERR;
1177
1352
    goto end;
1178
1353
  }
1179
1354
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1180
1355
  if(ret == -1){
1181
1356
    perror("sigaddset");
1182
 
    exitcode = EXIT_FAILURE;
 
1357
    exitcode = EX_OSERR;
1183
1358
    goto end;
1184
1359
  }
1185
1360
  /* Need to check if the handler is SIG_IGN before handling:
1189
1364
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1190
1365
  if(ret == -1){
1191
1366
    perror("sigaction");
1192
 
    return EXIT_FAILURE;
 
1367
    return EX_OSERR;
1193
1368
  }
1194
1369
  if(old_sigterm_action.sa_handler != SIG_IGN){
1195
1370
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1196
1371
    if(ret == -1){
1197
1372
      perror("sigaction");
1198
 
      exitcode = EXIT_FAILURE;
 
1373
      exitcode = EX_OSERR;
1199
1374
      goto end;
1200
1375
    }
1201
1376
  }
1202
1377
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1203
1378
  if(ret == -1){
1204
1379
    perror("sigaction");
1205
 
    return EXIT_FAILURE;
 
1380
    return EX_OSERR;
1206
1381
  }
1207
1382
  if(old_sigterm_action.sa_handler != SIG_IGN){
1208
1383
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1209
1384
    if(ret == -1){
1210
1385
      perror("sigaction");
1211
 
      exitcode = EXIT_FAILURE;
 
1386
      exitcode = EX_OSERR;
1212
1387
      goto end;
1213
1388
    }
1214
1389
  }
1215
1390
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1216
1391
  if(ret == -1){
1217
1392
    perror("sigaction");
1218
 
    return EXIT_FAILURE;
 
1393
    return EX_OSERR;
1219
1394
  }
1220
1395
  if(old_sigterm_action.sa_handler != SIG_IGN){
1221
1396
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1222
1397
    if(ret == -1){
1223
1398
      perror("sigaction");
1224
 
      exitcode = EXIT_FAILURE;
 
1399
      exitcode = EX_OSERR;
1225
1400
      goto end;
1226
1401
    }
1227
1402
  }
1228
1403
  
1229
1404
  /* If the interface is down, bring it up */
1230
 
  if(interface[0] != '\0'){
 
1405
  if(strcmp(interface, "none") != 0){
1231
1406
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1232
1407
    if(if_index == 0){
1233
1408
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1234
 
      exitcode = EXIT_FAILURE;
 
1409
      exitcode = EX_UNAVAILABLE;
1235
1410
      goto end;
1236
1411
    }
1237
1412
    
1260
1435
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1261
1436
    if(sd < 0){
1262
1437
      perror("socket");
1263
 
      exitcode = EXIT_FAILURE;
 
1438
      exitcode = EX_OSERR;
1264
1439
#ifdef __linux__
1265
1440
      if(restore_loglevel){
1266
1441
        ret = klogctl(7, NULL, 0);
1289
1464
        }
1290
1465
      }
1291
1466
#endif  /* __linux__ */
1292
 
      exitcode = EXIT_FAILURE;
 
1467
      exitcode = EX_OSERR;
1293
1468
      /* Lower privileges */
1294
1469
      errno = 0;
1295
1470
      ret = seteuid(uid);
1304
1479
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1305
1480
      if(ret == -1){
1306
1481
        take_down_interface = false;
1307
 
        perror("ioctl SIOCSIFFLAGS");
1308
 
        exitcode = EXIT_FAILURE;
 
1482
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1483
        exitcode = EX_OSERR;
1309
1484
#ifdef __linux__
1310
1485
        if(restore_loglevel){
1311
1486
          ret = klogctl(7, NULL, 0);
1377
1552
  ret = init_gnutls_global(pubkey, seckey);
1378
1553
  if(ret == -1){
1379
1554
    fprintf(stderr, "init_gnutls_global failed\n");
1380
 
    exitcode = EXIT_FAILURE;
 
1555
    exitcode = EX_UNAVAILABLE;
1381
1556
    goto end;
1382
1557
  } else {
1383
1558
    gnutls_initialized = true;
1400
1575
  
1401
1576
  if(not init_gpgme(pubkey, seckey, tempdir)){
1402
1577
    fprintf(stderr, "init_gpgme failed\n");
1403
 
    exitcode = EXIT_FAILURE;
 
1578
    exitcode = EX_UNAVAILABLE;
1404
1579
    goto end;
1405
1580
  } else {
1406
1581
    gpgme_initialized = true;
1416
1591
    char *address = strrchr(connect_to, ':');
1417
1592
    if(address == NULL){
1418
1593
      fprintf(stderr, "No colon in address\n");
1419
 
      exitcode = EXIT_FAILURE;
 
1594
      exitcode = EX_USAGE;
1420
1595
      goto end;
1421
1596
    }
1422
1597
    
1430
1605
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1431
1606
       or tmpmax != (uint16_t)tmpmax){
1432
1607
      fprintf(stderr, "Bad port number\n");
1433
 
      exitcode = EXIT_FAILURE;
 
1608
      exitcode = EX_USAGE;
1434
1609
      goto end;
1435
1610
    }
1436
1611
  
1452
1627
    if(quit_now){
1453
1628
      goto end;
1454
1629
    }
1455
 
    
1456
 
    ret = start_mandos_communication(address, port, if_index, af);
1457
 
    if(ret < 0){
1458
 
      exitcode = EXIT_FAILURE;
1459
 
    } else {
 
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){
1460
1640
      exitcode = EXIT_SUCCESS;
1461
1641
    }
 
1642
 
1462
1643
    goto end;
1463
1644
  }
1464
1645
  
1488
1669
  if(mc.server == NULL){
1489
1670
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1490
1671
            avahi_strerror(error));
1491
 
    exitcode = EXIT_FAILURE;
 
1672
    exitcode = EX_UNAVAILABLE;
1492
1673
    goto end;
1493
1674
  }
1494
1675
  
1503
1684
  if(sb == NULL){
1504
1685
    fprintf(stderr, "Failed to create service browser: %s\n",
1505
1686
            avahi_strerror(avahi_server_errno(mc.server)));
1506
 
    exitcode = EXIT_FAILURE;
 
1687
    exitcode = EX_UNAVAILABLE;
1507
1688
    goto end;
1508
1689
  }
1509
1690
  
1561
1742
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1562
1743
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1563
1744
        if(ret == -1){
1564
 
          perror("ioctl SIOCSIFFLAGS");
 
1745
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1565
1746
        }
1566
1747
      }
1567
1748
      ret = (int)TEMP_FAILURE_RETRY(close(sd));