/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: Björn Påhlsson
  • Date: 2011-06-19 20:25:38 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110619202538-0js072v8fso12u07
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.

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 */
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
87
 
86
88
#ifdef __linux__
87
89
#include <sys/klog.h>           /* klogctl() */
125
127
static const char mandos_protocol_version[] = "1";
126
128
const char *argp_program_version = "mandos-client " VERSION;
127
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;
128
132
 
129
133
/* Used for passing in values through the Avahi callback functions */
130
134
typedef struct {
552
556
  gnutls_session_t session;
553
557
  int pf;                       /* Protocol family */
554
558
  
 
559
  errno = 0;
 
560
  
555
561
  if(quit_now){
 
562
    errno = EINTR;
556
563
    return -1;
557
564
  }
558
565
  
565
572
    break;
566
573
  default:
567
574
    fprintf(stderr, "Bad address family: %d\n", af);
 
575
    errno = EINVAL;
568
576
    return -1;
569
577
  }
570
578
  
580
588
  
581
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
582
590
  if(tcp_sd < 0){
 
591
    int e = errno;
583
592
    perror("socket");
 
593
    errno = e;
584
594
    goto mandos_end;
585
595
  }
586
596
  
587
597
  if(quit_now){
 
598
    errno = EINTR;
588
599
    goto mandos_end;
589
600
  }
590
601
  
597
608
    ret = inet_pton(af, ip, &to.in.sin_addr);
598
609
  }
599
610
  if(ret < 0 ){
 
611
    int e = errno;
600
612
    perror("inet_pton");
 
613
    errno = e;
601
614
    goto mandos_end;
602
615
  }
603
616
  if(ret == 0){
 
617
    int e = errno;
604
618
    fprintf(stderr, "Bad address: %s\n", ip);
 
619
    errno = e;
605
620
    goto mandos_end;
606
621
  }
607
622
  if(af == AF_INET6){
615
630
      if(if_index == AVAHI_IF_UNSPEC){
616
631
        fprintf(stderr, "An IPv6 link-local address is incomplete"
617
632
                " without a network interface\n");
 
633
        errno = EINVAL;
618
634
        goto mandos_end;
619
635
      }
620
636
      /* Set the network interface number as scope */
627
643
  }
628
644
  
629
645
  if(quit_now){
 
646
    errno = EINTR;
630
647
    goto mandos_end;
631
648
  }
632
649
  
663
680
  }
664
681
  
665
682
  if(quit_now){
 
683
    errno = EINTR;
666
684
    goto mandos_end;
667
685
  }
668
686
  
672
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
673
691
  }
674
692
  if(ret < 0){
675
 
    perror("connect");
 
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
694
      int e = errno;
 
695
      perror("connect");
 
696
      errno = e;
 
697
    }
676
698
    goto mandos_end;
677
699
  }
678
700
  
679
701
  if(quit_now){
 
702
    errno = EINTR;
680
703
    goto mandos_end;
681
704
  }
682
705
  
687
710
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
688
711
                                   out_size - written));
689
712
    if(ret == -1){
 
713
      int e = errno;
690
714
      perror("write");
 
715
      errno = e;
691
716
      goto mandos_end;
692
717
    }
693
718
    written += (size_t)ret;
703
728
    }
704
729
  
705
730
    if(quit_now){
 
731
      errno = EINTR;
706
732
      goto mandos_end;
707
733
    }
708
734
  }
712
738
  }
713
739
  
714
740
  if(quit_now){
 
741
    errno = EINTR;
715
742
    goto mandos_end;
716
743
  }
717
 
  
 
744
 
 
745
  /* Spurious warnings from -Wint-to-pointer-cast */
718
746
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
719
747
  
720
748
  if(quit_now){
 
749
    errno = EINTR;
721
750
    goto mandos_end;
722
751
  }
723
752
  
724
753
  do {
725
754
    ret = gnutls_handshake(session);
726
755
    if(quit_now){
 
756
      errno = EINTR;
727
757
      goto mandos_end;
728
758
    }
729
759
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
733
763
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
734
764
      gnutls_perror(ret);
735
765
    }
 
766
    errno = EPROTO;
736
767
    goto mandos_end;
737
768
  }
738
769
  
746
777
  while(true){
747
778
    
748
779
    if(quit_now){
 
780
      errno = EINTR;
749
781
      goto mandos_end;
750
782
    }
751
783
    
752
784
    buffer_capacity = incbuffer(&buffer, buffer_length,
753
785
                                   buffer_capacity);
754
786
    if(buffer_capacity == 0){
 
787
      int e = errno;
755
788
      perror("incbuffer");
 
789
      errno = e;
756
790
      goto mandos_end;
757
791
    }
758
792
    
759
793
    if(quit_now){
 
794
      errno = EINTR;
760
795
      goto mandos_end;
761
796
    }
762
797
    
775
810
          ret = gnutls_handshake(session);
776
811
          
777
812
          if(quit_now){
 
813
            errno = EINTR;
778
814
            goto mandos_end;
779
815
          }
780
816
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
781
817
        if(ret < 0){
782
818
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
783
819
          gnutls_perror(ret);
 
820
          errno = EPROTO;
784
821
          goto mandos_end;
785
822
        }
786
823
        break;
788
825
        fprintf(stderr, "Unknown error while reading data from"
789
826
                " encrypted session with Mandos server\n");
790
827
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
828
        errno = EIO;
791
829
        goto mandos_end;
792
830
      }
793
831
    } else {
800
838
  }
801
839
  
802
840
  if(quit_now){
 
841
    errno = EINTR;
803
842
    goto mandos_end;
804
843
  }
805
844
  
806
845
  do {
807
846
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
808
847
    if(quit_now){
 
848
      errno = EINTR;
809
849
      goto mandos_end;
810
850
    }
811
851
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
820
860
      written = 0;
821
861
      while(written < (size_t) decrypted_buffer_size){
822
862
        if(quit_now){
 
863
          errno = EINTR;
823
864
          goto mandos_end;
824
865
        }
825
866
        
827
868
                          (size_t)decrypted_buffer_size - written,
828
869
                          stdout);
829
870
        if(ret == 0 and ferror(stdout)){
 
871
          int e = errno;
830
872
          if(debug){
831
873
            fprintf(stderr, "Error writing encrypted data: %s\n",
832
874
                    strerror(errno));
833
875
          }
 
876
          errno = e;
834
877
          goto mandos_end;
835
878
        }
836
879
        written += (size_t)ret;
842
885
  /* Shutdown procedure */
843
886
  
844
887
 mandos_end:
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;
 
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;
856
907
  }
857
908
  return retval;
858
909
}
974
1025
  errno = old_errno;
975
1026
}
976
1027
 
 
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
 
977
1144
int main(int argc, char *argv[]){
978
1145
  AvahiSServiceBrowser *sb = NULL;
979
1146
  int error;
981
1148
  intmax_t tmpmax;
982
1149
  char *tmp;
983
1150
  int exitcode = EXIT_SUCCESS;
984
 
  const char *interface = "eth0";
 
1151
  const char *interface = "";
985
1152
  struct ifreq network;
986
1153
  int sd = -1;
987
1154
  bool take_down_interface = false;
988
1155
  uid_t uid;
989
1156
  gid_t gid;
990
 
  char *connect_to = NULL;
991
1157
  char tempdir[] = "/tmp/mandosXXXXXX";
992
1158
  bool tempdir_created = false;
993
1159
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1056
1222
        .arg = "SECONDS",
1057
1223
        .doc = "Maximum delay to wait for interface startup",
1058
1224
        .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 },
1059
1234
      { .name = NULL }
1060
1235
    };
1061
1236
    
1062
1237
    error_t parse_opt(int key, char *arg,
1063
1238
                      struct argp_state *state){
 
1239
      errno = 0;
1064
1240
      switch(key){
1065
1241
      case 128:                 /* --debug */
1066
1242
        debug = true;
1082
1258
        tmpmax = strtoimax(arg, &tmp, 10);
1083
1259
        if(errno != 0 or tmp == arg or *tmp != '\0'
1084
1260
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1085
 
          fprintf(stderr, "Bad number of DH bits\n");
1086
 
          exit(EXIT_FAILURE);
 
1261
          argp_error(state, "Bad number of DH bits");
1087
1262
        }
1088
1263
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1089
1264
        break;
1094
1269
        errno = 0;
1095
1270
        delay = strtof(arg, &tmp);
1096
1271
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1097
 
          fprintf(stderr, "Bad delay\n");
1098
 
          exit(EXIT_FAILURE);
 
1272
          argp_error(state, "Bad delay");
1099
1273
        }
1100
1274
        break;
1101
 
      case ARGP_KEY_ARG:
1102
 
        argp_usage(state);
1103
 
      case ARGP_KEY_END:
 
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);
1104
1288
        break;
1105
1289
      default:
1106
1290
        return ARGP_ERR_UNKNOWN;
1107
1291
      }
1108
 
      return 0;
 
1292
      return errno;
1109
1293
    }
1110
1294
    
1111
1295
    struct argp argp = { .options = options, .parser = parse_opt,
1112
1296
                         .args_doc = "",
1113
1297
                         .doc = "Mandos client -- Get and decrypt"
1114
1298
                         " passwords from a Mandos server" };
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;
 
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;
1119
1312
      goto end;
1120
1313
    }
1121
1314
  }
1123
1316
  if(not debug){
1124
1317
    avahi_set_log_function(empty_log);
1125
1318
  }
 
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
  }
1126
1344
  
1127
1345
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1128
1346
     from the signal handler */
1131
1349
  mc.simple_poll = avahi_simple_poll_new();
1132
1350
  if(mc.simple_poll == NULL){
1133
1351
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1134
 
    exitcode = EXIT_FAILURE;
 
1352
    exitcode = EX_UNAVAILABLE;
1135
1353
    goto end;
1136
1354
  }
1137
1355
  
1139
1357
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1140
1358
  if(ret == -1){
1141
1359
    perror("sigaddset");
1142
 
    exitcode = EXIT_FAILURE;
 
1360
    exitcode = EX_OSERR;
1143
1361
    goto end;
1144
1362
  }
1145
1363
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1146
1364
  if(ret == -1){
1147
1365
    perror("sigaddset");
1148
 
    exitcode = EXIT_FAILURE;
 
1366
    exitcode = EX_OSERR;
1149
1367
    goto end;
1150
1368
  }
1151
1369
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1152
1370
  if(ret == -1){
1153
1371
    perror("sigaddset");
1154
 
    exitcode = EXIT_FAILURE;
 
1372
    exitcode = EX_OSERR;
1155
1373
    goto end;
1156
1374
  }
1157
1375
  /* Need to check if the handler is SIG_IGN before handling:
1161
1379
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1162
1380
  if(ret == -1){
1163
1381
    perror("sigaction");
1164
 
    return EXIT_FAILURE;
 
1382
    return EX_OSERR;
1165
1383
  }
1166
1384
  if(old_sigterm_action.sa_handler != SIG_IGN){
1167
1385
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1168
1386
    if(ret == -1){
1169
1387
      perror("sigaction");
1170
 
      exitcode = EXIT_FAILURE;
 
1388
      exitcode = EX_OSERR;
1171
1389
      goto end;
1172
1390
    }
1173
1391
  }
1174
1392
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1175
1393
  if(ret == -1){
1176
1394
    perror("sigaction");
1177
 
    return EXIT_FAILURE;
 
1395
    return EX_OSERR;
1178
1396
  }
1179
1397
  if(old_sigterm_action.sa_handler != SIG_IGN){
1180
1398
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1181
1399
    if(ret == -1){
1182
1400
      perror("sigaction");
1183
 
      exitcode = EXIT_FAILURE;
 
1401
      exitcode = EX_OSERR;
1184
1402
      goto end;
1185
1403
    }
1186
1404
  }
1187
1405
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1188
1406
  if(ret == -1){
1189
1407
    perror("sigaction");
1190
 
    return EXIT_FAILURE;
 
1408
    return EX_OSERR;
1191
1409
  }
1192
1410
  if(old_sigterm_action.sa_handler != SIG_IGN){
1193
1411
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1194
1412
    if(ret == -1){
1195
1413
      perror("sigaction");
1196
 
      exitcode = EXIT_FAILURE;
 
1414
      exitcode = EX_OSERR;
1197
1415
      goto end;
1198
1416
    }
1199
1417
  }
1200
1418
  
1201
1419
  /* If the interface is down, bring it up */
1202
 
  if(interface[0] != '\0'){
 
1420
  if(strcmp(interface, "none") != 0){
1203
1421
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1204
1422
    if(if_index == 0){
1205
1423
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1206
 
      exitcode = EXIT_FAILURE;
 
1424
      exitcode = EX_UNAVAILABLE;
1207
1425
      goto end;
1208
1426
    }
1209
1427
    
1220
1438
    
1221
1439
#ifdef __linux__
1222
1440
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1223
 
       messages to mess up the prompt */
 
1441
       messages about the network interface to mess up the prompt */
1224
1442
    ret = klogctl(8, NULL, 5);
1225
1443
    bool restore_loglevel = true;
1226
1444
    if(ret == -1){
1232
1450
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1233
1451
    if(sd < 0){
1234
1452
      perror("socket");
1235
 
      exitcode = EXIT_FAILURE;
 
1453
      exitcode = EX_OSERR;
1236
1454
#ifdef __linux__
1237
1455
      if(restore_loglevel){
1238
1456
        ret = klogctl(7, NULL, 0);
1261
1479
        }
1262
1480
      }
1263
1481
#endif  /* __linux__ */
1264
 
      exitcode = EXIT_FAILURE;
 
1482
      exitcode = EX_OSERR;
1265
1483
      /* Lower privileges */
1266
1484
      errno = 0;
1267
1485
      ret = seteuid(uid);
1276
1494
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1277
1495
      if(ret == -1){
1278
1496
        take_down_interface = false;
1279
 
        perror("ioctl SIOCSIFFLAGS");
1280
 
        exitcode = EXIT_FAILURE;
 
1497
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1498
        exitcode = EX_OSERR;
1281
1499
#ifdef __linux__
1282
1500
        if(restore_loglevel){
1283
1501
          ret = klogctl(7, NULL, 0);
1349
1567
  ret = init_gnutls_global(pubkey, seckey);
1350
1568
  if(ret == -1){
1351
1569
    fprintf(stderr, "init_gnutls_global failed\n");
1352
 
    exitcode = EXIT_FAILURE;
 
1570
    exitcode = EX_UNAVAILABLE;
1353
1571
    goto end;
1354
1572
  } else {
1355
1573
    gnutls_initialized = true;
1359
1577
    goto end;
1360
1578
  }
1361
1579
  
1362
 
  tempdir_created = true;
1363
1580
  if(mkdtemp(tempdir) == NULL){
1364
 
    tempdir_created = false;
1365
1581
    perror("mkdtemp");
1366
1582
    goto end;
1367
1583
  }
 
1584
  tempdir_created = true;
1368
1585
  
1369
1586
  if(quit_now){
1370
1587
    goto end;
1372
1589
  
1373
1590
  if(not init_gpgme(pubkey, seckey, tempdir)){
1374
1591
    fprintf(stderr, "init_gpgme failed\n");
1375
 
    exitcode = EXIT_FAILURE;
 
1592
    exitcode = EX_UNAVAILABLE;
1376
1593
    goto end;
1377
1594
  } else {
1378
1595
    gpgme_initialized = true;
1388
1605
    char *address = strrchr(connect_to, ':');
1389
1606
    if(address == NULL){
1390
1607
      fprintf(stderr, "No colon in address\n");
1391
 
      exitcode = EXIT_FAILURE;
 
1608
      exitcode = EX_USAGE;
1392
1609
      goto end;
1393
1610
    }
1394
1611
    
1402
1619
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1403
1620
       or tmpmax != (uint16_t)tmpmax){
1404
1621
      fprintf(stderr, "Bad port number\n");
1405
 
      exitcode = EXIT_FAILURE;
 
1622
      exitcode = EX_USAGE;
1406
1623
      goto end;
1407
1624
    }
1408
1625
  
1424
1641
    if(quit_now){
1425
1642
      goto end;
1426
1643
    }
1427
 
    
1428
 
    ret = start_mandos_communication(address, port, if_index, af);
1429
 
    if(ret < 0){
1430
 
      exitcode = EXIT_FAILURE;
1431
 
    } else {
 
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){
1432
1654
      exitcode = EXIT_SUCCESS;
1433
1655
    }
 
1656
 
1434
1657
    goto end;
1435
1658
  }
1436
1659
  
1460
1683
  if(mc.server == NULL){
1461
1684
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1462
1685
            avahi_strerror(error));
1463
 
    exitcode = EXIT_FAILURE;
 
1686
    exitcode = EX_UNAVAILABLE;
1464
1687
    goto end;
1465
1688
  }
1466
1689
  
1475
1698
  if(sb == NULL){
1476
1699
    fprintf(stderr, "Failed to create service browser: %s\n",
1477
1700
            avahi_strerror(avahi_server_errno(mc.server)));
1478
 
    exitcode = EXIT_FAILURE;
 
1701
    exitcode = EX_UNAVAILABLE;
1479
1702
    goto end;
1480
1703
  }
1481
1704
  
1530
1753
      if(ret == -1){
1531
1754
        perror("ioctl SIOCGIFFLAGS");
1532
1755
      } else if(network.ifr_flags & IFF_UP) {
1533
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1756
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1534
1757
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1535
1758
        if(ret == -1){
1536
 
          perror("ioctl SIOCSIFFLAGS");
 
1759
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1537
1760
        }
1538
1761
      }
1539
1762
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1549
1772
    }
1550
1773
  }
1551
1774
  
1552
 
  /* Removes the temp directory used by GPGME */
 
1775
  /* Removes the GPGME temp directory and all files inside */
1553
1776
  if(tempdir_created){
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
 
        }
 
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];
1574
1783
        char *fullname = NULL;
1575
1784
        ret = asprintf(&fullname, "%s/%s", tempdir,
1576
1785
                       direntry->d_name);
1585
1794
        }
1586
1795
        free(fullname);
1587
1796
      }
1588
 
      closedir(d);
 
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");
1589
1803
    }
1590
1804
    ret = rmdir(tempdir);
1591
1805
    if(ret == -1 and errno != ENOENT){