42
42
#include <stddef.h> /* NULL, size_t, ssize_t */
43
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
45
#include <stdbool.h> /* bool, true */
45
#include <stdbool.h> /* bool, false, 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, INET6_ADDRSTRLEN,
52
uid_t, gid_t, open(), opendir(),
51
SOCK_STREAM, uid_t, gid_t, open(),
54
53
#include <sys/stat.h> /* open() */
55
54
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
56
struct in6_addr, inet_pton(),
55
inet_pton(), connect() */
58
56
#include <fcntl.h> /* open() */
59
57
#include <dirent.h> /* opendir(), struct dirent, readdir()
65
63
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
64
SIOCSIFFLAGS, if_indextoname(),
67
65
if_nametoindex(), IF_NAMESIZE */
68
#include <netinet/in.h>
66
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
67
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
69
69
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
70
70
getuid(), getgid(), setuid(),
72
72
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, and, or */
73
#include <iso646.h> /* not, or, and */
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
416
416
safer_gnutls_strerror(ret));
417
417
gnutls_global_deinit();
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){
515
union { struct sockaddr in; struct sockaddr_in6 in6; } to;
516
struct sockaddr_in in;
517
struct sockaddr_in6 in6;
516
519
char *buffer = NULL;
517
520
char *decrypted_buffer;
518
521
size_t buffer_length = 0;
520
523
ssize_t decrypted_buffer_size;
523
char interface[IF_NAMESIZE];
524
526
gnutls_session_t session;
527
int pf; /* Protocol family */
537
fprintf(stderr, "Bad address family: %d\n", af);
526
541
ret = init_gnutls_session(mc, &session);
532
fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
547
fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
536
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
551
tcp_sd = socket(pf, SOCK_STREAM, 0);
538
553
perror("socket");
543
if(if_indextoname((unsigned int)if_index, interface) == NULL){
544
perror("if_indextoname");
547
fprintf(stderr, "Binding to interface %s\n", interface);
550
557
memset(&to, 0, sizeof(to));
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);
559
to.in6.sin6_family = (uint16_t)af;
560
ret = inet_pton(af, ip, &to.in6.sin6_addr);
562
to.in.sin_family = (sa_family_t)af;
563
ret = inet_pton(af, ip, &to.in.sin_addr);
556
566
perror("inet_pton");
560
570
fprintf(stderr, "Bad address: %s\n", ip);
563
to.in6.sin6_port = htons(port); /* Spurious warnings from
574
to.in6.sin6_port = htons(port); /* Spurious warnings from
576
-Wunreachable-code */
578
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
579
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
581
if(if_index == AVAHI_IF_UNSPEC){
582
fprintf(stderr, "An IPv6 link-local address is incomplete"
583
" without a network interface\n");
586
/* Set the network interface number as scope */
587
to.in6.sin6_scope_id = (uint32_t)if_index;
590
to.in.sin_port = htons(port); /* Spurious warnings from
565
592
-Wunreachable-code */
567
to.in6.sin6_scope_id = (uint32_t)if_index;
570
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
572
char addrstr[INET6_ADDRSTRLEN] = "";
573
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
574
sizeof(addrstr)) == NULL){
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");
601
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
602
ip, interface, port);
605
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
608
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
609
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
612
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
615
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
575
619
perror("inet_ntop");
577
621
if(strcmp(addrstr, ip) != 0){
583
ret = connect(tcp_sd, &to.in, sizeof(to));
628
ret = connect(tcp_sd, &to.in6, sizeof(to));
630
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
585
633
perror("connect");
632
680
/* Read OpenPGP packet that contains the wanted password */
635
fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
683
fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
761
809
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
762
810
ip, (intmax_t)interface, port);
764
int ret = start_mandos_communication(ip, port, interface, mc);
812
int ret = start_mandos_communication(ip, port, interface, mc,
813
avahi_proto_to_af(proto));
766
815
avahi_simple_poll_quit(mc->simple_poll);
858
907
{ .name = "interface", .key = 'i',
860
.doc = "Interface that will be used to search for Mandos"
909
.doc = "Network interface that will be used to search for"
863
912
{ .name = "seckey", .key = 's',
946
995
/* If the interface is down, bring it up */
996
if(interface[0] != '\0'){
949
998
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
950
999
messages to mess up the prompt */
951
1000
ret = klogctl(8, NULL, 5);
1001
bool restore_loglevel = true;
1003
restore_loglevel = false;
953
1004
perror("klogctl");
972
1025
perror("ioctl SIOCGIFFLAGS");
973
1026
#ifdef __linux__
974
ret = klogctl(7, NULL, 0);
1027
if(restore_loglevel){
1028
ret = klogctl(7, NULL, 0);
979
1034
exitcode = EXIT_FAILURE;
986
1041
perror("ioctl SIOCSIFFLAGS");
987
1042
exitcode = EXIT_FAILURE;
988
1043
#ifdef __linux__
989
ret = klogctl(7, NULL, 0);
1044
if(restore_loglevel){
1045
ret = klogctl(7, NULL, 0);
1013
1070
perror("close");
1015
1072
#ifdef __linux__
1016
/* Restores kernel loglevel to default */
1017
ret = klogctl(7, NULL, 0);
1073
if(restore_loglevel){
1074
/* Restores kernel loglevel to default */
1075
ret = klogctl(7, NULL, 0);
1057
1117
gpgme_initialized = true;
1060
if_index = (AvahiIfIndex) if_nametoindex(interface);
1062
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1063
exitcode = EXIT_FAILURE;
1120
if(interface[0] != '\0'){
1121
if_index = (AvahiIfIndex) if_nametoindex(interface);
1123
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1124
exitcode = EXIT_FAILURE;
1067
1129
if(connect_to != NULL){
1084
1146
port = (uint16_t)tmpmax;
1085
1147
*address = '\0';
1086
1148
address = connect_to;
1087
ret = start_mandos_communication(address, port, if_index, &mc);
1149
/* Colon in address indicates IPv6 */
1151
if(strchr(address, ':') != NULL){
1156
ret = start_mandos_communication(address, port, if_index, &mc,
1089
1159
exitcode = EXIT_FAILURE;