/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

* mandos (Client.start_checker): Bug fix: Add extra check in case the
                                 checker process completed quickly.

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() */
309
305
        }
310
306
        gpgme_recipient_t recipient;
311
307
        recipient = result->recipients;
312
 
        while(recipient != NULL){
313
 
          fprintf(stderr, "Public key algorithm: %s\n",
314
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
 
          fprintf(stderr, "Secret key available: %s\n",
317
 
                  recipient->status == GPG_ERR_NO_SECKEY
318
 
                  ? "No" : "Yes");
319
 
          recipient = recipient->next;
 
308
        if(recipient){
 
309
          while(recipient != NULL){
 
310
            fprintf(stderr, "Public key algorithm: %s\n",
 
311
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
312
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
313
            fprintf(stderr, "Secret key available: %s\n",
 
314
                    recipient->status == GPG_ERR_NO_SECKEY
 
315
                    ? "No" : "Yes");
 
316
            recipient = recipient->next;
 
317
          }
320
318
        }
321
319
      }
322
320
    }
564
562
  
565
563
  memset(&to, 0, sizeof(to));
566
564
  if(af == AF_INET6){
567
 
    to.in6.sin6_family = (sa_family_t)af;
 
565
    to.in6.sin6_family = (uint16_t)af;
568
566
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
569
567
  } else {                      /* IPv4 */
570
568
    to.in.sin_family = (sa_family_t)af;
856
854
       the callback function is called the Avahi server will free the
857
855
       resolver for us. */
858
856
    
859
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
860
 
                                    name, type, domain, protocol, 0,
861
 
                                    resolve_callback, NULL) == NULL)
 
857
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
858
                                       protocol, name, type, domain,
 
859
                                       AVAHI_PROTO_INET6, 0,
 
860
                                       resolve_callback, NULL)))
862
861
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
863
862
              name, avahi_strerror(avahi_server_errno(mc.server)));
864
863
    break;
895
894
  int error;
896
895
  int ret;
897
896
  intmax_t tmpmax;
898
 
  char *tmp;
 
897
  int numchars;
899
898
  int exitcode = EXIT_SUCCESS;
900
899
  const char *interface = "eth0";
901
900
  struct ifreq network;
902
 
  int sd = -1;
903
 
  bool interface_taken_up = false;
 
901
  int sd;
904
902
  uid_t uid;
905
903
  gid_t gid;
906
904
  char *connect_to = NULL;
910
908
  const char *seckey = PATHDIR "/" SECKEY;
911
909
  const char *pubkey = PATHDIR "/" PUBKEY;
912
910
  
 
911
  /* Initialize Mandos context */
 
912
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
913
                         .dh_bits = 1024, .priority = "SECURE256"
 
914
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
913
915
  bool gnutls_initialized = false;
914
916
  bool gpgme_initialized = false;
915
 
  float delay = 2.5f;
916
 
  
 
917
  double delay = 2.5;
 
918
 
917
919
  struct sigaction old_sigterm_action;
918
920
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
919
921
  
973
975
        pubkey = arg;
974
976
        break;
975
977
      case 129:                 /* --dh-bits */
976
 
        errno = 0;
977
 
        tmpmax = strtoimax(arg, &tmp, 10);
978
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
979
 
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
978
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
979
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
980
           or arg[numchars] != '\0'){
980
981
          fprintf(stderr, "Bad number of DH bits\n");
981
982
          exit(EXIT_FAILURE);
982
983
        }
986
987
        mc.priority = arg;
987
988
        break;
988
989
      case 131:                 /* --delay */
989
 
        errno = 0;
990
 
        delay = strtof(arg, &tmp);
991
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
990
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
991
        if(ret < 1 or arg[numchars] != '\0'){
992
992
          fprintf(stderr, "Bad delay\n");
993
993
          exit(EXIT_FAILURE);
994
994
        }
1114
1114
#endif  /* __linux__ */
1115
1115
        goto end;
1116
1116
      }
1117
 
      interface_taken_up = true;
1118
1117
    }
1119
1118
    /* sleep checking until interface is running */
1120
1119
    for(int i=0; i < delay * 4; i++){
1130
1129
        perror("nanosleep");
1131
1130
      }
1132
1131
    }
1133
 
    if(not interface_taken_up){
1134
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1135
 
      if(ret == -1){
1136
 
        perror("close");
1137
 
      }
 
1132
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1133
    if(ret == -1){
 
1134
      perror("close");
1138
1135
    }
1139
1136
#ifdef __linux__
1140
1137
    if(restore_loglevel){
1203
1200
      goto end;
1204
1201
    }
1205
1202
    uint16_t port;
1206
 
    errno = 0;
1207
 
    tmpmax = strtoimax(address+1, &tmp, 10);
1208
 
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1209
 
       or tmpmax != (uint16_t)tmpmax){
 
1203
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1204
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1205
       or address[numchars+1] != '\0'){
1210
1206
      fprintf(stderr, "Bad port number\n");
1211
1207
      exitcode = EXIT_FAILURE;
1212
1208
      goto end;
1258
1254
  
1259
1255
  /* Create the Avahi service browser */
1260
1256
  sb = avahi_s_service_browser_new(mc.server, if_index,
1261
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1257
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1262
1258
                                   NULL, 0, browse_callback, NULL);
1263
1259
  if(sb == NULL){
1264
1260
    fprintf(stderr, "Failed to create service browser: %s\n",
1301
1297
    gpgme_release(mc.ctx);
1302
1298
  }
1303
1299
  
1304
 
  /* Take down the network interface */
1305
 
  if(interface_taken_up){
1306
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1307
 
    if(ret == -1){
1308
 
      perror("ioctl SIOCGIFFLAGS");
1309
 
    } else if(network.ifr_flags & IFF_UP) {
1310
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1311
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1312
 
      if(ret == -1){
1313
 
        perror("ioctl SIOCSIFFLAGS");
1314
 
      }
1315
 
    }
1316
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1317
 
    if(ret == -1){
1318
 
      perror("close");
1319
 
    }
1320
 
  }
1321
 
  
1322
1300
  /* Removes the temp directory used by GPGME */
1323
1301
  if(tempdir_created){
1324
1302
    DIR *d;