| 1 | /** |
| 2 | * @addtogroup CPE |
| 3 | * @{ |
| 4 | * @addtogroup cpelang CPE Language |
| 5 | * @{ |
| 6 | * |
| 7 | * @image html cpe_language.png "Class diagram" |
| 8 | * @file cpe_lang.h |
| 9 | * |
| 10 | * @brief Interface to Common Platform Enumeration (CPE) Language |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina. |
| 15 | * All Rights Reserved. |
| 16 | * |
| 17 | * This library is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU Lesser General Public |
| 19 | * License as published by the Free Software Foundation; either |
| 20 | * version 2.1 of the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This library is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 25 | * Lesser General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU Lesser General Public |
| 28 | * License along with this library; if not, write to the Free Software |
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 | * |
| 31 | * Authors: |
| 32 | * Lukas Kuklinek <[email protected]> |
| 33 | * Maros Barabas <[email protected]> |
| 34 | */ |
| 35 | |
| 36 | #ifndef CPELANG_H_ |
| 37 | #define CPELANG_H_ |
| 38 | |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #include "cpe_name.h" |
| 42 | #include "oscap.h" |
| 43 | #include "oscap_text.h" |
| 44 | #include "oscap_source.h" |
| 45 | #include "oscap_export.h" |
| 46 | |
| 47 | /** |
| 48 | * CPE language operators |
| 49 | */ |
| 50 | typedef enum { |
| 51 | CPE_LANG_OPER_INVALID = 0x00, ///< invalid or unknown operation |
| 52 | CPE_LANG_OPER_AND = 0x01, ///< logical AND |
| 53 | CPE_LANG_OPER_OR = 0x02, ///< logical OR |
| 54 | CPE_LANG_OPER_MATCH = 0x04, ///< fact-ref = match given CPE name against available dictionaries |
| 55 | CPE_LANG_OPER_CHECK = 0x08, ///< check-ref = evaluate given check |
| 56 | |
| 57 | CPE_LANG_OPER_MASK = 0xFF, ///< mask to extract the operator w/o possible negation |
| 58 | CPE_LANG_OPER_NOT = 0x100, ///< negate |
| 59 | |
| 60 | CPE_LANG_OPER_NAND = CPE_LANG_OPER_AND | CPE_LANG_OPER_NOT, |
| 61 | CPE_LANG_OPER_NOR = CPE_LANG_OPER_OR | CPE_LANG_OPER_NOT, |
| 62 | } cpe_lang_oper_t; |
| 63 | |
| 64 | /** |
| 65 | * @struct cpe_lang_model |
| 66 | * CPE platform specification |
| 67 | */ |
| 68 | struct cpe_lang_model; |
| 69 | |
| 70 | /** |
| 71 | * @struct cpe_platform |
| 72 | * Single platform representation in CPE language |
| 73 | */ |
| 74 | struct cpe_platform; |
| 75 | |
| 76 | /** |
| 77 | * @struct cpe_testexpr |
| 78 | * CPE language boolean expression |
| 79 | */ |
| 80 | struct cpe_testexpr; |
| 81 | |
| 82 | |
| 83 | /************************************************************/ |
| 84 | /** |
| 85 | * @name Iterators |
| 86 | * @{ |
| 87 | * */ |
| 88 | |
| 89 | /** |
| 90 | * @struct cpe_platform_iterator |
| 91 | * Iterator over CPE dictionary items. |
| 92 | * @see oscap_iterator |
| 93 | */ |
| 94 | struct cpe_platform_iterator; |
| 95 | |
| 96 | /// @memberof cpe_platform_iterator |
| 97 | OSCAP_API struct cpe_platform *cpe_platform_iterator_next(struct cpe_platform_iterator *it); |
| 98 | /// @memberof cpe_platform_iterator |
| 99 | OSCAP_API bool cpe_platform_iterator_has_more(struct cpe_platform_iterator *it); |
| 100 | /// @memberof cpe_platform_iterator |
| 101 | OSCAP_API void cpe_platform_iterator_free(struct cpe_platform_iterator *it); |
| 102 | |
| 103 | /** |
| 104 | * @struct cpe_testexpr_iterator |
| 105 | * Iterator over CPE language expressions. |
| 106 | * @see oscap_iterator |
| 107 | */ |
| 108 | struct cpe_testexpr_iterator; |
| 109 | /// @memberof cpe_testexpr_iterator |
| 110 | OSCAP_API struct cpe_testexpr *cpe_testexpr_iterator_next(struct cpe_testexpr_iterator *it); |
| 111 | /// @memberof cpe_testexpr_iterator |
| 112 | OSCAP_API bool cpe_testexpr_iterator_has_more(struct cpe_testexpr_iterator *it); |
| 113 | /// @memberof cpe_testexpr_iterator |
| 114 | OSCAP_API void cpe_testexpr_iterator_free(struct cpe_testexpr_iterator *it); |
| 115 | |
| 116 | /************************************************************/ |
| 117 | /** @} End of Iterators group */ |
| 118 | |
| 119 | /************************************************************/ |
| 120 | /** |
| 121 | * @name Getters |
| 122 | * Return value is pointer to structure's member. Do not free unless you null the pointer in the structure. |
| 123 | * Use remove function otherwise. |
| 124 | * @{ |
| 125 | * */ |
| 126 | |
| 127 | /** |
| 128 | * cpe_testexpr functions to get variable members |
| 129 | * @memberof cpe_testexpr |
| 130 | */ |
| 131 | OSCAP_API cpe_lang_oper_t cpe_testexpr_get_oper(const struct cpe_testexpr *item); |
| 132 | |
| 133 | /** |
| 134 | * Get CPE expression subexpression. |
| 135 | * Not valid for CPE_LANG_OPER_MATCH operation. |
| 136 | * @memberof cpe_testexpr |
| 137 | */ |
| 138 | OSCAP_API struct cpe_testexpr_iterator *cpe_testexpr_get_meta_expr(const struct cpe_testexpr *item); |
| 139 | |
| 140 | /** |
| 141 | * Get CPE name to match against. |
| 142 | * Only valid for CPE_LANG_OPER_MATCH. |
| 143 | * @memberof cpe_testexpr |
| 144 | */ |
| 145 | OSCAP_API const struct cpe_name *cpe_testexpr_get_meta_cpe(const struct cpe_testexpr *item); |
| 146 | |
| 147 | /** |
| 148 | * Get check system to evaluate |
| 149 | * Only valid for CPE_LANG_OPER_CHECK. |
| 150 | * @memberof cpe_testexpr |
| 151 | */ |
| 152 | OSCAP_API const char* cpe_testexpr_get_meta_check_system(const struct cpe_testexpr *item); |
| 153 | |
| 154 | /** |
| 155 | * Get check href to evaluate |
| 156 | * Only valid for CPE_LANG_OPER_CHECK. |
| 157 | * @memberof cpe_testexpr |
| 158 | */ |
| 159 | OSCAP_API const char* cpe_testexpr_get_meta_check_href(const struct cpe_testexpr *item); |
| 160 | |
| 161 | /** |
| 162 | * Get check idref to evaluate |
| 163 | * Only valid for CPE_LANG_OPER_CHECK. |
| 164 | * @memberof cpe_testexpr |
| 165 | */ |
| 166 | OSCAP_API const char* cpe_testexpr_get_meta_check_id(const struct cpe_testexpr *item); |
| 167 | |
| 168 | /** |
| 169 | * Function to get next expr from array |
| 170 | * @param expr CPE Test expression structure |
| 171 | * @memberof cpe_testexpr |
| 172 | */ |
| 173 | OSCAP_API const struct cpe_testexpr *cpe_testexpr_get_next(const struct cpe_testexpr *expr); |
| 174 | |
| 175 | /** |
| 176 | * cpe_lang_model function to get CPE platforms |
| 177 | * @memberof cpe_lang_model |
| 178 | */ |
| 179 | OSCAP_API struct cpe_platform_iterator *cpe_lang_model_get_platforms(const struct cpe_lang_model *item); |
| 180 | |
| 181 | /** |
| 182 | * cpe_lang_model function to get CPE platforms |
| 183 | * @memberof cpe_lang_model |
| 184 | */ |
| 185 | OSCAP_API struct cpe_platform *cpe_lang_model_get_item(const struct cpe_lang_model *item, const char *key); |
| 186 | |
| 187 | /** |
| 188 | * Verify whether given CPE platform idref is applicable by evaluating test expression associated with it |
| 189 | * |
| 190 | * @memberof cpe_lang_model |
| 191 | * @param platform idref to the platform to verify (do not pass with "#" prepended) |
| 192 | * @param lang_model used CPE language model |
| 193 | * @return true if lang model contains given platform and the platform is applicable |
| 194 | */ |
| 195 | OSCAP_API bool cpe_platform_applicable_lang_model(const char* platform, struct cpe_lang_model *lang_model, cpe_check_fn check_cb, cpe_dict_fn dict_cb, void* usr); |
| 196 | |
| 197 | /** |
| 198 | * cpe_platform functions to get id |
| 199 | * @memberof cpe_platform |
| 200 | */ |
| 201 | OSCAP_API const char *cpe_platform_get_id(const struct cpe_platform *item); |
| 202 | /** |
| 203 | * cpe_platform functions to get remark |
| 204 | * @memberof cpe_platform |
| 205 | */ |
| 206 | OSCAP_API const char *cpe_platform_get_remark(const struct cpe_platform *item); |
| 207 | /** |
| 208 | * cpe_platform functions to get titles |
| 209 | * @memberof cpe_platform |
| 210 | */ |
| 211 | OSCAP_API struct oscap_text_iterator *cpe_platform_get_titles(const struct cpe_platform *item); |
| 212 | /** |
| 213 | * cpe_platform functions to get test expression |
| 214 | * @memberof cpe_platform |
| 215 | */ |
| 216 | OSCAP_API const struct cpe_testexpr *cpe_platform_get_expr(const struct cpe_platform *item); |
| 217 | |
| 218 | /************************************************************/ |
| 219 | /** @} End of Getters group */ |
| 220 | |
| 221 | /************************************************************/ |
| 222 | /** |
| 223 | * @name Setters |
| 224 | * For lists use add functions. Parameters of set functions are duplicated in memory and need to |
| 225 | * be freed by caller. |
| 226 | * @{ |
| 227 | */ |
| 228 | |
| 229 | /** |
| 230 | * Add platform to CPE lang model |
| 231 | * @memberof cpe_lang_model |
| 232 | */ |
| 233 | OSCAP_API bool cpe_lang_model_add_platform(struct cpe_lang_model *lang, struct cpe_platform *platform); |
| 234 | /** |
| 235 | * Add title to platform |
| 236 | * @memberof cpe_platform |
| 237 | */ |
| 238 | OSCAP_API bool cpe_platform_add_title(struct cpe_platform *platform, struct oscap_text *title); |
| 239 | |
| 240 | /** |
| 241 | * Add XML namespace to CPE lang model |
| 242 | * @memberof cpe_lang_model |
| 243 | */ |
| 244 | /*bool cpe_lang_model_add_xmlns(struct cpe_lang_model * model, struct xml_metadata * xml);*/ |
| 245 | |
| 246 | /** |
| 247 | * Add an subexpression to a compound CPE language expression. |
| 248 | * |
| 249 | * Expression has to be of type CPE_LANG_OPER_AND or CPE_LANG_OPER_OR, possibly with negation. |
| 250 | * @memberof cpe_testexpr |
| 251 | */ |
| 252 | OSCAP_API bool cpe_testexpr_add_subexpression(struct cpe_testexpr *expr, struct cpe_testexpr *sub); |
| 253 | |
| 254 | /** |
| 255 | * Set ID of CPE platform |
| 256 | * @memberof cpe_platform |
| 257 | */ |
| 258 | OSCAP_API bool cpe_platform_set_id(struct cpe_platform *platform, const char *new_id); |
| 259 | /** |
| 260 | * Set remark of CPE platform |
| 261 | * @memberof cpe_platform |
| 262 | */ |
| 263 | OSCAP_API bool cpe_platform_set_remark(struct cpe_platform *platform, const char *new_remark); |
| 264 | /** |
| 265 | * Set evaluation expression for this CPE platform. |
| 266 | * |
| 267 | * Expression has to be a logical-test (i.e. its operation shall be AND or OR, possibly with negation) |
| 268 | * @memberof cpe_platform |
| 269 | */ |
| 270 | OSCAP_API bool cpe_platform_set_expr(struct cpe_platform *platform, struct cpe_testexpr *expr); |
| 271 | /** |
| 272 | * Set CPE operation. |
| 273 | * |
| 274 | * Any subexpressions and CPE names associated with this expression will be removed. |
| 275 | * @memberof cpe_testexpr |
| 276 | */ |
| 277 | OSCAP_API bool cpe_testexpr_set_oper(struct cpe_testexpr *expr, cpe_lang_oper_t oper); |
| 278 | |
| 279 | /** |
| 280 | * Set CPE name. |
| 281 | * |
| 282 | * If the expression does not have type CPE_LANG_OPER_MATCH, |
| 283 | * this function has no effect and returns false. |
| 284 | * @memberof cpe_testexpr |
| 285 | */ |
| 286 | OSCAP_API bool cpe_testexpr_set_name(struct cpe_testexpr *expr, struct cpe_name *name); |
| 287 | |
| 288 | /************************************************************/ |
| 289 | /** @} End of Setters group */ |
| 290 | |
| 291 | /// @memberof cpe_platform_iterator |
| 292 | OSCAP_API void cpe_platform_iterator_remove(struct cpe_platform_iterator *it, struct cpe_lang_model *parent); |
| 293 | /// @memberof cpe_testexpr_iterator |
| 294 | OSCAP_API void cpe_platform_iterator_reset(struct cpe_platform_iterator *it); |
| 295 | |
| 296 | /** |
| 297 | * Constructor of CPE Language model |
| 298 | * @memberof cpe_lang_model |
| 299 | */ |
| 300 | OSCAP_API struct cpe_lang_model *cpe_lang_model_new(void); |
| 301 | |
| 302 | /** |
| 303 | * Constructor of CPE test expression |
| 304 | * @memberof cpe_testexpr |
| 305 | */ |
| 306 | OSCAP_API struct cpe_testexpr *cpe_testexpr_new(void); |
| 307 | |
| 308 | /** |
| 309 | * Constructor of CPE Platform |
| 310 | * @memberof cpe_platform |
| 311 | */ |
| 312 | OSCAP_API struct cpe_platform *cpe_platform_new(void); |
| 313 | |
| 314 | /** |
| 315 | * Clone CPE test expression |
| 316 | * @param old_expr CPE test expression |
| 317 | * @memberof cpe_testexpr |
| 318 | */ |
| 319 | OSCAP_API struct cpe_testexpr * cpe_testexpr_clone(struct cpe_testexpr * old_expr); |
| 320 | |
| 321 | /** |
| 322 | * Free function of CPE test expression |
| 323 | * @memberof cpe_testexpr |
| 324 | */ |
| 325 | OSCAP_API void cpe_testexpr_free(struct cpe_testexpr *expr); |
| 326 | |
| 327 | /** |
| 328 | * Reset function of CPE test expression |
| 329 | * @memberof cpe_testexpr |
| 330 | */ |
| 331 | OSCAP_API void cpe_testexpr_iterator_reset(struct cpe_testexpr_iterator *it); |
| 332 | |
| 333 | /** |
| 334 | * Free function of CPE test expression |
| 335 | * @memberof cpe_lang_model |
| 336 | */ |
| 337 | OSCAP_API void cpe_lang_model_free(struct cpe_lang_model *platformspec); |
| 338 | |
| 339 | /** |
| 340 | * Free function of CPE Platform |
| 341 | * @memberof cpe_platform |
| 342 | */ |
| 343 | OSCAP_API void cpe_platform_free(struct cpe_platform *platform); |
| 344 | |
| 345 | /************************************************************/ |
| 346 | /** |
| 347 | * @name Evaluators |
| 348 | * @{ |
| 349 | * */ |
| 350 | |
| 351 | /** |
| 352 | * Get supported version of CPE language XML |
| 353 | * @return version of XML file format |
| 354 | * @memberof cpe_lang_model |
| 355 | */ |
| 356 | OSCAP_API const char * cpe_lang_model_supported(void); |
| 357 | |
| 358 | /************************************************************/ |
| 359 | /** @} End of Evaluators group */ |
| 360 | |
| 361 | /** |
| 362 | * Load CPE language model from an oscap_source. |
| 363 | * @memberof cpe_lang_model |
| 364 | */ |
| 365 | OSCAP_API struct cpe_lang_model *cpe_lang_model_import_source(struct oscap_source *source); |
| 366 | |
| 367 | /** |
| 368 | * Write the lang_model to a file. |
| 369 | * @memberof cpe_lang_model |
| 370 | * @param spec CPE lang model |
| 371 | * @param file filename |
| 372 | */ |
| 373 | OSCAP_API void cpe_lang_model_export(const struct cpe_lang_model *spec, const char *file); |
| 374 | |
| 375 | /**@}*/ |
| 376 | |
| 377 | /**@}*/ |
| 378 | |
| 379 | #endif /* _CPELANG_H_ */ |