/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

Intermediate commit - this does *not* work yet.

* plugins.d/mandos-client.c (HOOKDIR): New.
  (up_interface): New.
  (runnable_hook): New.
  (main): Run network hooks with "start" argument.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open(), S_ISREG */
 
56
#include <sys/stat.h>           /* open() */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
85
85
                                   raise() */
86
86
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
 
                                   WEXITSTATUS(), WTERMSIG() */
90
88
 
91
89
#ifdef __linux__
92
90
#include <sys/klog.h>           /* klogctl() */
253
251
    
254
252
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
255
253
    if(rc != GPG_ERR_NO_ERROR){
256
 
      fprintf(stderr, "Mandos plugin mandos-client: "
257
 
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
254
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
258
255
              gpgme_strsource(rc), gpgme_strerror(rc));
259
256
      return false;
260
257
    }
261
258
    
262
259
    rc = gpgme_op_import(mc.ctx, pgp_data);
263
260
    if(rc != GPG_ERR_NO_ERROR){
264
 
      fprintf(stderr, "Mandos plugin mandos-client: "
265
 
              "bad gpgme_op_import: %s: %s\n",
 
261
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
266
262
              gpgme_strsource(rc), gpgme_strerror(rc));
267
263
      return false;
268
264
    }
276
272
  }
277
273
  
278
274
  if(debug){
279
 
    fprintf(stderr, "Mandos plugin mandos-client: "
280
 
            "Initializing GPGME\n");
 
275
    fprintf(stderr, "Initializing GPGME\n");
281
276
  }
282
277
  
283
278
  /* Init GPGME */
284
279
  gpgme_check_version(NULL);
285
280
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
286
281
  if(rc != GPG_ERR_NO_ERROR){
287
 
    fprintf(stderr, "Mandos plugin mandos-client: "
288
 
            "bad gpgme_engine_check_version: %s: %s\n",
 
282
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
289
283
            gpgme_strsource(rc), gpgme_strerror(rc));
290
284
    return false;
291
285
  }
293
287
  /* Set GPGME home directory for the OpenPGP engine only */
294
288
  rc = gpgme_get_engine_info(&engine_info);
295
289
  if(rc != GPG_ERR_NO_ERROR){
296
 
    fprintf(stderr, "Mandos plugin mandos-client: "
297
 
            "bad gpgme_get_engine_info: %s: %s\n",
 
290
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
298
291
            gpgme_strsource(rc), gpgme_strerror(rc));
299
292
    return false;
300
293
  }
307
300
    engine_info = engine_info->next;
308
301
  }
309
302
  if(engine_info == NULL){
310
 
    fprintf(stderr, "Mandos plugin mandos-client: "
311
 
            "Could not set GPGME home dir to %s\n", tempdir);
 
303
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
312
304
    return false;
313
305
  }
314
306
  
315
307
  /* Create new GPGME "context" */
316
308
  rc = gpgme_new(&(mc.ctx));
317
309
  if(rc != GPG_ERR_NO_ERROR){
318
 
    fprintf(stderr, "Mandos plugin mandos-client: "
319
 
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
320
 
            gpgme_strerror(rc));
 
310
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
311
            gpgme_strsource(rc), gpgme_strerror(rc));
321
312
    return false;
322
313
  }
323
314
  
342
333
  ssize_t plaintext_length = 0;
343
334
  
344
335
  if(debug){
345
 
    fprintf(stderr, "Mandos plugin mandos-client: "
346
 
            "Trying to decrypt OpenPGP data\n");
 
336
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
347
337
  }
348
338
  
349
339
  /* Create new GPGME data buffer from memory cryptotext */
350
340
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
351
341
                               0);
352
342
  if(rc != GPG_ERR_NO_ERROR){
353
 
    fprintf(stderr, "Mandos plugin mandos-client: "
354
 
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
343
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
355
344
            gpgme_strsource(rc), gpgme_strerror(rc));
356
345
    return -1;
357
346
  }
359
348
  /* Create new empty GPGME data buffer for the plaintext */
360
349
  rc = gpgme_data_new(&dh_plain);
361
350
  if(rc != GPG_ERR_NO_ERROR){
362
 
    fprintf(stderr, "Mandos plugin mandos-client: "
363
 
            "bad gpgme_data_new: %s: %s\n",
 
351
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
364
352
            gpgme_strsource(rc), gpgme_strerror(rc));
365
353
    gpgme_data_release(dh_crypto);
366
354
    return -1;
370
358
     data buffer */
371
359
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
360
  if(rc != GPG_ERR_NO_ERROR){
373
 
    fprintf(stderr, "Mandos plugin mandos-client: "
374
 
            "bad gpgme_op_decrypt: %s: %s\n",
 
361
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
375
362
            gpgme_strsource(rc), gpgme_strerror(rc));
376
363
    plaintext_length = -1;
377
364
    if(debug){
378
365
      gpgme_decrypt_result_t result;
379
366
      result = gpgme_op_decrypt_result(mc.ctx);
380
367
      if(result == NULL){
381
 
        fprintf(stderr, "Mandos plugin mandos-client: "
382
 
                "gpgme_op_decrypt_result failed\n");
 
368
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
383
369
      } else {
384
 
        fprintf(stderr, "Mandos plugin mandos-client: "
385
 
                "Unsupported algorithm: %s\n",
 
370
        fprintf(stderr, "Unsupported algorithm: %s\n",
386
371
                result->unsupported_algorithm);
387
 
        fprintf(stderr, "Mandos plugin mandos-client: "
388
 
                "Wrong key usage: %u\n",
 
372
        fprintf(stderr, "Wrong key usage: %u\n",
389
373
                result->wrong_key_usage);
390
374
        if(result->file_name != NULL){
391
 
          fprintf(stderr, "Mandos plugin mandos-client: "
392
 
                  "File name: %s\n", result->file_name);
 
375
          fprintf(stderr, "File name: %s\n", result->file_name);
393
376
        }
394
377
        gpgme_recipient_t recipient;
395
378
        recipient = result->recipients;
396
379
        while(recipient != NULL){
397
 
          fprintf(stderr, "Mandos plugin mandos-client: "
398
 
                  "Public key algorithm: %s\n",
 
380
          fprintf(stderr, "Public key algorithm: %s\n",
399
381
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
400
 
          fprintf(stderr, "Mandos plugin mandos-client: "
401
 
                  "Key ID: %s\n", recipient->keyid);
402
 
          fprintf(stderr, "Mandos plugin mandos-client: "
403
 
                  "Secret key available: %s\n",
 
382
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
383
          fprintf(stderr, "Secret key available: %s\n",
404
384
                  recipient->status == GPG_ERR_NO_SECKEY
405
385
                  ? "No" : "Yes");
406
386
          recipient = recipient->next;
411
391
  }
412
392
  
413
393
  if(debug){
414
 
    fprintf(stderr, "Mandos plugin mandos-client: "
415
 
            "Decryption of OpenPGP data succeeded\n");
 
394
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
416
395
  }
417
396
  
418
397
  /* Seek back to the beginning of the GPGME plaintext data buffer */
449
428
  }
450
429
  
451
430
  if(debug){
452
 
    fprintf(stderr, "Mandos plugin mandos-client: "
453
 
            "Decrypted password is: ");
 
431
    fprintf(stderr, "Decrypted password is: ");
454
432
    for(ssize_t i = 0; i < plaintext_length; i++){
455
433
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
456
434
    }
478
456
/* GnuTLS log function callback */
479
457
static void debuggnutls(__attribute__((unused)) int level,
480
458
                        const char* string){
481
 
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
 
459
  fprintf(stderr, "GnuTLS: %s", string);
482
460
}
483
461
 
484
462
static int init_gnutls_global(const char *pubkeyfilename,
486
464
  int ret;
487
465
  
488
466
  if(debug){
489
 
    fprintf(stderr, "Mandos plugin mandos-client: "
490
 
            "Initializing GnuTLS\n");
 
467
    fprintf(stderr, "Initializing GnuTLS\n");
491
468
  }
492
469
  
493
470
  ret = gnutls_global_init();
494
471
  if(ret != GNUTLS_E_SUCCESS){
495
 
    fprintf(stderr, "Mandos plugin mandos-client: "
496
 
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
472
    fprintf(stderr, "GnuTLS global_init: %s\n",
 
473
            safer_gnutls_strerror(ret));
497
474
    return -1;
498
475
  }
499
476
  
508
485
  /* OpenPGP credentials */
509
486
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
510
487
  if(ret != GNUTLS_E_SUCCESS){
511
 
    fprintf(stderr, "Mandos plugin mandos-client: "
512
 
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
488
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
489
            safer_gnutls_strerror(ret));
513
490
    gnutls_global_deinit();
514
491
    return -1;
515
492
  }
516
493
  
517
494
  if(debug){
518
 
    fprintf(stderr, "Mandos plugin mandos-client: "
519
 
            "Attempting to use OpenPGP public key %s and"
 
495
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
520
496
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
521
497
            seckeyfilename);
522
498
  }
526
502
     GNUTLS_OPENPGP_FMT_BASE64);
527
503
  if(ret != GNUTLS_E_SUCCESS){
528
504
    fprintf(stderr,
529
 
            "Mandos plugin mandos-client: "
530
505
            "Error[%d] while reading the OpenPGP key pair ('%s',"
531
506
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
532
 
    fprintf(stderr, "Mandos plugin mandos-client: "
533
 
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
 
507
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
508
            safer_gnutls_strerror(ret));
534
509
    goto globalfail;
535
510
  }
536
511
  
537
512
  /* GnuTLS server initialization */
538
513
  ret = gnutls_dh_params_init(&mc.dh_params);
539
514
  if(ret != GNUTLS_E_SUCCESS){
540
 
    fprintf(stderr, "Mandos plugin mandos-client: "
541
 
            "Error in GnuTLS DH parameter initialization:"
 
515
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
542
516
            " %s\n", safer_gnutls_strerror(ret));
543
517
    goto globalfail;
544
518
  }
545
519
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
546
520
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Mandos plugin mandos-client: "
548
 
            "Error in GnuTLS prime generation: %s\n",
 
521
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
549
522
            safer_gnutls_strerror(ret));
550
523
    goto globalfail;
551
524
  }
572
545
    }
573
546
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
574
547
  if(ret != GNUTLS_E_SUCCESS){
575
 
    fprintf(stderr, "Mandos plugin mandos-client: "
576
 
            "Error in GnuTLS session initialization: %s\n",
 
548
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
577
549
            safer_gnutls_strerror(ret));
578
550
  }
579
551
  
587
559
      }
588
560
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
589
561
    if(ret != GNUTLS_E_SUCCESS){
590
 
      fprintf(stderr, "Mandos plugin mandos-client: "
591
 
              "Syntax error at: %s\n", err);
592
 
      fprintf(stderr, "Mandos plugin mandos-client: "
593
 
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
 
562
      fprintf(stderr, "Syntax error at: %s\n", err);
 
563
      fprintf(stderr, "GnuTLS error: %s\n",
 
564
              safer_gnutls_strerror(ret));
594
565
      gnutls_deinit(*session);
595
566
      return -1;
596
567
    }
605
576
    }
606
577
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
607
578
  if(ret != GNUTLS_E_SUCCESS){
608
 
    fprintf(stderr, "Mandos plugin mandos-client: "
609
 
            "Error setting GnuTLS credentials: %s\n",
 
579
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
610
580
            safer_gnutls_strerror(ret));
611
581
    gnutls_deinit(*session);
612
582
    return -1;
658
628
    pf = PF_INET;
659
629
    break;
660
630
  default:
661
 
    fprintf(stderr, "Mandos plugin mandos-client: "
662
 
            "Bad address family: %d\n", af);
 
631
    fprintf(stderr, "Bad address family: %d\n", af);
663
632
    errno = EINVAL;
664
633
    return -1;
665
634
  }
670
639
  }
671
640
  
672
641
  if(debug){
673
 
    fprintf(stderr, "Mandos plugin mandos-client: "
674
 
            "Setting up a TCP connection to %s, port %" PRIu16
 
642
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
675
643
            "\n", ip, port);
676
644
  }
677
645
  
704
672
  }
705
673
  if(ret == 0){
706
674
    int e = errno;
707
 
    fprintf(stderr, "Mandos plugin mandos-client: "
708
 
            "Bad address: %s\n", ip);
 
675
    fprintf(stderr, "Bad address: %s\n", ip);
709
676
    errno = e;
710
677
    goto mandos_end;
711
678
  }
718
685
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
719
686
                              -Wunreachable-code*/
720
687
      if(if_index == AVAHI_IF_UNSPEC){
721
 
        fprintf(stderr, "Mandos plugin mandos-client: "
722
 
                "An IPv6 link-local address is incomplete"
 
688
        fprintf(stderr, "An IPv6 link-local address is incomplete"
723
689
                " without a network interface\n");
724
690
        errno = EINVAL;
725
691
        goto mandos_end;
744
710
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
711
        perror_plus("if_indextoname");
746
712
      } else {
747
 
        fprintf(stderr, "Mandos plugin mandos-client: "
748
 
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
713
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
749
714
                ip, interface, port);
750
715
      }
751
716
    } else {
752
 
      fprintf(stderr, "Mandos plugin mandos-client: "
753
 
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
717
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
718
              port);
754
719
    }
755
720
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
721
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
766
731
      perror_plus("inet_ntop");
767
732
    } else {
768
733
      if(strcmp(addrstr, ip) != 0){
769
 
        fprintf(stderr, "Mandos plugin mandos-client: "
770
 
                "Canonical address form: %s\n", addrstr);
 
734
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
771
735
      }
772
736
    }
773
737
  }
827
791
  }
828
792
  
829
793
  if(debug){
830
 
    fprintf(stderr, "Mandos plugin mandos-client: "
831
 
            "Establishing TLS session with %s\n", ip);
 
794
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
832
795
  }
833
796
  
834
797
  if(quit_now){
854
817
  
855
818
  if(ret != GNUTLS_E_SUCCESS){
856
819
    if(debug){
857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
858
 
              "*** GnuTLS Handshake failed ***\n");
 
820
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
859
821
      gnutls_perror(ret);
860
822
    }
861
823
    errno = EPROTO;
865
827
  /* Read OpenPGP packet that contains the wanted password */
866
828
  
867
829
  if(debug){
868
 
    fprintf(stderr, "Mandos plugin mandos-client: "
869
 
            "Retrieving OpenPGP encrypted password from %s\n", ip);
 
830
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
 
831
            ip);
870
832
  }
871
833
  
872
834
  while(true){
910
872
          }
911
873
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
912
874
        if(ret < 0){
913
 
          fprintf(stderr, "Mandos plugin mandos-client: "
914
 
                  "*** GnuTLS Re-handshake failed ***\n");
 
875
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
915
876
          gnutls_perror(ret);
916
877
          errno = EPROTO;
917
878
          goto mandos_end;
918
879
        }
919
880
        break;
920
881
      default:
921
 
        fprintf(stderr, "Mandos plugin mandos-client: "
922
 
                "Unknown error while reading data from"
 
882
        fprintf(stderr, "Unknown error while reading data from"
923
883
                " encrypted session with Mandos server\n");
924
884
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
925
885
        errno = EIO;
931
891
  }
932
892
  
933
893
  if(debug){
934
 
    fprintf(stderr, "Mandos plugin mandos-client: "
935
 
            "Closing TLS session\n");
 
894
    fprintf(stderr, "Closing TLS session\n");
936
895
  }
937
896
  
938
897
  if(quit_now){
968
927
        if(ret == 0 and ferror(stdout)){
969
928
          int e = errno;
970
929
          if(debug){
971
 
            fprintf(stderr, "Mandos plugin mandos-client: "
972
 
                    "Error writing encrypted data: %s\n",
 
930
            fprintf(stderr, "Error writing encrypted data: %s\n",
973
931
                    strerror(errno));
974
932
          }
975
933
          errno = e;
1033
991
  switch(event){
1034
992
  default:
1035
993
  case AVAHI_RESOLVER_FAILURE:
1036
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1037
 
            "(Avahi Resolver) Failed to resolve service '%s'"
 
994
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1038
995
            " of type '%s' in domain '%s': %s\n", name, type, domain,
1039
996
            avahi_strerror(avahi_server_errno(mc.server)));
1040
997
    break;
1044
1001
      char ip[AVAHI_ADDRESS_STR_MAX];
1045
1002
      avahi_address_snprint(ip, sizeof(ip), address);
1046
1003
      if(debug){
1047
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1048
 
                "Mandos server \"%s\" found on %s (%s, %"
 
1004
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1049
1005
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1050
1006
                ip, (intmax_t)interface, port);
1051
1007
      }
1085
1041
  default:
1086
1042
  case AVAHI_BROWSER_FAILURE:
1087
1043
    
1088
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1089
 
            "(Avahi browser) %s\n",
 
1044
    fprintf(stderr, "(Avahi browser) %s\n",
1090
1045
            avahi_strerror(avahi_server_errno(mc.server)));
1091
1046
    avahi_simple_poll_quit(mc.simple_poll);
1092
1047
    return;
1100
1055
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1101
1056
                                    name, type, domain, protocol, 0,
1102
1057
                                    resolve_callback, NULL) == NULL)
1103
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1104
 
              "Avahi: Failed to resolve service '%s': %s\n",
 
1058
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1105
1059
              name, avahi_strerror(avahi_server_errno(mc.server)));
1106
1060
    break;
1107
1061
    
1111
1065
  case AVAHI_BROWSER_ALL_FOR_NOW:
1112
1066
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1113
1067
    if(debug){
1114
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1115
 
              "No Mandos server found, still searching...\n");
 
1068
      fprintf(stderr, "No Mandos server found, still searching...\n");
1116
1069
    }
1117
1070
    break;
1118
1071
  }
1157
1110
  /* Reject the loopback device */
1158
1111
  if(ifr->ifr_flags & IFF_LOOPBACK){
1159
1112
    if(debug){
1160
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1161
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1113
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1114
              ifname);
1162
1115
    }
1163
1116
    return false;
1164
1117
  }
1165
1118
  /* Accept point-to-point devices only if connect_to is specified */
1166
1119
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1167
1120
    if(debug){
1168
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1169
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1121
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1122
              ifname);
1170
1123
    }
1171
1124
    return true;
1172
1125
  }
1173
1126
  /* Otherwise, reject non-broadcast-capable devices */
1174
1127
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1175
1128
    if(debug){
1176
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1177
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1129
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1130
              ifname);
1178
1131
    }
1179
1132
    return false;
1180
1133
  }
1181
1134
  /* Reject non-ARP interfaces (including dummy interfaces) */
1182
1135
  if(ifr->ifr_flags & IFF_NOARP){
1183
1136
    if(debug){
1184
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1185
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1137
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1186
1138
    }
1187
1139
    return false;
1188
1140
  }
1189
1141
  
1190
1142
  /* Accept this device */
1191
1143
  if(debug){
1192
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1193
 
            "Interface \"%s\" is good\n", ifname);
 
1144
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1194
1145
  }
1195
1146
  return true;
1196
1147
}
1201
1152
 * (This function is passed to scandir(3) as a filter function.)
1202
1153
 */
1203
1154
int good_interface(const struct dirent *if_entry){
 
1155
  int ret;
1204
1156
  if(if_entry->d_name[0] == '.'){
1205
1157
    return 0;
1206
1158
  }
1207
 
  
1208
1159
  struct ifreq ifr;
 
1160
 
1209
1161
  if(not get_flags(if_entry->d_name, &ifr)){
1210
 
    if(debug){
1211
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1212
 
              "Failed to get flags for interface \"%s\"\n",
1213
 
              if_entry->d_name);
1214
 
    }
1215
1162
    return 0;
1216
1163
  }
1217
1164
  
1227
1174
 * (This function is passed to scandir(3) as a filter function.)
1228
1175
 */
1229
1176
int up_interface(const struct dirent *if_entry){
 
1177
  ssize_t ssret;
 
1178
  char *flagname = NULL;
1230
1179
  if(if_entry->d_name[0] == '.'){
1231
1180
    return 0;
1232
1181
  }
1233
 
  
1234
 
  struct ifreq ifr;
1235
 
  if(not get_flags(if_entry->d_name, &ifr)){
1236
 
    if(debug){
1237
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1238
 
              "Failed to get flags for interface \"%s\"\n",
 
1182
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
1183
                     if_entry->d_name);
 
1184
  if(ret < 0){
 
1185
    perror_plus("asprintf");
 
1186
    return 0;
 
1187
  }
 
1188
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
 
1189
  if(flags_fd == -1){
 
1190
    perror_plus("open");
 
1191
    free(flagname);
 
1192
    return 0;
 
1193
  }
 
1194
  free(flagname);
 
1195
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
 
1196
  /* read line from flags_fd */
 
1197
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1198
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1199
  flagstring[(size_t)to_read] = '\0';
 
1200
  if(flagstring == NULL){
 
1201
    perror_plus("malloc");
 
1202
    close(flags_fd);
 
1203
    return 0;
 
1204
  }
 
1205
  while(to_read > 0){
 
1206
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1207
                                             (size_t)to_read));
 
1208
    if(ssret == -1){
 
1209
      perror_plus("read");
 
1210
      free(flagstring);
 
1211
      close(flags_fd);
 
1212
      return 0;
 
1213
    }
 
1214
    to_read -= ssret;
 
1215
    if(ssret == 0){
 
1216
      break;
 
1217
    }
 
1218
  }
 
1219
  close(flags_fd);
 
1220
  intmax_t tmpmax;
 
1221
  char *tmp;
 
1222
  errno = 0;
 
1223
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1224
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1225
                                         and not (isspace(*tmp)))
 
1226
     or tmpmax != (ifreq_flags)tmpmax){
 
1227
    if(debug){
 
1228
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1229
              flagstring, if_entry->d_name);
 
1230
    }
 
1231
    free(flagstring);
 
1232
    return 0;
 
1233
  }
 
1234
  free(flagstring);
 
1235
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1236
  /* Reject the loopback device */
 
1237
  if(flags & IFF_LOOPBACK){
 
1238
    if(debug){
 
1239
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1239
1240
              if_entry->d_name);
1240
1241
    }
1241
1242
    return 0;
1242
1243
  }
1243
 
  
 
1244
 
1244
1245
  /* Reject down interfaces */
1245
 
  if(not (ifr.ifr_flags & IFF_UP)){
1246
 
    if(debug){
1247
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1248
 
              "Rejecting down interface \"%s\"\n",
1249
 
              if_entry->d_name);
1250
 
    }
1251
 
    return 0;
1252
 
  }
1253
 
  
1254
 
  /* Reject non-running interfaces */
1255
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1256
 
    if(debug){
1257
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1258
 
              "Rejecting non-running interface \"%s\"\n",
1259
 
              if_entry->d_name);
1260
 
    }
1261
 
    return 0;
1262
 
  }
1263
 
  
1264
 
  if(not good_flags(if_entry->d_name, &ifr)){
1265
 
    return 0;
 
1246
  if(not (flags & IFF_UP)){
 
1247
    return 0;
 
1248
  }
 
1249
  
 
1250
  /* Accept point-to-point devices only if connect_to is specified */
 
1251
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1252
    if(debug){
 
1253
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1254
              if_entry->d_name);
 
1255
    }
 
1256
    return 1;
 
1257
  }
 
1258
  /* Otherwise, reject non-broadcast-capable devices */
 
1259
  if(not (flags & IFF_BROADCAST)){
 
1260
    if(debug){
 
1261
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1262
              if_entry->d_name);
 
1263
    }
 
1264
    return 0;
 
1265
  }
 
1266
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1267
  if(flags & IFF_NOARP){
 
1268
    if(debug){
 
1269
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1270
              if_entry->d_name);
 
1271
    }
 
1272
    return 0;
 
1273
  }
 
1274
  /* Accept this device */
 
1275
  if(debug){
 
1276
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1277
            if_entry->d_name);
1266
1278
  }
1267
1279
  return 1;
1268
1280
}
1311
1323
    }
1312
1324
    return 0;
1313
1325
  }
1314
 
  if(not (S_ISREG(st.st_mode))){
 
1326
  if(not (st.st_mode & S_ISREG)){
1315
1327
    /* Not a regular file */
1316
1328
    return 0;
1317
1329
  }
1331
1343
  while(true){
1332
1344
    if(mc.current_server == NULL){
1333
1345
      if (debug){
1334
 
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1346
        fprintf(stderr,
1335
1347
                "Wait until first server is found. No timeout!\n");
1336
1348
      }
1337
1349
      ret = avahi_simple_poll_iterate(s, -1);
1338
1350
    } else {
1339
1351
      if (debug){
1340
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1341
 
                "Check current_server if we should run it,"
 
1352
        fprintf(stderr, "Check current_server if we should run it,"
1342
1353
                " or wait\n");
1343
1354
      }
1344
1355
      /* the current time */
1361
1372
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1362
1373
      
1363
1374
      if (debug){
1364
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1365
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1375
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1366
1376
      }
1367
1377
      
1368
1378
      if(block_time <= 0){
1388
1398
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1389
1399
    }
1390
1400
    if(ret != 0){
1391
 
      if (ret > 0 or errno != EINTR){
 
1401
      if (ret > 0 or errno != EINTR) {
1392
1402
        return (ret != 1) ? ret : 0;
1393
1403
      }
1394
1404
    }
1551
1561
        argp_state_help(state, state->out_stream,
1552
1562
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1553
1563
      case 'V':                 /* --version */
1554
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1555
1564
        fprintf(state->out_stream, "%s\n", argp_program_version);
1556
1565
        exit(argp_err_exit_status);
1557
1566
        break;
1648
1657
    struct dirent *direntry;
1649
1658
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1650
1659
                           alphasort);
1651
 
    if(numhooks == -1){
1652
 
      perror_plus("scandir");
1653
 
    } else {
1654
 
      int devnull = open("/dev/null", O_RDONLY);
1655
 
      for(int i = 0; i < numhooks; i++){
1656
 
        direntry = direntries[0];
1657
 
        char *fullname = NULL;
1658
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
1659
 
                       direntry->d_name);
1660
 
        if(ret < 0){
1661
 
          perror_plus("asprintf");
1662
 
          continue;
1663
 
        }
1664
 
        pid_t hook_pid = fork();
1665
 
        if(hook_pid == 0){
1666
 
          /* Child */
1667
 
          dup2(devnull, STDIN_FILENO);
1668
 
          close(devnull);
1669
 
          dup2(STDERR_FILENO, STDOUT_FILENO);
1670
 
          ret = setenv("DEVICE", interface, 1);
1671
 
          if(ret == -1){
1672
 
            perror_plus("setenv");
1673
 
            exit(1);
1674
 
          }
1675
 
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1676
 
          if(ret == -1){
1677
 
            perror_plus("setenv");
1678
 
            exit(1);
1679
 
          }
1680
 
          ret = setenv("MODE", "start", 1);
1681
 
          if(ret == -1){
1682
 
            perror_plus("setenv");
1683
 
            exit(1);
1684
 
          }
1685
 
          char *delaystring;
1686
 
          ret = asprintf(&delaystring, "%f", delay);
1687
 
          if(ret == -1){
1688
 
            perror_plus("asprintf");
1689
 
            exit(1);
1690
 
          }
1691
 
          ret = setenv("DELAY", delaystring, 1);
1692
 
          if(ret == -1){
1693
 
            free(delaystring);
1694
 
            perror_plus("setenv");
1695
 
            exit(1);
1696
 
          }
1697
 
          free(delaystring);
1698
 
          ret = execl(fullname, direntry->d_name, "start", NULL);
1699
 
          perror_plus("execl");
1700
 
        } else {
1701
 
          int status;
1702
 
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1703
 
            perror_plus("waitpid");
1704
 
            free(fullname);
1705
 
            continue;
1706
 
          }
1707
 
          if(WIFEXITED(status)){
1708
 
            if(WEXITSTATUS(status) != 0){
1709
 
              fprintf(stderr, "Mandos plugin mandos-client: "
1710
 
                      "Warning: network hook \"%s\" exited"
1711
 
                      " with status %d\n", direntry->d_name,
1712
 
                      WEXITSTATUS(status));
1713
 
              free(fullname);
1714
 
              continue;
1715
 
            }
1716
 
          } else if(WIFSIGNALED(status)){
1717
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1718
 
                    "Warning: network hook \"%s\" died by"
1719
 
                    " signal %d\n", direntry->d_name,
1720
 
                    WTERMSIG(status));
1721
 
            free(fullname);
1722
 
            continue;
1723
 
          } else {
1724
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1725
 
                    "Warning: network hook \"%s\" crashed\n",
1726
 
                    direntry->d_name);
1727
 
            free(fullname);
1728
 
            continue;
1729
 
          }
1730
 
        }
1731
 
        free(fullname);
1732
 
        if(quit_now){
1733
 
          goto end;
1734
 
        }
1735
 
      }
1736
 
      close(devnull);
 
1660
    int devnull = open("/dev/null", O_RDONLY);
 
1661
    for(int i = 0; i < numhooks; i++){
 
1662
      direntry = direntries[0];
 
1663
      char *fullname = NULL;
 
1664
      ret = asprintf(&fullname, "%s/%s", tempdir,
 
1665
                     direntry->d_name);
 
1666
      if(ret < 0){
 
1667
        perror_plus("asprintf");
 
1668
        continue;
 
1669
      }
 
1670
      pid_t hook_pid = fork();
 
1671
      if(hook_pid == 0){
 
1672
        /* Child */
 
1673
        dup2(devnull, STDIN_FILENO);
 
1674
        close(devnull);
 
1675
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1676
        setenv("DEVICE", interface, 1);
 
1677
        setenv("VERBOSE", debug ? "1" : "0", 1);
 
1678
        setenv("MODE", "start", 1);
 
1679
        /* setenv( XXX more here */
 
1680
        ret = execl(fullname, direntry->d_name, "start");
 
1681
        perror_plus("execl");
 
1682
      }
 
1683
      free(fullname);
 
1684
      if(quit_now){
 
1685
        goto end;
 
1686
      }
1737
1687
    }
 
1688
    close(devnull);
1738
1689
  }
1739
1690
  
1740
1691
  if(not debug){
1743
1694
  
1744
1695
  if(interface[0] == '\0'){
1745
1696
    struct dirent **direntries;
1746
 
    /* First look for interfaces that are up */
1747
 
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1697
    ret = scandir(sys_class_net, &direntries, good_interface,
1748
1698
                  alphasort);
1749
 
    if(ret == 0){
1750
 
      /* No up interfaces, look for any good interfaces */
1751
 
      free(direntries);
1752
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1753
 
                    alphasort);
1754
 
    }
1755
1699
    if(ret >= 1){
1756
 
      /* Pick the first interface returned */
 
1700
      /* Pick the first good interface */
1757
1701
      interface = strdup(direntries[0]->d_name);
1758
1702
      if(debug){
1759
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1760
 
                "Using interface \"%s\"\n", interface);
 
1703
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1761
1704
      }
1762
1705
      if(interface == NULL){
1763
1706
        perror_plus("malloc");
1768
1711
      free(direntries);
1769
1712
    } else {
1770
1713
      free(direntries);
1771
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1772
 
              "Could not find a network interface\n");
 
1714
      fprintf(stderr, "Could not find a network interface\n");
1773
1715
      exitcode = EXIT_FAILURE;
1774
1716
      goto end;
1775
1717
    }
1781
1723
  srand((unsigned int) time(NULL));
1782
1724
  mc.simple_poll = avahi_simple_poll_new();
1783
1725
  if(mc.simple_poll == NULL){
1784
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1785
 
            "Avahi: Failed to create simple poll object.\n");
 
1726
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1786
1727
    exitcode = EX_UNAVAILABLE;
1787
1728
    goto end;
1788
1729
  }
1854
1795
  if(strcmp(interface, "none") != 0){
1855
1796
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1856
1797
    if(if_index == 0){
1857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1858
 
              "No such interface: \"%s\"\n", interface);
 
1798
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1859
1799
      exitcode = EX_UNAVAILABLE;
1860
1800
      goto end;
1861
1801
    }
2002
1942
  
2003
1943
  ret = init_gnutls_global(pubkey, seckey);
2004
1944
  if(ret == -1){
2005
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2006
 
            "init_gnutls_global failed\n");
 
1945
    fprintf(stderr, "init_gnutls_global failed\n");
2007
1946
    exitcode = EX_UNAVAILABLE;
2008
1947
    goto end;
2009
1948
  } else {
2025
1964
  }
2026
1965
  
2027
1966
  if(not init_gpgme(pubkey, seckey, tempdir)){
2028
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2029
 
            "init_gpgme failed\n");
 
1967
    fprintf(stderr, "init_gpgme failed\n");
2030
1968
    exitcode = EX_UNAVAILABLE;
2031
1969
    goto end;
2032
1970
  } else {
2042
1980
    /* (Mainly meant for debugging) */
2043
1981
    char *address = strrchr(connect_to, ':');
2044
1982
    if(address == NULL){
2045
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2046
 
              "No colon in address\n");
 
1983
      fprintf(stderr, "No colon in address\n");
2047
1984
      exitcode = EX_USAGE;
2048
1985
      goto end;
2049
1986
    }
2057
1994
    tmpmax = strtoimax(address+1, &tmp, 10);
2058
1995
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2059
1996
       or tmpmax != (uint16_t)tmpmax){
2060
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2061
 
              "Bad port number\n");
 
1997
      fprintf(stderr, "Bad port number\n");
2062
1998
      exitcode = EX_USAGE;
2063
1999
      goto end;
2064
2000
    }
2094
2030
        break;
2095
2031
      }
2096
2032
      if(debug){
2097
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2098
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
2033
        fprintf(stderr, "Retrying in %d seconds\n",
 
2034
                (int)retry_interval);
2099
2035
      }
2100
2036
      sleep((int)retry_interval);
2101
2037
    }
2131
2067
  
2132
2068
  /* Check if creating the Avahi server object succeeded */
2133
2069
  if(mc.server == NULL){
2134
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2135
 
            "Failed to create Avahi server: %s\n",
 
2070
    fprintf(stderr, "Failed to create Avahi server: %s\n",
2136
2071
            avahi_strerror(error));
2137
2072
    exitcode = EX_UNAVAILABLE;
2138
2073
    goto end;
2147
2082
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2148
2083
                                   NULL, 0, browse_callback, NULL);
2149
2084
  if(sb == NULL){
2150
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2151
 
            "Failed to create service browser: %s\n",
 
2085
    fprintf(stderr, "Failed to create service browser: %s\n",
2152
2086
            avahi_strerror(avahi_server_errno(mc.server)));
2153
2087
    exitcode = EX_UNAVAILABLE;
2154
2088
    goto end;
2161
2095
  /* Run the main loop */
2162
2096
  
2163
2097
  if(debug){
2164
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2165
 
            "Starting Avahi loop search\n");
 
2098
    fprintf(stderr, "Starting Avahi loop search\n");
2166
2099
  }
2167
2100
 
2168
2101
  ret = avahi_loop_with_timeout(mc.simple_poll,
2169
2102
                                (int)(retry_interval * 1000));
2170
2103
  if(debug){
2171
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2172
 
            "avahi_loop_with_timeout exited %s\n",
 
2104
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2173
2105
            (ret == 0) ? "successfully" : "with error");
2174
2106
  }
2175
2107
  
2176
2108
 end:
2177
2109
  
2178
2110
  if(debug){
2179
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2180
 
            "%s exiting\n", argv[0]);
 
2111
    fprintf(stderr, "%s exiting\n", argv[0]);
2181
2112
  }
2182
2113
  
2183
2114
  /* Cleanup things */
2225
2156
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2226
2157
      if(ret == -1){
2227
2158
        perror_plus("ioctl SIOCGIFFLAGS");
2228
 
      } else if(network.ifr_flags & IFF_UP){
 
2159
      } else if(network.ifr_flags & IFF_UP) {
2229
2160
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2230
2161
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2231
2162
        if(ret == -1){
2263
2194
        }
2264
2195
        ret = remove(fullname);
2265
2196
        if(ret == -1){
2266
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2267
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2197
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
2198
                  strerror(errno));
2268
2199
        }
2269
2200
        free(fullname);
2270
2201
      }