/mandos/release

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

« back to all changes in this revision

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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
 
33
#ifndef _LARGEFILE_SOURCE
33
34
#define _LARGEFILE_SOURCE
 
35
#endif
 
36
#ifndef _FILE_OFFSET_BITS
34
37
#define _FILE_OFFSET_BITS 64
 
38
#endif
35
39
 
36
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
41
 
38
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf(),
40
 
                                   remove() */
 
43
                                   stdout, ferror(), remove() */
41
44
#include <stdint.h>             /* uint16_t, uint32_t */
42
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
43
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
44
 
                                   srand() */
 
47
                                   srand(), strtof() */
45
48
#include <stdbool.h>            /* bool, false, true */
46
49
#include <string.h>             /* memset(), strcmp(), strlen(),
47
50
                                   strerror(), asprintf(), strcpy() */
56
59
#include <fcntl.h>              /* open() */
57
60
#include <dirent.h>             /* opendir(), struct dirent, readdir()
58
61
                                 */
59
 
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
 
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
 
63
                                   strtoimax() */
60
64
#include <assert.h>             /* assert() */
61
65
#include <errno.h>              /* perror(), errno */
62
66
#include <time.h>               /* nanosleep(), time() */
76
80
                                   argp_parse(), ARGP_KEY_ARG,
77
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
79
 
                                   sigaction(), SIGTERM, sigaction,
80
 
                                   sig_atomic_t */
 
83
                                   sigaction(), SIGTERM, sig_atomic_t,
 
84
                                   raise() */
81
85
 
82
86
#ifdef __linux__
83
87
#include <sys/klog.h>           /* klogctl() */
134
138
} mandos_context;
135
139
 
136
140
/* global context so signal handler can reach it*/
137
 
mandos_context mc;
 
141
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
142
                      .dh_bits = 1024, .priority = "SECURE256"
 
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
138
144
 
139
145
/*
140
146
 * Make additional room in "buffer" for at least BUFFER_SIZE more
158
164
 */
159
165
static bool init_gpgme(const char *seckey,
160
166
                       const char *pubkey, const char *tempdir){
161
 
  int ret;
162
167
  gpgme_error_t rc;
163
168
  gpgme_engine_info_t engine_info;
164
169
  
167
172
   * Helper function to insert pub and seckey to the engine keyring.
168
173
   */
169
174
  bool import_key(const char *filename){
 
175
    int ret;
170
176
    int fd;
171
177
    gpgme_data_t pgp_data;
172
178
    
243
249
    return false;
244
250
  }
245
251
  
246
 
  return true; 
 
252
  return true;
247
253
}
248
254
 
249
255
/* 
303
309
        }
304
310
        gpgme_recipient_t recipient;
305
311
        recipient = result->recipients;
306
 
        if(recipient){
307
 
          while(recipient != NULL){
308
 
            fprintf(stderr, "Public key algorithm: %s\n",
309
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
310
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
311
 
            fprintf(stderr, "Secret key available: %s\n",
312
 
                    recipient->status == GPG_ERR_NO_SECKEY
313
 
                    ? "No" : "Yes");
314
 
            recipient = recipient->next;
315
 
          }
 
312
        while(recipient != NULL){
 
313
          fprintf(stderr, "Public key algorithm: %s\n",
 
314
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
315
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
316
          fprintf(stderr, "Secret key available: %s\n",
 
317
                  recipient->status == GPG_ERR_NO_SECKEY
 
318
                  ? "No" : "Yes");
 
319
          recipient = recipient->next;
316
320
        }
317
321
      }
318
322
    }
510
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
511
515
                      __attribute__((unused)) const char *txt){}
512
516
 
 
517
sig_atomic_t quit_now = 0;
 
518
int signal_received = 0;
 
519
 
513
520
/* Called when a Mandos server is found */
514
521
static int start_mandos_communication(const char *ip, uint16_t port,
515
522
                                      AvahiIfIndex if_index,
516
523
                                      int af){
517
 
  int ret, tcp_sd;
 
524
  int ret, tcp_sd = -1;
518
525
  ssize_t sret;
519
526
  union {
520
527
    struct sockaddr_in in;
524
531
  char *decrypted_buffer;
525
532
  size_t buffer_length = 0;
526
533
  size_t buffer_capacity = 0;
527
 
  ssize_t decrypted_buffer_size;
528
534
  size_t written;
529
535
  int retval = 0;
530
536
  gnutls_session_t session;
531
537
  int pf;                       /* Protocol family */
532
538
  
 
539
  if(quit_now){
 
540
    return -1;
 
541
  }
 
542
  
533
543
  switch(af){
534
544
  case AF_INET6:
535
545
    pf = PF_INET6;
555
565
  tcp_sd = socket(pf, SOCK_STREAM, 0);
556
566
  if(tcp_sd < 0){
557
567
    perror("socket");
558
 
    return -1;
 
568
    retval = -1;
 
569
    goto mandos_end;
 
570
  }
 
571
  
 
572
  if(quit_now){
 
573
    goto mandos_end;
559
574
  }
560
575
  
561
576
  memset(&to, 0, sizeof(to));
562
577
  if(af == AF_INET6){
563
 
    to.in6.sin6_family = (uint16_t)af;
 
578
    to.in6.sin6_family = (sa_family_t)af;
564
579
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
565
580
  } else {                      /* IPv4 */
566
581
    to.in.sin_family = (sa_family_t)af;
568
583
  }
569
584
  if(ret < 0 ){
570
585
    perror("inet_pton");
571
 
    return -1;
 
586
    retval = -1;
 
587
    goto mandos_end;
572
588
  }
573
589
  if(ret == 0){
574
590
    fprintf(stderr, "Bad address: %s\n", ip);
575
 
    return -1;
 
591
    retval = -1;
 
592
    goto mandos_end;
576
593
  }
577
594
  if(af == AF_INET6){
578
595
    to.in6.sin6_port = htons(port); /* Spurious warnings from
585
602
      if(if_index == AVAHI_IF_UNSPEC){
586
603
        fprintf(stderr, "An IPv6 link-local address is incomplete"
587
604
                " without a network interface\n");
588
 
        return -1;
 
605
        retval = -1;
 
606
        goto mandos_end;
589
607
      }
590
608
      /* Set the network interface number as scope */
591
609
      to.in6.sin6_scope_id = (uint32_t)if_index;
596
614
                                     -Wunreachable-code */
597
615
  }
598
616
  
 
617
  if(quit_now){
 
618
    goto mandos_end;
 
619
  }
 
620
  
599
621
  if(debug){
600
622
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
601
623
      char interface[IF_NAMESIZE];
628
650
    }
629
651
  }
630
652
  
 
653
  if(quit_now){
 
654
    goto mandos_end;
 
655
  }
 
656
  
631
657
  if(af == AF_INET6){
632
658
    ret = connect(tcp_sd, &to.in6, sizeof(to));
633
659
  } else {
635
661
  }
636
662
  if(ret < 0){
637
663
    perror("connect");
638
 
    return -1;
 
664
    retval = -1;
 
665
    goto mandos_end;
 
666
  }
 
667
  
 
668
  if(quit_now){
 
669
    goto mandos_end;
639
670
  }
640
671
  
641
672
  const char *out = mandos_protocol_version;
660
691
        break;
661
692
      }
662
693
    }
 
694
  
 
695
    if(quit_now){
 
696
      goto mandos_end;
 
697
    }
663
698
  }
664
699
  
665
700
  if(debug){
666
701
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
667
702
  }
668
703
  
 
704
  if(quit_now){
 
705
    goto mandos_end;
 
706
  }
 
707
  
669
708
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
670
709
  
671
 
  do{
 
710
  if(quit_now){
 
711
    goto mandos_end;
 
712
  }
 
713
  
 
714
  do {
672
715
    ret = gnutls_handshake(session);
 
716
    if(quit_now){
 
717
      goto mandos_end;
 
718
    }
673
719
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
674
720
  
675
721
  if(ret != GNUTLS_E_SUCCESS){
689
735
  }
690
736
  
691
737
  while(true){
 
738
    
 
739
    if(quit_now){
 
740
      goto mandos_end;
 
741
    }
 
742
    
692
743
    buffer_capacity = incbuffer(&buffer, buffer_length,
693
744
                                   buffer_capacity);
694
745
    if(buffer_capacity == 0){
697
748
      goto mandos_end;
698
749
    }
699
750
    
 
751
    if(quit_now){
 
752
      goto mandos_end;
 
753
    }
 
754
    
700
755
    sret = gnutls_record_recv(session, buffer+buffer_length,
701
756
                              BUFFER_SIZE);
702
757
    if(sret == 0){
708
763
      case GNUTLS_E_AGAIN:
709
764
        break;
710
765
      case GNUTLS_E_REHANDSHAKE:
711
 
        do{
 
766
        do {
712
767
          ret = gnutls_handshake(session);
 
768
          
 
769
          if(quit_now){
 
770
            goto mandos_end;
 
771
          }
713
772
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
714
773
        if(ret < 0){
715
774
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
734
793
    fprintf(stderr, "Closing TLS session\n");
735
794
  }
736
795
  
 
796
  if(quit_now){
 
797
    goto mandos_end;
 
798
  }
 
799
  
737
800
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
738
801
  
 
802
  if(quit_now){
 
803
    goto mandos_end;
 
804
  }
 
805
  
739
806
  if(buffer_length > 0){
 
807
    ssize_t decrypted_buffer_size;
740
808
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
741
809
                                               buffer_length,
742
810
                                               &decrypted_buffer);
743
811
    if(decrypted_buffer_size >= 0){
 
812
      
744
813
      written = 0;
745
814
      while(written < (size_t) decrypted_buffer_size){
 
815
        if(quit_now){
 
816
          goto mandos_end;
 
817
        }
 
818
        
746
819
        ret = (int)fwrite(decrypted_buffer + written, 1,
747
820
                          (size_t)decrypted_buffer_size - written,
748
821
                          stdout);
768
841
  
769
842
 mandos_end:
770
843
  free(buffer);
771
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
844
  if(tcp_sd >= 0){
 
845
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
846
  }
772
847
  if(ret == -1){
773
848
    perror("close");
774
849
  }
775
850
  gnutls_deinit(session);
 
851
  if(quit_now){
 
852
    retval = -1;
 
853
  }
776
854
  return retval;
777
855
}
778
856
 
795
873
  /* Called whenever a service has been resolved successfully or
796
874
     timed out */
797
875
  
 
876
  if(quit_now){
 
877
    return;
 
878
  }
 
879
  
798
880
  switch(event){
799
881
  default:
800
882
  case AVAHI_RESOLVER_FAILURE:
837
919
  /* Called whenever a new services becomes available on the LAN or
838
920
     is removed from the LAN */
839
921
  
 
922
  if(quit_now){
 
923
    return;
 
924
  }
 
925
  
840
926
  switch(event){
841
927
  default:
842
928
  case AVAHI_BROWSER_FAILURE:
852
938
       the callback function is called the Avahi server will free the
853
939
       resolver for us. */
854
940
    
855
 
    if(!(avahi_s_service_resolver_new(mc.server, interface,
856
 
                                       protocol, name, type, domain,
857
 
                                       AVAHI_PROTO_INET6, 0,
858
 
                                       resolve_callback, NULL)))
 
941
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
 
942
                                    name, type, domain, protocol, 0,
 
943
                                    resolve_callback, NULL) == NULL)
859
944
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
860
945
              name, avahi_strerror(avahi_server_errno(mc.server)));
861
946
    break;
872
957
  }
873
958
}
874
959
 
875
 
sig_atomic_t quit_now = 0;
876
 
 
877
 
static void handle_sigterm(__attribute__((unused)) int sig){
 
960
/* stop main loop after sigterm has been called */
 
961
static void handle_sigterm(int sig){
878
962
  if(quit_now){
879
963
    return;
880
964
  }
881
965
  quit_now = 1;
 
966
  signal_received = sig;
882
967
  int old_errno = errno;
883
968
  if(mc.simple_poll != NULL){
884
969
    avahi_simple_poll_quit(mc.simple_poll);
891
976
  int error;
892
977
  int ret;
893
978
  intmax_t tmpmax;
894
 
  int numchars;
 
979
  char *tmp;
895
980
  int exitcode = EXIT_SUCCESS;
896
981
  const char *interface = "eth0";
897
982
  struct ifreq network;
898
 
  int sd;
 
983
  int sd = -1;
 
984
  bool take_down_interface = false;
899
985
  uid_t uid;
900
986
  gid_t gid;
901
987
  char *connect_to = NULL;
905
991
  const char *seckey = PATHDIR "/" SECKEY;
906
992
  const char *pubkey = PATHDIR "/" PUBKEY;
907
993
  
908
 
  /* Initialize Mandos context */
909
 
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
910
 
                         .dh_bits = 1024, .priority = "SECURE256"
911
 
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
912
994
  bool gnutls_initialized = false;
913
995
  bool gpgme_initialized = false;
914
 
  double delay = 2.5;
915
 
 
 
996
  float delay = 2.5f;
 
997
  
916
998
  struct sigaction old_sigterm_action;
917
999
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
918
1000
  
972
1054
        pubkey = arg;
973
1055
        break;
974
1056
      case 129:                 /* --dh-bits */
975
 
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
976
 
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
977
 
           or arg[numchars] != '\0'){
 
1057
        errno = 0;
 
1058
        tmpmax = strtoimax(arg, &tmp, 10);
 
1059
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1060
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
978
1061
          fprintf(stderr, "Bad number of DH bits\n");
979
1062
          exit(EXIT_FAILURE);
980
1063
        }
984
1067
        mc.priority = arg;
985
1068
        break;
986
1069
      case 131:                 /* --delay */
987
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
988
 
        if(ret < 1 or arg[numchars] != '\0'){
 
1070
        errno = 0;
 
1071
        delay = strtof(arg, &tmp);
 
1072
        if(errno != 0 or tmp == arg or *tmp != '\0'){
989
1073
          fprintf(stderr, "Bad delay\n");
990
1074
          exit(EXIT_FAILURE);
991
1075
        }
1012
1096
    }
1013
1097
  }
1014
1098
  
 
1099
  if(not debug){
 
1100
    avahi_set_log_function(empty_log);
 
1101
  }
 
1102
  
 
1103
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
 
1104
     from the signal handler */
 
1105
  /* Initialize the pseudo-RNG for Avahi */
 
1106
  srand((unsigned int) time(NULL));
 
1107
  mc.simple_poll = avahi_simple_poll_new();
 
1108
  if(mc.simple_poll == NULL){
 
1109
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1110
    exitcode = EXIT_FAILURE;
 
1111
    goto end;
 
1112
  }
 
1113
  
 
1114
  sigemptyset(&sigterm_action.sa_mask);
 
1115
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1116
  if(ret == -1){
 
1117
    perror("sigaddset");
 
1118
    exitcode = EXIT_FAILURE;
 
1119
    goto end;
 
1120
  }
 
1121
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1122
  if(ret == -1){
 
1123
    perror("sigaddset");
 
1124
    exitcode = EXIT_FAILURE;
 
1125
    goto end;
 
1126
  }
 
1127
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1128
  if(ret == -1){
 
1129
    perror("sigaddset");
 
1130
    exitcode = EXIT_FAILURE;
 
1131
    goto end;
 
1132
  }
 
1133
  /* Need to check if the handler is SIG_IGN before handling:
 
1134
     | [[info:libc:Initial Signal Actions]] |
 
1135
     | [[info:libc:Basic Signal Handling]]  |
 
1136
  */
 
1137
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
 
1138
  if(ret == -1){
 
1139
    perror("sigaction");
 
1140
    return EXIT_FAILURE;
 
1141
  }
 
1142
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1143
    ret = sigaction(SIGINT, &sigterm_action, NULL);
 
1144
    if(ret == -1){
 
1145
      perror("sigaction");
 
1146
      exitcode = EXIT_FAILURE;
 
1147
      goto end;
 
1148
    }
 
1149
  }
 
1150
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
 
1151
  if(ret == -1){
 
1152
    perror("sigaction");
 
1153
    return EXIT_FAILURE;
 
1154
  }
 
1155
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1156
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1157
    if(ret == -1){
 
1158
      perror("sigaction");
 
1159
      exitcode = EXIT_FAILURE;
 
1160
      goto end;
 
1161
    }
 
1162
  }
 
1163
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
 
1164
  if(ret == -1){
 
1165
    perror("sigaction");
 
1166
    return EXIT_FAILURE;
 
1167
  }
 
1168
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1169
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1170
    if(ret == -1){
 
1171
      perror("sigaction");
 
1172
      exitcode = EXIT_FAILURE;
 
1173
      goto end;
 
1174
    }
 
1175
  }
 
1176
  
1015
1177
  /* If the interface is down, bring it up */
1016
1178
  if(interface[0] != '\0'){
 
1179
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1180
    if(if_index == 0){
 
1181
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1182
      exitcode = EXIT_FAILURE;
 
1183
      goto end;
 
1184
    }
 
1185
    
 
1186
    if(quit_now){
 
1187
      goto end;
 
1188
    }
 
1189
    
1017
1190
#ifdef __linux__
1018
1191
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1019
1192
       messages to mess up the prompt */
1056
1229
    }
1057
1230
    if((network.ifr_flags & IFF_UP) == 0){
1058
1231
      network.ifr_flags |= IFF_UP;
 
1232
      take_down_interface = true;
1059
1233
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1060
1234
      if(ret == -1){
 
1235
        take_down_interface = false;
1061
1236
        perror("ioctl SIOCSIFFLAGS");
1062
1237
        exitcode = EXIT_FAILURE;
1063
1238
#ifdef __linux__
1085
1260
        perror("nanosleep");
1086
1261
      }
1087
1262
    }
1088
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1089
 
    if(ret == -1){
1090
 
      perror("close");
 
1263
    if(not take_down_interface){
 
1264
      /* We won't need the socket anymore */
 
1265
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1266
      if(ret == -1){
 
1267
        perror("close");
 
1268
      }
1091
1269
    }
1092
1270
#ifdef __linux__
1093
1271
    if(restore_loglevel){
1100
1278
#endif  /* __linux__ */
1101
1279
  }
1102
1280
  
 
1281
  if(quit_now){
 
1282
    goto end;
 
1283
  }
 
1284
  
1103
1285
  uid = getuid();
1104
1286
  gid = getgid();
1105
1287
  
1114
1296
    perror("setuid");
1115
1297
  }
1116
1298
  
 
1299
  if(quit_now){
 
1300
    goto end;
 
1301
  }
 
1302
  
1117
1303
  ret = init_gnutls_global(pubkey, seckey);
1118
1304
  if(ret == -1){
1119
1305
    fprintf(stderr, "init_gnutls_global failed\n");
1123
1309
    gnutls_initialized = true;
1124
1310
  }
1125
1311
  
 
1312
  if(quit_now){
 
1313
    goto end;
 
1314
  }
 
1315
  
 
1316
  tempdir_created = true;
1126
1317
  if(mkdtemp(tempdir) == NULL){
 
1318
    tempdir_created = false;
1127
1319
    perror("mkdtemp");
1128
1320
    goto end;
1129
1321
  }
1130
 
  tempdir_created = true;
 
1322
  
 
1323
  if(quit_now){
 
1324
    goto end;
 
1325
  }
1131
1326
  
1132
1327
  if(not init_gpgme(pubkey, seckey, tempdir)){
1133
1328
    fprintf(stderr, "init_gpgme failed\n");
1137
1332
    gpgme_initialized = true;
1138
1333
  }
1139
1334
  
1140
 
  if(interface[0] != '\0'){
1141
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1142
 
    if(if_index == 0){
1143
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1144
 
      exitcode = EXIT_FAILURE;
1145
 
      goto end;
1146
 
    }
 
1335
  if(quit_now){
 
1336
    goto end;
1147
1337
  }
1148
1338
  
1149
1339
  if(connect_to != NULL){
1155
1345
      exitcode = EXIT_FAILURE;
1156
1346
      goto end;
1157
1347
    }
 
1348
    
 
1349
    if(quit_now){
 
1350
      goto end;
 
1351
    }
 
1352
    
1158
1353
    uint16_t port;
1159
 
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1160
 
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
1161
 
       or address[numchars+1] != '\0'){
 
1354
    errno = 0;
 
1355
    tmpmax = strtoimax(address+1, &tmp, 10);
 
1356
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
 
1357
       or tmpmax != (uint16_t)tmpmax){
1162
1358
      fprintf(stderr, "Bad port number\n");
1163
1359
      exitcode = EXIT_FAILURE;
1164
1360
      goto end;
1165
1361
    }
 
1362
  
 
1363
    if(quit_now){
 
1364
      goto end;
 
1365
    }
 
1366
    
1166
1367
    port = (uint16_t)tmpmax;
1167
1368
    *address = '\0';
1168
1369
    address = connect_to;
1173
1374
    } else {
1174
1375
      af = AF_INET;
1175
1376
    }
 
1377
    
 
1378
    if(quit_now){
 
1379
      goto end;
 
1380
    }
 
1381
    
1176
1382
    ret = start_mandos_communication(address, port, if_index, af);
1177
1383
    if(ret < 0){
1178
1384
      exitcode = EXIT_FAILURE;
1182
1388
    goto end;
1183
1389
  }
1184
1390
  
1185
 
  if(not debug){
1186
 
    avahi_set_log_function(empty_log);
1187
 
  }
1188
 
  
1189
 
  /* Initialize the pseudo-RNG for Avahi */
1190
 
  srand((unsigned int) time(NULL));
1191
 
  
1192
 
  /* Allocate main Avahi loop object */
1193
 
  mc.simple_poll = avahi_simple_poll_new();
1194
 
  if(mc.simple_poll == NULL){
1195
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1196
 
    exitcode = EXIT_FAILURE;
 
1391
  if(quit_now){
1197
1392
    goto end;
1198
1393
  }
1199
1394
  
1223
1418
    goto end;
1224
1419
  }
1225
1420
  
 
1421
  if(quit_now){
 
1422
    goto end;
 
1423
  }
 
1424
  
1226
1425
  /* Create the Avahi service browser */
1227
1426
  sb = avahi_s_service_browser_new(mc.server, if_index,
1228
 
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1427
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1229
1428
                                   NULL, 0, browse_callback, NULL);
1230
1429
  if(sb == NULL){
1231
1430
    fprintf(stderr, "Failed to create service browser: %s\n",
1234
1433
    goto end;
1235
1434
  }
1236
1435
  
1237
 
  sigemptyset(&sigterm_action.sa_mask);
1238
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1239
 
  if(ret == -1){
1240
 
    perror("sigaddset");
1241
 
    exitcode = EXIT_FAILURE;
1242
 
    goto end;
1243
 
  }
1244
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1245
 
  if(ret == -1){
1246
 
    perror("sigaddset");
1247
 
    exitcode = EXIT_FAILURE;
1248
 
    goto end;
1249
 
  }
1250
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1251
 
  if(ret == -1){
1252
 
    perror("sigaddset");
1253
 
    exitcode = EXIT_FAILURE;
1254
 
    goto end;
1255
 
  }
1256
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1257
 
  if(ret == -1){
1258
 
    perror("sigaction");
1259
 
    exitcode = EXIT_FAILURE;
1260
 
    goto end;
1261
 
  }  
 
1436
  if(quit_now){
 
1437
    goto end;
 
1438
  }
1262
1439
  
1263
1440
  /* Run the main loop */
1264
1441
  
1294
1471
    gpgme_release(mc.ctx);
1295
1472
  }
1296
1473
  
 
1474
  /* Take down the network interface */
 
1475
  if(take_down_interface){
 
1476
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1477
    if(ret == -1){
 
1478
      perror("ioctl SIOCGIFFLAGS");
 
1479
    } else if(network.ifr_flags & IFF_UP) {
 
1480
      network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1481
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1482
      if(ret == -1){
 
1483
        perror("ioctl SIOCSIFFLAGS");
 
1484
      }
 
1485
    }
 
1486
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1487
    if(ret == -1){
 
1488
      perror("close");
 
1489
    }
 
1490
  }
 
1491
  
1297
1492
  /* Removes the temp directory used by GPGME */
1298
1493
  if(tempdir_created){
1299
1494
    DIR *d;
1338
1533
    }
1339
1534
  }
1340
1535
  
 
1536
  if(quit_now){
 
1537
    sigemptyset(&old_sigterm_action.sa_mask);
 
1538
    old_sigterm_action.sa_handler = SIG_DFL;
 
1539
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1540
    if(ret == -1){
 
1541
      perror("sigaction");
 
1542
    }
 
1543
    raise(signal_received);
 
1544
  }
 
1545
  
1341
1546
  return exitcode;
1342
1547
}