/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

* plugins.d/mandos-client.c (run_network_hooks): New.
  (main): Run run_network_hooks() with "stop" and "start" at
          appropriate places.  Don't lower privileges permanently
          prematurely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
174
174
  perror(print_text);
175
175
}
176
176
 
177
 
int fprintf_plus(FILE *stream, const char *format, ...){
178
 
  va_list ap;
179
 
  va_start (ap, format);
180
 
  
181
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
 
                             program_invocation_short_name));
183
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
184
 
}
185
 
 
186
177
/*
187
178
 * Make additional room in "buffer" for at least BUFFER_SIZE more
188
179
 * bytes. "buffer_capacity" is how much is currently allocated,
262
253
    
263
254
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
264
255
    if(rc != GPG_ERR_NO_ERROR){
265
 
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
256
      fprintf(stderr, "Mandos plugin mandos-client: "
 
257
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
258
              gpgme_strsource(rc), gpgme_strerror(rc));
267
259
      return false;
268
260
    }
269
261
    
270
262
    rc = gpgme_op_import(mc.ctx, pgp_data);
271
263
    if(rc != GPG_ERR_NO_ERROR){
272
 
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
264
      fprintf(stderr, "Mandos plugin mandos-client: "
 
265
              "bad gpgme_op_import: %s: %s\n",
 
266
              gpgme_strsource(rc), gpgme_strerror(rc));
274
267
      return false;
275
268
    }
276
269
    
283
276
  }
284
277
  
285
278
  if(debug){
286
 
    fprintf_plus(stderr, "Initializing GPGME\n");
 
279
    fprintf(stderr, "Mandos plugin mandos-client: "
 
280
            "Initializing GPGME\n");
287
281
  }
288
282
  
289
283
  /* Init GPGME */
290
284
  gpgme_check_version(NULL);
291
285
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
292
286
  if(rc != GPG_ERR_NO_ERROR){
293
 
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
294
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
287
    fprintf(stderr, "Mandos plugin mandos-client: "
 
288
            "bad gpgme_engine_check_version: %s: %s\n",
 
289
            gpgme_strsource(rc), gpgme_strerror(rc));
295
290
    return false;
296
291
  }
297
292
  
298
293
  /* Set GPGME home directory for the OpenPGP engine only */
299
294
  rc = gpgme_get_engine_info(&engine_info);
300
295
  if(rc != GPG_ERR_NO_ERROR){
301
 
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
302
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
296
    fprintf(stderr, "Mandos plugin mandos-client: "
 
297
            "bad gpgme_get_engine_info: %s: %s\n",
 
298
            gpgme_strsource(rc), gpgme_strerror(rc));
303
299
    return false;
304
300
  }
305
301
  while(engine_info != NULL){
311
307
    engine_info = engine_info->next;
312
308
  }
313
309
  if(engine_info == NULL){
314
 
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
315
 
                 tempdir);
 
310
    fprintf(stderr, "Mandos plugin mandos-client: "
 
311
            "Could not set GPGME home dir to %s\n", tempdir);
316
312
    return false;
317
313
  }
318
314
  
319
315
  /* Create new GPGME "context" */
320
316
  rc = gpgme_new(&(mc.ctx));
321
317
  if(rc != GPG_ERR_NO_ERROR){
322
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
323
 
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
324
 
                 gpgme_strerror(rc));
 
318
    fprintf(stderr, "Mandos plugin mandos-client: "
 
319
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
320
            gpgme_strerror(rc));
325
321
    return false;
326
322
  }
327
323
  
346
342
  ssize_t plaintext_length = 0;
347
343
  
348
344
  if(debug){
349
 
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
 
345
    fprintf(stderr, "Mandos plugin mandos-client: "
 
346
            "Trying to decrypt OpenPGP data\n");
350
347
  }
351
348
  
352
349
  /* Create new GPGME data buffer from memory cryptotext */
353
350
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
354
351
                               0);
355
352
  if(rc != GPG_ERR_NO_ERROR){
356
 
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
357
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
353
    fprintf(stderr, "Mandos plugin mandos-client: "
 
354
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
355
            gpgme_strsource(rc), gpgme_strerror(rc));
358
356
    return -1;
359
357
  }
360
358
  
361
359
  /* Create new empty GPGME data buffer for the plaintext */
362
360
  rc = gpgme_data_new(&dh_plain);
363
361
  if(rc != GPG_ERR_NO_ERROR){
364
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
365
 
                 "bad gpgme_data_new: %s: %s\n",
366
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
362
    fprintf(stderr, "Mandos plugin mandos-client: "
 
363
            "bad gpgme_data_new: %s: %s\n",
 
364
            gpgme_strsource(rc), gpgme_strerror(rc));
367
365
    gpgme_data_release(dh_crypto);
368
366
    return -1;
369
367
  }
372
370
     data buffer */
373
371
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
374
372
  if(rc != GPG_ERR_NO_ERROR){
375
 
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
373
    fprintf(stderr, "Mandos plugin mandos-client: "
 
374
            "bad gpgme_op_decrypt: %s: %s\n",
 
375
            gpgme_strsource(rc), gpgme_strerror(rc));
377
376
    plaintext_length = -1;
378
377
    if(debug){
379
378
      gpgme_decrypt_result_t result;
380
379
      result = gpgme_op_decrypt_result(mc.ctx);
381
380
      if(result == NULL){
382
 
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
 
381
        fprintf(stderr, "Mandos plugin mandos-client: "
 
382
                "gpgme_op_decrypt_result failed\n");
383
383
      } else {
384
 
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
385
 
                     result->unsupported_algorithm);
386
 
        fprintf_plus(stderr, "Wrong key usage: %u\n",
387
 
                     result->wrong_key_usage);
 
384
        fprintf(stderr, "Mandos plugin mandos-client: "
 
385
                "Unsupported algorithm: %s\n",
 
386
                result->unsupported_algorithm);
 
387
        fprintf(stderr, "Mandos plugin mandos-client: "
 
388
                "Wrong key usage: %u\n",
 
389
                result->wrong_key_usage);
388
390
        if(result->file_name != NULL){
389
 
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
 
391
          fprintf(stderr, "Mandos plugin mandos-client: "
 
392
                  "File name: %s\n", result->file_name);
390
393
        }
391
394
        gpgme_recipient_t recipient;
392
395
        recipient = result->recipients;
393
396
        while(recipient != NULL){
394
 
          fprintf_plus(stderr, "Public key algorithm: %s\n",
395
 
                       gpgme_pubkey_algo_name
396
 
                       (recipient->pubkey_algo));
397
 
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
398
 
          fprintf_plus(stderr, "Secret key available: %s\n",
399
 
                       recipient->status == GPG_ERR_NO_SECKEY
400
 
                       ? "No" : "Yes");
 
397
          fprintf(stderr, "Mandos plugin mandos-client: "
 
398
                  "Public key algorithm: %s\n",
 
399
                  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",
 
404
                  recipient->status == GPG_ERR_NO_SECKEY
 
405
                  ? "No" : "Yes");
401
406
          recipient = recipient->next;
402
407
        }
403
408
      }
406
411
  }
407
412
  
408
413
  if(debug){
409
 
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
 
414
    fprintf(stderr, "Mandos plugin mandos-client: "
 
415
            "Decryption of OpenPGP data succeeded\n");
410
416
  }
411
417
  
412
418
  /* Seek back to the beginning of the GPGME plaintext data buffer */
443
449
  }
444
450
  
445
451
  if(debug){
446
 
    fprintf_plus(stderr, "Decrypted password is: ");
 
452
    fprintf(stderr, "Mandos plugin mandos-client: "
 
453
            "Decrypted password is: ");
447
454
    for(ssize_t i = 0; i < plaintext_length; i++){
448
455
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
449
456
    }
471
478
/* GnuTLS log function callback */
472
479
static void debuggnutls(__attribute__((unused)) int level,
473
480
                        const char* string){
474
 
  fprintf_plus(stderr, "GnuTLS: %s", string);
 
481
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
475
482
}
476
483
 
477
484
static int init_gnutls_global(const char *pubkeyfilename,
479
486
  int ret;
480
487
  
481
488
  if(debug){
482
 
    fprintf_plus(stderr, "Initializing GnuTLS\n");
 
489
    fprintf(stderr, "Mandos plugin mandos-client: "
 
490
            "Initializing GnuTLS\n");
483
491
  }
484
492
  
485
493
  ret = gnutls_global_init();
486
494
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
 
                 safer_gnutls_strerror(ret));
 
495
    fprintf(stderr, "Mandos plugin mandos-client: "
 
496
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
489
497
    return -1;
490
498
  }
491
499
  
500
508
  /* OpenPGP credentials */
501
509
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
502
510
  if(ret != GNUTLS_E_SUCCESS){
503
 
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
 
                 safer_gnutls_strerror(ret));
 
511
    fprintf(stderr, "Mandos plugin mandos-client: "
 
512
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
505
513
    gnutls_global_deinit();
506
514
    return -1;
507
515
  }
508
516
  
509
517
  if(debug){
510
 
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
511
 
                 " secret key %s as GnuTLS credentials\n",
512
 
                 pubkeyfilename,
513
 
                 seckeyfilename);
 
518
    fprintf(stderr, "Mandos plugin mandos-client: "
 
519
            "Attempting to use OpenPGP public key %s and"
 
520
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
521
            seckeyfilename);
514
522
  }
515
523
  
516
524
  ret = gnutls_certificate_set_openpgp_key_file
517
525
    (mc.cred, pubkeyfilename, seckeyfilename,
518
526
     GNUTLS_OPENPGP_FMT_BASE64);
519
527
  if(ret != GNUTLS_E_SUCCESS){
520
 
    fprintf_plus(stderr,
521
 
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
522
 
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
523
 
    fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
 
                 safer_gnutls_strerror(ret));
 
528
    fprintf(stderr,
 
529
            "Mandos plugin mandos-client: "
 
530
            "Error[%d] while reading the OpenPGP key pair ('%s',"
 
531
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
532
    fprintf(stderr, "Mandos plugin mandos-client: "
 
533
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
525
534
    goto globalfail;
526
535
  }
527
536
  
528
537
  /* GnuTLS server initialization */
529
538
  ret = gnutls_dh_params_init(&mc.dh_params);
530
539
  if(ret != GNUTLS_E_SUCCESS){
531
 
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
 
                 " initialization: %s\n",
533
 
                 safer_gnutls_strerror(ret));
 
540
    fprintf(stderr, "Mandos plugin mandos-client: "
 
541
            "Error in GnuTLS DH parameter initialization:"
 
542
            " %s\n", safer_gnutls_strerror(ret));
534
543
    goto globalfail;
535
544
  }
536
545
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
537
546
  if(ret != GNUTLS_E_SUCCESS){
538
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
 
                 safer_gnutls_strerror(ret));
 
547
    fprintf(stderr, "Mandos plugin mandos-client: "
 
548
            "Error in GnuTLS prime generation: %s\n",
 
549
            safer_gnutls_strerror(ret));
540
550
    goto globalfail;
541
551
  }
542
552
  
562
572
    }
563
573
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
564
574
  if(ret != GNUTLS_E_SUCCESS){
565
 
    fprintf_plus(stderr,
566
 
                 "Error in GnuTLS session initialization: %s\n",
567
 
                 safer_gnutls_strerror(ret));
 
575
    fprintf(stderr, "Mandos plugin mandos-client: "
 
576
            "Error in GnuTLS session initialization: %s\n",
 
577
            safer_gnutls_strerror(ret));
568
578
  }
569
579
  
570
580
  {
577
587
      }
578
588
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
579
589
    if(ret != GNUTLS_E_SUCCESS){
580
 
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
581
 
      fprintf_plus(stderr, "GnuTLS error: %s\n",
582
 
                   safer_gnutls_strerror(ret));
 
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));
583
594
      gnutls_deinit(*session);
584
595
      return -1;
585
596
    }
594
605
    }
595
606
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
596
607
  if(ret != GNUTLS_E_SUCCESS){
597
 
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
598
 
                 safer_gnutls_strerror(ret));
 
608
    fprintf(stderr, "Mandos plugin mandos-client: "
 
609
            "Error setting GnuTLS credentials: %s\n",
 
610
            safer_gnutls_strerror(ret));
599
611
    gnutls_deinit(*session);
600
612
    return -1;
601
613
  }
646
658
    pf = PF_INET;
647
659
    break;
648
660
  default:
649
 
    fprintf_plus(stderr, "Bad address family: %d\n", af);
 
661
    fprintf(stderr, "Mandos plugin mandos-client: "
 
662
            "Bad address family: %d\n", af);
650
663
    errno = EINVAL;
651
664
    return -1;
652
665
  }
657
670
  }
658
671
  
659
672
  if(debug){
660
 
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
661
 
                 PRIu16 "\n", ip, port);
 
673
    fprintf(stderr, "Mandos plugin mandos-client: "
 
674
            "Setting up a TCP connection to %s, port %" PRIu16
 
675
            "\n", ip, port);
662
676
  }
663
677
  
664
678
  tcp_sd = socket(pf, SOCK_STREAM, 0);
690
704
  }
691
705
  if(ret == 0){
692
706
    int e = errno;
693
 
    fprintf_plus(stderr, "Bad address: %s\n", ip);
 
707
    fprintf(stderr, "Mandos plugin mandos-client: "
 
708
            "Bad address: %s\n", ip);
694
709
    errno = e;
695
710
    goto mandos_end;
696
711
  }
703
718
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
704
719
                                -Wunreachable-code*/
705
720
      if(if_index == AVAHI_IF_UNSPEC){
706
 
        fprintf_plus(stderr, "An IPv6 link-local address is"
707
 
                     " incomplete without a network interface\n");
 
721
        fprintf(stderr, "Mandos plugin mandos-client: "
 
722
                "An IPv6 link-local address is incomplete"
 
723
                " without a network interface\n");
708
724
        errno = EINVAL;
709
725
        goto mandos_end;
710
726
      }
728
744
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
729
745
        perror_plus("if_indextoname");
730
746
      } else {
731
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
 
                     "\n", ip, interface, port);
 
747
        fprintf(stderr, "Mandos plugin mandos-client: "
 
748
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
749
                ip, interface, port);
733
750
      }
734
751
    } else {
735
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
736
 
                   ip, port);
 
752
      fprintf(stderr, "Mandos plugin mandos-client: "
 
753
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
737
754
    }
738
755
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
739
756
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
749
766
      perror_plus("inet_ntop");
750
767
    } else {
751
768
      if(strcmp(addrstr, ip) != 0){
752
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
769
        fprintf(stderr, "Mandos plugin mandos-client: "
 
770
                "Canonical address form: %s\n", addrstr);
753
771
      }
754
772
    }
755
773
  }
809
827
  }
810
828
  
811
829
  if(debug){
812
 
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
 
830
    fprintf(stderr, "Mandos plugin mandos-client: "
 
831
            "Establishing TLS session with %s\n", ip);
813
832
  }
814
833
  
815
834
  if(quit_now){
835
854
  
836
855
  if(ret != GNUTLS_E_SUCCESS){
837
856
    if(debug){
838
 
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
 
857
      fprintf(stderr, "Mandos plugin mandos-client: "
 
858
              "*** GnuTLS Handshake failed ***\n");
839
859
      gnutls_perror(ret);
840
860
    }
841
861
    errno = EPROTO;
845
865
  /* Read OpenPGP packet that contains the wanted password */
846
866
  
847
867
  if(debug){
848
 
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
849
 
                 " %s\n", ip);
 
868
    fprintf(stderr, "Mandos plugin mandos-client: "
 
869
            "Retrieving OpenPGP encrypted password from %s\n", ip);
850
870
  }
851
871
  
852
872
  while(true){
890
910
          }
891
911
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
892
912
        if(ret < 0){
893
 
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
894
 
                       "***\n");
 
913
          fprintf(stderr, "Mandos plugin mandos-client: "
 
914
                  "*** GnuTLS Re-handshake failed ***\n");
895
915
          gnutls_perror(ret);
896
916
          errno = EPROTO;
897
917
          goto mandos_end;
898
918
        }
899
919
        break;
900
920
      default:
901
 
        fprintf_plus(stderr, "Unknown error while reading data from"
902
 
                     " encrypted session with Mandos server\n");
 
921
        fprintf(stderr, "Mandos plugin mandos-client: "
 
922
                "Unknown error while reading data from"
 
923
                " encrypted session with Mandos server\n");
903
924
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
904
925
        errno = EIO;
905
926
        goto mandos_end;
910
931
  }
911
932
  
912
933
  if(debug){
913
 
    fprintf_plus(stderr, "Closing TLS session\n");
 
934
    fprintf(stderr, "Mandos plugin mandos-client: "
 
935
            "Closing TLS session\n");
914
936
  }
915
937
  
916
938
  if(quit_now){
945
967
        if(ret == 0 and ferror(stdout)){
946
968
          int e = errno;
947
969
          if(debug){
948
 
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
949
 
                         strerror(errno));
 
970
            fprintf(stderr, "Mandos plugin mandos-client: "
 
971
                    "Error writing encrypted data: %s\n",
 
972
                    strerror(errno));
950
973
          }
951
974
          errno = e;
952
975
          goto mandos_end;
1009
1032
  switch(event){
1010
1033
  default:
1011
1034
  case AVAHI_RESOLVER_FAILURE:
1012
 
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1013
 
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
1014
 
                 domain,
1015
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1035
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1036
            "(Avahi Resolver) Failed to resolve service '%s'"
 
1037
            " of type '%s' in domain '%s': %s\n", name, type, domain,
 
1038
            avahi_strerror(avahi_server_errno(mc.server)));
1016
1039
    break;
1017
1040
    
1018
1041
  case AVAHI_RESOLVER_FOUND:
1020
1043
      char ip[AVAHI_ADDRESS_STR_MAX];
1021
1044
      avahi_address_snprint(ip, sizeof(ip), address);
1022
1045
      if(debug){
1023
 
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1024
 
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1025
 
                     host_name, ip, (intmax_t)interface, port);
 
1046
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1047
                "Mandos server \"%s\" found on %s (%s, %"
 
1048
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
1049
                ip, (intmax_t)interface, port);
1026
1050
      }
1027
1051
      int ret = start_mandos_communication(ip, port, interface,
1028
1052
                                           avahi_proto_to_af(proto));
1060
1084
  default:
1061
1085
  case AVAHI_BROWSER_FAILURE:
1062
1086
    
1063
 
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1064
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1087
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1088
            "(Avahi browser) %s\n",
 
1089
            avahi_strerror(avahi_server_errno(mc.server)));
1065
1090
    avahi_simple_poll_quit(mc.simple_poll);
1066
1091
    return;
1067
1092
    
1074
1099
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1075
1100
                                    name, type, domain, protocol, 0,
1076
1101
                                    resolve_callback, NULL) == NULL)
1077
 
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1078
 
                   " %s\n", name,
1079
 
                   avahi_strerror(avahi_server_errno(mc.server)));
 
1102
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1103
              "Avahi: Failed to resolve service '%s': %s\n",
 
1104
              name, avahi_strerror(avahi_server_errno(mc.server)));
1080
1105
    break;
1081
1106
    
1082
1107
  case AVAHI_BROWSER_REMOVE:
1085
1110
  case AVAHI_BROWSER_ALL_FOR_NOW:
1086
1111
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1087
1112
    if(debug){
1088
 
      fprintf_plus(stderr, "No Mandos server found, still"
1089
 
                   " searching...\n");
 
1113
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1114
              "No Mandos server found, still searching...\n");
1090
1115
    }
1091
1116
    break;
1092
1117
  }
1131
1156
  /* Reject the loopback device */
1132
1157
  if(ifr->ifr_flags & IFF_LOOPBACK){
1133
1158
    if(debug){
1134
 
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1135
 
                   ifname);
 
1159
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1160
              "Rejecting loopback interface \"%s\"\n", ifname);
1136
1161
    }
1137
1162
    return false;
1138
1163
  }
1139
1164
  /* Accept point-to-point devices only if connect_to is specified */
1140
1165
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1141
1166
    if(debug){
1142
 
      fprintf_plus(stderr, "Accepting point-to-point interface"
1143
 
                   " \"%s\"\n", ifname);
 
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1168
              "Accepting point-to-point interface \"%s\"\n", ifname);
1144
1169
    }
1145
1170
    return true;
1146
1171
  }
1147
1172
  /* Otherwise, reject non-broadcast-capable devices */
1148
1173
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1149
1174
    if(debug){
1150
 
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
 
                   " \"%s\"\n", ifname);
 
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1176
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1152
1177
    }
1153
1178
    return false;
1154
1179
  }
1155
1180
  /* Reject non-ARP interfaces (including dummy interfaces) */
1156
1181
  if(ifr->ifr_flags & IFF_NOARP){
1157
1182
    if(debug){
1158
 
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1159
 
                   ifname);
 
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1184
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1160
1185
    }
1161
1186
    return false;
1162
1187
  }
1163
1188
  
1164
1189
  /* Accept this device */
1165
1190
  if(debug){
1166
 
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
 
1191
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1192
            "Interface \"%s\" is good\n", ifname);
1167
1193
  }
1168
1194
  return true;
1169
1195
}
1181
1207
  struct ifreq ifr;
1182
1208
  if(not get_flags(if_entry->d_name, &ifr)){
1183
1209
    if(debug){
1184
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1185
 
                   "\"%s\"\n", if_entry->d_name);
 
1210
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1211
              "Failed to get flags for interface \"%s\"\n",
 
1212
              if_entry->d_name);
1186
1213
    }
1187
1214
    return 0;
1188
1215
  }
1206
1233
  struct ifreq ifr;
1207
1234
  if(not get_flags(if_entry->d_name, &ifr)){
1208
1235
    if(debug){
1209
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1210
 
                   "\"%s\"\n", if_entry->d_name);
 
1236
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1237
              "Failed to get flags for interface \"%s\"\n",
 
1238
              if_entry->d_name);
1211
1239
    }
1212
1240
    return 0;
1213
1241
  }
1215
1243
  /* Reject down interfaces */
1216
1244
  if(not (ifr.ifr_flags & IFF_UP)){
1217
1245
    if(debug){
1218
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1219
 
                   if_entry->d_name);
 
1246
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1247
              "Rejecting down interface \"%s\"\n",
 
1248
              if_entry->d_name);
1220
1249
    }
1221
1250
    return 0;
1222
1251
  }
1224
1253
  /* Reject non-running interfaces */
1225
1254
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1226
1255
    if(debug){
1227
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1228
 
                   if_entry->d_name);
 
1256
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1257
              "Rejecting non-running interface \"%s\"\n",
 
1258
              if_entry->d_name);
1229
1259
    }
1230
1260
    return 0;
1231
1261
  }
1265
1295
  if((direntry->d_name)[sret] != '\0'){
1266
1296
    /* Contains non-allowed characters */
1267
1297
    if(debug){
1268
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1269
 
                   direntry->d_name);
 
1298
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1299
              "Ignoring hook \"%s\" with bad name\n",
 
1300
              direntry->d_name);
1270
1301
    }
1271
1302
    return 0;
1272
1303
  }
1288
1319
  if(not (S_ISREG(st.st_mode))){
1289
1320
    /* Not a regular file */
1290
1321
    if(debug){
1291
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1292
 
                   direntry->d_name);
 
1322
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1323
              "Ignoring hook \"%s\" - not a file\n",
 
1324
              direntry->d_name);
1293
1325
    }
1294
1326
    return 0;
1295
1327
  }
1296
1328
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1297
1329
    /* Not executable */
1298
1330
    if(debug){
1299
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1300
 
                   direntry->d_name);
 
1331
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1332
              "Ignoring hook \"%s\" - not executable\n",
 
1333
              direntry->d_name);
1301
1334
    }
1302
1335
    return 0;
1303
1336
  }
1313
1346
  while(true){
1314
1347
    if(mc.current_server == NULL){
1315
1348
      if (debug){
1316
 
        fprintf_plus(stderr, "Wait until first server is found."
1317
 
                     " No timeout!\n");
 
1349
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1350
                "Wait until first server is found. No timeout!\n");
1318
1351
      }
1319
1352
      ret = avahi_simple_poll_iterate(s, -1);
1320
1353
    } else {
1321
1354
      if (debug){
1322
 
        fprintf_plus(stderr, "Check current_server if we should run"
1323
 
                     " it, or wait\n");
 
1355
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1356
                "Check current_server if we should run it,"
 
1357
                " or wait\n");
1324
1358
      }
1325
1359
      /* the current time */
1326
1360
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1342
1376
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1343
1377
      
1344
1378
      if (debug){
1345
 
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1346
 
                     block_time);
 
1379
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1380
                "Blocking for %" PRIdMAX " ms\n", block_time);
1347
1381
      }
1348
1382
      
1349
1383
      if(block_time <= 0){
1445
1479
        }
1446
1480
        if(WIFEXITED(status)){
1447
1481
          if(WEXITSTATUS(status) != 0){
1448
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1449
 
                         " with status %d\n", direntry->d_name,
1450
 
                         WEXITSTATUS(status));
 
1482
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1483
                    "Warning: network hook \"%s\" exited"
 
1484
                    " with status %d\n", direntry->d_name,
 
1485
                    WEXITSTATUS(status));
1451
1486
            free(fullname);
1452
1487
            continue;
1453
1488
          }
1454
1489
        } else if(WIFSIGNALED(status)){
1455
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1456
 
                       " signal %d\n", direntry->d_name,
1457
 
                       WTERMSIG(status));
 
1490
          fprintf(stderr, "Mandos plugin mandos-client: "
 
1491
                  "Warning: network hook \"%s\" died by"
 
1492
                  " signal %d\n", direntry->d_name,
 
1493
                  WTERMSIG(status));
1458
1494
          free(fullname);
1459
1495
          continue;
1460
1496
        } else {
1461
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1462
 
                       " crashed\n", direntry->d_name);
 
1497
          fprintf(stderr, "Mandos plugin mandos-client: "
 
1498
                  "Warning: network hook \"%s\" crashed\n",
 
1499
                  direntry->d_name);
1463
1500
          free(fullname);
1464
1501
          continue;
1465
1502
        }
1637
1674
        argp_state_help(state, state->out_stream,
1638
1675
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1676
      case 'V':                 /* --version */
1640
 
        fprintf_plus(state->out_stream,
1641
 
                     "Mandos plugin mandos-client: ");
1642
 
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
 
1677
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
 
1678
        fprintf(state->out_stream, "%s\n", argp_program_version);
1643
1679
        exit(argp_err_exit_status);
1644
1680
        break;
1645
1681
      default:
1690
1726
        if(ret == -1){
1691
1727
          perror_plus("fstat");
1692
1728
        } else {
1693
 
          if(S_ISREG(st.st_mode)
1694
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1729
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1695
1730
            ret = fchown(seckey_fd, uid, gid);
1696
1731
            if(ret == -1){
1697
1732
              perror_plus("fchown");
1711
1746
        if(ret == -1){
1712
1747
          perror_plus("fstat");
1713
1748
        } else {
1714
 
          if(S_ISREG(st.st_mode)
1715
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1749
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1716
1750
            ret = fchown(pubkey_fd, uid, gid);
1717
1751
            if(ret == -1){
1718
1752
              perror_plus("fchown");
1769
1803
      /* Pick the first interface returned */
1770
1804
      interface = strdup(direntries[0]->d_name);
1771
1805
      if(debug){
1772
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1806
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1807
                "Using interface \"%s\"\n", interface);
1773
1808
      }
1774
1809
      if(interface == NULL){
1775
1810
        perror_plus("malloc");
1780
1815
      free(direntries);
1781
1816
    } else {
1782
1817
      free(direntries);
1783
 
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1818
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1819
              "Could not find a network interface\n");
1784
1820
      exitcode = EXIT_FAILURE;
1785
1821
      goto end;
1786
1822
    }
1792
1828
  srand((unsigned int) time(NULL));
1793
1829
  mc.simple_poll = avahi_simple_poll_new();
1794
1830
  if(mc.simple_poll == NULL){
1795
 
    fprintf_plus(stderr,
1796
 
                 "Avahi: Failed to create simple poll object.\n");
 
1831
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1832
            "Avahi: Failed to create simple poll object.\n");
1797
1833
    exitcode = EX_UNAVAILABLE;
1798
1834
    goto end;
1799
1835
  }
1865
1901
  if(strcmp(interface, "none") != 0){
1866
1902
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1867
1903
    if(if_index == 0){
1868
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1904
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1905
              "No such interface: \"%s\"\n", interface);
1869
1906
      exitcode = EX_UNAVAILABLE;
1870
1907
      goto end;
1871
1908
    }
2004
2041
  
2005
2042
  ret = init_gnutls_global(pubkey, seckey);
2006
2043
  if(ret == -1){
2007
 
    fprintf_plus(stderr, "init_gnutls_global failed\n");
 
2044
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2045
            "init_gnutls_global failed\n");
2008
2046
    exitcode = EX_UNAVAILABLE;
2009
2047
    goto end;
2010
2048
  } else {
2026
2064
  }
2027
2065
  
2028
2066
  if(not init_gpgme(pubkey, seckey, tempdir)){
2029
 
    fprintf_plus(stderr, "init_gpgme failed\n");
 
2067
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2068
            "init_gpgme failed\n");
2030
2069
    exitcode = EX_UNAVAILABLE;
2031
2070
    goto end;
2032
2071
  } else {
2042
2081
    /* (Mainly meant for debugging) */
2043
2082
    char *address = strrchr(connect_to, ':');
2044
2083
    if(address == NULL){
2045
 
      fprintf_plus(stderr, "No colon in address\n");
 
2084
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2085
              "No colon in address\n");
2046
2086
      exitcode = EX_USAGE;
2047
2087
      goto end;
2048
2088
    }
2056
2096
    tmpmax = strtoimax(address+1, &tmp, 10);
2057
2097
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2058
2098
       or tmpmax != (uint16_t)tmpmax){
2059
 
      fprintf_plus(stderr, "Bad port number\n");
 
2099
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2100
              "Bad port number\n");
2060
2101
      exitcode = EX_USAGE;
2061
2102
      goto end;
2062
2103
    }
2092
2133
        break;
2093
2134
      }
2094
2135
      if(debug){
2095
 
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2096
 
                     (int)retry_interval);
 
2136
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2137
                "Retrying in %d seconds\n", (int)retry_interval);
2097
2138
      }
2098
2139
      sleep((int)retry_interval);
2099
2140
    }
2129
2170
  
2130
2171
  /* Check if creating the Avahi server object succeeded */
2131
2172
  if(mc.server == NULL){
2132
 
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2133
 
                 avahi_strerror(error));
 
2173
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2174
            "Failed to create Avahi server: %s\n",
 
2175
            avahi_strerror(error));
2134
2176
    exitcode = EX_UNAVAILABLE;
2135
2177
    goto end;
2136
2178
  }
2144
2186
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2145
2187
                                   NULL, 0, browse_callback, NULL);
2146
2188
  if(sb == NULL){
2147
 
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2148
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
2189
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2190
            "Failed to create service browser: %s\n",
 
2191
            avahi_strerror(avahi_server_errno(mc.server)));
2149
2192
    exitcode = EX_UNAVAILABLE;
2150
2193
    goto end;
2151
2194
  }
2157
2200
  /* Run the main loop */
2158
2201
  
2159
2202
  if(debug){
2160
 
    fprintf_plus(stderr, "Starting Avahi loop search\n");
 
2203
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2204
            "Starting Avahi loop search\n");
2161
2205
  }
2162
2206
 
2163
2207
  ret = avahi_loop_with_timeout(mc.simple_poll,
2164
2208
                                (int)(retry_interval * 1000));
2165
2209
  if(debug){
2166
 
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2167
 
                 (ret == 0) ? "successfully" : "with error");
 
2210
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2211
            "avahi_loop_with_timeout exited %s\n",
 
2212
            (ret == 0) ? "successfully" : "with error");
2168
2213
  }
2169
2214
  
2170
2215
 end:
2171
2216
  
2172
2217
  if(debug){
2173
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2218
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2219
            "%s exiting\n", argv[0]);
2174
2220
  }
2175
2221
  
2176
2222
  /* Cleanup things */
2259
2305
        }
2260
2306
        ret = remove(fullname);
2261
2307
        if(ret == -1){
2262
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2263
 
                       strerror(errno));
 
2308
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2309
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2264
2310
        }
2265
2311
        free(fullname);
2266
2312
      }