xtables-translate: add escape_quotes option to comment_xlate
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
@@ -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
@@ -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
@@ -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
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
@@ -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, ¶ms); }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
Pablo Neira Ayuso
maintainerQuoted 2 lines · Pablo M. Bermudo Garay
The
comment_xlatefunction 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
@@ -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 ; }
Pablo Neira Ayuso
maintainerRe: [PATCH iptables 2/2] xtables-translate-restore: do not escape quotes
In reply to Pablo M. Bermudo GarayQuoted 14 lines · Pablo M. Bermudo Garay
If quotes are escaped, nft -f is unable to parse and load the translated ruleset.
Signed-off-by Pablo M. Bermudo Garay <[email protected]>--- iptables/xtables-translate.c | 11 +++++++++++ 1 file changed, 11 insertions(+)iptables/xtables-translate.c +0
-0@@ -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, ¶ms); }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.
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_xlatefunction 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?
Pablo Neira Ayuso
maintainerQuoted 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_xlatefunction 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.