/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-23 00:58:49 UTC
  • Revision ID: teddy@recompile.se-20120623005849-02wj82cng433rt2k
* clients.conf: Convert all time intervals to new RFC 3339 syntax.
* mandos: All client options for time intervals now take an RFC 3339
          duration.
  (rfc3339_duration_to_delta): New function.
  (string_to_delta): Try rfc3339_duration_to_delta first.
* mandos-clients.conf.xml (OPTIONS/timeout): Document new format.
  (EXAMPLE): Update to new interval format.
  (SEE ALSO): Reference RFC 3339.

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