/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

* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
                                             full path.
  (mail): Take new "--network-hook-dir" option.

Show diffs side-by-side

added added

removed removed

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