| 1 | /*! \file cpename.c |
| 2 | * \brief Interface to Common Platform Enumeration (CPE) URI |
| 3 | * |
| 4 | * See more details at http://nvd.nist.gov/cpe.cfm |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * Brandon Dixon <[email protected]> |
| 28 | * Lukas Kuklinek <[email protected]> |
| 29 | */ |
| 30 | |
| 31 | #define _BSD_SOURCE |
| 32 | #define _DEFAULT_SOURCE |
| 33 | |
| 34 | #ifdef HAVE_CONFIG_H |
| 35 | #include <config.h> |
| 36 | #endif |
| 37 | |
| 38 | #include <string.h> |
| 39 | #include <stdio.h> |
| 40 | #include <ctype.h> |
| 41 | |
| 42 | #include "cpe_name.h" |
| 43 | #include "common/util.h" |
| 44 | #include "common/oscap_pcre.h" |
| 45 | #include "oscap_helpers.h" |
| 46 | |
| 47 | #define CPE_URI_SUPPORTED "2.3" |
| 48 | |
| 49 | // enumeration of CPE URI fields (useful for indexing arrays) |
| 50 | enum cpe_field_t { |
| 51 | CPE_FIELD_TYPE = 0, |
| 52 | CPE_FIELD_VENDOR = 1, |
| 53 | CPE_FIELD_PRODUCT = 2, |
| 54 | CPE_FIELD_VERSION = 3, |
| 55 | CPE_FIELD_UPDATE = 4, |
| 56 | CPE_FIELD_EDITION = 5, |
| 57 | CPE_FIELD_LANGUAGE = 6, |
| 58 | CPE_BASIC_FIELDNUM = 7, |
| 59 | |
| 60 | // Extended attributes CPE 2.3 only but packed so they are not garbled |
| 61 | // by CPE 2.2-only tools |
| 62 | CPE_FIELD_SW_EDITION = 7, |
| 63 | CPE_FIELD_TARGET_SW = 8, |
| 64 | CPE_FIELD_TARGET_HW = 9, |
| 65 | CPE_FIELD_OTHER = 10, |
| 66 | |
| 67 | CPE_TOTAL_FIELDNUM = 11 |
| 68 | }; |
| 69 | |
| 70 | struct cpe_name { |
| 71 | // char *uri; // complete URI cache |
| 72 | cpe_format_t format; |
| 73 | cpe_part_t part; // part |
| 74 | char *vendor; // vendor |
| 75 | char *product; // product |
| 76 | char *version; // version |
| 77 | char *update; // update |
| 78 | /* also used to pack the 4 extended attributes to CPE 2.2 URIs |
| 79 | we always load these into the model so the packed format should |
| 80 | never appear in the edition field itself |
| 81 | */ |
| 82 | char *edition; |
| 83 | char *language; // language |
| 84 | |
| 85 | /* extended attributes */ |
| 86 | char *sw_edition; |
| 87 | char *target_sw; |
| 88 | char *target_hw; |
| 89 | char *other; |
| 90 | /* end of extended attributes */ |
| 91 | }; |
| 92 | |
| 93 | /* h - hardware |
| 94 | * o - OS |
| 95 | * a - application |
| 96 | */ |
| 97 | //static const char *CPE_PART_CHAR[] = { NULL, "h", "o", "a" }; |
| 98 | |
| 99 | static const struct oscap_string_map CPE_PART_MAP[] = { |
| 100 | { CPE_PART_HW, "h" }, |
| 101 | { CPE_PART_OS, "o" }, |
| 102 | { CPE_PART_APP, "a" }, |
| 103 | { CPE_PART_NONE, NULL } |
| 104 | }; |
| 105 | |
| 106 | char **cpe_uri_split(char *str, const char *delim); |
| 107 | static bool cpe_urldecode(char *str); |
| 108 | static bool cpestring_comp_decode(char *str); |
| 109 | bool cpe_name_check(const char *str); |
| 110 | static const char *cpe_get_field(const struct cpe_name *cpe, int idx); |
| 111 | static const char *as_str(const char *str); |
| 112 | static bool cpe_set_field(struct cpe_name *cpe, int idx, const char *newval); |
| 113 | |
| 114 | static int cpe_fields_num(const struct cpe_name *cpe) |
| 115 | { |
| 116 | __attribute__nonnull__(cpe); |
| 117 | |
| 118 | if (cpe == NULL) |
| 119 | return 0; |
| 120 | int maxnum = 0; |
| 121 | int i; |
| 122 | for (i = 0; i < CPE_TOTAL_FIELDNUM; ++i) |
| 123 | if (cpe_get_field(cpe, i)) |
| 124 | maxnum = i + 1; |
| 125 | return maxnum; |
| 126 | } |
| 127 | |
| 128 | static const char *cpe_get_field(const struct cpe_name *cpe, int idx) |
| 129 | { |
| 130 | __attribute__nonnull__(cpe); |
| 131 | |
| 132 | if (cpe == NULL) |
| 133 | return NULL; |
| 134 | |
| 135 | switch (idx) { |
| 136 | case 0: |
| 137 | return oscap_enum_to_string(CPE_PART_MAP, cpe->part); |
| 138 | case 1: |
| 139 | return cpe->vendor; |
| 140 | case 2: |
| 141 | return cpe->product; |
| 142 | case 3: |
| 143 | return cpe->version; |
| 144 | case 4: |
| 145 | return cpe->update; |
| 146 | case 5: |
| 147 | return cpe->edition; |
| 148 | case 6: |
| 149 | return cpe->language; |
| 150 | case 7: |
| 151 | return cpe->sw_edition; |
| 152 | case 8: |
| 153 | return cpe->target_sw; |
| 154 | case 9: |
| 155 | return cpe->target_hw; |
| 156 | case 10: |
| 157 | return cpe->other; |
| 158 | default: |
| 159 | assert(false); |
| 160 | return NULL; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | bool cpe_set_field(struct cpe_name * cpe, int idx, const char *newval) |
| 165 | { |
| 166 | |
| 167 | __attribute__nonnull__(cpe); |
| 168 | /*__attribute__nonnull__(newval); <-- can't be here, we want to set NULL */ |
| 169 | |
| 170 | if (cpe == NULL) |
| 171 | return false; |
| 172 | |
| 173 | char **fieldptr = NULL; |
| 174 | switch (idx) { |
| 175 | case 0: |
| 176 | cpe->part = oscap_string_to_enum(CPE_PART_MAP, newval); |
| 177 | return cpe->part != CPE_PART_NONE; |
| 178 | case 1: |
| 179 | fieldptr = &cpe->vendor; |
| 180 | break; |
| 181 | case 2: |
| 182 | fieldptr = &cpe->product; |
| 183 | break; |
| 184 | case 3: |
| 185 | fieldptr = &cpe->version; |
| 186 | break; |
| 187 | case 4: |
| 188 | fieldptr = &cpe->update; |
| 189 | break; |
| 190 | case 5: |
| 191 | fieldptr = &cpe->edition; |
| 192 | break; |
| 193 | case 6: |
| 194 | fieldptr = &cpe->language; |
| 195 | break; |
| 196 | case 7: |
| 197 | fieldptr = &cpe->sw_edition; |
| 198 | break; |
| 199 | case 8: |
| 200 | fieldptr = &cpe->target_sw; |
| 201 | break; |
| 202 | case 9: |
| 203 | fieldptr = &cpe->target_hw; |
| 204 | break; |
| 205 | case 10: |
| 206 | fieldptr = &cpe->other; |
| 207 | break; |
| 208 | |
| 209 | default: |
| 210 | assert(false); |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | free(*fieldptr); |
| 215 | if (newval && strcmp(newval, "") == 0) |
| 216 | newval = NULL; |
| 217 | if (newval != NULL) |
| 218 | *fieldptr = oscap_strdup(newval); |
| 219 | else |
| 220 | *fieldptr = NULL; |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | struct cpe_name *cpe_name_new(const char *cpestr) |
| 226 | { |
| 227 | |
| 228 | int i; |
| 229 | struct cpe_name *cpe; |
| 230 | |
| 231 | cpe_format_t format = cpe_name_get_format_of_str(cpestr); |
| 232 | if (cpestr && format == CPE_FORMAT_UNKNOWN) |
| 233 | return NULL; |
| 234 | |
| 235 | cpe = malloc(sizeof(struct cpe_name)); |
| 236 | if (cpe == NULL) |
| 237 | return NULL; |
| 238 | memset(cpe, 0, sizeof(struct cpe_name)); // zero memory |
| 239 | |
| 240 | // remember the detected format so that we save with the same format |
| 241 | cpe_name_set_format(cpe, format); |
| 242 | |
| 243 | if (cpestr) { |
| 244 | if (format == CPE_FORMAT_URI) { |
| 245 | char *data_ = oscap_strdup(cpestr + 5); // without 'cpe:/' |
| 246 | char **fields_ = oscap_split(data_, ":"); |
| 247 | for (i = 0; fields_[i]; ++i) |
| 248 | { |
| 249 | if (i == CPE_FIELD_EDITION) |
| 250 | { |
| 251 | // extended properties may be packed in "edition" field |
| 252 | if (strlen(fields_[i]) >= 2 && fields_[i][0] == '~') { |
| 253 | // first character is ~, that means that extended |
| 254 | // attributes are embedded into "edition" field |
| 255 | |
| 256 | char **extended_attribs = oscap_split(fields_[i] + 1 * sizeof(char), "~"); |
| 257 | // the first extended attribute is actually the edition |
| 258 | fields_[i] = extended_attribs[0]; |
| 259 | // the rest are ~-encoded extended attributes |
| 260 | for (int j = 0; j < 5 && extended_attribs[1 + j] != NULL; ++j) { |
| 261 | if (!cpe_urldecode(extended_attribs[1 + j])) { |
| 262 | free(extended_attribs); |
| 263 | free(data_); |
| 264 | free(fields_); |
| 265 | cpe_name_free(cpe); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | cpe_set_field(cpe, CPE_BASIC_FIELDNUM + j, extended_attribs[1 + j]); |
| 270 | } |
| 271 | |
| 272 | free(extended_attribs); // we have used all the pointed to data |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (!cpe_urldecode(fields_[i])) { |
| 277 | free(data_); |
| 278 | free(fields_); |
| 279 | cpe_name_free(cpe); |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | cpe_set_field(cpe, i, fields_[i]); |
| 284 | } |
| 285 | |
| 286 | free(data_); |
| 287 | free(fields_); |
| 288 | } |
| 289 | else if (format == CPE_FORMAT_STRING) { |
| 290 | char *data_ = oscap_strdup(cpestr + 8); // without 'cpe:2.3:' |
| 291 | char **fields_ = oscap_split(data_, ":"); |
| 292 | for (i = 0; fields_[i] && i < CPE_TOTAL_FIELDNUM; ++i) |
| 293 | { |
| 294 | if (!cpestring_comp_decode(fields_[i])) { |
| 295 | free(data_); |
| 296 | free(fields_); |
| 297 | cpe_name_free(cpe); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
| 301 | cpe_set_field(cpe, i, fields_[i]); |
| 302 | } |
| 303 | |
| 304 | free(data_); |
| 305 | free(fields_); |
| 306 | } else if (format == CPE_FORMAT_WFN) { |
| 307 | /* FIXME: Do something here or remove the branch */ |
| 308 | } |
| 309 | } |
| 310 | return cpe; |
| 311 | } |
| 312 | |
| 313 | struct cpe_name * cpe_name_clone(struct cpe_name * old_name) |
| 314 | { |
| 315 | struct cpe_name * new_name = malloc(sizeof(struct cpe_name)); |
| 316 | if (new_name == NULL) |
| 317 | return NULL; |
| 318 | |
| 319 | new_name->format = old_name->format; |
| 320 | new_name->part = old_name->part; |
| 321 | new_name->vendor = oscap_strdup(old_name->vendor); |
| 322 | new_name->product = oscap_strdup(old_name->product); |
| 323 | new_name->version = oscap_strdup(old_name->version); |
| 324 | new_name->update = oscap_strdup(old_name->update); |
| 325 | new_name->edition = oscap_strdup(old_name->edition); |
| 326 | new_name->language = oscap_strdup(old_name->language); |
| 327 | new_name->sw_edition = oscap_strdup(old_name->sw_edition); |
| 328 | new_name->target_sw = oscap_strdup(old_name->target_sw); |
| 329 | new_name->target_hw = oscap_strdup(old_name->target_hw); |
| 330 | new_name->other = oscap_strdup(old_name->other); |
| 331 | |
| 332 | return new_name; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Takes given urlencoded string and replaces all occurences of % followed by |
| 337 | * 2 hex digits with a character. |
| 338 | * |
| 339 | * The operation is done in-place and is destructive to the data, even in |
| 340 | * case of a failure! |
| 341 | * |
| 342 | * @see cpe_urlencode |
| 343 | */ |
| 344 | static bool cpe_urldecode(char *str) |
| 345 | { |
| 346 | __attribute__nonnull__(str); |
| 347 | |
| 348 | char *inptr, *outptr; |
| 349 | |
| 350 | for (inptr = outptr = str; *inptr; ++inptr) { |
| 351 | if (*inptr == '%') { |
| 352 | if (isxdigit(inptr[1]) && isxdigit(inptr[2])) { |
| 353 | char hex[3] = { inptr[1], inptr[2], '\0' }; |
| 354 | unsigned out; |
| 355 | sscanf(hex, "%x", &out); |
| 356 | if (out == 0) { |
| 357 | // %00 encoded in the URI is definitely invalid |
| 358 | *outptr++ = '\0'; |
| 359 | return false; |
| 360 | } |
| 361 | *outptr++ = out; |
| 362 | inptr += 2; |
| 363 | } else { |
| 364 | // % followed by characters that are not hex digits is invalid |
| 365 | *outptr = '\0'; |
| 366 | return false; |
| 367 | } |
| 368 | } else |
| 369 | *outptr++ = *inptr; |
| 370 | } |
| 371 | *outptr = '\0'; |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Takes given string and replaces occurences of non-alphanumber characters |
| 377 | * except -._~ with % followed by hex digits representing that particular |
| 378 | * character. |
| 379 | * |
| 380 | * cpe_urlencode(" abc") would return "%20abc", |
| 381 | * cpe_urlencode("%") would return "%25". |
| 382 | */ |
| 383 | static char *cpe_urlencode(const char *str) |
| 384 | { |
| 385 | if (str == NULL) |
| 386 | return NULL; |
| 387 | |
| 388 | // allocate enough space |
| 389 | // in the worst case (all characters need to be replaced), the memory |
| 390 | // we will need is 3 times the size of input, + 1 for the terminating \0 |
| 391 | char *result = malloc(strlen(str) * 3 * sizeof(char) + 1); |
| 392 | char *out = result; |
| 393 | |
| 394 | for (const char *in = str; *in != '\0'; ++in, ++out) { |
| 395 | if (isalnum(*in) || strchr("-._~", *in)) |
| 396 | *out = *in; |
| 397 | else { |
| 398 | // this char shall be %-encoded |
| 399 | snprintf(out, 4, "%%%02X", *in); // we write 3 chars and \0 |
| 400 | out += 2; // for loop does another ++, giving us += 3 effectively |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // if the last character was alphanumeric we need to terminate |
| 405 | // if the last character was non-alphanum we will have 2 consecutive |
| 406 | // \0s at the end of the string which doesn't hurt anything |
| 407 | *out = '\0'; |
| 408 | |
| 409 | return result; |
| 410 | } |
| 411 | |
| 412 | static bool cpestring_comp_decode(char *str) |
| 413 | { |
| 414 | __attribute__nonnull__(str); |
| 415 | |
| 416 | char *inptr, *outptr; |
| 417 | |
| 418 | for (inptr = outptr = str; *inptr; ++inptr) { |
| 419 | if (*inptr == '\\' && *(inptr + sizeof(char)) == ':') { |
| 420 | *outptr = ':'; |
| 421 | inptr++; |
| 422 | } |
| 423 | else if (*inptr == '%') { |
| 424 | if (isxdigit(inptr[1]) && isxdigit(inptr[2])) { |
| 425 | char hex[3] = { inptr[1], inptr[2], '\0' }; |
| 426 | unsigned out; |
| 427 | sscanf(hex, "%x", &out); |
| 428 | if (out == 0) { |
| 429 | // %00 encoded in the URI is definitely invalid |
| 430 | *outptr++ = '\0'; |
| 431 | return false; |
| 432 | } |
| 433 | *outptr++ = out; |
| 434 | inptr += 2; |
| 435 | } else { |
| 436 | // % followed by characters that are not hex digits is invalid |
| 437 | *outptr = '\0'; |
| 438 | return false; |
| 439 | } |
| 440 | } else |
| 441 | *outptr++ = *inptr; |
| 442 | } |
| 443 | *outptr = '\0'; |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | static char *cpestring_comp_encode(const char *str) |
| 448 | { |
| 449 | if (str == NULL) |
| 450 | return NULL; |
| 451 | |
| 452 | // allocate enough space |
| 453 | // in the worst case (all characters need to be replaced), the memory |
| 454 | // we will need is 3 times the size of input, + 1 for the terminating \0 |
| 455 | char *result = malloc(strlen(str) * 3 * sizeof(char) + 1); |
| 456 | char *out = result; |
| 457 | |
| 458 | for (const char *in = str; *in != '\0'; ++in, ++out) { |
| 459 | if (isalnum(*in) || strchr("._~", *in)) { |
| 460 | *out = *in; |
| 461 | } |
| 462 | else if (*in == '\\') { |
| 463 | // anything escaped stays escaped |
| 464 | *(out++) = *(in++); |
| 465 | *(out) = *(in); |
| 466 | } |
| 467 | else if (*in == ':') { |
| 468 | *(out++) = '\\'; |
| 469 | *(out) = *in; |
| 470 | } |
| 471 | else if (*in == '*') { |
| 472 | *out = *in; |
| 473 | } |
| 474 | else if (*in == '-') { |
| 475 | *out = *in; |
| 476 | } |
| 477 | else { |
| 478 | // this char shall be %-encoded |
| 479 | snprintf(out, 4, "%%%02X", *in); // we write 3 chars and \0 |
| 480 | out += 2; // for loop does another ++, giving us += 3 effectively |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // if the last character was alphanumeric we need to terminate |
| 485 | // if the last character was non-alphanum we will have 2 consecutive |
| 486 | // \0s at the end of the string which doesn't hurt anything |
| 487 | *out = '\0'; |
| 488 | |
| 489 | return result; |
| 490 | } |
| 491 | |
| 492 | static bool cpe_has_extended_attributes(const struct cpe_name *cpe) |
| 493 | { |
| 494 | return cpe && ( |
| 495 | (cpe->sw_edition && strcmp(cpe->sw_edition, "")) || |
| 496 | (cpe->target_sw && strcmp(cpe->target_sw, "")) || |
| 497 | (cpe->target_hw && strcmp(cpe->target_hw, "")) || |
| 498 | (cpe->other && strcmp(cpe->other, "")) |
| 499 | ); |
| 500 | } |
| 501 | |
| 502 | static char *cpe_pack_extended_attributes(const struct cpe_name *cpe) |
| 503 | { |
| 504 | char* encoded_edition = cpe_urlencode(cpe->edition ? cpe->edition : ""); |
| 505 | char* encoded_sw_edition = cpe_urlencode(cpe->sw_edition ? cpe->sw_edition : ""); |
| 506 | char* encoded_target_sw = cpe_urlencode(cpe->target_sw ? cpe->target_sw : ""); |
| 507 | char* encoded_target_hw = cpe_urlencode(cpe->target_hw ? cpe->target_hw : ""); |
| 508 | char* encoded_other = cpe_urlencode(cpe->other ? cpe->other : ""); |
| 509 | |
| 510 | char* ret = oscap_sprintf("~%s~%s~%s~%s~%s", |
| 511 | encoded_edition, |
| 512 | encoded_sw_edition, |
| 513 | encoded_target_sw, |
| 514 | encoded_target_hw, |
| 515 | encoded_other |
| 516 | ); |
| 517 | |
| 518 | free(encoded_edition); |
| 519 | free(encoded_sw_edition); |
| 520 | free(encoded_target_sw); |
| 521 | free(encoded_target_hw); |
| 522 | free(encoded_other); |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | bool cpe_name_match_one(const struct cpe_name * cpe, const struct cpe_name * against) |
| 528 | { |
| 529 | |
| 530 | int i; |
| 531 | if (cpe == NULL || against == NULL) |
| 532 | return false; |
| 533 | |
| 534 | int cpefn = cpe_fields_num(cpe); |
| 535 | if (cpe_fields_num(against) < cpefn) |
| 536 | return false; |
| 537 | |
| 538 | for (i = 0; i < cpefn; ++i) { |
| 539 | const char *cpefield = cpe_get_field(cpe, i); |
| 540 | if (cpefield && oscap_strcasecmp(cpefield, as_str(cpe_get_field(against, i))) != 0) |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | cpe_format_t cpe_name_get_format_of_str(const char *str) |
| 548 | { |
| 549 | if (str == NULL) |
| 550 | return CPE_FORMAT_UNKNOWN; |
| 551 | |
| 552 | oscap_pcre_t *re; |
| 553 | char *error; |
| 554 | int erroffset; |
| 555 | int rc; |
| 556 | int ovector[30]; |
| 557 | |
| 558 | // The regex was taken from the official XSD at |
| 559 | // http://scap.nist.gov/schema/cpe/2.3/cpe-naming_2.3.xsd |
| 560 | // [c] was replaced with [cC] here and in the schemas |
| 561 | |
| 562 | re = oscap_pcre_compile("^[cC][pP][eE]:/[AHOaho]?(:[A-Za-z0-9\\._\\-~%]*){0,6}$", 0, &error, &erroffset); |
| 563 | if (re == NULL) { |
| 564 | oscap_pcre_err_free(error); |
| 565 | return CPE_FORMAT_UNKNOWN; |
| 566 | } |
| 567 | rc = oscap_pcre_exec(re, str, strlen(str), 0, 0, ovector, 30); |
| 568 | oscap_pcre_free(re); |
| 569 | |
| 570 | if (rc >= 0) |
| 571 | return CPE_FORMAT_URI; |
| 572 | |
| 573 | // The regex was taken from the official XSD at |
| 574 | // http://scap.nist.gov/schema/cpe/2.3/cpe-naming_2.3.xsd |
| 575 | re = oscap_pcre_compile("^cpe:2\\.3:[aho\\*\\-](:(((\\?*|\\*?)([a-zA-Z0-9\\-\\._]|(\\\\[\\\\\\*\\?!\"#$$%&'\\(\\)\\+,/:;<=>@\\[\\]\\^`\\{\\|}~]))+(\\?*|\\*?))|[\\*\\-])){5}(:(([a-zA-Z]{2,3}(-([a-zA-Z]{2}|[0-9]{3}))?)|[\\*\\-]))(:(((\\?*|\\*?)([a-zA-Z0-9\\-\\._]|(\\\\[\\\\\\*\\?!\"#$$%&'\\(\\)\\+,/:;<=>@\\[\\]\\^`\\{\\|}~]))+(\\?*|\\*?))|[\\*\\-])){4}$", 0, &error, &erroffset); |
| 576 | if (re == NULL) { |
| 577 | oscap_pcre_err_free(error); |
| 578 | return CPE_FORMAT_UNKNOWN; |
| 579 | } |
| 580 | rc = oscap_pcre_exec(re, str, strlen(str), 0, 0, ovector, 30); |
| 581 | oscap_pcre_free(re); |
| 582 | |
| 583 | if (rc >= 0) |
| 584 | return CPE_FORMAT_STRING; |
| 585 | |
| 586 | // FIXME: This should be way more strict |
| 587 | re = oscap_pcre_compile("^wfn:\\[.+\\]$", OSCAP_PCRE_OPTS_CASELESS, &error, &erroffset); |
| 588 | if (re == NULL) { |
| 589 | oscap_pcre_err_free(error); |
| 590 | return CPE_FORMAT_UNKNOWN; |
| 591 | } |
| 592 | rc = oscap_pcre_exec(re, str, strlen(str), 0, 0, ovector, 30); |
| 593 | oscap_pcre_free(re); |
| 594 | |
| 595 | if (rc >= 0) |
| 596 | return CPE_FORMAT_WFN; |
| 597 | |
| 598 | return CPE_FORMAT_UNKNOWN; |
| 599 | } |
| 600 | |
| 601 | bool cpe_name_check(const char *str) |
| 602 | { |
| 603 | return cpe_name_get_format_of_str(str) != CPE_FORMAT_UNKNOWN; |
| 604 | } |
| 605 | |
| 606 | static const char *as_str(const char *str) |
| 607 | { |
| 608 | if (str == NULL) |
| 609 | return ""; |
| 610 | return str; |
| 611 | } |
| 612 | |
| 613 | char *cpe_name_get_as_format(const struct cpe_name *cpe, cpe_format_t format) |
| 614 | { |
| 615 | __attribute__nonnull__(cpe); |
| 616 | |
| 617 | if (format == CPE_FORMAT_URI) |
| 618 | { |
| 619 | int len = 16; |
| 620 | int i; |
| 621 | char *result; |
| 622 | char* part[CPE_BASIC_FIELDNUM] = { NULL }; // CPE URI parts |
| 623 | |
| 624 | if (cpe == NULL) |
| 625 | return NULL; |
| 626 | |
| 627 | // get individual parts (%-encoded) |
| 628 | for (i = 0; i < CPE_BASIC_FIELDNUM; ++i) { |
| 629 | if (i == CPE_FIELD_EDITION && cpe_has_extended_attributes(cpe)) |
| 630 | part[i] = cpe_pack_extended_attributes(cpe); |
| 631 | else |
| 632 | part[i] = cpe_urlencode(as_str(cpe_get_field(cpe, i))); |
| 633 | |
| 634 | len += strlen(part[i]); |
| 635 | } |
| 636 | |
| 637 | result = malloc(len * sizeof(char)); |
| 638 | if (result == NULL) |
| 639 | return NULL; |
| 640 | |
| 641 | // create the URI |
| 642 | i = snprintf(result, len, "cpe:/%s:%s:%s:%s:%s:%s:%s", |
| 643 | part[0], part[1], part[2], part[3], part[4], part[5], part[6] |
| 644 | ); |
| 645 | |
| 646 | // free individual parts |
| 647 | for (int j = 0; j < CPE_BASIC_FIELDNUM; ++j) |
| 648 | free(part[j]); |
| 649 | |
| 650 | // trim trailing colons |
| 651 | while (result[--i] == ':') |
| 652 | result[i] = '\0'; |
| 653 | |
| 654 | return result; |
| 655 | } |
| 656 | else if (format == CPE_FORMAT_STRING) |
| 657 | { |
| 658 | int len = 19; |
| 659 | int i; |
| 660 | char *result; |
| 661 | char* part[CPE_TOTAL_FIELDNUM] = { NULL }; // CPE string parts |
| 662 | |
| 663 | if (cpe == NULL) |
| 664 | return NULL; |
| 665 | |
| 666 | // get individual parts (%-encoded) |
| 667 | for (i = 0; i < CPE_TOTAL_FIELDNUM; ++i) { |
| 668 | part[i] = cpestring_comp_encode(as_str(cpe_get_field(cpe, i))); |
| 669 | |
| 670 | len += strlen(part[i]); |
| 671 | } |
| 672 | |
| 673 | result = malloc(len * sizeof(char)); |
| 674 | if (result == NULL) |
| 675 | return NULL; |
| 676 | |
| 677 | // create the URI |
| 678 | i = snprintf(result, len, "cpe:2.3:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s", |
| 679 | part[0], part[1], part[2], part[3], part[4], part[5], part[6], |
| 680 | part[7], part[8], part[9], part[10] |
| 681 | ); |
| 682 | |
| 683 | // free individual parts |
| 684 | for (int j = 0; j < CPE_TOTAL_FIELDNUM; ++j) |
| 685 | free(part[j]); |
| 686 | |
| 687 | return result; |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | return NULL; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | char *cpe_name_get_as_str(const struct cpe_name *cpe) |
| 696 | { |
| 697 | cpe_format_t format = cpe_name_get_format(cpe); |
| 698 | |
| 699 | return cpe_name_get_as_format(cpe, format); |
| 700 | } |
| 701 | |
| 702 | int cpe_name_write(const struct cpe_name *cpe, FILE * f) |
| 703 | { |
| 704 | __attribute__nonnull__(cpe); |
| 705 | __attribute__nonnull__(f); |
| 706 | |
| 707 | int ret; |
| 708 | char *uri; |
| 709 | |
| 710 | uri = cpe_name_get_as_str(cpe); |
| 711 | if (uri == NULL) |
| 712 | return EOF; |
| 713 | |
| 714 | ret = fprintf(f, "%s", uri); |
| 715 | |
| 716 | free(uri); |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | void cpe_name_free(struct cpe_name *cpe) |
| 721 | { |
| 722 | if (cpe == NULL) |
| 723 | return; |
| 724 | |
| 725 | int i; |
| 726 | for (i = 0; i < CPE_TOTAL_FIELDNUM; ++i) |
| 727 | cpe_set_field(cpe, i, NULL); |
| 728 | free(cpe); |
| 729 | } |
| 730 | |
| 731 | const char * cpe_name_supported(void) |
| 732 | { |
| 733 | return CPE_URI_SUPPORTED; |
| 734 | } |
| 735 | |
| 736 | OSCAP_ACCESSOR_SIMPLE(cpe_format_t, cpe_name, format) |
| 737 | OSCAP_ACCESSOR_SIMPLE(cpe_part_t, cpe_name, part) |
| 738 | OSCAP_ACCESSOR_STRING(cpe_name, vendor) |
| 739 | OSCAP_ACCESSOR_STRING(cpe_name, product) |
| 740 | OSCAP_ACCESSOR_STRING(cpe_name, version) |
| 741 | OSCAP_ACCESSOR_STRING(cpe_name, update) |
| 742 | OSCAP_ACCESSOR_STRING(cpe_name, edition) |
| 743 | OSCAP_ACCESSOR_STRING(cpe_name, language) |
| 744 | OSCAP_ACCESSOR_STRING(cpe_name, sw_edition) |
| 745 | OSCAP_ACCESSOR_STRING(cpe_name, target_sw) |
| 746 | OSCAP_ACCESSOR_STRING(cpe_name, target_hw) |
| 747 | OSCAP_ACCESSOR_STRING(cpe_name, other) |