/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

  • Committer: Teddy Hogeborn
  • Date: 2009-02-12 23:17:14 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090212231714-59wxtc7h3w7ss8v6
* 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() */
134
134
} mandos_context;
135
135
 
136
136
/* global context so signal handler can reach it*/
137
 
mandos_context mc;
 
137
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
138
                      .dh_bits = 1024, .priority = "SECURE256"
 
139
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
138
140
 
139
141
/*
140
142
 * Make additional room in "buffer" for at least BUFFER_SIZE more
852
854
       the callback function is called the Avahi server will free the
853
855
       resolver for us. */
854
856
    
855
 
    if(!(avahi_s_service_resolver_new(mc.server, interface,
856
 
                                       protocol, name, type, domain,
857
 
                                       AVAHI_PROTO_INET6, 0,
858
 
                                       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)
859
860
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
860
861
              name, avahi_strerror(avahi_server_errno(mc.server)));
861
862
    break;
874
875
 
875
876
sig_atomic_t quit_now = 0;
876
877
 
 
878
/* stop main loop after sigterm has been called */
877
879
static void handle_sigterm(__attribute__((unused)) int sig){
878
880
  if(quit_now){
879
881
    return;
891
893
  int error;
892
894
  int ret;
893
895
  intmax_t tmpmax;
894
 
  int numchars;
 
896
  char *tmp;
895
897
  int exitcode = EXIT_SUCCESS;
896
898
  const char *interface = "eth0";
897
899
  struct ifreq network;
905
907
  const char *seckey = PATHDIR "/" SECKEY;
906
908
  const char *pubkey = PATHDIR "/" PUBKEY;
907
909
  
908
 
  /* Initialize Mandos context */
909
 
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
910
 
                         .dh_bits = 1024, .priority = "SECURE256"
911
 
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
912
910
  bool gnutls_initialized = false;
913
911
  bool gpgme_initialized = false;
914
 
  double delay = 2.5;
915
 
 
 
912
  float delay = 2.5f;
 
913
  
916
914
  struct sigaction old_sigterm_action;
917
915
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
918
916
  
972
970
        pubkey = arg;
973
971
        break;
974
972
      case 129:                 /* --dh-bits */
975
 
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
976
 
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
977
 
           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){
978
977
          fprintf(stderr, "Bad number of DH bits\n");
979
978
          exit(EXIT_FAILURE);
980
979
        }
984
983
        mc.priority = arg;
985
984
        break;
986
985
      case 131:                 /* --delay */
987
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
988
 
        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'){
989
989
          fprintf(stderr, "Bad delay\n");
990
990
          exit(EXIT_FAILURE);
991
991
        }
1012
1012
    }
1013
1013
  }
1014
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
  
1015
1056
  /* If the interface is down, bring it up */
1016
1057
  if(interface[0] != '\0'){
1017
1058
#ifdef __linux__
1156
1197
      goto end;
1157
1198
    }
1158
1199
    uint16_t port;
1159
 
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1160
 
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
1161
 
       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){
1162
1204
      fprintf(stderr, "Bad port number\n");
1163
1205
      exitcode = EXIT_FAILURE;
1164
1206
      goto end;
1181
1223
    }
1182
1224
    goto end;
1183
1225
  }
1184
 
  
1185
 
  if(not debug){
1186
 
    avahi_set_log_function(empty_log);
1187
 
  }
1188
 
  
1189
 
  /* Initialize the pseudo-RNG for Avahi */
1190
 
  srand((unsigned int) time(NULL));
1191
 
  
1192
 
  /* Allocate main Avahi loop object */
1193
 
  mc.simple_poll = avahi_simple_poll_new();
1194
 
  if(mc.simple_poll == NULL){
1195
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1196
 
    exitcode = EXIT_FAILURE;
1197
 
    goto end;
1198
 
  }
1199
 
  
 
1226
    
1200
1227
  {
1201
1228
    AvahiServerConfig config;
1202
1229
    /* Do not publish any local Zeroconf records */
1225
1252
  
1226
1253
  /* Create the Avahi service browser */
1227
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1228
 
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1229
1256
                                   NULL, 0, browse_callback, NULL);
1230
1257
  if(sb == NULL){
1231
1258
    fprintf(stderr, "Failed to create service browser: %s\n",
1234
1261
    goto end;
1235
1262
  }
1236
1263
  
1237
 
  sigemptyset(&sigterm_action.sa_mask);
1238
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1239
 
  if(ret == -1){
1240
 
    perror("sigaddset");
1241
 
    exitcode = EXIT_FAILURE;
1242
 
    goto end;
1243
 
  }
1244
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1245
 
  if(ret == -1){
1246
 
    perror("sigaddset");
1247
 
    exitcode = EXIT_FAILURE;
1248
 
    goto end;
1249
 
  }
1250
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1251
 
  if(ret == -1){
1252
 
    perror("sigaddset");
1253
 
    exitcode = EXIT_FAILURE;
1254
 
    goto end;
1255
 
  }
1256
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1257
 
  if(ret == -1){
1258
 
    perror("sigaction");
1259
 
    exitcode = EXIT_FAILURE;
1260
 
    goto end;
1261
 
  }  
1262
 
  
1263
1264
  /* Run the main loop */
1264
1265
  
1265
1266
  if(debug){