/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-16 23:28:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090916232839-3o7i8qmcdcz5j1ya
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
                 "$syslog", thanks to Petter Reinholdtsen
                 <pere@hungry.com> (Debian bug #546928).

* initramfs-tools-script: Removed erroneous comment.

* plugins.d/askpass-fifo.c: Removed TEMP_FAILURE_RETRY since it is
                            not needed.

* plugins.d/mandos-client.c (main): Bug fix: Initialize
                                    "old_sigterm_action".

* plugins.d/splashy.c (main): Bug fix: really check return value from
                              "sigaddset".  Fix some warnings on
                              64-bit systems.

* plugins.d/usplash.c (termination_handler, main): Save received
                                                   signal and
                                                   re-raise it on
                                                   exit.
  (usplash_write): Do not close FIFO, instead, take an additional file
                   descriptor pointer to it and open only when needed
                   (all callers changed).  Abort immediately on EINTR.
                   Bug fix:  Add NUL byte on single-word commands.
                   Ignore "interrupted_by_signal".
  (makeprompt, find_usplash): New; broken out from "main()".
  (find_usplash): Bug fix: close /proc/<pid>/cmdline FD on error.
  (main): Reorganized to jump to a new "failure" label on any error.
          Bug fix: check return values from sigaddset.
          New variable "usplash_accessed" to flag if usplash(8) needs
          to be killed and restarted.  Removed the "an_error_occured"
          variable.

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