/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

  • Committer: Björn Påhlsson
  • Date: 2009-02-09 18:15:18 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: belorn@braxen-20090209181518-powtql91qv3wu51d
Earlier signal handling

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, sigaction,
84
 
                                   sig_atomic_t */
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
143
136
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
137
 
145
138
/*
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.
 
139
 * Make additional room in "buffer" for at least BUFFER_SIZE
 
140
 * additional bytes. "buffer_capacity" is how much is currently
 
141
 * allocated, "buffer_length" is how much is already used.
149
142
 */
150
143
size_t incbuffer(char **buffer, size_t buffer_length,
151
144
                  size_t buffer_capacity){
205
198
  }
206
199
  
207
200
  if(debug){
208
 
    fprintf(stderr, "Initializing GPGME\n");
 
201
    fprintf(stderr, "Initialize gpgme\n");
209
202
  }
210
203
  
211
204
  /* Init GPGME */
566
559
  
567
560
  memset(&to, 0, sizeof(to));
568
561
  if(af == AF_INET6){
569
 
    to.in6.sin6_family = (sa_family_t)af;
 
562
    to.in6.sin6_family = (uint16_t)af;
570
563
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
571
564
  } else {                      /* IPv4 */
572
565
    to.in.sin_family = (sa_family_t)af;
795
788
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
796
789
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
797
790
                             flags,
798
 
                             AVAHI_GCC_UNUSED void* userdata){
 
791
                             __attribute__((unused)) void* userdata){
799
792
  assert(r);
800
793
  
801
794
  /* Called whenever a service has been resolved successfully or
837
830
                            const char *domain,
838
831
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
839
832
                            flags,
840
 
                            AVAHI_GCC_UNUSED void* userdata){
 
833
                            __attribute__((unused)) void* userdata){
841
834
  assert(b);
842
835
  
843
836
  /* Called whenever a new services becomes available on the LAN or
858
851
       the callback function is called the Avahi server will free the
859
852
       resolver for us. */
860
853
    
861
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
862
 
                                    name, type, domain, protocol, 0,
863
 
                                    resolve_callback, NULL) == NULL)
 
854
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
855
                                       protocol, name, type, domain,
 
856
                                       AVAHI_PROTO_INET6, 0,
 
857
                                       resolve_callback, NULL)))
864
858
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
865
859
              name, avahi_strerror(avahi_server_errno(mc.server)));
866
860
    break;
877
871
  }
878
872
}
879
873
 
880
 
sig_atomic_t quit_now = 0;
881
 
 
882
874
/* stop main loop after sigterm has been called */
883
875
static void handle_sigterm(__attribute__((unused)) int sig){
884
 
  if(quit_now){
885
 
    return;
886
 
  }
887
 
  quit_now = 1;
888
876
  int old_errno = errno;
889
 
  if(mc.simple_poll != NULL){
890
 
    avahi_simple_poll_quit(mc.simple_poll);
891
 
  }
 
877
  avahi_simple_poll_quit(mc.simple_poll);
892
878
  errno = old_errno;
893
879
}
894
880
 
897
883
  int error;
898
884
  int ret;
899
885
  intmax_t tmpmax;
900
 
  char *tmp;
 
886
  int numchars;
901
887
  int exitcode = EXIT_SUCCESS;
902
888
  const char *interface = "eth0";
903
889
  struct ifreq network;
913
899
  
914
900
  bool gnutls_initialized = false;
915
901
  bool gpgme_initialized = false;
916
 
  float delay = 2.5f;
917
 
  
 
902
  double delay = 2.5;
 
903
 
918
904
  struct sigaction old_sigterm_action;
919
905
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
920
 
  
 
906
 
921
907
  {
922
908
    struct argp_option options[] = {
923
909
      { .name = "debug", .key = 128,
974
960
        pubkey = arg;
975
961
        break;
976
962
      case 129:                 /* --dh-bits */
977
 
        errno = 0;
978
 
        tmpmax = strtoimax(arg, &tmp, 10);
979
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
980
 
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
963
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
964
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
965
           or arg[numchars] != '\0'){
981
966
          fprintf(stderr, "Bad number of DH bits\n");
982
967
          exit(EXIT_FAILURE);
983
968
        }
987
972
        mc.priority = arg;
988
973
        break;
989
974
      case 131:                 /* --delay */
990
 
        errno = 0;
991
 
        delay = strtof(arg, &tmp);
992
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
975
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
976
        if(ret < 1 or arg[numchars] != '\0'){
993
977
          fprintf(stderr, "Bad delay\n");
994
978
          exit(EXIT_FAILURE);
995
979
        }
1015
999
      goto end;
1016
1000
    }
1017
1001
  }
1018
 
  
 
1002
 
1019
1003
  if(not debug){
1020
1004
    avahi_set_log_function(empty_log);
1021
1005
  }
1022
 
  
 
1006
 
1023
1007
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
1008
     from the signal handler */
1025
1009
  /* Initialize the pseudo-RNG for Avahi */
1030
1014
    exitcode = EXIT_FAILURE;
1031
1015
    goto end;
1032
1016
  }
1033
 
  
 
1017
 
1034
1018
  sigemptyset(&sigterm_action.sa_mask);
1035
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1036
 
  if(ret == -1){
1037
 
    perror("sigaddset");
1038
 
    exitcode = EXIT_FAILURE;
1039
 
    goto end;
1040
 
  }
1041
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1042
 
  if(ret == -1){
1043
 
    perror("sigaddset");
1044
 
    exitcode = EXIT_FAILURE;
1045
 
    goto end;
1046
 
  }
1047
1019
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1048
1020
  if(ret == -1){
1049
1021
    perror("sigaddset");
1056
1028
    exitcode = EXIT_FAILURE;
1057
1029
    goto end;
1058
1030
  }  
 
1031
 
1059
1032
  
1060
1033
  /* If the interface is down, bring it up */
1061
1034
  if(interface[0] != '\0'){
1068
1041
      restore_loglevel = false;
1069
1042
      perror("klogctl");
1070
1043
    }
1071
 
#endif  /* __linux__ */
 
1044
#endif
1072
1045
    
1073
1046
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1074
1047
    if(sd < 0){
1081
1054
          perror("klogctl");
1082
1055
        }
1083
1056
      }
1084
 
#endif  /* __linux__ */
 
1057
#endif
1085
1058
      goto end;
1086
1059
    }
1087
1060
    strcpy(network.ifr_name, interface);
1095
1068
          perror("klogctl");
1096
1069
        }
1097
1070
      }
1098
 
#endif  /* __linux__ */
 
1071
#endif
1099
1072
      exitcode = EXIT_FAILURE;
1100
1073
      goto end;
1101
1074
    }
1112
1085
            perror("klogctl");
1113
1086
          }
1114
1087
        }
1115
 
#endif  /* __linux__ */
 
1088
#endif
1116
1089
        goto end;
1117
1090
      }
1118
1091
    }
1142
1115
        perror("klogctl");
1143
1116
      }
1144
1117
    }
1145
 
#endif  /* __linux__ */
 
1118
#endif
1146
1119
  }
1147
1120
  
1148
1121
  uid = getuid();
1201
1174
      goto end;
1202
1175
    }
1203
1176
    uint16_t port;
1204
 
    errno = 0;
1205
 
    tmpmax = strtoimax(address+1, &tmp, 10);
1206
 
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1207
 
       or tmpmax != (uint16_t)tmpmax){
 
1177
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1178
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1179
       or address[numchars+1] != '\0'){
1208
1180
      fprintf(stderr, "Bad port number\n");
1209
1181
      exitcode = EXIT_FAILURE;
1210
1182
      goto end;
1219
1191
    } else {
1220
1192
      af = AF_INET;
1221
1193
    }
1222
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1194
    ret = start_mandos_communication(address, port, if_index,
 
1195
                                     af);
1223
1196
    if(ret < 0){
1224
1197
      exitcode = EXIT_FAILURE;
1225
1198
    } else {
1256
1229
  
1257
1230
  /* Create the Avahi service browser */
1258
1231
  sb = avahi_s_service_browser_new(mc.server, if_index,
1259
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1232
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1260
1233
                                   NULL, 0, browse_callback, NULL);
1261
1234
  if(sb == NULL){
1262
1235
    fprintf(stderr, "Failed to create service browser: %s\n",