Skip to content

Commit 53ef9f1

Browse files
pticonjow-
authored andcommitted
firewall3: add UBUS support for include scripts
It gives the ability to include scripts via procd services and netifd interface firewall data. Signed-off-by: Pierre Lebleu <[email protected]>
1 parent 5cd4af4 commit 53ef9f1

File tree

3 files changed

+73
-25
lines changed

3 files changed

+73
-25
lines changed

includes.c

+66-21
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,97 @@ const struct fw3_option fw3_include_opts[] = {
3030
{ }
3131
};
3232

33+
static bool
34+
check_include(struct fw3_state *state, struct fw3_include *include, struct uci_element *e)
35+
{
36+
if (!include->enabled)
37+
return false;
38+
39+
if (!include->path)
40+
{
41+
warn_section("include", include, e, "must specify a path");
42+
return false;
43+
}
44+
45+
if (include->type == FW3_INC_TYPE_RESTORE && !include->family)
46+
warn_section("include", include, e, "does not specify a family, include will get"
47+
"loaded with both iptables-restore and ip6tables-restore!");
48+
49+
return true;
50+
}
51+
52+
static struct fw3_include *
53+
fw3_alloc_include(struct fw3_state *state)
54+
{
55+
struct fw3_include *include;
56+
57+
include = calloc(1, sizeof(*include));
58+
if (!include)
59+
return NULL;
60+
61+
include->enabled = true;
62+
63+
list_add_tail(&include->list, &state->includes);
64+
65+
return include;
66+
}
3367

3468
void
35-
fw3_load_includes(struct fw3_state *state, struct uci_package *p)
69+
fw3_load_includes(struct fw3_state *state, struct uci_package *p,
70+
struct blob_attr *a)
3671
{
3772
struct uci_section *s;
3873
struct uci_element *e;
3974
struct fw3_include *include;
75+
struct blob_attr *entry;
76+
unsigned rem;
4077

4178
INIT_LIST_HEAD(&state->includes);
4279

43-
uci_foreach_element(&p->sections, e)
80+
blob_for_each_attr(entry, a, rem)
4481
{
45-
s = uci_to_section(e);
82+
const char *type;
83+
const char *name = "ubus include";
4684

47-
if (strcmp(s->type, "include"))
85+
if (!fw3_attr_parse_name_type(entry, &name, &type))
4886
continue;
4987

50-
include = calloc(1, sizeof(*include));
51-
if (!include)
88+
if (strcmp(type, "script") && strcmp(type, "restore"))
5289
continue;
5390

54-
include->name = e->name;
55-
include->enabled = true;
56-
57-
if (!fw3_parse_options(include, fw3_include_opts, s))
58-
warn_elem(e, "has invalid options");
91+
include = fw3_alloc_include(state);
92+
if (!include)
93+
continue;
5994

60-
if (!include->enabled)
95+
if (!fw3_parse_blob_options(include, fw3_include_opts, entry, name))
6196
{
97+
warn_section("include", include, NULL, "skipped due to invalid options");
6298
fw3_free_include(include);
6399
continue;
64100
}
65101

66-
if (!include->path)
67-
{
68-
warn_elem(e, "must specify a path");
102+
if (!check_include(state, include, NULL))
69103
fw3_free_include(include);
104+
}
105+
106+
uci_foreach_element(&p->sections, e)
107+
{
108+
s = uci_to_section(e);
109+
110+
if (strcmp(s->type, "include"))
111+
continue;
112+
113+
include = fw3_alloc_include(state);
114+
if (!include)
70115
continue;
71-
}
72116

73-
if (include->type == FW3_INC_TYPE_RESTORE && !include->family)
74-
warn_elem(e, "does not specify a family, include will get loaded "
75-
"with both iptables-restore and ip6tables-restore!");
117+
include->name = e->name;
76118

77-
list_add_tail(&include->list, &state->includes);
78-
continue;
119+
if (!fw3_parse_options(include, fw3_include_opts, s))
120+
warn_elem(e, "has invalid options");
121+
122+
if (!check_include(state, include, e))
123+
fw3_free_include(include);
79124
}
80125
}
81126

includes.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424

2525
extern const struct fw3_option fw3_include_opts[];
2626

27-
void fw3_load_includes(struct fw3_state *state, struct uci_package *p);
27+
void fw3_load_includes(struct fw3_state *state, struct uci_package *p, struct blob_attr *a);
2828

2929
void fw3_print_includes(struct fw3_state *state, enum fw3_family family,
3030
bool reload);
3131

3232
void fw3_run_includes(struct fw3_state *state, bool reload);
3333

34-
#define fw3_free_include(include) \
35-
fw3_free_object(include, fw3_include_opts)
34+
static inline void fw3_free_include(struct fw3_include *include)
35+
{
36+
list_del(&include->list);
37+
fw3_free_object(include, fw3_include_opts);
38+
}
3639

3740
#endif

main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ build_state(bool runtime)
107107
fw3_load_redirects(state, p, b.head);
108108
fw3_load_snats(state, p, b.head);
109109
fw3_load_forwards(state, p, b.head);
110-
fw3_load_includes(state, p);
110+
fw3_load_includes(state, p, b.head);
111111

112112
return true;
113113
}

0 commit comments

Comments
 (0)