/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

  • Committer: Teddy Hogeborn
  • Date: 2009-09-19 17:41:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090919174118-274yt9wptmtx0ykn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
47
                                   srand(), strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
74
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid() */
 
75
                                   setgid(), pause() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
142
142
                      .dh_bits = 1024, .priority = "SECURE256"
143
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
144
 
 
145
sig_atomic_t quit_now = 0;
 
146
int signal_received = 0;
 
147
 
145
148
/*
146
149
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
150
 * bytes. "buffer_capacity" is how much is currently allocated,
476
479
  /* GnuTLS session creation */
477
480
  do {
478
481
    ret = gnutls_init(session, GNUTLS_SERVER);
 
482
    if(quit_now){
 
483
      return -1;
 
484
    }
479
485
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
480
486
  if(ret != GNUTLS_E_SUCCESS){
481
487
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
486
492
    const char *err;
487
493
    do {
488
494
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
495
      if(quit_now){
 
496
        gnutls_deinit(*session);
 
497
        return -1;
 
498
      }
489
499
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
490
500
    if(ret != GNUTLS_E_SUCCESS){
491
501
      fprintf(stderr, "Syntax error at: %s\n", err);
499
509
  do {
500
510
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
501
511
                                 mc.cred);
 
512
    if(quit_now){
 
513
      gnutls_deinit(*session);
 
514
      return -1;
 
515
    }
502
516
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
503
517
  if(ret != GNUTLS_E_SUCCESS){
504
518
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
519
533
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
520
534
                      __attribute__((unused)) const char *txt){}
521
535
 
522
 
sig_atomic_t quit_now = 0;
523
 
int signal_received = 0;
524
 
 
525
536
/* Called when a Mandos server is found */
526
537
static int start_mandos_communication(const char *ip, uint16_t port,
527
538
                                      AvahiIfIndex if_index,
533
544
    struct sockaddr_in6 in6;
534
545
  } to;
535
546
  char *buffer = NULL;
536
 
  char *decrypted_buffer;
 
547
  char *decrypted_buffer = NULL;
537
548
  size_t buffer_length = 0;
538
549
  size_t buffer_capacity = 0;
539
550
  size_t written;
540
 
  int retval = 0;
 
551
  int retval = -1;
541
552
  gnutls_session_t session;
542
553
  int pf;                       /* Protocol family */
543
554
  
570
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
571
582
  if(tcp_sd < 0){
572
583
    perror("socket");
573
 
    retval = -1;
574
584
    goto mandos_end;
575
585
  }
576
586
  
588
598
  }
589
599
  if(ret < 0 ){
590
600
    perror("inet_pton");
591
 
    retval = -1;
592
601
    goto mandos_end;
593
602
  }
594
603
  if(ret == 0){
595
604
    fprintf(stderr, "Bad address: %s\n", ip);
596
 
    retval = -1;
597
605
    goto mandos_end;
598
606
  }
599
607
  if(af == AF_INET6){
607
615
      if(if_index == AVAHI_IF_UNSPEC){
608
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
609
617
                " without a network interface\n");
610
 
        retval = -1;
611
618
        goto mandos_end;
612
619
      }
613
620
      /* Set the network interface number as scope */
666
673
  }
667
674
  if(ret < 0){
668
675
    perror("connect");
669
 
    retval = -1;
670
676
    goto mandos_end;
671
677
  }
672
678
  
682
688
                                   out_size - written));
683
689
    if(ret == -1){
684
690
      perror("write");
685
 
      retval = -1;
686
691
      goto mandos_end;
687
692
    }
688
693
    written += (size_t)ret;
728
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
729
734
      gnutls_perror(ret);
730
735
    }
731
 
    retval = -1;
732
736
    goto mandos_end;
733
737
  }
734
738
  
749
753
                                   buffer_capacity);
750
754
    if(buffer_capacity == 0){
751
755
      perror("incbuffer");
752
 
      retval = -1;
753
756
      goto mandos_end;
754
757
    }
755
758
    
778
781
        if(ret < 0){
779
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
780
783
          gnutls_perror(ret);
781
 
          retval = -1;
782
784
          goto mandos_end;
783
785
        }
784
786
        break;
785
787
      default:
786
788
        fprintf(stderr, "Unknown error while reading data from"
787
789
                " encrypted session with Mandos server\n");
788
 
        retval = -1;
789
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
790
791
        goto mandos_end;
791
792
      }
802
803
    goto mandos_end;
803
804
  }
804
805
  
805
 
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
806
 
  
807
 
  if(quit_now){
808
 
    goto mandos_end;
809
 
  }
 
806
  do {
 
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
808
    if(quit_now){
 
809
      goto mandos_end;
 
810
    }
 
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
810
812
  
811
813
  if(buffer_length > 0){
812
814
    ssize_t decrypted_buffer_size;
829
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
830
832
                    strerror(errno));
831
833
          }
832
 
          retval = -1;
833
 
          break;
 
834
          goto mandos_end;
834
835
        }
835
836
        written += (size_t)ret;
836
837
      }
837
 
      free(decrypted_buffer);
838
 
    } else {
839
 
      retval = -1;
 
838
      retval = 0;
840
839
    }
841
 
  } else {
842
 
    retval = -1;
843
840
  }
844
841
  
845
842
  /* Shutdown procedure */
846
843
  
847
844
 mandos_end:
 
845
  free(decrypted_buffer);
848
846
  free(buffer);
849
847
  if(tcp_sd >= 0){
850
848
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
1598
1596
  if(quit_now){
1599
1597
    sigemptyset(&old_sigterm_action.sa_mask);
1600
1598
    old_sigterm_action.sa_handler = SIG_DFL;
1601
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1599
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
1600
                                            &old_sigterm_action,
 
1601
                                            NULL));
1602
1602
    if(ret == -1){
1603
1603
      perror("sigaction");
1604
1604
    }
1605
 
    raise(signal_received);
 
1605
    do {
 
1606
      ret = raise(signal_received);
 
1607
    } while(ret != 0 and errno == EINTR);
 
1608
    if(ret != 0){
 
1609
      perror("raise");
 
1610
      abort();
 
1611
    }
 
1612
    TEMP_FAILURE_RETRY(pause());
1606
1613
  }
1607
1614
  
1608
1615
  return exitcode;