/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

* 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;
161
163
  const char *priority;
162
164
  gpgme_ctx_t ctx;
163
165
  server *current_server;
164
 
  char *interfaces;
165
 
  size_t interfaces_size;
166
166
} mandos_context;
167
167
 
168
 
/* global so signal handler can reach it*/
169
 
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 };
170
173
 
171
174
sig_atomic_t quit_now = 0;
172
175
int signal_received = 0;
208
211
}
209
212
 
210
213
/* Add server to set of servers to retry periodically */
211
 
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
212
 
                int af, server **current_server){
 
214
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
215
                int af){
213
216
  int ret;
214
217
  server *new_server = malloc(sizeof(server));
215
218
  if(new_server == NULL){
225
228
    return false;
226
229
  }
227
230
  /* Special case of first server */
228
 
  if(*current_server == NULL){
 
231
  if (mc.current_server == NULL){
229
232
    new_server->next = new_server;
230
233
    new_server->prev = new_server;
231
 
    *current_server = new_server;
 
234
    mc.current_server = new_server;
232
235
  /* Place the new server last in the list */
233
236
  } else {
234
 
    new_server->next = *current_server;
235
 
    new_server->prev = (*current_server)->prev;
 
237
    new_server->next = mc.current_server;
 
238
    new_server->prev = mc.current_server->prev;
236
239
    new_server->prev->next = new_server;
237
 
    (*current_server)->prev = new_server;
 
240
    mc.current_server->prev = new_server;
238
241
  }
239
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
 
242
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
240
243
  if(ret == -1){
241
244
    perror_plus("clock_gettime");
242
245
    return false;
248
251
 * Initialize GPGME.
249
252
 */
250
253
static bool init_gpgme(const char *seckey, const char *pubkey,
251
 
                       const char *tempdir, mandos_context *mc){
 
254
                       const char *tempdir){
252
255
  gpgme_error_t rc;
253
256
  gpgme_engine_info_t engine_info;
254
257
  
 
258
  
255
259
  /*
256
260
   * Helper function to insert pub and seckey to the engine keyring.
257
261
   */
273
277
      return false;
274
278
    }
275
279
    
276
 
    rc = gpgme_op_import(mc->ctx, pgp_data);
 
280
    rc = gpgme_op_import(mc.ctx, pgp_data);
277
281
    if(rc != GPG_ERR_NO_ERROR){
278
282
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
279
283
                   gpgme_strsource(rc), gpgme_strerror(rc));
323
327
  }
324
328
  
325
329
  /* Create new GPGME "context" */
326
 
  rc = gpgme_new(&(mc->ctx));
 
330
  rc = gpgme_new(&(mc.ctx));
327
331
  if(rc != GPG_ERR_NO_ERROR){
328
332
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
329
333
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
344
348
 */
345
349
static ssize_t pgp_packet_decrypt(const char *cryptotext,
346
350
                                  size_t crypto_size,
347
 
                                  char **plaintext,
348
 
                                  mandos_context *mc){
 
351
                                  char **plaintext){
349
352
  gpgme_data_t dh_crypto, dh_plain;
350
353
  gpgme_error_t rc;
351
354
  ssize_t ret;
377
380
  
378
381
  /* Decrypt data from the cryptotext data buffer to the plaintext
379
382
     data buffer */
380
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
383
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
381
384
  if(rc != GPG_ERR_NO_ERROR){
382
385
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
383
386
                 gpgme_strsource(rc), gpgme_strerror(rc));
384
387
    plaintext_length = -1;
385
388
    if(debug){
386
389
      gpgme_decrypt_result_t result;
387
 
      result = gpgme_op_decrypt_result(mc->ctx);
 
390
      result = gpgme_op_decrypt_result(mc.ctx);
388
391
      if(result == NULL){
389
392
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
390
393
      } else {
468
471
}
469
472
 
470
473
static const char * safer_gnutls_strerror(int value){
471
 
  const char *ret = gnutls_strerror(value);
 
474
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
475
                                               -Wunreachable-code */
472
476
  if(ret == NULL)
473
477
    ret = "(unknown)";
474
478
  return ret;
481
485
}
482
486
 
483
487
static int init_gnutls_global(const char *pubkeyfilename,
484
 
                              const char *seckeyfilename,
485
 
                              mandos_context *mc){
 
488
                              const char *seckeyfilename){
486
489
  int ret;
487
490
  
488
491
  if(debug){
505
508
  }
506
509
  
507
510
  /* OpenPGP credentials */
508
 
  ret = gnutls_certificate_allocate_credentials(&mc->cred);
 
511
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
509
512
  if(ret != GNUTLS_E_SUCCESS){
510
513
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
511
514
                 safer_gnutls_strerror(ret));
521
524
  }
522
525
  
523
526
  ret = gnutls_certificate_set_openpgp_key_file
524
 
    (mc->cred, pubkeyfilename, seckeyfilename,
 
527
    (mc.cred, pubkeyfilename, seckeyfilename,
525
528
     GNUTLS_OPENPGP_FMT_BASE64);
526
529
  if(ret != GNUTLS_E_SUCCESS){
527
530
    fprintf_plus(stderr,
533
536
  }
534
537
  
535
538
  /* GnuTLS server initialization */
536
 
  ret = gnutls_dh_params_init(&mc->dh_params);
 
539
  ret = gnutls_dh_params_init(&mc.dh_params);
537
540
  if(ret != GNUTLS_E_SUCCESS){
538
541
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
539
542
                 " initialization: %s\n",
540
543
                 safer_gnutls_strerror(ret));
541
544
    goto globalfail;
542
545
  }
543
 
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
 
546
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
544
547
  if(ret != GNUTLS_E_SUCCESS){
545
548
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
546
549
                 safer_gnutls_strerror(ret));
547
550
    goto globalfail;
548
551
  }
549
552
  
550
 
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
 
553
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
551
554
  
552
555
  return 0;
553
556
  
554
557
 globalfail:
555
558
  
556
 
  gnutls_certificate_free_credentials(mc->cred);
 
559
  gnutls_certificate_free_credentials(mc.cred);
557
560
  gnutls_global_deinit();
558
 
  gnutls_dh_params_deinit(mc->dh_params);
 
561
  gnutls_dh_params_deinit(mc.dh_params);
559
562
  return -1;
560
563
}
561
564
 
562
 
static int init_gnutls_session(gnutls_session_t *session,
563
 
                               mandos_context *mc){
 
565
static int init_gnutls_session(gnutls_session_t *session){
564
566
  int ret;
565
567
  /* GnuTLS session creation */
566
568
  do {
578
580
  {
579
581
    const char *err;
580
582
    do {
581
 
      ret = gnutls_priority_set_direct(*session, mc->priority, &err);
 
583
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
582
584
      if(quit_now){
583
585
        gnutls_deinit(*session);
584
586
        return -1;
595
597
  
596
598
  do {
597
599
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
598
 
                                 mc->cred);
 
600
                                 mc.cred);
599
601
    if(quit_now){
600
602
      gnutls_deinit(*session);
601
603
      return -1;
611
613
  /* ignore client certificate if any. */
612
614
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
613
615
  
614
 
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
 
616
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
615
617
  
616
618
  return 0;
617
619
}
621
623
                      __attribute__((unused)) const char *txt){}
622
624
 
623
625
/* Called when a Mandos server is found */
624
 
static int start_mandos_communication(const char *ip, in_port_t port,
 
626
static int start_mandos_communication(const char *ip, uint16_t port,
625
627
                                      AvahiIfIndex if_index,
626
 
                                      int af, mandos_context *mc){
 
628
                                      int af){
627
629
  int ret, tcp_sd = -1;
628
630
  ssize_t sret;
629
631
  union {
659
661
    return -1;
660
662
  }
661
663
  
662
 
  if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
663
 
    /* Check if the interface is one of the interfaces we are using */
664
 
    bool match = false;
665
 
    {
666
 
      char *interface = NULL;
667
 
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
668
 
                                 interface))){
669
 
        if(if_nametoindex(interface) == (unsigned int)if_index){
670
 
          match = true;
671
 
          break;
672
 
        }
673
 
      }
674
 
    }
675
 
    if(not match){
676
 
      if(debug){
677
 
        char interface[IF_NAMESIZE];
678
 
        if(if_indextoname((unsigned int)if_index, interface) == NULL){
679
 
          perror_plus("if_indextoname");
680
 
        } else {
681
 
          fprintf_plus(stderr, "Skipping server on non-used interface"
682
 
                       " \"%s\"\n",
683
 
                       if_indextoname((unsigned int)if_index,
684
 
                                      interface));
685
 
        }
686
 
      }
687
 
      return -1;
688
 
    }
689
 
  }
690
 
  
691
 
  ret = init_gnutls_session(&session, mc);
 
664
  ret = init_gnutls_session(&session);
692
665
  if(ret != 0){
693
666
    return -1;
694
667
  }
695
668
  
696
669
  if(debug){
697
670
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
698
 
                 PRIuMAX "\n", ip, (uintmax_t)port);
 
671
                 PRIu16 "\n", ip, port);
699
672
  }
700
673
  
701
674
  tcp_sd = socket(pf, SOCK_STREAM, 0);
732
705
    goto mandos_end;
733
706
  }
734
707
  if(af == AF_INET6){
735
 
    to.in6.sin6_port = htons(port);    
 
708
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
709
                                       -Wconversion and
 
710
                                       -Wunreachable-code */
 
711
    
736
712
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
737
713
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
738
714
                                -Wunreachable-code*/
762
738
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
763
739
        perror_plus("if_indextoname");
764
740
      } else {
765
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
766
 
                     "\n", ip, interface, (uintmax_t)port);
 
741
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
742
                     "\n", ip, interface, port);
767
743
      }
768
744
    } else {
769
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
770
 
                   ip, (uintmax_t)port);
 
745
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
746
                   ip, port);
771
747
    }
772
748
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
773
749
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
966
942
  if(buffer_length > 0){
967
943
    ssize_t decrypted_buffer_size;
968
944
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
969
 
                                               &decrypted_buffer, mc);
 
945
                                               &decrypted_buffer);
970
946
    if(decrypted_buffer_size >= 0){
971
947
      
972
948
      written = 0;
1033
1009
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1034
1010
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1035
1011
                             flags,
1036
 
                             void* mc){
1037
 
  if(r == NULL){
1038
 
    return;
1039
 
  }
 
1012
                             AVAHI_GCC_UNUSED void* userdata){
 
1013
  assert(r);
1040
1014
  
1041
1015
  /* Called whenever a service has been resolved successfully or
1042
1016
     timed out */
1051
1025
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1052
1026
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
1053
1027
                 domain,
1054
 
                 avahi_strerror(avahi_server_errno
1055
 
                                (((mandos_context*)mc)->server)));
 
1028
                 avahi_strerror(avahi_server_errno(mc.server)));
1056
1029
    break;
1057
1030
    
1058
1031
  case AVAHI_RESOLVER_FOUND:
1064
1037
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1065
1038
                     host_name, ip, (intmax_t)interface, port);
1066
1039
      }
1067
 
      int ret = start_mandos_communication(ip, (in_port_t)port,
1068
 
                                           interface,
1069
 
                                           avahi_proto_to_af(proto),
1070
 
                                           mc);
 
1040
      int ret = start_mandos_communication(ip, port, interface,
 
1041
                                           avahi_proto_to_af(proto));
1071
1042
      if(ret == 0){
1072
 
        avahi_simple_poll_quit(simple_poll);
 
1043
        avahi_simple_poll_quit(mc.simple_poll);
1073
1044
      } else {
1074
 
        if(not add_server(ip, (in_port_t)port, interface,
1075
 
                          avahi_proto_to_af(proto),
1076
 
                          &((mandos_context*)mc)->current_server)){
 
1045
        if(not add_server(ip, port, interface,
 
1046
                          avahi_proto_to_af(proto))){
1077
1047
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1078
1048
                       " list\n", name);
1079
1049
        }
1092
1062
                            const char *domain,
1093
1063
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1094
1064
                            flags,
1095
 
                            void* mc){
1096
 
  if(b == NULL){
1097
 
    return;
1098
 
  }
 
1065
                            AVAHI_GCC_UNUSED void* userdata){
 
1066
  assert(b);
1099
1067
  
1100
1068
  /* Called whenever a new services becomes available on the LAN or
1101
1069
     is removed from the LAN */
1109
1077
  case AVAHI_BROWSER_FAILURE:
1110
1078
    
1111
1079
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1112
 
                 avahi_strerror(avahi_server_errno
1113
 
                                (((mandos_context*)mc)->server)));
1114
 
    avahi_simple_poll_quit(simple_poll);
 
1080
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1081
    avahi_simple_poll_quit(mc.simple_poll);
1115
1082
    return;
1116
1083
    
1117
1084
  case AVAHI_BROWSER_NEW:
1120
1087
       the callback function is called the Avahi server will free the
1121
1088
       resolver for us. */
1122
1089
    
1123
 
    if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
1124
 
                                    interface, protocol, name, type,
1125
 
                                    domain, protocol, 0,
1126
 
                                    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)
1127
1093
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1128
1094
                   " %s\n", name,
1129
 
                   avahi_strerror(avahi_server_errno
1130
 
                                  (((mandos_context*)mc)->server)));
 
1095
                   avahi_strerror(avahi_server_errno(mc.server)));
1131
1096
    break;
1132
1097
    
1133
1098
  case AVAHI_BROWSER_REMOVE:
1152
1117
  signal_received = sig;
1153
1118
  int old_errno = errno;
1154
1119
  /* set main loop to exit */
1155
 
  if(simple_poll != NULL){
1156
 
    avahi_simple_poll_quit(simple_poll);
 
1120
  if(mc.simple_poll != NULL){
 
1121
    avahi_simple_poll_quit(mc.simple_poll);
1157
1122
  }
1158
1123
  errno = old_errno;
1159
1124
}
1353
1318
  return 1;
1354
1319
}
1355
1320
 
1356
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1357
 
                            mandos_context *mc){
 
1321
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1358
1322
  int ret;
1359
1323
  struct timespec now;
1360
1324
  struct timespec waited_time;
1361
1325
  intmax_t block_time;
1362
1326
  
1363
1327
  while(true){
1364
 
    if(mc->current_server == NULL){
 
1328
    if(mc.current_server == NULL){
1365
1329
      if (debug){
1366
1330
        fprintf_plus(stderr, "Wait until first server is found."
1367
1331
                     " No timeout!\n");
1381
1345
      /* Calculating in ms how long time between now and server
1382
1346
         who we visted longest time ago. Now - last seen.  */
1383
1347
      waited_time.tv_sec = (now.tv_sec
1384
 
                            - mc->current_server->last_seen.tv_sec);
 
1348
                            - mc.current_server->last_seen.tv_sec);
1385
1349
      waited_time.tv_nsec = (now.tv_nsec
1386
 
                             - mc->current_server->last_seen.tv_nsec);
 
1350
                             - mc.current_server->last_seen.tv_nsec);
1387
1351
      /* total time is 10s/10,000ms.
1388
1352
         Converting to s from ms by dividing by 1,000,
1389
1353
         and ns to ms by dividing by 1,000,000. */
1397
1361
      }
1398
1362
      
1399
1363
      if(block_time <= 0){
1400
 
        ret = start_mandos_communication(mc->current_server->ip,
1401
 
                                         mc->current_server->port,
1402
 
                                         mc->current_server->if_index,
1403
 
                                         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);
1404
1368
        if(ret == 0){
1405
 
          avahi_simple_poll_quit(s);
 
1369
          avahi_simple_poll_quit(mc.simple_poll);
1406
1370
          return 0;
1407
1371
        }
1408
1372
        ret = clock_gettime(CLOCK_MONOTONIC,
1409
 
                            &mc->current_server->last_seen);
 
1373
                            &mc.current_server->last_seen);
1410
1374
        if(ret == -1){
1411
1375
          perror_plus("clock_gettime");
1412
1376
          return -1;
1413
1377
        }
1414
 
        mc->current_server = mc->current_server->next;
 
1378
        mc.current_server = mc.current_server->next;
1415
1379
        block_time = 0;         /* Call avahi to find new Mandos
1416
1380
                                   servers, but don't block */
1417
1381
      }
1486
1450
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1487
1451
                         alphasort);
1488
1452
  if(numhooks == -1){
1489
 
    if(errno == ENOENT){
1490
 
      if(debug){
1491
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
1492
 
                     " found\n", hookdir);
1493
 
      }
1494
 
    } else {
1495
 
      perror_plus("scandir");
1496
 
    }
 
1453
    perror_plus("scandir");
1497
1454
  } else {
1498
1455
    int devnull = open("/dev/null", O_RDONLY);
1499
1456
    for(int i = 0; i < numhooks; i++){
1784
1741
}
1785
1742
 
1786
1743
int main(int argc, char *argv[]){
1787
 
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1788
 
                        .priority = "SECURE256:!CTYPE-X.509:"
1789
 
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1790
 
                        .interfaces = NULL, .interfaces_size = 0 };
1791
1744
  AvahiSServiceBrowser *sb = NULL;
1792
1745
  error_t ret_errno;
1793
1746
  int ret;
1794
1747
  intmax_t tmpmax;
1795
1748
  char *tmp;
1796
1749
  int exitcode = EXIT_SUCCESS;
 
1750
  char *interfaces = NULL;
 
1751
  size_t interfaces_size = 0;
1797
1752
  char *interfaces_to_take_down = NULL;
1798
1753
  size_t interfaces_to_take_down_size = 0;
1799
1754
  char tempdir[] = "/tmp/mandosXXXXXX";
1899
1854
        connect_to = arg;
1900
1855
        break;
1901
1856
      case 'i':                 /* --interface */
1902
 
        ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1903
 
                                 arg, (int)',');
 
1857
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
 
1858
                                 (int)',');
1904
1859
        if(ret_errno != 0){
1905
1860
          argp_error(state, "%s", strerror(ret_errno));
1906
1861
        }
2033
1988
      }
2034
1989
    
2035
1990
      /* Lower privileges */
2036
 
      lower_privileges();
 
1991
      errno = 0;
 
1992
      ret = seteuid(uid);
 
1993
      if(ret == -1){
 
1994
        perror_plus("seteuid");
 
1995
      }
2037
1996
    }
2038
1997
  }
2039
1998
  
2040
 
  /* Remove invalid interface names (except "none") */
 
1999
  /* Remove empty interface names */
2041
2000
  {
2042
2001
    char *interface = NULL;
2043
 
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
 
2002
    while((interface = argz_next(interfaces, interfaces_size,
2044
2003
                                 interface))){
2045
 
      if(strcmp(interface, "none") != 0
2046
 
         and if_nametoindex(interface) == 0){
2047
 
        if(interface[0] != '\0'){
 
2004
      if(if_nametoindex(interface) == 0){
 
2005
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2048
2006
          fprintf_plus(stderr, "Not using nonexisting interface"
2049
2007
                       " \"%s\"\n", interface);
2050
2008
        }
2051
 
        argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
 
2009
        argz_delete(&interfaces, &interfaces_size, interface);
2052
2010
        interface = NULL;
2053
2011
      }
2054
2012
    }
2056
2014
  
2057
2015
  /* Run network hooks */
2058
2016
  {
2059
 
    if(mc.interfaces != NULL){
2060
 
      interfaces_hooks = malloc(mc.interfaces_size);
2061
 
      if(interfaces_hooks == NULL){
2062
 
        perror_plus("malloc");
2063
 
        goto end;
2064
 
      }
2065
 
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2066
 
      interfaces_hooks_size = mc.interfaces_size;
2067
 
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
2068
 
                     (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;
2069
2023
    }
2070
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2071
 
                             interfaces_hooks : "", delay)){
 
2024
    argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
 
2025
    if(not run_network_hooks("start", interfaces_hooks, delay)){
2072
2026
      goto end;
2073
2027
    }
2074
2028
  }
2081
2035
     from the signal handler */
2082
2036
  /* Initialize the pseudo-RNG for Avahi */
2083
2037
  srand((unsigned int) time(NULL));
2084
 
  simple_poll = avahi_simple_poll_new();
2085
 
  if(simple_poll == NULL){
 
2038
  mc.simple_poll = avahi_simple_poll_new();
 
2039
  if(mc.simple_poll == NULL){
2086
2040
    fprintf_plus(stderr,
2087
2041
                 "Avahi: Failed to create simple poll object.\n");
2088
2042
    exitcode = EX_UNAVAILABLE;
2153
2107
  }
2154
2108
  
2155
2109
  /* If no interfaces were specified, make a list */
2156
 
  if(mc.interfaces == NULL){
 
2110
  if(interfaces == NULL){
2157
2111
    struct dirent **direntries;
2158
2112
    /* Look for any good interfaces */
2159
2113
    ret = scandir(sys_class_net, &direntries, good_interface,
2161
2115
    if(ret >= 1){
2162
2116
      /* Add all found interfaces to interfaces list */
2163
2117
      for(int i = 0; i < ret; ++i){
2164
 
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
 
2118
        ret_errno = argz_add(&interfaces, &interfaces_size,
2165
2119
                             direntries[i]->d_name);
2166
2120
        if(ret_errno != 0){
2167
2121
          perror_plus("argz_add");
2181
2135
    }
2182
2136
  }
2183
2137
  
 
2138
  /* If we only got one interface, explicitly use only that one */
 
2139
  if(argz_count(interfaces, interfaces_size) == 1){
 
2140
    if(debug){
 
2141
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2142
                   interfaces);
 
2143
    }
 
2144
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
 
2145
  }
 
2146
  
2184
2147
  /* Bring up interfaces which are down */
2185
 
  {
 
2148
  if(not (argz_count(interfaces, interfaces_size) == 1
 
2149
          and strcmp(interfaces, "none") == 0)){
2186
2150
    char *interface = NULL;
2187
 
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
 
2151
    while((interface = argz_next(interfaces, interfaces_size,
2188
2152
                                 interface))){
2189
 
      /* If interface name is "none", stop bringing up interfaces.
2190
 
         Also remove all instances of "none" from the list */
2191
 
      if(strcmp(interface, "none") == 0){
2192
 
        argz_delete(&mc.interfaces, &mc.interfaces_size,
2193
 
                    interface);
2194
 
        interface = NULL;
2195
 
        while((interface = argz_next(mc.interfaces,
2196
 
                                     mc.interfaces_size, interface))){
2197
 
          if(strcmp(interface, "none") == 0){
2198
 
            argz_delete(&mc.interfaces, &mc.interfaces_size,
2199
 
                        interface);
2200
 
            interface = NULL;
2201
 
          }
2202
 
        }
2203
 
        break;
2204
 
      }
2205
2153
      bool interface_was_up = interface_is_up(interface);
2206
2154
      ret = bring_up_interface(interface, delay);
2207
2155
      if(not interface_was_up){
2215
2163
        }
2216
2164
      }
2217
2165
    }
 
2166
    free(interfaces);
 
2167
    interfaces = NULL;
 
2168
    interfaces_size = 0;
2218
2169
    if(debug and (interfaces_to_take_down == NULL)){
2219
2170
      fprintf_plus(stderr, "No interfaces were brought up\n");
2220
2171
    }
2221
2172
  }
2222
2173
  
2223
 
  /* If we only got one interface, explicitly use only that one */
2224
 
  if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2225
 
    if(debug){
2226
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2227
 
                   mc.interfaces);
2228
 
    }
2229
 
    if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2230
 
  }
2231
 
  
2232
2174
  if(quit_now){
2233
2175
    goto end;
2234
2176
  }
2235
2177
  
2236
 
  ret = init_gnutls_global(pubkey, seckey, &mc);
 
2178
  ret = init_gnutls_global(pubkey, seckey);
2237
2179
  if(ret == -1){
2238
2180
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2239
2181
    exitcode = EX_UNAVAILABLE;
2256
2198
    goto end;
2257
2199
  }
2258
2200
  
2259
 
  if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
 
2201
  if(not init_gpgme(pubkey, seckey, tempdir)){
2260
2202
    fprintf_plus(stderr, "init_gpgme failed\n");
2261
2203
    exitcode = EX_UNAVAILABLE;
2262
2204
    goto end;
2283
2225
      goto end;
2284
2226
    }
2285
2227
    
2286
 
    in_port_t port;
 
2228
    uint16_t port;
2287
2229
    errno = 0;
2288
2230
    tmpmax = strtoimax(address+1, &tmp, 10);
2289
2231
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2290
 
       or tmpmax != (in_port_t)tmpmax){
 
2232
       or tmpmax != (uint16_t)tmpmax){
2291
2233
      fprintf_plus(stderr, "Bad port number\n");
2292
2234
      exitcode = EX_USAGE;
2293
2235
      goto end;
2294
2236
    }
2295
 
    
 
2237
  
2296
2238
    if(quit_now){
2297
2239
      goto end;
2298
2240
    }
2299
2241
    
2300
 
    port = (in_port_t)tmpmax;
 
2242
    port = (uint16_t)tmpmax;
2301
2243
    *address = '\0';
2302
2244
    /* Colon in address indicates IPv6 */
2303
2245
    int af;
2319
2261
    }
2320
2262
    
2321
2263
    while(not quit_now){
2322
 
      ret = start_mandos_communication(address, port, if_index, af,
2323
 
                                       &mc);
 
2264
      ret = start_mandos_communication(address, port, if_index, af);
2324
2265
      if(quit_now or ret == 0){
2325
2266
        break;
2326
2267
      }
2352
2293
    config.publish_domain = 0;
2353
2294
    
2354
2295
    /* Allocate a new server */
2355
 
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
2356
 
                                 &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);
2357
2299
    
2358
2300
    /* Free the Avahi configuration data */
2359
2301
    avahi_server_config_free(&config);
2374
2316
  /* Create the Avahi service browser */
2375
2317
  sb = avahi_s_service_browser_new(mc.server, if_index,
2376
2318
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2377
 
                                   NULL, 0, browse_callback,
2378
 
                                   (void *)&mc);
 
2319
                                   NULL, 0, browse_callback, NULL);
2379
2320
  if(sb == NULL){
2380
2321
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2381
2322
                 avahi_strerror(avahi_server_errno(mc.server)));
2393
2334
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2394
2335
  }
2395
2336
 
2396
 
  ret = avahi_loop_with_timeout(simple_poll,
2397
 
                                (int)(retry_interval * 1000), &mc);
 
2337
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
2338
                                (int)(retry_interval * 1000));
2398
2339
  if(debug){
2399
2340
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2400
2341
                 (ret == 0) ? "successfully" : "with error");
2407
2348
  }
2408
2349
  
2409
2350
  /* Cleanup things */
2410
 
  free(mc.interfaces);
2411
 
  
2412
2351
  if(sb != NULL)
2413
2352
    avahi_s_service_browser_free(sb);
2414
2353
  
2415
2354
  if(mc.server != NULL)
2416
2355
    avahi_server_free(mc.server);
2417
2356
  
2418
 
  if(simple_poll != NULL)
2419
 
    avahi_simple_poll_free(simple_poll);
 
2357
  if(mc.simple_poll != NULL)
 
2358
    avahi_simple_poll_free(mc.simple_poll);
2420
2359
  
2421
2360
  if(gnutls_initialized){
2422
2361
    gnutls_certificate_free_credentials(mc.cred);
2444
2383
    raise_privileges();
2445
2384
    
2446
2385
    /* Run network hooks */
2447
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2448
 
                      interfaces_hooks : "", delay);
 
2386
    run_network_hooks("stop", interfaces_hooks, delay);
2449
2387
    
2450
2388
    /* Take down the network interfaces which were brought up */
2451
2389
    {