2017-11-30 16:23:50 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2018-01-03 21:58:00 +01:00
|
|
|
* Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2017-11-30 16:23:50 +01:00
|
|
|
*/
|
2015-06-05 15:58:00 +02: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>
|
|
|
|
|
2017-09-25 04:22:09 +02:00
|
|
|
#include "containers.h"
|
2017-05-15 23:24:48 +02:00
|
|
|
#include "encoding.h"
|
2016-07-20 21:24:27 +02:00
|
|
|
#include "ipc.h"
|
2017-09-25 04:22:09 +02:00
|
|
|
#include "subcommands.h"
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
int showconf_main(int argc, char *argv[])
|
|
|
|
{
|
2017-04-16 01:20:43 +02:00
|
|
|
char base64[WG_KEY_LEN_BASE64];
|
2015-06-05 15:58:00 +02:00
|
|
|
char ip[INET6_ADDRSTRLEN];
|
|
|
|
struct wgdevice *device = NULL;
|
|
|
|
struct wgpeer *peer;
|
2017-09-25 04:22:09 +02:00
|
|
|
struct wgallowedip *allowedip;
|
2015-06-05 15:58:00 +02:00
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s %s <interface>\n", PROG_NAME, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-20 21:24:27 +02:00
|
|
|
if (ipc_get_device(&device, argv[1])) {
|
2015-06-05 15:58:00 +02:00
|
|
|
perror("Unable to get device");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[Interface]\n");
|
2017-09-25 04:22:09 +02:00
|
|
|
if (device->listen_port)
|
|
|
|
printf("ListenPort = %u\n", device->listen_port);
|
2017-01-24 17:43:35 +01:00
|
|
|
if (device->fwmark)
|
|
|
|
printf("FwMark = 0x%x\n", device->fwmark);
|
2017-11-11 04:30:21 +01:00
|
|
|
if (device->flags & WGDEVICE_HAS_PRIVATE_KEY) {
|
2017-04-16 01:20:43 +02:00
|
|
|
key_to_base64(base64, device->private_key);
|
|
|
|
printf("PrivateKey = %s\n", base64);
|
2015-06-05 15:58:00 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
2017-10-25 17:13:46 +02:00
|
|
|
for_each_wgpeer(device, peer) {
|
2017-04-16 01:20:43 +02:00
|
|
|
key_to_base64(base64, peer->public_key);
|
|
|
|
printf("[Peer]\nPublicKey = %s\n", base64);
|
2017-11-11 04:30:21 +01:00
|
|
|
if (peer->flags & WGPEER_HAS_PRESHARED_KEY) {
|
2017-04-27 11:10:50 +02:00
|
|
|
key_to_base64(base64, peer->preshared_key);
|
|
|
|
printf("PresharedKey = %s\n", base64);
|
|
|
|
}
|
2017-09-25 04:22:09 +02:00
|
|
|
if (peer->first_allowedip)
|
2015-06-05 15:58:00 +02:00
|
|
|
printf("AllowedIPs = ");
|
2017-10-25 17:13:46 +02:00
|
|
|
for_each_wgallowedip(peer, allowedip) {
|
2017-09-25 04:22:09 +02:00
|
|
|
if (allowedip->family == AF_INET) {
|
|
|
|
if (!inet_ntop(AF_INET, &allowedip->ip4, ip, INET6_ADDRSTRLEN))
|
2015-06-05 15:58:00 +02:00
|
|
|
continue;
|
2017-09-25 04:22:09 +02:00
|
|
|
} else if (allowedip->family == AF_INET6) {
|
|
|
|
if (!inet_ntop(AF_INET6, &allowedip->ip6, ip, INET6_ADDRSTRLEN))
|
2015-06-05 15:58:00 +02:00
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
continue;
|
2017-09-25 04:22:09 +02:00
|
|
|
printf("%s/%d", ip, allowedip->cidr);
|
|
|
|
if (allowedip->next_allowedip)
|
2015-06-05 15:58:00 +02:00
|
|
|
printf(", ");
|
|
|
|
}
|
2017-09-25 04:22:09 +02:00
|
|
|
if (peer->first_allowedip)
|
2015-06-05 15:58:00 +02:00
|
|
|
printf("\n");
|
|
|
|
|
2017-01-10 05:52:06 +01:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6) {
|
2015-06-05 15:58:00 +02:00
|
|
|
char host[4096 + 1];
|
|
|
|
char service[512 + 1];
|
|
|
|
socklen_t addr_len = 0;
|
2017-10-25 17:56:08 +02:00
|
|
|
|
2017-01-10 05:52:06 +01:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET)
|
2015-06-05 15:58:00 +02:00
|
|
|
addr_len = sizeof(struct sockaddr_in);
|
2017-01-10 05:52:06 +01:00
|
|
|
else if (peer->endpoint.addr.sa_family == AF_INET6)
|
2015-06-05 15:58:00 +02:00
|
|
|
addr_len = sizeof(struct sockaddr_in6);
|
2017-01-10 05:52:06 +01:00
|
|
|
if (!getnameinfo(&peer->endpoint.addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST)) {
|
2017-05-15 23:24:48 +02: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 15:58:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 02:30:03 +02:00
|
|
|
if (peer->persistent_keepalive_interval)
|
|
|
|
printf("PersistentKeepalive = %u\n", peer->persistent_keepalive_interval);
|
|
|
|
|
2017-09-25 04:22:09 +02:00
|
|
|
if (peer->next_peer)
|
2015-06-05 15:58:00 +02:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2017-09-25 04:22:09 +02:00
|
|
|
free_wgdevice(device);
|
2015-06-05 15:58:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|