/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

* plugin-runner.c: Minor stylistic changes.

* plugins.d/askpass-fifo.c: Changed contact information and did minor
                            stylistic changes.

* plugins.d/mandos-client.c: Minor stylistic changes.

* plugins.d/password-prompt.c: Changed contact information.

* plugins.d/splashy.c: Changed contact information.
  (main): Changed error handling design.  Bug fix: Will now be much
          more tolerant against signals.  Bug fix: Will now re-raise
          signal received.

* plugins.d/usplash.c: Changed contact information and did minor
                       stylistic changes.

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() */
160
164
 */
161
165
static bool init_gpgme(const char *seckey,
162
166
                       const char *pubkey, const char *tempdir){
163
 
  int ret;
164
167
  gpgme_error_t rc;
165
168
  gpgme_engine_info_t engine_info;
166
169
  
169
172
   * Helper function to insert pub and seckey to the engine keyring.
170
173
   */
171
174
  bool import_key(const char *filename){
 
175
    int ret;
172
176
    int fd;
173
177
    gpgme_data_t pgp_data;
174
178
    
245
249
    return false;
246
250
  }
247
251
  
248
 
  return true; 
 
252
  return true;
249
253
}
250
254
 
251
255
/* 
305
309
        }
306
310
        gpgme_recipient_t recipient;
307
311
        recipient = result->recipients;
308
 
        if(recipient){
309
 
          while(recipient != NULL){
310
 
            fprintf(stderr, "Public key algorithm: %s\n",
311
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
312
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
313
 
            fprintf(stderr, "Secret key available: %s\n",
314
 
                    recipient->status == GPG_ERR_NO_SECKEY
315
 
                    ? "No" : "Yes");
316
 
            recipient = recipient->next;
317
 
          }
 
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;
318
320
        }
319
321
      }
320
322
    }
512
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
513
515
                      __attribute__((unused)) const char *txt){}
514
516
 
 
517
sig_atomic_t quit_now = 0;
 
518
int signal_received = 0;
 
519
 
515
520
/* Called when a Mandos server is found */
516
521
static int start_mandos_communication(const char *ip, uint16_t port,
517
522
                                      AvahiIfIndex if_index,
518
523
                                      int af){
519
 
  int ret, tcp_sd;
 
524
  int ret, tcp_sd = -1;
520
525
  ssize_t sret;
521
526
  union {
522
527
    struct sockaddr_in in;
526
531
  char *decrypted_buffer;
527
532
  size_t buffer_length = 0;
528
533
  size_t buffer_capacity = 0;
529
 
  ssize_t decrypted_buffer_size;
530
534
  size_t written;
531
535
  int retval = 0;
532
536
  gnutls_session_t session;
533
537
  int pf;                       /* Protocol family */
534
538
  
 
539
  if(quit_now){
 
540
    return -1;
 
541
  }
 
542
  
535
543
  switch(af){
536
544
  case AF_INET6:
537
545
    pf = PF_INET6;
557
565
  tcp_sd = socket(pf, SOCK_STREAM, 0);
558
566
  if(tcp_sd < 0){
559
567
    perror("socket");
560
 
    return -1;
 
568
    retval = -1;
 
569
    goto mandos_end;
 
570
  }
 
571
  
 
572
  if(quit_now){
 
573
    goto mandos_end;
561
574
  }
562
575
  
563
576
  memset(&to, 0, sizeof(to));
564
577
  if(af == AF_INET6){
565
 
    to.in6.sin6_family = (uint16_t)af;
 
578
    to.in6.sin6_family = (sa_family_t)af;
566
579
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
567
580
  } else {                      /* IPv4 */
568
581
    to.in.sin_family = (sa_family_t)af;
570
583
  }
571
584
  if(ret < 0 ){
572
585
    perror("inet_pton");
573
 
    return -1;
 
586
    retval = -1;
 
587
    goto mandos_end;
574
588
  }
575
589
  if(ret == 0){
576
590
    fprintf(stderr, "Bad address: %s\n", ip);
577
 
    return -1;
 
591
    retval = -1;
 
592
    goto mandos_end;
578
593
  }
579
594
  if(af == AF_INET6){
580
595
    to.in6.sin6_port = htons(port); /* Spurious warnings from
587
602
      if(if_index == AVAHI_IF_UNSPEC){
588
603
        fprintf(stderr, "An IPv6 link-local address is incomplete"
589
604
                " without a network interface\n");
590
 
        return -1;
 
605
        retval = -1;
 
606
        goto mandos_end;
591
607
      }
592
608
      /* Set the network interface number as scope */
593
609
      to.in6.sin6_scope_id = (uint32_t)if_index;
598
614
                                     -Wunreachable-code */
599
615
  }
600
616
  
 
617
  if(quit_now){
 
618
    goto mandos_end;
 
619
  }
 
620
  
601
621
  if(debug){
602
622
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
603
623
      char interface[IF_NAMESIZE];
630
650
    }
631
651
  }
632
652
  
 
653
  if(quit_now){
 
654
    goto mandos_end;
 
655
  }
 
656
  
633
657
  if(af == AF_INET6){
634
658
    ret = connect(tcp_sd, &to.in6, sizeof(to));
635
659
  } else {
637
661
  }
638
662
  if(ret < 0){
639
663
    perror("connect");
640
 
    return -1;
 
664
    retval = -1;
 
665
    goto mandos_end;
 
666
  }
 
667
  
 
668
  if(quit_now){
 
669
    goto mandos_end;
641
670
  }
642
671
  
643
672
  const char *out = mandos_protocol_version;
662
691
        break;
663
692
      }
664
693
    }
 
694
  
 
695
    if(quit_now){
 
696
      goto mandos_end;
 
697
    }
665
698
  }
666
699
  
667
700
  if(debug){
668
701
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
669
702
  }
670
703
  
 
704
  if(quit_now){
 
705
    goto mandos_end;
 
706
  }
 
707
  
671
708
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
672
709
  
673
 
  do{
 
710
  if(quit_now){
 
711
    goto mandos_end;
 
712
  }
 
713
  
 
714
  do {
674
715
    ret = gnutls_handshake(session);
 
716
    if(quit_now){
 
717
      goto mandos_end;
 
718
    }
675
719
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
676
720
  
677
721
  if(ret != GNUTLS_E_SUCCESS){
691
735
  }
692
736
  
693
737
  while(true){
 
738
    
 
739
    if(quit_now){
 
740
      goto mandos_end;
 
741
    }
 
742
    
694
743
    buffer_capacity = incbuffer(&buffer, buffer_length,
695
744
                                   buffer_capacity);
696
745
    if(buffer_capacity == 0){
699
748
      goto mandos_end;
700
749
    }
701
750
    
 
751
    if(quit_now){
 
752
      goto mandos_end;
 
753
    }
 
754
    
702
755
    sret = gnutls_record_recv(session, buffer+buffer_length,
703
756
                              BUFFER_SIZE);
704
757
    if(sret == 0){
710
763
      case GNUTLS_E_AGAIN:
711
764
        break;
712
765
      case GNUTLS_E_REHANDSHAKE:
713
 
        do{
 
766
        do {
714
767
          ret = gnutls_handshake(session);
 
768
          
 
769
          if(quit_now){
 
770
            goto mandos_end;
 
771
          }
715
772
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
716
773
        if(ret < 0){
717
774
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
736
793
    fprintf(stderr, "Closing TLS session\n");
737
794
  }
738
795
  
 
796
  if(quit_now){
 
797
    goto mandos_end;
 
798
  }
 
799
  
739
800
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
740
801
  
 
802
  if(quit_now){
 
803
    goto mandos_end;
 
804
  }
 
805
  
741
806
  if(buffer_length > 0){
 
807
    ssize_t decrypted_buffer_size;
742
808
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
743
809
                                               buffer_length,
744
810
                                               &decrypted_buffer);
745
811
    if(decrypted_buffer_size >= 0){
 
812
      
746
813
      written = 0;
747
814
      while(written < (size_t) decrypted_buffer_size){
 
815
        if(quit_now){
 
816
          goto mandos_end;
 
817
        }
 
818
        
748
819
        ret = (int)fwrite(decrypted_buffer + written, 1,
749
820
                          (size_t)decrypted_buffer_size - written,
750
821
                          stdout);
770
841
  
771
842
 mandos_end:
772
843
  free(buffer);
773
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
844
  if(tcp_sd >= 0){
 
845
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
846
  }
774
847
  if(ret == -1){
775
848
    perror("close");
776
849
  }
777
850
  gnutls_deinit(session);
 
851
  if(quit_now){
 
852
    retval = -1;
 
853
  }
778
854
  return retval;
779
855
}
780
856
 
797
873
  /* Called whenever a service has been resolved successfully or
798
874
     timed out */
799
875
  
 
876
  if(quit_now){
 
877
    return;
 
878
  }
 
879
  
800
880
  switch(event){
801
881
  default:
802
882
  case AVAHI_RESOLVER_FAILURE:
839
919
  /* Called whenever a new services becomes available on the LAN or
840
920
     is removed from the LAN */
841
921
  
 
922
  if(quit_now){
 
923
    return;
 
924
  }
 
925
  
842
926
  switch(event){
843
927
  default:
844
928
  case AVAHI_BROWSER_FAILURE:
854
938
       the callback function is called the Avahi server will free the
855
939
       resolver for us. */
856
940
    
857
 
    if(!(avahi_s_service_resolver_new(mc.server, interface,
858
 
                                       protocol, name, type, domain,
859
 
                                       AVAHI_PROTO_INET6, 0,
860
 
                                       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)
861
944
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
862
945
              name, avahi_strerror(avahi_server_errno(mc.server)));
863
946
    break;
874
957
  }
875
958
}
876
959
 
877
 
sig_atomic_t quit_now = 0;
878
 
 
879
960
/* stop main loop after sigterm has been called */
880
 
static void handle_sigterm(__attribute__((unused)) int sig){
 
961
static void handle_sigterm(int sig){
881
962
  if(quit_now){
882
963
    return;
883
964
  }
884
965
  quit_now = 1;
 
966
  signal_received = sig;
885
967
  int old_errno = errno;
886
968
  if(mc.simple_poll != NULL){
887
969
    avahi_simple_poll_quit(mc.simple_poll);
894
976
  int error;
895
977
  int ret;
896
978
  intmax_t tmpmax;
897
 
  int numchars;
 
979
  char *tmp;
898
980
  int exitcode = EXIT_SUCCESS;
899
981
  const char *interface = "eth0";
900
982
  struct ifreq network;
901
 
  int sd;
 
983
  int sd = -1;
 
984
  bool take_down_interface = false;
902
985
  uid_t uid;
903
986
  gid_t gid;
904
987
  char *connect_to = NULL;
908
991
  const char *seckey = PATHDIR "/" SECKEY;
909
992
  const char *pubkey = PATHDIR "/" PUBKEY;
910
993
  
911
 
  /* Initialize Mandos context */
912
 
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
913
 
                         .dh_bits = 1024, .priority = "SECURE256"
914
 
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
915
994
  bool gnutls_initialized = false;
916
995
  bool gpgme_initialized = false;
917
 
  double delay = 2.5;
918
 
 
 
996
  float delay = 2.5f;
 
997
  
919
998
  struct sigaction old_sigterm_action;
920
999
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
921
1000
  
975
1054
        pubkey = arg;
976
1055
        break;
977
1056
      case 129:                 /* --dh-bits */
978
 
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
979
 
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
980
 
           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){
981
1061
          fprintf(stderr, "Bad number of DH bits\n");
982
1062
          exit(EXIT_FAILURE);
983
1063
        }
987
1067
        mc.priority = arg;
988
1068
        break;
989
1069
      case 131:                 /* --delay */
990
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
991
 
        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'){
992
1073
          fprintf(stderr, "Bad delay\n");
993
1074
          exit(EXIT_FAILURE);
994
1075
        }
1049
1130
    exitcode = EXIT_FAILURE;
1050
1131
    goto end;
1051
1132
  }
1052
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1053
 
  if(ret == -1){
1054
 
    perror("sigaction");
1055
 
    exitcode = EXIT_FAILURE;
1056
 
    goto end;
1057
 
  }  
 
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
  }
1058
1176
  
1059
1177
  /* If the interface is down, bring it up */
1060
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
    
1061
1190
#ifdef __linux__
1062
1191
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1063
1192
       messages to mess up the prompt */
1100
1229
    }
1101
1230
    if((network.ifr_flags & IFF_UP) == 0){
1102
1231
      network.ifr_flags |= IFF_UP;
 
1232
      take_down_interface = true;
1103
1233
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1104
1234
      if(ret == -1){
 
1235
        take_down_interface = false;
1105
1236
        perror("ioctl SIOCSIFFLAGS");
1106
1237
        exitcode = EXIT_FAILURE;
1107
1238
#ifdef __linux__
1129
1260
        perror("nanosleep");
1130
1261
      }
1131
1262
    }
1132
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1133
 
    if(ret == -1){
1134
 
      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
      }
1135
1269
    }
1136
1270
#ifdef __linux__
1137
1271
    if(restore_loglevel){
1144
1278
#endif  /* __linux__ */
1145
1279
  }
1146
1280
  
 
1281
  if(quit_now){
 
1282
    goto end;
 
1283
  }
 
1284
  
1147
1285
  uid = getuid();
1148
1286
  gid = getgid();
1149
1287
  
1158
1296
    perror("setuid");
1159
1297
  }
1160
1298
  
 
1299
  if(quit_now){
 
1300
    goto end;
 
1301
  }
 
1302
  
1161
1303
  ret = init_gnutls_global(pubkey, seckey);
1162
1304
  if(ret == -1){
1163
1305
    fprintf(stderr, "init_gnutls_global failed\n");
1167
1309
    gnutls_initialized = true;
1168
1310
  }
1169
1311
  
 
1312
  if(quit_now){
 
1313
    goto end;
 
1314
  }
 
1315
  
 
1316
  tempdir_created = true;
1170
1317
  if(mkdtemp(tempdir) == NULL){
 
1318
    tempdir_created = false;
1171
1319
    perror("mkdtemp");
1172
1320
    goto end;
1173
1321
  }
1174
 
  tempdir_created = true;
 
1322
  
 
1323
  if(quit_now){
 
1324
    goto end;
 
1325
  }
1175
1326
  
1176
1327
  if(not init_gpgme(pubkey, seckey, tempdir)){
1177
1328
    fprintf(stderr, "init_gpgme failed\n");
1181
1332
    gpgme_initialized = true;
1182
1333
  }
1183
1334
  
1184
 
  if(interface[0] != '\0'){
1185
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1186
 
    if(if_index == 0){
1187
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1188
 
      exitcode = EXIT_FAILURE;
1189
 
      goto end;
1190
 
    }
 
1335
  if(quit_now){
 
1336
    goto end;
1191
1337
  }
1192
1338
  
1193
1339
  if(connect_to != NULL){
1199
1345
      exitcode = EXIT_FAILURE;
1200
1346
      goto end;
1201
1347
    }
 
1348
    
 
1349
    if(quit_now){
 
1350
      goto end;
 
1351
    }
 
1352
    
1202
1353
    uint16_t port;
1203
 
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1204
 
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
1205
 
       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){
1206
1358
      fprintf(stderr, "Bad port number\n");
1207
1359
      exitcode = EXIT_FAILURE;
1208
1360
      goto end;
1209
1361
    }
 
1362
  
 
1363
    if(quit_now){
 
1364
      goto end;
 
1365
    }
 
1366
    
1210
1367
    port = (uint16_t)tmpmax;
1211
1368
    *address = '\0';
1212
1369
    address = connect_to;
1217
1374
    } else {
1218
1375
      af = AF_INET;
1219
1376
    }
 
1377
    
 
1378
    if(quit_now){
 
1379
      goto end;
 
1380
    }
 
1381
    
1220
1382
    ret = start_mandos_communication(address, port, if_index, af);
1221
1383
    if(ret < 0){
1222
1384
      exitcode = EXIT_FAILURE;
1225
1387
    }
1226
1388
    goto end;
1227
1389
  }
1228
 
    
 
1390
  
 
1391
  if(quit_now){
 
1392
    goto end;
 
1393
  }
 
1394
  
1229
1395
  {
1230
1396
    AvahiServerConfig config;
1231
1397
    /* Do not publish any local Zeroconf records */
1252
1418
    goto end;
1253
1419
  }
1254
1420
  
 
1421
  if(quit_now){
 
1422
    goto end;
 
1423
  }
 
1424
  
1255
1425
  /* Create the Avahi service browser */
1256
1426
  sb = avahi_s_service_browser_new(mc.server, if_index,
1257
 
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1427
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1258
1428
                                   NULL, 0, browse_callback, NULL);
1259
1429
  if(sb == NULL){
1260
1430
    fprintf(stderr, "Failed to create service browser: %s\n",
1263
1433
    goto end;
1264
1434
  }
1265
1435
  
 
1436
  if(quit_now){
 
1437
    goto end;
 
1438
  }
 
1439
  
1266
1440
  /* Run the main loop */
1267
1441
  
1268
1442
  if(debug){
1297
1471
    gpgme_release(mc.ctx);
1298
1472
  }
1299
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
  
1300
1492
  /* Removes the temp directory used by GPGME */
1301
1493
  if(tempdir_created){
1302
1494
    DIR *d;
1341
1533
    }
1342
1534
  }
1343
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
  
1344
1546
  return exitcode;
1345
1547
}