/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 at bsnet
  • Date: 2010-08-10 19:08:24 UTC
  • mto: This revision was merged to the branch mainline in revision 419.
  • Revision ID: teddy@fukt.bsnet.se-20100810190824-5yquozxy4kh6py3f
* plugins.d/mandos-client.c: An empty interface name now means to
                             autodetect an interface; to specify no
                             particular interface, use "none".
  (sys_class_net): New global variable for the "/sys/class/net" path.
  (good_interface): New function to determine the suitability of an
                    interface.  Used by a scandir() call in main().
  (main): Changed default value for "interface" to the empty string.
          Moved "connect_to" to be a global variable.  Only take down
          and up interface if its name is not "none".
* plugins.d/mandos-client.xml (OPTIONS): Update documentation for the
                                         "--interface" option.

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() */
423
421
  }
424
422
  
425
423
  /* OpenPGP credentials */
426
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
424
  gnutls_certificate_allocate_credentials(&mc.cred);
427
425
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
426
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
427
                                                    from
 
428
                                                    -Wunreachable-code
 
429
                                                 */
429
430
            safer_gnutls_strerror(ret));
430
431
    gnutls_global_deinit();
431
432
    return -1;
553
554
  gnutls_session_t session;
554
555
  int pf;                       /* Protocol family */
555
556
  
556
 
  errno = 0;
557
 
  
558
557
  if(quit_now){
559
 
    errno = EINTR;
560
558
    return -1;
561
559
  }
562
560
  
569
567
    break;
570
568
  default:
571
569
    fprintf(stderr, "Bad address family: %d\n", af);
572
 
    errno = EINVAL;
573
570
    return -1;
574
571
  }
575
572
  
585
582
  
586
583
  tcp_sd = socket(pf, SOCK_STREAM, 0);
587
584
  if(tcp_sd < 0){
588
 
    int e = errno;
589
585
    perror("socket");
590
 
    errno = e;
591
586
    goto mandos_end;
592
587
  }
593
588
  
594
589
  if(quit_now){
595
 
    errno = EINTR;
596
590
    goto mandos_end;
597
591
  }
598
592
  
605
599
    ret = inet_pton(af, ip, &to.in.sin_addr);
606
600
  }
607
601
  if(ret < 0 ){
608
 
    int e = errno;
609
602
    perror("inet_pton");
610
 
    errno = e;
611
603
    goto mandos_end;
612
604
  }
613
605
  if(ret == 0){
614
 
    int e = errno;
615
606
    fprintf(stderr, "Bad address: %s\n", ip);
616
 
    errno = e;
617
607
    goto mandos_end;
618
608
  }
619
609
  if(af == AF_INET6){
627
617
      if(if_index == AVAHI_IF_UNSPEC){
628
618
        fprintf(stderr, "An IPv6 link-local address is incomplete"
629
619
                " without a network interface\n");
630
 
        errno = EINVAL;
631
620
        goto mandos_end;
632
621
      }
633
622
      /* Set the network interface number as scope */
640
629
  }
641
630
  
642
631
  if(quit_now){
643
 
    errno = EINTR;
644
632
    goto mandos_end;
645
633
  }
646
634
  
677
665
  }
678
666
  
679
667
  if(quit_now){
680
 
    errno = EINTR;
681
668
    goto mandos_end;
682
669
  }
683
670
  
687
674
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
688
675
  }
689
676
  if(ret < 0){
690
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
691
 
      int e = errno;
692
 
      perror("connect");
693
 
      errno = e;
694
 
    }
 
677
    perror("connect");
695
678
    goto mandos_end;
696
679
  }
697
680
  
698
681
  if(quit_now){
699
 
    errno = EINTR;
700
682
    goto mandos_end;
701
683
  }
702
684
  
707
689
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
708
690
                                   out_size - written));
709
691
    if(ret == -1){
710
 
      int e = errno;
711
692
      perror("write");
712
 
      errno = e;
713
693
      goto mandos_end;
714
694
    }
715
695
    written += (size_t)ret;
725
705
    }
726
706
  
727
707
    if(quit_now){
728
 
      errno = EINTR;
729
708
      goto mandos_end;
730
709
    }
731
710
  }
735
714
  }
736
715
  
737
716
  if(quit_now){
738
 
    errno = EINTR;
739
717
    goto mandos_end;
740
718
  }
741
719
  
742
720
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
743
721
  
744
722
  if(quit_now){
745
 
    errno = EINTR;
746
723
    goto mandos_end;
747
724
  }
748
725
  
749
726
  do {
750
727
    ret = gnutls_handshake(session);
751
728
    if(quit_now){
752
 
      errno = EINTR;
753
729
      goto mandos_end;
754
730
    }
755
731
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
759
735
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
760
736
      gnutls_perror(ret);
761
737
    }
762
 
    errno = EPROTO;
763
738
    goto mandos_end;
764
739
  }
765
740
  
773
748
  while(true){
774
749
    
775
750
    if(quit_now){
776
 
      errno = EINTR;
777
751
      goto mandos_end;
778
752
    }
779
753
    
780
754
    buffer_capacity = incbuffer(&buffer, buffer_length,
781
755
                                   buffer_capacity);
782
756
    if(buffer_capacity == 0){
783
 
      int e = errno;
784
757
      perror("incbuffer");
785
 
      errno = e;
786
758
      goto mandos_end;
787
759
    }
788
760
    
789
761
    if(quit_now){
790
 
      errno = EINTR;
791
762
      goto mandos_end;
792
763
    }
793
764
    
806
777
          ret = gnutls_handshake(session);
807
778
          
808
779
          if(quit_now){
809
 
            errno = EINTR;
810
780
            goto mandos_end;
811
781
          }
812
782
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
813
783
        if(ret < 0){
814
784
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
815
785
          gnutls_perror(ret);
816
 
          errno = EPROTO;
817
786
          goto mandos_end;
818
787
        }
819
788
        break;
821
790
        fprintf(stderr, "Unknown error while reading data from"
822
791
                " encrypted session with Mandos server\n");
823
792
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
824
 
        errno = EIO;
825
793
        goto mandos_end;
826
794
      }
827
795
    } else {
834
802
  }
835
803
  
836
804
  if(quit_now){
837
 
    errno = EINTR;
838
805
    goto mandos_end;
839
806
  }
840
807
  
841
808
  do {
842
809
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
843
810
    if(quit_now){
844
 
      errno = EINTR;
845
811
      goto mandos_end;
846
812
    }
847
813
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
856
822
      written = 0;
857
823
      while(written < (size_t) decrypted_buffer_size){
858
824
        if(quit_now){
859
 
          errno = EINTR;
860
825
          goto mandos_end;
861
826
        }
862
827
        
864
829
                          (size_t)decrypted_buffer_size - written,
865
830
                          stdout);
866
831
        if(ret == 0 and ferror(stdout)){
867
 
          int e = errno;
868
832
          if(debug){
869
833
            fprintf(stderr, "Error writing encrypted data: %s\n",
870
834
                    strerror(errno));
871
835
          }
872
 
          errno = e;
873
836
          goto mandos_end;
874
837
        }
875
838
        written += (size_t)ret;
881
844
  /* Shutdown procedure */
882
845
  
883
846
 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;
 
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;
903
858
  }
904
859
  return retval;
905
860
}
1029
984
int good_interface(const struct dirent *if_entry){
1030
985
  ssize_t ssret;
1031
986
  char *flagname = NULL;
1032
 
  if(if_entry->d_name[0] == '.'){
1033
 
    return 0;
1034
 
  }
1035
987
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
988
                     if_entry->d_name);
1037
989
  if(ret < 0){
1038
990
    perror("asprintf");
1039
991
    return 0;
1040
992
  }
 
993
  if(if_entry->d_name[0] == '.'){
 
994
    return 0;
 
995
  }
1041
996
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1042
997
  if(flags_fd == -1){
1043
998
    perror("open");
1044
 
    free(flagname);
1045
999
    return 0;
1046
1000
  }
1047
 
  free(flagname);
1048
1001
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1049
1002
  /* 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';
 
1003
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1004
  char *flagstring = malloc((size_t)to_read);
1053
1005
  if(flagstring == NULL){
1054
1006
    perror("malloc");
1055
1007
    close(flags_fd);
1077
1029
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1078
1030
                                         and not (isspace(*tmp)))
1079
1031
     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
1032
    free(flagstring);
1085
1033
    return 0;
1086
1034
  }
1088
1036
  ifreq_flags flags = (ifreq_flags)tmpmax;
1089
1037
  /* Reject the loopback device */
1090
1038
  if(flags & IFF_LOOPBACK){
1091
 
    if(debug){
1092
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1093
 
              if_entry->d_name);
1094
 
    }
1095
1039
    return 0;
1096
1040
  }
1097
1041
  /* Accept point-to-point devices only if connect_to is specified */
1098
1042
  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
1043
    return 1;
1104
1044
  }
1105
1045
  /* Otherwise, reject non-broadcast-capable devices */
1106
1046
  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
1047
    return 0;
1120
1048
  }
1121
1049
  /* Accept this device */
1122
 
  if(debug){
1123
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1124
 
            if_entry->d_name);
1125
 
  }
1126
1050
  return 1;
1127
1051
}
1128
1052
 
1207
1131
        .arg = "SECONDS",
1208
1132
        .doc = "Maximum delay to wait for interface startup",
1209
1133
        .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
1134
      { .name = NULL }
1220
1135
    };
1221
1136
    
1222
1137
    error_t parse_opt(int key, char *arg,
1223
1138
                      struct argp_state *state){
1224
 
      errno = 0;
1225
1139
      switch(key){
1226
1140
      case 128:                 /* --debug */
1227
1141
        debug = true;
1243
1157
        tmpmax = strtoimax(arg, &tmp, 10);
1244
1158
        if(errno != 0 or tmp == arg or *tmp != '\0'
1245
1159
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1246
 
          argp_error(state, "Bad number of DH bits");
 
1160
          fprintf(stderr, "Bad number of DH bits\n");
 
1161
          exit(EXIT_FAILURE);
1247
1162
        }
1248
1163
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1249
1164
        break;
1254
1169
        errno = 0;
1255
1170
        delay = strtof(arg, &tmp);
1256
1171
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1257
 
          argp_error(state, "Bad delay");
 
1172
          fprintf(stderr, "Bad delay\n");
 
1173
          exit(EXIT_FAILURE);
1258
1174
        }
1259
1175
        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);
 
1176
      case ARGP_KEY_ARG:
 
1177
        argp_usage(state);
 
1178
      case ARGP_KEY_END:
1273
1179
        break;
1274
1180
      default:
1275
1181
        return ARGP_ERR_UNKNOWN;
1276
1182
      }
1277
 
      return errno;
 
1183
      return 0;
1278
1184
    }
1279
1185
    
1280
1186
    struct argp argp = { .options = options, .parser = parse_opt,
1281
1187
                         .args_doc = "",
1282
1188
                         .doc = "Mandos client -- Get and decrypt"
1283
1189
                         " 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;
 
1190
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1191
    if(ret == ARGP_ERR_UNKNOWN){
 
1192
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1193
      exitcode = EXIT_FAILURE;
1297
1194
      goto end;
1298
1195
    }
1299
1196
  }
1309
1206
    if(ret >= 1){
1310
1207
      /* Pick the first good interface */
1311
1208
      interface = strdup(direntries[0]->d_name);
1312
 
      if(debug){
1313
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1314
 
      }
1315
1209
      if(interface == NULL){
1316
1210
        perror("malloc");
1317
1211
        free(direntries);
1334
1228
  mc.simple_poll = avahi_simple_poll_new();
1335
1229
  if(mc.simple_poll == NULL){
1336
1230
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1337
 
    exitcode = EX_UNAVAILABLE;
 
1231
    exitcode = EXIT_FAILURE;
1338
1232
    goto end;
1339
1233
  }
1340
1234
  
1342
1236
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1343
1237
  if(ret == -1){
1344
1238
    perror("sigaddset");
1345
 
    exitcode = EX_OSERR;
 
1239
    exitcode = EXIT_FAILURE;
1346
1240
    goto end;
1347
1241
  }
1348
1242
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1349
1243
  if(ret == -1){
1350
1244
    perror("sigaddset");
1351
 
    exitcode = EX_OSERR;
 
1245
    exitcode = EXIT_FAILURE;
1352
1246
    goto end;
1353
1247
  }
1354
1248
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1355
1249
  if(ret == -1){
1356
1250
    perror("sigaddset");
1357
 
    exitcode = EX_OSERR;
 
1251
    exitcode = EXIT_FAILURE;
1358
1252
    goto end;
1359
1253
  }
1360
1254
  /* Need to check if the handler is SIG_IGN before handling:
1364
1258
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1365
1259
  if(ret == -1){
1366
1260
    perror("sigaction");
1367
 
    return EX_OSERR;
 
1261
    return EXIT_FAILURE;
1368
1262
  }
1369
1263
  if(old_sigterm_action.sa_handler != SIG_IGN){
1370
1264
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1371
1265
    if(ret == -1){
1372
1266
      perror("sigaction");
1373
 
      exitcode = EX_OSERR;
 
1267
      exitcode = EXIT_FAILURE;
1374
1268
      goto end;
1375
1269
    }
1376
1270
  }
1377
1271
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1378
1272
  if(ret == -1){
1379
1273
    perror("sigaction");
1380
 
    return EX_OSERR;
 
1274
    return EXIT_FAILURE;
1381
1275
  }
1382
1276
  if(old_sigterm_action.sa_handler != SIG_IGN){
1383
1277
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1384
1278
    if(ret == -1){
1385
1279
      perror("sigaction");
1386
 
      exitcode = EX_OSERR;
 
1280
      exitcode = EXIT_FAILURE;
1387
1281
      goto end;
1388
1282
    }
1389
1283
  }
1390
1284
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1391
1285
  if(ret == -1){
1392
1286
    perror("sigaction");
1393
 
    return EX_OSERR;
 
1287
    return EXIT_FAILURE;
1394
1288
  }
1395
1289
  if(old_sigterm_action.sa_handler != SIG_IGN){
1396
1290
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1397
1291
    if(ret == -1){
1398
1292
      perror("sigaction");
1399
 
      exitcode = EX_OSERR;
 
1293
      exitcode = EXIT_FAILURE;
1400
1294
      goto end;
1401
1295
    }
1402
1296
  }
1406
1300
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1407
1301
    if(if_index == 0){
1408
1302
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1409
 
      exitcode = EX_UNAVAILABLE;
 
1303
      exitcode = EXIT_FAILURE;
1410
1304
      goto end;
1411
1305
    }
1412
1306
    
1423
1317
    
1424
1318
#ifdef __linux__
1425
1319
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1426
 
       messages about the network interface to mess up the prompt */
 
1320
       messages to mess up the prompt */
1427
1321
    ret = klogctl(8, NULL, 5);
1428
1322
    bool restore_loglevel = true;
1429
1323
    if(ret == -1){
1435
1329
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1436
1330
    if(sd < 0){
1437
1331
      perror("socket");
1438
 
      exitcode = EX_OSERR;
 
1332
      exitcode = EXIT_FAILURE;
1439
1333
#ifdef __linux__
1440
1334
      if(restore_loglevel){
1441
1335
        ret = klogctl(7, NULL, 0);
1464
1358
        }
1465
1359
      }
1466
1360
#endif  /* __linux__ */
1467
 
      exitcode = EX_OSERR;
 
1361
      exitcode = EXIT_FAILURE;
1468
1362
      /* Lower privileges */
1469
1363
      errno = 0;
1470
1364
      ret = seteuid(uid);
1479
1373
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1480
1374
      if(ret == -1){
1481
1375
        take_down_interface = false;
1482
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1483
 
        exitcode = EX_OSERR;
 
1376
        perror("ioctl SIOCSIFFLAGS");
 
1377
        exitcode = EXIT_FAILURE;
1484
1378
#ifdef __linux__
1485
1379
        if(restore_loglevel){
1486
1380
          ret = klogctl(7, NULL, 0);
1552
1446
  ret = init_gnutls_global(pubkey, seckey);
1553
1447
  if(ret == -1){
1554
1448
    fprintf(stderr, "init_gnutls_global failed\n");
1555
 
    exitcode = EX_UNAVAILABLE;
 
1449
    exitcode = EXIT_FAILURE;
1556
1450
    goto end;
1557
1451
  } else {
1558
1452
    gnutls_initialized = true;
1575
1469
  
1576
1470
  if(not init_gpgme(pubkey, seckey, tempdir)){
1577
1471
    fprintf(stderr, "init_gpgme failed\n");
1578
 
    exitcode = EX_UNAVAILABLE;
 
1472
    exitcode = EXIT_FAILURE;
1579
1473
    goto end;
1580
1474
  } else {
1581
1475
    gpgme_initialized = true;
1591
1485
    char *address = strrchr(connect_to, ':');
1592
1486
    if(address == NULL){
1593
1487
      fprintf(stderr, "No colon in address\n");
1594
 
      exitcode = EX_USAGE;
 
1488
      exitcode = EXIT_FAILURE;
1595
1489
      goto end;
1596
1490
    }
1597
1491
    
1605
1499
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1606
1500
       or tmpmax != (uint16_t)tmpmax){
1607
1501
      fprintf(stderr, "Bad port number\n");
1608
 
      exitcode = EX_USAGE;
 
1502
      exitcode = EXIT_FAILURE;
1609
1503
      goto end;
1610
1504
    }
1611
1505
  
1627
1521
    if(quit_now){
1628
1522
      goto end;
1629
1523
    }
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){
 
1524
    
 
1525
    ret = start_mandos_communication(address, port, if_index, af);
 
1526
    if(ret < 0){
 
1527
      exitcode = EXIT_FAILURE;
 
1528
    } else {
1640
1529
      exitcode = EXIT_SUCCESS;
1641
1530
    }
1642
 
 
1643
1531
    goto end;
1644
1532
  }
1645
1533
  
1669
1557
  if(mc.server == NULL){
1670
1558
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1671
1559
            avahi_strerror(error));
1672
 
    exitcode = EX_UNAVAILABLE;
 
1560
    exitcode = EXIT_FAILURE;
1673
1561
    goto end;
1674
1562
  }
1675
1563
  
1684
1572
  if(sb == NULL){
1685
1573
    fprintf(stderr, "Failed to create service browser: %s\n",
1686
1574
            avahi_strerror(avahi_server_errno(mc.server)));
1687
 
    exitcode = EX_UNAVAILABLE;
 
1575
    exitcode = EXIT_FAILURE;
1688
1576
    goto end;
1689
1577
  }
1690
1578
  
1739
1627
      if(ret == -1){
1740
1628
        perror("ioctl SIOCGIFFLAGS");
1741
1629
      } else if(network.ifr_flags & IFF_UP) {
1742
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1630
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1743
1631
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1744
1632
        if(ret == -1){
1745
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1633
          perror("ioctl SIOCSIFFLAGS");
1746
1634
        }
1747
1635
      }
1748
1636
      ret = (int)TEMP_FAILURE_RETRY(close(sd));