/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

* plugins.d/mandos-client.c: Some white space fixes.
  (runnable_hook): Simplify name rule.  More debug messages.

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(), _exit() */
 
76
                                   setgid(), pause() */
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() */
91
90
 
92
91
#ifdef __linux__
93
92
#include <sys/klog.h>           /* klogctl() */
175
174
  perror(print_text);
176
175
}
177
176
 
178
 
int fprintf_plus(FILE *stream, const char *format, ...){
179
 
  va_list ap;
180
 
  va_start (ap, format);
181
 
  
182
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
183
 
                             program_invocation_short_name));
184
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
185
 
}
186
 
 
187
177
/*
188
178
 * Make additional room in "buffer" for at least BUFFER_SIZE more
189
179
 * bytes. "buffer_capacity" is how much is currently allocated,
263
253
    
264
254
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
265
255
    if(rc != GPG_ERR_NO_ERROR){
266
 
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
267
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
256
      fprintf(stderr, "Mandos plugin mandos-client: "
 
257
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
258
              gpgme_strsource(rc), gpgme_strerror(rc));
268
259
      return false;
269
260
    }
270
261
    
271
262
    rc = gpgme_op_import(mc.ctx, pgp_data);
272
263
    if(rc != GPG_ERR_NO_ERROR){
273
 
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
274
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
264
      fprintf(stderr, "Mandos plugin mandos-client: "
 
265
              "bad gpgme_op_import: %s: %s\n",
 
266
              gpgme_strsource(rc), gpgme_strerror(rc));
275
267
      return false;
276
268
    }
277
269
    
284
276
  }
285
277
  
286
278
  if(debug){
287
 
    fprintf_plus(stderr, "Initializing GPGME\n");
 
279
    fprintf(stderr, "Mandos plugin mandos-client: "
 
280
            "Initializing GPGME\n");
288
281
  }
289
282
  
290
283
  /* Init GPGME */
291
284
  gpgme_check_version(NULL);
292
285
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
293
286
  if(rc != GPG_ERR_NO_ERROR){
294
 
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
295
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
287
    fprintf(stderr, "Mandos plugin mandos-client: "
 
288
            "bad gpgme_engine_check_version: %s: %s\n",
 
289
            gpgme_strsource(rc), gpgme_strerror(rc));
296
290
    return false;
297
291
  }
298
292
  
299
293
  /* Set GPGME home directory for the OpenPGP engine only */
300
294
  rc = gpgme_get_engine_info(&engine_info);
301
295
  if(rc != GPG_ERR_NO_ERROR){
302
 
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
303
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
296
    fprintf(stderr, "Mandos plugin mandos-client: "
 
297
            "bad gpgme_get_engine_info: %s: %s\n",
 
298
            gpgme_strsource(rc), gpgme_strerror(rc));
304
299
    return false;
305
300
  }
306
301
  while(engine_info != NULL){
312
307
    engine_info = engine_info->next;
313
308
  }
314
309
  if(engine_info == NULL){
315
 
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
316
 
                 tempdir);
 
310
    fprintf(stderr, "Mandos plugin mandos-client: "
 
311
            "Could not set GPGME home dir to %s\n", tempdir);
317
312
    return false;
318
313
  }
319
314
  
320
315
  /* Create new GPGME "context" */
321
316
  rc = gpgme_new(&(mc.ctx));
322
317
  if(rc != GPG_ERR_NO_ERROR){
323
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
324
 
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
325
 
                 gpgme_strerror(rc));
 
318
    fprintf(stderr, "Mandos plugin mandos-client: "
 
319
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
320
            gpgme_strerror(rc));
326
321
    return false;
327
322
  }
328
323
  
347
342
  ssize_t plaintext_length = 0;
348
343
  
349
344
  if(debug){
350
 
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
 
345
    fprintf(stderr, "Mandos plugin mandos-client: "
 
346
            "Trying to decrypt OpenPGP data\n");
351
347
  }
352
348
  
353
349
  /* Create new GPGME data buffer from memory cryptotext */
354
350
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
355
351
                               0);
356
352
  if(rc != GPG_ERR_NO_ERROR){
357
 
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
358
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
353
    fprintf(stderr, "Mandos plugin mandos-client: "
 
354
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
355
            gpgme_strsource(rc), gpgme_strerror(rc));
359
356
    return -1;
360
357
  }
361
358
  
362
359
  /* Create new empty GPGME data buffer for the plaintext */
363
360
  rc = gpgme_data_new(&dh_plain);
364
361
  if(rc != GPG_ERR_NO_ERROR){
365
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
366
 
                 "bad gpgme_data_new: %s: %s\n",
367
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
362
    fprintf(stderr, "Mandos plugin mandos-client: "
 
363
            "bad gpgme_data_new: %s: %s\n",
 
364
            gpgme_strsource(rc), gpgme_strerror(rc));
368
365
    gpgme_data_release(dh_crypto);
369
366
    return -1;
370
367
  }
373
370
     data buffer */
374
371
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
375
372
  if(rc != GPG_ERR_NO_ERROR){
376
 
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
377
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
373
    fprintf(stderr, "Mandos plugin mandos-client: "
 
374
            "bad gpgme_op_decrypt: %s: %s\n",
 
375
            gpgme_strsource(rc), gpgme_strerror(rc));
378
376
    plaintext_length = -1;
379
377
    if(debug){
380
378
      gpgme_decrypt_result_t result;
381
379
      result = gpgme_op_decrypt_result(mc.ctx);
382
380
      if(result == NULL){
383
 
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
 
381
        fprintf(stderr, "Mandos plugin mandos-client: "
 
382
                "gpgme_op_decrypt_result failed\n");
384
383
      } else {
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);
 
384
        fprintf(stderr, "Mandos plugin mandos-client: "
 
385
                "Unsupported algorithm: %s\n",
 
386
                result->unsupported_algorithm);
 
387
        fprintf(stderr, "Mandos plugin mandos-client: "
 
388
                "Wrong key usage: %u\n",
 
389
                result->wrong_key_usage);
389
390
        if(result->file_name != NULL){
390
 
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
 
391
          fprintf(stderr, "Mandos plugin mandos-client: "
 
392
                  "File name: %s\n", result->file_name);
391
393
        }
392
394
        gpgme_recipient_t recipient;
393
395
        recipient = result->recipients;
394
396
        while(recipient != NULL){
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");
 
397
          fprintf(stderr, "Mandos plugin mandos-client: "
 
398
                  "Public key algorithm: %s\n",
 
399
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
400
          fprintf(stderr, "Mandos plugin mandos-client: "
 
401
                  "Key ID: %s\n", recipient->keyid);
 
402
          fprintf(stderr, "Mandos plugin mandos-client: "
 
403
                  "Secret key available: %s\n",
 
404
                  recipient->status == GPG_ERR_NO_SECKEY
 
405
                  ? "No" : "Yes");
402
406
          recipient = recipient->next;
403
407
        }
404
408
      }
407
411
  }
408
412
  
409
413
  if(debug){
410
 
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
 
414
    fprintf(stderr, "Mandos plugin mandos-client: "
 
415
            "Decryption of OpenPGP data succeeded\n");
411
416
  }
412
417
  
413
418
  /* Seek back to the beginning of the GPGME plaintext data buffer */
444
449
  }
445
450
  
446
451
  if(debug){
447
 
    fprintf_plus(stderr, "Decrypted password is: ");
 
452
    fprintf(stderr, "Mandos plugin mandos-client: "
 
453
            "Decrypted password is: ");
448
454
    for(ssize_t i = 0; i < plaintext_length; i++){
449
455
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
450
456
    }
472
478
/* GnuTLS log function callback */
473
479
static void debuggnutls(__attribute__((unused)) int level,
474
480
                        const char* string){
475
 
  fprintf_plus(stderr, "GnuTLS: %s", string);
 
481
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
476
482
}
477
483
 
478
484
static int init_gnutls_global(const char *pubkeyfilename,
480
486
  int ret;
481
487
  
482
488
  if(debug){
483
 
    fprintf_plus(stderr, "Initializing GnuTLS\n");
 
489
    fprintf(stderr, "Mandos plugin mandos-client: "
 
490
            "Initializing GnuTLS\n");
484
491
  }
485
492
  
486
493
  ret = gnutls_global_init();
487
494
  if(ret != GNUTLS_E_SUCCESS){
488
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
489
 
                 safer_gnutls_strerror(ret));
 
495
    fprintf(stderr, "Mandos plugin mandos-client: "
 
496
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
490
497
    return -1;
491
498
  }
492
499
  
501
508
  /* OpenPGP credentials */
502
509
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
503
510
  if(ret != GNUTLS_E_SUCCESS){
504
 
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
505
 
                 safer_gnutls_strerror(ret));
 
511
    fprintf(stderr, "Mandos plugin mandos-client: "
 
512
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
506
513
    gnutls_global_deinit();
507
514
    return -1;
508
515
  }
509
516
  
510
517
  if(debug){
511
 
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
512
 
                 " secret key %s as GnuTLS credentials\n",
513
 
                 pubkeyfilename,
514
 
                 seckeyfilename);
 
518
    fprintf(stderr, "Mandos plugin mandos-client: "
 
519
            "Attempting to use OpenPGP public key %s and"
 
520
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
521
            seckeyfilename);
515
522
  }
516
523
  
517
524
  ret = gnutls_certificate_set_openpgp_key_file
518
525
    (mc.cred, pubkeyfilename, seckeyfilename,
519
526
     GNUTLS_OPENPGP_FMT_BASE64);
520
527
  if(ret != GNUTLS_E_SUCCESS){
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));
 
528
    fprintf(stderr,
 
529
            "Mandos plugin mandos-client: "
 
530
            "Error[%d] while reading the OpenPGP key pair ('%s',"
 
531
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
532
    fprintf(stderr, "Mandos plugin mandos-client: "
 
533
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
526
534
    goto globalfail;
527
535
  }
528
536
  
529
537
  /* GnuTLS server initialization */
530
538
  ret = gnutls_dh_params_init(&mc.dh_params);
531
539
  if(ret != GNUTLS_E_SUCCESS){
532
 
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
533
 
                 " initialization: %s\n",
534
 
                 safer_gnutls_strerror(ret));
 
540
    fprintf(stderr, "Mandos plugin mandos-client: "
 
541
            "Error in GnuTLS DH parameter initialization:"
 
542
            " %s\n", safer_gnutls_strerror(ret));
535
543
    goto globalfail;
536
544
  }
537
545
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
538
546
  if(ret != GNUTLS_E_SUCCESS){
539
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
540
 
                 safer_gnutls_strerror(ret));
 
547
    fprintf(stderr, "Mandos plugin mandos-client: "
 
548
            "Error in GnuTLS prime generation: %s\n",
 
549
            safer_gnutls_strerror(ret));
541
550
    goto globalfail;
542
551
  }
543
552
  
563
572
    }
564
573
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
565
574
  if(ret != GNUTLS_E_SUCCESS){
566
 
    fprintf_plus(stderr,
567
 
                 "Error in GnuTLS session initialization: %s\n",
568
 
                 safer_gnutls_strerror(ret));
 
575
    fprintf(stderr, "Mandos plugin mandos-client: "
 
576
            "Error in GnuTLS session initialization: %s\n",
 
577
            safer_gnutls_strerror(ret));
569
578
  }
570
579
  
571
580
  {
578
587
      }
579
588
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
580
589
    if(ret != GNUTLS_E_SUCCESS){
581
 
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
582
 
      fprintf_plus(stderr, "GnuTLS error: %s\n",
583
 
                   safer_gnutls_strerror(ret));
 
590
      fprintf(stderr, "Mandos plugin mandos-client: "
 
591
              "Syntax error at: %s\n", err);
 
592
      fprintf(stderr, "Mandos plugin mandos-client: "
 
593
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
584
594
      gnutls_deinit(*session);
585
595
      return -1;
586
596
    }
595
605
    }
596
606
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
597
607
  if(ret != GNUTLS_E_SUCCESS){
598
 
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
599
 
                 safer_gnutls_strerror(ret));
 
608
    fprintf(stderr, "Mandos plugin mandos-client: "
 
609
            "Error setting GnuTLS credentials: %s\n",
 
610
            safer_gnutls_strerror(ret));
600
611
    gnutls_deinit(*session);
601
612
    return -1;
602
613
  }
647
658
    pf = PF_INET;
648
659
    break;
649
660
  default:
650
 
    fprintf_plus(stderr, "Bad address family: %d\n", af);
 
661
    fprintf(stderr, "Mandos plugin mandos-client: "
 
662
            "Bad address family: %d\n", af);
651
663
    errno = EINVAL;
652
664
    return -1;
653
665
  }
658
670
  }
659
671
  
660
672
  if(debug){
661
 
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
662
 
                 PRIu16 "\n", ip, port);
 
673
    fprintf(stderr, "Mandos plugin mandos-client: "
 
674
            "Setting up a TCP connection to %s, port %" PRIu16
 
675
            "\n", ip, port);
663
676
  }
664
677
  
665
678
  tcp_sd = socket(pf, SOCK_STREAM, 0);
691
704
  }
692
705
  if(ret == 0){
693
706
    int e = errno;
694
 
    fprintf_plus(stderr, "Bad address: %s\n", ip);
 
707
    fprintf(stderr, "Mandos plugin mandos-client: "
 
708
            "Bad address: %s\n", ip);
695
709
    errno = e;
696
710
    goto mandos_end;
697
711
  }
704
718
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
705
719
                                -Wunreachable-code*/
706
720
      if(if_index == AVAHI_IF_UNSPEC){
707
 
        fprintf_plus(stderr, "An IPv6 link-local address is"
708
 
                     " incomplete without a network interface\n");
 
721
        fprintf(stderr, "Mandos plugin mandos-client: "
 
722
                "An IPv6 link-local address is incomplete"
 
723
                " without a network interface\n");
709
724
        errno = EINVAL;
710
725
        goto mandos_end;
711
726
      }
729
744
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
730
745
        perror_plus("if_indextoname");
731
746
      } else {
732
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
 
                     "\n", ip, interface, port);
 
747
        fprintf(stderr, "Mandos plugin mandos-client: "
 
748
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
749
                ip, interface, port);
734
750
      }
735
751
    } else {
736
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
737
 
                   ip, port);
 
752
      fprintf(stderr, "Mandos plugin mandos-client: "
 
753
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
738
754
    }
739
755
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
740
756
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
750
766
      perror_plus("inet_ntop");
751
767
    } else {
752
768
      if(strcmp(addrstr, ip) != 0){
753
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
769
        fprintf(stderr, "Mandos plugin mandos-client: "
 
770
                "Canonical address form: %s\n", addrstr);
754
771
      }
755
772
    }
756
773
  }
810
827
  }
811
828
  
812
829
  if(debug){
813
 
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
 
830
    fprintf(stderr, "Mandos plugin mandos-client: "
 
831
            "Establishing TLS session with %s\n", ip);
814
832
  }
815
833
  
816
834
  if(quit_now){
836
854
  
837
855
  if(ret != GNUTLS_E_SUCCESS){
838
856
    if(debug){
839
 
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
 
857
      fprintf(stderr, "Mandos plugin mandos-client: "
 
858
              "*** GnuTLS Handshake failed ***\n");
840
859
      gnutls_perror(ret);
841
860
    }
842
861
    errno = EPROTO;
846
865
  /* Read OpenPGP packet that contains the wanted password */
847
866
  
848
867
  if(debug){
849
 
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
850
 
                 " %s\n", ip);
 
868
    fprintf(stderr, "Mandos plugin mandos-client: "
 
869
            "Retrieving OpenPGP encrypted password from %s\n", ip);
851
870
  }
852
871
  
853
872
  while(true){
891
910
          }
892
911
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
893
912
        if(ret < 0){
894
 
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
895
 
                       "***\n");
 
913
          fprintf(stderr, "Mandos plugin mandos-client: "
 
914
                  "*** GnuTLS Re-handshake failed ***\n");
896
915
          gnutls_perror(ret);
897
916
          errno = EPROTO;
898
917
          goto mandos_end;
899
918
        }
900
919
        break;
901
920
      default:
902
 
        fprintf_plus(stderr, "Unknown error while reading data from"
903
 
                     " encrypted session with Mandos server\n");
 
921
        fprintf(stderr, "Mandos plugin mandos-client: "
 
922
                "Unknown error while reading data from"
 
923
                " encrypted session with Mandos server\n");
904
924
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
905
925
        errno = EIO;
906
926
        goto mandos_end;
911
931
  }
912
932
  
913
933
  if(debug){
914
 
    fprintf_plus(stderr, "Closing TLS session\n");
 
934
    fprintf(stderr, "Mandos plugin mandos-client: "
 
935
            "Closing TLS session\n");
915
936
  }
916
937
  
917
938
  if(quit_now){
946
967
        if(ret == 0 and ferror(stdout)){
947
968
          int e = errno;
948
969
          if(debug){
949
 
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
950
 
                         strerror(errno));
 
970
            fprintf(stderr, "Mandos plugin mandos-client: "
 
971
                    "Error writing encrypted data: %s\n",
 
972
                    strerror(errno));
951
973
          }
952
974
          errno = e;
953
975
          goto mandos_end;
1010
1032
  switch(event){
1011
1033
  default:
1012
1034
  case AVAHI_RESOLVER_FAILURE:
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)));
 
1035
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1036
            "(Avahi Resolver) Failed to resolve service '%s'"
 
1037
            " of type '%s' in domain '%s': %s\n", name, type, domain,
 
1038
            avahi_strerror(avahi_server_errno(mc.server)));
1017
1039
    break;
1018
1040
    
1019
1041
  case AVAHI_RESOLVER_FOUND:
1021
1043
      char ip[AVAHI_ADDRESS_STR_MAX];
1022
1044
      avahi_address_snprint(ip, sizeof(ip), address);
1023
1045
      if(debug){
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);
 
1046
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1047
                "Mandos server \"%s\" found on %s (%s, %"
 
1048
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
1049
                ip, (intmax_t)interface, port);
1027
1050
      }
1028
1051
      int ret = start_mandos_communication(ip, port, interface,
1029
1052
                                           avahi_proto_to_af(proto));
1061
1084
  default:
1062
1085
  case AVAHI_BROWSER_FAILURE:
1063
1086
    
1064
 
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1065
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1087
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1088
            "(Avahi browser) %s\n",
 
1089
            avahi_strerror(avahi_server_errno(mc.server)));
1066
1090
    avahi_simple_poll_quit(mc.simple_poll);
1067
1091
    return;
1068
1092
    
1075
1099
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1076
1100
                                    name, type, domain, protocol, 0,
1077
1101
                                    resolve_callback, NULL) == NULL)
1078
 
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1079
 
                   " %s\n", name,
1080
 
                   avahi_strerror(avahi_server_errno(mc.server)));
 
1102
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1103
              "Avahi: Failed to resolve service '%s': %s\n",
 
1104
              name, avahi_strerror(avahi_server_errno(mc.server)));
1081
1105
    break;
1082
1106
    
1083
1107
  case AVAHI_BROWSER_REMOVE:
1086
1110
  case AVAHI_BROWSER_ALL_FOR_NOW:
1087
1111
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1088
1112
    if(debug){
1089
 
      fprintf_plus(stderr, "No Mandos server found, still"
1090
 
                   " searching...\n");
 
1113
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1114
              "No Mandos server found, still searching...\n");
1091
1115
    }
1092
1116
    break;
1093
1117
  }
1132
1156
  /* Reject the loopback device */
1133
1157
  if(ifr->ifr_flags & IFF_LOOPBACK){
1134
1158
    if(debug){
1135
 
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1136
 
                   ifname);
 
1159
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1160
              "Rejecting loopback interface \"%s\"\n", ifname);
1137
1161
    }
1138
1162
    return false;
1139
1163
  }
1140
1164
  /* Accept point-to-point devices only if connect_to is specified */
1141
1165
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1142
1166
    if(debug){
1143
 
      fprintf_plus(stderr, "Accepting point-to-point interface"
1144
 
                   " \"%s\"\n", ifname);
 
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1168
              "Accepting point-to-point interface \"%s\"\n", ifname);
1145
1169
    }
1146
1170
    return true;
1147
1171
  }
1148
1172
  /* Otherwise, reject non-broadcast-capable devices */
1149
1173
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1150
1174
    if(debug){
1151
 
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
1152
 
                   " \"%s\"\n", ifname);
 
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1176
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1153
1177
    }
1154
1178
    return false;
1155
1179
  }
1156
1180
  /* Reject non-ARP interfaces (including dummy interfaces) */
1157
1181
  if(ifr->ifr_flags & IFF_NOARP){
1158
1182
    if(debug){
1159
 
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1160
 
                   ifname);
 
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1184
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1161
1185
    }
1162
1186
    return false;
1163
1187
  }
1164
1188
  
1165
1189
  /* Accept this device */
1166
1190
  if(debug){
1167
 
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
 
1191
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1192
            "Interface \"%s\" is good\n", ifname);
1168
1193
  }
1169
1194
  return true;
1170
1195
}
1182
1207
  struct ifreq ifr;
1183
1208
  if(not get_flags(if_entry->d_name, &ifr)){
1184
1209
    if(debug){
1185
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1186
 
                   "\"%s\"\n", if_entry->d_name);
 
1210
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1211
              "Failed to get flags for interface \"%s\"\n",
 
1212
              if_entry->d_name);
1187
1213
    }
1188
1214
    return 0;
1189
1215
  }
1207
1233
  struct ifreq ifr;
1208
1234
  if(not get_flags(if_entry->d_name, &ifr)){
1209
1235
    if(debug){
1210
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1211
 
                   "\"%s\"\n", if_entry->d_name);
 
1236
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1237
              "Failed to get flags for interface \"%s\"\n",
 
1238
              if_entry->d_name);
1212
1239
    }
1213
1240
    return 0;
1214
1241
  }
1216
1243
  /* Reject down interfaces */
1217
1244
  if(not (ifr.ifr_flags & IFF_UP)){
1218
1245
    if(debug){
1219
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1220
 
                   if_entry->d_name);
 
1246
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1247
              "Rejecting down interface \"%s\"\n",
 
1248
              if_entry->d_name);
1221
1249
    }
1222
1250
    return 0;
1223
1251
  }
1225
1253
  /* Reject non-running interfaces */
1226
1254
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1227
1255
    if(debug){
1228
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1229
 
                   if_entry->d_name);
 
1256
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1257
              "Rejecting non-running interface \"%s\"\n",
 
1258
              if_entry->d_name);
1230
1259
    }
1231
1260
    return 0;
1232
1261
  }
1266
1295
  if((direntry->d_name)[sret] != '\0'){
1267
1296
    /* Contains non-allowed characters */
1268
1297
    if(debug){
1269
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1270
 
                   direntry->d_name);
 
1298
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1299
              "Ignoring hook \"%s\" with bad name\n",
 
1300
              direntry->d_name);
1271
1301
    }
1272
1302
    return 0;
1273
1303
  }
1289
1319
  if(not (S_ISREG(st.st_mode))){
1290
1320
    /* Not a regular file */
1291
1321
    if(debug){
1292
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1293
 
                   direntry->d_name);
 
1322
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1323
              "Ignoring hook \"%s\" - not a file\n",
 
1324
              direntry->d_name);
1294
1325
    }
1295
1326
    return 0;
1296
1327
  }
1297
1328
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1298
1329
    /* Not executable */
1299
1330
    if(debug){
1300
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1301
 
                   direntry->d_name);
 
1331
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1332
              "Ignoring hook \"%s\" - not executable\n",
 
1333
              direntry->d_name);
1302
1334
    }
1303
1335
    return 0;
1304
1336
  }
1305
 
  if(debug){
1306
 
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1307
 
                 direntry->d_name);
1308
 
  }
1309
1337
  return 1;
1310
1338
}
1311
1339
 
1318
1346
  while(true){
1319
1347
    if(mc.current_server == NULL){
1320
1348
      if (debug){
1321
 
        fprintf_plus(stderr, "Wait until first server is found."
1322
 
                     " No timeout!\n");
 
1349
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1350
                "Wait until first server is found. No timeout!\n");
1323
1351
      }
1324
1352
      ret = avahi_simple_poll_iterate(s, -1);
1325
1353
    } else {
1326
1354
      if (debug){
1327
 
        fprintf_plus(stderr, "Check current_server if we should run"
1328
 
                     " it, or wait\n");
 
1355
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1356
                "Check current_server if we should run it,"
 
1357
                " or wait\n");
1329
1358
      }
1330
1359
      /* the current time */
1331
1360
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1347
1376
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1348
1377
      
1349
1378
      if (debug){
1350
 
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1351
 
                     block_time);
 
1379
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1380
                "Blocking for %" PRIdMAX " ms\n", block_time);
1352
1381
      }
1353
1382
      
1354
1383
      if(block_time <= 0){
1381
1410
  }
1382
1411
}
1383
1412
 
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
 
 
1511
1413
int main(int argc, char *argv[]){
1512
1414
  AvahiSServiceBrowser *sb = NULL;
1513
1415
  int error;
1671
1573
        argp_state_help(state, state->out_stream,
1672
1574
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1673
1575
      case 'V':                 /* --version */
1674
 
        fprintf_plus(state->out_stream,
1675
 
                     "Mandos plugin mandos-client: ");
1676
 
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
 
1576
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
 
1577
        fprintf(state->out_stream, "%s\n", argp_program_version);
1677
1578
        exit(argp_err_exit_status);
1678
1579
        break;
1679
1580
      default:
1706
1607
  {
1707
1608
    /* Work around Debian bug #633582:
1708
1609
       <http://bugs.debian.org/633582> */
 
1610
    struct stat st;
1709
1611
    
1710
1612
    /* Re-raise priviliges */
1711
1613
    errno = 0;
1712
1614
    ret = seteuid(0);
1713
1615
    if(ret == -1){
1714
1616
      perror_plus("seteuid");
 
1617
    }
 
1618
    
 
1619
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1620
      int seckey_fd = open(seckey, O_RDONLY);
 
1621
      if(seckey_fd == -1){
 
1622
        perror_plus("open");
 
1623
      } else {
 
1624
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1625
        if(ret == -1){
 
1626
          perror_plus("fstat");
 
1627
        } else {
 
1628
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1629
            ret = fchown(seckey_fd, uid, gid);
 
1630
            if(ret == -1){
 
1631
              perror_plus("fchown");
 
1632
            }
 
1633
          }
 
1634
        }
 
1635
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1636
      }
 
1637
    }
 
1638
    
 
1639
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1640
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1641
      if(pubkey_fd == -1){
 
1642
        perror_plus("open");
 
1643
      } else {
 
1644
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1645
        if(ret == -1){
 
1646
          perror_plus("fstat");
 
1647
        } else {
 
1648
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1649
            ret = fchown(pubkey_fd, uid, gid);
 
1650
            if(ret == -1){
 
1651
              perror_plus("fchown");
 
1652
            }
 
1653
          }
 
1654
        }
 
1655
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1656
      }
 
1657
    }
 
1658
    
 
1659
    /* Lower privileges */
 
1660
    errno = 0;
 
1661
    ret = seteuid(uid);
 
1662
    if(ret == -1){
 
1663
      perror_plus("seteuid");
 
1664
    }
 
1665
  }
 
1666
  
 
1667
  /* Find network hooks and run them */
 
1668
  {
 
1669
    struct dirent **direntries;
 
1670
    struct dirent *direntry;
 
1671
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1672
                           alphasort);
 
1673
    if(numhooks == -1){
 
1674
      perror_plus("scandir");
1715
1675
    } else {
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
 
      }
 
1676
      int devnull = open("/dev/null", O_RDONLY);
 
1677
      for(int i = 0; i < numhooks; i++){
 
1678
        direntry = direntries[0];
 
1679
        char *fullname = NULL;
 
1680
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1681
        if(ret < 0){
 
1682
          perror_plus("asprintf");
 
1683
          continue;
 
1684
        }
 
1685
        pid_t hook_pid = fork();
 
1686
        if(hook_pid == 0){
 
1687
          /* Child */
 
1688
          dup2(devnull, STDIN_FILENO);
 
1689
          close(devnull);
 
1690
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1691
          ret = setenv("DEVICE", interface, 1);
 
1692
          if(ret == -1){
 
1693
            perror_plus("setenv");
 
1694
            exit(1);
 
1695
          }
 
1696
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1697
          if(ret == -1){
 
1698
            perror_plus("setenv");
 
1699
            exit(1);
 
1700
          }
 
1701
          ret = setenv("MODE", "start", 1);
 
1702
          if(ret == -1){
 
1703
            perror_plus("setenv");
 
1704
            exit(1);
 
1705
          }
 
1706
          char *delaystring;
 
1707
          ret = asprintf(&delaystring, "%f", delay);
 
1708
          if(ret == -1){
 
1709
            perror_plus("asprintf");
 
1710
            exit(1);
 
1711
          }
 
1712
          ret = setenv("DELAY", delaystring, 1);
 
1713
          if(ret == -1){
 
1714
            free(delaystring);
 
1715
            perror_plus("setenv");
 
1716
            exit(1);
 
1717
          }
 
1718
          free(delaystring);
 
1719
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1720
          perror_plus("execl");
 
1721
        } else {
 
1722
          int status;
 
1723
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1724
            perror_plus("waitpid");
 
1725
            free(fullname);
 
1726
            continue;
 
1727
          }
 
1728
          if(WIFEXITED(status)){
 
1729
            if(WEXITSTATUS(status) != 0){
 
1730
              fprintf(stderr, "Mandos plugin mandos-client: "
 
1731
                      "Warning: network hook \"%s\" exited"
 
1732
                      " with status %d\n", direntry->d_name,
 
1733
                      WEXITSTATUS(status));
 
1734
              free(fullname);
 
1735
              continue;
 
1736
            }
 
1737
          } else if(WIFSIGNALED(status)){
 
1738
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1739
                    "Warning: network hook \"%s\" died by"
 
1740
                    " signal %d\n", direntry->d_name,
 
1741
                    WTERMSIG(status));
 
1742
            free(fullname);
 
1743
            continue;
 
1744
          } else {
 
1745
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1746
                    "Warning: network hook \"%s\" crashed\n",
 
1747
                    direntry->d_name);
 
1748
            free(fullname);
 
1749
            continue;
 
1750
          }
 
1751
        }
 
1752
        free(fullname);
 
1753
        if(quit_now){
 
1754
          goto end;
 
1755
        }
 
1756
      }
 
1757
      close(devnull);
1766
1758
    }
1767
1759
  }
1768
1760
  
1769
 
  /* Run network hooks */
1770
 
  if(not run_network_hooks("start", interface, delay)){
1771
 
    goto end;
1772
 
  }
1773
 
  
1774
1761
  if(not debug){
1775
1762
    avahi_set_log_function(empty_log);
1776
1763
  }
1790
1777
      /* Pick the first interface returned */
1791
1778
      interface = strdup(direntries[0]->d_name);
1792
1779
      if(debug){
1793
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1780
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1781
                "Using interface \"%s\"\n", interface);
1794
1782
      }
1795
1783
      if(interface == NULL){
1796
1784
        perror_plus("malloc");
1801
1789
      free(direntries);
1802
1790
    } else {
1803
1791
      free(direntries);
1804
 
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1792
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1793
              "Could not find a network interface\n");
1805
1794
      exitcode = EXIT_FAILURE;
1806
1795
      goto end;
1807
1796
    }
1813
1802
  srand((unsigned int) time(NULL));
1814
1803
  mc.simple_poll = avahi_simple_poll_new();
1815
1804
  if(mc.simple_poll == NULL){
1816
 
    fprintf_plus(stderr,
1817
 
                 "Avahi: Failed to create simple poll object.\n");
 
1805
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1806
            "Avahi: Failed to create simple poll object.\n");
1818
1807
    exitcode = EX_UNAVAILABLE;
1819
1808
    goto end;
1820
1809
  }
1886
1875
  if(strcmp(interface, "none") != 0){
1887
1876
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1888
1877
    if(if_index == 0){
1889
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1878
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1879
              "No such interface: \"%s\"\n", interface);
1890
1880
      exitcode = EX_UNAVAILABLE;
1891
1881
      goto end;
1892
1882
    }
2012
2002
#endif  /* __linux__ */
2013
2003
    /* Lower privileges */
2014
2004
    errno = 0;
2015
 
    /* Lower privileges */
2016
 
    ret = seteuid(uid);
2017
 
    if(ret == -1){
2018
 
      perror_plus("seteuid");
 
2005
    if(take_down_interface){
 
2006
      /* Lower privileges */
 
2007
      ret = seteuid(uid);
 
2008
      if(ret == -1){
 
2009
        perror_plus("seteuid");
 
2010
      }
 
2011
    } else {
 
2012
      /* Lower privileges permanently */
 
2013
      ret = setuid(uid);
 
2014
      if(ret == -1){
 
2015
        perror_plus("setuid");
 
2016
      }
2019
2017
    }
2020
2018
  }
2021
2019
  
2025
2023
  
2026
2024
  ret = init_gnutls_global(pubkey, seckey);
2027
2025
  if(ret == -1){
2028
 
    fprintf_plus(stderr, "init_gnutls_global failed\n");
 
2026
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2027
            "init_gnutls_global failed\n");
2029
2028
    exitcode = EX_UNAVAILABLE;
2030
2029
    goto end;
2031
2030
  } else {
2047
2046
  }
2048
2047
  
2049
2048
  if(not init_gpgme(pubkey, seckey, tempdir)){
2050
 
    fprintf_plus(stderr, "init_gpgme failed\n");
 
2049
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2050
            "init_gpgme failed\n");
2051
2051
    exitcode = EX_UNAVAILABLE;
2052
2052
    goto end;
2053
2053
  } else {
2063
2063
    /* (Mainly meant for debugging) */
2064
2064
    char *address = strrchr(connect_to, ':');
2065
2065
    if(address == NULL){
2066
 
      fprintf_plus(stderr, "No colon in address\n");
 
2066
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2067
              "No colon in address\n");
2067
2068
      exitcode = EX_USAGE;
2068
2069
      goto end;
2069
2070
    }
2077
2078
    tmpmax = strtoimax(address+1, &tmp, 10);
2078
2079
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2079
2080
       or tmpmax != (uint16_t)tmpmax){
2080
 
      fprintf_plus(stderr, "Bad port number\n");
 
2081
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2082
              "Bad port number\n");
2081
2083
      exitcode = EX_USAGE;
2082
2084
      goto end;
2083
2085
    }
2113
2115
        break;
2114
2116
      }
2115
2117
      if(debug){
2116
 
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2117
 
                     (int)retry_interval);
 
2118
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2119
                "Retrying in %d seconds\n", (int)retry_interval);
2118
2120
      }
2119
2121
      sleep((int)retry_interval);
2120
2122
    }
2150
2152
  
2151
2153
  /* Check if creating the Avahi server object succeeded */
2152
2154
  if(mc.server == NULL){
2153
 
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2154
 
                 avahi_strerror(error));
 
2155
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2156
            "Failed to create Avahi server: %s\n",
 
2157
            avahi_strerror(error));
2155
2158
    exitcode = EX_UNAVAILABLE;
2156
2159
    goto end;
2157
2160
  }
2165
2168
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2166
2169
                                   NULL, 0, browse_callback, NULL);
2167
2170
  if(sb == NULL){
2168
 
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2169
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
2171
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2172
            "Failed to create service browser: %s\n",
 
2173
            avahi_strerror(avahi_server_errno(mc.server)));
2170
2174
    exitcode = EX_UNAVAILABLE;
2171
2175
    goto end;
2172
2176
  }
2178
2182
  /* Run the main loop */
2179
2183
  
2180
2184
  if(debug){
2181
 
    fprintf_plus(stderr, "Starting Avahi loop search\n");
 
2185
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2186
            "Starting Avahi loop search\n");
2182
2187
  }
2183
2188
 
2184
2189
  ret = avahi_loop_with_timeout(mc.simple_poll,
2185
2190
                                (int)(retry_interval * 1000));
2186
2191
  if(debug){
2187
 
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2188
 
                 (ret == 0) ? "successfully" : "with error");
 
2192
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2193
            "avahi_loop_with_timeout exited %s\n",
 
2194
            (ret == 0) ? "successfully" : "with error");
2189
2195
  }
2190
2196
  
2191
2197
 end:
2192
2198
  
2193
2199
  if(debug){
2194
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2200
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2201
            "%s exiting\n", argv[0]);
2195
2202
  }
2196
2203
  
2197
2204
  /* Cleanup things */
2213
2220
  if(gpgme_initialized){
2214
2221
    gpgme_release(mc.ctx);
2215
2222
  }
2216
 
  
 
2223
 
2217
2224
  /* Cleans up the circular linked list of Mandos servers the client
2218
2225
     has seen */
2219
2226
  if(mc.current_server != NULL){
2225
2232
    }
2226
2233
  }
2227
2234
  
2228
 
  /* Run network hooks */
2229
 
  run_network_hooks("stop", interface, delay);
 
2235
  /* XXX run network hooks "stop" here  */
2230
2236
  
2231
 
  /* Re-raise priviliges */
2232
 
  {
 
2237
  /* Take down the network interface */
 
2238
  if(take_down_interface){
 
2239
    /* Re-raise priviliges */
2233
2240
    errno = 0;
2234
2241
    ret = seteuid(0);
2235
2242
    if(ret == -1){
2236
2243
      perror_plus("seteuid");
2237
2244
    }
2238
 
    
2239
 
    /* Take down the network interface */
2240
 
    if(take_down_interface and geteuid() == 0){
 
2245
    if(geteuid() == 0){
2241
2246
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2242
2247
      if(ret == -1){
2243
2248
        perror_plus("ioctl SIOCGIFFLAGS");
2252
2257
      if(ret == -1){
2253
2258
        perror_plus("close");
2254
2259
      }
 
2260
      /* Lower privileges permanently */
 
2261
      errno = 0;
 
2262
      ret = setuid(uid);
 
2263
      if(ret == -1){
 
2264
        perror_plus("setuid");
 
2265
      }
2255
2266
    }
2256
2267
  }
2257
 
  /* Lower privileges permanently */
2258
 
  errno = 0;
2259
 
  ret = setuid(uid);
2260
 
  if(ret == -1){
2261
 
    perror_plus("setuid");
2262
 
  }
2263
2268
  
2264
2269
  /* Removes the GPGME temp directory and all files inside */
2265
2270
  if(tempdir_created){
2279
2284
        }
2280
2285
        ret = remove(fullname);
2281
2286
        if(ret == -1){
2282
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2283
 
                       strerror(errno));
 
2287
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2288
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2284
2289
        }
2285
2290
        free(fullname);
2286
2291
      }