Skip to content

Commit 7aac81a

Browse files
committed
pfctl: support recusive printing of tables
Currently 'pfctl -a "*" -sr' recursively walks anchor tree and shows rules found in every anchor. This commit introduces the same behavior for tables. Command 'pfctl -a "*" -sT' prints all tables attached to every anchor loaded to pf(4). Inconsistency has been noticed by Klemens (kn@). OK @bluhm, OK @kn Obtained from: OpenBSD, sashan <[email protected]>, 3898e3532e Sponsored by: Rubicon Communications, LLC ("Netgate")
1 parent 6669467 commit 7aac81a

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

sbin/pfctl/pfctl.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ int pfctl_recurse(int, int, const char *,
137137
int pfctl_call_clearrules(int, int, struct pfr_anchoritem *);
138138
int pfctl_call_cleartables(int, int, struct pfr_anchoritem *);
139139
int pfctl_call_clearanchors(int, int, struct pfr_anchoritem *);
140+
int pfctl_call_showtables(int, int, struct pfr_anchoritem *);
140141

141142
static struct pfctl_anchor_global pf_anchors;
142143
struct pfctl_anchor pf_main_anchor;
@@ -3056,6 +3057,13 @@ pfctl_call_clearanchors(int dev, int opts, struct pfr_anchoritem *pfra)
30563057
return (rv);
30573058
}
30583059

3060+
int
3061+
pfctl_call_showtables(int dev, int opts, struct pfr_anchoritem *pfra)
3062+
{
3063+
pfctl_show_tables(pfra->pfra_anchorname, opts);
3064+
return (0);
3065+
}
3066+
30593067
int
30603068
pfctl_recurse(int dev, int opts, const char *anchorname,
30613069
int(*walkf)(int, int, struct pfr_anchoritem *))
@@ -3070,11 +3078,13 @@ pfctl_recurse(int dev, int opts, const char *anchorname,
30703078
* so that failures on one anchor do not prevent clearing others.
30713079
*/
30723080
opts |= PF_OPT_IGNFAIL;
3073-
printf("Removing:\n");
3081+
if ((opts & PF_OPT_CALLSHOW) == 0)
3082+
printf("Removing:\n");
30743083
SLIST_FOREACH_SAFE(pfra, anchors, pfra_sle, pfra_save) {
3075-
printf(" %s\n",
3076-
(*pfra->pfra_anchorname == '\0') ? "/" :
3077-
pfra->pfra_anchorname);
3084+
if ((opts & PF_OPT_CALLSHOW) == 0)
3085+
printf(" %s\n",
3086+
(*pfra->pfra_anchorname == '\0') ? "/" :
3087+
pfra->pfra_anchorname);
30783088
rv |= walkf(dev, opts, pfra);
30793089
SLIST_REMOVE(anchors, pfra, pfr_anchoritem, pfra_sle);
30803090
free(pfra->pfra_anchorname);
@@ -3477,7 +3487,12 @@ main(int argc, char *argv[])
34773487
pfctl_show_fingerprints(opts);
34783488
break;
34793489
case 'T':
3480-
pfctl_show_tables(anchorname, opts);
3490+
if (opts & PF_OPT_RECURSE) {
3491+
opts |= PF_OPT_CALLSHOW;
3492+
pfctl_recurse(dev, opts, anchorname,
3493+
pfctl_call_showtables);
3494+
} else
3495+
pfctl_show_tables(anchorname, opts);
34813496
break;
34823497
case 'o':
34833498
pfctl_load_fingerprints(dev, opts);

sbin/pfctl/pfctl_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define PF_OPT_KILLMATCH 0x08000
5757
#define PF_OPT_NODNS 0x10000
5858
#define PF_OPT_IGNFAIL 0x20000
59+
#define PF_OPT_CALLSHOW 0x40000
5960

6061
#define PF_NAT_PROXY_PORT_LOW 50001
6162
#define PF_NAT_PROXY_PORT_HIGH 65535

sbin/pfctl/pfctl_table.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,21 +417,21 @@ print_table(const struct pfr_table *ta, int verbose, int debug)
417417
{
418418
if (!debug && !(ta->pfrt_flags & PFR_TFLAG_ACTIVE))
419419
return;
420-
if (verbose) {
421-
printf("%c%c%c%c%c%c%c\t%s",
420+
if (verbose)
421+
printf("%c%c%c%c%c%c%c\t",
422422
(ta->pfrt_flags & PFR_TFLAG_CONST) ? 'c' : '-',
423423
(ta->pfrt_flags & PFR_TFLAG_PERSIST) ? 'p' : '-',
424424
(ta->pfrt_flags & PFR_TFLAG_ACTIVE) ? 'a' : '-',
425425
(ta->pfrt_flags & PFR_TFLAG_INACTIVE) ? 'i' : '-',
426426
(ta->pfrt_flags & PFR_TFLAG_REFERENCED) ? 'r' : '-',
427427
(ta->pfrt_flags & PFR_TFLAG_REFDANCHOR) ? 'h' : '-',
428-
(ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-',
429-
ta->pfrt_name);
430-
if (ta->pfrt_anchor[0])
431-
printf("\t%s", ta->pfrt_anchor);
432-
puts("");
433-
} else
434-
puts(ta->pfrt_name);
428+
(ta->pfrt_flags & PFR_TFLAG_COUNTERS) ? 'C' : '-');
429+
430+
printf("%s", ta->pfrt_name);
431+
if (ta->pfrt_anchor[0] != '\0')
432+
printf("@%s", ta->pfrt_anchor);
433+
434+
printf("\n");
435435
}
436436

437437
int

0 commit comments

Comments
 (0)