/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 05:48:04 UTC
  • mfrom: (24.1.134 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090209054804-6phrjjrzjlpvuz7u
Merge from Björn:

* plugin-runner.c (main): Bug fix: For the "--options-for" option, do
                          not mangle arguments with colon characters.
                          Also support adding empty arguments.

* plugins.d/mandos-client.c: (mc): New global variable; moved from
                                   "main".
  (init_gpgme, pgp_packet_decrypt, init_gnutls_global,
  init_gnutls_session, start_mandos_communication): Removed "mc"
                                                    argument.  All
                                                    callers changed.

  (resolve_callback, browse_callback): Ignore "userdata" argument.
                                       All callers changed.
  (handle_sigterm): New function.
  (main): Add "handle_sigterm" as signal handler for SIGTERM before
          starting the main loop.

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() */
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sigaction,
84
 
                                   sig_atomic_t */
 
79
                                   sigaction(), SIGTERM, sigaction */
85
80
 
86
81
#ifdef __linux__
87
82
#include <sys/klog.h>           /* klogctl() */
88
 
#endif  /* __linux__ */
 
83
#endif
89
84
 
90
85
/* Avahi */
91
86
/* All Avahi types, constants and functions
138
133
} mandos_context;
139
134
 
140
135
/* 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" };
 
136
mandos_context mc;
144
137
 
145
138
/*
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.
 
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.
149
142
 */
150
143
size_t incbuffer(char **buffer, size_t buffer_length,
151
144
                  size_t buffer_capacity){
309
302
        }
310
303
        gpgme_recipient_t recipient;
311
304
        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;
 
305
        if(recipient){
 
306
          while(recipient != NULL){
 
307
            fprintf(stderr, "Public key algorithm: %s\n",
 
308
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
309
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
310
            fprintf(stderr, "Secret key available: %s\n",
 
311
                    recipient->status == GPG_ERR_NO_SECKEY
 
312
                    ? "No" : "Yes");
 
313
            recipient = recipient->next;
 
314
          }
320
315
        }
321
316
      }
322
317
    }
564
559
  
565
560
  memset(&to, 0, sizeof(to));
566
561
  if(af == AF_INET6){
567
 
    to.in6.sin6_family = (sa_family_t)af;
 
562
    to.in6.sin6_family = (uint16_t)af;
568
563
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
569
564
  } else {                      /* IPv4 */
570
565
    to.in.sin_family = (sa_family_t)af;
856
851
       the callback function is called the Avahi server will free the
857
852
       resolver for us. */
858
853
    
859
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
860
 
                                    name, type, domain, protocol, 0,
861
 
                                    resolve_callback, NULL) == NULL)
 
854
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
855
                                       protocol, name, type, domain,
 
856
                                       AVAHI_PROTO_INET6, 0,
 
857
                                       resolve_callback, NULL)))
862
858
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
863
859
              name, avahi_strerror(avahi_server_errno(mc.server)));
864
860
    break;
875
871
  }
876
872
}
877
873
 
878
 
sig_atomic_t quit_now = 0;
879
 
 
880
 
/* stop main loop after sigterm has been called */
881
874
static void handle_sigterm(__attribute__((unused)) int sig){
882
 
  if(quit_now){
883
 
    return;
884
 
  }
885
 
  quit_now = 1;
886
875
  int old_errno = errno;
887
 
  if(mc.simple_poll != NULL){
888
 
    avahi_simple_poll_quit(mc.simple_poll);
889
 
  }
 
876
  avahi_simple_poll_quit(mc.simple_poll);
890
877
  errno = old_errno;
891
878
}
892
879
 
895
882
  int error;
896
883
  int ret;
897
884
  intmax_t tmpmax;
898
 
  char *tmp;
 
885
  int numchars;
899
886
  int exitcode = EXIT_SUCCESS;
900
887
  const char *interface = "eth0";
901
888
  struct ifreq network;
902
 
  int sd = -1;
903
 
  bool interface_taken_up = false;
 
889
  int sd;
904
890
  uid_t uid;
905
891
  gid_t gid;
906
892
  char *connect_to = NULL;
910
896
  const char *seckey = PATHDIR "/" SECKEY;
911
897
  const char *pubkey = PATHDIR "/" PUBKEY;
912
898
  
 
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" };
913
903
  bool gnutls_initialized = false;
914
904
  bool gpgme_initialized = false;
915
 
  float delay = 2.5f;
916
 
  
 
905
  double delay = 2.5;
 
906
 
917
907
  struct sigaction old_sigterm_action;
918
908
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
919
909
  
973
963
        pubkey = arg;
974
964
        break;
975
965
      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){
 
966
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
967
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
968
           or arg[numchars] != '\0'){
980
969
          fprintf(stderr, "Bad number of DH bits\n");
981
970
          exit(EXIT_FAILURE);
982
971
        }
986
975
        mc.priority = arg;
987
976
        break;
988
977
      case 131:                 /* --delay */
989
 
        errno = 0;
990
 
        delay = strtof(arg, &tmp);
991
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
978
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
979
        if(ret < 1 or arg[numchars] != '\0'){
992
980
          fprintf(stderr, "Bad delay\n");
993
981
          exit(EXIT_FAILURE);
994
982
        }
1015
1003
    }
1016
1004
  }
1017
1005
  
1018
 
  if(not debug){
1019
 
    avahi_set_log_function(empty_log);
1020
 
  }
1021
 
  
1022
 
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1023
 
     from the signal handler */
1024
 
  /* Initialize the pseudo-RNG for Avahi */
1025
 
  srand((unsigned int) time(NULL));
1026
 
  mc.simple_poll = avahi_simple_poll_new();
1027
 
  if(mc.simple_poll == NULL){
1028
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1029
 
    exitcode = EXIT_FAILURE;
1030
 
    goto end;
1031
 
  }
1032
 
  
1033
 
  sigemptyset(&sigterm_action.sa_mask);
1034
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1035
 
  if(ret == -1){
1036
 
    perror("sigaddset");
1037
 
    exitcode = EXIT_FAILURE;
1038
 
    goto end;
1039
 
  }
1040
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1041
 
  if(ret == -1){
1042
 
    perror("sigaddset");
1043
 
    exitcode = EXIT_FAILURE;
1044
 
    goto end;
1045
 
  }
1046
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1047
 
  if(ret == -1){
1048
 
    perror("sigaddset");
1049
 
    exitcode = EXIT_FAILURE;
1050
 
    goto end;
1051
 
  }
1052
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1053
 
  if(ret == -1){
1054
 
    perror("sigaction");
1055
 
    exitcode = EXIT_FAILURE;
1056
 
    goto end;
1057
 
  }  
1058
 
  
1059
1006
  /* If the interface is down, bring it up */
1060
1007
  if(interface[0] != '\0'){
1061
1008
#ifdef __linux__
1067
1014
      restore_loglevel = false;
1068
1015
      perror("klogctl");
1069
1016
    }
1070
 
#endif  /* __linux__ */
 
1017
#endif
1071
1018
    
1072
1019
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1073
1020
    if(sd < 0){
1080
1027
          perror("klogctl");
1081
1028
        }
1082
1029
      }
1083
 
#endif  /* __linux__ */
 
1030
#endif
1084
1031
      goto end;
1085
1032
    }
1086
1033
    strcpy(network.ifr_name, interface);
1094
1041
          perror("klogctl");
1095
1042
        }
1096
1043
      }
1097
 
#endif  /* __linux__ */
 
1044
#endif
1098
1045
      exitcode = EXIT_FAILURE;
1099
1046
      goto end;
1100
1047
    }
1111
1058
            perror("klogctl");
1112
1059
          }
1113
1060
        }
1114
 
#endif  /* __linux__ */
 
1061
#endif
1115
1062
        goto end;
1116
1063
      }
1117
 
      interface_taken_up = true;
1118
1064
    }
1119
1065
    /* sleep checking until interface is running */
1120
1066
    for(int i=0; i < delay * 4; i++){
1130
1076
        perror("nanosleep");
1131
1077
      }
1132
1078
    }
1133
 
    if(not interface_taken_up){
1134
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1135
 
      if(ret == -1){
1136
 
        perror("close");
1137
 
      }
 
1079
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1080
    if(ret == -1){
 
1081
      perror("close");
1138
1082
    }
1139
1083
#ifdef __linux__
1140
1084
    if(restore_loglevel){
1144
1088
        perror("klogctl");
1145
1089
      }
1146
1090
    }
1147
 
#endif  /* __linux__ */
 
1091
#endif
1148
1092
  }
1149
1093
  
1150
1094
  uid = getuid();
1203
1147
      goto end;
1204
1148
    }
1205
1149
    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){
 
1150
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1151
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1152
       or address[numchars+1] != '\0'){
1210
1153
      fprintf(stderr, "Bad port number\n");
1211
1154
      exitcode = EXIT_FAILURE;
1212
1155
      goto end;
1229
1172
    }
1230
1173
    goto end;
1231
1174
  }
1232
 
    
 
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
  
1233
1191
  {
1234
1192
    AvahiServerConfig config;
1235
1193
    /* Do not publish any local Zeroconf records */
1258
1216
  
1259
1217
  /* Create the Avahi service browser */
1260
1218
  sb = avahi_s_service_browser_new(mc.server, if_index,
1261
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1219
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1262
1220
                                   NULL, 0, browse_callback, NULL);
1263
1221
  if(sb == NULL){
1264
1222
    fprintf(stderr, "Failed to create service browser: %s\n",
1267
1225
    goto end;
1268
1226
  }
1269
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
  }  
 
1241
  
1270
1242
  /* Run the main loop */
1271
1243
  
1272
1244
  if(debug){
1301
1273
    gpgme_release(mc.ctx);
1302
1274
  }
1303
1275
  
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
1276
  /* Removes the temp directory used by GPGME */
1323
1277
  if(tempdir_created){
1324
1278
    DIR *d;