Debian Codegraph @

src/CPE/cpe_session.c

raw symbols SCIP
1 /*
2 * Copyright 2014 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author:
20 * Šimon Lukašík
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "common/_error.h"
28 #include "common/list.h"
29 #include "common/public/oscap.h"
30 #include "common/util.h"
31 #include "cpe_session_priv.h"
32 #include "CPE/public/cpe_dict.h"
33 #include "CPE/public/cpe_lang.h"
34 #include "OVAL/public/oval_agent_api.h"
35 #include "source/public/oscap_source.h"
36 #include "source/oscap_source_priv.h"
37 #include "oscap_helpers.h"
38
39 static inline bool cpe_session_add_default_cpe(struct cpe_session *session)
40 {
41 char* cpe_dict_path = oscap_sprintf("%s/openscap-cpe-dict.xml", oscap_path_to_cpe());
42 struct oscap_source *source = oscap_source_new_from_file(cpe_dict_path);
43 free(cpe_dict_path);
44 const bool ret = cpe_session_add_cpe_dict_source(session, source);
45 oscap_source_free(source);
46 return ret;
47 }
48
49 struct cpe_session *cpe_session_new(void)
50 {
51 struct cpe_session *cpe = calloc(1, sizeof(struct cpe_session));
52 cpe->dicts = oscap_list_new();
53 cpe->lang_models = oscap_list_new();
54 cpe->oval_sessions = oscap_htable_new();
55 cpe->applicable_platforms = oscap_htable_new();
56 cpe->thin_results = false;
57 if (!cpe_session_add_default_cpe(cpe)) {
58 oscap_seterr(OSCAP_EFAMILY_XCCDF, "Failed to add default CPE to newly created CPE Session.");
59 }
60 return cpe;
61 }
62
63 static inline void _xccdf_policy_destroy_cpe_oval_session(void* ptr)
64 {
65 struct oval_agent_session* session = (struct oval_agent_session*)ptr;
66 struct oval_definition_model* model = oval_agent_get_definition_model(session);
67 oval_agent_destroy_session(session);
68 oval_definition_model_free(model);
69 }
70
71 void cpe_session_free(struct cpe_session *session)
72 {
73 if (session != NULL) {
74 oscap_list_free(session->dicts, (oscap_destruct_func) cpe_dict_model_free);
75 oscap_list_free(session->lang_models, (oscap_destruct_func) cpe_lang_model_free);
76 oscap_htable_free(session->oval_sessions, (oscap_destruct_func) _xccdf_policy_destroy_cpe_oval_session);
77 oscap_htable_free(session->applicable_platforms, NULL);
78 free(session);
79 }
80 }
81
82 static inline struct oscap_source *_lookup_source_in_cache(struct cpe_session *session, const char *prefixed_href)
83 {
84 if (session->sources_cache == NULL) {
85 return NULL;
86 }
87 struct oscap_source *source = oscap_htable_get(session->sources_cache, prefixed_href);
88 return source;
89 }
90
91 void cpe_session_set_thin_results(struct cpe_session *cpe, bool thin_results)
92 {
93 cpe->thin_results = thin_results;
94 }
95
96 struct oval_agent_session *cpe_session_lookup_oval_session(struct cpe_session *cpe, const char *prefixed_href)
97 {
98 struct oval_agent_session* session = (struct oval_agent_session*)oscap_htable_get(cpe->oval_sessions, prefixed_href);
99
100 if (session == NULL)
101 {
102 struct oscap_source *source = NULL;
103 struct oscap_source *cached = _lookup_source_in_cache(cpe, prefixed_href);
104 if (cached == NULL) {
105 source = oscap_source_new_from_file(prefixed_href);
106 cached = source;
107 }
108 struct oval_definition_model* oval_model = oval_definition_model_import_source(cached);
109 oscap_source_free(source);
110
111 if (oval_model == NULL)
112 {
113 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Can't import OVAL definition model '%s' for CPE applicability checking", prefixed_href);
114 return NULL;
115 }
116
117 char *prefixed_href_dup = oscap_strdup(prefixed_href);
118 char *base_name = oscap_basename(prefixed_href_dup);
119 free(prefixed_href_dup);
120 session = oval_agent_new_session(oval_model, base_name);
121 free(base_name);
122 if (session == NULL) {
123 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot create OVAL session for '%s' for CPE applicability checking", prefixed_href);
124 return NULL;
125 }
126 if (cpe->thin_results) {
127 struct oval_results_model *res_model = oval_agent_get_results_model(session);
128 struct oval_directives_model *dir_model = oval_results_model_get_directives_model(res_model);
129 // This is the worst function name in existence, despite its name,
130 // it's getting the oval_result_directives of the oval_directives_model.
131 // You would expect oval_directives_model_getresdirs at least, but no..
132 struct oval_result_directives *dir = oval_directives_model_get_defdirs(dir_model);
133 oval_result_directives_set_content(dir, OVAL_RESULT_TRUE | OVAL_RESULT_FALSE |
134 OVAL_RESULT_UNKNOWN | OVAL_RESULT_NOT_EVALUATED |
135 OVAL_RESULT_NOT_APPLICABLE | OVAL_RESULT_ERROR,
136 OVAL_DIRECTIVE_CONTENT_THIN);
137 }
138 oscap_htable_add(cpe->oval_sessions, prefixed_href, session);
139 }
140 return session;
141 }
142
143 bool cpe_session_add_cpe_lang_model_source(struct cpe_session *session, struct oscap_source *source)
144 {
145 struct cpe_lang_model *lang_model = cpe_lang_model_import_source(source);
146 return oscap_list_add(session->lang_models, lang_model);
147 }
148
149 bool cpe_session_add_cpe_dict_source(struct cpe_session *session, struct oscap_source *source)
150 {
151 struct cpe_dict_model *dict = cpe_dict_model_import_source(source);
152 return oscap_list_add(session->dicts, dict);
153 }
154
155 bool cpe_session_add_cpe_autodetect_source(struct cpe_session *session, struct oscap_source *source)
156 {
157 oscap_document_type_t doc_type = oscap_source_get_scap_type(source);
158 if (doc_type == OSCAP_DOCUMENT_CPE_DICTIONARY) {
159 return cpe_session_add_cpe_dict_source(session, source);
160 } else if (doc_type == OSCAP_DOCUMENT_CPE_LANGUAGE) {
161 return cpe_session_add_cpe_lang_model_source(session, source);
162 } else {
163 oscap_seterr(OSCAP_EFAMILY_OSCAP, "File '%s' wasn't detected as either CPE dictionary or "
164 "CPE lang model. Can't register it to the XCCDF policy model.", oscap_source_readable_origin(source));
165 return false;
166 }
167 }
168
169 void cpe_session_set_cache(struct cpe_session *session, struct oscap_htable *sources_cache)
170 {
171 session->sources_cache = sources_cache;
172 }