| 1 | /** |
| 2 | * @addtogroup CPE |
| 3 | * @{ |
| 4 | * @addtogroup cpeuri CPE URIs |
| 5 | * @{ |
| 6 | * |
| 7 | * |
| 8 | * @file cpe_name.h |
| 9 | * \brief Interface to Common Platform Enumeration (CPE) URI |
| 10 | * |
| 11 | * See more details at http://nvd.nist.gov/cpe.cfm |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 17 | * All Rights Reserved. |
| 18 | * |
| 19 | * This library is free software; you can redistribute it and/or |
| 20 | * modify it under the terms of the GNU Lesser General Public |
| 21 | * License as published by the Free Software Foundation; either |
| 22 | * version 2.1 of the License, or (at your option) any later version. |
| 23 | * |
| 24 | * This library is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 27 | * Lesser General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU Lesser General Public |
| 30 | * License along with this library; if not, write to the Free Software |
| 31 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 | * |
| 33 | * Authors: |
| 34 | * Lukas Kuklinek <[email protected]> |
| 35 | */ |
| 36 | |
| 37 | #ifndef _CPEURI_H_ |
| 38 | #define _CPEURI_H_ |
| 39 | |
| 40 | #include <stdbool.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <stdio.h> |
| 43 | #include "oscap_export.h" |
| 44 | |
| 45 | /// enumeration of possible CPE parts |
| 46 | typedef enum { |
| 47 | CPE_PART_NONE, ///< no part specified |
| 48 | CPE_PART_HW, ///< hardware |
| 49 | CPE_PART_OS, ///< operating system |
| 50 | CPE_PART_APP ///< application |
| 51 | } cpe_part_t; |
| 52 | |
| 53 | typedef enum { |
| 54 | CPE_FORMAT_UNKNOWN, ///< can't be serialized, is usually used to signal errors |
| 55 | CPE_FORMAT_URI, ///< available in all CPE versions, default in CPE 2.2 and previous |
| 56 | CPE_FORMAT_STRING, ///< available in CPE 2.3 and newer |
| 57 | CPE_FORMAT_WFN ///< available in CPE 2.3 and newer versions, default in CPE 2.3 and newer |
| 58 | } cpe_format_t; |
| 59 | |
| 60 | /** |
| 61 | * @struct cpe_name |
| 62 | * Structure holding Common Platform Enumeration URI data. |
| 63 | * |
| 64 | * Empty components are set to NULL. |
| 65 | */ |
| 66 | struct cpe_name; |
| 67 | |
| 68 | /** |
| 69 | * Create a new CPE structure from string @a cpe. |
| 70 | * |
| 71 | * @memberof cpe_name |
| 72 | * @note If @a cpe is NULL, empty cpe will be created. |
| 73 | * @param cpe CPE URI string to be parsed |
| 74 | * @return new structure holding parsed data |
| 75 | * @retval NULL on failure |
| 76 | */ |
| 77 | OSCAP_API struct cpe_name *cpe_name_new(const char *cpe); |
| 78 | |
| 79 | /** |
| 80 | * Clone CPE Name |
| 81 | * @param old_name CPE name |
| 82 | * @memberof cpe_name |
| 83 | */ |
| 84 | OSCAP_API struct cpe_name * cpe_name_clone(struct cpe_name * old_name); |
| 85 | |
| 86 | /** |
| 87 | * Destructor. Frees any used resources and safely destroys @a cpe. |
| 88 | * @memberof cpe_name |
| 89 | * @param cpe CPE to be deleted |
| 90 | */ |
| 91 | OSCAP_API void cpe_name_free(struct cpe_name *cpe); |
| 92 | |
| 93 | /************************************************************/ |
| 94 | /** |
| 95 | * @name Getters |
| 96 | * Return value is pointer to structure's member. Do not free unless you null the pointer in the structure. |
| 97 | * Use remove function otherwise. |
| 98 | * @{ |
| 99 | * */ |
| 100 | |
| 101 | /** |
| 102 | * Get how the CPE name was loaded and how it should be saved |
| 103 | * @memberof cpe_name |
| 104 | */ |
| 105 | OSCAP_API cpe_format_t cpe_name_get_format(const struct cpe_name *cpe); |
| 106 | |
| 107 | /** |
| 108 | * Get CPE name part type field. |
| 109 | * @memberof cpe_name |
| 110 | */ |
| 111 | OSCAP_API cpe_part_t cpe_name_get_part(const struct cpe_name *cpe); |
| 112 | |
| 113 | /** |
| 114 | * Get CPE name vendor field. |
| 115 | * @memberof cpe_name |
| 116 | */ |
| 117 | OSCAP_API const char *cpe_name_get_vendor(const struct cpe_name *cpe); |
| 118 | |
| 119 | /** |
| 120 | * Get CPE name product field. |
| 121 | * @memberof cpe_name |
| 122 | */ |
| 123 | OSCAP_API const char *cpe_name_get_product(const struct cpe_name *cpe); |
| 124 | |
| 125 | /** |
| 126 | * Get CPE name version field. |
| 127 | * @memberof cpe_name |
| 128 | */ |
| 129 | OSCAP_API const char *cpe_name_get_version(const struct cpe_name *cpe); |
| 130 | |
| 131 | /** |
| 132 | * Get CPE name update field. |
| 133 | * @memberof cpe_name |
| 134 | */ |
| 135 | OSCAP_API const char *cpe_name_get_update(const struct cpe_name *cpe); |
| 136 | |
| 137 | /** |
| 138 | * Get CPE name edition field. |
| 139 | * @memberof cpe_name |
| 140 | */ |
| 141 | OSCAP_API const char *cpe_name_get_edition(const struct cpe_name *cpe); |
| 142 | |
| 143 | /** |
| 144 | * Get CPE name language field. |
| 145 | * @memberof cpe_name |
| 146 | */ |
| 147 | OSCAP_API const char *cpe_name_get_language(const struct cpe_name *cpe); |
| 148 | |
| 149 | /** |
| 150 | * Get CPE name sw_edition field. |
| 151 | * @memberof cpe_name |
| 152 | */ |
| 153 | OSCAP_API const char *cpe_name_get_sw_edition(const struct cpe_name *cpe); |
| 154 | |
| 155 | /** |
| 156 | * Get CPE name target_sw field. |
| 157 | * @memberof cpe_name |
| 158 | */ |
| 159 | OSCAP_API const char *cpe_name_get_target_sw(const struct cpe_name *cpe); |
| 160 | |
| 161 | /** |
| 162 | * Get CPE name target_hw field. |
| 163 | * @memberof cpe_name |
| 164 | */ |
| 165 | OSCAP_API const char *cpe_name_get_target_hw(const struct cpe_name *cpe); |
| 166 | |
| 167 | /** |
| 168 | * Get CPE name other field. |
| 169 | * @memberof cpe_name |
| 170 | */ |
| 171 | OSCAP_API const char *cpe_name_get_other(const struct cpe_name *cpe); |
| 172 | |
| 173 | /** |
| 174 | * Return CPE URI as a new string in specified format. |
| 175 | * @memberof cpe_name |
| 176 | * @note Returned string is newly allocated and is caller's responsibility to free it. |
| 177 | * @param cpe CPE to be converted |
| 178 | * @param format Which format should the string be in |
| 179 | * @return CPE URI as string |
| 180 | * @retval NULL on failure |
| 181 | */ |
| 182 | OSCAP_API char *cpe_name_get_as_format(const struct cpe_name *cpe, cpe_format_t format); |
| 183 | |
| 184 | /** |
| 185 | * Return CPE URI as a new string in the format in which it was loaded. |
| 186 | * @memberof cpe_name |
| 187 | * @note Returned string is newly allocated and is caller's responsibility to free it. |
| 188 | * @param cpe CPE to be converted |
| 189 | * @return CPE URI as string |
| 190 | * @retval NULL on failure |
| 191 | */ |
| 192 | OSCAP_API char *cpe_name_get_as_str(const struct cpe_name *cpe); |
| 193 | |
| 194 | /************************************************************/ |
| 195 | /** @} End of Getters group */ |
| 196 | |
| 197 | /************************************************************/ |
| 198 | /** |
| 199 | * @name Setters |
| 200 | * For lists use add functions. Parameters of set functions are duplicated in memory and need to |
| 201 | * be freed by caller. |
| 202 | * @{ |
| 203 | */ |
| 204 | |
| 205 | /** |
| 206 | * Set how the CPE name was loaded and how it should be saved |
| 207 | * @memberof cpe_name |
| 208 | */ |
| 209 | OSCAP_API bool cpe_name_set_format(struct cpe_name *cpe, cpe_format_t newval); |
| 210 | |
| 211 | /** |
| 212 | * Set CPE name part type field. |
| 213 | * @memberof cpe_name |
| 214 | */ |
| 215 | OSCAP_API bool cpe_name_set_part(struct cpe_name *cpe, cpe_part_t newval); |
| 216 | |
| 217 | /** |
| 218 | * Set CPE name vendor field. |
| 219 | * @memberof cpe_name |
| 220 | */ |
| 221 | OSCAP_API bool cpe_name_set_vendor(struct cpe_name *cpe, const char *newval); |
| 222 | |
| 223 | /** |
| 224 | * Set CPE name product field. |
| 225 | * @memberof cpe_name |
| 226 | */ |
| 227 | OSCAP_API bool cpe_name_set_product(struct cpe_name *cpe, const char *newval); |
| 228 | |
| 229 | /** |
| 230 | * Set CPE name version field. |
| 231 | * @memberof cpe_name |
| 232 | */ |
| 233 | OSCAP_API bool cpe_name_set_version(struct cpe_name *cpe, const char *newval); |
| 234 | |
| 235 | /** |
| 236 | * Set CPE name update field. |
| 237 | * @memberof cpe_name |
| 238 | */ |
| 239 | OSCAP_API bool cpe_name_set_update(struct cpe_name *cpe, const char *newval); |
| 240 | |
| 241 | /** |
| 242 | * Set CPE name edition field. |
| 243 | * @memberof cpe_name |
| 244 | */ |
| 245 | OSCAP_API bool cpe_name_set_edition(struct cpe_name *cpe, const char *newval); |
| 246 | |
| 247 | /** |
| 248 | * Set CPE name language field. |
| 249 | * @memberof cpe_name |
| 250 | */ |
| 251 | OSCAP_API bool cpe_name_set_language(struct cpe_name *cpe, const char *newval); |
| 252 | |
| 253 | /** |
| 254 | * Set CPE name sw_edition field. |
| 255 | * @memberof cpe_name |
| 256 | */ |
| 257 | OSCAP_API bool cpe_name_set_sw_edition(struct cpe_name *cpe, const char *newval); |
| 258 | |
| 259 | /** |
| 260 | * Set CPE name target_sw field. |
| 261 | * @memberof cpe_name |
| 262 | */ |
| 263 | OSCAP_API bool cpe_name_set_target_sw(struct cpe_name *cpe, const char *newval); |
| 264 | |
| 265 | /** |
| 266 | * Set CPE name target_hw field. |
| 267 | * @memberof cpe_name |
| 268 | */ |
| 269 | OSCAP_API bool cpe_name_set_target_hw(struct cpe_name *cpe, const char *newval); |
| 270 | |
| 271 | /** |
| 272 | * Set CPE name other field. |
| 273 | * @memberof cpe_name |
| 274 | */ |
| 275 | OSCAP_API bool cpe_name_set_other(struct cpe_name *cpe, const char *newval); |
| 276 | |
| 277 | /************************************************************/ |
| 278 | /** @} End of Setters group */ |
| 279 | |
| 280 | /************************************************************/ |
| 281 | /** |
| 282 | * @name Evaluators |
| 283 | * @{ |
| 284 | * */ |
| 285 | |
| 286 | /** |
| 287 | * Check if candidate CPE @a cpe matches CPE @a against |
| 288 | * according to CPE specification v 2.1. |
| 289 | * @memberof cpe_name |
| 290 | */ |
| 291 | OSCAP_API bool cpe_name_match_one(const struct cpe_name *cpe, const struct cpe_name *against); |
| 292 | |
| 293 | /** |
| 294 | * Write CPE URI @a cpe to file a descriptor @a f |
| 295 | * @memberof cpe_name |
| 296 | * @param cpe cpe to write |
| 297 | * @param f file descriptor to write CPE URI to |
| 298 | * @return number of written characters |
| 299 | * @retval <0 on failure |
| 300 | */ |
| 301 | OSCAP_API int cpe_name_write(const struct cpe_name *cpe, FILE * f); |
| 302 | |
| 303 | /** |
| 304 | * Looks at given string and returns format it is in |
| 305 | * |
| 306 | * CPE_FORMAT_UNKNOWN is used in case of errors |
| 307 | */ |
| 308 | OSCAP_API cpe_format_t cpe_name_get_format_of_str(const char *str); |
| 309 | |
| 310 | /** |
| 311 | * Checks whether @a str is valid CPE string (in any supported format). |
| 312 | * @memberof cpe_name |
| 313 | * @param str string to be validated |
| 314 | */ |
| 315 | OSCAP_API bool cpe_name_check(const char *str); |
| 316 | |
| 317 | /** |
| 318 | * Get supported version of CPE uri XML |
| 319 | * @return version of XML file format |
| 320 | * @memberof cpe_name |
| 321 | */ |
| 322 | OSCAP_API const char * cpe_name_supported(void); |
| 323 | |
| 324 | /************************************************************/ |
| 325 | /** @} End of Evaluators group */ |
| 326 | |
| 327 | /**@}*/ |
| 328 | |
| 329 | /**@}*/ |
| 330 | |
| 331 | /** |
| 332 | * Shared callback definition used to evaluate checks to perform applicability tests |
| 333 | * |
| 334 | * first arg = system |
| 335 | * second arg = href |
| 336 | * third arg = check name / id |
| 337 | * fourth arg = arbitrary pointer / user data |
| 338 | * |
| 339 | * returns true = applicable, false = not applicable |
| 340 | */ |
| 341 | typedef bool (*cpe_check_fn) (const char*, const char*, const char*, void*); |
| 342 | |
| 343 | /** |
| 344 | * Shared callback definition used to match CPE names to perform applicability tests |
| 345 | * |
| 346 | * first argument = cpe name to match |
| 347 | * second argument = arbitrary pointer / user data |
| 348 | * returns true = matched to existing applicable name, false = not matched/not applicable |
| 349 | */ |
| 350 | typedef bool (*cpe_dict_fn) (const struct cpe_name*, void*); |
| 351 | |
| 352 | #endif /* _CPEURI_H_ */ |