/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

  • Committer: Teddy Hogeborn
  • Date: 2008-07-31 19:48:05 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080731194805-mseis21dxwrdfqhk
* plugins.d/mandosclient.c (start_mandos_communication): Changed
                                                        "if_index" to
                                                        be of type
                                                        "AvahiIfIndex".
                                                        All callers
                                                        changed.
 (main): Add default values to "interface" and "if_index".  Only
         change if_index from default if "interface" was given.

* server.py (IPv6_TCPServer.server_bind): Bug fix: test if interface
                                          is empty, not if equal to
                                          avahi.IF_UNSPEC.
  (if_nametoindex): Bug fix; typo: assign to _func[0], not func[0].
  (main): Bug fix: Do not set service.interface unless the interface
          setting has been given.

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 Copyright © 2007-2008 Teddy Hogeborn and Björn
12
 
 * Påhlsson.
 
11
 * Everything else is
 
12
 * Copyright © 2007-2008 Teddy Hogeborn & Björn 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
 
#define _FORTIFY_SOURCE 2
33
 
 
 
32
/* Needed by GPGME, specifically gpgme_data_seek() */
34
33
#define _LARGEFILE_SOURCE
35
34
#define _FILE_OFFSET_BITS 64
36
35
 
344
343
               __attribute__((unused)) const char *txt){}
345
344
 
346
345
int start_mandos_communication(const char *ip, uint16_t port,
347
 
                               unsigned int if_index){
 
346
                               AvahiIfIndex if_index){
348
347
  int ret, tcp_sd;
349
348
  struct sockaddr_in6 to;
350
349
  encrypted_session es;
358
357
  char interface[IF_NAMESIZE];
359
358
  
360
359
  if(debug){
361
 
    fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
 
360
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
361
            ip, port);
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(if_index, interface) == NULL){
 
370
  if(if_indextoname((unsigned int)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\n", ip);
 
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
/*     } */
398
406
  }
399
407
  
400
408
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
481
489
                                               &decrypted_buffer,
482
490
                                               CERT_ROOT);
483
491
    if (decrypted_buffer_size >= 0){
484
 
      while(written < decrypted_buffer_size){
 
492
      while(written < (size_t) decrypted_buffer_size){
485
493
        ret = (int)fwrite (decrypted_buffer + written, 1,
486
494
                           (size_t)decrypted_buffer_size - written,
487
495
                           stdout);
556
564
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
557
565
                " port %d\n", name, host_name, ip, port);
558
566
      }
559
 
      int ret = start_mandos_communication(ip, port,
560
 
                                           (unsigned int) interface);
 
567
      int ret = start_mandos_communication(ip, port, interface);
561
568
      if (ret == 0){
562
569
        exit(EXIT_SUCCESS);
563
570
      }
621
628
    int error;
622
629
    int ret;
623
630
    int returncode = EXIT_SUCCESS;
624
 
    const char *interface = "eth0";
 
631
    const char *interface = NULL;
 
632
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
633
    char *connect_to = NULL;
625
634
    
626
635
    while (true){
627
636
      static struct option long_options[] = {
628
637
        {"debug", no_argument, (int *)&debug, 1},
 
638
        {"connect", required_argument, 0, 'c'},
629
639
        {"interface", required_argument, 0, 'i'},
630
640
        {0, 0, 0, 0} };
631
641
      
643
653
      case 'i':
644
654
        interface = optarg;
645
655
        break;
 
656
      case 'c':
 
657
        connect_to = optarg;
 
658
        break;
646
659
      default:
647
660
        exit(EXIT_FAILURE);
648
661
      }
649
662
    }
650
663
    
 
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
    
651
696
    if (not debug){
652
697
      avahi_set_log_function(empty_log);
653
698
    }
685
730
    }
686
731
    
687
732
    /* Create the service browser */
688
 
    sb = avahi_s_service_browser_new(server,
689
 
                                     (AvahiIfIndex)
690
 
                                     if_nametoindex(interface),
 
733
    sb = avahi_s_service_browser_new(server, if_index,
691
734
                                     AVAHI_PROTO_INET6,
692
735
                                     "_mandos._tcp", NULL, 0,
693
736
                                     browse_callback, server);