/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, 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
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){
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 */
566
557
  
567
558
  memset(&to, 0, sizeof(to));
568
559
  if(af == AF_INET6){
569
 
    to.in6.sin6_family = (sa_family_t)af;
 
560
    to.in6.sin6_family = (uint16_t)af;
570
561
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
571
562
  } else {                      /* IPv4 */
572
563
    to.in.sin_family = (sa_family_t)af;
795
786
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
796
787
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
797
788
                             flags,
798
 
                             AVAHI_GCC_UNUSED void* userdata){
 
789
                             __attribute__((unused)) void* userdata){
799
790
  assert(r);
800
791
  
801
792
  /* Called whenever a service has been resolved successfully or
837
828
                            const char *domain,
838
829
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
839
830
                            flags,
840
 
                            AVAHI_GCC_UNUSED void* userdata){
 
831
                            __attribute__((unused)) void* userdata){
841
832
  assert(b);
842
833
  
843
834
  /* Called whenever a new services becomes available on the LAN or
858
849
       the callback function is called the Avahi server will free the
859
850
       resolver for us. */
860
851
    
861
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
862
 
                                    name, type, domain, protocol, 0,
863
 
                                    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)))
864
856
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
865
857
              name, avahi_strerror(avahi_server_errno(mc.server)));
866
858
    break;
877
869
  }
878
870
}
879
871
 
880
 
sig_atomic_t quit_now = 0;
881
 
 
882
 
/* stop main loop after sigterm has been called */
883
872
static void handle_sigterm(__attribute__((unused)) int sig){
884
 
  if(quit_now){
885
 
    return;
886
 
  }
887
 
  quit_now = 1;
888
873
  int old_errno = errno;
889
 
  if(mc.simple_poll != NULL){
890
 
    avahi_simple_poll_quit(mc.simple_poll);
891
 
  }
 
874
  avahi_simple_poll_quit(mc.simple_poll);
892
875
  errno = old_errno;
893
876
}
894
877
 
897
880
  int error;
898
881
  int ret;
899
882
  intmax_t tmpmax;
900
 
  char *tmp;
 
883
  int numchars;
901
884
  int exitcode = EXIT_SUCCESS;
902
885
  const char *interface = "eth0";
903
886
  struct ifreq network;
913
896
  
914
897
  bool gnutls_initialized = false;
915
898
  bool gpgme_initialized = false;
916
 
  float delay = 2.5f;
917
 
  
 
899
  double delay = 2.5;
 
900
 
918
901
  struct sigaction old_sigterm_action;
919
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" };
920
908
  
921
909
  {
922
910
    struct argp_option options[] = {
974
962
        pubkey = arg;
975
963
        break;
976
964
      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){
 
965
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
966
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
967
           or arg[numchars] != '\0'){
981
968
          fprintf(stderr, "Bad number of DH bits\n");
982
969
          exit(EXIT_FAILURE);
983
970
        }
987
974
        mc.priority = arg;
988
975
        break;
989
976
      case 131:                 /* --delay */
990
 
        errno = 0;
991
 
        delay = strtof(arg, &tmp);
992
 
        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'){
993
979
          fprintf(stderr, "Bad delay\n");
994
980
          exit(EXIT_FAILURE);
995
981
        }
1016
1002
    }
1017
1003
  }
1018
1004
  
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
1005
  /* If the interface is down, bring it up */
1061
1006
  if(interface[0] != '\0'){
1062
1007
#ifdef __linux__
1068
1013
      restore_loglevel = false;
1069
1014
      perror("klogctl");
1070
1015
    }
1071
 
#endif  /* __linux__ */
 
1016
#endif
1072
1017
    
1073
1018
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1074
1019
    if(sd < 0){
1081
1026
          perror("klogctl");
1082
1027
        }
1083
1028
      }
1084
 
#endif  /* __linux__ */
 
1029
#endif
1085
1030
      goto end;
1086
1031
    }
1087
1032
    strcpy(network.ifr_name, interface);
1095
1040
          perror("klogctl");
1096
1041
        }
1097
1042
      }
1098
 
#endif  /* __linux__ */
 
1043
#endif
1099
1044
      exitcode = EXIT_FAILURE;
1100
1045
      goto end;
1101
1046
    }
1112
1057
            perror("klogctl");
1113
1058
          }
1114
1059
        }
1115
 
#endif  /* __linux__ */
 
1060
#endif
1116
1061
        goto end;
1117
1062
      }
1118
1063
    }
1142
1087
        perror("klogctl");
1143
1088
      }
1144
1089
    }
1145
 
#endif  /* __linux__ */
 
1090
#endif
1146
1091
  }
1147
1092
  
1148
1093
  uid = getuid();
1201
1146
      goto end;
1202
1147
    }
1203
1148
    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){
 
1149
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1150
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1151
       or address[numchars+1] != '\0'){
1208
1152
      fprintf(stderr, "Bad port number\n");
1209
1153
      exitcode = EXIT_FAILURE;
1210
1154
      goto end;
1219
1163
    } else {
1220
1164
      af = AF_INET;
1221
1165
    }
1222
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1166
    ret = start_mandos_communication(address, port, if_index,
 
1167
                                     af);
1223
1168
    if(ret < 0){
1224
1169
      exitcode = EXIT_FAILURE;
1225
1170
    } else {
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",
1264
1224
    exitcode = EXIT_FAILURE;
1265
1225
    goto end;
1266
1226
  }
 
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
  }  
1267
1241
  
1268
1242
  /* Run the main loop */
1269
1243