File: oval_behavior.c

package info (click to toggle)
openscap 0.5.12-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 27,052 kB
  • ctags: 21,075
  • sloc: xml: 82,351; ansic: 52,101; sh: 17,802; makefile: 748; perl: 442; cpp: 117; python: 110
file content (181 lines) | stat: -rw-r--r-- 5,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * @file oval_behavior.c
 * \brief Open Vulnerability and Assessment Language
 *
 * See more details at http://oval.mitre.org/
 */

/*
 * Copyright 2009-2010 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:
 *      "David Niemoller" <David.Niemoller@g2-inc.com>
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "oval_definitions_impl.h"
#include "oval_collection_impl.h"
#include "oval_string_map_impl.h"

#include "common/util.h"
#include "common/debug_priv.h"
#include "common/_error.h"

/***************************************************************************/
/* Variable definitions
 * */

typedef struct oval_behavior {
	struct oval_definition_model *model;
	char *value;
	char *key;
} oval_behavior_t;

/* End of variable definitions
 * */
/***************************************************************************/

bool oval_behavior_iterator_has_more(struct oval_behavior_iterator *oc_behavior)
{
	return oval_collection_iterator_has_more((struct oval_iterator *)
						 oc_behavior);
}

struct oval_behavior *oval_behavior_iterator_next(struct oval_behavior_iterator
						  *oc_behavior)
{
	return (struct oval_behavior *)
	    oval_collection_iterator_next((struct oval_iterator *)oc_behavior);
}

void oval_behavior_iterator_free(struct oval_behavior_iterator
				 *oc_behavior)
{
	oval_collection_iterator_free((struct oval_iterator *)oc_behavior);
}

char *oval_behavior_get_value(struct oval_behavior *behavior)
{
	__attribute__nonnull__(behavior);

	return behavior->value;
}

char *oval_behavior_get_key(struct oval_behavior *behavior)
{
	__attribute__nonnull__(behavior);

	return behavior->key;
}

struct oval_behavior *oval_behavior_new(struct oval_definition_model *model)
{
	oval_behavior_t *behavior = (oval_behavior_t *) oscap_alloc(sizeof(oval_behavior_t));
	if (behavior == NULL)
		return NULL;

	behavior->model = model;
	behavior->value = NULL;
	behavior->key = NULL;
	return behavior;
}

struct oval_behavior *oval_behavior_clone(struct oval_definition_model *new_model, struct oval_behavior *old_behavior)
{
	struct oval_behavior *new_behavior = oval_behavior_new(new_model);
	oval_behavior_set_keyval
	    (new_behavior, oval_behavior_get_key(old_behavior), oval_behavior_get_value(old_behavior));
	return new_behavior;
}

bool oval_behavior_is_valid(struct oval_behavior * behavior)
{
	if (behavior == NULL) {
                oscap_dprintf("WARNING: argument is not valid: NULL");
		return false;
        }

	return true;
}

bool oval_behavior_is_locked(struct oval_behavior * behavior)
{
	__attribute__nonnull__(behavior);

	return oval_definition_model_is_locked(behavior->model);
}

void oval_behavior_free(struct oval_behavior *behavior)
{
	__attribute__nonnull__(behavior);

	if (behavior->value)
		oscap_free(behavior->value);
	if (behavior->key)
		oscap_free(behavior->key);
	behavior->key = NULL;
	behavior->value = NULL;
	oscap_free(behavior);
}

void oval_behavior_set_keyval(struct oval_behavior *behavior, const char *key, const char *value)
{
	if (behavior && !oval_behavior_is_locked(behavior)) {
		behavior->key = oscap_strdup(key);
		behavior->value = oscap_strdup(value);
	} else
		oscap_dprintf("WARNING: attempt to update locked content (%s:%d)", __FILE__, __LINE__);
}

//typedef void (*oval_behavior_consumer)(struct oval_behavior_node *, void*);
int oval_behavior_parse_tag(xmlTextReaderPtr reader,
			    struct oval_parser_context *context,
			    oval_family_t family, oval_behavior_consumer consumer, void *user)
{
	__attribute__nonnull__(context);

	while (xmlTextReaderMoveToNextAttribute(reader) == 1) {

		const char *name = (const char *)xmlTextReaderConstName(reader);
		const char *value = (const char *)xmlTextReaderConstValue(reader);

		if (name && value) {
			oval_behavior_t *behavior = oval_behavior_new(context->definition_model);
			oval_behavior_set_keyval(behavior, name, value);
			(*consumer) (behavior, user);
		}
	}
	return 1;
}

void oval_behavior_to_print(struct oval_behavior *behavior, char *indent, int idx)
{
	char nxtindent[100];

	if (strlen(indent) > 80)
		indent = "....";

	if (idx == 0)
		snprintf(nxtindent, sizeof(nxtindent), "%sBEHAVIOR.", indent);
	else
		snprintf(nxtindent, sizeof(nxtindent), "%sBEHAVIOR[%d].", indent, idx);

	printf("%s%s = [%s]\n", nxtindent, oval_behavior_get_key(behavior), oval_behavior_get_value(behavior));
}