/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

plugin-runner: Added support for empty string arguments
               Fixed bug where IP:port argument failed
mandos-client: Fixed bug where files wasnt removed after sigterm signal

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
34
33
#define _LARGEFILE_SOURCE
35
 
#endif
36
 
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
38
 
#endif
39
35
 
40
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
37
 
42
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
 
39
                                   stdout, ferror(), sscanf(),
 
40
                                   remove() */
44
41
#include <stdint.h>             /* uint16_t, uint32_t */
45
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
44
                                   srand() */
48
45
#include <stdbool.h>            /* bool, false, true */
49
46
#include <string.h>             /* memset(), strcmp(), strlen(),
50
47
                                   strerror(), asprintf(), strcpy() */
59
56
#include <fcntl.h>              /* open() */
60
57
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
58
                                 */
62
 
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
 
                                   strtoimax() */
 
59
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
64
60
#include <assert.h>             /* assert() */
65
61
#include <errno.h>              /* perror(), errno */
66
62
#include <time.h>               /* nanosleep(), time() */
79
75
                                   argp_state, struct argp,
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
 
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
85
 
 
 
78
#include <signal.h>             /* sigemptyset(), sigaddset(), sigaction(), SIGTERM, sigaction */
86
79
#ifdef __linux__
87
80
#include <sys/klog.h>           /* klogctl() */
88
 
#endif  /* __linux__ */
 
81
#endif
89
82
 
90
83
/* Avahi */
91
84
/* All Avahi types, constants and functions
138
131
} mandos_context;
139
132
 
140
133
/* global context so signal handler can reach it*/
141
 
mandos_context mc = { .simple_poll = NULL, .server = NULL,
142
 
                      .dh_bits = 1024, .priority = "SECURE256"
143
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
134
mandos_context mc;
144
135
 
145
136
/*
146
 
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
 
 * bytes. "buffer_capacity" is how much is currently allocated,
148
 
 * "buffer_length" is how much is already used.
 
137
 * Make additional room in "buffer" for at least BUFFER_SIZE
 
138
 * additional bytes. "buffer_capacity" is how much is currently
 
139
 * allocated, "buffer_length" is how much is already used.
149
140
 */
150
141
size_t incbuffer(char **buffer, size_t buffer_length,
151
142
                  size_t buffer_capacity){
164
155
 */
165
156
static bool init_gpgme(const char *seckey,
166
157
                       const char *pubkey, const char *tempdir){
 
158
  int ret;
167
159
  gpgme_error_t rc;
168
160
  gpgme_engine_info_t engine_info;
169
161
  
172
164
   * Helper function to insert pub and seckey to the engine keyring.
173
165
   */
174
166
  bool import_key(const char *filename){
175
 
    int ret;
176
167
    int fd;
177
168
    gpgme_data_t pgp_data;
178
169
    
205
196
  }
206
197
  
207
198
  if(debug){
208
 
    fprintf(stderr, "Initializing GPGME\n");
 
199
    fprintf(stderr, "Initialize gpgme\n");
209
200
  }
210
201
  
211
202
  /* Init GPGME */
249
240
    return false;
250
241
  }
251
242
  
252
 
  return true;
 
243
  return true; 
253
244
}
254
245
 
255
246
/* 
309
300
        }
310
301
        gpgme_recipient_t recipient;
311
302
        recipient = result->recipients;
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;
 
303
        if(recipient){
 
304
          while(recipient != NULL){
 
305
            fprintf(stderr, "Public key algorithm: %s\n",
 
306
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
307
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
308
            fprintf(stderr, "Secret key available: %s\n",
 
309
                    recipient->status == GPG_ERR_NO_SECKEY
 
310
                    ? "No" : "Yes");
 
311
            recipient = recipient->next;
 
312
          }
320
313
        }
321
314
      }
322
315
    }
514
507
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
508
                      __attribute__((unused)) const char *txt){}
516
509
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
510
/* Called when a Mandos server is found */
521
511
static int start_mandos_communication(const char *ip, uint16_t port,
522
512
                                      AvahiIfIndex if_index,
523
513
                                      int af){
524
 
  int ret, tcp_sd = -1;
 
514
  int ret, tcp_sd;
525
515
  ssize_t sret;
526
516
  union {
527
517
    struct sockaddr_in in;
531
521
  char *decrypted_buffer;
532
522
  size_t buffer_length = 0;
533
523
  size_t buffer_capacity = 0;
 
524
  ssize_t decrypted_buffer_size;
534
525
  size_t written;
535
526
  int retval = 0;
536
527
  gnutls_session_t session;
537
528
  int pf;                       /* Protocol family */
538
529
  
539
 
  if(quit_now){
540
 
    return -1;
541
 
  }
542
 
  
543
530
  switch(af){
544
531
  case AF_INET6:
545
532
    pf = PF_INET6;
565
552
  tcp_sd = socket(pf, SOCK_STREAM, 0);
566
553
  if(tcp_sd < 0){
567
554
    perror("socket");
568
 
    retval = -1;
569
 
    goto mandos_end;
570
 
  }
571
 
  
572
 
  if(quit_now){
573
 
    goto mandos_end;
 
555
    return -1;
574
556
  }
575
557
  
576
558
  memset(&to, 0, sizeof(to));
577
559
  if(af == AF_INET6){
578
 
    to.in6.sin6_family = (sa_family_t)af;
 
560
    to.in6.sin6_family = (uint16_t)af;
579
561
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
580
562
  } else {                      /* IPv4 */
581
563
    to.in.sin_family = (sa_family_t)af;
583
565
  }
584
566
  if(ret < 0 ){
585
567
    perror("inet_pton");
586
 
    retval = -1;
587
 
    goto mandos_end;
 
568
    return -1;
588
569
  }
589
570
  if(ret == 0){
590
571
    fprintf(stderr, "Bad address: %s\n", ip);
591
 
    retval = -1;
592
 
    goto mandos_end;
 
572
    return -1;
593
573
  }
594
574
  if(af == AF_INET6){
595
575
    to.in6.sin6_port = htons(port); /* Spurious warnings from
602
582
      if(if_index == AVAHI_IF_UNSPEC){
603
583
        fprintf(stderr, "An IPv6 link-local address is incomplete"
604
584
                " without a network interface\n");
605
 
        retval = -1;
606
 
        goto mandos_end;
 
585
        return -1;
607
586
      }
608
587
      /* Set the network interface number as scope */
609
588
      to.in6.sin6_scope_id = (uint32_t)if_index;
614
593
                                     -Wunreachable-code */
615
594
  }
616
595
  
617
 
  if(quit_now){
618
 
    goto mandos_end;
619
 
  }
620
 
  
621
596
  if(debug){
622
597
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
623
598
      char interface[IF_NAMESIZE];
650
625
    }
651
626
  }
652
627
  
653
 
  if(quit_now){
654
 
    goto mandos_end;
655
 
  }
656
 
  
657
628
  if(af == AF_INET6){
658
629
    ret = connect(tcp_sd, &to.in6, sizeof(to));
659
630
  } else {
661
632
  }
662
633
  if(ret < 0){
663
634
    perror("connect");
664
 
    retval = -1;
665
 
    goto mandos_end;
666
 
  }
667
 
  
668
 
  if(quit_now){
669
 
    goto mandos_end;
 
635
    return -1;
670
636
  }
671
637
  
672
638
  const char *out = mandos_protocol_version;
691
657
        break;
692
658
      }
693
659
    }
694
 
  
695
 
    if(quit_now){
696
 
      goto mandos_end;
697
 
    }
698
660
  }
699
661
  
700
662
  if(debug){
701
663
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
702
664
  }
703
665
  
704
 
  if(quit_now){
705
 
    goto mandos_end;
706
 
  }
707
 
  
708
666
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
709
667
  
710
 
  if(quit_now){
711
 
    goto mandos_end;
712
 
  }
713
 
  
714
 
  do {
 
668
  do{
715
669
    ret = gnutls_handshake(session);
716
 
    if(quit_now){
717
 
      goto mandos_end;
718
 
    }
719
670
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
720
671
  
721
672
  if(ret != GNUTLS_E_SUCCESS){
735
686
  }
736
687
  
737
688
  while(true){
738
 
    
739
 
    if(quit_now){
740
 
      goto mandos_end;
741
 
    }
742
 
    
743
689
    buffer_capacity = incbuffer(&buffer, buffer_length,
744
690
                                   buffer_capacity);
745
691
    if(buffer_capacity == 0){
748
694
      goto mandos_end;
749
695
    }
750
696
    
751
 
    if(quit_now){
752
 
      goto mandos_end;
753
 
    }
754
 
    
755
697
    sret = gnutls_record_recv(session, buffer+buffer_length,
756
698
                              BUFFER_SIZE);
757
699
    if(sret == 0){
763
705
      case GNUTLS_E_AGAIN:
764
706
        break;
765
707
      case GNUTLS_E_REHANDSHAKE:
766
 
        do {
 
708
        do{
767
709
          ret = gnutls_handshake(session);
768
 
          
769
 
          if(quit_now){
770
 
            goto mandos_end;
771
 
          }
772
710
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
773
711
        if(ret < 0){
774
712
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
793
731
    fprintf(stderr, "Closing TLS session\n");
794
732
  }
795
733
  
796
 
  if(quit_now){
797
 
    goto mandos_end;
798
 
  }
799
 
  
800
734
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
801
735
  
802
 
  if(quit_now){
803
 
    goto mandos_end;
804
 
  }
805
 
  
806
736
  if(buffer_length > 0){
807
 
    ssize_t decrypted_buffer_size;
808
737
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
809
738
                                               buffer_length,
810
739
                                               &decrypted_buffer);
811
740
    if(decrypted_buffer_size >= 0){
812
 
      
813
741
      written = 0;
814
742
      while(written < (size_t) decrypted_buffer_size){
815
 
        if(quit_now){
816
 
          goto mandos_end;
817
 
        }
818
 
        
819
743
        ret = (int)fwrite(decrypted_buffer + written, 1,
820
744
                          (size_t)decrypted_buffer_size - written,
821
745
                          stdout);
841
765
  
842
766
 mandos_end:
843
767
  free(buffer);
844
 
  if(tcp_sd >= 0){
845
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
 
  }
 
768
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
847
769
  if(ret == -1){
848
770
    perror("close");
849
771
  }
850
772
  gnutls_deinit(session);
851
 
  if(quit_now){
852
 
    retval = -1;
853
 
  }
854
773
  return retval;
855
774
}
856
775
 
867
786
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
868
787
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
869
788
                             flags,
870
 
                             AVAHI_GCC_UNUSED void* userdata){
 
789
                             __attribute__((unused)) void* userdata){
871
790
  assert(r);
872
791
  
873
792
  /* Called whenever a service has been resolved successfully or
874
793
     timed out */
875
794
  
876
 
  if(quit_now){
877
 
    return;
878
 
  }
879
 
  
880
795
  switch(event){
881
796
  default:
882
797
  case AVAHI_RESOLVER_FAILURE:
913
828
                            const char *domain,
914
829
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
915
830
                            flags,
916
 
                            AVAHI_GCC_UNUSED void* userdata){
 
831
                            __attribute__((unused)) void* userdata){
917
832
  assert(b);
918
833
  
919
834
  /* Called whenever a new services becomes available on the LAN or
920
835
     is removed from the LAN */
921
836
  
922
 
  if(quit_now){
923
 
    return;
924
 
  }
925
 
  
926
837
  switch(event){
927
838
  default:
928
839
  case AVAHI_BROWSER_FAILURE:
938
849
       the callback function is called the Avahi server will free the
939
850
       resolver for us. */
940
851
    
941
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
942
 
                                    name, type, domain, protocol, 0,
943
 
                                    resolve_callback, NULL) == NULL)
 
852
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
853
                                       protocol, name, type, domain,
 
854
                                       AVAHI_PROTO_INET6, 0,
 
855
                                       resolve_callback, NULL)))
944
856
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
945
857
              name, avahi_strerror(avahi_server_errno(mc.server)));
946
858
    break;
957
869
  }
958
870
}
959
871
 
960
 
/* stop main loop after sigterm has been called */
961
 
static void handle_sigterm(int sig){
962
 
  if(quit_now){
963
 
    return;
964
 
  }
965
 
  quit_now = 1;
966
 
  signal_received = sig;
 
872
static void handle_sigterm(__attribute__((unused)) int sig){
967
873
  int old_errno = errno;
968
 
  if(mc.simple_poll != NULL){
969
 
    avahi_simple_poll_quit(mc.simple_poll);
970
 
  }
 
874
  avahi_simple_poll_quit(mc.simple_poll);
971
875
  errno = old_errno;
972
876
}
973
877
 
976
880
  int error;
977
881
  int ret;
978
882
  intmax_t tmpmax;
979
 
  char *tmp;
 
883
  int numchars;
980
884
  int exitcode = EXIT_SUCCESS;
981
885
  const char *interface = "eth0";
982
886
  struct ifreq network;
983
 
  int sd = -1;
984
 
  bool take_down_interface = false;
 
887
  int sd;
985
888
  uid_t uid;
986
889
  gid_t gid;
987
890
  char *connect_to = NULL;
993
896
  
994
897
  bool gnutls_initialized = false;
995
898
  bool gpgme_initialized = false;
996
 
  float delay = 2.5f;
997
 
  
 
899
  double delay = 2.5;
 
900
 
998
901
  struct sigaction old_sigterm_action;
999
902
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
 
903
 
 
904
  /* Initialize mandos context */
 
905
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
906
                         .dh_bits = 1024, .priority = "SECURE256"
 
907
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
1000
908
  
1001
909
  {
1002
910
    struct argp_option options[] = {
1054
962
        pubkey = arg;
1055
963
        break;
1056
964
      case 129:                 /* --dh-bits */
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){
 
965
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
966
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
967
           or arg[numchars] != '\0'){
1061
968
          fprintf(stderr, "Bad number of DH bits\n");
1062
969
          exit(EXIT_FAILURE);
1063
970
        }
1067
974
        mc.priority = arg;
1068
975
        break;
1069
976
      case 131:                 /* --delay */
1070
 
        errno = 0;
1071
 
        delay = strtof(arg, &tmp);
1072
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
977
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
978
        if(ret < 1 or arg[numchars] != '\0'){
1073
979
          fprintf(stderr, "Bad delay\n");
1074
980
          exit(EXIT_FAILURE);
1075
981
        }
1096
1002
    }
1097
1003
  }
1098
1004
  
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
 
  
1177
1005
  /* If the interface is down, bring it up */
1178
1006
  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
 
    
1190
1007
#ifdef __linux__
1191
1008
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
1009
       messages to mess up the prompt */
1196
1013
      restore_loglevel = false;
1197
1014
      perror("klogctl");
1198
1015
    }
1199
 
#endif  /* __linux__ */
 
1016
#endif
1200
1017
    
1201
1018
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1202
1019
    if(sd < 0){
1209
1026
          perror("klogctl");
1210
1027
        }
1211
1028
      }
1212
 
#endif  /* __linux__ */
 
1029
#endif
1213
1030
      goto end;
1214
1031
    }
1215
1032
    strcpy(network.ifr_name, interface);
1223
1040
          perror("klogctl");
1224
1041
        }
1225
1042
      }
1226
 
#endif  /* __linux__ */
 
1043
#endif
1227
1044
      exitcode = EXIT_FAILURE;
1228
1045
      goto end;
1229
1046
    }
1230
1047
    if((network.ifr_flags & IFF_UP) == 0){
1231
1048
      network.ifr_flags |= IFF_UP;
1232
 
      take_down_interface = true;
1233
1049
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
1050
      if(ret == -1){
1235
 
        take_down_interface = false;
1236
1051
        perror("ioctl SIOCSIFFLAGS");
1237
1052
        exitcode = EXIT_FAILURE;
1238
1053
#ifdef __linux__
1242
1057
            perror("klogctl");
1243
1058
          }
1244
1059
        }
1245
 
#endif  /* __linux__ */
 
1060
#endif
1246
1061
        goto end;
1247
1062
      }
1248
1063
    }
1260
1075
        perror("nanosleep");
1261
1076
      }
1262
1077
    }
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
 
      }
 
1078
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1079
    if(ret == -1){
 
1080
      perror("close");
1269
1081
    }
1270
1082
#ifdef __linux__
1271
1083
    if(restore_loglevel){
1275
1087
        perror("klogctl");
1276
1088
      }
1277
1089
    }
1278
 
#endif  /* __linux__ */
1279
 
  }
1280
 
  
1281
 
  if(quit_now){
1282
 
    goto end;
 
1090
#endif
1283
1091
  }
1284
1092
  
1285
1093
  uid = getuid();
1296
1104
    perror("setuid");
1297
1105
  }
1298
1106
  
1299
 
  if(quit_now){
1300
 
    goto end;
1301
 
  }
1302
 
  
1303
1107
  ret = init_gnutls_global(pubkey, seckey);
1304
1108
  if(ret == -1){
1305
1109
    fprintf(stderr, "init_gnutls_global failed\n");
1309
1113
    gnutls_initialized = true;
1310
1114
  }
1311
1115
  
1312
 
  if(quit_now){
1313
 
    goto end;
1314
 
  }
1315
 
  
1316
 
  tempdir_created = true;
1317
1116
  if(mkdtemp(tempdir) == NULL){
1318
 
    tempdir_created = false;
1319
1117
    perror("mkdtemp");
1320
1118
    goto end;
1321
1119
  }
1322
 
  
1323
 
  if(quit_now){
1324
 
    goto end;
1325
 
  }
 
1120
  tempdir_created = true;
1326
1121
  
1327
1122
  if(not init_gpgme(pubkey, seckey, tempdir)){
1328
1123
    fprintf(stderr, "init_gpgme failed\n");
1332
1127
    gpgme_initialized = true;
1333
1128
  }
1334
1129
  
1335
 
  if(quit_now){
1336
 
    goto end;
 
1130
  if(interface[0] != '\0'){
 
1131
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1132
    if(if_index == 0){
 
1133
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1134
      exitcode = EXIT_FAILURE;
 
1135
      goto end;
 
1136
    }
1337
1137
  }
1338
1138
  
1339
1139
  if(connect_to != NULL){
1345
1145
      exitcode = EXIT_FAILURE;
1346
1146
      goto end;
1347
1147
    }
1348
 
    
1349
 
    if(quit_now){
1350
 
      goto end;
1351
 
    }
1352
 
    
1353
1148
    uint16_t port;
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){
 
1149
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1150
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1151
       or address[numchars+1] != '\0'){
1358
1152
      fprintf(stderr, "Bad port number\n");
1359
1153
      exitcode = EXIT_FAILURE;
1360
1154
      goto end;
1361
1155
    }
1362
 
  
1363
 
    if(quit_now){
1364
 
      goto end;
1365
 
    }
1366
 
    
1367
1156
    port = (uint16_t)tmpmax;
1368
1157
    *address = '\0';
1369
1158
    address = connect_to;
1374
1163
    } else {
1375
1164
      af = AF_INET;
1376
1165
    }
1377
 
    
1378
 
    if(quit_now){
1379
 
      goto end;
1380
 
    }
1381
 
    
1382
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1166
    ret = start_mandos_communication(address, port, if_index,
 
1167
                                     af);
1383
1168
    if(ret < 0){
1384
1169
      exitcode = EXIT_FAILURE;
1385
1170
    } else {
1388
1173
    goto end;
1389
1174
  }
1390
1175
  
1391
 
  if(quit_now){
 
1176
  if(not debug){
 
1177
    avahi_set_log_function(empty_log);
 
1178
  }
 
1179
  
 
1180
  /* Initialize the pseudo-RNG for Avahi */
 
1181
  srand((unsigned int) time(NULL));
 
1182
  
 
1183
  /* Allocate main Avahi loop object */
 
1184
  mc.simple_poll = avahi_simple_poll_new();
 
1185
  if(mc.simple_poll == NULL){
 
1186
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1187
    exitcode = EXIT_FAILURE;
1392
1188
    goto end;
1393
1189
  }
1394
1190
  
1418
1214
    goto end;
1419
1215
  }
1420
1216
  
1421
 
  if(quit_now){
1422
 
    goto end;
1423
 
  }
1424
 
  
1425
1217
  /* Create the Avahi service browser */
1426
1218
  sb = avahi_s_service_browser_new(mc.server, if_index,
1427
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1219
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1428
1220
                                   NULL, 0, browse_callback, NULL);
1429
1221
  if(sb == NULL){
1430
1222
    fprintf(stderr, "Failed to create service browser: %s\n",
1432
1224
    exitcode = EXIT_FAILURE;
1433
1225
    goto end;
1434
1226
  }
1435
 
  
1436
 
  if(quit_now){
 
1227
 
 
1228
  sigemptyset(&sigterm_action.sa_mask);
 
1229
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1230
  if(ret == -1){
 
1231
    perror("sigaddset");
 
1232
    exitcode = EXIT_FAILURE;
1437
1233
    goto end;
1438
1234
  }
 
1235
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1236
  if(ret == -1){
 
1237
    perror("sigaction");
 
1238
    exitcode = EXIT_FAILURE;
 
1239
    goto end;
 
1240
  }  
1439
1241
  
1440
1242
  /* Run the main loop */
1441
1243
  
1471
1273
    gpgme_release(mc.ctx);
1472
1274
  }
1473
1275
  
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
 
  
1492
1276
  /* Removes the temp directory used by GPGME */
1493
1277
  if(tempdir_created){
1494
1278
    DIR *d;
1533
1317
    }
1534
1318
  }
1535
1319
  
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
 
  
1546
1320
  return exitcode;
1547
1321
}