/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

plugbasedclient:
        Added support to disable plugins
        Added support to change plugin directory

mandosclient.c
        Fixed some funtion calls that lacked error handling
        small bugfix

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
25
25
 * along with this program.  If not, see
26
26
 * <http://www.gnu.org/licenses/>.
27
27
 * 
28
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
28
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
 
29
 * <https://www.fukt.bsnet.se/~teddy/>.
29
30
 */
30
31
 
31
 
/* Needed by GPGME, specifically gpgme_data_seek() */
 
32
#define _FORTIFY_SOURCE 2
 
33
 
32
34
#define _LARGEFILE_SOURCE
33
35
#define _FILE_OFFSET_BITS 64
34
36
 
348
350
               __attribute__((unused)) const char *txt){}
349
351
 
350
352
int start_mandos_communication(const char *ip, uint16_t port,
351
 
                               AvahiIfIndex if_index){
 
353
                               unsigned int if_index){
352
354
  int ret, tcp_sd;
353
355
  struct sockaddr_in6 to;
354
356
  encrypted_session es;
362
364
  char interface[IF_NAMESIZE];
363
365
  
364
366
  if(debug){
365
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
366
 
            ip, port);
 
367
    fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
367
368
  }
368
369
  
369
370
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
372
373
    return -1;
373
374
  }
374
375
  
375
 
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
376
  if(if_indextoname(if_index, interface) == NULL){
376
377
    if(debug){
377
378
      perror("if_indextoname");
378
379
    }
399
400
  to.sin6_scope_id = (uint32_t)if_index;
400
401
  
401
402
  if(debug){
402
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
403
 
/*     char addrstr[INET6_ADDRSTRLEN]; */
404
 
/*     if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
405
 
/*               sizeof(addrstr)) == NULL){ */
406
 
/*       perror("inet_ntop"); */
407
 
/*     } else { */
408
 
/*       fprintf(stderr, "Really connecting to: %s, port %d\n", */
409
 
/*            addrstr, ntohs(to.sin6_port)); */
410
 
/*     } */
 
403
    fprintf(stderr, "Connection to: %s\n", ip);
411
404
  }
412
405
  
413
406
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
494
487
                                               &decrypted_buffer,
495
488
                                               certdir);
496
489
    if (decrypted_buffer_size >= 0){
497
 
      while(written < (size_t) decrypted_buffer_size){
 
490
      while(written < decrypted_buffer_size){
498
491
        ret = (int)fwrite (decrypted_buffer + written, 1,
499
492
                           (size_t)decrypted_buffer_size - written,
500
493
                           stdout);
569
562
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
570
563
                " port %d\n", name, host_name, ip, port);
571
564
      }
572
 
      int ret = start_mandos_communication(ip, port, interface);
 
565
      int ret = start_mandos_communication(ip, port,
 
566
                                           (unsigned int) interface);
573
567
      if (ret == 0){
574
568
        exit(EXIT_SUCCESS);
575
569
      }
650
644
    int error;
651
645
    int ret;
652
646
    int returncode = EXIT_SUCCESS;
653
 
    const char *interface = NULL;
654
 
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
655
 
    char *connect_to = NULL;
 
647
    const char *interface = "eth0";
656
648
    
657
649
    while (true){
658
650
      static struct option long_options[] = {
659
651
        {"debug", no_argument, (int *)&debug, 1},
660
 
        {"connect", required_argument, 0, 'C'},
661
652
        {"interface", required_argument, 0, 'i'},
662
653
        {"certdir", required_argument, 0, 'd'},
663
654
        {"certkey", required_argument, 0, 'c'},
678
669
      case 'i':
679
670
        interface = optarg;
680
671
        break;
681
 
      case 'C':
682
 
        connect_to = optarg;
683
 
        break;
684
672
      case 'd':
685
673
        certdir = optarg;
686
674
        break;
700
688
      goto exit;
701
689
    }
702
690
    
703
 
    if(interface != NULL){
704
 
      if_index = (AvahiIfIndex) if_nametoindex(interface);
705
 
      if(if_index == 0){
706
 
        fprintf(stderr, "No such interface: \"%s\"\n", interface);
707
 
        exit(EXIT_FAILURE);
708
 
      }
709
 
    }
710
 
    
711
 
    if(connect_to != NULL){
712
 
      /* Connect directly, do not use Zeroconf */
713
 
      /* (Mainly meant for debugging) */
714
 
      char *address = strrchr(connect_to, ':');
715
 
      if(address == NULL){
716
 
        fprintf(stderr, "No colon in address\n");
717
 
        exit(EXIT_FAILURE);
718
 
      }
719
 
      errno = 0;
720
 
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
721
 
      if(errno){
722
 
        perror("Bad port number");
723
 
        exit(EXIT_FAILURE);
724
 
      }
725
 
      *address = '\0';
726
 
      address = connect_to;
727
 
      ret = start_mandos_communication(address, port, if_index);
728
 
      if(ret < 0){
729
 
        exit(EXIT_FAILURE);
730
 
      } else {
731
 
        exit(EXIT_SUCCESS);
732
 
      }
733
 
    }
734
 
    
735
691
    certkey = combinepath(certdir, certkey);
736
692
    if (certkey == NULL){
737
693
      goto exit;
738
694
    }
739
 
    
 
695
        
740
696
    if (not debug){
741
697
      avahi_set_log_function(empty_log);
742
698
    }
774
730
    }
775
731
    
776
732
    /* Create the service browser */
777
 
    sb = avahi_s_service_browser_new(server, if_index,
 
733
    sb = avahi_s_service_browser_new(server,
 
734
                                     (AvahiIfIndex)
 
735
                                     if_nametoindex(interface),
778
736
                                     AVAHI_PROTO_INET6,
779
737
                                     "_mandos._tcp", NULL, 0,
780
738
                                     browse_callback, server);