Return-Path: manuel.schoelling@gmx.de Received: from krantz.zx2c4.com (localhost [127.0.0.1]) by krantz.zx2c4.com (ZX2C4 Mail Server) with ESMTP id b16b9a92 for ; Sun, 10 Dec 2017 13:41:00 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by krantz.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 1bfe83df for ; Sun, 10 Dec 2017 13:41:00 +0000 (UTC) From: =?UTF-8?q?Manuel=20Sch=C3=B6lling?= To: wireguard@lists.zx2c4.com Subject: [PATCH] tools: Use '-' to read from stdin instead of file Date: Sun, 10 Dec 2017 14:48:05 +0100 Message-Id: <20171210134805.3441-1-manuel.schoelling@gmx.de> List-Id: Development discussion of WireGuard List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , --- src/tools/config.c | 15 ++++++++++----- src/tools/setconf.c | 17 +++++++++++------ src/tools/wg.8 | 6 ++++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/tools/config.c b/src/tools/config.c index 1fddb64..d7bd926 100644 --- a/src/tools/config.c +++ b/src/tools/config.c @@ -121,10 +121,14 @@ static bool parse_keyfile(uint8_t key[static WG_KEY_LEN], const char *path) char dst[WG_KEY_LEN_BASE64]; bool ret = false; - f = fopen(path, "r"); - if (!f) { - perror("fopen"); - return false; + if (strcmp(path, "-") == 0) + f = stdin; + else { + f = fopen(path, "r"); + if (!f) { + perror("fopen"); + return false; + } } if (fread(dst, WG_KEY_LEN_BASE64 - 1, 1, f) != 1) { @@ -157,7 +161,8 @@ static bool parse_keyfile(uint8_t key[static WG_KEY_LEN], const char *path) ret = parse_key(key, dst); out: - fclose(f); + if (f != stdin) + fclose(f); return ret; } diff --git a/src/tools/setconf.c b/src/tools/setconf.c index b87a13f..137d2bd 100644 --- a/src/tools/setconf.c +++ b/src/tools/setconf.c @@ -27,13 +27,18 @@ int setconf_main(int argc, char *argv[]) return 1; } - config_input = fopen(argv[2], "r"); - if (!config_input) { - perror("fopen"); - return 1; + if (strcmp(argv[2], "-") == 0) + config_input = stdin; + else { + config_input = fopen(argv[2], "r"); + if (!config_input) { + perror("fopen"); + return 1; + } } if (!config_read_init(&ctx, !strcmp(argv[0], "addconf"))) { - fclose(config_input); + if (config_input != stdin) + fclose(config_input); return 1; } while (getline(&config_buffer, &config_buffer_len, config_input) >= 0) { @@ -58,7 +63,7 @@ int setconf_main(int argc, char *argv[]) ret = 0; cleanup: - if (config_input) + if (config_input && config_input != stdin) fclose(config_input); free(config_buffer); free_wgdevice(device); diff --git a/src/tools/wg.8 b/src/tools/wg.8 index 612fb4e..9e03882 100644 --- a/src/tools/wg.8 +++ b/src/tools/wg.8 @@ -65,8 +65,9 @@ be a files, because command line arguments are not considered private on most systems but if you are using .BR bash (1), you may safely pass in a string by specifying as \fIprivate-key\fP or -\fIpreshared-key\fP the expression: <(echo PRIVATEKEYSTRING). If -\fI/dev/null\fP or another empty file is specified as the filename for +\fIpreshared-key\fP the expression: <(echo PRIVATEKEYSTRING). You can also +pass '-' as filename to read the key from stdin. +If \fI/dev/null\fP or another empty file is specified as the filename for either \fIprivate-key\fP or \fIpreshared-key\fP, the key is removed from the device. The use of \fIpreshared-key\fP is optional, and may be omitted; it adds an additional layer of symmetric-key cryptography to be mixed into @@ -88,6 +89,7 @@ and may be specified in hexadecimal by prepending "0x". Sets the current configuration of \fI\fP to the contents of \fI\fP, which must be in the format described by \fICONFIGURATION FILE FORMAT\fP below. +If you pass '-' as filename, the configuration will be read from stdin. .TP \fBaddconf\fP \fI\fP \fI\fP Appends the contents of \fI\fP, which must -- 2.15.1