summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZ. Liu <liuzx@knownsec.com>2024-05-20 22:23:40 +0800
committerJozsef Kadlecsik <kadlec@netfilter.org>2024-06-05 08:25:45 +0200
commit776f37405da713d3d5ad3908dd9aee4f50c1b1f9 (patch)
treec832ed511bb7469b16d91eeddea2ad7d4554565b
parented47b815a0d2ca9fc3bd4af5207a5d95d66d54ba (diff)
ipset: fix json output format for IPSET_OPT_IP
IPSET_OPT_IP should be quoted to be a well formed json file, otherwise see following bad example (range is not quoted): # ipset create foo bitmap:ip range 192.168.0.0/16 # ipset list -o json foo [ { "name" : "foo", "type" : "bitmap:ip", "revision" : 3, "header" : { "range" : 192.168.0.0-192.168.255.255, "memsize" : 8280, "references" : 0, "numentries" : 0 }, "members" : [ ] } ] Signed-off-by: Z. Liu <liuzx@knownsec.com> Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
-rw-r--r--lib/print.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/print.c b/lib/print.c
index 6ea79cb..3806e1c 100644
--- a/lib/print.c
+++ b/lib/print.c
@@ -261,6 +261,7 @@ ipset_print_ip(char *buf, unsigned int len,
uint8_t family, cidr;
int flags, size, offset = 0;
enum ipset_opt cidropt;
+ const char *quoted = env & IPSET_ENV_QUOTED ? "\"" : "";
assert(buf);
assert(len > 0);
@@ -277,20 +278,26 @@ ipset_print_ip(char *buf, unsigned int len,
cidr = family == NFPROTO_IPV6 ? 128 : 32;
flags = (env & IPSET_ENV_RESOLVE) ? 0 : NI_NUMERICHOST;
+ size = snprintf(buf, len, "%s", quoted);
+ SNPRINTF_FAILURE(size, len, offset);
+
ip = ipset_data_get(data, opt);
assert(ip);
if (family == NFPROTO_IPV4)
- size = snprintf_ipv4(buf, len, flags, ip, cidr);
+ size = snprintf_ipv4(buf + offset, len, flags, ip, cidr);
else if (family == NFPROTO_IPV6)
- size = snprintf_ipv6(buf, len, flags, ip, cidr);
+ size = snprintf_ipv6(buf + offset, len, flags, ip, cidr);
else
return -1;
D("size %i, len %u", size, len);
SNPRINTF_FAILURE(size, len, offset);
D("len: %u, offset %u", len, offset);
- if (!ipset_data_test(data, IPSET_OPT_IP_TO))
+ if (!ipset_data_test(data, IPSET_OPT_IP_TO)) {
+ size = snprintf(buf + offset, len, "%s", quoted);
+ SNPRINTF_FAILURE(size, len, offset);
return offset;
+ }
size = snprintf(buf + offset, len, "%s", IPSET_RANGE_SEPARATOR);
SNPRINTF_FAILURE(size, len, offset);
@@ -304,6 +311,10 @@ ipset_print_ip(char *buf, unsigned int len,
return -1;
SNPRINTF_FAILURE(size, len, offset);
+
+ size = snprintf(buf + offset, len, "%s", quoted);
+ SNPRINTF_FAILURE(size, len, offset);
+
return offset;
}