Skip to contentSkip to conversation

xtables-translate: add escape_quotes option to comment_xlate

PATCH Superseded Pablo M. Bermudo Garay 2 participants 6 messages Aug 16, 2016 – Aug 19, 2016 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xtables-translate: add escape_quotes option to comment_xlate

The comment_xlate function was not supporting this option that is necessary in some situations.

---
 extensions/libxt_comment.c | 9 ++++++++-
 iptables/nft-ipv4.c        | 2 +-
 iptables/nft-ipv6.c        | 2 +-
 3 files changed, 10 insertions(+), 3 deletions(-)
extensions/libxt_comment.c +8 -1
index 0e31edd..bf9a039 100644
@@ -52,9 +52,16 @@ static int comment_xlate(struct xt_xlate *xl,
 			 const struct xt_xlate_mt_params *params)
 {
 	struct xt_comment_info *commentinfo = (void *)params->match->data;
+	char comment[XT_MAX_COMMENT_LEN];
 
 	commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
-	xt_xlate_add_comment(xl, commentinfo->comment);
+	if (params->escape_quotes)
+		snprintf(comment, XT_MAX_COMMENT_LEN, "comment \\\"%s\\\"",
+			 commentinfo->comment);
+	else
+		snprintf(comment, XT_MAX_COMMENT_LEN, "comment \"%s\"",
+			 commentinfo->comment);
+	xt_xlate_add_comment(xl, comment);
 
 	return 1;
 }
iptables/nft-ipv4.c +1 -1
index 50706cb..f5c0d95 100644
@@ -490,7 +490,7 @@ static int nft_ipv4_xlate(const void *data, struct xt_xlate *xl)
 
 	comment = xt_xlate_get_comment(xl);
 	if (comment)
-		xt_xlate_add(xl, "comment \\\"%s\\\" ", comment);
+		xt_xlate_add(xl, "%s", comment);
 
 	ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
 
iptables/nft-ipv6.c +1 -1
index 8ca523c..3792c68 100644
@@ -439,7 +439,7 @@ static int nft_ipv6_xlate(const void *data, struct xt_xlate *xl)
 
 	comment = xt_xlate_get_comment(xl);
 	if (comment)
-		xt_xlate_add(xl, "comment \\\"%s\\\" ", comment);
+		xt_xlate_add(xl, "%s", comment);
 
 	ret = xlate_action(cs, !!(cs->fw6.ipv6.flags & IP6T_F_GOTO), xl);
 
Signature · 1 line
2.9.3
#1 lore raw

PATCH 2/2 xtables-translate-restore: do not escape quotes

If quotes are escaped, nft -f is unable to parse and load the translated ruleset.

---
 iptables/xtables-translate.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
iptables/xtables-translate.c +11 -0
index 3c577ed..914d3b1 100644
@@ -72,6 +72,11 @@ int xlate_action(const struct iptables_command_state *cs, bool goto_set,
 				.numeric	= numeric,
 				.escape_quotes	= true,
 			};
+			if (!strcmp(xtables_globals.program_name,
+				   "iptables-translate-restore") ||
+			    !strcmp(xtables_globals.program_name,
+				   "ip6tables-translate-restore"))
+				params.escape_quotes = false;
 			ret = cs->target->xlate(xl, &params);
 		}
 		else
@@ -100,6 +105,12 @@ int xlate_matches(const struct iptables_command_state *cs, struct xt_xlate *xl)
 			.escape_quotes	= true,
 		};
 
+		if (!strcmp(xtables_globals.program_name,
+			   "iptables-translate-restore") ||
+		    !strcmp(xtables_globals.program_name,
+			   "ip6tables-translate-restore"))
+			params.escape_quotes = false;
+
 		if (!matchp->match->xlate)
 			return 0;
 
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH iptables 2/2] xtables-translate-restore: do not escape quotes

Quoted 14 lines · Pablo M. Bermudo Garay

If quotes are escaped, nft -f is unable to parse and load the translated ruleset.

---
 iptables/xtables-translate.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
iptables/xtables-translate.c +0 -0
index 3c577ed..914d3b1 100644
@@ -72,6 +72,11 @@ int xlate_action(const struct iptables_command_state *cs, bool goto_set,
 				.numeric	= numeric,

iptables/xtables-translate.c int xlate_action(const struct iptables_command_state *cs, bool goto_set, · line 82

Quoted 10 lines
iptables/xtables-translate.c +5 -0
 				.escape_quotes	= true,
 			};
+			if (!strcmp(xtables_globals.program_name,
+				   "iptables-translate-restore") ||
+			    !strcmp(xtables_globals.program_name,
+				   "ip6tables-translate-restore"))
+				params.escape_quotes = false;
 			ret = cs->target->xlate(xl, &params);
 		}
 		else

I can see a 'bool restore' in do_command_xlate() provides this context, you can probably propagate this.

This strcmp() is ugly using the global.

#3 lore raw
In reply to Pablo M. Bermudo Garay
Quoted 2 lines · Pablo M. Bermudo Garay

The comment_xlate function was not supporting this option that is necessary in some situations.

I have applied what I'm attaching to this email, that is more simple than this and makes sure buffer is nul-terminated (given snprintf doesn't guarantee this).

commit 3317b14f0d6fa0e460e4e758b7e3010f940d07bc

Author: Pablo M. Bermudo Garay <[email protected]>
Date:   Tue Aug 16 19:44:32 2016 +0200

xtables-translate: add escape_quotes option to comment_xlate

The comment_xlate function was not supporting this option that is necessary in some situations.

extensions/libxt_comment.c +10 -1
index 0e31edd..b635d16 100644
@@ -52,9 +52,18 @@ static int comment_xlate(struct xt_xlate *xl,
 			 const struct xt_xlate_mt_params *params)
 {
 	struct xt_comment_info *commentinfo = (void *)params->match->data;
+	char comment[XT_MAX_COMMENT_LEN];
 
 	commentinfo->comment[XT_MAX_COMMENT_LEN - 1] = '\0';
-	xt_xlate_add_comment(xl, commentinfo->comment);
+	if (params->escape_quotes)
+		snprintf(comment, XT_MAX_COMMENT_LEN, "\\\"%s\\\"",
+			 commentinfo->comment);
+	else
+		snprintf(comment, XT_MAX_COMMENT_LEN, "\"%s\"",
+			 commentinfo->comment);
+
+	comment[XT_MAX_COMMENT_LEN - 1] = '\0';
+	xt_xlate_add_comment(xl, comment);
 
 	return 1;
 }
#4 lore raw

2016-08-17 16:23 GMT+02:00 Pablo Neira Ayuso <[email protected]>:

Quoted 7 lines · Pablo M. Bermudo Garay
Quoted 2 lines

The comment_xlate function was not supporting this option that is necessary in some situations.

I have applied what I'm attaching to this email, that is more simple than this and makes sure buffer is nul-terminated (given snprintf doesn't guarantee this).

Ok, the simplification seems great. But AFAIK both standards (C99+ and POSIX.1-2001+) guarantee that the string will be null-terminated. Am I wrong about that?

#5 lore raw
Quoted 5 lines · Pablo M. Bermudo Garay

2016-08-17 16:23 GMT+02:00 Pablo Neira Ayuso <[email protected]>:

Quoted 4 lines · Pablo M. Bermudo Garay
Quoted 2 lines

The comment_xlate function was not supporting this option that is necessary in some situations.

Quoted 7 lines
Quoted 3 lines

I have applied what I'm attaching to this email, that is more simple than this and makes sure buffer is nul-terminated (given snprintf doesn't guarantee this).

Ok, the simplification seems great. But AFAIK both standards (C99+ and POSIX.1-2001+) guarantee that the string will be null-terminated. Am I wrong about that?

If you write as many bytes as you can fit into the buffer, then snprintf doesn't guarantee a nul-terminated string.