Open SCAP Library

XCCDF/helpers.h
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <lkuklinek@redhat.com>
21 */
22
23 #ifndef XCCDF_HELPERS_H_
24 #define XCCDF_HELPERS_H_
25
26 #include <assert.h>
27
28 #define MACRO_BLOCK(code) do { code } while(false)
29 #define ASSERT_TYPE(item,t) assert((item)->type & t)
30 #define ASSERT_BENCHMARK(item) ASSERT_TYPE(item, XCCDF_BENCHMARK)
31 #define XBENCHMARK(item) ((struct xccdf_benchmark*)item)
32 #define XPROFILE(item) ((struct xccdf_profile*)item)
33 #define XGROUP(item) ((struct xccdf_group*)item)
34 #define XRULE(item) ((struct xccdf_rule*)item)
35 #define XITEM(item) ((struct xccdf_item*)item)
36 #define XVALUE(item) ((struct xccdf_value*)item)
37 #define XRESULT(item) ((struct xccdf_result*)item)
38
39 #define XCCDF_STATUS_CURRENT(TYPE) \
40 struct xccdf_status * xccdf_##TYPE##_get_status_current(const struct xccdf_##TYPE* item) {\
41 return xccdf_item_get_current_status(XITEM(item)); }
42
43 #define XCCDF_TEXT_IGETTER(SNAME,MNAME,MEXP) \
44 struct oscap_text_iterator* xccdf_##SNAME##_get_##MNAME(const struct xccdf_##SNAME* item) \
45 { return oscap_iterator_new(XITEM(item)->MEXP); }
46 #define XCCDF_GENERIC_GETTER(RTYPE,TNAME,MEMBER) \
47 RTYPE xccdf_##TNAME##_get_##MEMBER(const struct xccdf_##TNAME* item) { return (RTYPE)((item)->MEMBER); }
48 #define XCCDF_GENERIC_IGETTER(ITYPE,TNAME,MNAME) \
49 struct xccdf_##ITYPE##_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
50 { return oscap_iterator_new(item->MNAME); }
51 #define XCCDF_ABSTRACT_GETTER(RTYPE,TNAME,MNAME,MEMBER) \
52 RTYPE xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) { return (RTYPE)(XITEM(item)->MEMBER); }
53 #define XCCDF_ITERATOR_GETTER(ITYPE,TNAME,MNAME,MEMBER) \
54 struct xccdf_##ITYPE##_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
55 { return oscap_iterator_new(XITEM(item)->MEMBER); }
56 #define XCCDF_SITERATOR_GETTER(TNAME,MNAME,MEMBER) \
57 struct oscap_string_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
58 { return oscap_iterator_new(XITEM(item)->MEMBER); }
59 #define XCCDF_TITERATOR_GETTER(TNAME,MNAME,MEMBER) \
60 struct oscap_text_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
61 { return oscap_iterator_new(XITEM(item)->MEMBER); }
62 #define XCCDF_HTABLE_GETTER(RTYPE,TNAME,MNAME,MEMBER) \
63 RTYPE xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item, const char* key) \
64 { return (RTYPE)oscap_htable_get(XITEM(item)->MEMBER, key); }
65 #define XCCDF_SIGETTER(TNAME,MNAME) \
66 struct oscap_string_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
67 { return oscap_iterator_new(XITEM(item)->sub.TNAME.MNAME); }
68
69 #define XCCDF_BENCHMARK_GETTER_A(RTYPE,MNAME,MEMBER) XCCDF_ABSTRACT_GETTER(RTYPE,benchmark,MNAME,MEMBER)
70 #define XCCDF_BENCHMARK_GETTER_I(RTYPE,MNAME) XCCDF_BENCHMARK_GETTER_A(RTYPE,MNAME,item.MNAME)
71 #define XCCDF_BENCHMARK_IGETTER_I(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,benchmark,MNAME,item.MNAME)
72 #define XCCDF_BENCHMARK_GETTER(RTYPE,MNAME) XCCDF_BENCHMARK_GETTER_A(RTYPE,MNAME,sub.benchmark.MNAME)
73 #define XCCDF_BENCHMARK_IGETTER(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,benchmark,MNAME,sub.benchmark.MNAME)
74
75 #define XCCDF_PROFILE_GETTER_A(RTYPE,MNAME,MEMBER) XCCDF_ABSTRACT_GETTER(RTYPE,profile,MNAME,MEMBER)
76 #define XCCDF_PROFILE_GETTER_I(RTYPE,MNAME) XCCDF_PROFILE_GETTER_A(RTYPE,MNAME,item.MNAME)
77 #define XCCDF_PROFILE_IGETTER_I(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,profile,MNAME,item.MNAME)
78 #define XCCDF_PROFILE_GETTER(RTYPE,MNAME) XCCDF_PROFILE_GETTER_A(RTYPE,MNAME,sub.profile.MNAME)
79 #define XCCDF_PROFILE_IGETTER(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,profile,MNAME,sub.profile.MNAME)
80
81 #define XCCDF_RULE_GETTER_A(RTYPE,MNAME,MEMBER) XCCDF_ABSTRACT_GETTER(RTYPE,rule,MNAME,MEMBER)
82 #define XCCDF_RULE_GETTER_I(RTYPE,MNAME) XCCDF_RULE_GETTER_A(RTYPE,MNAME,item.MNAME)
83 #define XCCDF_RULE_IGETTER_I(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,rule,MNAME,item.MNAME)
84 #define XCCDF_RULE_GETTER(RTYPE,MNAME) XCCDF_RULE_GETTER_A(RTYPE,MNAME,sub.rule.MNAME)
85 #define XCCDF_RULE_IGETTER(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,rule,MNAME,sub.rule.MNAME)
86
87 #define XCCDF_GROUP_GETTER_A(RTYPE,MNAME,MEMBER) XCCDF_ABSTRACT_GETTER(RTYPE,group,MNAME,MEMBER)
88 #define XCCDF_GROUP_GETTER_I(RTYPE,MNAME) XCCDF_GROUP_GETTER_A(RTYPE,MNAME,item.MNAME)
89 #define XCCDF_GROUP_IGETTER_I(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,group,MNAME,item.MNAME)
90 #define XCCDF_GROUP_GETTER(RTYPE,MNAME) XCCDF_GROUP_GETTER_A(RTYPE,MNAME,sub.group.MNAME)
91 #define XCCDF_GROUP_IGETTER(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,group,MNAME,sub.group.MNAME)
92
93 #define XCCDF_VALUE_GETTER_A(RTYPE,MNAME,MEMBER) XCCDF_ABSTRACT_GETTER(RTYPE,value,MNAME,MEMBER)
94 #define XCCDF_VALUE_GETTER_I(RTYPE,MNAME) XCCDF_VALUE_GETTER_A(RTYPE,MNAME,item.MNAME)
95 #define XCCDF_VALUE_IGETTER_I(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,value,MNAME,item.MNAME)
96 #define XCCDF_VALUE_GETTER(RTYPE,MNAME) XCCDF_VALUE_GETTER_A(RTYPE,MNAME,sub.value.MNAME)
97 #define XCCDF_VALUE_IGETTER(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,value,MNAME,sub.value.MNAME)
98
99 #define XCCDF_RESULT_GETTER_A(RTYPE,MNAME,MEMBER) XCCDF_ABSTRACT_GETTER(RTYPE,result,MNAME,MEMBER)
100 #define XCCDF_RESULT_GETTER_I(RTYPE,MNAME) XCCDF_RESULT_GETTER_A(RTYPE,MNAME,item.MNAME)
101 #define XCCDF_RESULT_IGETTER_I(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,result,MNAME,item.MNAME)
102 #define XCCDF_RESULT_GETTER(RTYPE,MNAME) XCCDF_RESULT_GETTER_A(RTYPE,MNAME,sub.result.MNAME)
103 #define XCCDF_RESULT_IGETTER(ITYPE,MNAME) XCCDF_ITERATOR_GETTER(ITYPE,result,MNAME,sub.result.MNAME)
104
105 #define XCCDF_SETTER_GENERIC(STYPE,MNAME,MTYPE,FREE,DUP) \
106 bool xccdf_##STYPE##_set_##MNAME(struct xccdf_##STYPE* item, MTYPE newval) \
107 { FREE(XITEM(item)->sub.STYPE.MNAME); XITEM(item)->sub.STYPE.MNAME = DUP(newval); return true; }
108 #define XCCDF_SETTER_SIMPLE(STYPE,MNAME,MTYPE) \
109 bool xccdf_##STYPE##_set_##MNAME(struct xccdf_##STYPE* item, MTYPE newval) \
110 { XITEM(item)->sub.STYPE.MNAME = (newval); return true; }
111 #define XCCDF_SETTER_STRING(STYPE,MNAME) \
112 XCCDF_SETTER_GENERIC(STYPE, MNAME, const char*, free, oscap_strdup)
113 #define XCCDF_ACCESSOR_STRING(STYPE,MNAME) \
114 XCCDF_SETTER_STRING(STYPE, MNAME) XCCDF_ABSTRACT_GETTER(const char*, STYPE, MNAME, sub.STYPE.MNAME)
115 #define XCCDF_ACCESSOR_SIMPLE(STYPE,MTYPE,MNAME) \
116 XCCDF_SETTER_SIMPLE(STYPE,MNAME,MTYPE) XCCDF_ABSTRACT_GETTER(MTYPE,STYPE,MNAME,sub.STYPE.MNAME)
117 #define XCCDF_IGETTER(TNAME,ITYPE,MNAME) \
118 XCCDF_ITERATOR_GETTER(ITYPE, TNAME, MNAME, sub.TNAME.MNAME)
119 #define XCCDF_ADDER(STYPE,MNAME,SINGNAME,MTYPE,DUP) \
120 bool xccdf_##STYPE##_add_##SINGNAME(struct xccdf_##STYPE* item, MTYPE newval) \
121 { return oscap_list_add(XITEM(item)->sub.STYPE.MNAME, DUP(newval)); }
122 #define XCCDF_LISTMANIP(TNAME,ITYPE,MNAME) \
123 XCCDF_IGETTER(TNAME,ITYPE,MNAME) XCCDF_ADDER(TNAME,MNAME,ITYPE,struct xccdf_##ITYPE*,)
124 #define XCCDF_LISTMANIP_STRING(TNAME,SNAME,MNAME) \
125 struct oscap_string_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
126 { return oscap_iterator_new(XITEM(item)->sub.TNAME.MNAME); } \
127 XCCDF_ADDER(TNAME,MNAME,SNAME,const char *,oscap_strdup)
128 #define XCCDF_LISTMANIP_TEXT(TNAME,SNAME,MNAME) \
129 struct oscap_text_iterator* xccdf_##TNAME##_get_##MNAME(const struct xccdf_##TNAME* item) \
130 { return oscap_iterator_new(XITEM(item)->sub.TNAME.MNAME); } \
131 XCCDF_ADDER(TNAME,MNAME,SNAME,struct oscap_text *,)
132
133 #define XCCDF_ITEM_GETTER(RTYPE,MNAME) \
134 XCCDF_ABSTRACT_GETTER(RTYPE,item,MNAME,item.MNAME) \
135 XCCDF_BENCHMARK_GETTER_A(RTYPE,MNAME,item.MNAME) \
136 XCCDF_PROFILE_GETTER_A(RTYPE,MNAME,item.MNAME) \
137 XCCDF_RULE_GETTER_A(RTYPE,MNAME,item.MNAME) \
138 XCCDF_VALUE_GETTER_A(RTYPE,MNAME,item.MNAME) \
139 XCCDF_GROUP_GETTER_A(RTYPE,MNAME,item.MNAME) \
140 XCCDF_RESULT_GETTER_A(RTYPE,MNAME,item.MNAME)
141 #define XCCDF_ITEM_IGETTER(RTYPE,MNAME) \
142 XCCDF_ITERATOR_GETTER(RTYPE,item,MNAME,item.MNAME) \
143 XCCDF_ITERATOR_GETTER(RTYPE,benchmark,MNAME,item.MNAME) \
144 XCCDF_ITERATOR_GETTER(RTYPE,profile,MNAME,item.MNAME) \
145 XCCDF_ITERATOR_GETTER(RTYPE,rule,MNAME,item.MNAME) \
146 XCCDF_ITERATOR_GETTER(RTYPE,value,MNAME,item.MNAME) \
147 XCCDF_ITERATOR_GETTER(RTYPE,group,MNAME,item.MNAME) \
148 XCCDF_ITERATOR_GETTER(RTYPE,result,MNAME,item.MNAME)
149 #define XCCDF_ITEM_SIGETTER(MNAME) \
150 XCCDF_SITERATOR_GETTER(item,MNAME,item.MNAME) \
151 XCCDF_SITERATOR_GETTER(benchmark,MNAME,item.MNAME) \
152 XCCDF_SITERATOR_GETTER(profile,MNAME,item.MNAME) \
153 XCCDF_SITERATOR_GETTER(rule,MNAME,item.MNAME) \
154 XCCDF_SITERATOR_GETTER(value,MNAME,item.MNAME) \
155 XCCDF_SITERATOR_GETTER(group,MNAME,item.MNAME) \
156 XCCDF_SITERATOR_GETTER(result,MNAME,item.MNAME)
157 #define XCCDF_ITEM_TIGETTER(MNAME) \
158 XCCDF_TITERATOR_GETTER(item,MNAME,item.MNAME) \
159 XCCDF_TITERATOR_GETTER(benchmark,MNAME,item.MNAME) \
160 XCCDF_TITERATOR_GETTER(profile,MNAME,item.MNAME) \
161 XCCDF_TITERATOR_GETTER(rule,MNAME,item.MNAME) \
162 XCCDF_TITERATOR_GETTER(value,MNAME,item.MNAME) \
163 XCCDF_TITERATOR_GETTER(group,MNAME,item.MNAME) \
164 XCCDF_TITERATOR_GETTER(result,MNAME,item.MNAME)
165 #define XCCDF_FLAG_SETTER(SNAME, FNAME) \
166 bool xccdf_##SNAME##_set_##FNAME(struct xccdf_##SNAME *item, bool newval) \
167 { assert(item != NULL); XITEM(item)->item.flags.FNAME = newval; return true; }
168 #define XCCDF_FLAG_GETTER(MNAME) \
169 XCCDF_ABSTRACT_GETTER(bool,item,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(item,MNAME) \
170 XCCDF_BENCHMARK_GETTER_A(bool,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(benchmark,MNAME) \
171 XCCDF_PROFILE_GETTER_A(bool,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(profile,MNAME) \
172 XCCDF_RULE_GETTER_A(bool,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(rule,MNAME) \
173 XCCDF_GROUP_GETTER_A(bool,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(group,MNAME) \
174 XCCDF_VALUE_GETTER_A(bool,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(value,MNAME) \
175 XCCDF_RESULT_GETTER_A(bool,MNAME,item.flags.MNAME) XCCDF_FLAG_SETTER(result,MNAME)
176
177 #define XCCDF_ITEM_SETTER_ONE(STYPE,MNAME,MTYPE,DUP) \
178 bool xccdf_##STYPE##_set_##MNAME(struct xccdf_##STYPE* item, MTYPE newval) \
179 { XITEM(item)->item.MNAME = DUP(newval); return true; }
180 #define XCCDF_ITEM_SETTER_ONEF(STYPE,MNAME,MTYPE,FREE,DUP) \
181 bool xccdf_##STYPE##_set_##MNAME(struct xccdf_##STYPE* item, MTYPE newval) \
182 { FREE(XITEM(item)->item.MNAME); XITEM(item)->item.MNAME = DUP(newval); return true; }
183 #define XCCDF_ITEM_SETTER_ONES(STYPE,MNAME) XCCDF_ITEM_SETTER_ONEF(STYPE,MNAME,const char*,free,oscap_strdup)
184 #define XCCDF_ITEM_SETTER_SIMPLE(MTYPE,MNAME) XCCDF_ITEM_SETTER_ONE(item,MNAME,MTYPE,) \
185 XCCDF_ITEM_SETTER_ONE(benchmark,MNAME,MTYPE,) XCCDF_ITEM_SETTER_ONE(profile,MNAME,MTYPE,) \
186 XCCDF_ITEM_SETTER_ONE(rule,MNAME,MTYPE,) XCCDF_ITEM_SETTER_ONE(value,MNAME,MTYPE,) \
187 XCCDF_ITEM_SETTER_ONE(group,MNAME,MTYPE,) XCCDF_ITEM_SETTER_ONE(result,MNAME,MTYPE,)
188 #define XCCDF_ITEM_SETTER_STRING(MNAME) XCCDF_ITEM_SETTER_ONES(item,MNAME) \
189 XCCDF_ITEM_SETTER_ONES(benchmark,MNAME) XCCDF_ITEM_SETTER_ONES(profile,MNAME) \
190 XCCDF_ITEM_SETTER_ONES(rule,MNAME) XCCDF_ITEM_SETTER_ONES(value,MNAME) \
191 XCCDF_ITEM_SETTER_ONES(group,MNAME) XCCDF_ITEM_SETTER_ONES(result,MNAME)
192
193 #define XCCDF_ITEM_ADDER_ONE(STYPE,MNAME,SINGNAME,MTYPE,DUP) \
194 bool xccdf_##STYPE##_add_##SINGNAME(struct xccdf_##STYPE* item, MTYPE newval) \
195 { return oscap_list_add(XITEM(item)->item.MNAME, DUP(newval)); }
196 #define XCCDF_ITEM_ADDER_ONES(STYPE,MNAME,SINGNAME) XCCDF_ITEM_ADDER_ONE(STYPE,MNAME,SINGNAME,const char*,oscap_strdup)
197 #define XCCDF_ITEM_ADDER_STRING(MNAMES,MNAME) XCCDF_ITEM_ADDER_ONES(item,MNAME,MNAMES) \
198 XCCDF_ITEM_ADDER_ONES(benchmark,MNAME,MNAMES) XCCDF_ITEM_ADDER_ONES(profile,MNAME,MNAMES) \
199 XCCDF_ITEM_ADDER_ONES(group,MNAME,MNAMES) XCCDF_ITEM_ADDER_ONES(rule,MNAME,MNAMES) \
200 XCCDF_ITEM_ADDER_ONES(value,MNAME,MNAMES) XCCDF_ITEM_ADDER_ONES(result,MNAME,MNAMES)
201 #define XCCDF_ITEM_ADDER(MTYPE,MNAMES,MNAME) XCCDF_ITEM_ADDER_ONE(item,MNAME,MNAMES,MTYPE,) \
202 XCCDF_ITEM_ADDER_ONE(benchmark,MNAME,MNAMES,MTYPE,) XCCDF_ITEM_ADDER_ONE(profile,MNAME,MNAMES,MTYPE,) \
203 XCCDF_ITEM_ADDER_ONE(group,MNAME,MNAMES,MTYPE,) XCCDF_ITEM_ADDER_ONE(rule,MNAME,MNAMES,MTYPE,) \
204 XCCDF_ITEM_ADDER_ONE(value,MNAME,MNAMES,MTYPE,) XCCDF_ITEM_ADDER_ONE(result,MNAME,MNAMES,MTYPE,)
205
206 #define XCCDF_ITEM_ADDER_REG(STYPE, MTYPE, MNAME) \
207 bool xccdf_##STYPE##_add_##MTYPE(struct xccdf_##STYPE *STYPE, struct xccdf_##MTYPE *item) \
208 { return xccdf_add_item(XITEM(STYPE)->sub.STYPE.MNAME, XITEM(STYPE), XITEM(item), #MTYPE "-"); }
209
210 #define XITERATOR(x) ((struct oscap_iterator*)(x))
211 #define XCCDF_ITERATOR(n) struct xccdf_##n##_iterator*
212 #define XCCDF_ITERATOR_FWD(n) struct xccdf_##n##_iterator;
213 #define XCCDF_ITERATOR_HAS_MORE(n) bool xccdf_##n##_iterator_has_more(XCCDF_ITERATOR(n) it) { return oscap_iterator_has_more(XITERATOR(it)); }
214 #define XCCDF_ITERATOR_NEXT(t,n) t xccdf_##n##_iterator_next(XCCDF_ITERATOR(n) it) { return oscap_iterator_next(XITERATOR(it)); }
215 #define XCCDF_ITERATOR_FREE(n) void xccdf_##n##_iterator_free(XCCDF_ITERATOR(n) it) { oscap_iterator_free(XITERATOR(it)); }
216 #define XCCDF_ITERATOR_RESET(n) void xccdf_##n##_iterator_reset(XCCDF_ITERATOR(n) it) { oscap_iterator_reset(XITERATOR(it)); }
217 #define XCCDF_ITERATOR_GEN_T(t,n) XCCDF_ITERATOR_FWD(n) XCCDF_ITERATOR_HAS_MORE(n) XCCDF_ITERATOR_RESET(n) XCCDF_ITERATOR_NEXT(t,n) OSCAP_ITERATOR_REMOVE_F(xccdf_##n) XCCDF_ITERATOR_FREE(n)
218 #define XCCDF_ITERATOR_GEN_S(n) XCCDF_ITERATOR_GEN_T(struct xccdf_##n*,n)
219 #define XCCDF_ITERATOR_GEN_TEXT(n) XCCDF_ITERATOR_GEN_T(struct oscap_text *,n)
220
221 #define XCCDF_FREE_GEN(TYPE) void xccdf_##TYPE##_free(struct xccdf_##TYPE* item) { xccdf_##TYPE##_free_impl((struct xccdf_item *)item); }
222
223 #endif