/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

* plugins-runner.c (main): Bug fix: Close config file.

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
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;
530
537
  gnutls_session_t session;
531
538
  int pf;                       /* Protocol family */
532
539
  
 
540
  if(quit_now){
 
541
    return -1;
 
542
  }
 
543
  
533
544
  switch(af){
534
545
  case AF_INET6:
535
546
    pf = PF_INET6;
555
566
  tcp_sd = socket(pf, SOCK_STREAM, 0);
556
567
  if(tcp_sd < 0){
557
568
    perror("socket");
558
 
    return -1;
 
569
    retval = -1;
 
570
    goto mandos_end;
 
571
  }
 
572
  
 
573
  if(quit_now){
 
574
    goto mandos_end;
559
575
  }
560
576
  
561
577
  memset(&to, 0, sizeof(to));
562
578
  if(af == AF_INET6){
563
 
    to.in6.sin6_family = (uint16_t)af;
 
579
    to.in6.sin6_family = (sa_family_t)af;
564
580
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
565
581
  } else {                      /* IPv4 */
566
582
    to.in.sin_family = (sa_family_t)af;
568
584
  }
569
585
  if(ret < 0 ){
570
586
    perror("inet_pton");
571
 
    return -1;
 
587
    retval = -1;
 
588
    goto mandos_end;
572
589
  }
573
590
  if(ret == 0){
574
591
    fprintf(stderr, "Bad address: %s\n", ip);
575
 
    return -1;
 
592
    retval = -1;
 
593
    goto mandos_end;
576
594
  }
577
595
  if(af == AF_INET6){
578
596
    to.in6.sin6_port = htons(port); /* Spurious warnings from
585
603
      if(if_index == AVAHI_IF_UNSPEC){
586
604
        fprintf(stderr, "An IPv6 link-local address is incomplete"
587
605
                " without a network interface\n");
588
 
        return -1;
 
606
        retval = -1;
 
607
        goto mandos_end;
589
608
      }
590
609
      /* Set the network interface number as scope */
591
610
      to.in6.sin6_scope_id = (uint32_t)if_index;
596
615
                                     -Wunreachable-code */
597
616
  }
598
617
  
 
618
  if(quit_now){
 
619
    goto mandos_end;
 
620
  }
 
621
  
599
622
  if(debug){
600
623
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
601
624
      char interface[IF_NAMESIZE];
628
651
    }
629
652
  }
630
653
  
 
654
  if(quit_now){
 
655
    goto mandos_end;
 
656
  }
 
657
  
631
658
  if(af == AF_INET6){
632
659
    ret = connect(tcp_sd, &to.in6, sizeof(to));
633
660
  } else {
635
662
  }
636
663
  if(ret < 0){
637
664
    perror("connect");
638
 
    return -1;
 
665
    retval = -1;
 
666
    goto mandos_end;
 
667
  }
 
668
  
 
669
  if(quit_now){
 
670
    goto mandos_end;
639
671
  }
640
672
  
641
673
  const char *out = mandos_protocol_version;
660
692
        break;
661
693
      }
662
694
    }
 
695
  
 
696
    if(quit_now){
 
697
      goto mandos_end;
 
698
    }
663
699
  }
664
700
  
665
701
  if(debug){
666
702
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
667
703
  }
668
704
  
 
705
  if(quit_now){
 
706
    goto mandos_end;
 
707
  }
 
708
  
669
709
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
670
710
  
 
711
  if(quit_now){
 
712
    goto mandos_end;
 
713
  }
 
714
  
671
715
  do{
672
716
    ret = gnutls_handshake(session);
 
717
    if(quit_now){
 
718
      goto mandos_end;
 
719
    }
673
720
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
674
721
  
675
722
  if(ret != GNUTLS_E_SUCCESS){
689
736
  }
690
737
  
691
738
  while(true){
 
739
    
 
740
    if(quit_now){
 
741
      goto mandos_end;
 
742
    }
 
743
    
692
744
    buffer_capacity = incbuffer(&buffer, buffer_length,
693
745
                                   buffer_capacity);
694
746
    if(buffer_capacity == 0){
697
749
      goto mandos_end;
698
750
    }
699
751
    
 
752
    if(quit_now){
 
753
      goto mandos_end;
 
754
    }
 
755
    
700
756
    sret = gnutls_record_recv(session, buffer+buffer_length,
701
757
                              BUFFER_SIZE);
702
758
    if(sret == 0){
710
766
      case GNUTLS_E_REHANDSHAKE:
711
767
        do{
712
768
          ret = gnutls_handshake(session);
 
769
          
 
770
          if(quit_now){
 
771
            goto mandos_end;
 
772
          }
713
773
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
714
774
        if(ret < 0){
715
775
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
734
794
    fprintf(stderr, "Closing TLS session\n");
735
795
  }
736
796
  
 
797
  if(quit_now){
 
798
    goto mandos_end;
 
799
  }
 
800
  
737
801
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
738
802
  
 
803
  if(quit_now){
 
804
    goto mandos_end;
 
805
  }
 
806
  
739
807
  if(buffer_length > 0){
740
808
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
741
809
                                               buffer_length,
743
811
    if(decrypted_buffer_size >= 0){
744
812
      written = 0;
745
813
      while(written < (size_t) decrypted_buffer_size){
 
814
        if(quit_now){
 
815
          goto mandos_end;
 
816
        }
 
817
        
746
818
        ret = (int)fwrite(decrypted_buffer + written, 1,
747
819
                          (size_t)decrypted_buffer_size - written,
748
820
                          stdout);
768
840
  
769
841
 mandos_end:
770
842
  free(buffer);
771
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
843
  if(tcp_sd >= 0){
 
844
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
845
  }
772
846
  if(ret == -1){
773
847
    perror("close");
774
848
  }
775
849
  gnutls_deinit(session);
 
850
  if(quit_now){
 
851
    retval = -1;
 
852
  }
776
853
  return retval;
777
854
}
778
855
 
795
872
  /* Called whenever a service has been resolved successfully or
796
873
     timed out */
797
874
  
 
875
  if(quit_now){
 
876
    return;
 
877
  }
 
878
  
798
879
  switch(event){
799
880
  default:
800
881
  case AVAHI_RESOLVER_FAILURE:
837
918
  /* Called whenever a new services becomes available on the LAN or
838
919
     is removed from the LAN */
839
920
  
 
921
  if(quit_now){
 
922
    return;
 
923
  }
 
924
  
840
925
  switch(event){
841
926
  default:
842
927
  case AVAHI_BROWSER_FAILURE:
852
937
       the callback function is called the Avahi server will free the
853
938
       resolver for us. */
854
939
    
855
 
    if(!(avahi_s_service_resolver_new(mc.server, interface,
856
 
                                       protocol, name, type, domain,
857
 
                                       AVAHI_PROTO_INET6, 0,
858
 
                                       resolve_callback, NULL)))
 
940
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
 
941
                                    name, type, domain, protocol, 0,
 
942
                                    resolve_callback, NULL) == NULL)
859
943
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
860
944
              name, avahi_strerror(avahi_server_errno(mc.server)));
861
945
    break;
872
956
  }
873
957
}
874
958
 
875
 
sig_atomic_t quit_now = 0;
876
 
 
877
 
static void handle_sigterm(__attribute__((unused)) int sig){
 
959
/* stop main loop after sigterm has been called */
 
960
static void handle_sigterm(int sig){
878
961
  if(quit_now){
879
962
    return;
880
963
  }
881
964
  quit_now = 1;
 
965
  signal_received = sig;
882
966
  int old_errno = errno;
883
967
  if(mc.simple_poll != NULL){
884
968
    avahi_simple_poll_quit(mc.simple_poll);
891
975
  int error;
892
976
  int ret;
893
977
  intmax_t tmpmax;
894
 
  int numchars;
 
978
  char *tmp;
895
979
  int exitcode = EXIT_SUCCESS;
896
980
  const char *interface = "eth0";
897
981
  struct ifreq network;
898
 
  int sd;
 
982
  int sd = -1;
 
983
  bool take_down_interface = false;
899
984
  uid_t uid;
900
985
  gid_t gid;
901
986
  char *connect_to = NULL;
905
990
  const char *seckey = PATHDIR "/" SECKEY;
906
991
  const char *pubkey = PATHDIR "/" PUBKEY;
907
992
  
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
993
  bool gnutls_initialized = false;
913
994
  bool gpgme_initialized = false;
914
 
  double delay = 2.5;
915
 
 
 
995
  float delay = 2.5f;
 
996
  
916
997
  struct sigaction old_sigterm_action;
917
998
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
918
999
  
972
1053
        pubkey = arg;
973
1054
        break;
974
1055
      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'){
 
1056
        errno = 0;
 
1057
        tmpmax = strtoimax(arg, &tmp, 10);
 
1058
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1059
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
978
1060
          fprintf(stderr, "Bad number of DH bits\n");
979
1061
          exit(EXIT_FAILURE);
980
1062
        }
984
1066
        mc.priority = arg;
985
1067
        break;
986
1068
      case 131:                 /* --delay */
987
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
988
 
        if(ret < 1 or arg[numchars] != '\0'){
 
1069
        errno = 0;
 
1070
        delay = strtof(arg, &tmp);
 
1071
        if(errno != 0 or tmp == arg or *tmp != '\0'){
989
1072
          fprintf(stderr, "Bad delay\n");
990
1073
          exit(EXIT_FAILURE);
991
1074
        }
1012
1095
    }
1013
1096
  }
1014
1097
  
 
1098
  if(not debug){
 
1099
    avahi_set_log_function(empty_log);
 
1100
  }
 
1101
  
 
1102
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
 
1103
     from the signal handler */
 
1104
  /* Initialize the pseudo-RNG for Avahi */
 
1105
  srand((unsigned int) time(NULL));
 
1106
  mc.simple_poll = avahi_simple_poll_new();
 
1107
  if(mc.simple_poll == NULL){
 
1108
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1109
    exitcode = EXIT_FAILURE;
 
1110
    goto end;
 
1111
  }
 
1112
  
 
1113
  sigemptyset(&sigterm_action.sa_mask);
 
1114
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1115
  if(ret == -1){
 
1116
    perror("sigaddset");
 
1117
    exitcode = EXIT_FAILURE;
 
1118
    goto end;
 
1119
  }
 
1120
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1121
  if(ret == -1){
 
1122
    perror("sigaddset");
 
1123
    exitcode = EXIT_FAILURE;
 
1124
    goto end;
 
1125
  }
 
1126
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1127
  if(ret == -1){
 
1128
    perror("sigaddset");
 
1129
    exitcode = EXIT_FAILURE;
 
1130
    goto end;
 
1131
  }
 
1132
  /* Need to check if the handler is SIG_IGN before handling:
 
1133
     | [[info:libc:Initial Signal Actions]] |
 
1134
     | [[info:libc:Basic Signal Handling]]  |
 
1135
  */
 
1136
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
 
1137
  if(ret == -1){
 
1138
    perror("sigaction");
 
1139
    return EXIT_FAILURE;
 
1140
  }
 
1141
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1142
    ret = sigaction(SIGINT, &sigterm_action, NULL);
 
1143
    if(ret == -1){
 
1144
      perror("sigaction");
 
1145
      exitcode = EXIT_FAILURE;
 
1146
      goto end;
 
1147
    }
 
1148
  }
 
1149
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
 
1150
  if(ret == -1){
 
1151
    perror("sigaction");
 
1152
    return EXIT_FAILURE;
 
1153
  }
 
1154
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1155
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1156
    if(ret == -1){
 
1157
      perror("sigaction");
 
1158
      exitcode = EXIT_FAILURE;
 
1159
      goto end;
 
1160
    }
 
1161
  }
 
1162
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
 
1163
  if(ret == -1){
 
1164
    perror("sigaction");
 
1165
    return EXIT_FAILURE;
 
1166
  }
 
1167
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1168
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1169
    if(ret == -1){
 
1170
      perror("sigaction");
 
1171
      exitcode = EXIT_FAILURE;
 
1172
      goto end;
 
1173
    }
 
1174
  }
 
1175
  
1015
1176
  /* If the interface is down, bring it up */
1016
1177
  if(interface[0] != '\0'){
 
1178
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1179
    if(if_index == 0){
 
1180
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1181
      exitcode = EXIT_FAILURE;
 
1182
      goto end;
 
1183
    }
 
1184
    
 
1185
    if(quit_now){
 
1186
      goto end;
 
1187
    }
 
1188
    
1017
1189
#ifdef __linux__
1018
1190
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1019
1191
       messages to mess up the prompt */
1056
1228
    }
1057
1229
    if((network.ifr_flags & IFF_UP) == 0){
1058
1230
      network.ifr_flags |= IFF_UP;
 
1231
      take_down_interface = true;
1059
1232
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1060
1233
      if(ret == -1){
 
1234
        take_down_interface = false;
1061
1235
        perror("ioctl SIOCSIFFLAGS");
1062
1236
        exitcode = EXIT_FAILURE;
1063
1237
#ifdef __linux__
1085
1259
        perror("nanosleep");
1086
1260
      }
1087
1261
    }
1088
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1089
 
    if(ret == -1){
1090
 
      perror("close");
 
1262
    if(not take_down_interface){
 
1263
      /* We won't need the socket anymore */
 
1264
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1265
      if(ret == -1){
 
1266
        perror("close");
 
1267
      }
1091
1268
    }
1092
1269
#ifdef __linux__
1093
1270
    if(restore_loglevel){
1100
1277
#endif  /* __linux__ */
1101
1278
  }
1102
1279
  
 
1280
  if(quit_now){
 
1281
    goto end;
 
1282
  }
 
1283
  
1103
1284
  uid = getuid();
1104
1285
  gid = getgid();
1105
1286
  
1114
1295
    perror("setuid");
1115
1296
  }
1116
1297
  
 
1298
  if(quit_now){
 
1299
    goto end;
 
1300
  }
 
1301
  
1117
1302
  ret = init_gnutls_global(pubkey, seckey);
1118
1303
  if(ret == -1){
1119
1304
    fprintf(stderr, "init_gnutls_global failed\n");
1123
1308
    gnutls_initialized = true;
1124
1309
  }
1125
1310
  
 
1311
  if(quit_now){
 
1312
    goto end;
 
1313
  }
 
1314
  
 
1315
  tempdir_created = true;
1126
1316
  if(mkdtemp(tempdir) == NULL){
 
1317
    tempdir_created = false;
1127
1318
    perror("mkdtemp");
1128
1319
    goto end;
1129
1320
  }
1130
 
  tempdir_created = true;
 
1321
  
 
1322
  if(quit_now){
 
1323
    goto end;
 
1324
  }
1131
1325
  
1132
1326
  if(not init_gpgme(pubkey, seckey, tempdir)){
1133
1327
    fprintf(stderr, "init_gpgme failed\n");
1137
1331
    gpgme_initialized = true;
1138
1332
  }
1139
1333
  
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
 
    }
 
1334
  if(quit_now){
 
1335
    goto end;
1147
1336
  }
1148
1337
  
1149
1338
  if(connect_to != NULL){
1155
1344
      exitcode = EXIT_FAILURE;
1156
1345
      goto end;
1157
1346
    }
 
1347
    
 
1348
    if(quit_now){
 
1349
      goto end;
 
1350
    }
 
1351
    
1158
1352
    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'){
 
1353
    errno = 0;
 
1354
    tmpmax = strtoimax(address+1, &tmp, 10);
 
1355
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
 
1356
       or tmpmax != (uint16_t)tmpmax){
1162
1357
      fprintf(stderr, "Bad port number\n");
1163
1358
      exitcode = EXIT_FAILURE;
1164
1359
      goto end;
1165
1360
    }
 
1361
  
 
1362
    if(quit_now){
 
1363
      goto end;
 
1364
    }
 
1365
    
1166
1366
    port = (uint16_t)tmpmax;
1167
1367
    *address = '\0';
1168
1368
    address = connect_to;
1173
1373
    } else {
1174
1374
      af = AF_INET;
1175
1375
    }
 
1376
    
 
1377
    if(quit_now){
 
1378
      goto end;
 
1379
    }
 
1380
    
1176
1381
    ret = start_mandos_communication(address, port, if_index, af);
1177
1382
    if(ret < 0){
1178
1383
      exitcode = EXIT_FAILURE;
1182
1387
    goto end;
1183
1388
  }
1184
1389
  
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;
 
1390
  if(quit_now){
1197
1391
    goto end;
1198
1392
  }
1199
1393
  
1223
1417
    goto end;
1224
1418
  }
1225
1419
  
 
1420
  if(quit_now){
 
1421
    goto end;
 
1422
  }
 
1423
  
1226
1424
  /* Create the Avahi service browser */
1227
1425
  sb = avahi_s_service_browser_new(mc.server, if_index,
1228
 
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1426
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1229
1427
                                   NULL, 0, browse_callback, NULL);
1230
1428
  if(sb == NULL){
1231
1429
    fprintf(stderr, "Failed to create service browser: %s\n",
1234
1432
    goto end;
1235
1433
  }
1236
1434
  
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
 
  }  
 
1435
  if(quit_now){
 
1436
    goto end;
 
1437
  }
1262
1438
  
1263
1439
  /* Run the main loop */
1264
1440
  
1294
1470
    gpgme_release(mc.ctx);
1295
1471
  }
1296
1472
  
 
1473
  /* Take down the network interface */
 
1474
  if(take_down_interface){
 
1475
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1476
    if(ret == -1){
 
1477
      perror("ioctl SIOCGIFFLAGS");
 
1478
    } else if(network.ifr_flags & IFF_UP) {
 
1479
      network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1480
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1481
      if(ret == -1){
 
1482
        perror("ioctl SIOCSIFFLAGS");
 
1483
      }
 
1484
    }
 
1485
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1486
    if(ret == -1){
 
1487
      perror("close");
 
1488
    }
 
1489
  }
 
1490
  
1297
1491
  /* Removes the temp directory used by GPGME */
1298
1492
  if(tempdir_created){
1299
1493
    DIR *d;
1338
1532
    }
1339
1533
  }
1340
1534
  
 
1535
  if(quit_now){
 
1536
    sigemptyset(&old_sigterm_action.sa_mask);
 
1537
    old_sigterm_action.sa_handler = SIG_DFL;
 
1538
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1539
    if(ret == -1){
 
1540
      perror("sigaction");
 
1541
    }
 
1542
    raise(signal_received);
 
1543
  }
 
1544
  
1341
1545
  return exitcode;
1342
1546
}