/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 (good_interface): Use SIOCGIFFLAGS instead
                                              of reading
                                              /sys/class/net/*/flags.

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