Skip to content

Commit 8d3846f

Browse files
committed
Rename a static function to xccdf_item_add_applicable_platform
Also introduces the function xccdf_item_add_applicable_platform to a private header file. This will enable us to reuse the function also for XCCDF Rules to fix their applicability.
1 parent 9c0110a commit 8d3846f

File tree

3 files changed

+51
-49
lines changed

3 files changed

+51
-49
lines changed

src/XCCDF/benchmark.c

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#endif
2626

2727
#include <string.h>
28-
#include <pcre.h>
2928

3029
#include "oscap_text.h"
3130
#include "item.h"
@@ -43,11 +42,6 @@
4342

4443
#define XCCDF_SUPPORTED "1.2"
4544

46-
/* According to `man 3 pcreapi`, the number passed in ovecsize should always
47-
* be a multiple of three.
48-
*/
49-
#define OVECTOR_LEN 30
50-
5145
static struct oscap_htable *xccdf_benchmark_find_target_htable(const struct xccdf_benchmark *, xccdf_type_t);
5246
static xmlNode *xccdf_plain_text_to_dom(const struct xccdf_plain_text *ptext, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info);
5347

@@ -132,48 +126,6 @@ struct xccdf_benchmark *xccdf_benchmark_clone(const struct xccdf_benchmark *old_
132126
return XBENCHMARK(new_benchmark);
133127
}
134128

135-
static void _xccdf_benchmark_add_platform(struct xccdf_item *benchmark, xmlTextReaderPtr reader)
136-
{
137-
char *platform_idref = xccdf_attribute_copy(reader, XCCDFA_IDREF);
138-
139-
/* Official Windows 7 CPE according to National Vulnerability Database
140-
* CPE Dictionary as of 2018-08-29 is 'cpe:/o:microsoft:windows_7'.
141-
* However, content exported from Microsoft Security Compliance Manager
142-
* as of version 4.0.0.1 in CAB archive using 'Export in SCAP 1.0' is
143-
* 'cpe:/o:microsoft:windows7'. If this pattern is matched, we will add
144-
* an underscore to workaround the situation that this XCCDF benchmark is
145-
* not applicable.
146-
*/
147-
const char *pcreerror = NULL;
148-
int erroffset = 0;
149-
pcre *regex = pcre_compile("^(cpe:/o:microsoft:windows)(7.*)", 0, &pcreerror, &erroffset, NULL);
150-
int ovector[OVECTOR_LEN];
151-
int rc = pcre_exec(regex, NULL, platform_idref, strlen(platform_idref), 0, 0, ovector, OVECTOR_LEN);
152-
/* 1 pattern + 2 groups = 3 */
153-
if (rc == 3) {
154-
int match_len = ovector[1] - ovector[0];
155-
/* match_len + 1 underscore + 1 zero byte */
156-
char *alternate_platform_idref = malloc(match_len + 1 + 1);
157-
int first_group_start = ovector[2];
158-
int first_group_end = ovector[3];
159-
size_t first_group_len = first_group_end - first_group_start;
160-
int second_group_start = ovector[4];
161-
int second_group_end = ovector[5];
162-
size_t second_group_len = second_group_end - second_group_start;
163-
char *aptr = alternate_platform_idref;
164-
strncpy(aptr, platform_idref + first_group_start, first_group_len);
165-
aptr += first_group_len;
166-
*aptr = '_';
167-
aptr++;
168-
strncpy(aptr, platform_idref + second_group_start, second_group_len);
169-
aptr += second_group_len;
170-
*aptr = '\0';
171-
oscap_list_add(benchmark->item.platforms, alternate_platform_idref);
172-
}
173-
174-
oscap_list_add(benchmark->item.platforms, platform_idref);
175-
}
176-
177129
bool xccdf_benchmark_parse(struct xccdf_item * benchmark, xmlTextReaderPtr reader)
178130
{
179131
XCCDF_ASSERT_ELEMENT(reader, XCCDFE_BENCHMARK);
@@ -208,7 +160,7 @@ bool xccdf_benchmark_parse(struct xccdf_item * benchmark, xmlTextReaderPtr reade
208160
oscap_list_add(benchmark->sub.benchmark.rear_matter, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
209161
break;
210162
case XCCDFE_PLATFORM:
211-
_xccdf_benchmark_add_platform(benchmark, reader);
163+
xccdf_item_add_applicable_platform(benchmark, reader);
212164
break;
213165
case XCCDFE_MODEL:
214166
parsed_model = xccdf_model_new_xml(reader);

src/XCCDF/item.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <string.h>
3131
#include <time.h>
3232
#include <math.h>
33+
#include <pcre.h>
3334

3435
#include <libxml/tree.h>
3536
#include <libxml/xpath.h>
@@ -41,6 +42,11 @@
4142
#include "xccdf_impl.h"
4243
#include "common/util.h"
4344

45+
/* According to `man 3 pcreapi`, the number passed in ovecsize should always
46+
* be a multiple of three.
47+
*/
48+
#define OVECTOR_LEN 30
49+
4450
const struct oscap_string_map XCCDF_OPERATOR_MAP[] = {
4551
{XCCDF_OPERATOR_EQUALS, "equals"},
4652
{XCCDF_OPERATOR_NOT_EQUAL, "not equal"},
@@ -742,6 +748,48 @@ bool xccdf_item_process_attributes(struct xccdf_item *item, xmlTextReaderPtr rea
742748
return item->item.id != NULL;
743749
}
744750

751+
void xccdf_item_add_applicable_platform(struct xccdf_item *item, xmlTextReaderPtr reader)
752+
{
753+
char *platform_idref = xccdf_attribute_copy(reader, XCCDFA_IDREF);
754+
755+
/* Official Windows 7 CPE according to National Vulnerability Database
756+
* CPE Dictionary as of 2018-08-29 is 'cpe:/o:microsoft:windows_7'.
757+
* However, content exported from Microsoft Security Compliance Manager
758+
* as of version 4.0.0.1 in CAB archive using 'Export in SCAP 1.0' is
759+
* 'cpe:/o:microsoft:windows7'. If this pattern is matched, we will add
760+
* an underscore to workaround the situation that this XCCDF benchmark is
761+
* not applicable.
762+
*/
763+
const char *pcreerror = NULL;
764+
int erroffset = 0;
765+
pcre *regex = pcre_compile("^(cpe:/o:microsoft:windows)(7.*)", 0, &pcreerror, &erroffset, NULL);
766+
int ovector[OVECTOR_LEN];
767+
int rc = pcre_exec(regex, NULL, platform_idref, strlen(platform_idref), 0, 0, ovector, OVECTOR_LEN);
768+
/* 1 pattern + 2 groups = 3 */
769+
if (rc == 3) {
770+
int match_len = ovector[1] - ovector[0];
771+
/* match_len + 1 underscore + 1 zero byte */
772+
char *alternate_platform_idref = malloc(match_len + 1 + 1);
773+
int first_group_start = ovector[2];
774+
int first_group_end = ovector[3];
775+
size_t first_group_len = first_group_end - first_group_start;
776+
int second_group_start = ovector[4];
777+
int second_group_end = ovector[5];
778+
size_t second_group_len = second_group_end - second_group_start;
779+
char *aptr = alternate_platform_idref;
780+
strncpy(aptr, platform_idref + first_group_start, first_group_len);
781+
aptr += first_group_len;
782+
*aptr = '_';
783+
aptr++;
784+
strncpy(aptr, platform_idref + second_group_start, second_group_len);
785+
aptr += second_group_len;
786+
*aptr = '\0';
787+
oscap_list_add(item->item.platforms, alternate_platform_idref);
788+
}
789+
790+
oscap_list_add(item->item.platforms, platform_idref);
791+
}
792+
745793
bool xccdf_item_process_element(struct xccdf_item * item, xmlTextReaderPtr reader)
746794
{
747795
xccdf_element_t el = xccdf_element_get(reader);

src/XCCDF/item.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ void xccdf_reparent_item(struct xccdf_item * item, struct xccdf_item * parent);
520520

521521
void xccdf_texts_to_dom(struct oscap_text_iterator *texts, xmlNode *parent, const char *elname);
522522

523+
void xccdf_item_add_applicable_platform(struct xccdf_item *item, xmlTextReaderPtr reader);
524+
523525
#include "unused.h"
524526

525527

0 commit comments

Comments
 (0)