/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-02-07 05:05:03 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090207050503-wi4qno0t5ua46vvx
* initramfs-tools-hook: Add extra newline to plugin-runner.conf to
                        guard against a possible comment character
                        without a newline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
43
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
44
44
                                   srand() */
45
 
#include <stdbool.h>            /* bool, false, true */
 
45
#include <stdbool.h>            /* bool, true */
46
46
#include <string.h>             /* memset(), strcmp(), strlen(),
47
47
                                   strerror(), asprintf(), strcpy() */
48
 
#include <sys/ioctl.h>          /* ioctl */
 
48
#include <sys/ioctl.h>          /* ioctl */
49
49
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
50
50
                                   sockaddr_in6, PF_INET6,
51
 
                                   SOCK_STREAM, uid_t, gid_t, open(),
52
 
                                   opendir(), DIR */
 
51
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
 
52
                                   uid_t, gid_t, open(), opendir(),
 
53
                                   DIR */
53
54
#include <sys/stat.h>           /* open() */
54
55
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
55
 
                                   inet_pton(), connect() */
 
56
                                   struct in6_addr, inet_pton(),
 
57
                                   connect() */
56
58
#include <fcntl.h>              /* open() */
57
59
#include <dirent.h>             /* opendir(), struct dirent, readdir()
58
60
                                 */
63
65
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
64
66
                                   SIOCSIFFLAGS, if_indextoname(),
65
67
                                   if_nametoindex(), IF_NAMESIZE */
66
 
#include <netinet/in.h>         /* IN6_IS_ADDR_LINKLOCAL,
67
 
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
68
 
                                */
 
68
#include <netinet/in.h>
69
69
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
70
70
                                   getuid(), getgid(), setuid(),
71
71
                                   setgid() */
72
72
#include <arpa/inet.h>          /* inet_pton(), htons */
73
 
#include <iso646.h>             /* not, or, and */
 
73
#include <iso646.h>             /* not, and, or */
74
74
#include <argp.h>               /* struct argp_option, error_t, struct
75
75
                                   argp_state, struct argp,
76
76
                                   argp_parse(), ARGP_KEY_ARG,
410
410
  gnutls_certificate_allocate_credentials(&mc->cred);
411
411
  if(ret != GNUTLS_E_SUCCESS){
412
412
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
413
 
                                                    from
414
 
                                                    -Wunreachable-code
415
 
                                                 */
 
413
                                                  * from
 
414
                                                  * -Wunreachable-code
 
415
                                                  */
416
416
            safer_gnutls_strerror(ret));
417
417
    gnutls_global_deinit();
418
418
    return -1;
509
509
/* Called when a Mandos server is found */
510
510
static int start_mandos_communication(const char *ip, uint16_t port,
511
511
                                      AvahiIfIndex if_index,
512
 
                                      mandos_context *mc, int af){
 
512
                                      mandos_context *mc){
513
513
  int ret, tcp_sd;
514
514
  ssize_t sret;
515
 
  union {
516
 
    struct sockaddr_in in;
517
 
    struct sockaddr_in6 in6;
518
 
  } to;
 
515
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
519
516
  char *buffer = NULL;
520
517
  char *decrypted_buffer;
521
518
  size_t buffer_length = 0;
523
520
  ssize_t decrypted_buffer_size;
524
521
  size_t written;
525
522
  int retval = 0;
 
523
  char interface[IF_NAMESIZE];
526
524
  gnutls_session_t session;
527
 
  int pf;                       /* Protocol family */
528
 
  
529
 
  switch(af){
530
 
  case AF_INET6:
531
 
    pf = PF_INET6;
532
 
    break;
533
 
  case AF_INET:
534
 
    pf = PF_INET;
535
 
    break;
536
 
  default:
537
 
    fprintf(stderr, "Bad address family: %d\n", af);
538
 
    return -1;
539
 
  }
540
525
  
541
526
  ret = init_gnutls_session(mc, &session);
542
527
  if(ret != 0){
544
529
  }
545
530
  
546
531
  if(debug){
547
 
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
532
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
548
533
            "\n", ip, port);
549
534
  }
550
535
  
551
 
  tcp_sd = socket(pf, SOCK_STREAM, 0);
 
536
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
552
537
  if(tcp_sd < 0){
553
538
    perror("socket");
554
539
    return -1;
555
540
  }
556
541
  
 
542
  if(debug){
 
543
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
544
      perror("if_indextoname");
 
545
      return -1;
 
546
    }
 
547
    fprintf(stderr, "Binding to interface %s\n", interface);
 
548
  }
 
549
  
557
550
  memset(&to, 0, sizeof(to));
558
 
  if(af == AF_INET6){
559
 
    to.in6.sin6_family = (uint16_t)af;
560
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
561
 
  } else {                      /* IPv4 */
562
 
    to.in.sin_family = (sa_family_t)af;
563
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
564
 
  }
 
551
  to.in6.sin6_family = AF_INET6;
 
552
  /* It would be nice to have a way to detect if we were passed an
 
553
     IPv4 address here.   Now we assume an IPv6 address. */
 
554
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
565
555
  if(ret < 0 ){
566
556
    perror("inet_pton");
567
557
    return -1;
570
560
    fprintf(stderr, "Bad address: %s\n", ip);
571
561
    return -1;
572
562
  }
573
 
  if(af == AF_INET6){
574
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
575
 
                                       -Wconversion and
576
 
                                       -Wunreachable-code */
577
 
    
578
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
579
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
580
 
                              -Wunreachable-code*/
581
 
      if(if_index == AVAHI_IF_UNSPEC){
582
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
583
 
                " without a network interface\n");
584
 
        return -1;
585
 
      }
586
 
      /* Set the network interface number as scope */
587
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
588
 
    }
589
 
  } else {
590
 
    to.in.sin_port = htons(port); /* Spurious warnings from
 
563
  to.in6.sin6_port = htons(port); /* Spurious warnings from
591
564
                                     -Wconversion and
592
565
                                     -Wunreachable-code */
593
 
  }
 
566
  
 
567
  to.in6.sin6_scope_id = (uint32_t)if_index;
594
568
  
595
569
  if(debug){
596
 
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
597
 
      char interface[IF_NAMESIZE];
598
 
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
599
 
        perror("if_indextoname");
600
 
      } else {
601
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
602
 
                ip, interface, port);
603
 
      }
604
 
    } else {
605
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
606
 
              port);
607
 
    }
608
 
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
609
 
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
610
 
    const char *pcret;
611
 
    if(af == AF_INET6){
612
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
613
 
                        sizeof(addrstr));
614
 
    } else {
615
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
616
 
                        sizeof(addrstr));
617
 
    }
618
 
    if(pcret == NULL){
 
570
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
571
            port);
 
572
    char addrstr[INET6_ADDRSTRLEN] = "";
 
573
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
574
                 sizeof(addrstr)) == NULL){
619
575
      perror("inet_ntop");
620
576
    } else {
621
577
      if(strcmp(addrstr, ip) != 0){
624
580
    }
625
581
  }
626
582
  
627
 
  if(af == AF_INET6){
628
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
629
 
  } else {
630
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
631
 
  }
 
583
  ret = connect(tcp_sd, &to.in, sizeof(to));
632
584
  if(ret < 0){
633
585
    perror("connect");
634
586
    return -1;
680
632
  /* Read OpenPGP packet that contains the wanted password */
681
633
  
682
634
  if(debug){
683
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
 
635
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
684
636
            ip);
685
637
  }
686
638
  
774
726
 
775
727
static void resolve_callback(AvahiSServiceResolver *r,
776
728
                             AvahiIfIndex interface,
777
 
                             AvahiProtocol proto,
 
729
                             AVAHI_GCC_UNUSED AvahiProtocol protocol,
778
730
                             AvahiResolverEvent event,
779
731
                             const char *name,
780
732
                             const char *type,
809
761
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
810
762
                ip, (intmax_t)interface, port);
811
763
      }
812
 
      int ret = start_mandos_communication(ip, port, interface, mc,
813
 
                                           avahi_proto_to_af(proto));
 
764
      int ret = start_mandos_communication(ip, port, interface, mc);
814
765
      if(ret == 0){
815
766
        avahi_simple_poll_quit(mc->simple_poll);
816
767
      }
906
857
        .group = 1 },
907
858
      { .name = "interface", .key = 'i',
908
859
        .arg = "NAME",
909
 
        .doc = "Network interface that will be used to search for"
910
 
        " Mandos servers",
 
860
        .doc = "Interface that will be used to search for Mandos"
 
861
        " servers",
911
862
        .group = 1 },
912
863
      { .name = "seckey", .key = 's',
913
864
        .arg = "FILE",
993
944
  }
994
945
  
995
946
  /* If the interface is down, bring it up */
996
 
  if(interface[0] != '\0'){
 
947
  {
997
948
#ifdef __linux__
998
949
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
999
950
       messages to mess up the prompt */
1000
951
    ret = klogctl(8, NULL, 5);
1001
 
    bool restore_loglevel = true;
1002
952
    if(ret == -1){
1003
 
      restore_loglevel = false;
1004
953
      perror("klogctl");
1005
954
    }
1006
955
#endif
1010
959
      perror("socket");
1011
960
      exitcode = EXIT_FAILURE;
1012
961
#ifdef __linux__
1013
 
      if(restore_loglevel){
1014
 
        ret = klogctl(7, NULL, 0);
1015
 
        if(ret == -1){
1016
 
          perror("klogctl");
1017
 
        }
 
962
      ret = klogctl(7, NULL, 0);
 
963
      if(ret == -1){
 
964
        perror("klogctl");
1018
965
      }
1019
966
#endif
1020
967
      goto end;
1024
971
    if(ret == -1){
1025
972
      perror("ioctl SIOCGIFFLAGS");
1026
973
#ifdef __linux__
1027
 
      if(restore_loglevel){
1028
 
        ret = klogctl(7, NULL, 0);
1029
 
        if(ret == -1){
1030
 
          perror("klogctl");
1031
 
        }
 
974
      ret = klogctl(7, NULL, 0);
 
975
      if(ret == -1){
 
976
        perror("klogctl");
1032
977
      }
1033
978
#endif
1034
979
      exitcode = EXIT_FAILURE;
1041
986
        perror("ioctl SIOCSIFFLAGS");
1042
987
        exitcode = EXIT_FAILURE;
1043
988
#ifdef __linux__
1044
 
        if(restore_loglevel){
1045
 
          ret = klogctl(7, NULL, 0);
1046
 
          if(ret == -1){
1047
 
            perror("klogctl");
1048
 
          }
 
989
        ret = klogctl(7, NULL, 0);
 
990
        if(ret == -1){
 
991
          perror("klogctl");
1049
992
        }
1050
993
#endif
1051
994
        goto end;
1070
1013
      perror("close");
1071
1014
    }
1072
1015
#ifdef __linux__
1073
 
    if(restore_loglevel){
1074
 
      /* Restores kernel loglevel to default */
1075
 
      ret = klogctl(7, NULL, 0);
1076
 
      if(ret == -1){
1077
 
        perror("klogctl");
1078
 
      }
 
1016
    /* Restores kernel loglevel to default */
 
1017
    ret = klogctl(7, NULL, 0);
 
1018
    if(ret == -1){
 
1019
      perror("klogctl");
1079
1020
    }
1080
1021
#endif
1081
1022
  }
1083
1024
  uid = getuid();
1084
1025
  gid = getgid();
1085
1026
  
1086
 
  errno = 0;
1087
1027
  setgid(gid);
1088
1028
  if(ret == -1){
1089
1029
    perror("setgid");
1117
1057
    gpgme_initialized = true;
1118
1058
  }
1119
1059
  
1120
 
  if(interface[0] != '\0'){
1121
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1122
 
    if(if_index == 0){
1123
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1124
 
      exitcode = EXIT_FAILURE;
1125
 
      goto end;
1126
 
    }
 
1060
  if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1061
  if(if_index == 0){
 
1062
    fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1063
    exitcode = EXIT_FAILURE;
 
1064
    goto end;
1127
1065
  }
1128
1066
  
1129
1067
  if(connect_to != NULL){
1146
1084
    port = (uint16_t)tmpmax;
1147
1085
    *address = '\0';
1148
1086
    address = connect_to;
1149
 
    /* Colon in address indicates IPv6 */
1150
 
    int af;
1151
 
    if(strchr(address, ':') != NULL){
1152
 
      af = AF_INET6;
1153
 
    } else {
1154
 
      af = AF_INET;
1155
 
    }
1156
 
    ret = start_mandos_communication(address, port, if_index, &mc,
1157
 
                                     af);
 
1087
    ret = start_mandos_communication(address, port, if_index, &mc);
1158
1088
    if(ret < 0){
1159
1089
      exitcode = EXIT_FAILURE;
1160
1090
    } else {