/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: 2012-06-16 23:25:46 UTC
  • Revision ID: teddy@recompile.se-20120616232546-dcrkmnhd4yhq6mvd
* plugins.d/mandos-client (mandos_context): Moved to inside "main()".
                                            All users changed.
  (add_server): Take new "current_server" argument; all callers
                changed.
  (init_gpgme, pgp_packet_decrypt, init_gnutls_global,
  init_gnutls_session, start_mandos_communication,
  avahi_loop_with_timeout): Take new "mc" argument"; all callers
                            changed.
  (resolve_callback, browse_callback): Handle void pointer userdata as
                                       "mc", a pointer to
                                       mandos_context.  All callers
                                       changed.
  (main): New "mc" variable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
                                 */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
 
#include <assert.h>             /* assert() */
65
64
#include <errno.h>              /* perror(), errno,
66
65
                                   program_invocation_short_name */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
145
144
/* Doubly linked list that need to be circularly linked when used */
146
145
typedef struct server{
147
146
  const char *ip;
148
 
  uint16_t port;
 
147
  in_port_t port;
149
148
  AvahiIfIndex if_index;
150
149
  int af;
151
150
  struct timespec last_seen;
155
154
 
156
155
/* Used for passing in values through the Avahi callback functions */
157
156
typedef struct {
158
 
  AvahiSimplePoll *simple_poll;
159
157
  AvahiServer *server;
160
158
  gnutls_certificate_credentials_t cred;
161
159
  unsigned int dh_bits;
165
163
  server *current_server;
166
164
} mandos_context;
167
165
 
168
 
/* global context so signal handler can reach it*/
169
 
mandos_context mc = { .simple_poll = NULL, .server = NULL,
170
 
                      .dh_bits = 1024, .priority = "SECURE256"
171
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
172
 
                      .current_server = NULL };
 
166
/* global so signal handler can reach it*/
 
167
AvahiSimplePoll *simple_poll;
173
168
 
174
169
sig_atomic_t quit_now = 0;
175
170
int signal_received = 0;
211
206
}
212
207
 
213
208
/* Add server to set of servers to retry periodically */
214
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
215
 
                int af){
 
209
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
 
210
                int af, server **current_server){
216
211
  int ret;
217
212
  server *new_server = malloc(sizeof(server));
218
213
  if(new_server == NULL){
228
223
    return false;
229
224
  }
230
225
  /* Special case of first server */
231
 
  if (mc.current_server == NULL){
 
226
  if(*current_server == NULL){
232
227
    new_server->next = new_server;
233
228
    new_server->prev = new_server;
234
 
    mc.current_server = new_server;
 
229
    *current_server = new_server;
235
230
  /* Place the new server last in the list */
236
231
  } else {
237
 
    new_server->next = mc.current_server;
238
 
    new_server->prev = mc.current_server->prev;
 
232
    new_server->next = *current_server;
 
233
    new_server->prev = (*current_server)->prev;
239
234
    new_server->prev->next = new_server;
240
 
    mc.current_server->prev = new_server;
 
235
    (*current_server)->prev = new_server;
241
236
  }
242
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
 
237
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
243
238
  if(ret == -1){
244
239
    perror_plus("clock_gettime");
245
240
    return false;
251
246
 * Initialize GPGME.
252
247
 */
253
248
static bool init_gpgme(const char *seckey, const char *pubkey,
254
 
                       const char *tempdir){
 
249
                       const char *tempdir, mandos_context *mc){
255
250
  gpgme_error_t rc;
256
251
  gpgme_engine_info_t engine_info;
257
252
  
258
 
  
259
253
  /*
260
254
   * Helper function to insert pub and seckey to the engine keyring.
261
255
   */
277
271
      return false;
278
272
    }
279
273
    
280
 
    rc = gpgme_op_import(mc.ctx, pgp_data);
 
274
    rc = gpgme_op_import(mc->ctx, pgp_data);
281
275
    if(rc != GPG_ERR_NO_ERROR){
282
276
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
283
277
                   gpgme_strsource(rc), gpgme_strerror(rc));
327
321
  }
328
322
  
329
323
  /* Create new GPGME "context" */
330
 
  rc = gpgme_new(&(mc.ctx));
 
324
  rc = gpgme_new(&(mc->ctx));
331
325
  if(rc != GPG_ERR_NO_ERROR){
332
326
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
333
327
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
348
342
 */
349
343
static ssize_t pgp_packet_decrypt(const char *cryptotext,
350
344
                                  size_t crypto_size,
351
 
                                  char **plaintext){
 
345
                                  char **plaintext,
 
346
                                  mandos_context *mc){
352
347
  gpgme_data_t dh_crypto, dh_plain;
353
348
  gpgme_error_t rc;
354
349
  ssize_t ret;
380
375
  
381
376
  /* Decrypt data from the cryptotext data buffer to the plaintext
382
377
     data buffer */
383
 
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
 
378
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
384
379
  if(rc != GPG_ERR_NO_ERROR){
385
380
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
386
381
                 gpgme_strsource(rc), gpgme_strerror(rc));
387
382
    plaintext_length = -1;
388
383
    if(debug){
389
384
      gpgme_decrypt_result_t result;
390
 
      result = gpgme_op_decrypt_result(mc.ctx);
 
385
      result = gpgme_op_decrypt_result(mc->ctx);
391
386
      if(result == NULL){
392
387
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
393
388
      } else {
471
466
}
472
467
 
473
468
static const char * safer_gnutls_strerror(int value){
474
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
475
 
                                               -Wunreachable-code */
 
469
  const char *ret = gnutls_strerror(value);
476
470
  if(ret == NULL)
477
471
    ret = "(unknown)";
478
472
  return ret;
485
479
}
486
480
 
487
481
static int init_gnutls_global(const char *pubkeyfilename,
488
 
                              const char *seckeyfilename){
 
482
                              const char *seckeyfilename,
 
483
                              mandos_context *mc){
489
484
  int ret;
490
485
  
491
486
  if(debug){
508
503
  }
509
504
  
510
505
  /* OpenPGP credentials */
511
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
506
  ret = gnutls_certificate_allocate_credentials(&mc->cred);
512
507
  if(ret != GNUTLS_E_SUCCESS){
513
508
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
514
509
                 safer_gnutls_strerror(ret));
524
519
  }
525
520
  
526
521
  ret = gnutls_certificate_set_openpgp_key_file
527
 
    (mc.cred, pubkeyfilename, seckeyfilename,
 
522
    (mc->cred, pubkeyfilename, seckeyfilename,
528
523
     GNUTLS_OPENPGP_FMT_BASE64);
529
524
  if(ret != GNUTLS_E_SUCCESS){
530
525
    fprintf_plus(stderr,
536
531
  }
537
532
  
538
533
  /* GnuTLS server initialization */
539
 
  ret = gnutls_dh_params_init(&mc.dh_params);
 
534
  ret = gnutls_dh_params_init(&mc->dh_params);
540
535
  if(ret != GNUTLS_E_SUCCESS){
541
536
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
542
537
                 " initialization: %s\n",
543
538
                 safer_gnutls_strerror(ret));
544
539
    goto globalfail;
545
540
  }
546
 
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
 
541
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
547
542
  if(ret != GNUTLS_E_SUCCESS){
548
543
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
549
544
                 safer_gnutls_strerror(ret));
550
545
    goto globalfail;
551
546
  }
552
547
  
553
 
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
 
548
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
554
549
  
555
550
  return 0;
556
551
  
557
552
 globalfail:
558
553
  
559
 
  gnutls_certificate_free_credentials(mc.cred);
 
554
  gnutls_certificate_free_credentials(mc->cred);
560
555
  gnutls_global_deinit();
561
 
  gnutls_dh_params_deinit(mc.dh_params);
 
556
  gnutls_dh_params_deinit(mc->dh_params);
562
557
  return -1;
563
558
}
564
559
 
565
 
static int init_gnutls_session(gnutls_session_t *session){
 
560
static int init_gnutls_session(gnutls_session_t *session,
 
561
                               mandos_context *mc){
566
562
  int ret;
567
563
  /* GnuTLS session creation */
568
564
  do {
580
576
  {
581
577
    const char *err;
582
578
    do {
583
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
579
      ret = gnutls_priority_set_direct(*session, mc->priority, &err);
584
580
      if(quit_now){
585
581
        gnutls_deinit(*session);
586
582
        return -1;
597
593
  
598
594
  do {
599
595
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
600
 
                                 mc.cred);
 
596
                                 mc->cred);
601
597
    if(quit_now){
602
598
      gnutls_deinit(*session);
603
599
      return -1;
613
609
  /* ignore client certificate if any. */
614
610
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
615
611
  
616
 
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
 
612
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
617
613
  
618
614
  return 0;
619
615
}
623
619
                      __attribute__((unused)) const char *txt){}
624
620
 
625
621
/* Called when a Mandos server is found */
626
 
static int start_mandos_communication(const char *ip, uint16_t port,
 
622
static int start_mandos_communication(const char *ip, in_port_t port,
627
623
                                      AvahiIfIndex if_index,
628
 
                                      int af){
 
624
                                      int af, mandos_context *mc){
629
625
  int ret, tcp_sd = -1;
630
626
  ssize_t sret;
631
627
  union {
661
657
    return -1;
662
658
  }
663
659
  
664
 
  ret = init_gnutls_session(&session);
 
660
  ret = init_gnutls_session(&session, mc);
665
661
  if(ret != 0){
666
662
    return -1;
667
663
  }
668
664
  
669
665
  if(debug){
670
666
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
671
 
                 PRIu16 "\n", ip, port);
 
667
                 PRIuMAX "\n", ip, (uintmax_t)port);
672
668
  }
673
669
  
674
670
  tcp_sd = socket(pf, SOCK_STREAM, 0);
705
701
    goto mandos_end;
706
702
  }
707
703
  if(af == AF_INET6){
708
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
709
 
                                       -Wconversion and
710
 
                                       -Wunreachable-code */
711
 
    
 
704
    to.in6.sin6_port = htons(port);    
712
705
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
713
706
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
714
707
                                -Wunreachable-code*/
738
731
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
739
732
        perror_plus("if_indextoname");
740
733
      } else {
741
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
742
 
                     "\n", ip, interface, port);
 
734
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
 
735
                     "\n", ip, interface, (uintmax_t)port);
743
736
      }
744
737
    } else {
745
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
746
 
                   ip, port);
 
738
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
 
739
                   ip, (uintmax_t)port);
747
740
    }
748
741
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
749
742
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
942
935
  if(buffer_length > 0){
943
936
    ssize_t decrypted_buffer_size;
944
937
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
945
 
                                               &decrypted_buffer);
 
938
                                               &decrypted_buffer, mc);
946
939
    if(decrypted_buffer_size >= 0){
947
940
      
948
941
      written = 0;
1009
1002
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1010
1003
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1011
1004
                             flags,
1012
 
                             AVAHI_GCC_UNUSED void* userdata){
1013
 
  assert(r);
 
1005
                             void* mc){
 
1006
  if(r == NULL){
 
1007
    return;
 
1008
  }
1014
1009
  
1015
1010
  /* Called whenever a service has been resolved successfully or
1016
1011
     timed out */
1025
1020
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1026
1021
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
1027
1022
                 domain,
1028
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1023
                 avahi_strerror(avahi_server_errno
 
1024
                                (((mandos_context*)mc)->server)));
1029
1025
    break;
1030
1026
    
1031
1027
  case AVAHI_RESOLVER_FOUND:
1037
1033
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1038
1034
                     host_name, ip, (intmax_t)interface, port);
1039
1035
      }
1040
 
      int ret = start_mandos_communication(ip, port, interface,
1041
 
                                           avahi_proto_to_af(proto));
 
1036
      int ret = start_mandos_communication(ip, (in_port_t)port,
 
1037
                                           interface,
 
1038
                                           avahi_proto_to_af(proto),
 
1039
                                           mc);
1042
1040
      if(ret == 0){
1043
 
        avahi_simple_poll_quit(mc.simple_poll);
 
1041
        avahi_simple_poll_quit(simple_poll);
1044
1042
      } else {
1045
 
        if(not add_server(ip, port, interface,
1046
 
                          avahi_proto_to_af(proto))){
 
1043
        if(not add_server(ip, (in_port_t)port, interface,
 
1044
                          avahi_proto_to_af(proto),
 
1045
                          &((mandos_context*)mc)->current_server)){
1047
1046
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1048
1047
                       " list\n", name);
1049
1048
        }
1062
1061
                            const char *domain,
1063
1062
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1064
1063
                            flags,
1065
 
                            AVAHI_GCC_UNUSED void* userdata){
1066
 
  assert(b);
 
1064
                            void* mc){
 
1065
  if(b == NULL){
 
1066
    return;
 
1067
  }
1067
1068
  
1068
1069
  /* Called whenever a new services becomes available on the LAN or
1069
1070
     is removed from the LAN */
1077
1078
  case AVAHI_BROWSER_FAILURE:
1078
1079
    
1079
1080
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1080
 
                 avahi_strerror(avahi_server_errno(mc.server)));
1081
 
    avahi_simple_poll_quit(mc.simple_poll);
 
1081
                 avahi_strerror(avahi_server_errno
 
1082
                                (((mandos_context*)mc)->server)));
 
1083
    avahi_simple_poll_quit(simple_poll);
1082
1084
    return;
1083
1085
    
1084
1086
  case AVAHI_BROWSER_NEW:
1087
1089
       the callback function is called the Avahi server will free the
1088
1090
       resolver for us. */
1089
1091
    
1090
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1091
 
                                    name, type, domain, protocol, 0,
1092
 
                                    resolve_callback, NULL) == NULL)
 
1092
    if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
 
1093
                                    interface, protocol, name, type,
 
1094
                                    domain, protocol, 0,
 
1095
                                    resolve_callback, mc) == NULL)
1093
1096
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1094
1097
                   " %s\n", name,
1095
 
                   avahi_strerror(avahi_server_errno(mc.server)));
 
1098
                   avahi_strerror(avahi_server_errno
 
1099
                                  (((mandos_context*)mc)->server)));
1096
1100
    break;
1097
1101
    
1098
1102
  case AVAHI_BROWSER_REMOVE:
1117
1121
  signal_received = sig;
1118
1122
  int old_errno = errno;
1119
1123
  /* set main loop to exit */
1120
 
  if(mc.simple_poll != NULL){
1121
 
    avahi_simple_poll_quit(mc.simple_poll);
 
1124
  if(simple_poll != NULL){
 
1125
    avahi_simple_poll_quit(simple_poll);
1122
1126
  }
1123
1127
  errno = old_errno;
1124
1128
}
1318
1322
  return 1;
1319
1323
}
1320
1324
 
1321
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
 
1325
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
 
1326
                            mandos_context *mc){
1322
1327
  int ret;
1323
1328
  struct timespec now;
1324
1329
  struct timespec waited_time;
1325
1330
  intmax_t block_time;
1326
1331
  
1327
1332
  while(true){
1328
 
    if(mc.current_server == NULL){
 
1333
    if(mc->current_server == NULL){
1329
1334
      if (debug){
1330
1335
        fprintf_plus(stderr, "Wait until first server is found."
1331
1336
                     " No timeout!\n");
1345
1350
      /* Calculating in ms how long time between now and server
1346
1351
         who we visted longest time ago. Now - last seen.  */
1347
1352
      waited_time.tv_sec = (now.tv_sec
1348
 
                            - mc.current_server->last_seen.tv_sec);
 
1353
                            - mc->current_server->last_seen.tv_sec);
1349
1354
      waited_time.tv_nsec = (now.tv_nsec
1350
 
                             - mc.current_server->last_seen.tv_nsec);
 
1355
                             - mc->current_server->last_seen.tv_nsec);
1351
1356
      /* total time is 10s/10,000ms.
1352
1357
         Converting to s from ms by dividing by 1,000,
1353
1358
         and ns to ms by dividing by 1,000,000. */
1361
1366
      }
1362
1367
      
1363
1368
      if(block_time <= 0){
1364
 
        ret = start_mandos_communication(mc.current_server->ip,
1365
 
                                         mc.current_server->port,
1366
 
                                         mc.current_server->if_index,
1367
 
                                         mc.current_server->af);
 
1369
        ret = start_mandos_communication(mc->current_server->ip,
 
1370
                                         mc->current_server->port,
 
1371
                                         mc->current_server->if_index,
 
1372
                                         mc->current_server->af, mc);
1368
1373
        if(ret == 0){
1369
 
          avahi_simple_poll_quit(mc.simple_poll);
 
1374
          avahi_simple_poll_quit(s);
1370
1375
          return 0;
1371
1376
        }
1372
1377
        ret = clock_gettime(CLOCK_MONOTONIC,
1373
 
                            &mc.current_server->last_seen);
 
1378
                            &mc->current_server->last_seen);
1374
1379
        if(ret == -1){
1375
1380
          perror_plus("clock_gettime");
1376
1381
          return -1;
1377
1382
        }
1378
 
        mc.current_server = mc.current_server->next;
 
1383
        mc->current_server = mc->current_server->next;
1379
1384
        block_time = 0;         /* Call avahi to find new Mandos
1380
1385
                                   servers, but don't block */
1381
1386
      }
1450
1455
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1451
1456
                         alphasort);
1452
1457
  if(numhooks == -1){
1453
 
    perror_plus("scandir");
 
1458
    if(errno == ENOENT){
 
1459
      if(debug){
 
1460
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1461
                     " found\n", hookdir);
 
1462
      }
 
1463
    } else {
 
1464
      perror_plus("scandir");
 
1465
    }
1454
1466
  } else {
1455
1467
    int devnull = open("/dev/null", O_RDONLY);
1456
1468
    for(int i = 0; i < numhooks; i++){
1741
1753
}
1742
1754
 
1743
1755
int main(int argc, char *argv[]){
 
1756
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
 
1757
                        .priority = "SECURE256:!CTYPE-X.509:"
 
1758
                        "+CTYPE-OPENPGP", .current_server = NULL };
1744
1759
  AvahiSServiceBrowser *sb = NULL;
1745
1760
  error_t ret_errno;
1746
1761
  int ret;
2014
2029
  
2015
2030
  /* Run network hooks */
2016
2031
  {
2017
 
    ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
 
                            interfaces, interfaces_size);
2019
 
    if(ret_errno != 0){
2020
 
      errno = ret_errno;
2021
 
      perror_plus("argz_append");
2022
 
      goto end;
 
2032
    
 
2033
    if(interfaces != NULL){
 
2034
      interfaces_hooks = malloc(interfaces_size);
 
2035
      if(interfaces_hooks == NULL){
 
2036
        perror_plus("malloc");
 
2037
        goto end;
 
2038
      }
 
2039
      memcpy(interfaces_hooks, interfaces, interfaces_size);
 
2040
      interfaces_hooks_size = interfaces_size;
 
2041
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2042
                     (int)',');
2023
2043
    }
2024
 
    argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
 
    if(not run_network_hooks("start", interfaces_hooks, delay)){
 
2044
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2045
                             interfaces_hooks : "", delay)){
2026
2046
      goto end;
2027
2047
    }
2028
2048
  }
2035
2055
     from the signal handler */
2036
2056
  /* Initialize the pseudo-RNG for Avahi */
2037
2057
  srand((unsigned int) time(NULL));
2038
 
  mc.simple_poll = avahi_simple_poll_new();
2039
 
  if(mc.simple_poll == NULL){
 
2058
  simple_poll = avahi_simple_poll_new();
 
2059
  if(simple_poll == NULL){
2040
2060
    fprintf_plus(stderr,
2041
2061
                 "Avahi: Failed to create simple poll object.\n");
2042
2062
    exitcode = EX_UNAVAILABLE;
2175
2195
    goto end;
2176
2196
  }
2177
2197
  
2178
 
  ret = init_gnutls_global(pubkey, seckey);
 
2198
  ret = init_gnutls_global(pubkey, seckey, &mc);
2179
2199
  if(ret == -1){
2180
2200
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2181
2201
    exitcode = EX_UNAVAILABLE;
2198
2218
    goto end;
2199
2219
  }
2200
2220
  
2201
 
  if(not init_gpgme(pubkey, seckey, tempdir)){
 
2221
  if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2202
2222
    fprintf_plus(stderr, "init_gpgme failed\n");
2203
2223
    exitcode = EX_UNAVAILABLE;
2204
2224
    goto end;
2225
2245
      goto end;
2226
2246
    }
2227
2247
    
2228
 
    uint16_t port;
 
2248
    in_port_t port;
2229
2249
    errno = 0;
2230
2250
    tmpmax = strtoimax(address+1, &tmp, 10);
2231
2251
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2232
 
       or tmpmax != (uint16_t)tmpmax){
 
2252
       or tmpmax != (in_port_t)tmpmax){
2233
2253
      fprintf_plus(stderr, "Bad port number\n");
2234
2254
      exitcode = EX_USAGE;
2235
2255
      goto end;
2239
2259
      goto end;
2240
2260
    }
2241
2261
    
2242
 
    port = (uint16_t)tmpmax;
 
2262
    port = (in_port_t)tmpmax;
2243
2263
    *address = '\0';
2244
2264
    /* Colon in address indicates IPv6 */
2245
2265
    int af;
2261
2281
    }
2262
2282
    
2263
2283
    while(not quit_now){
2264
 
      ret = start_mandos_communication(address, port, if_index, af);
 
2284
      ret = start_mandos_communication(address, port, if_index, af,
 
2285
                                       &mc);
2265
2286
      if(quit_now or ret == 0){
2266
2287
        break;
2267
2288
      }
2293
2314
    config.publish_domain = 0;
2294
2315
    
2295
2316
    /* Allocate a new server */
2296
 
    mc.server = avahi_server_new(avahi_simple_poll_get
2297
 
                                 (mc.simple_poll), &config, NULL,
2298
 
                                 NULL, &ret_errno);
 
2317
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
 
2318
                                 &config, NULL, NULL, &ret_errno);
2299
2319
    
2300
2320
    /* Free the Avahi configuration data */
2301
2321
    avahi_server_config_free(&config);
2316
2336
  /* Create the Avahi service browser */
2317
2337
  sb = avahi_s_service_browser_new(mc.server, if_index,
2318
2338
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2319
 
                                   NULL, 0, browse_callback, NULL);
 
2339
                                   NULL, 0, browse_callback,
 
2340
                                   (void *)&mc);
2320
2341
  if(sb == NULL){
2321
2342
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2322
2343
                 avahi_strerror(avahi_server_errno(mc.server)));
2334
2355
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2335
2356
  }
2336
2357
 
2337
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
2338
 
                                (int)(retry_interval * 1000));
 
2358
  ret = avahi_loop_with_timeout(simple_poll,
 
2359
                                (int)(retry_interval * 1000), &mc);
2339
2360
  if(debug){
2340
2361
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2341
2362
                 (ret == 0) ? "successfully" : "with error");
2354
2375
  if(mc.server != NULL)
2355
2376
    avahi_server_free(mc.server);
2356
2377
  
2357
 
  if(mc.simple_poll != NULL)
2358
 
    avahi_simple_poll_free(mc.simple_poll);
 
2378
  if(simple_poll != NULL)
 
2379
    avahi_simple_poll_free(simple_poll);
2359
2380
  
2360
2381
  if(gnutls_initialized){
2361
2382
    gnutls_certificate_free_credentials(mc.cred);
2383
2404
    raise_privileges();
2384
2405
    
2385
2406
    /* Run network hooks */
2386
 
    run_network_hooks("stop", interfaces_hooks, delay);
 
2407
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2408
                      interfaces_hooks : "", delay);
2387
2409
    
2388
2410
    /* Take down the network interfaces which were brought up */
2389
2411
    {