/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

merge

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 */
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 {
556
552
  gnutls_session_t session;
557
553
  int pf;                       /* Protocol family */
558
554
  
559
 
  errno = 0;
560
 
  
561
555
  if(quit_now){
562
 
    errno = EINTR;
563
556
    return -1;
564
557
  }
565
558
  
572
565
    break;
573
566
  default:
574
567
    fprintf(stderr, "Bad address family: %d\n", af);
575
 
    errno = EINVAL;
576
568
    return -1;
577
569
  }
578
570
  
588
580
  
589
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
582
  if(tcp_sd < 0){
591
 
    int e = errno;
592
583
    perror("socket");
593
 
    errno = e;
594
584
    goto mandos_end;
595
585
  }
596
586
  
597
587
  if(quit_now){
598
 
    errno = EINTR;
599
588
    goto mandos_end;
600
589
  }
601
590
  
608
597
    ret = inet_pton(af, ip, &to.in.sin_addr);
609
598
  }
610
599
  if(ret < 0 ){
611
 
    int e = errno;
612
600
    perror("inet_pton");
613
 
    errno = e;
614
601
    goto mandos_end;
615
602
  }
616
603
  if(ret == 0){
617
 
    int e = errno;
618
604
    fprintf(stderr, "Bad address: %s\n", ip);
619
 
    errno = e;
620
605
    goto mandos_end;
621
606
  }
622
607
  if(af == AF_INET6){
630
615
      if(if_index == AVAHI_IF_UNSPEC){
631
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
632
617
                " without a network interface\n");
633
 
        errno = EINVAL;
634
618
        goto mandos_end;
635
619
      }
636
620
      /* Set the network interface number as scope */
643
627
  }
644
628
  
645
629
  if(quit_now){
646
 
    errno = EINTR;
647
630
    goto mandos_end;
648
631
  }
649
632
  
680
663
  }
681
664
  
682
665
  if(quit_now){
683
 
    errno = EINTR;
684
666
    goto mandos_end;
685
667
  }
686
668
  
690
672
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
673
  }
692
674
  if(ret < 0){
693
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
 
      int e = errno;
695
 
      perror("connect");
696
 
      errno = e;
697
 
    }
 
675
    perror("connect");
698
676
    goto mandos_end;
699
677
  }
700
678
  
701
679
  if(quit_now){
702
 
    errno = EINTR;
703
680
    goto mandos_end;
704
681
  }
705
682
  
710
687
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
711
688
                                   out_size - written));
712
689
    if(ret == -1){
713
 
      int e = errno;
714
690
      perror("write");
715
 
      errno = e;
716
691
      goto mandos_end;
717
692
    }
718
693
    written += (size_t)ret;
728
703
    }
729
704
  
730
705
    if(quit_now){
731
 
      errno = EINTR;
732
706
      goto mandos_end;
733
707
    }
734
708
  }
738
712
  }
739
713
  
740
714
  if(quit_now){
741
 
    errno = EINTR;
742
715
    goto mandos_end;
743
716
  }
744
 
 
745
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
717
  
746
718
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
747
719
  
748
720
  if(quit_now){
749
 
    errno = EINTR;
750
721
    goto mandos_end;
751
722
  }
752
723
  
753
724
  do {
754
725
    ret = gnutls_handshake(session);
755
726
    if(quit_now){
756
 
      errno = EINTR;
757
727
      goto mandos_end;
758
728
    }
759
729
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
763
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
764
734
      gnutls_perror(ret);
765
735
    }
766
 
    errno = EPROTO;
767
736
    goto mandos_end;
768
737
  }
769
738
  
777
746
  while(true){
778
747
    
779
748
    if(quit_now){
780
 
      errno = EINTR;
781
749
      goto mandos_end;
782
750
    }
783
751
    
784
752
    buffer_capacity = incbuffer(&buffer, buffer_length,
785
753
                                   buffer_capacity);
786
754
    if(buffer_capacity == 0){
787
 
      int e = errno;
788
755
      perror("incbuffer");
789
 
      errno = e;
790
756
      goto mandos_end;
791
757
    }
792
758
    
793
759
    if(quit_now){
794
 
      errno = EINTR;
795
760
      goto mandos_end;
796
761
    }
797
762
    
810
775
          ret = gnutls_handshake(session);
811
776
          
812
777
          if(quit_now){
813
 
            errno = EINTR;
814
778
            goto mandos_end;
815
779
          }
816
780
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
817
781
        if(ret < 0){
818
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
819
783
          gnutls_perror(ret);
820
 
          errno = EPROTO;
821
784
          goto mandos_end;
822
785
        }
823
786
        break;
825
788
        fprintf(stderr, "Unknown error while reading data from"
826
789
                " encrypted session with Mandos server\n");
827
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
828
 
        errno = EIO;
829
791
        goto mandos_end;
830
792
      }
831
793
    } else {
838
800
  }
839
801
  
840
802
  if(quit_now){
841
 
    errno = EINTR;
842
803
    goto mandos_end;
843
804
  }
844
805
  
845
806
  do {
846
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
847
808
    if(quit_now){
848
 
      errno = EINTR;
849
809
      goto mandos_end;
850
810
    }
851
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
860
820
      written = 0;
861
821
      while(written < (size_t) decrypted_buffer_size){
862
822
        if(quit_now){
863
 
          errno = EINTR;
864
823
          goto mandos_end;
865
824
        }
866
825
        
868
827
                          (size_t)decrypted_buffer_size - written,
869
828
                          stdout);
870
829
        if(ret == 0 and ferror(stdout)){
871
 
          int e = errno;
872
830
          if(debug){
873
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
874
832
                    strerror(errno));
875
833
          }
876
 
          errno = e;
877
834
          goto mandos_end;
878
835
        }
879
836
        written += (size_t)ret;
885
842
  /* Shutdown procedure */
886
843
  
887
844
 mandos_end:
888
 
  {
889
 
    int e = errno;
890
 
    free(decrypted_buffer);
891
 
    free(buffer);
892
 
    if(tcp_sd >= 0){
893
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
894
 
    }
895
 
    if(ret == -1){
896
 
      if(e == 0){
897
 
        e = errno;
898
 
      }
899
 
      perror("close");
900
 
    }
901
 
    gnutls_deinit(session);
902
 
    if(quit_now){
903
 
      e = EINTR;
904
 
      retval = -1;
905
 
    }
906
 
    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;
907
856
  }
908
857
  return retval;
909
858
}
1025
974
  errno = old_errno;
1026
975
}
1027
976
 
1028
 
/* 
1029
 
 * This function determines if a directory entry in /sys/class/net
1030
 
 * corresponds to an acceptable network device.
1031
 
 * (This function is passed to scandir(3) as a filter function.)
1032
 
 */
1033
 
int good_interface(const struct dirent *if_entry){
1034
 
  ssize_t ssret;
1035
 
  char *flagname = NULL;
1036
 
  if(if_entry->d_name[0] == '.'){
1037
 
    return 0;
1038
 
  }
1039
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1040
 
                     if_entry->d_name);
1041
 
  if(ret < 0){
1042
 
    perror("asprintf");
1043
 
    return 0;
1044
 
  }
1045
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1046
 
  if(flags_fd == -1){
1047
 
    perror("open");
1048
 
    free(flagname);
1049
 
    return 0;
1050
 
  }
1051
 
  free(flagname);
1052
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1053
 
  /* read line from flags_fd */
1054
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1055
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1056
 
  flagstring[(size_t)to_read] = '\0';
1057
 
  if(flagstring == NULL){
1058
 
    perror("malloc");
1059
 
    close(flags_fd);
1060
 
    return 0;
1061
 
  }
1062
 
  while(to_read > 0){
1063
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1064
 
                                             (size_t)to_read));
1065
 
    if(ssret == -1){
1066
 
      perror("read");
1067
 
      free(flagstring);
1068
 
      close(flags_fd);
1069
 
      return 0;
1070
 
    }
1071
 
    to_read -= ssret;
1072
 
    if(ssret == 0){
1073
 
      break;
1074
 
    }
1075
 
  }
1076
 
  close(flags_fd);
1077
 
  intmax_t tmpmax;
1078
 
  char *tmp;
1079
 
  errno = 0;
1080
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1081
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1082
 
                                         and not (isspace(*tmp)))
1083
 
     or tmpmax != (ifreq_flags)tmpmax){
1084
 
    if(debug){
1085
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1086
 
              flagstring, if_entry->d_name);
1087
 
    }
1088
 
    free(flagstring);
1089
 
    return 0;
1090
 
  }
1091
 
  free(flagstring);
1092
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1093
 
  /* Reject the loopback device */
1094
 
  if(flags & IFF_LOOPBACK){
1095
 
    if(debug){
1096
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1097
 
              if_entry->d_name);
1098
 
    }
1099
 
    return 0;
1100
 
  }
1101
 
  /* Accept point-to-point devices only if connect_to is specified */
1102
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1103
 
    if(debug){
1104
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1105
 
              if_entry->d_name);
1106
 
    }
1107
 
    return 1;
1108
 
  }
1109
 
  /* Otherwise, reject non-broadcast-capable devices */
1110
 
  if(not (flags & IFF_BROADCAST)){
1111
 
    if(debug){
1112
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1113
 
              if_entry->d_name);
1114
 
    }
1115
 
    return 0;
1116
 
  }
1117
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1118
 
  if(flags & IFF_NOARP){
1119
 
    if(debug){
1120
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1121
 
              if_entry->d_name);
1122
 
    }
1123
 
    return 0;
1124
 
  }
1125
 
  /* Accept this device */
1126
 
  if(debug){
1127
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1128
 
            if_entry->d_name);
1129
 
  }
1130
 
  return 1;
1131
 
}
1132
 
 
1133
 
int notdotentries(const struct dirent *direntry){
1134
 
  /* Skip "." and ".." */
1135
 
  if(direntry->d_name[0] == '.'
1136
 
     and (direntry->d_name[1] == '\0'
1137
 
          or (direntry->d_name[1] == '.'
1138
 
              and direntry->d_name[2] == '\0'))){
1139
 
    return 0;
1140
 
  }
1141
 
  return 1;
1142
 
}
1143
 
 
1144
977
int main(int argc, char *argv[]){
1145
978
  AvahiSServiceBrowser *sb = NULL;
1146
979
  int error;
1148
981
  intmax_t tmpmax;
1149
982
  char *tmp;
1150
983
  int exitcode = EXIT_SUCCESS;
1151
 
  const char *interface = "";
 
984
  const char *interface = "eth0";
1152
985
  struct ifreq network;
1153
986
  int sd = -1;
1154
987
  bool take_down_interface = false;
1155
988
  uid_t uid;
1156
989
  gid_t gid;
 
990
  char *connect_to = NULL;
1157
991
  char tempdir[] = "/tmp/mandosXXXXXX";
1158
992
  bool tempdir_created = false;
1159
993
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1222
1056
        .arg = "SECONDS",
1223
1057
        .doc = "Maximum delay to wait for interface startup",
1224
1058
        .group = 2 },
1225
 
      /*
1226
 
       * These reproduce what we would get without ARGP_NO_HELP
1227
 
       */
1228
 
      { .name = "help", .key = '?',
1229
 
        .doc = "Give this help list", .group = -1 },
1230
 
      { .name = "usage", .key = -3,
1231
 
        .doc = "Give a short usage message", .group = -1 },
1232
 
      { .name = "version", .key = 'V',
1233
 
        .doc = "Print program version", .group = -1 },
1234
1059
      { .name = NULL }
1235
1060
    };
1236
1061
    
1237
1062
    error_t parse_opt(int key, char *arg,
1238
1063
                      struct argp_state *state){
1239
 
      errno = 0;
1240
1064
      switch(key){
1241
1065
      case 128:                 /* --debug */
1242
1066
        debug = true;
1258
1082
        tmpmax = strtoimax(arg, &tmp, 10);
1259
1083
        if(errno != 0 or tmp == arg or *tmp != '\0'
1260
1084
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1261
 
          argp_error(state, "Bad number of DH bits");
 
1085
          fprintf(stderr, "Bad number of DH bits\n");
 
1086
          exit(EXIT_FAILURE);
1262
1087
        }
1263
1088
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1264
1089
        break;
1269
1094
        errno = 0;
1270
1095
        delay = strtof(arg, &tmp);
1271
1096
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1272
 
          argp_error(state, "Bad delay");
 
1097
          fprintf(stderr, "Bad delay\n");
 
1098
          exit(EXIT_FAILURE);
1273
1099
        }
1274
1100
        break;
1275
 
        /*
1276
 
         * These reproduce what we would get without ARGP_NO_HELP
1277
 
         */
1278
 
      case '?':                 /* --help */
1279
 
        argp_state_help(state, state->out_stream,
1280
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1281
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1282
 
      case -3:                  /* --usage */
1283
 
        argp_state_help(state, state->out_stream,
1284
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1285
 
      case 'V':                 /* --version */
1286
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1287
 
        exit(argp_err_exit_status);
 
1101
      case ARGP_KEY_ARG:
 
1102
        argp_usage(state);
 
1103
      case ARGP_KEY_END:
1288
1104
        break;
1289
1105
      default:
1290
1106
        return ARGP_ERR_UNKNOWN;
1291
1107
      }
1292
 
      return errno;
 
1108
      return 0;
1293
1109
    }
1294
1110
    
1295
1111
    struct argp argp = { .options = options, .parser = parse_opt,
1296
1112
                         .args_doc = "",
1297
1113
                         .doc = "Mandos client -- Get and decrypt"
1298
1114
                         " passwords from a Mandos server" };
1299
 
    ret = argp_parse(&argp, argc, argv,
1300
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1301
 
    switch(ret){
1302
 
    case 0:
1303
 
      break;
1304
 
    case ENOMEM:
1305
 
    default:
1306
 
      errno = ret;
1307
 
      perror("argp_parse");
1308
 
      exitcode = EX_OSERR;
1309
 
      goto end;
1310
 
    case EINVAL:
1311
 
      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;
1312
1119
      goto end;
1313
1120
    }
1314
1121
  }
1316
1123
  if(not debug){
1317
1124
    avahi_set_log_function(empty_log);
1318
1125
  }
1319
 
 
1320
 
  if(interface[0] == '\0'){
1321
 
    struct dirent **direntries;
1322
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1323
 
                  alphasort);
1324
 
    if(ret >= 1){
1325
 
      /* Pick the first good interface */
1326
 
      interface = strdup(direntries[0]->d_name);
1327
 
      if(debug){
1328
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1329
 
      }
1330
 
      if(interface == NULL){
1331
 
        perror("malloc");
1332
 
        free(direntries);
1333
 
        exitcode = EXIT_FAILURE;
1334
 
        goto end;
1335
 
      }
1336
 
      free(direntries);
1337
 
    } else {
1338
 
      free(direntries);
1339
 
      fprintf(stderr, "Could not find a network interface\n");
1340
 
      exitcode = EXIT_FAILURE;
1341
 
      goto end;
1342
 
    }
1343
 
  }
1344
1126
  
1345
1127
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1346
1128
     from the signal handler */
1349
1131
  mc.simple_poll = avahi_simple_poll_new();
1350
1132
  if(mc.simple_poll == NULL){
1351
1133
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1352
 
    exitcode = EX_UNAVAILABLE;
 
1134
    exitcode = EXIT_FAILURE;
1353
1135
    goto end;
1354
1136
  }
1355
1137
  
1357
1139
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1358
1140
  if(ret == -1){
1359
1141
    perror("sigaddset");
1360
 
    exitcode = EX_OSERR;
 
1142
    exitcode = EXIT_FAILURE;
1361
1143
    goto end;
1362
1144
  }
1363
1145
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1364
1146
  if(ret == -1){
1365
1147
    perror("sigaddset");
1366
 
    exitcode = EX_OSERR;
 
1148
    exitcode = EXIT_FAILURE;
1367
1149
    goto end;
1368
1150
  }
1369
1151
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1370
1152
  if(ret == -1){
1371
1153
    perror("sigaddset");
1372
 
    exitcode = EX_OSERR;
 
1154
    exitcode = EXIT_FAILURE;
1373
1155
    goto end;
1374
1156
  }
1375
1157
  /* Need to check if the handler is SIG_IGN before handling:
1379
1161
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1380
1162
  if(ret == -1){
1381
1163
    perror("sigaction");
1382
 
    return EX_OSERR;
 
1164
    return EXIT_FAILURE;
1383
1165
  }
1384
1166
  if(old_sigterm_action.sa_handler != SIG_IGN){
1385
1167
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1386
1168
    if(ret == -1){
1387
1169
      perror("sigaction");
1388
 
      exitcode = EX_OSERR;
 
1170
      exitcode = EXIT_FAILURE;
1389
1171
      goto end;
1390
1172
    }
1391
1173
  }
1392
1174
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1393
1175
  if(ret == -1){
1394
1176
    perror("sigaction");
1395
 
    return EX_OSERR;
 
1177
    return EXIT_FAILURE;
1396
1178
  }
1397
1179
  if(old_sigterm_action.sa_handler != SIG_IGN){
1398
1180
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1399
1181
    if(ret == -1){
1400
1182
      perror("sigaction");
1401
 
      exitcode = EX_OSERR;
 
1183
      exitcode = EXIT_FAILURE;
1402
1184
      goto end;
1403
1185
    }
1404
1186
  }
1405
1187
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1406
1188
  if(ret == -1){
1407
1189
    perror("sigaction");
1408
 
    return EX_OSERR;
 
1190
    return EXIT_FAILURE;
1409
1191
  }
1410
1192
  if(old_sigterm_action.sa_handler != SIG_IGN){
1411
1193
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1412
1194
    if(ret == -1){
1413
1195
      perror("sigaction");
1414
 
      exitcode = EX_OSERR;
 
1196
      exitcode = EXIT_FAILURE;
1415
1197
      goto end;
1416
1198
    }
1417
1199
  }
1418
1200
  
1419
1201
  /* If the interface is down, bring it up */
1420
 
  if(strcmp(interface, "none") != 0){
 
1202
  if(interface[0] != '\0'){
1421
1203
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1422
1204
    if(if_index == 0){
1423
1205
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1424
 
      exitcode = EX_UNAVAILABLE;
 
1206
      exitcode = EXIT_FAILURE;
1425
1207
      goto end;
1426
1208
    }
1427
1209
    
1438
1220
    
1439
1221
#ifdef __linux__
1440
1222
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1441
 
       messages about the network interface to mess up the prompt */
 
1223
       messages to mess up the prompt */
1442
1224
    ret = klogctl(8, NULL, 5);
1443
1225
    bool restore_loglevel = true;
1444
1226
    if(ret == -1){
1450
1232
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1451
1233
    if(sd < 0){
1452
1234
      perror("socket");
1453
 
      exitcode = EX_OSERR;
 
1235
      exitcode = EXIT_FAILURE;
1454
1236
#ifdef __linux__
1455
1237
      if(restore_loglevel){
1456
1238
        ret = klogctl(7, NULL, 0);
1479
1261
        }
1480
1262
      }
1481
1263
#endif  /* __linux__ */
1482
 
      exitcode = EX_OSERR;
 
1264
      exitcode = EXIT_FAILURE;
1483
1265
      /* Lower privileges */
1484
1266
      errno = 0;
1485
1267
      ret = seteuid(uid);
1494
1276
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1495
1277
      if(ret == -1){
1496
1278
        take_down_interface = false;
1497
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1498
 
        exitcode = EX_OSERR;
 
1279
        perror("ioctl SIOCSIFFLAGS");
 
1280
        exitcode = EXIT_FAILURE;
1499
1281
#ifdef __linux__
1500
1282
        if(restore_loglevel){
1501
1283
          ret = klogctl(7, NULL, 0);
1567
1349
  ret = init_gnutls_global(pubkey, seckey);
1568
1350
  if(ret == -1){
1569
1351
    fprintf(stderr, "init_gnutls_global failed\n");
1570
 
    exitcode = EX_UNAVAILABLE;
 
1352
    exitcode = EXIT_FAILURE;
1571
1353
    goto end;
1572
1354
  } else {
1573
1355
    gnutls_initialized = true;
1577
1359
    goto end;
1578
1360
  }
1579
1361
  
 
1362
  tempdir_created = true;
1580
1363
  if(mkdtemp(tempdir) == NULL){
 
1364
    tempdir_created = false;
1581
1365
    perror("mkdtemp");
1582
1366
    goto end;
1583
1367
  }
1584
 
  tempdir_created = true;
1585
1368
  
1586
1369
  if(quit_now){
1587
1370
    goto end;
1589
1372
  
1590
1373
  if(not init_gpgme(pubkey, seckey, tempdir)){
1591
1374
    fprintf(stderr, "init_gpgme failed\n");
1592
 
    exitcode = EX_UNAVAILABLE;
 
1375
    exitcode = EXIT_FAILURE;
1593
1376
    goto end;
1594
1377
  } else {
1595
1378
    gpgme_initialized = true;
1605
1388
    char *address = strrchr(connect_to, ':');
1606
1389
    if(address == NULL){
1607
1390
      fprintf(stderr, "No colon in address\n");
1608
 
      exitcode = EX_USAGE;
 
1391
      exitcode = EXIT_FAILURE;
1609
1392
      goto end;
1610
1393
    }
1611
1394
    
1619
1402
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1620
1403
       or tmpmax != (uint16_t)tmpmax){
1621
1404
      fprintf(stderr, "Bad port number\n");
1622
 
      exitcode = EX_USAGE;
 
1405
      exitcode = EXIT_FAILURE;
1623
1406
      goto end;
1624
1407
    }
1625
1408
  
1641
1424
    if(quit_now){
1642
1425
      goto end;
1643
1426
    }
1644
 
 
1645
 
    while(not quit_now){
1646
 
      ret = start_mandos_communication(address, port, if_index, af);
1647
 
      if(quit_now or ret == 0){
1648
 
        break;
1649
 
      }
1650
 
      sleep(15);
1651
 
    };
1652
 
 
1653
 
    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 {
1654
1432
      exitcode = EXIT_SUCCESS;
1655
1433
    }
1656
 
 
1657
1434
    goto end;
1658
1435
  }
1659
1436
  
1683
1460
  if(mc.server == NULL){
1684
1461
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1685
1462
            avahi_strerror(error));
1686
 
    exitcode = EX_UNAVAILABLE;
 
1463
    exitcode = EXIT_FAILURE;
1687
1464
    goto end;
1688
1465
  }
1689
1466
  
1698
1475
  if(sb == NULL){
1699
1476
    fprintf(stderr, "Failed to create service browser: %s\n",
1700
1477
            avahi_strerror(avahi_server_errno(mc.server)));
1701
 
    exitcode = EX_UNAVAILABLE;
 
1478
    exitcode = EXIT_FAILURE;
1702
1479
    goto end;
1703
1480
  }
1704
1481
  
1753
1530
      if(ret == -1){
1754
1531
        perror("ioctl SIOCGIFFLAGS");
1755
1532
      } else if(network.ifr_flags & IFF_UP) {
1756
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1533
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1757
1534
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1758
1535
        if(ret == -1){
1759
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1536
          perror("ioctl SIOCSIFFLAGS");
1760
1537
        }
1761
1538
      }
1762
1539
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1772
1549
    }
1773
1550
  }
1774
1551
  
1775
 
  /* Removes the GPGME temp directory and all files inside */
 
1552
  /* Removes the temp directory used by GPGME */
1776
1553
  if(tempdir_created){
1777
 
    struct dirent **direntries = NULL;
1778
 
    struct dirent *direntry = NULL;
1779
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1780
 
    if (ret > 0){
1781
 
      for(int i = 0; i < ret; i++){
1782
 
        direntry = direntries[i];
 
1554
    DIR *d;
 
1555
    struct dirent *direntry;
 
1556
    d = opendir(tempdir);
 
1557
    if(d == NULL){
 
1558
      if(errno != ENOENT){
 
1559
        perror("opendir");
 
1560
      }
 
1561
    } else {
 
1562
      while(true){
 
1563
        direntry = readdir(d);
 
1564
        if(direntry == NULL){
 
1565
          break;
 
1566
        }
 
1567
        /* Skip "." and ".." */
 
1568
        if(direntry->d_name[0] == '.'
 
1569
           and (direntry->d_name[1] == '\0'
 
1570
                or (direntry->d_name[1] == '.'
 
1571
                    and direntry->d_name[2] == '\0'))){
 
1572
          continue;
 
1573
        }
1783
1574
        char *fullname = NULL;
1784
1575
        ret = asprintf(&fullname, "%s/%s", tempdir,
1785
1576
                       direntry->d_name);
1794
1585
        }
1795
1586
        free(fullname);
1796
1587
      }
1797
 
    }
1798
 
 
1799
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
1800
 
    free(direntries);
1801
 
    if (ret == -1){
1802
 
      perror("scandir");
 
1588
      closedir(d);
1803
1589
    }
1804
1590
    ret = rmdir(tempdir);
1805
1591
    if(ret == -1 and errno != ENOENT){