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