2017-01-10 05:36:19 +00:00
|
|
|
/* Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include "subcommands.h"
|
2017-05-15 21:24:48 +00:00
|
|
|
#include "encoding.h"
|
2016-07-20 19:24:27 +00:00
|
|
|
#include "ipc.h"
|
2015-06-05 13:58:00 +00:00
|
|
|
#include "../uapi.h"
|
|
|
|
|
|
|
|
int showconf_main(int argc, char *argv[])
|
|
|
|
{
|
2017-04-15 23:20:43 +00:00
|
|
|
char base64[WG_KEY_LEN_BASE64];
|
2015-06-05 13:58:00 +00:00
|
|
|
char ip[INET6_ADDRSTRLEN];
|
|
|
|
struct wgdevice *device = NULL;
|
|
|
|
struct wgpeer *peer;
|
|
|
|
struct wgipmask *ipmask;
|
|
|
|
size_t i, j;
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s %s <interface>\n", PROG_NAME, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:24:27 +00:00
|
|
|
if (!ipc_has_device(argv[1])) {
|
2015-06-05 13:58:00 +00:00
|
|
|
fprintf(stderr, "`%s` is not a valid WireGuard interface\n", argv[1]);
|
|
|
|
fprintf(stderr, "Usage: %s %s <interface>\n", PROG_NAME, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:24:27 +00:00
|
|
|
if (ipc_get_device(&device, argv[1])) {
|
2015-06-05 13:58:00 +00:00
|
|
|
perror("Unable to get device");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[Interface]\n");
|
|
|
|
if (device->port)
|
2017-01-24 16:43:35 +00:00
|
|
|
printf("ListenPort = %u\n", device->port);
|
|
|
|
if (device->fwmark)
|
|
|
|
printf("FwMark = 0x%x\n", device->fwmark);
|
2017-09-22 02:04:00 +00:00
|
|
|
if (!key_is_zero(device->private_key)) {
|
2017-04-15 23:20:43 +00:00
|
|
|
key_to_base64(base64, device->private_key);
|
|
|
|
printf("PrivateKey = %s\n", base64);
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
for_each_wgpeer(device, peer, i) {
|
2017-04-15 23:20:43 +00:00
|
|
|
key_to_base64(base64, peer->public_key);
|
|
|
|
printf("[Peer]\nPublicKey = %s\n", base64);
|
2017-09-22 02:04:00 +00:00
|
|
|
if (!key_is_zero(peer->preshared_key)) {
|
2017-04-27 09:10:50 +00:00
|
|
|
key_to_base64(base64, peer->preshared_key);
|
|
|
|
printf("PresharedKey = %s\n", base64);
|
|
|
|
}
|
2015-06-05 13:58:00 +00:00
|
|
|
if (peer->num_ipmasks)
|
|
|
|
printf("AllowedIPs = ");
|
|
|
|
for_each_wgipmask(peer, ipmask, j) {
|
|
|
|
if (ipmask->family == AF_INET) {
|
|
|
|
if (!inet_ntop(AF_INET, &ipmask->ip4, ip, INET6_ADDRSTRLEN))
|
|
|
|
continue;
|
|
|
|
} else if (ipmask->family == AF_INET6) {
|
|
|
|
if (!inet_ntop(AF_INET6, &ipmask->ip6, ip, INET6_ADDRSTRLEN))
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
printf("%s/%d", ip, ipmask->cidr);
|
|
|
|
if (j + 1 < (size_t)peer->num_ipmasks)
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
if (peer->num_ipmasks)
|
|
|
|
printf("\n");
|
|
|
|
|
2017-01-10 04:52:06 +00:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6) {
|
2015-06-05 13:58:00 +00:00
|
|
|
char host[4096 + 1];
|
|
|
|
char service[512 + 1];
|
|
|
|
socklen_t addr_len = 0;
|
2017-01-10 04:52:06 +00:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET)
|
2015-06-05 13:58:00 +00:00
|
|
|
addr_len = sizeof(struct sockaddr_in);
|
2017-01-10 04:52:06 +00:00
|
|
|
else if (peer->endpoint.addr.sa_family == AF_INET6)
|
2015-06-05 13:58:00 +00:00
|
|
|
addr_len = sizeof(struct sockaddr_in6);
|
2017-01-10 04:52:06 +00:00
|
|
|
if (!getnameinfo(&peer->endpoint.addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST)) {
|
2017-05-15 21:24:48 +00:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET6 && strchr(host, ':'))
|
|
|
|
printf("Endpoint = [%s]:%s\n", host, service);
|
|
|
|
else
|
|
|
|
printf("Endpoint = %s:%s\n", host, service);
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 00:30:03 +00:00
|
|
|
if (peer->persistent_keepalive_interval)
|
|
|
|
printf("PersistentKeepalive = %u\n", peer->persistent_keepalive_interval);
|
|
|
|
|
2015-06-05 13:58:00 +00:00
|
|
|
if (i + 1 < device->num_peers)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
free(device);
|
|
|
|
return ret;
|
|
|
|
}
|