/mandos/release

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

« back to all changes in this revision

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

  • Committer: Björn Påhlsson
  • Date: 2011-11-14 19:10:50 UTC
  • mto: (237.16.9 client-network-hooks)
  • mto: This revision was merged to the branch mainline in revision 290.
  • Revision ID: belorn@fukt.bsnet.se-20111114191050-g11po6084bl9j5kf
replace calls to fprintf with fprintf_plus.

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
                                   init_gnutls_session(),
111
111
                                   GNUTLS_* */
112
112
#include <gnutls/openpgp.h>
113
 
                          /* gnutls_certificate_set_openpgp_key_file(),
114
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
113
                         /* gnutls_certificate_set_openpgp_key_file(),
 
114
                            GNUTLS_OPENPGP_FMT_BASE64 */
115
115
 
116
116
/* GPGME */
117
117
#include <gpgme.h>              /* All GPGME types, constants and
133
133
const char *argp_program_bug_address = "<mandos@recompile.se>";
134
134
static const char sys_class_net[] = "/sys/class/net";
135
135
char *connect_to = NULL;
 
136
const char *hookdir = HOOKDIR;
136
137
 
137
138
/* Doubly linked list that need to be circularly linked when used */
138
139
typedef struct server{
173
174
  perror(print_text);
174
175
}
175
176
 
 
177
int fprintf_plus(FILE *stream, const char *format, ...){
 
178
  va_list ap;
 
179
  va_start (ap, format);
 
180
  
 
181
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
 
182
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
183
}
 
184
 
176
185
/*
177
186
 * Make additional room in "buffer" for at least BUFFER_SIZE more
178
187
 * bytes. "buffer_capacity" is how much is currently allocated,
179
188
 * "buffer_length" is how much is already used.
180
189
 */
181
190
size_t incbuffer(char **buffer, size_t buffer_length,
182
 
                  size_t buffer_capacity){
 
191
                 size_t buffer_capacity){
183
192
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
184
193
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
185
194
    if(buffer == NULL){
191
200
}
192
201
 
193
202
/* Add server to set of servers to retry periodically */
194
 
int add_server(const char *ip, uint16_t port,
195
 
                 AvahiIfIndex if_index,
196
 
                 int af){
 
203
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
204
               int af){
197
205
  int ret;
198
206
  server *new_server = malloc(sizeof(server));
199
207
  if(new_server == NULL){
201
209
    return -1;
202
210
  }
203
211
  *new_server = (server){ .ip = strdup(ip),
204
 
                         .port = port,
205
 
                         .if_index = if_index,
206
 
                         .af = af };
 
212
                          .port = port,
 
213
                          .if_index = if_index,
 
214
                          .af = af };
207
215
  if(new_server->ip == NULL){
208
216
    perror_plus("strdup");
209
217
    return -1;
231
239
/* 
232
240
 * Initialize GPGME.
233
241
 */
234
 
static bool init_gpgme(const char *seckey,
235
 
                       const char *pubkey, const char *tempdir){
 
242
static bool init_gpgme(const char *seckey, const char *pubkey,
 
243
                       const char *tempdir){
236
244
  gpgme_error_t rc;
237
245
  gpgme_engine_info_t engine_info;
238
246
  
253
261
    
254
262
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
255
263
    if(rc != GPG_ERR_NO_ERROR){
256
 
      fprintf(stderr, "Mandos plugin mandos-client: "
257
 
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
264
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
258
265
              gpgme_strsource(rc), gpgme_strerror(rc));
259
266
      return false;
260
267
    }
261
268
    
262
269
    rc = gpgme_op_import(mc.ctx, pgp_data);
263
270
    if(rc != GPG_ERR_NO_ERROR){
264
 
      fprintf(stderr, "Mandos plugin mandos-client: "
265
 
              "bad gpgme_op_import: %s: %s\n",
 
271
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
266
272
              gpgme_strsource(rc), gpgme_strerror(rc));
267
273
      return false;
268
274
    }
276
282
  }
277
283
  
278
284
  if(debug){
279
 
    fprintf(stderr, "Mandos plugin mandos-client: "
280
 
            "Initializing GPGME\n");
 
285
    fprintf_plus(stderr, "Initializing GPGME\n");
281
286
  }
282
287
  
283
288
  /* Init GPGME */
284
289
  gpgme_check_version(NULL);
285
290
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
286
291
  if(rc != GPG_ERR_NO_ERROR){
287
 
    fprintf(stderr, "Mandos plugin mandos-client: "
288
 
            "bad gpgme_engine_check_version: %s: %s\n",
 
292
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
289
293
            gpgme_strsource(rc), gpgme_strerror(rc));
290
294
    return false;
291
295
  }
293
297
  /* Set GPGME home directory for the OpenPGP engine only */
294
298
  rc = gpgme_get_engine_info(&engine_info);
295
299
  if(rc != GPG_ERR_NO_ERROR){
296
 
    fprintf(stderr, "Mandos plugin mandos-client: "
297
 
            "bad gpgme_get_engine_info: %s: %s\n",
 
300
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
298
301
            gpgme_strsource(rc), gpgme_strerror(rc));
299
302
    return false;
300
303
  }
307
310
    engine_info = engine_info->next;
308
311
  }
309
312
  if(engine_info == NULL){
310
 
    fprintf(stderr, "Mandos plugin mandos-client: "
311
 
            "Could not set GPGME home dir to %s\n", tempdir);
 
313
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n", tempdir);
312
314
    return false;
313
315
  }
314
316
  
315
317
  /* Create new GPGME "context" */
316
318
  rc = gpgme_new(&(mc.ctx));
317
319
  if(rc != GPG_ERR_NO_ERROR){
318
 
    fprintf(stderr, "Mandos plugin mandos-client: "
 
320
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
319
321
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
320
322
            gpgme_strerror(rc));
321
323
    return false;
342
344
  ssize_t plaintext_length = 0;
343
345
  
344
346
  if(debug){
345
 
    fprintf(stderr, "Mandos plugin mandos-client: "
346
 
            "Trying to decrypt OpenPGP data\n");
 
347
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
347
348
  }
348
349
  
349
350
  /* Create new GPGME data buffer from memory cryptotext */
350
351
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
351
352
                               0);
352
353
  if(rc != GPG_ERR_NO_ERROR){
353
 
    fprintf(stderr, "Mandos plugin mandos-client: "
354
 
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
354
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
355
355
            gpgme_strsource(rc), gpgme_strerror(rc));
356
356
    return -1;
357
357
  }
359
359
  /* Create new empty GPGME data buffer for the plaintext */
360
360
  rc = gpgme_data_new(&dh_plain);
361
361
  if(rc != GPG_ERR_NO_ERROR){
362
 
    fprintf(stderr, "Mandos plugin mandos-client: "
 
362
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
363
363
            "bad gpgme_data_new: %s: %s\n",
364
364
            gpgme_strsource(rc), gpgme_strerror(rc));
365
365
    gpgme_data_release(dh_crypto);
370
370
     data buffer */
371
371
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
372
  if(rc != GPG_ERR_NO_ERROR){
373
 
    fprintf(stderr, "Mandos plugin mandos-client: "
374
 
            "bad gpgme_op_decrypt: %s: %s\n",
 
373
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
375
374
            gpgme_strsource(rc), gpgme_strerror(rc));
376
375
    plaintext_length = -1;
377
376
    if(debug){
378
377
      gpgme_decrypt_result_t result;
379
378
      result = gpgme_op_decrypt_result(mc.ctx);
380
379
      if(result == NULL){
381
 
        fprintf(stderr, "Mandos plugin mandos-client: "
382
 
                "gpgme_op_decrypt_result failed\n");
 
380
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
383
381
      } else {
384
 
        fprintf(stderr, "Mandos plugin mandos-client: "
385
 
                "Unsupported algorithm: %s\n",
 
382
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
386
383
                result->unsupported_algorithm);
387
 
        fprintf(stderr, "Mandos plugin mandos-client: "
388
 
                "Wrong key usage: %u\n",
 
384
        fprintf_plus(stderr, "Wrong key usage: %u\n",
389
385
                result->wrong_key_usage);
390
386
        if(result->file_name != NULL){
391
 
          fprintf(stderr, "Mandos plugin mandos-client: "
392
 
                  "File name: %s\n", result->file_name);
 
387
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
393
388
        }
394
389
        gpgme_recipient_t recipient;
395
390
        recipient = result->recipients;
396
391
        while(recipient != NULL){
397
 
          fprintf(stderr, "Mandos plugin mandos-client: "
398
 
                  "Public key algorithm: %s\n",
 
392
          fprintf_plus(stderr, "Public key algorithm: %s\n",
399
393
                  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",
 
394
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
 
395
          fprintf_plus(stderr, "Secret key available: %s\n",
404
396
                  recipient->status == GPG_ERR_NO_SECKEY
405
397
                  ? "No" : "Yes");
406
398
          recipient = recipient->next;
411
403
  }
412
404
  
413
405
  if(debug){
414
 
    fprintf(stderr, "Mandos plugin mandos-client: "
415
 
            "Decryption of OpenPGP data succeeded\n");
 
406
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
416
407
  }
417
408
  
418
409
  /* Seek back to the beginning of the GPGME plaintext data buffer */
425
416
  *plaintext = NULL;
426
417
  while(true){
427
418
    plaintext_capacity = incbuffer(plaintext,
428
 
                                      (size_t)plaintext_length,
429
 
                                      plaintext_capacity);
 
419
                                   (size_t)plaintext_length,
 
420
                                   plaintext_capacity);
430
421
    if(plaintext_capacity == 0){
431
 
        perror_plus("incbuffer");
432
 
        plaintext_length = -1;
433
 
        goto decrypt_end;
 
422
      perror_plus("incbuffer");
 
423
      plaintext_length = -1;
 
424
      goto decrypt_end;
434
425
    }
435
426
    
436
427
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
449
440
  }
450
441
  
451
442
  if(debug){
452
 
    fprintf(stderr, "Mandos plugin mandos-client: "
453
 
            "Decrypted password is: ");
 
443
    fprintf_plus(stderr, "Decrypted password is: ");
454
444
    for(ssize_t i = 0; i < plaintext_length; i++){
455
445
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
456
446
    }
478
468
/* GnuTLS log function callback */
479
469
static void debuggnutls(__attribute__((unused)) int level,
480
470
                        const char* string){
481
 
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
 
471
  fprintf_plus(stderr, "GnuTLS: %s", string);
482
472
}
483
473
 
484
474
static int init_gnutls_global(const char *pubkeyfilename,
486
476
  int ret;
487
477
  
488
478
  if(debug){
489
 
    fprintf(stderr, "Mandos plugin mandos-client: "
490
 
            "Initializing GnuTLS\n");
 
479
    fprintf_plus(stderr, "Initializing GnuTLS\n");
491
480
  }
492
481
  
493
482
  ret = gnutls_global_init();
494
483
  if(ret != GNUTLS_E_SUCCESS){
495
 
    fprintf(stderr, "Mandos plugin mandos-client: "
496
 
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
484
    fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
497
485
    return -1;
498
486
  }
499
487
  
508
496
  /* OpenPGP credentials */
509
497
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
510
498
  if(ret != GNUTLS_E_SUCCESS){
511
 
    fprintf(stderr, "Mandos plugin mandos-client: "
512
 
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
499
    fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
513
500
    gnutls_global_deinit();
514
501
    return -1;
515
502
  }
516
503
  
517
504
  if(debug){
518
 
    fprintf(stderr, "Mandos plugin mandos-client: "
519
 
            "Attempting to use OpenPGP public key %s and"
 
505
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
520
506
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
521
507
            seckeyfilename);
522
508
  }
525
511
    (mc.cred, pubkeyfilename, seckeyfilename,
526
512
     GNUTLS_OPENPGP_FMT_BASE64);
527
513
  if(ret != GNUTLS_E_SUCCESS){
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));
 
514
    fprintf_plus(stderr,
 
515
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
 
516
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
517
    fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
534
518
    goto globalfail;
535
519
  }
536
520
  
537
521
  /* GnuTLS server initialization */
538
522
  ret = gnutls_dh_params_init(&mc.dh_params);
539
523
  if(ret != GNUTLS_E_SUCCESS){
540
 
    fprintf(stderr, "Mandos plugin mandos-client: "
541
 
            "Error in GnuTLS DH parameter initialization:"
 
524
    fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
542
525
            " %s\n", safer_gnutls_strerror(ret));
543
526
    goto globalfail;
544
527
  }
545
528
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
546
529
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Mandos plugin mandos-client: "
548
 
            "Error in GnuTLS prime generation: %s\n",
 
530
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
549
531
            safer_gnutls_strerror(ret));
550
532
    goto globalfail;
551
533
  }
572
554
    }
573
555
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
574
556
  if(ret != GNUTLS_E_SUCCESS){
575
 
    fprintf(stderr, "Mandos plugin mandos-client: "
576
 
            "Error in GnuTLS session initialization: %s\n",
 
557
    fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
577
558
            safer_gnutls_strerror(ret));
578
559
  }
579
560
  
587
568
      }
588
569
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
589
570
    if(ret != GNUTLS_E_SUCCESS){
590
 
      fprintf(stderr, "Mandos plugin mandos-client: "
591
 
              "Syntax error at: %s\n", err);
592
 
      fprintf(stderr, "Mandos plugin mandos-client: "
593
 
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
 
571
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
 
572
      fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
594
573
      gnutls_deinit(*session);
595
574
      return -1;
596
575
    }
605
584
    }
606
585
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
607
586
  if(ret != GNUTLS_E_SUCCESS){
608
 
    fprintf(stderr, "Mandos plugin mandos-client: "
609
 
            "Error setting GnuTLS credentials: %s\n",
 
587
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
610
588
            safer_gnutls_strerror(ret));
611
589
    gnutls_deinit(*session);
612
590
    return -1;
658
636
    pf = PF_INET;
659
637
    break;
660
638
  default:
661
 
    fprintf(stderr, "Mandos plugin mandos-client: "
662
 
            "Bad address family: %d\n", af);
 
639
    fprintf_plus(stderr, "Bad address family: %d\n", af);
663
640
    errno = EINVAL;
664
641
    return -1;
665
642
  }
670
647
  }
671
648
  
672
649
  if(debug){
673
 
    fprintf(stderr, "Mandos plugin mandos-client: "
674
 
            "Setting up a TCP connection to %s, port %" PRIu16
 
650
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %" PRIu16
675
651
            "\n", ip, port);
676
652
  }
677
653
  
704
680
  }
705
681
  if(ret == 0){
706
682
    int e = errno;
707
 
    fprintf(stderr, "Mandos plugin mandos-client: "
708
 
            "Bad address: %s\n", ip);
 
683
    fprintf_plus(stderr, "Bad address: %s\n", ip);
709
684
    errno = e;
710
685
    goto mandos_end;
711
686
  }
716
691
    
717
692
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
718
693
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
719
 
                              -Wunreachable-code*/
 
694
                                -Wunreachable-code*/
720
695
      if(if_index == AVAHI_IF_UNSPEC){
721
 
        fprintf(stderr, "Mandos plugin mandos-client: "
722
 
                "An IPv6 link-local address is incomplete"
 
696
        fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
723
697
                " without a network interface\n");
724
698
        errno = EINVAL;
725
699
        goto mandos_end;
744
718
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
719
        perror_plus("if_indextoname");
746
720
      } else {
747
 
        fprintf(stderr, "Mandos plugin mandos-client: "
748
 
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
721
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
749
722
                ip, interface, port);
750
723
      }
751
724
    } else {
752
 
      fprintf(stderr, "Mandos plugin mandos-client: "
753
 
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
725
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
754
726
    }
755
727
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
728
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
766
738
      perror_plus("inet_ntop");
767
739
    } else {
768
740
      if(strcmp(addrstr, ip) != 0){
769
 
        fprintf(stderr, "Mandos plugin mandos-client: "
770
 
                "Canonical address form: %s\n", addrstr);
 
741
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
771
742
      }
772
743
    }
773
744
  }
801
772
  while(true){
802
773
    size_t out_size = strlen(out);
803
774
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
804
 
                                   out_size - written));
 
775
                                        out_size - written));
805
776
    if(ret == -1){
806
777
      int e = errno;
807
778
      perror_plus("write");
827
798
  }
828
799
  
829
800
  if(debug){
830
 
    fprintf(stderr, "Mandos plugin mandos-client: "
831
 
            "Establishing TLS session with %s\n", ip);
 
801
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
832
802
  }
833
803
  
834
804
  if(quit_now){
854
824
  
855
825
  if(ret != GNUTLS_E_SUCCESS){
856
826
    if(debug){
857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
858
 
              "*** GnuTLS Handshake failed ***\n");
 
827
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
859
828
      gnutls_perror(ret);
860
829
    }
861
830
    errno = EPROTO;
865
834
  /* Read OpenPGP packet that contains the wanted password */
866
835
  
867
836
  if(debug){
868
 
    fprintf(stderr, "Mandos plugin mandos-client: "
869
 
            "Retrieving OpenPGP encrypted password from %s\n", ip);
 
837
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from %s\n", ip);
870
838
  }
871
839
  
872
840
  while(true){
877
845
    }
878
846
    
879
847
    buffer_capacity = incbuffer(&buffer, buffer_length,
880
 
                                   buffer_capacity);
 
848
                                buffer_capacity);
881
849
    if(buffer_capacity == 0){
882
850
      int e = errno;
883
851
      perror_plus("incbuffer");
910
878
          }
911
879
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
912
880
        if(ret < 0){
913
 
          fprintf(stderr, "Mandos plugin mandos-client: "
914
 
                  "*** GnuTLS Re-handshake failed ***\n");
 
881
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed ***\n");
915
882
          gnutls_perror(ret);
916
883
          errno = EPROTO;
917
884
          goto mandos_end;
918
885
        }
919
886
        break;
920
887
      default:
921
 
        fprintf(stderr, "Mandos plugin mandos-client: "
922
 
                "Unknown error while reading data from"
 
888
        fprintf_plus(stderr, "Unknown error while reading data from"
923
889
                " encrypted session with Mandos server\n");
924
890
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
925
891
        errno = EIO;
931
897
  }
932
898
  
933
899
  if(debug){
934
 
    fprintf(stderr, "Mandos plugin mandos-client: "
935
 
            "Closing TLS session\n");
 
900
    fprintf_plus(stderr, "Closing TLS session\n");
936
901
  }
937
902
  
938
903
  if(quit_now){
950
915
  
951
916
  if(buffer_length > 0){
952
917
    ssize_t decrypted_buffer_size;
953
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
954
 
                                               buffer_length,
 
918
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
955
919
                                               &decrypted_buffer);
956
920
    if(decrypted_buffer_size >= 0){
957
921
      
968
932
        if(ret == 0 and ferror(stdout)){
969
933
          int e = errno;
970
934
          if(debug){
971
 
            fprintf(stderr, "Mandos plugin mandos-client: "
972
 
                    "Error writing encrypted data: %s\n",
 
935
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
973
936
                    strerror(errno));
974
937
          }
975
938
          errno = e;
1033
996
  switch(event){
1034
997
  default:
1035
998
  case AVAHI_RESOLVER_FAILURE:
1036
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1037
 
            "(Avahi Resolver) Failed to resolve service '%s'"
 
999
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1038
1000
            " of type '%s' in domain '%s': %s\n", name, type, domain,
1039
1001
            avahi_strerror(avahi_server_errno(mc.server)));
1040
1002
    break;
1044
1006
      char ip[AVAHI_ADDRESS_STR_MAX];
1045
1007
      avahi_address_snprint(ip, sizeof(ip), address);
1046
1008
      if(debug){
1047
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1048
 
                "Mandos server \"%s\" found on %s (%s, %"
 
1009
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1049
1010
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1050
1011
                ip, (intmax_t)interface, port);
1051
1012
      }
1085
1046
  default:
1086
1047
  case AVAHI_BROWSER_FAILURE:
1087
1048
    
1088
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1089
 
            "(Avahi browser) %s\n",
 
1049
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1090
1050
            avahi_strerror(avahi_server_errno(mc.server)));
1091
1051
    avahi_simple_poll_quit(mc.simple_poll);
1092
1052
    return;
1100
1060
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1101
1061
                                    name, type, domain, protocol, 0,
1102
1062
                                    resolve_callback, NULL) == NULL)
1103
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1104
 
              "Avahi: Failed to resolve service '%s': %s\n",
 
1063
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1105
1064
              name, avahi_strerror(avahi_server_errno(mc.server)));
1106
1065
    break;
1107
1066
    
1111
1070
  case AVAHI_BROWSER_ALL_FOR_NOW:
1112
1071
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1113
1072
    if(debug){
1114
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1115
 
              "No Mandos server found, still searching...\n");
 
1073
      fprintf_plus(stderr, "No Mandos server found, still searching...\n");
1116
1074
    }
1117
1075
    break;
1118
1076
  }
1157
1115
  /* Reject the loopback device */
1158
1116
  if(ifr->ifr_flags & IFF_LOOPBACK){
1159
1117
    if(debug){
1160
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1161
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1118
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1162
1119
    }
1163
1120
    return false;
1164
1121
  }
1165
1122
  /* Accept point-to-point devices only if connect_to is specified */
1166
1123
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1167
1124
    if(debug){
1168
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1169
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1125
      fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1170
1126
    }
1171
1127
    return true;
1172
1128
  }
1173
1129
  /* Otherwise, reject non-broadcast-capable devices */
1174
1130
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1175
1131
    if(debug){
1176
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1177
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1132
      fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1178
1133
    }
1179
1134
    return false;
1180
1135
  }
1181
1136
  /* Reject non-ARP interfaces (including dummy interfaces) */
1182
1137
  if(ifr->ifr_flags & IFF_NOARP){
1183
1138
    if(debug){
1184
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1185
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1139
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1186
1140
    }
1187
1141
    return false;
1188
1142
  }
1189
1143
  
1190
1144
  /* Accept this device */
1191
1145
  if(debug){
1192
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1193
 
            "Interface \"%s\" is good\n", ifname);
 
1146
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1194
1147
  }
1195
1148
  return true;
1196
1149
}
1208
1161
  struct ifreq ifr;
1209
1162
  if(not get_flags(if_entry->d_name, &ifr)){
1210
1163
    if(debug){
1211
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1212
 
              "Failed to get flags for interface \"%s\"\n",
 
1164
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1213
1165
              if_entry->d_name);
1214
1166
    }
1215
1167
    return 0;
1234
1186
  struct ifreq ifr;
1235
1187
  if(not get_flags(if_entry->d_name, &ifr)){
1236
1188
    if(debug){
1237
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1238
 
              "Failed to get flags for interface \"%s\"\n",
 
1189
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1239
1190
              if_entry->d_name);
1240
1191
    }
1241
1192
    return 0;
1244
1195
  /* Reject down interfaces */
1245
1196
  if(not (ifr.ifr_flags & IFF_UP)){
1246
1197
    if(debug){
1247
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1248
 
              "Rejecting down interface \"%s\"\n",
 
1198
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1249
1199
              if_entry->d_name);
1250
1200
    }
1251
1201
    return 0;
1254
1204
  /* Reject non-running interfaces */
1255
1205
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1256
1206
    if(debug){
1257
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1258
 
              "Rejecting non-running interface \"%s\"\n",
 
1207
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1259
1208
              if_entry->d_name);
1260
1209
    }
1261
1210
    return 0;
1281
1230
/* Is this directory entry a runnable program? */
1282
1231
int runnable_hook(const struct dirent *direntry){
1283
1232
  int ret;
 
1233
  size_t sret;
1284
1234
  struct stat st;
1285
1235
  
1286
1236
  if((direntry->d_name)[0] == '\0'){
1288
1238
    return 0;
1289
1239
  }
1290
1240
  
1291
 
  /* Save pointer to last character */
1292
 
  char *end = strchr(direntry->d_name, '\0')-1;
1293
 
  
1294
 
  if(*end == '~'){
1295
 
    /* Backup name~ */
1296
 
    return 0;
1297
 
  }
1298
 
  
1299
 
  if(((direntry->d_name)[0] == '#')
1300
 
     and (*end == '#')){
1301
 
    /* Temporary #name# */
1302
 
    return 0;
1303
 
  }
1304
 
  
1305
 
  /* XXX more rules here */
1306
 
  
1307
 
  ret = stat(direntry->d_name, &st);
 
1241
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
1242
                "abcdefghijklmnopqrstuvwxyz"
 
1243
                "0123456789"
 
1244
                "_-");
 
1245
  if((direntry->d_name)[sret] != '\0'){
 
1246
    /* Contains non-allowed characters */
 
1247
    if(debug){
 
1248
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
 
1249
              direntry->d_name);
 
1250
    }
 
1251
    return 0;
 
1252
  }
 
1253
  
 
1254
  char *fullname = NULL;
 
1255
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1256
  if(ret < 0){
 
1257
    perror_plus("asprintf");
 
1258
    return 0;
 
1259
  }
 
1260
  
 
1261
  ret = stat(fullname, &st);
1308
1262
  if(ret == -1){
1309
1263
    if(debug){
1310
 
      perror_plus("Could not stat plugin");
 
1264
      perror_plus("Could not stat hook");
1311
1265
    }
1312
1266
    return 0;
1313
1267
  }
1314
1268
  if(not (S_ISREG(st.st_mode))){
1315
1269
    /* Not a regular file */
 
1270
    if(debug){
 
1271
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
 
1272
              direntry->d_name);
 
1273
    }
1316
1274
    return 0;
1317
1275
  }
1318
1276
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1319
1277
    /* Not executable */
 
1278
    if(debug){
 
1279
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
 
1280
              direntry->d_name);
 
1281
    }
1320
1282
    return 0;
1321
1283
  }
1322
1284
  return 1;
1331
1293
  while(true){
1332
1294
    if(mc.current_server == NULL){
1333
1295
      if (debug){
1334
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1335
 
                "Wait until first server is found. No timeout!\n");
 
1296
        fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1336
1297
      }
1337
1298
      ret = avahi_simple_poll_iterate(s, -1);
1338
1299
    } else {
1339
1300
      if (debug){
1340
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1341
 
                "Check current_server if we should run it,"
 
1301
        fprintf_plus(stderr, "Check current_server if we should run it,"
1342
1302
                " or wait\n");
1343
1303
      }
1344
1304
      /* the current time */
1361
1321
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1362
1322
      
1363
1323
      if (debug){
1364
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1365
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1324
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1366
1325
      }
1367
1326
      
1368
1327
      if(block_time <= 0){
1482
1441
        .arg = "SECONDS",
1483
1442
        .doc = "Retry interval used when denied by the mandos server",
1484
1443
        .group = 2 },
 
1444
      { .name = "network-hook-dir", .key = 133,
 
1445
        .arg = "DIR",
 
1446
        .doc = "Directory where network hooks are located",
 
1447
        .group = 2 },
1485
1448
      /*
1486
1449
       * These reproduce what we would get without ARGP_NO_HELP
1487
1450
       */
1540
1503
          argp_error(state, "Bad retry interval");
1541
1504
        }
1542
1505
        break;
 
1506
      case 133:                 /* --network-hook-dir */
 
1507
        hookdir = arg;
 
1508
        break;
1543
1509
        /*
1544
1510
         * These reproduce what we would get without ARGP_NO_HELP
1545
1511
         */
1551
1517
        argp_state_help(state, state->out_stream,
1552
1518
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1553
1519
      case 'V':                 /* --version */
1554
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1555
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1520
        fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
 
1521
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1556
1522
        exit(argp_err_exit_status);
1557
1523
        break;
1558
1524
      default:
1646
1612
  {
1647
1613
    struct dirent **direntries;
1648
1614
    struct dirent *direntry;
1649
 
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
 
1615
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
1650
1616
                           alphasort);
1651
1617
    if(numhooks == -1){
1652
1618
      perror_plus("scandir");
1655
1621
      for(int i = 0; i < numhooks; i++){
1656
1622
        direntry = direntries[0];
1657
1623
        char *fullname = NULL;
1658
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
1659
 
                       direntry->d_name);
 
1624
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1660
1625
        if(ret < 0){
1661
1626
          perror_plus("asprintf");
1662
1627
          continue;
1667
1632
          dup2(devnull, STDIN_FILENO);
1668
1633
          close(devnull);
1669
1634
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1635
          ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1636
          if(ret == -1){
 
1637
            perror_plus("setenv");
 
1638
            exit(1);
 
1639
          }
1670
1640
          ret = setenv("DEVICE", interface, 1);
1671
1641
          if(ret == -1){
1672
1642
            perror_plus("setenv");
1706
1676
          }
1707
1677
          if(WIFEXITED(status)){
1708
1678
            if(WEXITSTATUS(status) != 0){
1709
 
              fprintf(stderr, "Mandos plugin mandos-client: "
1710
 
                      "Warning: network hook \"%s\" exited"
 
1679
              fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1711
1680
                      " with status %d\n", direntry->d_name,
1712
1681
                      WEXITSTATUS(status));
1713
1682
              free(fullname);
1714
1683
              continue;
1715
1684
            }
1716
1685
          } else if(WIFSIGNALED(status)){
1717
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1718
 
                    "Warning: network hook \"%s\" died by"
 
1686
            fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1719
1687
                    " signal %d\n", direntry->d_name,
1720
1688
                    WTERMSIG(status));
1721
1689
            free(fullname);
1722
1690
            continue;
1723
1691
          } else {
1724
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1725
 
                    "Warning: network hook \"%s\" crashed\n",
 
1692
            fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1726
1693
                    direntry->d_name);
1727
1694
            free(fullname);
1728
1695
            continue;
1756
1723
      /* Pick the first interface returned */
1757
1724
      interface = strdup(direntries[0]->d_name);
1758
1725
      if(debug){
1759
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1760
 
                "Using interface \"%s\"\n", interface);
 
1726
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1761
1727
      }
1762
1728
      if(interface == NULL){
1763
1729
        perror_plus("malloc");
1768
1734
      free(direntries);
1769
1735
    } else {
1770
1736
      free(direntries);
1771
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1772
 
              "Could not find a network interface\n");
 
1737
      fprintf_plus(stderr, "Could not find a network interface\n");
1773
1738
      exitcode = EXIT_FAILURE;
1774
1739
      goto end;
1775
1740
    }
1781
1746
  srand((unsigned int) time(NULL));
1782
1747
  mc.simple_poll = avahi_simple_poll_new();
1783
1748
  if(mc.simple_poll == NULL){
1784
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1785
 
            "Avahi: Failed to create simple poll object.\n");
 
1749
    fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1786
1750
    exitcode = EX_UNAVAILABLE;
1787
1751
    goto end;
1788
1752
  }
1854
1818
  if(strcmp(interface, "none") != 0){
1855
1819
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1856
1820
    if(if_index == 0){
1857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1858
 
              "No such interface: \"%s\"\n", interface);
 
1821
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1859
1822
      exitcode = EX_UNAVAILABLE;
1860
1823
      goto end;
1861
1824
    }
2002
1965
  
2003
1966
  ret = init_gnutls_global(pubkey, seckey);
2004
1967
  if(ret == -1){
2005
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2006
 
            "init_gnutls_global failed\n");
 
1968
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2007
1969
    exitcode = EX_UNAVAILABLE;
2008
1970
    goto end;
2009
1971
  } else {
2025
1987
  }
2026
1988
  
2027
1989
  if(not init_gpgme(pubkey, seckey, tempdir)){
2028
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2029
 
            "init_gpgme failed\n");
 
1990
    fprintf_plus(stderr, "init_gpgme failed\n");
2030
1991
    exitcode = EX_UNAVAILABLE;
2031
1992
    goto end;
2032
1993
  } else {
2042
2003
    /* (Mainly meant for debugging) */
2043
2004
    char *address = strrchr(connect_to, ':');
2044
2005
    if(address == NULL){
2045
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2046
 
              "No colon in address\n");
 
2006
      fprintf_plus(stderr, "No colon in address\n");
2047
2007
      exitcode = EX_USAGE;
2048
2008
      goto end;
2049
2009
    }
2057
2017
    tmpmax = strtoimax(address+1, &tmp, 10);
2058
2018
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2059
2019
       or tmpmax != (uint16_t)tmpmax){
2060
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2061
 
              "Bad port number\n");
 
2020
      fprintf_plus(stderr, "Bad port number\n");
2062
2021
      exitcode = EX_USAGE;
2063
2022
      goto end;
2064
2023
    }
2094
2053
        break;
2095
2054
      }
2096
2055
      if(debug){
2097
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2098
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
2056
        fprintf_plus(stderr, "Retrying in %d seconds\n", (int)retry_interval);
2099
2057
      }
2100
2058
      sleep((int)retry_interval);
2101
2059
    }
2131
2089
  
2132
2090
  /* Check if creating the Avahi server object succeeded */
2133
2091
  if(mc.server == NULL){
2134
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2135
 
            "Failed to create Avahi server: %s\n",
 
2092
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2136
2093
            avahi_strerror(error));
2137
2094
    exitcode = EX_UNAVAILABLE;
2138
2095
    goto end;
2147
2104
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2148
2105
                                   NULL, 0, browse_callback, NULL);
2149
2106
  if(sb == NULL){
2150
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2151
 
            "Failed to create service browser: %s\n",
 
2107
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2152
2108
            avahi_strerror(avahi_server_errno(mc.server)));
2153
2109
    exitcode = EX_UNAVAILABLE;
2154
2110
    goto end;
2161
2117
  /* Run the main loop */
2162
2118
  
2163
2119
  if(debug){
2164
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2165
 
            "Starting Avahi loop search\n");
 
2120
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2166
2121
  }
2167
2122
 
2168
2123
  ret = avahi_loop_with_timeout(mc.simple_poll,
2169
2124
                                (int)(retry_interval * 1000));
2170
2125
  if(debug){
2171
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2172
 
            "avahi_loop_with_timeout exited %s\n",
 
2126
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2173
2127
            (ret == 0) ? "successfully" : "with error");
2174
2128
  }
2175
2129
  
2176
2130
 end:
2177
2131
  
2178
2132
  if(debug){
2179
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2180
 
            "%s exiting\n", argv[0]);
 
2133
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
2181
2134
  }
2182
2135
  
2183
2136
  /* Cleanup things */
2263
2216
        }
2264
2217
        ret = remove(fullname);
2265
2218
        if(ret == -1){
2266
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2267
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2219
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
2268
2220
        }
2269
2221
        free(fullname);
2270
2222
      }