/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
178
178
  va_list ap;
179
179
  va_start (ap, format);
180
180
  
181
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
 
                             program_invocation_short_name));
 
181
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
183
182
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
184
183
}
185
184
 
262
261
    
263
262
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
264
263
    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));
 
264
      fprintf(stderr, "Mandos plugin mandos-client: "
 
265
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
266
              gpgme_strsource(rc), gpgme_strerror(rc));
267
267
      return false;
268
268
    }
269
269
    
270
270
    rc = gpgme_op_import(mc.ctx, pgp_data);
271
271
    if(rc != GPG_ERR_NO_ERROR){
272
 
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
272
      fprintf(stderr, "Mandos plugin mandos-client: "
 
273
              "bad gpgme_op_import: %s: %s\n",
 
274
              gpgme_strsource(rc), gpgme_strerror(rc));
274
275
      return false;
275
276
    }
276
277
    
283
284
  }
284
285
  
285
286
  if(debug){
286
 
    fprintf_plus(stderr, "Initializing GPGME\n");
 
287
    fprintf(stderr, "Mandos plugin mandos-client: "
 
288
            "Initializing GPGME\n");
287
289
  }
288
290
  
289
291
  /* Init GPGME */
290
292
  gpgme_check_version(NULL);
291
293
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
292
294
  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));
 
295
    fprintf(stderr, "Mandos plugin mandos-client: "
 
296
            "bad gpgme_engine_check_version: %s: %s\n",
 
297
            gpgme_strsource(rc), gpgme_strerror(rc));
295
298
    return false;
296
299
  }
297
300
  
298
301
  /* Set GPGME home directory for the OpenPGP engine only */
299
302
  rc = gpgme_get_engine_info(&engine_info);
300
303
  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));
 
304
    fprintf(stderr, "Mandos plugin mandos-client: "
 
305
            "bad gpgme_get_engine_info: %s: %s\n",
 
306
            gpgme_strsource(rc), gpgme_strerror(rc));
303
307
    return false;
304
308
  }
305
309
  while(engine_info != NULL){
311
315
    engine_info = engine_info->next;
312
316
  }
313
317
  if(engine_info == NULL){
314
 
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
315
 
                 tempdir);
 
318
    fprintf(stderr, "Mandos plugin mandos-client: "
 
319
            "Could not set GPGME home dir to %s\n", tempdir);
316
320
    return false;
317
321
  }
318
322
  
319
323
  /* Create new GPGME "context" */
320
324
  rc = gpgme_new(&(mc.ctx));
321
325
  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));
 
326
    fprintf(stderr, "Mandos plugin mandos-client: "
 
327
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
328
            gpgme_strerror(rc));
325
329
    return false;
326
330
  }
327
331
  
346
350
  ssize_t plaintext_length = 0;
347
351
  
348
352
  if(debug){
349
 
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
 
353
    fprintf(stderr, "Mandos plugin mandos-client: "
 
354
            "Trying to decrypt OpenPGP data\n");
350
355
  }
351
356
  
352
357
  /* Create new GPGME data buffer from memory cryptotext */
353
358
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
354
359
                               0);
355
360
  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));
 
361
    fprintf(stderr, "Mandos plugin mandos-client: "
 
362
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
363
            gpgme_strsource(rc), gpgme_strerror(rc));
358
364
    return -1;
359
365
  }
360
366
  
361
367
  /* Create new empty GPGME data buffer for the plaintext */
362
368
  rc = gpgme_data_new(&dh_plain);
363
369
  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));
 
370
    fprintf(stderr, "Mandos plugin mandos-client: "
 
371
            "bad gpgme_data_new: %s: %s\n",
 
372
            gpgme_strsource(rc), gpgme_strerror(rc));
367
373
    gpgme_data_release(dh_crypto);
368
374
    return -1;
369
375
  }
372
378
     data buffer */
373
379
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
374
380
  if(rc != GPG_ERR_NO_ERROR){
375
 
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
381
    fprintf(stderr, "Mandos plugin mandos-client: "
 
382
            "bad gpgme_op_decrypt: %s: %s\n",
 
383
            gpgme_strsource(rc), gpgme_strerror(rc));
377
384
    plaintext_length = -1;
378
385
    if(debug){
379
386
      gpgme_decrypt_result_t result;
380
387
      result = gpgme_op_decrypt_result(mc.ctx);
381
388
      if(result == NULL){
382
 
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
 
389
        fprintf(stderr, "Mandos plugin mandos-client: "
 
390
                "gpgme_op_decrypt_result failed\n");
383
391
      } 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);
 
392
        fprintf(stderr, "Mandos plugin mandos-client: "
 
393
                "Unsupported algorithm: %s\n",
 
394
                result->unsupported_algorithm);
 
395
        fprintf(stderr, "Mandos plugin mandos-client: "
 
396
                "Wrong key usage: %u\n",
 
397
                result->wrong_key_usage);
388
398
        if(result->file_name != NULL){
389
 
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
 
399
          fprintf(stderr, "Mandos plugin mandos-client: "
 
400
                  "File name: %s\n", result->file_name);
390
401
        }
391
402
        gpgme_recipient_t recipient;
392
403
        recipient = result->recipients;
393
404
        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");
 
405
          fprintf(stderr, "Mandos plugin mandos-client: "
 
406
                  "Public key algorithm: %s\n",
 
407
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
408
          fprintf(stderr, "Mandos plugin mandos-client: "
 
409
                  "Key ID: %s\n", recipient->keyid);
 
410
          fprintf(stderr, "Mandos plugin mandos-client: "
 
411
                  "Secret key available: %s\n",
 
412
                  recipient->status == GPG_ERR_NO_SECKEY
 
413
                  ? "No" : "Yes");
401
414
          recipient = recipient->next;
402
415
        }
403
416
      }
406
419
  }
407
420
  
408
421
  if(debug){
409
 
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
 
422
    fprintf(stderr, "Mandos plugin mandos-client: "
 
423
            "Decryption of OpenPGP data succeeded\n");
410
424
  }
411
425
  
412
426
  /* Seek back to the beginning of the GPGME plaintext data buffer */
443
457
  }
444
458
  
445
459
  if(debug){
446
 
    fprintf_plus(stderr, "Decrypted password is: ");
 
460
    fprintf(stderr, "Mandos plugin mandos-client: "
 
461
            "Decrypted password is: ");
447
462
    for(ssize_t i = 0; i < plaintext_length; i++){
448
463
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
449
464
    }
471
486
/* GnuTLS log function callback */
472
487
static void debuggnutls(__attribute__((unused)) int level,
473
488
                        const char* string){
474
 
  fprintf_plus(stderr, "GnuTLS: %s", string);
 
489
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
475
490
}
476
491
 
477
492
static int init_gnutls_global(const char *pubkeyfilename,
479
494
  int ret;
480
495
  
481
496
  if(debug){
482
 
    fprintf_plus(stderr, "Initializing GnuTLS\n");
 
497
    fprintf(stderr, "Mandos plugin mandos-client: "
 
498
            "Initializing GnuTLS\n");
483
499
  }
484
500
  
485
501
  ret = gnutls_global_init();
486
502
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
 
                 safer_gnutls_strerror(ret));
 
503
    fprintf(stderr, "Mandos plugin mandos-client: "
 
504
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
489
505
    return -1;
490
506
  }
491
507
  
500
516
  /* OpenPGP credentials */
501
517
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
502
518
  if(ret != GNUTLS_E_SUCCESS){
503
 
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
 
                 safer_gnutls_strerror(ret));
 
519
    fprintf(stderr, "Mandos plugin mandos-client: "
 
520
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
505
521
    gnutls_global_deinit();
506
522
    return -1;
507
523
  }
508
524
  
509
525
  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);
 
526
    fprintf(stderr, "Mandos plugin mandos-client: "
 
527
            "Attempting to use OpenPGP public key %s and"
 
528
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
529
            seckeyfilename);
514
530
  }
515
531
  
516
532
  ret = gnutls_certificate_set_openpgp_key_file
517
533
    (mc.cred, pubkeyfilename, seckeyfilename,
518
534
     GNUTLS_OPENPGP_FMT_BASE64);
519
535
  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));
 
536
    fprintf(stderr,
 
537
            "Mandos plugin mandos-client: "
 
538
            "Error[%d] while reading the OpenPGP key pair ('%s',"
 
539
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
540
    fprintf(stderr, "Mandos plugin mandos-client: "
 
541
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
525
542
    goto globalfail;
526
543
  }
527
544
  
528
545
  /* GnuTLS server initialization */
529
546
  ret = gnutls_dh_params_init(&mc.dh_params);
530
547
  if(ret != GNUTLS_E_SUCCESS){
531
 
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
 
                 " initialization: %s\n",
533
 
                 safer_gnutls_strerror(ret));
 
548
    fprintf(stderr, "Mandos plugin mandos-client: "
 
549
            "Error in GnuTLS DH parameter initialization:"
 
550
            " %s\n", safer_gnutls_strerror(ret));
534
551
    goto globalfail;
535
552
  }
536
553
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
537
554
  if(ret != GNUTLS_E_SUCCESS){
538
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
 
                 safer_gnutls_strerror(ret));
 
555
    fprintf(stderr, "Mandos plugin mandos-client: "
 
556
            "Error in GnuTLS prime generation: %s\n",
 
557
            safer_gnutls_strerror(ret));
540
558
    goto globalfail;
541
559
  }
542
560
  
562
580
    }
563
581
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
564
582
  if(ret != GNUTLS_E_SUCCESS){
565
 
    fprintf_plus(stderr,
566
 
                 "Error in GnuTLS session initialization: %s\n",
567
 
                 safer_gnutls_strerror(ret));
 
583
    fprintf(stderr, "Mandos plugin mandos-client: "
 
584
            "Error in GnuTLS session initialization: %s\n",
 
585
            safer_gnutls_strerror(ret));
568
586
  }
569
587
  
570
588
  {
577
595
      }
578
596
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
579
597
    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));
 
598
      fprintf(stderr, "Mandos plugin mandos-client: "
 
599
              "Syntax error at: %s\n", err);
 
600
      fprintf(stderr, "Mandos plugin mandos-client: "
 
601
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
583
602
      gnutls_deinit(*session);
584
603
      return -1;
585
604
    }
594
613
    }
595
614
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
596
615
  if(ret != GNUTLS_E_SUCCESS){
597
 
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
598
 
                 safer_gnutls_strerror(ret));
 
616
    fprintf(stderr, "Mandos plugin mandos-client: "
 
617
            "Error setting GnuTLS credentials: %s\n",
 
618
            safer_gnutls_strerror(ret));
599
619
    gnutls_deinit(*session);
600
620
    return -1;
601
621
  }
646
666
    pf = PF_INET;
647
667
    break;
648
668
  default:
649
 
    fprintf_plus(stderr, "Bad address family: %d\n", af);
 
669
    fprintf(stderr, "Mandos plugin mandos-client: "
 
670
            "Bad address family: %d\n", af);
650
671
    errno = EINVAL;
651
672
    return -1;
652
673
  }
657
678
  }
658
679
  
659
680
  if(debug){
660
 
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
661
 
                 PRIu16 "\n", ip, port);
 
681
    fprintf(stderr, "Mandos plugin mandos-client: "
 
682
            "Setting up a TCP connection to %s, port %" PRIu16
 
683
            "\n", ip, port);
662
684
  }
663
685
  
664
686
  tcp_sd = socket(pf, SOCK_STREAM, 0);
690
712
  }
691
713
  if(ret == 0){
692
714
    int e = errno;
693
 
    fprintf_plus(stderr, "Bad address: %s\n", ip);
 
715
    fprintf(stderr, "Mandos plugin mandos-client: "
 
716
            "Bad address: %s\n", ip);
694
717
    errno = e;
695
718
    goto mandos_end;
696
719
  }
703
726
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
704
727
                                -Wunreachable-code*/
705
728
      if(if_index == AVAHI_IF_UNSPEC){
706
 
        fprintf_plus(stderr, "An IPv6 link-local address is"
707
 
                     " incomplete without a network interface\n");
 
729
        fprintf(stderr, "Mandos plugin mandos-client: "
 
730
                "An IPv6 link-local address is incomplete"
 
731
                " without a network interface\n");
708
732
        errno = EINVAL;
709
733
        goto mandos_end;
710
734
      }
728
752
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
729
753
        perror_plus("if_indextoname");
730
754
      } else {
731
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
 
                     "\n", ip, interface, port);
 
755
        fprintf(stderr, "Mandos plugin mandos-client: "
 
756
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
757
                ip, interface, port);
733
758
      }
734
759
    } else {
735
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
736
 
                   ip, port);
 
760
      fprintf(stderr, "Mandos plugin mandos-client: "
 
761
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
737
762
    }
738
763
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
739
764
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
749
774
      perror_plus("inet_ntop");
750
775
    } else {
751
776
      if(strcmp(addrstr, ip) != 0){
752
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
777
        fprintf(stderr, "Mandos plugin mandos-client: "
 
778
                "Canonical address form: %s\n", addrstr);
753
779
      }
754
780
    }
755
781
  }
809
835
  }
810
836
  
811
837
  if(debug){
812
 
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
 
838
    fprintf(stderr, "Mandos plugin mandos-client: "
 
839
            "Establishing TLS session with %s\n", ip);
813
840
  }
814
841
  
815
842
  if(quit_now){
835
862
  
836
863
  if(ret != GNUTLS_E_SUCCESS){
837
864
    if(debug){
838
 
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
 
865
      fprintf(stderr, "Mandos plugin mandos-client: "
 
866
              "*** GnuTLS Handshake failed ***\n");
839
867
      gnutls_perror(ret);
840
868
    }
841
869
    errno = EPROTO;
845
873
  /* Read OpenPGP packet that contains the wanted password */
846
874
  
847
875
  if(debug){
848
 
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
849
 
                 " %s\n", ip);
 
876
    fprintf(stderr, "Mandos plugin mandos-client: "
 
877
            "Retrieving OpenPGP encrypted password from %s\n", ip);
850
878
  }
851
879
  
852
880
  while(true){
890
918
          }
891
919
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
892
920
        if(ret < 0){
893
 
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
894
 
                       "***\n");
 
921
          fprintf(stderr, "Mandos plugin mandos-client: "
 
922
                  "*** GnuTLS Re-handshake failed ***\n");
895
923
          gnutls_perror(ret);
896
924
          errno = EPROTO;
897
925
          goto mandos_end;
898
926
        }
899
927
        break;
900
928
      default:
901
 
        fprintf_plus(stderr, "Unknown error while reading data from"
902
 
                     " encrypted session with Mandos server\n");
 
929
        fprintf(stderr, "Mandos plugin mandos-client: "
 
930
                "Unknown error while reading data from"
 
931
                " encrypted session with Mandos server\n");
903
932
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
904
933
        errno = EIO;
905
934
        goto mandos_end;
910
939
  }
911
940
  
912
941
  if(debug){
913
 
    fprintf_plus(stderr, "Closing TLS session\n");
 
942
    fprintf(stderr, "Mandos plugin mandos-client: "
 
943
            "Closing TLS session\n");
914
944
  }
915
945
  
916
946
  if(quit_now){
945
975
        if(ret == 0 and ferror(stdout)){
946
976
          int e = errno;
947
977
          if(debug){
948
 
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
949
 
                         strerror(errno));
 
978
            fprintf(stderr, "Mandos plugin mandos-client: "
 
979
                    "Error writing encrypted data: %s\n",
 
980
                    strerror(errno));
950
981
          }
951
982
          errno = e;
952
983
          goto mandos_end;
1009
1040
  switch(event){
1010
1041
  default:
1011
1042
  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)));
 
1043
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1044
            "(Avahi Resolver) Failed to resolve service '%s'"
 
1045
            " of type '%s' in domain '%s': %s\n", name, type, domain,
 
1046
            avahi_strerror(avahi_server_errno(mc.server)));
1016
1047
    break;
1017
1048
    
1018
1049
  case AVAHI_RESOLVER_FOUND:
1020
1051
      char ip[AVAHI_ADDRESS_STR_MAX];
1021
1052
      avahi_address_snprint(ip, sizeof(ip), address);
1022
1053
      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);
 
1054
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1055
                "Mandos server \"%s\" found on %s (%s, %"
 
1056
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
1057
                ip, (intmax_t)interface, port);
1026
1058
      }
1027
1059
      int ret = start_mandos_communication(ip, port, interface,
1028
1060
                                           avahi_proto_to_af(proto));
1060
1092
  default:
1061
1093
  case AVAHI_BROWSER_FAILURE:
1062
1094
    
1063
 
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1064
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1095
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1096
            "(Avahi browser) %s\n",
 
1097
            avahi_strerror(avahi_server_errno(mc.server)));
1065
1098
    avahi_simple_poll_quit(mc.simple_poll);
1066
1099
    return;
1067
1100
    
1074
1107
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1075
1108
                                    name, type, domain, protocol, 0,
1076
1109
                                    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)));
 
1110
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1111
              "Avahi: Failed to resolve service '%s': %s\n",
 
1112
              name, avahi_strerror(avahi_server_errno(mc.server)));
1080
1113
    break;
1081
1114
    
1082
1115
  case AVAHI_BROWSER_REMOVE:
1085
1118
  case AVAHI_BROWSER_ALL_FOR_NOW:
1086
1119
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1087
1120
    if(debug){
1088
 
      fprintf_plus(stderr, "No Mandos server found, still"
1089
 
                   " searching...\n");
 
1121
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1122
              "No Mandos server found, still searching...\n");
1090
1123
    }
1091
1124
    break;
1092
1125
  }
1131
1164
  /* Reject the loopback device */
1132
1165
  if(ifr->ifr_flags & IFF_LOOPBACK){
1133
1166
    if(debug){
1134
 
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1135
 
                   ifname);
 
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1168
              "Rejecting loopback interface \"%s\"\n", ifname);
1136
1169
    }
1137
1170
    return false;
1138
1171
  }
1139
1172
  /* Accept point-to-point devices only if connect_to is specified */
1140
1173
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1141
1174
    if(debug){
1142
 
      fprintf_plus(stderr, "Accepting point-to-point interface"
1143
 
                   " \"%s\"\n", ifname);
 
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1176
              "Accepting point-to-point interface \"%s\"\n", ifname);
1144
1177
    }
1145
1178
    return true;
1146
1179
  }
1147
1180
  /* Otherwise, reject non-broadcast-capable devices */
1148
1181
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1149
1182
    if(debug){
1150
 
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
 
                   " \"%s\"\n", ifname);
 
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1184
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1152
1185
    }
1153
1186
    return false;
1154
1187
  }
1155
1188
  /* Reject non-ARP interfaces (including dummy interfaces) */
1156
1189
  if(ifr->ifr_flags & IFF_NOARP){
1157
1190
    if(debug){
1158
 
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1159
 
                   ifname);
 
1191
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1192
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1160
1193
    }
1161
1194
    return false;
1162
1195
  }
1163
1196
  
1164
1197
  /* Accept this device */
1165
1198
  if(debug){
1166
 
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
 
1199
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1200
            "Interface \"%s\" is good\n", ifname);
1167
1201
  }
1168
1202
  return true;
1169
1203
}
1181
1215
  struct ifreq ifr;
1182
1216
  if(not get_flags(if_entry->d_name, &ifr)){
1183
1217
    if(debug){
1184
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1185
 
                   "\"%s\"\n", if_entry->d_name);
 
1218
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1219
              "Failed to get flags for interface \"%s\"\n",
 
1220
              if_entry->d_name);
1186
1221
    }
1187
1222
    return 0;
1188
1223
  }
1206
1241
  struct ifreq ifr;
1207
1242
  if(not get_flags(if_entry->d_name, &ifr)){
1208
1243
    if(debug){
1209
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1210
 
                   "\"%s\"\n", if_entry->d_name);
 
1244
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1245
              "Failed to get flags for interface \"%s\"\n",
 
1246
              if_entry->d_name);
1211
1247
    }
1212
1248
    return 0;
1213
1249
  }
1215
1251
  /* Reject down interfaces */
1216
1252
  if(not (ifr.ifr_flags & IFF_UP)){
1217
1253
    if(debug){
1218
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1219
 
                   if_entry->d_name);
 
1254
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1255
              "Rejecting down interface \"%s\"\n",
 
1256
              if_entry->d_name);
1220
1257
    }
1221
1258
    return 0;
1222
1259
  }
1224
1261
  /* Reject non-running interfaces */
1225
1262
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1226
1263
    if(debug){
1227
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1228
 
                   if_entry->d_name);
 
1264
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1265
              "Rejecting non-running interface \"%s\"\n",
 
1266
              if_entry->d_name);
1229
1267
    }
1230
1268
    return 0;
1231
1269
  }
1265
1303
  if((direntry->d_name)[sret] != '\0'){
1266
1304
    /* Contains non-allowed characters */
1267
1305
    if(debug){
1268
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1269
 
                   direntry->d_name);
 
1306
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1307
              "Ignoring hook \"%s\" with bad name\n",
 
1308
              direntry->d_name);
1270
1309
    }
1271
1310
    return 0;
1272
1311
  }
1288
1327
  if(not (S_ISREG(st.st_mode))){
1289
1328
    /* Not a regular file */
1290
1329
    if(debug){
1291
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1292
 
                   direntry->d_name);
 
1330
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1331
              "Ignoring hook \"%s\" - not a file\n",
 
1332
              direntry->d_name);
1293
1333
    }
1294
1334
    return 0;
1295
1335
  }
1296
1336
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1297
1337
    /* Not executable */
1298
1338
    if(debug){
1299
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1300
 
                   direntry->d_name);
 
1339
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1340
              "Ignoring hook \"%s\" - not executable\n",
 
1341
              direntry->d_name);
1301
1342
    }
1302
1343
    return 0;
1303
1344
  }
1313
1354
  while(true){
1314
1355
    if(mc.current_server == NULL){
1315
1356
      if (debug){
1316
 
        fprintf_plus(stderr, "Wait until first server is found."
1317
 
                     " No timeout!\n");
 
1357
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1358
                "Wait until first server is found. No timeout!\n");
1318
1359
      }
1319
1360
      ret = avahi_simple_poll_iterate(s, -1);
1320
1361
    } else {
1321
1362
      if (debug){
1322
 
        fprintf_plus(stderr, "Check current_server if we should run"
1323
 
                     " it, or wait\n");
 
1363
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1364
                "Check current_server if we should run it,"
 
1365
                " or wait\n");
1324
1366
      }
1325
1367
      /* the current time */
1326
1368
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1342
1384
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1343
1385
      
1344
1386
      if (debug){
1345
 
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1346
 
                     block_time);
 
1387
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1388
                "Blocking for %" PRIdMAX " ms\n", block_time);
1347
1389
      }
1348
1390
      
1349
1391
      if(block_time <= 0){
1376
1418
  }
1377
1419
}
1378
1420
 
1379
 
bool run_network_hooks(const char *mode, const char *interface,
1380
 
                       const float delay){
1381
 
  struct dirent **direntries;
1382
 
  struct dirent *direntry;
1383
 
  int ret;
1384
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1385
 
                         alphasort);
1386
 
  if(numhooks == -1){
1387
 
    perror_plus("scandir");
1388
 
  } else {
1389
 
    int devnull = open("/dev/null", O_RDONLY);
1390
 
    for(int i = 0; i < numhooks; i++){
1391
 
      direntry = direntries[0];
1392
 
      char *fullname = NULL;
1393
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1394
 
      if(ret < 0){
1395
 
        perror_plus("asprintf");
1396
 
        continue;
1397
 
      }
1398
 
      pid_t hook_pid = fork();
1399
 
      if(hook_pid == 0){
1400
 
        /* Child */
1401
 
        dup2(devnull, STDIN_FILENO);
1402
 
        close(devnull);
1403
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1404
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1405
 
        if(ret == -1){
1406
 
          perror_plus("setenv");
1407
 
          return false;
1408
 
        }
1409
 
        ret = setenv("DEVICE", interface, 1);
1410
 
        if(ret == -1){
1411
 
          perror_plus("setenv");
1412
 
          return false;
1413
 
        }
1414
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1415
 
        if(ret == -1){
1416
 
          perror_plus("setenv");
1417
 
          return false;
1418
 
        }
1419
 
        ret = setenv("MODE", mode, 1);
1420
 
        if(ret == -1){
1421
 
          perror_plus("setenv");
1422
 
          return false;
1423
 
        }
1424
 
        char *delaystring;
1425
 
        ret = asprintf(&delaystring, "%f", delay);
1426
 
        if(ret == -1){
1427
 
          perror_plus("asprintf");
1428
 
          return false;
1429
 
        }
1430
 
        ret = setenv("DELAY", delaystring, 1);
1431
 
        if(ret == -1){
1432
 
          free(delaystring);
1433
 
          perror_plus("setenv");
1434
 
          return false;
1435
 
        }
1436
 
        free(delaystring);
1437
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1438
 
        perror_plus("execl");
1439
 
      } else {
1440
 
        int status;
1441
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1442
 
          perror_plus("waitpid");
1443
 
          free(fullname);
1444
 
          continue;
1445
 
        }
1446
 
        if(WIFEXITED(status)){
1447
 
          if(WEXITSTATUS(status) != 0){
1448
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1449
 
                         " with status %d\n", direntry->d_name,
1450
 
                         WEXITSTATUS(status));
1451
 
            free(fullname);
1452
 
            continue;
1453
 
          }
1454
 
        } else if(WIFSIGNALED(status)){
1455
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1456
 
                       " signal %d\n", direntry->d_name,
1457
 
                       WTERMSIG(status));
1458
 
          free(fullname);
1459
 
          continue;
1460
 
        } else {
1461
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1462
 
                       " crashed\n", direntry->d_name);
1463
 
          free(fullname);
1464
 
          continue;
1465
 
        }
1466
 
      }
1467
 
      free(fullname);
1468
 
      if(quit_now){
1469
 
        break;
1470
 
      }
1471
 
    }
1472
 
    close(devnull);
1473
 
  }
1474
 
  return true;
1475
 
}
1476
 
 
1477
1421
int main(int argc, char *argv[]){
1478
1422
  AvahiSServiceBrowser *sb = NULL;
1479
1423
  int error;
1637
1581
        argp_state_help(state, state->out_stream,
1638
1582
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1583
      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);
 
1584
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
 
1585
        fprintf(state->out_stream, "%s\n", argp_program_version);
1643
1586
        exit(argp_err_exit_status);
1644
1587
        break;
1645
1588
      default:
1690
1633
        if(ret == -1){
1691
1634
          perror_plus("fstat");
1692
1635
        } else {
1693
 
          if(S_ISREG(st.st_mode)
1694
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1636
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1695
1637
            ret = fchown(seckey_fd, uid, gid);
1696
1638
            if(ret == -1){
1697
1639
              perror_plus("fchown");
1711
1653
        if(ret == -1){
1712
1654
          perror_plus("fstat");
1713
1655
        } else {
1714
 
          if(S_ISREG(st.st_mode)
1715
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1656
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1716
1657
            ret = fchown(pubkey_fd, uid, gid);
1717
1658
            if(ret == -1){
1718
1659
              perror_plus("fchown");
1731
1672
    }
1732
1673
  }
1733
1674
  
1734
 
  /* Run network hooks */
 
1675
  /* Find network hooks and run them */
1735
1676
  {
1736
 
    /* Re-raise priviliges */
1737
 
    errno = 0;
1738
 
    ret = seteuid(0);
1739
 
    if(ret == -1){
1740
 
      perror_plus("seteuid");
1741
 
    }
1742
 
    if(not run_network_hooks("start", interface, delay)){
1743
 
      goto end;
1744
 
    }
1745
 
    /* Lower privileges */
1746
 
    errno = 0;
1747
 
    ret = seteuid(uid);
1748
 
    if(ret == -1){
1749
 
      perror_plus("seteuid");
 
1677
    struct dirent **direntries;
 
1678
    struct dirent *direntry;
 
1679
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1680
                           alphasort);
 
1681
    if(numhooks == -1){
 
1682
      perror_plus("scandir");
 
1683
    } else {
 
1684
      int devnull = open("/dev/null", O_RDONLY);
 
1685
      for(int i = 0; i < numhooks; i++){
 
1686
        direntry = direntries[0];
 
1687
        char *fullname = NULL;
 
1688
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1689
        if(ret < 0){
 
1690
          perror_plus("asprintf");
 
1691
          continue;
 
1692
        }
 
1693
        pid_t hook_pid = fork();
 
1694
        if(hook_pid == 0){
 
1695
          /* Child */
 
1696
          dup2(devnull, STDIN_FILENO);
 
1697
          close(devnull);
 
1698
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1699
          ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1700
          if(ret == -1){
 
1701
            perror_plus("setenv");
 
1702
            exit(1);
 
1703
          }
 
1704
          ret = setenv("DEVICE", interface, 1);
 
1705
          if(ret == -1){
 
1706
            perror_plus("setenv");
 
1707
            exit(1);
 
1708
          }
 
1709
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1710
          if(ret == -1){
 
1711
            perror_plus("setenv");
 
1712
            exit(1);
 
1713
          }
 
1714
          ret = setenv("MODE", "start", 1);
 
1715
          if(ret == -1){
 
1716
            perror_plus("setenv");
 
1717
            exit(1);
 
1718
          }
 
1719
          char *delaystring;
 
1720
          ret = asprintf(&delaystring, "%f", delay);
 
1721
          if(ret == -1){
 
1722
            perror_plus("asprintf");
 
1723
            exit(1);
 
1724
          }
 
1725
          ret = setenv("DELAY", delaystring, 1);
 
1726
          if(ret == -1){
 
1727
            free(delaystring);
 
1728
            perror_plus("setenv");
 
1729
            exit(1);
 
1730
          }
 
1731
          free(delaystring);
 
1732
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1733
          perror_plus("execl");
 
1734
        } else {
 
1735
          int status;
 
1736
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1737
            perror_plus("waitpid");
 
1738
            free(fullname);
 
1739
            continue;
 
1740
          }
 
1741
          if(WIFEXITED(status)){
 
1742
            if(WEXITSTATUS(status) != 0){
 
1743
              fprintf(stderr, "Mandos plugin mandos-client: "
 
1744
                      "Warning: network hook \"%s\" exited"
 
1745
                      " with status %d\n", direntry->d_name,
 
1746
                      WEXITSTATUS(status));
 
1747
              free(fullname);
 
1748
              continue;
 
1749
            }
 
1750
          } else if(WIFSIGNALED(status)){
 
1751
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1752
                    "Warning: network hook \"%s\" died by"
 
1753
                    " signal %d\n", direntry->d_name,
 
1754
                    WTERMSIG(status));
 
1755
            free(fullname);
 
1756
            continue;
 
1757
          } else {
 
1758
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1759
                    "Warning: network hook \"%s\" crashed\n",
 
1760
                    direntry->d_name);
 
1761
            free(fullname);
 
1762
            continue;
 
1763
          }
 
1764
        }
 
1765
        free(fullname);
 
1766
        if(quit_now){
 
1767
          goto end;
 
1768
        }
 
1769
      }
 
1770
      close(devnull);
1750
1771
    }
1751
1772
  }
1752
1773
  
1769
1790
      /* Pick the first interface returned */
1770
1791
      interface = strdup(direntries[0]->d_name);
1771
1792
      if(debug){
1772
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1793
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1794
                "Using interface \"%s\"\n", interface);
1773
1795
      }
1774
1796
      if(interface == NULL){
1775
1797
        perror_plus("malloc");
1780
1802
      free(direntries);
1781
1803
    } else {
1782
1804
      free(direntries);
1783
 
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1805
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1806
              "Could not find a network interface\n");
1784
1807
      exitcode = EXIT_FAILURE;
1785
1808
      goto end;
1786
1809
    }
1792
1815
  srand((unsigned int) time(NULL));
1793
1816
  mc.simple_poll = avahi_simple_poll_new();
1794
1817
  if(mc.simple_poll == NULL){
1795
 
    fprintf_plus(stderr,
1796
 
                 "Avahi: Failed to create simple poll object.\n");
 
1818
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1819
            "Avahi: Failed to create simple poll object.\n");
1797
1820
    exitcode = EX_UNAVAILABLE;
1798
1821
    goto end;
1799
1822
  }
1865
1888
  if(strcmp(interface, "none") != 0){
1866
1889
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1867
1890
    if(if_index == 0){
1868
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1891
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1892
              "No such interface: \"%s\"\n", interface);
1869
1893
      exitcode = EX_UNAVAILABLE;
1870
1894
      goto end;
1871
1895
    }
1991
2015
#endif  /* __linux__ */
1992
2016
    /* Lower privileges */
1993
2017
    errno = 0;
1994
 
    /* Lower privileges */
1995
 
    ret = seteuid(uid);
1996
 
    if(ret == -1){
1997
 
      perror_plus("seteuid");
 
2018
    if(take_down_interface){
 
2019
      /* Lower privileges */
 
2020
      ret = seteuid(uid);
 
2021
      if(ret == -1){
 
2022
        perror_plus("seteuid");
 
2023
      }
 
2024
    } else {
 
2025
      /* Lower privileges permanently */
 
2026
      ret = setuid(uid);
 
2027
      if(ret == -1){
 
2028
        perror_plus("setuid");
 
2029
      }
1998
2030
    }
1999
2031
  }
2000
2032
  
2004
2036
  
2005
2037
  ret = init_gnutls_global(pubkey, seckey);
2006
2038
  if(ret == -1){
2007
 
    fprintf_plus(stderr, "init_gnutls_global failed\n");
 
2039
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2040
            "init_gnutls_global failed\n");
2008
2041
    exitcode = EX_UNAVAILABLE;
2009
2042
    goto end;
2010
2043
  } else {
2026
2059
  }
2027
2060
  
2028
2061
  if(not init_gpgme(pubkey, seckey, tempdir)){
2029
 
    fprintf_plus(stderr, "init_gpgme failed\n");
 
2062
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2063
            "init_gpgme failed\n");
2030
2064
    exitcode = EX_UNAVAILABLE;
2031
2065
    goto end;
2032
2066
  } else {
2042
2076
    /* (Mainly meant for debugging) */
2043
2077
    char *address = strrchr(connect_to, ':');
2044
2078
    if(address == NULL){
2045
 
      fprintf_plus(stderr, "No colon in address\n");
 
2079
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2080
              "No colon in address\n");
2046
2081
      exitcode = EX_USAGE;
2047
2082
      goto end;
2048
2083
    }
2056
2091
    tmpmax = strtoimax(address+1, &tmp, 10);
2057
2092
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2058
2093
       or tmpmax != (uint16_t)tmpmax){
2059
 
      fprintf_plus(stderr, "Bad port number\n");
 
2094
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2095
              "Bad port number\n");
2060
2096
      exitcode = EX_USAGE;
2061
2097
      goto end;
2062
2098
    }
2092
2128
        break;
2093
2129
      }
2094
2130
      if(debug){
2095
 
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2096
 
                     (int)retry_interval);
 
2131
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2132
                "Retrying in %d seconds\n", (int)retry_interval);
2097
2133
      }
2098
2134
      sleep((int)retry_interval);
2099
2135
    }
2129
2165
  
2130
2166
  /* Check if creating the Avahi server object succeeded */
2131
2167
  if(mc.server == NULL){
2132
 
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2133
 
                 avahi_strerror(error));
 
2168
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2169
            "Failed to create Avahi server: %s\n",
 
2170
            avahi_strerror(error));
2134
2171
    exitcode = EX_UNAVAILABLE;
2135
2172
    goto end;
2136
2173
  }
2144
2181
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2145
2182
                                   NULL, 0, browse_callback, NULL);
2146
2183
  if(sb == NULL){
2147
 
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2148
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
2184
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2185
            "Failed to create service browser: %s\n",
 
2186
            avahi_strerror(avahi_server_errno(mc.server)));
2149
2187
    exitcode = EX_UNAVAILABLE;
2150
2188
    goto end;
2151
2189
  }
2157
2195
  /* Run the main loop */
2158
2196
  
2159
2197
  if(debug){
2160
 
    fprintf_plus(stderr, "Starting Avahi loop search\n");
 
2198
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2199
            "Starting Avahi loop search\n");
2161
2200
  }
2162
2201
 
2163
2202
  ret = avahi_loop_with_timeout(mc.simple_poll,
2164
2203
                                (int)(retry_interval * 1000));
2165
2204
  if(debug){
2166
 
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2167
 
                 (ret == 0) ? "successfully" : "with error");
 
2205
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2206
            "avahi_loop_with_timeout exited %s\n",
 
2207
            (ret == 0) ? "successfully" : "with error");
2168
2208
  }
2169
2209
  
2170
2210
 end:
2171
2211
  
2172
2212
  if(debug){
2173
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2213
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2214
            "%s exiting\n", argv[0]);
2174
2215
  }
2175
2216
  
2176
2217
  /* Cleanup things */
2204
2245
    }
2205
2246
  }
2206
2247
  
2207
 
  /* Re-raise priviliges */
2208
 
  {
 
2248
  /* XXX run network hooks "stop" here  */
 
2249
  
 
2250
  /* Take down the network interface */
 
2251
  if(take_down_interface){
 
2252
    /* Re-raise priviliges */
2209
2253
    errno = 0;
2210
2254
    ret = seteuid(0);
2211
2255
    if(ret == -1){
2212
2256
      perror_plus("seteuid");
2213
2257
    }
2214
 
    /* Run network hooks */
2215
 
    if(not run_network_hooks("stop", interface, delay)){
2216
 
      goto end;
2217
 
    }
2218
 
    
2219
 
    /* Take down the network interface */
2220
 
    if(take_down_interface and geteuid() == 0){
 
2258
    if(geteuid() == 0){
2221
2259
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2222
2260
      if(ret == -1){
2223
2261
        perror_plus("ioctl SIOCGIFFLAGS");
2232
2270
      if(ret == -1){
2233
2271
        perror_plus("close");
2234
2272
      }
 
2273
      /* Lower privileges permanently */
 
2274
      errno = 0;
 
2275
      ret = setuid(uid);
 
2276
      if(ret == -1){
 
2277
        perror_plus("setuid");
 
2278
      }
2235
2279
    }
2236
2280
  }
2237
 
  /* Lower privileges permanently */
2238
 
  errno = 0;
2239
 
  ret = setuid(uid);
2240
 
  if(ret == -1){
2241
 
    perror_plus("setuid");
2242
 
  }
2243
2281
  
2244
2282
  /* Removes the GPGME temp directory and all files inside */
2245
2283
  if(tempdir_created){
2259
2297
        }
2260
2298
        ret = remove(fullname);
2261
2299
        if(ret == -1){
2262
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2263
 
                       strerror(errno));
 
2300
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2301
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2264
2302
        }
2265
2303
        free(fullname);
2266
2304
      }