/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2009-10-26 21:16:16 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091026211616-38e9wynf4428ip2l
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 */
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 {
556
553
  gnutls_session_t session;
557
554
  int pf;                       /* Protocol family */
558
555
  
559
 
  errno = 0;
560
 
  
561
556
  if(quit_now){
562
 
    errno = EINTR;
563
557
    return -1;
564
558
  }
565
559
  
572
566
    break;
573
567
  default:
574
568
    fprintf(stderr, "Bad address family: %d\n", af);
575
 
    errno = EINVAL;
576
569
    return -1;
577
570
  }
578
571
  
588
581
  
589
582
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
583
  if(tcp_sd < 0){
591
 
    int e = errno;
592
584
    perror("socket");
593
 
    errno = e;
594
585
    goto mandos_end;
595
586
  }
596
587
  
597
588
  if(quit_now){
598
 
    errno = EINTR;
599
589
    goto mandos_end;
600
590
  }
601
591
  
608
598
    ret = inet_pton(af, ip, &to.in.sin_addr);
609
599
  }
610
600
  if(ret < 0 ){
611
 
    int e = errno;
612
601
    perror("inet_pton");
613
 
    errno = e;
614
602
    goto mandos_end;
615
603
  }
616
604
  if(ret == 0){
617
 
    int e = errno;
618
605
    fprintf(stderr, "Bad address: %s\n", ip);
619
 
    errno = e;
620
606
    goto mandos_end;
621
607
  }
622
608
  if(af == AF_INET6){
630
616
      if(if_index == AVAHI_IF_UNSPEC){
631
617
        fprintf(stderr, "An IPv6 link-local address is incomplete"
632
618
                " without a network interface\n");
633
 
        errno = EINVAL;
634
619
        goto mandos_end;
635
620
      }
636
621
      /* Set the network interface number as scope */
643
628
  }
644
629
  
645
630
  if(quit_now){
646
 
    errno = EINTR;
647
631
    goto mandos_end;
648
632
  }
649
633
  
680
664
  }
681
665
  
682
666
  if(quit_now){
683
 
    errno = EINTR;
684
667
    goto mandos_end;
685
668
  }
686
669
  
690
673
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
674
  }
692
675
  if(ret < 0){
693
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
 
      int e = errno;
695
 
      perror("connect");
696
 
      errno = e;
697
 
    }
 
676
    perror("connect");
698
677
    goto mandos_end;
699
678
  }
700
679
  
701
680
  if(quit_now){
702
 
    errno = EINTR;
703
681
    goto mandos_end;
704
682
  }
705
683
  
710
688
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
711
689
                                   out_size - written));
712
690
    if(ret == -1){
713
 
      int e = errno;
714
691
      perror("write");
715
 
      errno = e;
716
692
      goto mandos_end;
717
693
    }
718
694
    written += (size_t)ret;
728
704
    }
729
705
  
730
706
    if(quit_now){
731
 
      errno = EINTR;
732
707
      goto mandos_end;
733
708
    }
734
709
  }
738
713
  }
739
714
  
740
715
  if(quit_now){
741
 
    errno = EINTR;
742
716
    goto mandos_end;
743
717
  }
744
 
 
745
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
718
  
746
719
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
747
720
  
748
721
  if(quit_now){
749
 
    errno = EINTR;
750
722
    goto mandos_end;
751
723
  }
752
724
  
753
725
  do {
754
726
    ret = gnutls_handshake(session);
755
727
    if(quit_now){
756
 
      errno = EINTR;
757
728
      goto mandos_end;
758
729
    }
759
730
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
763
734
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
764
735
      gnutls_perror(ret);
765
736
    }
766
 
    errno = EPROTO;
767
737
    goto mandos_end;
768
738
  }
769
739
  
777
747
  while(true){
778
748
    
779
749
    if(quit_now){
780
 
      errno = EINTR;
781
750
      goto mandos_end;
782
751
    }
783
752
    
784
753
    buffer_capacity = incbuffer(&buffer, buffer_length,
785
754
                                   buffer_capacity);
786
755
    if(buffer_capacity == 0){
787
 
      int e = errno;
788
756
      perror("incbuffer");
789
 
      errno = e;
790
757
      goto mandos_end;
791
758
    }
792
759
    
793
760
    if(quit_now){
794
 
      errno = EINTR;
795
761
      goto mandos_end;
796
762
    }
797
763
    
810
776
          ret = gnutls_handshake(session);
811
777
          
812
778
          if(quit_now){
813
 
            errno = EINTR;
814
779
            goto mandos_end;
815
780
          }
816
781
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
817
782
        if(ret < 0){
818
783
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
819
784
          gnutls_perror(ret);
820
 
          errno = EPROTO;
821
785
          goto mandos_end;
822
786
        }
823
787
        break;
825
789
        fprintf(stderr, "Unknown error while reading data from"
826
790
                " encrypted session with Mandos server\n");
827
791
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
828
 
        errno = EIO;
829
792
        goto mandos_end;
830
793
      }
831
794
    } else {
838
801
  }
839
802
  
840
803
  if(quit_now){
841
 
    errno = EINTR;
842
804
    goto mandos_end;
843
805
  }
844
806
  
845
807
  do {
846
808
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
847
809
    if(quit_now){
848
 
      errno = EINTR;
849
810
      goto mandos_end;
850
811
    }
851
812
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
860
821
      written = 0;
861
822
      while(written < (size_t) decrypted_buffer_size){
862
823
        if(quit_now){
863
 
          errno = EINTR;
864
824
          goto mandos_end;
865
825
        }
866
826
        
868
828
                          (size_t)decrypted_buffer_size - written,
869
829
                          stdout);
870
830
        if(ret == 0 and ferror(stdout)){
871
 
          int e = errno;
872
831
          if(debug){
873
832
            fprintf(stderr, "Error writing encrypted data: %s\n",
874
833
                    strerror(errno));
875
834
          }
876
 
          errno = e;
877
835
          goto mandos_end;
878
836
        }
879
837
        written += (size_t)ret;
885
843
  /* Shutdown procedure */
886
844
  
887
845
 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;
 
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;
907
857
  }
908
858
  return retval;
909
859
}
1025
975
  errno = old_errno;
1026
976
}
1027
977
 
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
978
int main(int argc, char *argv[]){
1145
979
  AvahiSServiceBrowser *sb = NULL;
1146
980
  int error;
1148
982
  intmax_t tmpmax;
1149
983
  char *tmp;
1150
984
  int exitcode = EXIT_SUCCESS;
1151
 
  const char *interface = "";
 
985
  const char *interface = "eth0";
1152
986
  struct ifreq network;
1153
987
  int sd = -1;
1154
988
  bool take_down_interface = false;
1155
989
  uid_t uid;
1156
990
  gid_t gid;
 
991
  char *connect_to = NULL;
1157
992
  char tempdir[] = "/tmp/mandosXXXXXX";
1158
993
  bool tempdir_created = false;
1159
994
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1316
1151
  if(not debug){
1317
1152
    avahi_set_log_function(empty_log);
1318
1153
  }
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
1154
  
1345
1155
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1346
1156
     from the signal handler */
1349
1159
  mc.simple_poll = avahi_simple_poll_new();
1350
1160
  if(mc.simple_poll == NULL){
1351
1161
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1352
 
    exitcode = EX_UNAVAILABLE;
 
1162
    exitcode = EXIT_FAILURE;
1353
1163
    goto end;
1354
1164
  }
1355
1165
  
1357
1167
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1358
1168
  if(ret == -1){
1359
1169
    perror("sigaddset");
1360
 
    exitcode = EX_OSERR;
 
1170
    exitcode = EXIT_FAILURE;
1361
1171
    goto end;
1362
1172
  }
1363
1173
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1364
1174
  if(ret == -1){
1365
1175
    perror("sigaddset");
1366
 
    exitcode = EX_OSERR;
 
1176
    exitcode = EXIT_FAILURE;
1367
1177
    goto end;
1368
1178
  }
1369
1179
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1370
1180
  if(ret == -1){
1371
1181
    perror("sigaddset");
1372
 
    exitcode = EX_OSERR;
 
1182
    exitcode = EXIT_FAILURE;
1373
1183
    goto end;
1374
1184
  }
1375
1185
  /* Need to check if the handler is SIG_IGN before handling:
1379
1189
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1380
1190
  if(ret == -1){
1381
1191
    perror("sigaction");
1382
 
    return EX_OSERR;
 
1192
    return EXIT_FAILURE;
1383
1193
  }
1384
1194
  if(old_sigterm_action.sa_handler != SIG_IGN){
1385
1195
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1386
1196
    if(ret == -1){
1387
1197
      perror("sigaction");
1388
 
      exitcode = EX_OSERR;
 
1198
      exitcode = EXIT_FAILURE;
1389
1199
      goto end;
1390
1200
    }
1391
1201
  }
1392
1202
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1393
1203
  if(ret == -1){
1394
1204
    perror("sigaction");
1395
 
    return EX_OSERR;
 
1205
    return EXIT_FAILURE;
1396
1206
  }
1397
1207
  if(old_sigterm_action.sa_handler != SIG_IGN){
1398
1208
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1399
1209
    if(ret == -1){
1400
1210
      perror("sigaction");
1401
 
      exitcode = EX_OSERR;
 
1211
      exitcode = EXIT_FAILURE;
1402
1212
      goto end;
1403
1213
    }
1404
1214
  }
1405
1215
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1406
1216
  if(ret == -1){
1407
1217
    perror("sigaction");
1408
 
    return EX_OSERR;
 
1218
    return EXIT_FAILURE;
1409
1219
  }
1410
1220
  if(old_sigterm_action.sa_handler != SIG_IGN){
1411
1221
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1412
1222
    if(ret == -1){
1413
1223
      perror("sigaction");
1414
 
      exitcode = EX_OSERR;
 
1224
      exitcode = EXIT_FAILURE;
1415
1225
      goto end;
1416
1226
    }
1417
1227
  }
1418
1228
  
1419
1229
  /* If the interface is down, bring it up */
1420
 
  if(strcmp(interface, "none") != 0){
 
1230
  if(interface[0] != '\0'){
1421
1231
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1422
1232
    if(if_index == 0){
1423
1233
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1424
 
      exitcode = EX_UNAVAILABLE;
 
1234
      exitcode = EXIT_FAILURE;
1425
1235
      goto end;
1426
1236
    }
1427
1237
    
1450
1260
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1451
1261
    if(sd < 0){
1452
1262
      perror("socket");
1453
 
      exitcode = EX_OSERR;
 
1263
      exitcode = EXIT_FAILURE;
1454
1264
#ifdef __linux__
1455
1265
      if(restore_loglevel){
1456
1266
        ret = klogctl(7, NULL, 0);
1479
1289
        }
1480
1290
      }
1481
1291
#endif  /* __linux__ */
1482
 
      exitcode = EX_OSERR;
 
1292
      exitcode = EXIT_FAILURE;
1483
1293
      /* Lower privileges */
1484
1294
      errno = 0;
1485
1295
      ret = seteuid(uid);
1494
1304
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1495
1305
      if(ret == -1){
1496
1306
        take_down_interface = false;
1497
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1498
 
        exitcode = EX_OSERR;
 
1307
        perror("ioctl SIOCSIFFLAGS");
 
1308
        exitcode = EXIT_FAILURE;
1499
1309
#ifdef __linux__
1500
1310
        if(restore_loglevel){
1501
1311
          ret = klogctl(7, NULL, 0);
1567
1377
  ret = init_gnutls_global(pubkey, seckey);
1568
1378
  if(ret == -1){
1569
1379
    fprintf(stderr, "init_gnutls_global failed\n");
1570
 
    exitcode = EX_UNAVAILABLE;
 
1380
    exitcode = EXIT_FAILURE;
1571
1381
    goto end;
1572
1382
  } else {
1573
1383
    gnutls_initialized = true;
1577
1387
    goto end;
1578
1388
  }
1579
1389
  
 
1390
  tempdir_created = true;
1580
1391
  if(mkdtemp(tempdir) == NULL){
 
1392
    tempdir_created = false;
1581
1393
    perror("mkdtemp");
1582
1394
    goto end;
1583
1395
  }
1584
 
  tempdir_created = true;
1585
1396
  
1586
1397
  if(quit_now){
1587
1398
    goto end;
1589
1400
  
1590
1401
  if(not init_gpgme(pubkey, seckey, tempdir)){
1591
1402
    fprintf(stderr, "init_gpgme failed\n");
1592
 
    exitcode = EX_UNAVAILABLE;
 
1403
    exitcode = EXIT_FAILURE;
1593
1404
    goto end;
1594
1405
  } else {
1595
1406
    gpgme_initialized = true;
1605
1416
    char *address = strrchr(connect_to, ':');
1606
1417
    if(address == NULL){
1607
1418
      fprintf(stderr, "No colon in address\n");
1608
 
      exitcode = EX_USAGE;
 
1419
      exitcode = EXIT_FAILURE;
1609
1420
      goto end;
1610
1421
    }
1611
1422
    
1619
1430
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1620
1431
       or tmpmax != (uint16_t)tmpmax){
1621
1432
      fprintf(stderr, "Bad port number\n");
1622
 
      exitcode = EX_USAGE;
 
1433
      exitcode = EXIT_FAILURE;
1623
1434
      goto end;
1624
1435
    }
1625
1436
  
1641
1452
    if(quit_now){
1642
1453
      goto end;
1643
1454
    }
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){
 
1455
    
 
1456
    ret = start_mandos_communication(address, port, if_index, af);
 
1457
    if(ret < 0){
 
1458
      exitcode = EXIT_FAILURE;
 
1459
    } else {
1654
1460
      exitcode = EXIT_SUCCESS;
1655
1461
    }
1656
 
 
1657
1462
    goto end;
1658
1463
  }
1659
1464
  
1683
1488
  if(mc.server == NULL){
1684
1489
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1685
1490
            avahi_strerror(error));
1686
 
    exitcode = EX_UNAVAILABLE;
 
1491
    exitcode = EXIT_FAILURE;
1687
1492
    goto end;
1688
1493
  }
1689
1494
  
1698
1503
  if(sb == NULL){
1699
1504
    fprintf(stderr, "Failed to create service browser: %s\n",
1700
1505
            avahi_strerror(avahi_server_errno(mc.server)));
1701
 
    exitcode = EX_UNAVAILABLE;
 
1506
    exitcode = EXIT_FAILURE;
1702
1507
    goto end;
1703
1508
  }
1704
1509
  
1756
1561
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1757
1562
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1758
1563
        if(ret == -1){
1759
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1564
          perror("ioctl SIOCSIFFLAGS");
1760
1565
        }
1761
1566
      }
1762
1567
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1772
1577
    }
1773
1578
  }
1774
1579
  
1775
 
  /* Removes the GPGME temp directory and all files inside */
 
1580
  /* Removes the temp directory used by GPGME */
1776
1581
  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];
 
1582
    DIR *d;
 
1583
    struct dirent *direntry;
 
1584
    d = opendir(tempdir);
 
1585
    if(d == NULL){
 
1586
      if(errno != ENOENT){
 
1587
        perror("opendir");
 
1588
      }
 
1589
    } else {
 
1590
      while(true){
 
1591
        direntry = readdir(d);
 
1592
        if(direntry == NULL){
 
1593
          break;
 
1594
        }
 
1595
        /* Skip "." and ".." */
 
1596
        if(direntry->d_name[0] == '.'
 
1597
           and (direntry->d_name[1] == '\0'
 
1598
                or (direntry->d_name[1] == '.'
 
1599
                    and direntry->d_name[2] == '\0'))){
 
1600
          continue;
 
1601
        }
1783
1602
        char *fullname = NULL;
1784
1603
        ret = asprintf(&fullname, "%s/%s", tempdir,
1785
1604
                       direntry->d_name);
1794
1613
        }
1795
1614
        free(fullname);
1796
1615
      }
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");
 
1616
      closedir(d);
1803
1617
    }
1804
1618
    ret = rmdir(tempdir);
1805
1619
    if(ret == -1 and errno != ENOENT){