/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/mandosclient.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 * includes the following functions: "resolve_callback",
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
 
 * Everything else is
12
 
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
11
 * Everything else is Copyright © 2007-2008 Teddy Hogeborn and Björn
 
12
 * Påhlsson.
13
13
 * 
14
14
 * This program is free software: you can redistribute it and/or
15
15
 * modify it under the terms of the GNU General Public License as
29
29
 * <https://www.fukt.bsnet.se/~teddy/>.
30
30
 */
31
31
 
32
 
/* Needed by GPGME, specifically gpgme_data_seek() */
 
32
#define _FORTIFY_SOURCE 2
 
33
 
33
34
#define _LARGEFILE_SOURCE
34
35
#define _FILE_OFFSET_BITS 64
35
36
 
343
344
               __attribute__((unused)) const char *txt){}
344
345
 
345
346
int start_mandos_communication(const char *ip, uint16_t port,
346
 
                               AvahiIfIndex if_index){
 
347
                               unsigned int if_index){
347
348
  int ret, tcp_sd;
348
349
  struct sockaddr_in6 to;
349
350
  encrypted_session es;
357
358
  char interface[IF_NAMESIZE];
358
359
  
359
360
  if(debug){
360
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
361
 
            ip, port);
 
361
    fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
362
362
  }
363
363
  
364
364
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
367
367
    return -1;
368
368
  }
369
369
  
370
 
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
370
  if(if_indextoname(if_index, interface) == NULL){
371
371
    if(debug){
372
372
      perror("if_indextoname");
373
373
    }
394
394
  to.sin6_scope_id = (uint32_t)if_index;
395
395
  
396
396
  if(debug){
397
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
398
 
/*     char addrstr[INET6_ADDRSTRLEN]; */
399
 
/*     if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
400
 
/*               sizeof(addrstr)) == NULL){ */
401
 
/*       perror("inet_ntop"); */
402
 
/*     } else { */
403
 
/*       fprintf(stderr, "Really connecting to: %s, port %d\n", */
404
 
/*            addrstr, ntohs(to.sin6_port)); */
405
 
/*     } */
 
397
    fprintf(stderr, "Connection to: %s\n", ip);
406
398
  }
407
399
  
408
400
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
489
481
                                               &decrypted_buffer,
490
482
                                               CERT_ROOT);
491
483
    if (decrypted_buffer_size >= 0){
492
 
      while(written < (size_t) decrypted_buffer_size){
 
484
      while(written < decrypted_buffer_size){
493
485
        ret = (int)fwrite (decrypted_buffer + written, 1,
494
486
                           (size_t)decrypted_buffer_size - written,
495
487
                           stdout);
564
556
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
565
557
                " port %d\n", name, host_name, ip, port);
566
558
      }
567
 
      int ret = start_mandos_communication(ip, port, interface);
 
559
      int ret = start_mandos_communication(ip, port,
 
560
                                           (unsigned int) interface);
568
561
      if (ret == 0){
569
562
        exit(EXIT_SUCCESS);
570
563
      }
628
621
    int error;
629
622
    int ret;
630
623
    int returncode = EXIT_SUCCESS;
631
 
    const char *interface = NULL;
632
 
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
633
 
    char *connect_to = NULL;
 
624
    const char *interface = "eth0";
634
625
    
635
626
    while (true){
636
627
      static struct option long_options[] = {
637
628
        {"debug", no_argument, (int *)&debug, 1},
638
 
        {"connect", required_argument, 0, 'c'},
639
629
        {"interface", required_argument, 0, 'i'},
640
630
        {0, 0, 0, 0} };
641
631
      
653
643
      case 'i':
654
644
        interface = optarg;
655
645
        break;
656
 
      case 'c':
657
 
        connect_to = optarg;
658
 
        break;
659
646
      default:
660
647
        exit(EXIT_FAILURE);
661
648
      }
662
649
    }
663
650
    
664
 
    if(interface != NULL){
665
 
      if_index = (AvahiIfIndex) if_nametoindex(interface);
666
 
      if(if_index == 0){
667
 
        fprintf(stderr, "No such interface: \"%s\"\n", interface);
668
 
        exit(EXIT_FAILURE);
669
 
      }
670
 
    }
671
 
    
672
 
    if(connect_to != NULL){
673
 
      /* Connect directly, do not use Zeroconf */
674
 
      /* (Mainly meant for debugging) */
675
 
      char *address = strrchr(connect_to, ':');
676
 
      if(address == NULL){
677
 
        fprintf(stderr, "No colon in address\n");
678
 
        exit(EXIT_FAILURE);
679
 
      }
680
 
      errno = 0;
681
 
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
682
 
      if(errno){
683
 
        perror("Bad port number");
684
 
        exit(EXIT_FAILURE);
685
 
      }
686
 
      *address = '\0';
687
 
      address = connect_to;
688
 
      ret = start_mandos_communication(address, port, if_index);
689
 
      if(ret < 0){
690
 
        exit(EXIT_FAILURE);
691
 
      } else {
692
 
        exit(EXIT_SUCCESS);
693
 
      }
694
 
    }
695
 
    
696
651
    if (not debug){
697
652
      avahi_set_log_function(empty_log);
698
653
    }
730
685
    }
731
686
    
732
687
    /* Create the service browser */
733
 
    sb = avahi_s_service_browser_new(server, if_index,
 
688
    sb = avahi_s_service_browser_new(server,
 
689
                                     (AvahiIfIndex)
 
690
                                     if_nametoindex(interface),
734
691
                                     AVAHI_PROTO_INET6,
735
692
                                     "_mandos._tcp", NULL, 0,
736
693
                                     browse_callback, server);