/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

Change "fukt.bsnet.se" to "recompile.se" throughout.

* README: - '' -
* debian/control: - '' -
* debian/copyright: - '' -
* debian/mandos-client.README.Debian: - '' - and some rewriting.
* debian/mandos.README.Debian: - '' -
* debian/watch: Change "fukt.bsnet.se" to "recompile.se".
* init.d-mandos: - '' -
* intro.xml: - '' -
* mandos: - '' -
* mandos-clients.conf.xml: - '' -
* mandos-ctl: - '' -
* mandos-ctl.xml: - '' -
* mandos-keygen: - '' -
* mandos-keygen.xml: - '' -
* mandos-monitor: - '' -
* mandos-monitor.xml: - '' -
* mandos.conf.xml: - '' -
* mandos.lsm: - '' -
* mandos.xml: - '' -
* plugin-runner.c: - '' -
* plugin-runner.xml: - '' -
* plugins.d/askpass-fifo.c: - '' -
* plugins.d/askpass-fifo.xml: - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/mandos-client.xml: - '' -
* plugins.d/password-prompt.c: - '' -
* plugins.d/password-prompt.xml: - '' -
* plugins.d/plymouth.c: - '' -
* plugins.d/plymouth.xml: - '' -
* plugins.d/splashy.c: - '' -
* plugins.d/splashy.xml: - '' -
* plugins.d/usplash.c: - '' -
* plugins.d/usplash.xml: - '' -

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
  }
1134
1085
  errno = old_errno;
1135
1086
}
1136
1087
 
1137
 
bool get_flags(const char *ifname, struct ifreq *ifr){
1138
 
  int ret;
1139
 
  
1140
 
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1141
 
  if(s < 0){
1142
 
    perror_plus("socket");
1143
 
    return false;
1144
 
  }
1145
 
  strcpy(ifr->ifr_name, ifname);
1146
 
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1147
 
  if(ret == -1){
 
1088
/* 
 
1089
 * This function determines if a directory entry in /sys/class/net
 
1090
 * corresponds to an acceptable network device.
 
1091
 * (This function is passed to scandir(3) as a filter function.)
 
1092
 */
 
1093
int good_interface(const struct dirent *if_entry){
 
1094
  ssize_t ssret;
 
1095
  char *flagname = NULL;
 
1096
  if(if_entry->d_name[0] == '.'){
 
1097
    return 0;
 
1098
  }
 
1099
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
1100
                     if_entry->d_name);
 
1101
  if(ret < 0){
 
1102
    perror_plus("asprintf");
 
1103
    return 0;
 
1104
  }
 
1105
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
 
1106
  if(flags_fd == -1){
 
1107
    perror_plus("open");
 
1108
    free(flagname);
 
1109
    return 0;
 
1110
  }
 
1111
  free(flagname);
 
1112
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
 
1113
  /* read line from flags_fd */
 
1114
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1115
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1116
  flagstring[(size_t)to_read] = '\0';
 
1117
  if(flagstring == NULL){
 
1118
    perror_plus("malloc");
 
1119
    close(flags_fd);
 
1120
    return 0;
 
1121
  }
 
1122
  while(to_read > 0){
 
1123
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1124
                                             (size_t)to_read));
 
1125
    if(ssret == -1){
 
1126
      perror_plus("read");
 
1127
      free(flagstring);
 
1128
      close(flags_fd);
 
1129
      return 0;
 
1130
    }
 
1131
    to_read -= ssret;
 
1132
    if(ssret == 0){
 
1133
      break;
 
1134
    }
 
1135
  }
 
1136
  close(flags_fd);
 
1137
  intmax_t tmpmax;
 
1138
  char *tmp;
 
1139
  errno = 0;
 
1140
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1141
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1142
                                         and not (isspace(*tmp)))
 
1143
     or tmpmax != (ifreq_flags)tmpmax){
1148
1144
    if(debug){
1149
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1145
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1146
              flagstring, if_entry->d_name);
1150
1147
    }
1151
 
    return false;
 
1148
    free(flagstring);
 
1149
    return 0;
1152
1150
  }
1153
 
  return true;
1154
 
}
1155
 
 
1156
 
bool good_flags(const char *ifname, const struct ifreq *ifr){
1157
 
  
 
1151
  free(flagstring);
 
1152
  ifreq_flags flags = (ifreq_flags)tmpmax;
1158
1153
  /* Reject the loopback device */
1159
 
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1154
  if(flags & IFF_LOOPBACK){
1160
1155
    if(debug){
1161
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1162
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1156
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1157
              if_entry->d_name);
1163
1158
    }
1164
 
    return false;
 
1159
    return 0;
1165
1160
  }
1166
1161
  /* Accept point-to-point devices only if connect_to is specified */
1167
 
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1162
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1168
1163
    if(debug){
1169
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1170
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1164
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1165
              if_entry->d_name);
1171
1166
    }
1172
 
    return true;
 
1167
    return 1;
1173
1168
  }
1174
1169
  /* Otherwise, reject non-broadcast-capable devices */
1175
 
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1170
  if(not (flags & IFF_BROADCAST)){
1176
1171
    if(debug){
1177
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1178
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1172
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1173
              if_entry->d_name);
1179
1174
    }
1180
 
    return false;
 
1175
    return 0;
1181
1176
  }
1182
1177
  /* Reject non-ARP interfaces (including dummy interfaces) */
1183
 
  if(ifr->ifr_flags & IFF_NOARP){
 
1178
  if(flags & IFF_NOARP){
1184
1179
    if(debug){
1185
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1186
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1180
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1181
              if_entry->d_name);
1187
1182
    }
1188
 
    return false;
 
1183
    return 0;
1189
1184
  }
1190
 
  
1191
1185
  /* Accept this device */
1192
1186
  if(debug){
1193
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1194
 
            "Interface \"%s\" is good\n", ifname);
1195
 
  }
1196
 
  return true;
1197
 
}
1198
 
 
1199
 
/* 
1200
 
 * This function determines if a directory entry in /sys/class/net
1201
 
 * corresponds to an acceptable network device.
1202
 
 * (This function is passed to scandir(3) as a filter function.)
1203
 
 */
1204
 
int good_interface(const struct dirent *if_entry){
1205
 
  if(if_entry->d_name[0] == '.'){
1206
 
    return 0;
1207
 
  }
1208
 
  
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
 
}
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;
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
 
    }
1262
 
    return 0;
1263
 
  }
1264
 
  
1265
 
  if(not good_flags(if_entry->d_name, &ifr)){
1266
 
    return 0;
 
1187
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1188
            if_entry->d_name);
1267
1189
  }
1268
1190
  return 1;
1269
1191
}
1279
1201
  return 1;
1280
1202
}
1281
1203
 
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
1204
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1335
1205
  int ret;
1336
1206
  struct timespec now;
1340
1210
  while(true){
1341
1211
    if(mc.current_server == NULL){
1342
1212
      if (debug){
1343
 
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1213
        fprintf(stderr,
1344
1214
                "Wait until first server is found. No timeout!\n");
1345
1215
      }
1346
1216
      ret = avahi_simple_poll_iterate(s, -1);
1347
1217
    } else {
1348
1218
      if (debug){
1349
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1350
 
                "Check current_server if we should run it,"
 
1219
        fprintf(stderr, "Check current_server if we should run it,"
1351
1220
                " or wait\n");
1352
1221
      }
1353
1222
      /* the current time */
1370
1239
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1371
1240
      
1372
1241
      if (debug){
1373
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1374
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1242
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1375
1243
      }
1376
1244
      
1377
1245
      if(block_time <= 0){
1397
1265
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1398
1266
    }
1399
1267
    if(ret != 0){
1400
 
      if (ret > 0 or errno != EINTR){
 
1268
      if (ret > 0 or errno != EINTR) {
1401
1269
        return (ret != 1) ? ret : 0;
1402
1270
      }
1403
1271
    }
1491
1359
        .arg = "SECONDS",
1492
1360
        .doc = "Retry interval used when denied by the mandos server",
1493
1361
        .group = 2 },
1494
 
      { .name = "network-hook-dir", .key = 133,
1495
 
        .arg = "DIR",
1496
 
        .doc = "Directory where network hooks are located",
1497
 
        .group = 2 },
1498
1362
      /*
1499
1363
       * These reproduce what we would get without ARGP_NO_HELP
1500
1364
       */
1553
1417
          argp_error(state, "Bad retry interval");
1554
1418
        }
1555
1419
        break;
1556
 
      case 133:                 /* --network-hook-dir */
1557
 
        hookdir = arg;
1558
 
        break;
1559
1420
        /*
1560
1421
         * These reproduce what we would get without ARGP_NO_HELP
1561
1422
         */
1567
1428
        argp_state_help(state, state->out_stream,
1568
1429
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1569
1430
      case 'V':                 /* --version */
1570
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1571
1431
        fprintf(state->out_stream, "%s\n", argp_program_version);
1572
1432
        exit(argp_err_exit_status);
1573
1433
        break;
1658
1518
    }
1659
1519
  }
1660
1520
  
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
1521
  if(not debug){
1756
1522
    avahi_set_log_function(empty_log);
1757
1523
  }
1758
 
  
 
1524
 
1759
1525
  if(interface[0] == '\0'){
1760
1526
    struct dirent **direntries;
1761
 
    /* First look for interfaces that are up */
1762
 
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1527
    ret = scandir(sys_class_net, &direntries, good_interface,
1763
1528
                  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
1529
    if(ret >= 1){
1771
 
      /* Pick the first interface returned */
 
1530
      /* Pick the first good interface */
1772
1531
      interface = strdup(direntries[0]->d_name);
1773
1532
      if(debug){
1774
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1775
 
                "Using interface \"%s\"\n", interface);
 
1533
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1776
1534
      }
1777
1535
      if(interface == NULL){
1778
1536
        perror_plus("malloc");
1783
1541
      free(direntries);
1784
1542
    } else {
1785
1543
      free(direntries);
1786
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1787
 
              "Could not find a network interface\n");
 
1544
      fprintf(stderr, "Could not find a network interface\n");
1788
1545
      exitcode = EXIT_FAILURE;
1789
1546
      goto end;
1790
1547
    }
1796
1553
  srand((unsigned int) time(NULL));
1797
1554
  mc.simple_poll = avahi_simple_poll_new();
1798
1555
  if(mc.simple_poll == NULL){
1799
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1800
 
            "Avahi: Failed to create simple poll object.\n");
 
1556
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1801
1557
    exitcode = EX_UNAVAILABLE;
1802
1558
    goto end;
1803
1559
  }
1869
1625
  if(strcmp(interface, "none") != 0){
1870
1626
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1871
1627
    if(if_index == 0){
1872
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1873
 
              "No such interface: \"%s\"\n", interface);
 
1628
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1874
1629
      exitcode = EX_UNAVAILABLE;
1875
1630
      goto end;
1876
1631
    }
2017
1772
  
2018
1773
  ret = init_gnutls_global(pubkey, seckey);
2019
1774
  if(ret == -1){
2020
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2021
 
            "init_gnutls_global failed\n");
 
1775
    fprintf(stderr, "init_gnutls_global failed\n");
2022
1776
    exitcode = EX_UNAVAILABLE;
2023
1777
    goto end;
2024
1778
  } else {
2040
1794
  }
2041
1795
  
2042
1796
  if(not init_gpgme(pubkey, seckey, tempdir)){
2043
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2044
 
            "init_gpgme failed\n");
 
1797
    fprintf(stderr, "init_gpgme failed\n");
2045
1798
    exitcode = EX_UNAVAILABLE;
2046
1799
    goto end;
2047
1800
  } else {
2057
1810
    /* (Mainly meant for debugging) */
2058
1811
    char *address = strrchr(connect_to, ':');
2059
1812
    if(address == NULL){
2060
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2061
 
              "No colon in address\n");
 
1813
      fprintf(stderr, "No colon in address\n");
2062
1814
      exitcode = EX_USAGE;
2063
1815
      goto end;
2064
1816
    }
2072
1824
    tmpmax = strtoimax(address+1, &tmp, 10);
2073
1825
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2074
1826
       or tmpmax != (uint16_t)tmpmax){
2075
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2076
 
              "Bad port number\n");
 
1827
      fprintf(stderr, "Bad port number\n");
2077
1828
      exitcode = EX_USAGE;
2078
1829
      goto end;
2079
1830
    }
2109
1860
        break;
2110
1861
      }
2111
1862
      if(debug){
2112
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2113
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
1863
        fprintf(stderr, "Retrying in %d seconds\n",
 
1864
                (int)retry_interval);
2114
1865
      }
2115
1866
      sleep((int)retry_interval);
2116
1867
    }
2118
1869
    if (not quit_now){
2119
1870
      exitcode = EXIT_SUCCESS;
2120
1871
    }
2121
 
    
 
1872
 
2122
1873
    goto end;
2123
1874
  }
2124
1875
  
2146
1897
  
2147
1898
  /* Check if creating the Avahi server object succeeded */
2148
1899
  if(mc.server == NULL){
2149
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2150
 
            "Failed to create Avahi server: %s\n",
 
1900
    fprintf(stderr, "Failed to create Avahi server: %s\n",
2151
1901
            avahi_strerror(error));
2152
1902
    exitcode = EX_UNAVAILABLE;
2153
1903
    goto end;
2162
1912
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2163
1913
                                   NULL, 0, browse_callback, NULL);
2164
1914
  if(sb == NULL){
2165
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2166
 
            "Failed to create service browser: %s\n",
 
1915
    fprintf(stderr, "Failed to create service browser: %s\n",
2167
1916
            avahi_strerror(avahi_server_errno(mc.server)));
2168
1917
    exitcode = EX_UNAVAILABLE;
2169
1918
    goto end;
2176
1925
  /* Run the main loop */
2177
1926
  
2178
1927
  if(debug){
2179
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2180
 
            "Starting Avahi loop search\n");
 
1928
    fprintf(stderr, "Starting Avahi loop search\n");
2181
1929
  }
2182
1930
 
2183
1931
  ret = avahi_loop_with_timeout(mc.simple_poll,
2184
1932
                                (int)(retry_interval * 1000));
2185
1933
  if(debug){
2186
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2187
 
            "avahi_loop_with_timeout exited %s\n",
 
1934
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2188
1935
            (ret == 0) ? "successfully" : "with error");
2189
1936
  }
2190
1937
  
2191
1938
 end:
2192
1939
  
2193
1940
  if(debug){
2194
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2195
 
            "%s exiting\n", argv[0]);
 
1941
    fprintf(stderr, "%s exiting\n", argv[0]);
2196
1942
  }
2197
1943
  
2198
1944
  /* Cleanup things */
2226
1972
    }
2227
1973
  }
2228
1974
  
2229
 
  /* XXX run network hooks "stop" here  */
2230
 
  
2231
1975
  /* Take down the network interface */
2232
1976
  if(take_down_interface){
2233
1977
    /* Re-raise priviliges */
2240
1984
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2241
1985
      if(ret == -1){
2242
1986
        perror_plus("ioctl SIOCGIFFLAGS");
2243
 
      } else if(network.ifr_flags & IFF_UP){
 
1987
      } else if(network.ifr_flags & IFF_UP) {
2244
1988
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2245
1989
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2246
1990
        if(ret == -1){
2278
2022
        }
2279
2023
        ret = remove(fullname);
2280
2024
        if(ret == -1){
2281
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2282
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2025
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
2026
                  strerror(errno));
2283
2027
        }
2284
2028
        free(fullname);
2285
2029
      }