/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-14 16:11:26 UTC
  • mfrom: (594.1.4 client-all-interfaces)
  • Revision ID: teddy@recompile.se-20120614161126-6xkpzbyddk3jm0zt
* plugins.d/mandos-client.c: Use all interfaces.

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