wg: allow in-line comments

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-02-16 20:10:25 +01:00
parent cc8a25e2f6
commit 437116f238
4 changed files with 20 additions and 13 deletions

View file

@ -29,8 +29,9 @@ reset_peer_section() {
reset_peer_section reset_peer_section
while read -r line || [[ -n $line ]]; do while read -r line || [[ -n $line ]]; do
key="${line%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}" stripped="${line%%\#*}"
value="${line#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:space:]])}" key="${stripped%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}"
value="${stripped#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:space:]])}"
[[ $key == "["* ]] && { process_peer; reset_peer_section; } [[ $key == "["* ]] && { process_peer; reset_peer_section; }
[[ $key == "[Peer]" ]] && PEER_SECTION=1 [[ $key == "[Peer]" ]] && PEER_SECTION=1
if [[ $PEER_SECTION -eq 1 ]]; then if [[ $PEER_SECTION -eq 1 ]]; then

View file

@ -417,25 +417,30 @@ error:
bool config_read_line(struct config_ctx *ctx, const char *input) bool config_read_line(struct config_ctx *ctx, const char *input)
{ {
size_t len = strlen(input), cleaned_len = 0; size_t len, cleaned_len = 0;
char *line = calloc(len + 1, sizeof(char)); char *line, *comment;
bool ret = true; bool ret = true;
/* This is what strchrnull is for, but that isn't portable. */
comment = strchr(input, COMMENT_CHAR);
if (comment)
len = comment - input;
else
len = strlen(input);
line = calloc(len + 1, sizeof(char));
if (!line) { if (!line) {
perror("calloc"); perror("calloc");
ret = false; ret = false;
goto out; goto out;
} }
if (!len)
goto out;
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
if (!isspace(input[i])) if (!isspace(input[i]))
line[cleaned_len++] = input[i]; line[cleaned_len++] = input[i];
} }
if (!cleaned_len) if (!cleaned_len)
goto out; goto out;
if (line[0] == COMMENT_CHAR)
goto out;
ret = process_line(ctx, line); ret = process_line(ctx, line);
out: out:
free(line); free(line);

View file

@ -27,7 +27,7 @@ PROGRAM="${0##*/}"
ARGS=( "$@" ) ARGS=( "$@" )
parse_options() { parse_options() {
local interface_section=0 line key value local interface_section=0 line key value stripped
CONFIG_FILE="$1" CONFIG_FILE="$1"
[[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf" [[ $CONFIG_FILE =~ ^[a-zA-Z0-9_=+.-]{1,15}$ ]] && CONFIG_FILE="/etc/wireguard/$CONFIG_FILE.conf"
[[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist" [[ -e $CONFIG_FILE ]] || die "\`$CONFIG_FILE' does not exist"
@ -37,8 +37,9 @@ parse_options() {
INTERFACE="${BASH_REMATCH[2]}" INTERFACE="${BASH_REMATCH[2]}"
shopt -s nocasematch shopt -s nocasematch
while read -r line || [[ -n $line ]]; do while read -r line || [[ -n $line ]]; do
key="${line%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}" stripped="${line%%\#*}"
value="${line#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:space:]])}" key="${stripped%%=*}"; key="${key##*([[:space:]])}"; key="${key%%*([[:space:]])}"
value="${stripped#*=}"; value="${value##*([[:space:]])}"; value="${value%%*([[:space:]])}"
[[ $key == "["* ]] && interface_section=0 [[ $key == "["* ]] && interface_section=0
[[ $key == "[Interface]" ]] && interface_section=1 [[ $key == "[Interface]" ]] && interface_section=1
if [[ $interface_section -eq 1 ]]; then if [[ $interface_section -eq 1 ]]; then

View file

@ -165,8 +165,8 @@ when unspecified, this option is off. Most users will not need this. Optional.
.SH CONFIGURATION FILE FORMAT EXAMPLE .SH CONFIGURATION FILE FORMAT EXAMPLE
This example may be used as a model for writing configuration files, following an This example may be used as a model for writing configuration files, following an
INI-like syntax. Lines that start with a '#' are considered comments and are thus INI-like syntax. Characters after and including a '#' are considered comments and
ignored. are thus ignored.
[Interface] [Interface]
.br .br