/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 (SYNOPSIS, OPTIONS): Document
                                                 "--network-hook-dir"
                                                 option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open() */
 
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
85
85
                                   raise() */
86
86
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
89
                                   WEXITSTATUS(), WTERMSIG() */
88
90
 
89
91
#ifdef __linux__
90
92
#include <sys/klog.h>           /* klogctl() */
123
125
#define PATHDIR "/conf/conf.d/mandos"
124
126
#define SECKEY "seckey.txt"
125
127
#define PUBKEY "pubkey.txt"
 
128
#define HOOKDIR "/lib/mandos/network-hooks.d"
126
129
 
127
130
bool debug = false;
128
131
static const char mandos_protocol_version[] = "1";
130
133
const char *argp_program_bug_address = "<mandos@recompile.se>";
131
134
static const char sys_class_net[] = "/sys/class/net";
132
135
char *connect_to = NULL;
 
136
const char *hookdir = HOOKDIR;
133
137
 
134
138
/* Doubly linked list that need to be circularly linked when used */
135
139
typedef struct server{
250
254
    
251
255
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
252
256
    if(rc != GPG_ERR_NO_ERROR){
253
 
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
257
      fprintf(stderr, "Mandos plugin mandos-client: "
 
258
              "bad gpgme_data_new_from_fd: %s: %s\n",
254
259
              gpgme_strsource(rc), gpgme_strerror(rc));
255
260
      return false;
256
261
    }
257
262
    
258
263
    rc = gpgme_op_import(mc.ctx, pgp_data);
259
264
    if(rc != GPG_ERR_NO_ERROR){
260
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
265
      fprintf(stderr, "Mandos plugin mandos-client: "
 
266
              "bad gpgme_op_import: %s: %s\n",
261
267
              gpgme_strsource(rc), gpgme_strerror(rc));
262
268
      return false;
263
269
    }
271
277
  }
272
278
  
273
279
  if(debug){
274
 
    fprintf(stderr, "Initializing GPGME\n");
 
280
    fprintf(stderr, "Mandos plugin mandos-client: "
 
281
            "Initializing GPGME\n");
275
282
  }
276
283
  
277
284
  /* Init GPGME */
278
285
  gpgme_check_version(NULL);
279
286
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
280
287
  if(rc != GPG_ERR_NO_ERROR){
281
 
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
288
    fprintf(stderr, "Mandos plugin mandos-client: "
 
289
            "bad gpgme_engine_check_version: %s: %s\n",
282
290
            gpgme_strsource(rc), gpgme_strerror(rc));
283
291
    return false;
284
292
  }
286
294
  /* Set GPGME home directory for the OpenPGP engine only */
287
295
  rc = gpgme_get_engine_info(&engine_info);
288
296
  if(rc != GPG_ERR_NO_ERROR){
289
 
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
297
    fprintf(stderr, "Mandos plugin mandos-client: "
 
298
            "bad gpgme_get_engine_info: %s: %s\n",
290
299
            gpgme_strsource(rc), gpgme_strerror(rc));
291
300
    return false;
292
301
  }
299
308
    engine_info = engine_info->next;
300
309
  }
301
310
  if(engine_info == NULL){
302
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
311
    fprintf(stderr, "Mandos plugin mandos-client: "
 
312
            "Could not set GPGME home dir to %s\n", tempdir);
303
313
    return false;
304
314
  }
305
315
  
306
316
  /* Create new GPGME "context" */
307
317
  rc = gpgme_new(&(mc.ctx));
308
318
  if(rc != GPG_ERR_NO_ERROR){
309
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
319
    fprintf(stderr, "Mandos plugin mandos-client: "
 
320
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
321
            gpgme_strerror(rc));
311
322
    return false;
312
323
  }
313
324
  
332
343
  ssize_t plaintext_length = 0;
333
344
  
334
345
  if(debug){
335
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
346
    fprintf(stderr, "Mandos plugin mandos-client: "
 
347
            "Trying to decrypt OpenPGP data\n");
336
348
  }
337
349
  
338
350
  /* Create new GPGME data buffer from memory cryptotext */
339
351
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
340
352
                               0);
341
353
  if(rc != GPG_ERR_NO_ERROR){
342
 
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
354
    fprintf(stderr, "Mandos plugin mandos-client: "
 
355
            "bad gpgme_data_new_from_mem: %s: %s\n",
343
356
            gpgme_strsource(rc), gpgme_strerror(rc));
344
357
    return -1;
345
358
  }
347
360
  /* Create new empty GPGME data buffer for the plaintext */
348
361
  rc = gpgme_data_new(&dh_plain);
349
362
  if(rc != GPG_ERR_NO_ERROR){
350
 
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
 
363
    fprintf(stderr, "Mandos plugin mandos-client: "
 
364
            "bad gpgme_data_new: %s: %s\n",
351
365
            gpgme_strsource(rc), gpgme_strerror(rc));
352
366
    gpgme_data_release(dh_crypto);
353
367
    return -1;
357
371
     data buffer */
358
372
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
359
373
  if(rc != GPG_ERR_NO_ERROR){
360
 
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
374
    fprintf(stderr, "Mandos plugin mandos-client: "
 
375
            "bad gpgme_op_decrypt: %s: %s\n",
361
376
            gpgme_strsource(rc), gpgme_strerror(rc));
362
377
    plaintext_length = -1;
363
378
    if(debug){
364
379
      gpgme_decrypt_result_t result;
365
380
      result = gpgme_op_decrypt_result(mc.ctx);
366
381
      if(result == NULL){
367
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
382
        fprintf(stderr, "Mandos plugin mandos-client: "
 
383
                "gpgme_op_decrypt_result failed\n");
368
384
      } else {
369
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
385
        fprintf(stderr, "Mandos plugin mandos-client: "
 
386
                "Unsupported algorithm: %s\n",
370
387
                result->unsupported_algorithm);
371
 
        fprintf(stderr, "Wrong key usage: %u\n",
 
388
        fprintf(stderr, "Mandos plugin mandos-client: "
 
389
                "Wrong key usage: %u\n",
372
390
                result->wrong_key_usage);
373
391
        if(result->file_name != NULL){
374
 
          fprintf(stderr, "File name: %s\n", result->file_name);
 
392
          fprintf(stderr, "Mandos plugin mandos-client: "
 
393
                  "File name: %s\n", result->file_name);
375
394
        }
376
395
        gpgme_recipient_t recipient;
377
396
        recipient = result->recipients;
378
397
        while(recipient != NULL){
379
 
          fprintf(stderr, "Public key algorithm: %s\n",
 
398
          fprintf(stderr, "Mandos plugin mandos-client: "
 
399
                  "Public key algorithm: %s\n",
380
400
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
 
          fprintf(stderr, "Secret key available: %s\n",
 
401
          fprintf(stderr, "Mandos plugin mandos-client: "
 
402
                  "Key ID: %s\n", recipient->keyid);
 
403
          fprintf(stderr, "Mandos plugin mandos-client: "
 
404
                  "Secret key available: %s\n",
383
405
                  recipient->status == GPG_ERR_NO_SECKEY
384
406
                  ? "No" : "Yes");
385
407
          recipient = recipient->next;
390
412
  }
391
413
  
392
414
  if(debug){
393
 
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
 
415
    fprintf(stderr, "Mandos plugin mandos-client: "
 
416
            "Decryption of OpenPGP data succeeded\n");
394
417
  }
395
418
  
396
419
  /* Seek back to the beginning of the GPGME plaintext data buffer */
427
450
  }
428
451
  
429
452
  if(debug){
430
 
    fprintf(stderr, "Decrypted password is: ");
 
453
    fprintf(stderr, "Mandos plugin mandos-client: "
 
454
            "Decrypted password is: ");
431
455
    for(ssize_t i = 0; i < plaintext_length; i++){
432
456
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
433
457
    }
455
479
/* GnuTLS log function callback */
456
480
static void debuggnutls(__attribute__((unused)) int level,
457
481
                        const char* string){
458
 
  fprintf(stderr, "GnuTLS: %s", string);
 
482
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
459
483
}
460
484
 
461
485
static int init_gnutls_global(const char *pubkeyfilename,
463
487
  int ret;
464
488
  
465
489
  if(debug){
466
 
    fprintf(stderr, "Initializing GnuTLS\n");
 
490
    fprintf(stderr, "Mandos plugin mandos-client: "
 
491
            "Initializing GnuTLS\n");
467
492
  }
468
493
  
469
494
  ret = gnutls_global_init();
470
495
  if(ret != GNUTLS_E_SUCCESS){
471
 
    fprintf(stderr, "GnuTLS global_init: %s\n",
472
 
            safer_gnutls_strerror(ret));
 
496
    fprintf(stderr, "Mandos plugin mandos-client: "
 
497
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
473
498
    return -1;
474
499
  }
475
500
  
484
509
  /* OpenPGP credentials */
485
510
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
511
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
488
 
            safer_gnutls_strerror(ret));
 
512
    fprintf(stderr, "Mandos plugin mandos-client: "
 
513
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
489
514
    gnutls_global_deinit();
490
515
    return -1;
491
516
  }
492
517
  
493
518
  if(debug){
494
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
519
    fprintf(stderr, "Mandos plugin mandos-client: "
 
520
            "Attempting to use OpenPGP public key %s and"
495
521
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
496
522
            seckeyfilename);
497
523
  }
501
527
     GNUTLS_OPENPGP_FMT_BASE64);
502
528
  if(ret != GNUTLS_E_SUCCESS){
503
529
    fprintf(stderr,
 
530
            "Mandos plugin mandos-client: "
504
531
            "Error[%d] while reading the OpenPGP key pair ('%s',"
505
532
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
507
 
            safer_gnutls_strerror(ret));
 
533
    fprintf(stderr, "Mandos plugin mandos-client: "
 
534
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
508
535
    goto globalfail;
509
536
  }
510
537
  
511
538
  /* GnuTLS server initialization */
512
539
  ret = gnutls_dh_params_init(&mc.dh_params);
513
540
  if(ret != GNUTLS_E_SUCCESS){
514
 
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
 
541
    fprintf(stderr, "Mandos plugin mandos-client: "
 
542
            "Error in GnuTLS DH parameter initialization:"
515
543
            " %s\n", safer_gnutls_strerror(ret));
516
544
    goto globalfail;
517
545
  }
518
546
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
547
  if(ret != GNUTLS_E_SUCCESS){
520
 
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
 
548
    fprintf(stderr, "Mandos plugin mandos-client: "
 
549
            "Error in GnuTLS prime generation: %s\n",
521
550
            safer_gnutls_strerror(ret));
522
551
    goto globalfail;
523
552
  }
544
573
    }
545
574
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
546
575
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
 
576
    fprintf(stderr, "Mandos plugin mandos-client: "
 
577
            "Error in GnuTLS session initialization: %s\n",
548
578
            safer_gnutls_strerror(ret));
549
579
  }
550
580
  
558
588
      }
559
589
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
560
590
    if(ret != GNUTLS_E_SUCCESS){
561
 
      fprintf(stderr, "Syntax error at: %s\n", err);
562
 
      fprintf(stderr, "GnuTLS error: %s\n",
563
 
              safer_gnutls_strerror(ret));
 
591
      fprintf(stderr, "Mandos plugin mandos-client: "
 
592
              "Syntax error at: %s\n", err);
 
593
      fprintf(stderr, "Mandos plugin mandos-client: "
 
594
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
564
595
      gnutls_deinit(*session);
565
596
      return -1;
566
597
    }
575
606
    }
576
607
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
577
608
  if(ret != GNUTLS_E_SUCCESS){
578
 
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
609
    fprintf(stderr, "Mandos plugin mandos-client: "
 
610
            "Error setting GnuTLS credentials: %s\n",
579
611
            safer_gnutls_strerror(ret));
580
612
    gnutls_deinit(*session);
581
613
    return -1;
627
659
    pf = PF_INET;
628
660
    break;
629
661
  default:
630
 
    fprintf(stderr, "Bad address family: %d\n", af);
 
662
    fprintf(stderr, "Mandos plugin mandos-client: "
 
663
            "Bad address family: %d\n", af);
631
664
    errno = EINVAL;
632
665
    return -1;
633
666
  }
638
671
  }
639
672
  
640
673
  if(debug){
641
 
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
674
    fprintf(stderr, "Mandos plugin mandos-client: "
 
675
            "Setting up a TCP connection to %s, port %" PRIu16
642
676
            "\n", ip, port);
643
677
  }
644
678
  
671
705
  }
672
706
  if(ret == 0){
673
707
    int e = errno;
674
 
    fprintf(stderr, "Bad address: %s\n", ip);
 
708
    fprintf(stderr, "Mandos plugin mandos-client: "
 
709
            "Bad address: %s\n", ip);
675
710
    errno = e;
676
711
    goto mandos_end;
677
712
  }
684
719
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
685
720
                              -Wunreachable-code*/
686
721
      if(if_index == AVAHI_IF_UNSPEC){
687
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
722
        fprintf(stderr, "Mandos plugin mandos-client: "
 
723
                "An IPv6 link-local address is incomplete"
688
724
                " without a network interface\n");
689
725
        errno = EINVAL;
690
726
        goto mandos_end;
709
745
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
746
        perror_plus("if_indextoname");
711
747
      } else {
712
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
748
        fprintf(stderr, "Mandos plugin mandos-client: "
 
749
                "Connection to: %s%%%s, port %" PRIu16 "\n",
713
750
                ip, interface, port);
714
751
      }
715
752
    } else {
716
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
717
 
              port);
 
753
      fprintf(stderr, "Mandos plugin mandos-client: "
 
754
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
718
755
    }
719
756
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
720
757
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
730
767
      perror_plus("inet_ntop");
731
768
    } else {
732
769
      if(strcmp(addrstr, ip) != 0){
733
 
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
 
770
        fprintf(stderr, "Mandos plugin mandos-client: "
 
771
                "Canonical address form: %s\n", addrstr);
734
772
      }
735
773
    }
736
774
  }
790
828
  }
791
829
  
792
830
  if(debug){
793
 
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
 
831
    fprintf(stderr, "Mandos plugin mandos-client: "
 
832
            "Establishing TLS session with %s\n", ip);
794
833
  }
795
834
  
796
835
  if(quit_now){
816
855
  
817
856
  if(ret != GNUTLS_E_SUCCESS){
818
857
    if(debug){
819
 
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
858
      fprintf(stderr, "Mandos plugin mandos-client: "
 
859
              "*** GnuTLS Handshake failed ***\n");
820
860
      gnutls_perror(ret);
821
861
    }
822
862
    errno = EPROTO;
826
866
  /* Read OpenPGP packet that contains the wanted password */
827
867
  
828
868
  if(debug){
829
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
830
 
            ip);
 
869
    fprintf(stderr, "Mandos plugin mandos-client: "
 
870
            "Retrieving OpenPGP encrypted password from %s\n", ip);
831
871
  }
832
872
  
833
873
  while(true){
871
911
          }
872
912
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
873
913
        if(ret < 0){
874
 
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
914
          fprintf(stderr, "Mandos plugin mandos-client: "
 
915
                  "*** GnuTLS Re-handshake failed ***\n");
875
916
          gnutls_perror(ret);
876
917
          errno = EPROTO;
877
918
          goto mandos_end;
878
919
        }
879
920
        break;
880
921
      default:
881
 
        fprintf(stderr, "Unknown error while reading data from"
 
922
        fprintf(stderr, "Mandos plugin mandos-client: "
 
923
                "Unknown error while reading data from"
882
924
                " encrypted session with Mandos server\n");
883
925
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
884
926
        errno = EIO;
890
932
  }
891
933
  
892
934
  if(debug){
893
 
    fprintf(stderr, "Closing TLS session\n");
 
935
    fprintf(stderr, "Mandos plugin mandos-client: "
 
936
            "Closing TLS session\n");
894
937
  }
895
938
  
896
939
  if(quit_now){
926
969
        if(ret == 0 and ferror(stdout)){
927
970
          int e = errno;
928
971
          if(debug){
929
 
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
972
            fprintf(stderr, "Mandos plugin mandos-client: "
 
973
                    "Error writing encrypted data: %s\n",
930
974
                    strerror(errno));
931
975
          }
932
976
          errno = e;
990
1034
  switch(event){
991
1035
  default:
992
1036
  case AVAHI_RESOLVER_FAILURE:
993
 
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
 
1037
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1038
            "(Avahi Resolver) Failed to resolve service '%s'"
994
1039
            " of type '%s' in domain '%s': %s\n", name, type, domain,
995
1040
            avahi_strerror(avahi_server_errno(mc.server)));
996
1041
    break;
1000
1045
      char ip[AVAHI_ADDRESS_STR_MAX];
1001
1046
      avahi_address_snprint(ip, sizeof(ip), address);
1002
1047
      if(debug){
1003
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1048
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1049
                "Mandos server \"%s\" found on %s (%s, %"
1004
1050
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
1051
                ip, (intmax_t)interface, port);
1006
1052
      }
1040
1086
  default:
1041
1087
  case AVAHI_BROWSER_FAILURE:
1042
1088
    
1043
 
    fprintf(stderr, "(Avahi browser) %s\n",
 
1089
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1090
            "(Avahi browser) %s\n",
1044
1091
            avahi_strerror(avahi_server_errno(mc.server)));
1045
1092
    avahi_simple_poll_quit(mc.simple_poll);
1046
1093
    return;
1054
1101
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1055
1102
                                    name, type, domain, protocol, 0,
1056
1103
                                    resolve_callback, NULL) == NULL)
1057
 
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
 
1104
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1105
              "Avahi: Failed to resolve service '%s': %s\n",
1058
1106
              name, avahi_strerror(avahi_server_errno(mc.server)));
1059
1107
    break;
1060
1108
    
1064
1112
  case AVAHI_BROWSER_ALL_FOR_NOW:
1065
1113
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1066
1114
    if(debug){
1067
 
      fprintf(stderr, "No Mandos server found, still searching...\n");
 
1115
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1116
              "No Mandos server found, still searching...\n");
1068
1117
    }
1069
1118
    break;
1070
1119
  }
1109
1158
  /* Reject the loopback device */
1110
1159
  if(ifr->ifr_flags & IFF_LOOPBACK){
1111
1160
    if(debug){
1112
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1113
 
              ifname);
 
1161
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1162
              "Rejecting loopback interface \"%s\"\n", ifname);
1114
1163
    }
1115
1164
    return false;
1116
1165
  }
1117
1166
  /* Accept point-to-point devices only if connect_to is specified */
1118
1167
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1119
1168
    if(debug){
1120
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1121
 
              ifname);
 
1169
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1170
              "Accepting point-to-point interface \"%s\"\n", ifname);
1122
1171
    }
1123
1172
    return true;
1124
1173
  }
1125
1174
  /* Otherwise, reject non-broadcast-capable devices */
1126
1175
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1127
1176
    if(debug){
1128
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1129
 
              ifname);
 
1177
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1178
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1130
1179
    }
1131
1180
    return false;
1132
1181
  }
1133
1182
  /* Reject non-ARP interfaces (including dummy interfaces) */
1134
1183
  if(ifr->ifr_flags & IFF_NOARP){
1135
1184
    if(debug){
1136
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1185
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1186
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1137
1187
    }
1138
1188
    return false;
1139
1189
  }
1140
1190
  
1141
1191
  /* Accept this device */
1142
1192
  if(debug){
1143
 
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1193
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1194
            "Interface \"%s\" is good\n", ifname);
1144
1195
  }
1145
1196
  return true;
1146
1197
}
1151
1202
 * (This function is passed to scandir(3) as a filter function.)
1152
1203
 */
1153
1204
int good_interface(const struct dirent *if_entry){
1154
 
  int ret;
1155
1205
  if(if_entry->d_name[0] == '.'){
1156
1206
    return 0;
1157
1207
  }
 
1208
  
1158
1209
  struct ifreq ifr;
 
1210
  if(not get_flags(if_entry->d_name, &ifr)){
 
1211
    if(debug){
 
1212
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1213
              "Failed to get flags for interface \"%s\"\n",
 
1214
              if_entry->d_name);
 
1215
    }
 
1216
    return 0;
 
1217
  }
 
1218
  
 
1219
  if(not good_flags(if_entry->d_name, &ifr)){
 
1220
    return 0;
 
1221
  }
 
1222
  return 1;
 
1223
}
1159
1224
 
 
1225
/* 
 
1226
 * This function determines if a directory entry in /sys/class/net
 
1227
 * corresponds to an acceptable network device which is up.
 
1228
 * (This function is passed to scandir(3) as a filter function.)
 
1229
 */
 
1230
int up_interface(const struct dirent *if_entry){
 
1231
  if(if_entry->d_name[0] == '.'){
 
1232
    return 0;
 
1233
  }
 
1234
  
 
1235
  struct ifreq ifr;
1160
1236
  if(not get_flags(if_entry->d_name, &ifr)){
 
1237
    if(debug){
 
1238
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1239
              "Failed to get flags for interface \"%s\"\n",
 
1240
              if_entry->d_name);
 
1241
    }
 
1242
    return 0;
 
1243
  }
 
1244
  
 
1245
  /* Reject down interfaces */
 
1246
  if(not (ifr.ifr_flags & IFF_UP)){
 
1247
    if(debug){
 
1248
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1249
              "Rejecting down interface \"%s\"\n",
 
1250
              if_entry->d_name);
 
1251
    }
 
1252
    return 0;
 
1253
  }
 
1254
  
 
1255
  /* Reject non-running interfaces */
 
1256
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1257
    if(debug){
 
1258
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1259
              "Rejecting non-running interface \"%s\"\n",
 
1260
              if_entry->d_name);
 
1261
    }
1161
1262
    return 0;
1162
1263
  }
1163
1264
  
1178
1279
  return 1;
1179
1280
}
1180
1281
 
 
1282
/* Is this directory entry a runnable program? */
 
1283
int runnable_hook(const struct dirent *direntry){
 
1284
  int ret;
 
1285
  struct stat st;
 
1286
  
 
1287
  if((direntry->d_name)[0] == '\0'){
 
1288
    /* Empty name? */
 
1289
    return 0;
 
1290
  }
 
1291
  
 
1292
  /* Save pointer to last character */
 
1293
  char *end = strchr(direntry->d_name, '\0')-1;
 
1294
  
 
1295
  if(*end == '~'){
 
1296
    /* Backup name~ */
 
1297
    return 0;
 
1298
  }
 
1299
  
 
1300
  if(((direntry->d_name)[0] == '#')
 
1301
     and (*end == '#')){
 
1302
    /* Temporary #name# */
 
1303
    return 0;
 
1304
  }
 
1305
  
 
1306
  /* XXX more rules here */
 
1307
  
 
1308
  char *fullname = NULL;
 
1309
  ret = asprintf(&fullname, "%s/%s", hookdir,
 
1310
                 direntry->d_name);
 
1311
  if(ret < 0){
 
1312
    perror_plus("asprintf");
 
1313
    return 0;
 
1314
  }
 
1315
  
 
1316
  ret = stat(fullname, &st);
 
1317
  if(ret == -1){
 
1318
    if(debug){
 
1319
      perror_plus("Could not stat plugin");
 
1320
    }
 
1321
    return 0;
 
1322
  }
 
1323
  if(not (S_ISREG(st.st_mode))){
 
1324
    /* Not a regular file */
 
1325
    return 0;
 
1326
  }
 
1327
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1328
    /* Not executable */
 
1329
    return 0;
 
1330
  }
 
1331
  return 1;
 
1332
}
 
1333
 
1181
1334
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1182
1335
  int ret;
1183
1336
  struct timespec now;
1187
1340
  while(true){
1188
1341
    if(mc.current_server == NULL){
1189
1342
      if (debug){
1190
 
        fprintf(stderr,
 
1343
        fprintf(stderr, "Mandos plugin mandos-client: "
1191
1344
                "Wait until first server is found. No timeout!\n");
1192
1345
      }
1193
1346
      ret = avahi_simple_poll_iterate(s, -1);
1194
1347
    } else {
1195
1348
      if (debug){
1196
 
        fprintf(stderr, "Check current_server if we should run it,"
 
1349
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1350
                "Check current_server if we should run it,"
1197
1351
                " or wait\n");
1198
1352
      }
1199
1353
      /* the current time */
1216
1370
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1217
1371
      
1218
1372
      if (debug){
1219
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1373
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1374
                "Blocking for %" PRIdMAX " ms\n", block_time);
1220
1375
      }
1221
1376
      
1222
1377
      if(block_time <= 0){
1242
1397
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1243
1398
    }
1244
1399
    if(ret != 0){
1245
 
      if (ret > 0 or errno != EINTR) {
 
1400
      if (ret > 0 or errno != EINTR){
1246
1401
        return (ret != 1) ? ret : 0;
1247
1402
      }
1248
1403
    }
1336
1491
        .arg = "SECONDS",
1337
1492
        .doc = "Retry interval used when denied by the mandos server",
1338
1493
        .group = 2 },
 
1494
      { .name = "network-hook-dir", .key = 133,
 
1495
        .arg = "DIR",
 
1496
        .doc = "Directory where network hooks are located",
 
1497
        .group = 2 },
1339
1498
      /*
1340
1499
       * These reproduce what we would get without ARGP_NO_HELP
1341
1500
       */
1394
1553
          argp_error(state, "Bad retry interval");
1395
1554
        }
1396
1555
        break;
 
1556
      case 133:                 /* --network-hook-dir */
 
1557
        hookdir = arg;
 
1558
        break;
1397
1559
        /*
1398
1560
         * These reproduce what we would get without ARGP_NO_HELP
1399
1561
         */
1405
1567
        argp_state_help(state, state->out_stream,
1406
1568
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1407
1569
      case 'V':                 /* --version */
 
1570
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1408
1571
        fprintf(state->out_stream, "%s\n", argp_program_version);
1409
1572
        exit(argp_err_exit_status);
1410
1573
        break;
1495
1658
    }
1496
1659
  }
1497
1660
  
 
1661
  /* Find network hooks and run them */
 
1662
  {
 
1663
    struct dirent **direntries;
 
1664
    struct dirent *direntry;
 
1665
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1666
                           alphasort);
 
1667
    if(numhooks == -1){
 
1668
      perror_plus("scandir");
 
1669
    } else {
 
1670
      int devnull = open("/dev/null", O_RDONLY);
 
1671
      for(int i = 0; i < numhooks; i++){
 
1672
        direntry = direntries[0];
 
1673
        char *fullname = NULL;
 
1674
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1675
        if(ret < 0){
 
1676
          perror_plus("asprintf");
 
1677
          continue;
 
1678
        }
 
1679
        pid_t hook_pid = fork();
 
1680
        if(hook_pid == 0){
 
1681
          /* Child */
 
1682
          dup2(devnull, STDIN_FILENO);
 
1683
          close(devnull);
 
1684
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1685
          ret = setenv("DEVICE", interface, 1);
 
1686
          if(ret == -1){
 
1687
            perror_plus("setenv");
 
1688
            exit(1);
 
1689
          }
 
1690
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1691
          if(ret == -1){
 
1692
            perror_plus("setenv");
 
1693
            exit(1);
 
1694
          }
 
1695
          ret = setenv("MODE", "start", 1);
 
1696
          if(ret == -1){
 
1697
            perror_plus("setenv");
 
1698
            exit(1);
 
1699
          }
 
1700
          char *delaystring;
 
1701
          ret = asprintf(&delaystring, "%f", delay);
 
1702
          if(ret == -1){
 
1703
            perror_plus("asprintf");
 
1704
            exit(1);
 
1705
          }
 
1706
          ret = setenv("DELAY", delaystring, 1);
 
1707
          if(ret == -1){
 
1708
            free(delaystring);
 
1709
            perror_plus("setenv");
 
1710
            exit(1);
 
1711
          }
 
1712
          free(delaystring);
 
1713
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1714
          perror_plus("execl");
 
1715
        } else {
 
1716
          int status;
 
1717
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1718
            perror_plus("waitpid");
 
1719
            free(fullname);
 
1720
            continue;
 
1721
          }
 
1722
          if(WIFEXITED(status)){
 
1723
            if(WEXITSTATUS(status) != 0){
 
1724
              fprintf(stderr, "Mandos plugin mandos-client: "
 
1725
                      "Warning: network hook \"%s\" exited"
 
1726
                      " with status %d\n", direntry->d_name,
 
1727
                      WEXITSTATUS(status));
 
1728
              free(fullname);
 
1729
              continue;
 
1730
            }
 
1731
          } else if(WIFSIGNALED(status)){
 
1732
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1733
                    "Warning: network hook \"%s\" died by"
 
1734
                    " signal %d\n", direntry->d_name,
 
1735
                    WTERMSIG(status));
 
1736
            free(fullname);
 
1737
            continue;
 
1738
          } else {
 
1739
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1740
                    "Warning: network hook \"%s\" crashed\n",
 
1741
                    direntry->d_name);
 
1742
            free(fullname);
 
1743
            continue;
 
1744
          }
 
1745
        }
 
1746
        free(fullname);
 
1747
        if(quit_now){
 
1748
          goto end;
 
1749
        }
 
1750
      }
 
1751
      close(devnull);
 
1752
    }
 
1753
  }
 
1754
  
1498
1755
  if(not debug){
1499
1756
    avahi_set_log_function(empty_log);
1500
1757
  }
1501
 
 
 
1758
  
1502
1759
  if(interface[0] == '\0'){
1503
1760
    struct dirent **direntries;
1504
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1761
    /* First look for interfaces that are up */
 
1762
    ret = scandir(sys_class_net, &direntries, up_interface,
1505
1763
                  alphasort);
 
1764
    if(ret == 0){
 
1765
      /* No up interfaces, look for any good interfaces */
 
1766
      free(direntries);
 
1767
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1768
                    alphasort);
 
1769
    }
1506
1770
    if(ret >= 1){
1507
 
      /* Pick the first good interface */
 
1771
      /* Pick the first interface returned */
1508
1772
      interface = strdup(direntries[0]->d_name);
1509
1773
      if(debug){
1510
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1774
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1775
                "Using interface \"%s\"\n", interface);
1511
1776
      }
1512
1777
      if(interface == NULL){
1513
1778
        perror_plus("malloc");
1518
1783
      free(direntries);
1519
1784
    } else {
1520
1785
      free(direntries);
1521
 
      fprintf(stderr, "Could not find a network interface\n");
 
1786
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1787
              "Could not find a network interface\n");
1522
1788
      exitcode = EXIT_FAILURE;
1523
1789
      goto end;
1524
1790
    }
1530
1796
  srand((unsigned int) time(NULL));
1531
1797
  mc.simple_poll = avahi_simple_poll_new();
1532
1798
  if(mc.simple_poll == NULL){
1533
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1799
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1800
            "Avahi: Failed to create simple poll object.\n");
1534
1801
    exitcode = EX_UNAVAILABLE;
1535
1802
    goto end;
1536
1803
  }
1602
1869
  if(strcmp(interface, "none") != 0){
1603
1870
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1604
1871
    if(if_index == 0){
1605
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1872
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1873
              "No such interface: \"%s\"\n", interface);
1606
1874
      exitcode = EX_UNAVAILABLE;
1607
1875
      goto end;
1608
1876
    }
1749
2017
  
1750
2018
  ret = init_gnutls_global(pubkey, seckey);
1751
2019
  if(ret == -1){
1752
 
    fprintf(stderr, "init_gnutls_global failed\n");
 
2020
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2021
            "init_gnutls_global failed\n");
1753
2022
    exitcode = EX_UNAVAILABLE;
1754
2023
    goto end;
1755
2024
  } else {
1771
2040
  }
1772
2041
  
1773
2042
  if(not init_gpgme(pubkey, seckey, tempdir)){
1774
 
    fprintf(stderr, "init_gpgme failed\n");
 
2043
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2044
            "init_gpgme failed\n");
1775
2045
    exitcode = EX_UNAVAILABLE;
1776
2046
    goto end;
1777
2047
  } else {
1787
2057
    /* (Mainly meant for debugging) */
1788
2058
    char *address = strrchr(connect_to, ':');
1789
2059
    if(address == NULL){
1790
 
      fprintf(stderr, "No colon in address\n");
 
2060
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2061
              "No colon in address\n");
1791
2062
      exitcode = EX_USAGE;
1792
2063
      goto end;
1793
2064
    }
1801
2072
    tmpmax = strtoimax(address+1, &tmp, 10);
1802
2073
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1803
2074
       or tmpmax != (uint16_t)tmpmax){
1804
 
      fprintf(stderr, "Bad port number\n");
 
2075
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2076
              "Bad port number\n");
1805
2077
      exitcode = EX_USAGE;
1806
2078
      goto end;
1807
2079
    }
1837
2109
        break;
1838
2110
      }
1839
2111
      if(debug){
1840
 
        fprintf(stderr, "Retrying in %d seconds\n",
1841
 
                (int)retry_interval);
 
2112
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2113
                "Retrying in %d seconds\n", (int)retry_interval);
1842
2114
      }
1843
2115
      sleep((int)retry_interval);
1844
2116
    }
1846
2118
    if (not quit_now){
1847
2119
      exitcode = EXIT_SUCCESS;
1848
2120
    }
1849
 
 
 
2121
    
1850
2122
    goto end;
1851
2123
  }
1852
2124
  
1874
2146
  
1875
2147
  /* Check if creating the Avahi server object succeeded */
1876
2148
  if(mc.server == NULL){
1877
 
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
2149
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2150
            "Failed to create Avahi server: %s\n",
1878
2151
            avahi_strerror(error));
1879
2152
    exitcode = EX_UNAVAILABLE;
1880
2153
    goto end;
1889
2162
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1890
2163
                                   NULL, 0, browse_callback, NULL);
1891
2164
  if(sb == NULL){
1892
 
    fprintf(stderr, "Failed to create service browser: %s\n",
 
2165
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2166
            "Failed to create service browser: %s\n",
1893
2167
            avahi_strerror(avahi_server_errno(mc.server)));
1894
2168
    exitcode = EX_UNAVAILABLE;
1895
2169
    goto end;
1902
2176
  /* Run the main loop */
1903
2177
  
1904
2178
  if(debug){
1905
 
    fprintf(stderr, "Starting Avahi loop search\n");
 
2179
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2180
            "Starting Avahi loop search\n");
1906
2181
  }
1907
2182
 
1908
2183
  ret = avahi_loop_with_timeout(mc.simple_poll,
1909
2184
                                (int)(retry_interval * 1000));
1910
2185
  if(debug){
1911
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2186
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2187
            "avahi_loop_with_timeout exited %s\n",
1912
2188
            (ret == 0) ? "successfully" : "with error");
1913
2189
  }
1914
2190
  
1915
2191
 end:
1916
2192
  
1917
2193
  if(debug){
1918
 
    fprintf(stderr, "%s exiting\n", argv[0]);
 
2194
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2195
            "%s exiting\n", argv[0]);
1919
2196
  }
1920
2197
  
1921
2198
  /* Cleanup things */
1949
2226
    }
1950
2227
  }
1951
2228
  
 
2229
  /* XXX run network hooks "stop" here  */
 
2230
  
1952
2231
  /* Take down the network interface */
1953
2232
  if(take_down_interface){
1954
2233
    /* Re-raise priviliges */
1961
2240
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1962
2241
      if(ret == -1){
1963
2242
        perror_plus("ioctl SIOCGIFFLAGS");
1964
 
      } else if(network.ifr_flags & IFF_UP) {
 
2243
      } else if(network.ifr_flags & IFF_UP){
1965
2244
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1966
2245
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1967
2246
        if(ret == -1){
1999
2278
        }
2000
2279
        ret = remove(fullname);
2001
2280
        if(ret == -1){
2002
 
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2003
 
                  strerror(errno));
 
2281
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2282
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2004
2283
        }
2005
2284
        free(fullname);
2006
2285
      }