/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-21 15:34:44 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080721153444-lugbjkj1oq65ugq3
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
                     $(LANGUAGE).
  (WARN, DEBUG, COVERAGE, LANGUAGE): New.
  (LDFLAGS): New; use $(COVERAGE)

* plugbasedclient.c: Added copyright header.
  (process.buffer_size, process.buffer_length): Changed to "size_t".
  (main): Cast arguments to malloc and realloc.  Detect read errors
          from processes.

* mandosclient.c: Added copyright header.
  (interface): Moved to inside "main".
  (gpg_packet_decrypt): Renamed to "pgp_packet_decrypt"; all callers
                        changed.  Changed "new_packet_capacity" and
                        "new_packet_length" to be ssize_t.  Cast
                        arguments to realloc.
  (debuggnutls): Attribute "level" argument as unused.
  (empty_log): Attribute "level" and "txt" arguments as unused.
  (start_mandos_communication): New argument "if_index".  Bug fix:
                                check ret, no tcp_sd, for errors from
                                setsockopt.  Use "if_index" directly
                                instead of looking up the index.  Loop
                                around fwrite until all data is written.
  (resolve_callback): Attribute "txt", and "flags" as usused.  Added
                      default case to switch.  Also show server host
                      name.  Call start_mandos_communication with
                      "interface".
  (browse_callback): Added default case to switch.
  (main): Variable "interface" moved here.  Cast "srand" argument.
          Bug fix: Call avahi_s_service_browser_new with index of
          "interface", not "eth0".

* passprompt.c: Added copyright header.
  (termination_handler): Attribute "signum" argument as unused.

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
 
47
48
#include <avahi-common/error.h>
48
49
 
49
50
//mandos client part
50
 
#include <sys/types.h>          /* socket(), inet_pton() */
51
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
51
#include <sys/types.h>          /* socket(), setsockopt(),
 
52
                                   inet_pton() */
 
53
#include <sys/socket.h>         /* socket(), setsockopt(),
 
54
                                   struct sockaddr_in6,
52
55
                                   struct in6_addr, inet_pton() */
53
56
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
54
57
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
192
195
  gpgme_data_release(dh_crypto);
193
196
  
194
197
  /* Seek back to the beginning of the GPGME plaintext data buffer */
195
 
  gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET);
 
198
  gpgme_data_seek(dh_plain, 0, SEEK_SET);
196
199
 
197
200
  *new_packet = 0;
198
201
  while(true){
342
345
void empty_log(__attribute__((unused)) AvahiLogLevel level,
343
346
               __attribute__((unused)) const char *txt){}
344
347
 
345
 
int start_mandos_communication(const char *ip, uint16_t port,
346
 
                               AvahiIfIndex if_index){
 
348
int start_mandos_communication(char *ip, uint16_t port,
 
349
                               unsigned int if_index){
347
350
  int ret, tcp_sd;
348
351
  struct sockaddr_in6 to;
349
352
  encrypted_session es;
352
355
  size_t buffer_length = 0;
353
356
  size_t buffer_capacity = 0;
354
357
  ssize_t decrypted_buffer_size;
355
 
  size_t written = 0;
356
358
  int retval = 0;
357
359
  char interface[IF_NAMESIZE];
358
360
  
359
361
  if(debug){
360
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
361
 
            ip, port);
 
362
    fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
362
363
  }
363
364
  
364
365
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
367
368
    return -1;
368
369
  }
369
370
  
370
 
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
371
  if(if_indextoname(if_index, interface) == NULL){
371
372
    if(debug){
372
373
      perror("if_indextoname");
373
374
    }
378
379
    fprintf(stderr, "Binding to interface %s\n", interface);
379
380
  }
380
381
  
381
 
  memset(&to,0,sizeof(to));     /* Spurious warning */
 
382
  ret = setsockopt(tcp_sd, SOL_SOCKET, SO_BINDTODEVICE, interface, 5);
 
383
  if(ret < 0) {
 
384
    perror("setsockopt bindtodevice");
 
385
    return -1;
 
386
  }
 
387
  
 
388
  memset(&to,0,sizeof(to));
382
389
  to.sin6_family = AF_INET6;
383
390
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
384
391
  if (ret < 0 ){
389
396
    fprintf(stderr, "Bad address: %s\n", ip);
390
397
    return -1;
391
398
  }
392
 
  to.sin6_port = htons(port);   /* Spurious warning */
 
399
  /* Spurious warnings for the next line, see for instance
 
400
     <http://bugs.debian.org/488884> */
 
401
  to.sin6_port = htons(port);
393
402
  
394
403
  to.sin6_scope_id = (uint32_t)if_index;
395
404
  
396
405
  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
 
/*     } */
 
406
    fprintf(stderr, "Connection to: %s\n", ip);
406
407
  }
407
408
  
408
409
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
427
428
  ret = gnutls_handshake (es.session);
428
429
  
429
430
  if (ret != GNUTLS_E_SUCCESS){
430
 
    if(debug){
431
 
      fprintf(stderr, "\n*** Handshake failed ***\n");
432
 
      gnutls_perror (ret);
433
 
    }
 
431
    fprintf(stderr, "\n*** Handshake failed ***\n");
 
432
    gnutls_perror (ret);
434
433
    retval = -1;
435
434
    goto exit;
436
435
  }
489
488
                                               &decrypted_buffer,
490
489
                                               CERT_ROOT);
491
490
    if (decrypted_buffer_size >= 0){
492
 
      while(written < (size_t) decrypted_buffer_size){
493
 
        ret = (int)fwrite (decrypted_buffer + written, 1,
494
 
                           (size_t)decrypted_buffer_size - written,
495
 
                           stdout);
 
491
      while(decrypted_buffer_size > 0){
 
492
        ret = fwrite (decrypted_buffer, 1, (size_t)decrypted_buffer_size,
 
493
                      stdout);
496
494
        if(ret == 0 and ferror(stdout)){
497
495
          if(debug){
498
496
            fprintf(stderr, "Error writing encrypted data: %s\n",
501
499
          retval = -1;
502
500
          break;
503
501
        }
504
 
        written += (size_t)ret;
 
502
        decrypted_buffer += ret;
 
503
        decrypted_buffer_size -= ret;
505
504
      }
506
505
      free(decrypted_buffer);
507
506
    } else {
530
529
 
531
530
static void resolve_callback(
532
531
    AvahiSServiceResolver *r,
533
 
    AvahiIfIndex interface,
 
532
    AVAHI_GCC_UNUSED AvahiIfIndex interface,
534
533
    AVAHI_GCC_UNUSED AvahiProtocol protocol,
535
534
    AvahiResolverEvent event,
536
535
    const char *name,
543
542
    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
544
543
    AVAHI_GCC_UNUSED void* userdata) {
545
544
    
546
 
  assert(r);                    /* Spurious warning */
547
 
  
 
545
  assert(r);
 
546
 
548
547
  /* Called whenever a service has been resolved successfully or
549
548
     timed out */
550
 
  
 
549
 
551
550
  switch (event) {
552
551
  default:
553
552
  case AVAHI_RESOLVER_FAILURE:
555
554
            " type '%s' in domain '%s': %s\n", name, type, domain,
556
555
            avahi_strerror(avahi_server_errno(server)));
557
556
    break;
558
 
    
 
557
      
559
558
  case AVAHI_RESOLVER_FOUND:
560
559
    {
561
560
      char ip[AVAHI_ADDRESS_STR_MAX];
562
561
      avahi_address_snprint(ip, sizeof(ip), address);
563
562
      if(debug){
564
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
565
 
                " port %d\n", name, host_name, ip, port);
 
563
        fprintf(stderr, "Mandos server found on %s (%s) on port %d\n",
 
564
                host_name, ip, port);
566
565
      }
567
 
      int ret = start_mandos_communication(ip, port, interface);
 
566
      int ret = start_mandos_communication(ip, port,
 
567
                                           (unsigned int)
 
568
                                           interface);
568
569
      if (ret == 0){
569
570
        exit(EXIT_SUCCESS);
 
571
      } else {
 
572
        exit(EXIT_FAILURE);
570
573
      }
571
574
    }
572
575
  }
585
588
    void* userdata) {
586
589
    
587
590
    AvahiServer *s = userdata;
588
 
    assert(b);                  /* Spurious warning */
589
 
    
 
591
    assert(b);
 
592
 
590
593
    /* Called whenever a new services becomes available on the LAN or
591
594
       is removed from the LAN */
592
 
    
 
595
 
593
596
    switch (event) {
594
597
    default:
595
598
    case AVAHI_BROWSER_FAILURE:
628
631
    int error;
629
632
    int ret;
630
633
    int returncode = EXIT_SUCCESS;
631
 
    const char *interface = NULL;
632
 
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
633
 
    char *connect_to = NULL;
 
634
    const char *interface = "eth0";
634
635
    
635
636
    while (true){
636
637
      static struct option long_options[] = {
637
638
        {"debug", no_argument, (int *)&debug, 1},
638
 
        {"connect", required_argument, 0, 'c'},
639
639
        {"interface", required_argument, 0, 'i'},
640
640
        {0, 0, 0, 0} };
641
 
      
 
641
 
642
642
      int option_index = 0;
643
643
      ret = getopt_long (argc, argv, "i:", long_options,
644
644
                         &option_index);
645
 
      
 
645
 
646
646
      if (ret == -1){
647
647
        break;
648
648
      }
653
653
      case 'i':
654
654
        interface = optarg;
655
655
        break;
656
 
      case 'c':
657
 
        connect_to = optarg;
658
 
        break;
659
656
      default:
660
657
        exit(EXIT_FAILURE);
661
658
      }
662
659
    }
663
660
    
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
661
    if (not debug){
697
662
      avahi_set_log_function(empty_log);
698
663
    }
730
695
    }
731
696
    
732
697
    /* Create the service browser */
733
 
    sb = avahi_s_service_browser_new(server, if_index,
 
698
    sb = avahi_s_service_browser_new(server,
 
699
                                     (AvahiIfIndex)
 
700
                                     if_nametoindex(interface),
734
701
                                     AVAHI_PROTO_INET6,
735
702
                                     "_mandos._tcp", NULL, 0,
736
703
                                     browse_callback, server);