/mandos/trunk

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

« back to all changes in this revision

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

* plugins.d/mandos-client.c (SYNOPSIS, OPTIONS): Document
                                                 "--network-hook-dir"
                                                 option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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{
187
191
  return buffer_capacity;
188
192
}
189
193
 
 
194
/* Add server to set of servers to retry periodically */
190
195
int add_server(const char *ip, uint16_t port,
191
196
                 AvahiIfIndex if_index,
192
197
                 int af){
204
209
    perror_plus("strdup");
205
210
    return -1;
206
211
  }
207
 
  /* unique case of first server */
 
212
  /* Special case of first server */
208
213
  if (mc.current_server == NULL){
209
214
    new_server->next = new_server;
210
215
    new_server->prev = new_server;
211
216
    mc.current_server = new_server;
212
 
  /* Placing the new server last in the list */
 
217
  /* Place the new server last in the list */
213
218
  } else {
214
219
    new_server->next = mc.current_server;
215
220
    new_server->prev = mc.current_server->prev;
249
254
    
250
255
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
251
256
    if(rc != GPG_ERR_NO_ERROR){
252
 
      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",
253
259
              gpgme_strsource(rc), gpgme_strerror(rc));
254
260
      return false;
255
261
    }
256
262
    
257
263
    rc = gpgme_op_import(mc.ctx, pgp_data);
258
264
    if(rc != GPG_ERR_NO_ERROR){
259
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
265
      fprintf(stderr, "Mandos plugin mandos-client: "
 
266
              "bad gpgme_op_import: %s: %s\n",
260
267
              gpgme_strsource(rc), gpgme_strerror(rc));
261
268
      return false;
262
269
    }
270
277
  }
271
278
  
272
279
  if(debug){
273
 
    fprintf(stderr, "Initializing GPGME\n");
 
280
    fprintf(stderr, "Mandos plugin mandos-client: "
 
281
            "Initializing GPGME\n");
274
282
  }
275
283
  
276
284
  /* Init GPGME */
277
285
  gpgme_check_version(NULL);
278
286
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
279
287
  if(rc != GPG_ERR_NO_ERROR){
280
 
    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",
281
290
            gpgme_strsource(rc), gpgme_strerror(rc));
282
291
    return false;
283
292
  }
284
293
  
285
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
294
  /* Set GPGME home directory for the OpenPGP engine only */
286
295
  rc = gpgme_get_engine_info(&engine_info);
287
296
  if(rc != GPG_ERR_NO_ERROR){
288
 
    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",
289
299
            gpgme_strsource(rc), gpgme_strerror(rc));
290
300
    return false;
291
301
  }
298
308
    engine_info = engine_info->next;
299
309
  }
300
310
  if(engine_info == NULL){
301
 
    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);
302
313
    return false;
303
314
  }
304
315
  
305
316
  /* Create new GPGME "context" */
306
317
  rc = gpgme_new(&(mc.ctx));
307
318
  if(rc != GPG_ERR_NO_ERROR){
308
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
309
 
            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));
310
322
    return false;
311
323
  }
312
324
  
331
343
  ssize_t plaintext_length = 0;
332
344
  
333
345
  if(debug){
334
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
346
    fprintf(stderr, "Mandos plugin mandos-client: "
 
347
            "Trying to decrypt OpenPGP data\n");
335
348
  }
336
349
  
337
350
  /* Create new GPGME data buffer from memory cryptotext */
338
351
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
339
352
                               0);
340
353
  if(rc != GPG_ERR_NO_ERROR){
341
 
    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",
342
356
            gpgme_strsource(rc), gpgme_strerror(rc));
343
357
    return -1;
344
358
  }
346
360
  /* Create new empty GPGME data buffer for the plaintext */
347
361
  rc = gpgme_data_new(&dh_plain);
348
362
  if(rc != GPG_ERR_NO_ERROR){
349
 
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
 
363
    fprintf(stderr, "Mandos plugin mandos-client: "
 
364
            "bad gpgme_data_new: %s: %s\n",
350
365
            gpgme_strsource(rc), gpgme_strerror(rc));
351
366
    gpgme_data_release(dh_crypto);
352
367
    return -1;
356
371
     data buffer */
357
372
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
358
373
  if(rc != GPG_ERR_NO_ERROR){
359
 
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
374
    fprintf(stderr, "Mandos plugin mandos-client: "
 
375
            "bad gpgme_op_decrypt: %s: %s\n",
360
376
            gpgme_strsource(rc), gpgme_strerror(rc));
361
377
    plaintext_length = -1;
362
378
    if(debug){
363
379
      gpgme_decrypt_result_t result;
364
380
      result = gpgme_op_decrypt_result(mc.ctx);
365
381
      if(result == NULL){
366
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
382
        fprintf(stderr, "Mandos plugin mandos-client: "
 
383
                "gpgme_op_decrypt_result failed\n");
367
384
      } else {
368
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
385
        fprintf(stderr, "Mandos plugin mandos-client: "
 
386
                "Unsupported algorithm: %s\n",
369
387
                result->unsupported_algorithm);
370
 
        fprintf(stderr, "Wrong key usage: %u\n",
 
388
        fprintf(stderr, "Mandos plugin mandos-client: "
 
389
                "Wrong key usage: %u\n",
371
390
                result->wrong_key_usage);
372
391
        if(result->file_name != NULL){
373
 
          fprintf(stderr, "File name: %s\n", result->file_name);
 
392
          fprintf(stderr, "Mandos plugin mandos-client: "
 
393
                  "File name: %s\n", result->file_name);
374
394
        }
375
395
        gpgme_recipient_t recipient;
376
396
        recipient = result->recipients;
377
397
        while(recipient != NULL){
378
 
          fprintf(stderr, "Public key algorithm: %s\n",
 
398
          fprintf(stderr, "Mandos plugin mandos-client: "
 
399
                  "Public key algorithm: %s\n",
379
400
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
380
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
381
 
          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",
382
405
                  recipient->status == GPG_ERR_NO_SECKEY
383
406
                  ? "No" : "Yes");
384
407
          recipient = recipient->next;
389
412
  }
390
413
  
391
414
  if(debug){
392
 
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
 
415
    fprintf(stderr, "Mandos plugin mandos-client: "
 
416
            "Decryption of OpenPGP data succeeded\n");
393
417
  }
394
418
  
395
419
  /* Seek back to the beginning of the GPGME plaintext data buffer */
426
450
  }
427
451
  
428
452
  if(debug){
429
 
    fprintf(stderr, "Decrypted password is: ");
 
453
    fprintf(stderr, "Mandos plugin mandos-client: "
 
454
            "Decrypted password is: ");
430
455
    for(ssize_t i = 0; i < plaintext_length; i++){
431
456
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
432
457
    }
454
479
/* GnuTLS log function callback */
455
480
static void debuggnutls(__attribute__((unused)) int level,
456
481
                        const char* string){
457
 
  fprintf(stderr, "GnuTLS: %s", string);
 
482
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
458
483
}
459
484
 
460
485
static int init_gnutls_global(const char *pubkeyfilename,
462
487
  int ret;
463
488
  
464
489
  if(debug){
465
 
    fprintf(stderr, "Initializing GnuTLS\n");
 
490
    fprintf(stderr, "Mandos plugin mandos-client: "
 
491
            "Initializing GnuTLS\n");
466
492
  }
467
493
  
468
494
  ret = gnutls_global_init();
469
495
  if(ret != GNUTLS_E_SUCCESS){
470
 
    fprintf(stderr, "GnuTLS global_init: %s\n",
471
 
            safer_gnutls_strerror(ret));
 
496
    fprintf(stderr, "Mandos plugin mandos-client: "
 
497
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
472
498
    return -1;
473
499
  }
474
500
  
483
509
  /* OpenPGP credentials */
484
510
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
485
511
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
487
 
            safer_gnutls_strerror(ret));
 
512
    fprintf(stderr, "Mandos plugin mandos-client: "
 
513
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
488
514
    gnutls_global_deinit();
489
515
    return -1;
490
516
  }
491
517
  
492
518
  if(debug){
493
 
    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"
494
521
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
495
522
            seckeyfilename);
496
523
  }
500
527
     GNUTLS_OPENPGP_FMT_BASE64);
501
528
  if(ret != GNUTLS_E_SUCCESS){
502
529
    fprintf(stderr,
 
530
            "Mandos plugin mandos-client: "
503
531
            "Error[%d] while reading the OpenPGP key pair ('%s',"
504
532
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
505
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
506
 
            safer_gnutls_strerror(ret));
 
533
    fprintf(stderr, "Mandos plugin mandos-client: "
 
534
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
507
535
    goto globalfail;
508
536
  }
509
537
  
510
538
  /* GnuTLS server initialization */
511
539
  ret = gnutls_dh_params_init(&mc.dh_params);
512
540
  if(ret != GNUTLS_E_SUCCESS){
513
 
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
 
541
    fprintf(stderr, "Mandos plugin mandos-client: "
 
542
            "Error in GnuTLS DH parameter initialization:"
514
543
            " %s\n", safer_gnutls_strerror(ret));
515
544
    goto globalfail;
516
545
  }
517
546
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
518
547
  if(ret != GNUTLS_E_SUCCESS){
519
 
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
 
548
    fprintf(stderr, "Mandos plugin mandos-client: "
 
549
            "Error in GnuTLS prime generation: %s\n",
520
550
            safer_gnutls_strerror(ret));
521
551
    goto globalfail;
522
552
  }
543
573
    }
544
574
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
545
575
  if(ret != GNUTLS_E_SUCCESS){
546
 
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
 
576
    fprintf(stderr, "Mandos plugin mandos-client: "
 
577
            "Error in GnuTLS session initialization: %s\n",
547
578
            safer_gnutls_strerror(ret));
548
579
  }
549
580
  
557
588
      }
558
589
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
559
590
    if(ret != GNUTLS_E_SUCCESS){
560
 
      fprintf(stderr, "Syntax error at: %s\n", err);
561
 
      fprintf(stderr, "GnuTLS error: %s\n",
562
 
              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));
563
595
      gnutls_deinit(*session);
564
596
      return -1;
565
597
    }
574
606
    }
575
607
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
576
608
  if(ret != GNUTLS_E_SUCCESS){
577
 
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
609
    fprintf(stderr, "Mandos plugin mandos-client: "
 
610
            "Error setting GnuTLS credentials: %s\n",
578
611
            safer_gnutls_strerror(ret));
579
612
    gnutls_deinit(*session);
580
613
    return -1;
626
659
    pf = PF_INET;
627
660
    break;
628
661
  default:
629
 
    fprintf(stderr, "Bad address family: %d\n", af);
 
662
    fprintf(stderr, "Mandos plugin mandos-client: "
 
663
            "Bad address family: %d\n", af);
630
664
    errno = EINVAL;
631
665
    return -1;
632
666
  }
637
671
  }
638
672
  
639
673
  if(debug){
640
 
    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
641
676
            "\n", ip, port);
642
677
  }
643
678
  
670
705
  }
671
706
  if(ret == 0){
672
707
    int e = errno;
673
 
    fprintf(stderr, "Bad address: %s\n", ip);
 
708
    fprintf(stderr, "Mandos plugin mandos-client: "
 
709
            "Bad address: %s\n", ip);
674
710
    errno = e;
675
711
    goto mandos_end;
676
712
  }
683
719
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
684
720
                              -Wunreachable-code*/
685
721
      if(if_index == AVAHI_IF_UNSPEC){
686
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
722
        fprintf(stderr, "Mandos plugin mandos-client: "
 
723
                "An IPv6 link-local address is incomplete"
687
724
                " without a network interface\n");
688
725
        errno = EINVAL;
689
726
        goto mandos_end;
708
745
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
746
        perror_plus("if_indextoname");
710
747
      } else {
711
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
748
        fprintf(stderr, "Mandos plugin mandos-client: "
 
749
                "Connection to: %s%%%s, port %" PRIu16 "\n",
712
750
                ip, interface, port);
713
751
      }
714
752
    } else {
715
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
716
 
              port);
 
753
      fprintf(stderr, "Mandos plugin mandos-client: "
 
754
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
717
755
    }
718
756
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
719
757
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
729
767
      perror_plus("inet_ntop");
730
768
    } else {
731
769
      if(strcmp(addrstr, ip) != 0){
732
 
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
 
770
        fprintf(stderr, "Mandos plugin mandos-client: "
 
771
                "Canonical address form: %s\n", addrstr);
733
772
      }
734
773
    }
735
774
  }
789
828
  }
790
829
  
791
830
  if(debug){
792
 
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
 
831
    fprintf(stderr, "Mandos plugin mandos-client: "
 
832
            "Establishing TLS session with %s\n", ip);
793
833
  }
794
834
  
795
835
  if(quit_now){
815
855
  
816
856
  if(ret != GNUTLS_E_SUCCESS){
817
857
    if(debug){
818
 
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
858
      fprintf(stderr, "Mandos plugin mandos-client: "
 
859
              "*** GnuTLS Handshake failed ***\n");
819
860
      gnutls_perror(ret);
820
861
    }
821
862
    errno = EPROTO;
825
866
  /* Read OpenPGP packet that contains the wanted password */
826
867
  
827
868
  if(debug){
828
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
829
 
            ip);
 
869
    fprintf(stderr, "Mandos plugin mandos-client: "
 
870
            "Retrieving OpenPGP encrypted password from %s\n", ip);
830
871
  }
831
872
  
832
873
  while(true){
870
911
          }
871
912
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
872
913
        if(ret < 0){
873
 
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
914
          fprintf(stderr, "Mandos plugin mandos-client: "
 
915
                  "*** GnuTLS Re-handshake failed ***\n");
874
916
          gnutls_perror(ret);
875
917
          errno = EPROTO;
876
918
          goto mandos_end;
877
919
        }
878
920
        break;
879
921
      default:
880
 
        fprintf(stderr, "Unknown error while reading data from"
 
922
        fprintf(stderr, "Mandos plugin mandos-client: "
 
923
                "Unknown error while reading data from"
881
924
                " encrypted session with Mandos server\n");
882
925
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
883
926
        errno = EIO;
889
932
  }
890
933
  
891
934
  if(debug){
892
 
    fprintf(stderr, "Closing TLS session\n");
 
935
    fprintf(stderr, "Mandos plugin mandos-client: "
 
936
            "Closing TLS session\n");
893
937
  }
894
938
  
895
939
  if(quit_now){
925
969
        if(ret == 0 and ferror(stdout)){
926
970
          int e = errno;
927
971
          if(debug){
928
 
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
972
            fprintf(stderr, "Mandos plugin mandos-client: "
 
973
                    "Error writing encrypted data: %s\n",
929
974
                    strerror(errno));
930
975
          }
931
976
          errno = e;
989
1034
  switch(event){
990
1035
  default:
991
1036
  case AVAHI_RESOLVER_FAILURE:
992
 
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
 
1037
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1038
            "(Avahi Resolver) Failed to resolve service '%s'"
993
1039
            " of type '%s' in domain '%s': %s\n", name, type, domain,
994
1040
            avahi_strerror(avahi_server_errno(mc.server)));
995
1041
    break;
999
1045
      char ip[AVAHI_ADDRESS_STR_MAX];
1000
1046
      avahi_address_snprint(ip, sizeof(ip), address);
1001
1047
      if(debug){
1002
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1048
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1049
                "Mandos server \"%s\" found on %s (%s, %"
1003
1050
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1004
1051
                ip, (intmax_t)interface, port);
1005
1052
      }
1039
1086
  default:
1040
1087
  case AVAHI_BROWSER_FAILURE:
1041
1088
    
1042
 
    fprintf(stderr, "(Avahi browser) %s\n",
 
1089
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1090
            "(Avahi browser) %s\n",
1043
1091
            avahi_strerror(avahi_server_errno(mc.server)));
1044
1092
    avahi_simple_poll_quit(mc.simple_poll);
1045
1093
    return;
1053
1101
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1054
1102
                                    name, type, domain, protocol, 0,
1055
1103
                                    resolve_callback, NULL) == NULL)
1056
 
      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",
1057
1106
              name, avahi_strerror(avahi_server_errno(mc.server)));
1058
1107
    break;
1059
1108
    
1063
1112
  case AVAHI_BROWSER_ALL_FOR_NOW:
1064
1113
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1065
1114
    if(debug){
1066
 
      fprintf(stderr, "No Mandos server found, still searching...\n");
 
1115
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1116
              "No Mandos server found, still searching...\n");
1067
1117
    }
1068
1118
    break;
1069
1119
  }
1084
1134
  errno = old_errno;
1085
1135
}
1086
1136
 
1087
 
/* 
1088
 
 * This function determines if a directory entry in /sys/class/net
1089
 
 * corresponds to an acceptable network device.
1090
 
 * (This function is passed to scandir(3) as a filter function.)
1091
 
 */
1092
 
int good_interface(const struct dirent *if_entry){
1093
 
  ssize_t ssret;
1094
 
  char *flagname = NULL;
1095
 
  if(if_entry->d_name[0] == '.'){
1096
 
    return 0;
1097
 
  }
1098
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
 
                     if_entry->d_name);
1100
 
  if(ret < 0){
1101
 
    perror_plus("asprintf");
1102
 
    return 0;
1103
 
  }
1104
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
 
  if(flags_fd == -1){
1106
 
    perror_plus("open");
1107
 
    free(flagname);
1108
 
    return 0;
1109
 
  }
1110
 
  free(flagname);
1111
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
 
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
 
  flagstring[(size_t)to_read] = '\0';
1116
 
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
1118
 
    close(flags_fd);
1119
 
    return 0;
1120
 
  }
1121
 
  while(to_read > 0){
1122
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
 
                                             (size_t)to_read));
1124
 
    if(ssret == -1){
1125
 
      perror_plus("read");
1126
 
      free(flagstring);
1127
 
      close(flags_fd);
1128
 
      return 0;
1129
 
    }
1130
 
    to_read -= ssret;
1131
 
    if(ssret == 0){
1132
 
      break;
1133
 
    }
1134
 
  }
1135
 
  close(flags_fd);
1136
 
  intmax_t tmpmax;
1137
 
  char *tmp;
1138
 
  errno = 0;
1139
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1140
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
 
                                         and not (isspace(*tmp)))
1142
 
     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){
1143
1148
    if(debug){
1144
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
 
              flagstring, if_entry->d_name);
 
1149
      perror_plus("ioctl SIOCGIFFLAGS");
1146
1150
    }
1147
 
    free(flagstring);
1148
 
    return 0;
 
1151
    return false;
1149
1152
  }
1150
 
  free(flagstring);
1151
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1153
  return true;
 
1154
}
 
1155
 
 
1156
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1157
  
1152
1158
  /* Reject the loopback device */
1153
 
  if(flags & IFF_LOOPBACK){
 
1159
  if(ifr->ifr_flags & IFF_LOOPBACK){
1154
1160
    if(debug){
1155
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1156
 
              if_entry->d_name);
 
1161
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1162
              "Rejecting loopback interface \"%s\"\n", ifname);
1157
1163
    }
1158
 
    return 0;
 
1164
    return false;
1159
1165
  }
1160
1166
  /* Accept point-to-point devices only if connect_to is specified */
1161
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1167
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1162
1168
    if(debug){
1163
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1164
 
              if_entry->d_name);
 
1169
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1170
              "Accepting point-to-point interface \"%s\"\n", ifname);
1165
1171
    }
1166
 
    return 1;
 
1172
    return true;
1167
1173
  }
1168
1174
  /* Otherwise, reject non-broadcast-capable devices */
1169
 
  if(not (flags & IFF_BROADCAST)){
 
1175
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1170
1176
    if(debug){
1171
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1172
 
              if_entry->d_name);
 
1177
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1178
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1173
1179
    }
1174
 
    return 0;
 
1180
    return false;
1175
1181
  }
1176
1182
  /* Reject non-ARP interfaces (including dummy interfaces) */
1177
 
  if(flags & IFF_NOARP){
 
1183
  if(ifr->ifr_flags & IFF_NOARP){
1178
1184
    if(debug){
1179
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1180
 
              if_entry->d_name);
 
1185
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1186
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1181
1187
    }
1182
 
    return 0;
 
1188
    return false;
1183
1189
  }
 
1190
  
1184
1191
  /* Accept this device */
1185
1192
  if(debug){
1186
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1187
 
            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;
1188
1267
  }
1189
1268
  return 1;
1190
1269
}
1200
1279
  return 1;
1201
1280
}
1202
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
 
1203
1334
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1204
1335
  int ret;
1205
1336
  struct timespec now;
1206
1337
  struct timespec waited_time;
1207
1338
  intmax_t block_time;
1208
 
 
 
1339
  
1209
1340
  while(true){
1210
1341
    if(mc.current_server == NULL){
1211
1342
      if (debug){
1212
 
        fprintf(stderr,
 
1343
        fprintf(stderr, "Mandos plugin mandos-client: "
1213
1344
                "Wait until first server is found. No timeout!\n");
1214
1345
      }
1215
1346
      ret = avahi_simple_poll_iterate(s, -1);
1216
1347
    } else {
1217
1348
      if (debug){
1218
 
        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,"
1219
1351
                " or wait\n");
1220
1352
      }
1221
1353
      /* the current time */
1236
1368
      block_time = ((retry_interval
1237
1369
                     - ((intmax_t)waited_time.tv_sec * 1000))
1238
1370
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1239
 
 
 
1371
      
1240
1372
      if (debug){
1241
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
 
1373
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1374
                "Blocking for %" PRIdMAX " ms\n", block_time);
1242
1375
      }
1243
 
 
 
1376
      
1244
1377
      if(block_time <= 0){
1245
1378
        ret = start_mandos_communication(mc.current_server->ip,
1246
1379
                                         mc.current_server->port,
1264
1397
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1265
1398
    }
1266
1399
    if(ret != 0){
1267
 
      if (ret > 0 or errno != EINTR) {
 
1400
      if (ret > 0 or errno != EINTR){
1268
1401
        return (ret != 1) ? ret : 0;
1269
1402
      }
1270
1403
    }
1358
1491
        .arg = "SECONDS",
1359
1492
        .doc = "Retry interval used when denied by the mandos server",
1360
1493
        .group = 2 },
 
1494
      { .name = "network-hook-dir", .key = 133,
 
1495
        .arg = "DIR",
 
1496
        .doc = "Directory where network hooks are located",
 
1497
        .group = 2 },
1361
1498
      /*
1362
1499
       * These reproduce what we would get without ARGP_NO_HELP
1363
1500
       */
1411
1548
        errno = 0;
1412
1549
        retry_interval = strtod(arg, &tmp);
1413
1550
        if(errno != 0 or tmp == arg or *tmp != '\0'
1414
 
           or (retry_interval * 1000) > INT_MAX){
 
1551
           or (retry_interval * 1000) > INT_MAX
 
1552
           or retry_interval < 0){
1415
1553
          argp_error(state, "Bad retry interval");
1416
1554
        }
1417
1555
        break;
 
1556
      case 133:                 /* --network-hook-dir */
 
1557
        hookdir = arg;
 
1558
        break;
1418
1559
        /*
1419
1560
         * These reproduce what we would get without ARGP_NO_HELP
1420
1561
         */
1426
1567
        argp_state_help(state, state->out_stream,
1427
1568
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1428
1569
      case 'V':                 /* --version */
 
1570
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1429
1571
        fprintf(state->out_stream, "%s\n", argp_program_version);
1430
1572
        exit(argp_err_exit_status);
1431
1573
        break;
1468
1610
      perror_plus("seteuid");
1469
1611
    }
1470
1612
    
1471
 
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
 
    if(seckey_fd == -1){
1473
 
      perror_plus("open");
1474
 
    } else {
1475
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1476
 
      if(ret == -1){
1477
 
        perror_plus("fstat");
 
1613
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1614
      int seckey_fd = open(seckey, O_RDONLY);
 
1615
      if(seckey_fd == -1){
 
1616
        perror_plus("open");
1478
1617
      } else {
1479
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
 
          ret = fchown(seckey_fd, uid, gid);
1481
 
          if(ret == -1){
1482
 
            perror_plus("fchown");
 
1618
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1619
        if(ret == -1){
 
1620
          perror_plus("fstat");
 
1621
        } else {
 
1622
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1623
            ret = fchown(seckey_fd, uid, gid);
 
1624
            if(ret == -1){
 
1625
              perror_plus("fchown");
 
1626
            }
1483
1627
          }
1484
1628
        }
 
1629
        TEMP_FAILURE_RETRY(close(seckey_fd));
1485
1630
      }
1486
 
      TEMP_FAILURE_RETRY(close(seckey_fd));
1487
1631
    }
1488
1632
    
1489
 
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
 
    if(pubkey_fd == -1){
1491
 
      perror_plus("open");
1492
 
    } else {
1493
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1494
 
      if(ret == -1){
1495
 
        perror_plus("fstat");
 
1633
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1634
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1635
      if(pubkey_fd == -1){
 
1636
        perror_plus("open");
1496
1637
      } else {
1497
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
 
          ret = fchown(pubkey_fd, uid, gid);
1499
 
          if(ret == -1){
1500
 
            perror_plus("fchown");
 
1638
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1639
        if(ret == -1){
 
1640
          perror_plus("fstat");
 
1641
        } else {
 
1642
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1643
            ret = fchown(pubkey_fd, uid, gid);
 
1644
            if(ret == -1){
 
1645
              perror_plus("fchown");
 
1646
            }
1501
1647
          }
1502
1648
        }
 
1649
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1503
1650
      }
1504
 
      TEMP_FAILURE_RETRY(close(pubkey_fd));
1505
1651
    }
1506
1652
    
1507
1653
    /* Lower privileges */
1512
1658
    }
1513
1659
  }
1514
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
  
1515
1755
  if(not debug){
1516
1756
    avahi_set_log_function(empty_log);
1517
1757
  }
1518
 
 
 
1758
  
1519
1759
  if(interface[0] == '\0'){
1520
1760
    struct dirent **direntries;
1521
 
    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,
1522
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
    }
1523
1770
    if(ret >= 1){
1524
 
      /* Pick the first good interface */
 
1771
      /* Pick the first interface returned */
1525
1772
      interface = strdup(direntries[0]->d_name);
1526
1773
      if(debug){
1527
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1774
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1775
                "Using interface \"%s\"\n", interface);
1528
1776
      }
1529
1777
      if(interface == NULL){
1530
1778
        perror_plus("malloc");
1535
1783
      free(direntries);
1536
1784
    } else {
1537
1785
      free(direntries);
1538
 
      fprintf(stderr, "Could not find a network interface\n");
 
1786
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1787
              "Could not find a network interface\n");
1539
1788
      exitcode = EXIT_FAILURE;
1540
1789
      goto end;
1541
1790
    }
1547
1796
  srand((unsigned int) time(NULL));
1548
1797
  mc.simple_poll = avahi_simple_poll_new();
1549
1798
  if(mc.simple_poll == NULL){
1550
 
    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");
1551
1801
    exitcode = EX_UNAVAILABLE;
1552
1802
    goto end;
1553
1803
  }
1619
1869
  if(strcmp(interface, "none") != 0){
1620
1870
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1621
1871
    if(if_index == 0){
1622
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1872
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1873
              "No such interface: \"%s\"\n", interface);
1623
1874
      exitcode = EX_UNAVAILABLE;
1624
1875
      goto end;
1625
1876
    }
1766
2017
  
1767
2018
  ret = init_gnutls_global(pubkey, seckey);
1768
2019
  if(ret == -1){
1769
 
    fprintf(stderr, "init_gnutls_global failed\n");
 
2020
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2021
            "init_gnutls_global failed\n");
1770
2022
    exitcode = EX_UNAVAILABLE;
1771
2023
    goto end;
1772
2024
  } else {
1788
2040
  }
1789
2041
  
1790
2042
  if(not init_gpgme(pubkey, seckey, tempdir)){
1791
 
    fprintf(stderr, "init_gpgme failed\n");
 
2043
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2044
            "init_gpgme failed\n");
1792
2045
    exitcode = EX_UNAVAILABLE;
1793
2046
    goto end;
1794
2047
  } else {
1804
2057
    /* (Mainly meant for debugging) */
1805
2058
    char *address = strrchr(connect_to, ':');
1806
2059
    if(address == NULL){
1807
 
      fprintf(stderr, "No colon in address\n");
 
2060
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2061
              "No colon in address\n");
1808
2062
      exitcode = EX_USAGE;
1809
2063
      goto end;
1810
2064
    }
1818
2072
    tmpmax = strtoimax(address+1, &tmp, 10);
1819
2073
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1820
2074
       or tmpmax != (uint16_t)tmpmax){
1821
 
      fprintf(stderr, "Bad port number\n");
 
2075
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2076
              "Bad port number\n");
1822
2077
      exitcode = EX_USAGE;
1823
2078
      goto end;
1824
2079
    }
1829
2084
    
1830
2085
    port = (uint16_t)tmpmax;
1831
2086
    *address = '\0';
1832
 
    address = connect_to;
1833
2087
    /* Colon in address indicates IPv6 */
1834
2088
    int af;
1835
 
    if(strchr(address, ':') != NULL){
 
2089
    if(strchr(connect_to, ':') != NULL){
1836
2090
      af = AF_INET6;
 
2091
      /* Accept [] around IPv6 address - see RFC 5952 */
 
2092
      if(connect_to[0] == '[' and address[-1] == ']')
 
2093
        {
 
2094
          connect_to++;
 
2095
          address[-1] = '\0';
 
2096
        }
1837
2097
    } else {
1838
2098
      af = AF_INET;
1839
2099
    }
 
2100
    address = connect_to;
1840
2101
    
1841
2102
    if(quit_now){
1842
2103
      goto end;
1843
2104
    }
1844
 
 
 
2105
    
1845
2106
    while(not quit_now){
1846
2107
      ret = start_mandos_communication(address, port, if_index, af);
1847
2108
      if(quit_now or ret == 0){
1848
2109
        break;
1849
2110
      }
1850
 
      sleep((int)retry_interval or 1);
1851
 
    };
1852
 
 
 
2111
      if(debug){
 
2112
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2113
                "Retrying in %d seconds\n", (int)retry_interval);
 
2114
      }
 
2115
      sleep((int)retry_interval);
 
2116
    }
 
2117
    
1853
2118
    if (not quit_now){
1854
2119
      exitcode = EXIT_SUCCESS;
1855
2120
    }
1856
 
 
 
2121
    
1857
2122
    goto end;
1858
2123
  }
1859
2124
  
1881
2146
  
1882
2147
  /* Check if creating the Avahi server object succeeded */
1883
2148
  if(mc.server == NULL){
1884
 
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
2149
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2150
            "Failed to create Avahi server: %s\n",
1885
2151
            avahi_strerror(error));
1886
2152
    exitcode = EX_UNAVAILABLE;
1887
2153
    goto end;
1896
2162
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1897
2163
                                   NULL, 0, browse_callback, NULL);
1898
2164
  if(sb == NULL){
1899
 
    fprintf(stderr, "Failed to create service browser: %s\n",
 
2165
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2166
            "Failed to create service browser: %s\n",
1900
2167
            avahi_strerror(avahi_server_errno(mc.server)));
1901
2168
    exitcode = EX_UNAVAILABLE;
1902
2169
    goto end;
1909
2176
  /* Run the main loop */
1910
2177
  
1911
2178
  if(debug){
1912
 
    fprintf(stderr, "Starting Avahi loop search\n");
 
2179
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2180
            "Starting Avahi loop search\n");
1913
2181
  }
1914
2182
 
1915
2183
  ret = avahi_loop_with_timeout(mc.simple_poll,
1916
2184
                                (int)(retry_interval * 1000));
1917
2185
  if(debug){
1918
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2186
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2187
            "avahi_loop_with_timeout exited %s\n",
1919
2188
            (ret == 0) ? "successfully" : "with error");
1920
2189
  }
1921
2190
  
1922
2191
 end:
1923
2192
  
1924
2193
  if(debug){
1925
 
    fprintf(stderr, "%s exiting\n", argv[0]);
 
2194
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2195
            "%s exiting\n", argv[0]);
1926
2196
  }
1927
2197
  
1928
2198
  /* Cleanup things */
1956
2226
    }
1957
2227
  }
1958
2228
  
 
2229
  /* XXX run network hooks "stop" here  */
 
2230
  
1959
2231
  /* Take down the network interface */
1960
2232
  if(take_down_interface){
1961
2233
    /* Re-raise priviliges */
1968
2240
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1969
2241
      if(ret == -1){
1970
2242
        perror_plus("ioctl SIOCGIFFLAGS");
1971
 
      } else if(network.ifr_flags & IFF_UP) {
 
2243
      } else if(network.ifr_flags & IFF_UP){
1972
2244
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1973
2245
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1974
2246
        if(ret == -1){
1992
2264
  if(tempdir_created){
1993
2265
    struct dirent **direntries = NULL;
1994
2266
    struct dirent *direntry = NULL;
1995
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1996
 
    if (ret > 0){
1997
 
      for(int i = 0; i < ret; i++){
 
2267
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2268
                             alphasort);
 
2269
    if (numentries > 0){
 
2270
      for(int i = 0; i < numentries; i++){
1998
2271
        direntry = direntries[i];
1999
2272
        char *fullname = NULL;
2000
2273
        ret = asprintf(&fullname, "%s/%s", tempdir,
2005
2278
        }
2006
2279
        ret = remove(fullname);
2007
2280
        if(ret == -1){
2008
 
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2009
 
                  strerror(errno));
 
2281
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2282
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2010
2283
        }
2011
2284
        free(fullname);
2012
2285
      }
2013
2286
    }
2014
2287
 
2015
 
    /* need to be cleaned even if ret == 0 because man page doesn't
2016
 
       specify */
 
2288
    /* need to clean even if 0 because man page doesn't specify */
2017
2289
    free(direntries);
2018
 
    if (ret == -1){
 
2290
    if (numentries == -1){
2019
2291
      perror_plus("scandir");
2020
2292
    }
2021
2293
    ret = rmdir(tempdir);