/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-09 19:26:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090209192619-7rse82x9pypq4ihk
* plugin-runner.c: Comment change.

* plugins.d/mandos-client.c: Comment changes.
  (quit_now): New global flag.
  (handle_sigterm): Only call avahi_simple_poll_quit once.
  (main): Also handle SIGINT and SIGHUP.

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() */
138
134
} mandos_context;
139
135
 
140
136
/* 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" };
 
137
mandos_context mc;
144
138
 
145
139
/*
146
140
 * Make additional room in "buffer" for at least BUFFER_SIZE more
566
560
  
567
561
  memset(&to, 0, sizeof(to));
568
562
  if(af == AF_INET6){
569
 
    to.in6.sin6_family = (sa_family_t)af;
 
563
    to.in6.sin6_family = (uint16_t)af;
570
564
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
571
565
  } else {                      /* IPv4 */
572
566
    to.in.sin_family = (sa_family_t)af;
858
852
       the callback function is called the Avahi server will free the
859
853
       resolver for us. */
860
854
    
861
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
862
 
                                    name, type, domain, protocol, 0,
863
 
                                    resolve_callback, NULL) == NULL)
 
855
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
856
                                       protocol, name, type, domain,
 
857
                                       AVAHI_PROTO_INET6, 0,
 
858
                                       resolve_callback, NULL)))
864
859
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
865
860
              name, avahi_strerror(avahi_server_errno(mc.server)));
866
861
    break;
879
874
 
880
875
sig_atomic_t quit_now = 0;
881
876
 
882
 
/* stop main loop after sigterm has been called */
883
877
static void handle_sigterm(__attribute__((unused)) int sig){
884
878
  if(quit_now){
885
879
    return;
897
891
  int error;
898
892
  int ret;
899
893
  intmax_t tmpmax;
900
 
  char *tmp;
 
894
  int numchars;
901
895
  int exitcode = EXIT_SUCCESS;
902
896
  const char *interface = "eth0";
903
897
  struct ifreq network;
911
905
  const char *seckey = PATHDIR "/" SECKEY;
912
906
  const char *pubkey = PATHDIR "/" PUBKEY;
913
907
  
 
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" };
914
912
  bool gnutls_initialized = false;
915
913
  bool gpgme_initialized = false;
916
 
  float delay = 2.5f;
917
 
  
 
914
  double delay = 2.5;
 
915
 
918
916
  struct sigaction old_sigterm_action;
919
917
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
920
918
  
974
972
        pubkey = arg;
975
973
        break;
976
974
      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){
 
975
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
976
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
977
           or arg[numchars] != '\0'){
981
978
          fprintf(stderr, "Bad number of DH bits\n");
982
979
          exit(EXIT_FAILURE);
983
980
        }
987
984
        mc.priority = arg;
988
985
        break;
989
986
      case 131:                 /* --delay */
990
 
        errno = 0;
991
 
        delay = strtof(arg, &tmp);
992
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
987
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
988
        if(ret < 1 or arg[numchars] != '\0'){
993
989
          fprintf(stderr, "Bad delay\n");
994
990
          exit(EXIT_FAILURE);
995
991
        }
1016
1012
    }
1017
1013
  }
1018
1014
  
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
1015
  /* If the interface is down, bring it up */
1061
1016
  if(interface[0] != '\0'){
1062
1017
#ifdef __linux__
1201
1156
      goto end;
1202
1157
    }
1203
1158
    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){
 
1159
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1160
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1161
       or address[numchars+1] != '\0'){
1208
1162
      fprintf(stderr, "Bad port number\n");
1209
1163
      exitcode = EXIT_FAILURE;
1210
1164
      goto end;
1227
1181
    }
1228
1182
    goto end;
1229
1183
  }
1230
 
    
 
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
  
1231
1200
  {
1232
1201
    AvahiServerConfig config;
1233
1202
    /* Do not publish any local Zeroconf records */
1256
1225
  
1257
1226
  /* Create the Avahi service browser */
1258
1227
  sb = avahi_s_service_browser_new(mc.server, if_index,
1259
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1228
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1260
1229
                                   NULL, 0, browse_callback, NULL);
1261
1230
  if(sb == NULL){
1262
1231
    fprintf(stderr, "Failed to create service browser: %s\n",
1265
1234
    goto end;
1266
1235
  }
1267
1236
  
 
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
  
1268
1263
  /* Run the main loop */
1269
1264
  
1270
1265
  if(debug){