1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* Test suite for address binding in the server.
*
* Written by Russ Allbery <eagle@eyrie.org>
* Copyright 2018 Russ Allbery <eagle@eyrie.org>
* Copyright 2011-2012, 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* SPDX-License-Identifier: MIT
*/
#include <config.h>
#include <portable/system.h>
#include <client/remctl.h>
#include <tests/tap/basic.h>
#include <tests/tap/kerberos.h>
#include <tests/tap/process.h>
#include <tests/tap/remctl.h>
#include <util/network.h>
/*
* Test whether IPv6 is available.
*/
static bool
have_ipv6(void)
{
socket_type fd;
fd = network_bind_ipv6(SOCK_STREAM, "::1", 14373);
if (fd == INVALID_SOCKET) {
if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT
|| errno == EADDRNOTAVAIL)
return false;
return true;
}
close(fd);
return true;
}
/*
* Test whether we have any IPv6 addresses. This is used to decide
* whether to expect the server to bind IPv6 addresses by default.
*/
static bool
have_ipv6_addr(void)
{
struct addrinfo hints, *addrs, *addr;
int error;
char service[16];
bool result = false;
/* Do the query to find all the available addresses. */
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(service, sizeof(service), "%d", 14373);
error = getaddrinfo(NULL, service, &hints, &addrs);
if (error < 0)
return false;
for (addr = addrs; addr != NULL; addr = addr->ai_next) {
if (addr->ai_family == AF_INET6)
result = true;
}
freeaddrinfo(addrs);
return result;
}
/*
* Run the remote test command and confirm the output is correct. Takes the
* string to expect the REMOTE_ADDR environment variable to be set to.
*/
static void
test_command(struct remctl *r, const char *addr)
{
struct remctl_output *output;
char *seen;
const char *command[] = {"test", "env", "REMOTE_ADDR", NULL};
if (!remctl_command(r, command)) {
diag("remctl error %s", remctl_error(r));
ok_block(0, 3, "... command failed");
return;
}
do {
output = remctl_output(r);
switch (output->type) {
case REMCTL_OUT_OUTPUT:
is_int(strlen(addr) + 1, output->length, "... length ok");
seen = bstrndup(output->data, output->length - 1);
is_string(addr, seen, "... REMOTE_ADDR correct");
free(seen);
break;
case REMCTL_OUT_STATUS:
is_int(0, output->status, "... status ok");
break;
case REMCTL_OUT_ERROR:
diag("test env returned error: %.*s", (int) output->length,
output->data);
ok_block(0, 3, "... error received");
break;
case REMCTL_OUT_DONE:
diag("unexpected done token");
break;
}
} while (output->type == REMCTL_OUT_OUTPUT);
}
int
main(void)
{
struct kerberos_config *config;
struct remctl *r;
struct process *remctld;
#ifdef HAVE_INET6
bool ipv6 = have_ipv6();
#endif
/* Unless we have Kerberos available, we can't really do anything. */
config = kerberos_setup(TAP_KRB_NEEDS_KEYTAB);
/* Initialize our testing. */
plan(26);
/* Test connecting to IPv4 and IPv6 with default bind. */
remctld = remctld_start(config, "data/conf-simple", NULL);
r = remctl_new();
ok(remctl_open(r, "127.0.0.1", 14373, config->principal),
"Connect to 127.0.0.1");
test_command(r, "127.0.0.1");
remctl_close(r);
#ifdef HAVE_INET6
if (ipv6 && have_ipv6_addr()) {
r = remctl_new();
ok(remctl_open(r, "::1", 14373, config->principal), "Connect to ::1");
test_command(r, "::1");
remctl_close(r);
} else {
skip_block(4, "IPv6 not supported");
}
#else
skip_block(4, "IPv6 not supported");
#endif
process_stop(remctld);
/* Try binding to only IPv4. */
remctld =
remctld_start(config, "data/conf-simple", "-b", "127.0.0.1", NULL);
r = remctl_new();
ok(remctl_open(r, "127.0.0.1", 14373, config->principal),
"Connect to 127.0.0.1 when bound to that address");
test_command(r, "127.0.0.1");
remctl_close(r);
#ifdef HAVE_INET6
if (ipv6) {
r = remctl_new();
ok(!remctl_open(r, "::1", 14373, config->principal),
"Cannot connect to ::1 when only bound to 127.0.0.1");
remctl_close(r);
} else {
skip("IPv6 not supported");
}
#else
skip("IPv6 not supported");
#endif
process_stop(remctld);
/* Try binding to only IPv6. */
#ifdef HAVE_INET6
if (ipv6) {
remctld = remctld_start(config, "data/conf-simple", "-b", "::1", NULL);
r = remctl_new();
ok(!remctl_open(r, "127.0.0.1", 14373, config->principal),
"Cannot connect to 127.0.0.1 when only bound to ::1");
remctl_close(r);
r = remctl_new();
ok(remctl_open(r, "::1", 14373, config->principal),
"Connect to ::1 when bound only to it");
test_command(r, "::1");
remctl_close(r);
process_stop(remctld);
} else {
skip_block(5, "IPv6 not supported");
}
#else
skip_block(5, "IPv6 not supported");
#endif
/* Try binding explicitly to local IPv4 and IPv6 addresses. */
#ifdef HAVE_INET6
if (ipv6) {
remctld = remctld_start(config, "data/conf-simple", "-b", "127.0.0.1",
"-b", "::1", NULL);
r = remctl_new();
ok(remctl_open(r, "127.0.0.1", 14373, config->principal),
"Connect to 127.0.0.1 when bound to both local addresses");
test_command(r, "127.0.0.1");
remctl_close(r);
r = remctl_new();
ok(remctl_open(r, "::1", 14373, config->principal),
"Connect to ::1 when bound to both local addresses");
test_command(r, "::1");
remctl_close(r);
process_stop(remctld);
} else {
skip_block(8, "IPv6 not supported");
}
#else
skip_block(8, "IPv6 not supported");
#endif
return 0;
}
|