/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

* network-hooks.d/openvpn: Tolerate relative MANDOSNETHOOKDIR path.
  (stop): Bug fix: Fix quote escapes.
* network-hooks.d/openvpn.conf: Remove compression.

Show diffs side-by-side

added added

removed removed

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