/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

* plugins.d/mandos-client.c (browse_callback, main): Do not require
                                                     IPv6.
  (main): Removed redundant initialization of "mc".

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
37
 
38
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf(),
40
 
                                   remove() */
 
39
                                   stdout, ferror(), remove() */
41
40
#include <stdint.h>             /* uint16_t, uint32_t */
42
41
#include <stddef.h>             /* NULL, size_t, ssize_t */
43
42
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
44
 
                                   srand() */
 
43
                                   srand(), strtof() */
45
44
#include <stdbool.h>            /* bool, false, true */
46
45
#include <string.h>             /* memset(), strcmp(), strlen(),
47
46
                                   strerror(), asprintf(), strcpy() */
56
55
#include <fcntl.h>              /* open() */
57
56
#include <dirent.h>             /* opendir(), struct dirent, readdir()
58
57
                                 */
59
 
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
 
58
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
 
59
                                   strtoimax() */
60
60
#include <assert.h>             /* assert() */
61
61
#include <errno.h>              /* perror(), errno */
62
62
#include <time.h>               /* nanosleep(), time() */
76
76
                                   argp_parse(), ARGP_KEY_ARG,
77
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
79
 
                                   sigaction(), SIGTERM, sigaction */
 
79
                                   sigaction(), SIGTERM, sigaction,
 
80
                                   sig_atomic_t */
80
81
 
81
82
#ifdef __linux__
82
83
#include <sys/klog.h>           /* klogctl() */
83
 
#endif
 
84
#endif  /* __linux__ */
84
85
 
85
86
/* Avahi */
86
87
/* All Avahi types, constants and functions
133
134
} mandos_context;
134
135
 
135
136
/* global context so signal handler can reach it*/
136
 
mandos_context mc;
 
137
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
138
                      .dh_bits = 1024, .priority = "SECURE256"
 
139
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
137
140
 
138
141
/*
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.
 
142
 * Make additional room in "buffer" for at least BUFFER_SIZE more
 
143
 * bytes. "buffer_capacity" is how much is currently allocated,
 
144
 * "buffer_length" is how much is already used.
142
145
 */
143
146
size_t incbuffer(char **buffer, size_t buffer_length,
144
147
                  size_t buffer_capacity){
851
854
       the callback function is called the Avahi server will free the
852
855
       resolver for us. */
853
856
    
854
 
    if(!(avahi_s_service_resolver_new(mc.server, interface,
855
 
                                       protocol, name, type, domain,
856
 
                                       AVAHI_PROTO_INET6, 0,
857
 
                                       resolve_callback, NULL)))
 
857
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
 
858
                                    name, type, domain, protocol, 0,
 
859
                                    resolve_callback, NULL) == NULL)
858
860
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
859
861
              name, avahi_strerror(avahi_server_errno(mc.server)));
860
862
    break;
871
873
  }
872
874
}
873
875
 
 
876
sig_atomic_t quit_now = 0;
 
877
 
 
878
/* stop main loop after sigterm has been called */
874
879
static void handle_sigterm(__attribute__((unused)) int sig){
 
880
  if(quit_now){
 
881
    return;
 
882
  }
 
883
  quit_now = 1;
875
884
  int old_errno = errno;
876
 
  avahi_simple_poll_quit(mc.simple_poll);
 
885
  if(mc.simple_poll != NULL){
 
886
    avahi_simple_poll_quit(mc.simple_poll);
 
887
  }
877
888
  errno = old_errno;
878
889
}
879
890
 
882
893
  int error;
883
894
  int ret;
884
895
  intmax_t tmpmax;
885
 
  int numchars;
 
896
  char *tmp;
886
897
  int exitcode = EXIT_SUCCESS;
887
898
  const char *interface = "eth0";
888
899
  struct ifreq network;
896
907
  const char *seckey = PATHDIR "/" SECKEY;
897
908
  const char *pubkey = PATHDIR "/" PUBKEY;
898
909
  
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" };
903
910
  bool gnutls_initialized = false;
904
911
  bool gpgme_initialized = false;
905
 
  double delay = 2.5;
906
 
 
 
912
  float delay = 2.5f;
 
913
  
907
914
  struct sigaction old_sigterm_action;
908
915
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
909
916
  
963
970
        pubkey = arg;
964
971
        break;
965
972
      case 129:                 /* --dh-bits */
966
 
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
967
 
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
968
 
           or arg[numchars] != '\0'){
 
973
        errno = 0;
 
974
        tmpmax = strtoimax(arg, &tmp, 10);
 
975
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
976
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
969
977
          fprintf(stderr, "Bad number of DH bits\n");
970
978
          exit(EXIT_FAILURE);
971
979
        }
975
983
        mc.priority = arg;
976
984
        break;
977
985
      case 131:                 /* --delay */
978
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
979
 
        if(ret < 1 or arg[numchars] != '\0'){
 
986
        errno = 0;
 
987
        delay = strtof(arg, &tmp);
 
988
        if(errno != 0 or tmp == arg or *tmp != '\0'){
980
989
          fprintf(stderr, "Bad delay\n");
981
990
          exit(EXIT_FAILURE);
982
991
        }
1003
1012
    }
1004
1013
  }
1005
1014
  
 
1015
  if(not debug){
 
1016
    avahi_set_log_function(empty_log);
 
1017
  }
 
1018
  
 
1019
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
 
1020
     from the signal handler */
 
1021
  /* Initialize the pseudo-RNG for Avahi */
 
1022
  srand((unsigned int) time(NULL));
 
1023
  mc.simple_poll = avahi_simple_poll_new();
 
1024
  if(mc.simple_poll == NULL){
 
1025
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1026
    exitcode = EXIT_FAILURE;
 
1027
    goto end;
 
1028
  }
 
1029
  
 
1030
  sigemptyset(&sigterm_action.sa_mask);
 
1031
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1032
  if(ret == -1){
 
1033
    perror("sigaddset");
 
1034
    exitcode = EXIT_FAILURE;
 
1035
    goto end;
 
1036
  }
 
1037
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1038
  if(ret == -1){
 
1039
    perror("sigaddset");
 
1040
    exitcode = EXIT_FAILURE;
 
1041
    goto end;
 
1042
  }
 
1043
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1044
  if(ret == -1){
 
1045
    perror("sigaddset");
 
1046
    exitcode = EXIT_FAILURE;
 
1047
    goto end;
 
1048
  }
 
1049
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1050
  if(ret == -1){
 
1051
    perror("sigaction");
 
1052
    exitcode = EXIT_FAILURE;
 
1053
    goto end;
 
1054
  }  
 
1055
  
1006
1056
  /* If the interface is down, bring it up */
1007
1057
  if(interface[0] != '\0'){
1008
1058
#ifdef __linux__
1014
1064
      restore_loglevel = false;
1015
1065
      perror("klogctl");
1016
1066
    }
1017
 
#endif
 
1067
#endif  /* __linux__ */
1018
1068
    
1019
1069
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1020
1070
    if(sd < 0){
1027
1077
          perror("klogctl");
1028
1078
        }
1029
1079
      }
1030
 
#endif
 
1080
#endif  /* __linux__ */
1031
1081
      goto end;
1032
1082
    }
1033
1083
    strcpy(network.ifr_name, interface);
1041
1091
          perror("klogctl");
1042
1092
        }
1043
1093
      }
1044
 
#endif
 
1094
#endif  /* __linux__ */
1045
1095
      exitcode = EXIT_FAILURE;
1046
1096
      goto end;
1047
1097
    }
1058
1108
            perror("klogctl");
1059
1109
          }
1060
1110
        }
1061
 
#endif
 
1111
#endif  /* __linux__ */
1062
1112
        goto end;
1063
1113
      }
1064
1114
    }
1088
1138
        perror("klogctl");
1089
1139
      }
1090
1140
    }
1091
 
#endif
 
1141
#endif  /* __linux__ */
1092
1142
  }
1093
1143
  
1094
1144
  uid = getuid();
1147
1197
      goto end;
1148
1198
    }
1149
1199
    uint16_t port;
1150
 
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1151
 
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
1152
 
       or address[numchars+1] != '\0'){
 
1200
    errno = 0;
 
1201
    tmpmax = strtoimax(address+1, &tmp, 10);
 
1202
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
 
1203
       or tmpmax != (uint16_t)tmpmax){
1153
1204
      fprintf(stderr, "Bad port number\n");
1154
1205
      exitcode = EXIT_FAILURE;
1155
1206
      goto end;
1172
1223
    }
1173
1224
    goto end;
1174
1225
  }
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
 
  
 
1226
    
1191
1227
  {
1192
1228
    AvahiServerConfig config;
1193
1229
    /* Do not publish any local Zeroconf records */
1216
1252
  
1217
1253
  /* Create the Avahi service browser */
1218
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1219
 
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1220
1256
                                   NULL, 0, browse_callback, NULL);
1221
1257
  if(sb == NULL){
1222
1258
    fprintf(stderr, "Failed to create service browser: %s\n",
1225
1261
    goto end;
1226
1262
  }
1227
1263
  
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
 
  
1242
1264
  /* Run the main loop */
1243
1265
  
1244
1266
  if(debug){