/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-10-18 17:29:55 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091018172955-qqnsfad2lkpkt35f
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>.  Do
                               close(STDOUT_FILENO) after writing to
                               check its return code.

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
544
544
    struct sockaddr_in6 in6;
545
545
  } to;
546
546
  char *buffer = NULL;
547
 
  char *decrypted_buffer;
 
547
  char *decrypted_buffer = NULL;
548
548
  size_t buffer_length = 0;
549
549
  size_t buffer_capacity = 0;
550
550
  size_t written;
551
 
  int retval = 0;
 
551
  int retval = -1;
552
552
  gnutls_session_t session;
553
553
  int pf;                       /* Protocol family */
554
554
  
581
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
582
582
  if(tcp_sd < 0){
583
583
    perror("socket");
584
 
    retval = -1;
585
584
    goto mandos_end;
586
585
  }
587
586
  
588
587
  if(quit_now){
589
 
    retval = -1;
590
588
    goto mandos_end;
591
589
  }
592
590
  
600
598
  }
601
599
  if(ret < 0 ){
602
600
    perror("inet_pton");
603
 
    retval = -1;
604
601
    goto mandos_end;
605
602
  }
606
603
  if(ret == 0){
607
604
    fprintf(stderr, "Bad address: %s\n", ip);
608
 
    retval = -1;
609
605
    goto mandos_end;
610
606
  }
611
607
  if(af == AF_INET6){
619
615
      if(if_index == AVAHI_IF_UNSPEC){
620
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
621
617
                " without a network interface\n");
622
 
        retval = -1;
623
618
        goto mandos_end;
624
619
      }
625
620
      /* Set the network interface number as scope */
632
627
  }
633
628
  
634
629
  if(quit_now){
635
 
    retval = -1;
636
630
    goto mandos_end;
637
631
  }
638
632
  
669
663
  }
670
664
  
671
665
  if(quit_now){
672
 
    retval = -1;
673
666
    goto mandos_end;
674
667
  }
675
668
  
680
673
  }
681
674
  if(ret < 0){
682
675
    perror("connect");
683
 
    retval = -1;
684
676
    goto mandos_end;
685
677
  }
686
678
  
687
679
  if(quit_now){
688
 
    retval = -1;
689
680
    goto mandos_end;
690
681
  }
691
682
  
697
688
                                   out_size - written));
698
689
    if(ret == -1){
699
690
      perror("write");
700
 
      retval = -1;
701
691
      goto mandos_end;
702
692
    }
703
693
    written += (size_t)ret;
713
703
    }
714
704
  
715
705
    if(quit_now){
716
 
      retval = -1;
717
706
      goto mandos_end;
718
707
    }
719
708
  }
723
712
  }
724
713
  
725
714
  if(quit_now){
726
 
    retval = -1;
727
715
    goto mandos_end;
728
716
  }
729
717
  
730
718
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
731
719
  
732
720
  if(quit_now){
733
 
    retval = -1;
734
721
    goto mandos_end;
735
722
  }
736
723
  
737
724
  do {
738
725
    ret = gnutls_handshake(session);
739
726
    if(quit_now){
740
 
      retval = -1;
741
727
      goto mandos_end;
742
728
    }
743
729
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
747
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
748
734
      gnutls_perror(ret);
749
735
    }
750
 
    retval = -1;
751
736
    goto mandos_end;
752
737
  }
753
738
  
761
746
  while(true){
762
747
    
763
748
    if(quit_now){
764
 
      retval = -1;
765
749
      goto mandos_end;
766
750
    }
767
751
    
769
753
                                   buffer_capacity);
770
754
    if(buffer_capacity == 0){
771
755
      perror("incbuffer");
772
 
      retval = -1;
773
756
      goto mandos_end;
774
757
    }
775
758
    
776
759
    if(quit_now){
777
 
      retval = -1;
778
760
      goto mandos_end;
779
761
    }
780
762
    
793
775
          ret = gnutls_handshake(session);
794
776
          
795
777
          if(quit_now){
796
 
            retval = -1;
797
778
            goto mandos_end;
798
779
          }
799
780
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
800
781
        if(ret < 0){
801
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
802
783
          gnutls_perror(ret);
803
 
          retval = -1;
804
784
          goto mandos_end;
805
785
        }
806
786
        break;
807
787
      default:
808
788
        fprintf(stderr, "Unknown error while reading data from"
809
789
                " encrypted session with Mandos server\n");
810
 
        retval = -1;
811
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
812
791
        goto mandos_end;
813
792
      }
821
800
  }
822
801
  
823
802
  if(quit_now){
824
 
    retval = -1;
825
803
    goto mandos_end;
826
804
  }
827
805
  
828
806
  do {
829
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
830
808
    if(quit_now){
831
 
      retval = -1;
832
809
      goto mandos_end;
833
810
    }
834
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
843
820
      written = 0;
844
821
      while(written < (size_t) decrypted_buffer_size){
845
822
        if(quit_now){
846
 
          retval = -1;
847
823
          goto mandos_end;
848
824
        }
849
825
        
855
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
856
832
                    strerror(errno));
857
833
          }
858
 
          retval = -1;
859
 
          break;
 
834
          goto mandos_end;
860
835
        }
861
836
        written += (size_t)ret;
862
837
      }
863
 
      free(decrypted_buffer);
864
 
    } else {
865
 
      retval = -1;
 
838
      retval = 0;
866
839
    }
867
 
  } else {
868
 
    retval = -1;
869
840
  }
870
841
  
871
842
  /* Shutdown procedure */
872
843
  
873
844
 mandos_end:
 
845
  free(decrypted_buffer);
874
846
  free(buffer);
875
847
  if(tcp_sd >= 0){
876
848
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
1624
1596
  if(quit_now){
1625
1597
    sigemptyset(&old_sigterm_action.sa_mask);
1626
1598
    old_sigterm_action.sa_handler = SIG_DFL;
1627
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1599
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
1600
                                            &old_sigterm_action,
 
1601
                                            NULL));
1628
1602
    if(ret == -1){
1629
1603
      perror("sigaction");
1630
1604
    }
1631
 
    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());
1632
1613
  }
1633
1614
  
1634
1615
  return exitcode;