fec49fe8d6a1e6c7a52015e7b90f0b2e1c492b2a
[strongswan.git] / src / libimcv / plugins / imv_os / imv_os_state.c
1 /*
2 * Copyright (C) 2012 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imv_os_state.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_imv_os_state_t private_imv_os_state_t;
21
22 /**
23 * Private data of an imv_os_state_t object.
24 */
25 struct private_imv_os_state_t {
26
27 /**
28 * Public members of imv_os_state_t
29 */
30 imv_os_state_t public;
31
32 /**
33 * TNCCS connection ID
34 */
35 TNC_ConnectionID connection_id;
36
37 /**
38 * TNCCS connection state
39 */
40 TNC_ConnectionState state;
41
42 /**
43 * Does the TNCCS connection support long message types?
44 */
45 bool has_long;
46
47 /**
48 * Does the TNCCS connection support exclusive delivery?
49 */
50 bool has_excl;
51
52 /**
53 * Maximum PA-TNC message size for this TNCCS connection
54 */
55 u_int32_t max_msg_len;
56
57 /**
58 * IMV action recommendation
59 */
60 TNC_IMV_Action_Recommendation rec;
61
62 /**
63 * IMV evaluation result
64 */
65 TNC_IMV_Evaluation_Result eval;
66
67 /**
68 * OS Product Information (concatenation of OS Name and Version)
69 */
70 char *info;
71
72 /**
73 * OS Type
74 */
75 os_type_t type;
76
77 /**
78 * OS Name
79 */
80 chunk_t name;
81
82 /**
83 * OS Version
84 */
85 chunk_t version;
86
87 /**
88 * Number of processed packages
89 */
90 int count;
91
92 /**
93 * Number of blacklisted or not updated packages
94 */
95 int count_bad;
96
97 /**
98 * Number of whitelisted packages
99 */
100 int count_ok;
101
102 /**
103 * OS Installed Package request sent - mandatory response expected
104 */
105 bool package_request;
106
107 /**
108 * Angel count
109 */
110 int angel_count;
111
112 };
113
114 typedef struct entry_t entry_t;
115
116 /**
117 * Define an internal reason string entry
118 */
119 struct entry_t {
120 char *lang;
121 char *string;
122 };
123
124 /**
125 * Table of multi-lingual reason string entries
126 */
127 static entry_t reasons[] = {
128 { "en", "" },
129 { "de", "" },
130 { "fr", "" },
131 { "pl", "" }
132 };
133
134 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
135 private_imv_os_state_t *this)
136 {
137 return this->connection_id;
138 }
139
140 METHOD(imv_state_t, has_long, bool,
141 private_imv_os_state_t *this)
142 {
143 return this->has_long;
144 }
145
146 METHOD(imv_state_t, has_excl, bool,
147 private_imv_os_state_t *this)
148 {
149 return this->has_excl;
150 }
151
152 METHOD(imv_state_t, set_flags, void,
153 private_imv_os_state_t *this, bool has_long, bool has_excl)
154 {
155 this->has_long = has_long;
156 this->has_excl = has_excl;
157 }
158
159 METHOD(imv_state_t, set_max_msg_len, void,
160 private_imv_os_state_t *this, u_int32_t max_msg_len)
161 {
162 this->max_msg_len = max_msg_len;
163 }
164
165 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
166 private_imv_os_state_t *this)
167 {
168 return this->max_msg_len;
169 }
170
171 METHOD(imv_state_t, change_state, void,
172 private_imv_os_state_t *this, TNC_ConnectionState new_state)
173 {
174 this->state = new_state;
175 }
176
177 METHOD(imv_state_t, get_recommendation, void,
178 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation *rec,
179 TNC_IMV_Evaluation_Result *eval)
180 {
181 *rec = this->rec;
182 *eval = this->eval;
183 }
184
185 METHOD(imv_state_t, set_recommendation, void,
186 private_imv_os_state_t *this, TNC_IMV_Action_Recommendation rec,
187 TNC_IMV_Evaluation_Result eval)
188 {
189 this->rec = rec;
190 this->eval = eval;
191 }
192
193 METHOD(imv_state_t, get_reason_string, bool,
194 private_imv_os_state_t *this, enumerator_t *language_enumerator,
195 char **reason_string, char **reason_language)
196 {
197 return FALSE;
198 }
199
200 METHOD(imv_state_t, get_remediation_instructions, bool,
201 private_imv_os_state_t *this, enumerator_t *language_enumerator,
202 char **string, char **lang_code)
203 {
204 return FALSE;
205 }
206
207 METHOD(imv_state_t, destroy, void,
208 private_imv_os_state_t *this)
209 {
210 free(this->info);
211 free(this->name.ptr);
212 free(this->version.ptr);
213 free(this);
214 }
215
216 METHOD(imv_os_state_t, set_info, void,
217 private_imv_os_state_t *this, os_type_t type, chunk_t name, chunk_t version)
218 {
219 int len = name.len + 1 + version.len + 1;
220
221 /* OS info is a concatenation of OS name and OS version */
222 free(this->info);
223 this->info = malloc(len);
224 snprintf(this->info, len, "%.*s %.*s", name.len, name.ptr,
225 version.len, version.ptr);
226 this->type = type;
227 this->name = chunk_clone(name);
228 this->version = chunk_clone(version);
229 }
230
231 METHOD(imv_os_state_t, get_info, char*,
232 private_imv_os_state_t *this, os_type_t *type, chunk_t *name,
233 chunk_t *version)
234 {
235 if (type)
236 {
237 *type = this->type;
238 }
239 if (name)
240 {
241 *name = this->name;
242 }
243 if (version)
244 {
245 *version = this->version;
246 }
247 return this->info;
248 }
249
250 METHOD(imv_os_state_t, set_count, void,
251 private_imv_os_state_t *this, int count, int count_bad, int count_ok)
252 {
253 this->count += count;
254 this->count_bad += count_bad;
255 this->count_ok += count_ok;
256 }
257
258 METHOD(imv_os_state_t, get_count, void,
259 private_imv_os_state_t *this, int *count, int *count_bad, int *count_ok)
260 {
261 if (count)
262 {
263 *count = this->count;
264 }
265 if (count_bad)
266 {
267 *count_bad = this->count_bad;
268 }
269 if (count_ok)
270 {
271 *count_ok = this->count_ok;
272 }
273 }
274
275 METHOD(imv_os_state_t, set_package_request, void,
276 private_imv_os_state_t *this, bool set)
277 {
278 this->package_request = set;
279 }
280
281 METHOD(imv_os_state_t, get_package_request, bool,
282 private_imv_os_state_t *this)
283 {
284 return this->package_request;
285 }
286
287 METHOD(imv_os_state_t, set_angel_count, void,
288 private_imv_os_state_t *this, bool start)
289 {
290 this->angel_count += start ? 1 : -1;
291 }
292
293 METHOD(imv_os_state_t, get_angel_count, int,
294 private_imv_os_state_t *this)
295 {
296 return this->angel_count;
297 }
298
299 /**
300 * Described in header.
301 */
302 imv_state_t *imv_os_state_create(TNC_ConnectionID connection_id)
303 {
304 private_imv_os_state_t *this;
305
306 INIT(this,
307 .public = {
308 .interface = {
309 .get_connection_id = _get_connection_id,
310 .has_long = _has_long,
311 .has_excl = _has_excl,
312 .set_flags = _set_flags,
313 .set_max_msg_len = _set_max_msg_len,
314 .get_max_msg_len = _get_max_msg_len,
315 .change_state = _change_state,
316 .get_recommendation = _get_recommendation,
317 .set_recommendation = _set_recommendation,
318 .get_reason_string = _get_reason_string,
319 .get_remediation_instructions = _get_remediation_instructions,
320 .destroy = _destroy,
321 },
322 .set_info = _set_info,
323 .get_info = _get_info,
324 .set_count = _set_count,
325 .get_count = _get_count,
326 .set_package_request = _set_package_request,
327 .get_package_request = _get_package_request,
328 .set_angel_count = _set_angel_count,
329 .get_angel_count = _get_angel_count,
330 },
331 .state = TNC_CONNECTION_STATE_CREATE,
332 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
333 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
334 .connection_id = connection_id,
335 );
336
337 return &this->public.interface;
338 }
339
340