/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 from Björn:

* plugin-runner.c (main): Bug fix: For the "--options-for" option, do
                          not mangle arguments with colon characters.
                          Also support adding empty arguments.

* plugins.d/mandos-client.c: (mc): New global variable; moved from
                                   "main".
  (init_gpgme, pgp_packet_decrypt, init_gnutls_global,
  init_gnutls_session, start_mandos_communication): Removed "mc"
                                                    argument.  All
                                                    callers changed.

  (resolve_callback, browse_callback): Ignore "userdata" argument.
                                       All callers changed.
  (handle_sigterm): New function.
  (main): Add "handle_sigterm" as signal handler for SIGTERM before
          starting the main loop.

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() */
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sigaction,
84
 
                                   sig_atomic_t */
 
79
                                   sigaction(), SIGTERM, sigaction */
85
80
 
86
81
#ifdef __linux__
87
82
#include <sys/klog.h>           /* klogctl() */
88
 
#endif  /* __linux__ */
 
83
#endif
89
84
 
90
85
/* Avahi */
91
86
/* All Avahi types, constants and functions
138
133
} mandos_context;
139
134
 
140
135
/* 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" };
 
136
mandos_context mc;
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){
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
 
/* stop main loop after sigterm has been called */
883
874
static void handle_sigterm(__attribute__((unused)) int sig){
884
 
  if(quit_now){
885
 
    return;
886
 
  }
887
 
  quit_now = 1;
888
875
  int old_errno = errno;
889
 
  if(mc.simple_poll != NULL){
890
 
    avahi_simple_poll_quit(mc.simple_poll);
891
 
  }
 
876
  avahi_simple_poll_quit(mc.simple_poll);
892
877
  errno = old_errno;
893
878
}
894
879
 
897
882
  int error;
898
883
  int ret;
899
884
  intmax_t tmpmax;
900
 
  char *tmp;
 
885
  int numchars;
901
886
  int exitcode = EXIT_SUCCESS;
902
887
  const char *interface = "eth0";
903
888
  struct ifreq network;
911
896
  const char *seckey = PATHDIR "/" SECKEY;
912
897
  const char *pubkey = PATHDIR "/" PUBKEY;
913
898
  
 
899
  /* Initialize Mandos context */
 
900
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
901
                         .dh_bits = 1024, .priority = "SECURE256"
 
902
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
914
903
  bool gnutls_initialized = false;
915
904
  bool gpgme_initialized = false;
916
 
  float delay = 2.5f;
917
 
  
 
905
  double delay = 2.5;
 
906
 
918
907
  struct sigaction old_sigterm_action;
919
908
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
920
909
  
974
963
        pubkey = arg;
975
964
        break;
976
965
      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){
 
966
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
967
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
968
           or arg[numchars] != '\0'){
981
969
          fprintf(stderr, "Bad number of DH bits\n");
982
970
          exit(EXIT_FAILURE);
983
971
        }
987
975
        mc.priority = arg;
988
976
        break;
989
977
      case 131:                 /* --delay */
990
 
        errno = 0;
991
 
        delay = strtof(arg, &tmp);
992
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
978
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
979
        if(ret < 1 or arg[numchars] != '\0'){
993
980
          fprintf(stderr, "Bad delay\n");
994
981
          exit(EXIT_FAILURE);
995
982
        }
1016
1003
    }
1017
1004
  }
1018
1005
  
1019
 
  if(not debug){
1020
 
    avahi_set_log_function(empty_log);
1021
 
  }
1022
 
  
1023
 
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
 
     from the signal handler */
1025
 
  /* Initialize the pseudo-RNG for Avahi */
1026
 
  srand((unsigned int) time(NULL));
1027
 
  mc.simple_poll = avahi_simple_poll_new();
1028
 
  if(mc.simple_poll == NULL){
1029
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
 
    exitcode = EXIT_FAILURE;
1031
 
    goto end;
1032
 
  }
1033
 
  
1034
 
  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
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1048
 
  if(ret == -1){
1049
 
    perror("sigaddset");
1050
 
    exitcode = EXIT_FAILURE;
1051
 
    goto end;
1052
 
  }
1053
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1054
 
  if(ret == -1){
1055
 
    perror("sigaction");
1056
 
    exitcode = EXIT_FAILURE;
1057
 
    goto end;
1058
 
  }  
1059
 
  
1060
1006
  /* If the interface is down, bring it up */
1061
1007
  if(interface[0] != '\0'){
1062
1008
#ifdef __linux__
1068
1014
      restore_loglevel = false;
1069
1015
      perror("klogctl");
1070
1016
    }
1071
 
#endif  /* __linux__ */
 
1017
#endif
1072
1018
    
1073
1019
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1074
1020
    if(sd < 0){
1081
1027
          perror("klogctl");
1082
1028
        }
1083
1029
      }
1084
 
#endif  /* __linux__ */
 
1030
#endif
1085
1031
      goto end;
1086
1032
    }
1087
1033
    strcpy(network.ifr_name, interface);
1095
1041
          perror("klogctl");
1096
1042
        }
1097
1043
      }
1098
 
#endif  /* __linux__ */
 
1044
#endif
1099
1045
      exitcode = EXIT_FAILURE;
1100
1046
      goto end;
1101
1047
    }
1112
1058
            perror("klogctl");
1113
1059
          }
1114
1060
        }
1115
 
#endif  /* __linux__ */
 
1061
#endif
1116
1062
        goto end;
1117
1063
      }
1118
1064
    }
1142
1088
        perror("klogctl");
1143
1089
      }
1144
1090
    }
1145
 
#endif  /* __linux__ */
 
1091
#endif
1146
1092
  }
1147
1093
  
1148
1094
  uid = getuid();
1201
1147
      goto end;
1202
1148
    }
1203
1149
    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){
 
1150
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1151
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1152
       or address[numchars+1] != '\0'){
1208
1153
      fprintf(stderr, "Bad port number\n");
1209
1154
      exitcode = EXIT_FAILURE;
1210
1155
      goto end;
1227
1172
    }
1228
1173
    goto end;
1229
1174
  }
1230
 
    
 
1175
  
 
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;
 
1188
    goto end;
 
1189
  }
 
1190
  
1231
1191
  {
1232
1192
    AvahiServerConfig config;
1233
1193
    /* Do not publish any local Zeroconf records */
1256
1216
  
1257
1217
  /* Create the Avahi service browser */
1258
1218
  sb = avahi_s_service_browser_new(mc.server, if_index,
1259
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1219
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1260
1220
                                   NULL, 0, browse_callback, NULL);
1261
1221
  if(sb == NULL){
1262
1222
    fprintf(stderr, "Failed to create service browser: %s\n",
1265
1225
    goto end;
1266
1226
  }
1267
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;
 
1233
    goto end;
 
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
  }  
 
1241
  
1268
1242
  /* Run the main loop */
1269
1243
  
1270
1244
  if(debug){