Debian Codegraph @

src/OVAL/oval_probe_ext.c

raw symbols SCIP
1 /*
2 * Copyright 2010 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 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "_seap.h"
27 #include <stdarg.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #ifdef OS_WINDOWS
34 #include <io.h>
35 #else
36 #include <unistd.h>
37 #endif
38 #include <stdlib.h>
39
40 #include "_seap-types.h"
41 #include "common/_error.h"
42 #include "common/util.h"
43 #include "common/bfind.h"
44 #include "common/debug_priv.h"
45 #include "probes/public/probe-api.h"
46 #include "oval_probe_ext.h"
47 #include "oval_sexp.h"
48 #include "probe-table.h"
49 #include "_oval_probe_handler.h"
50
51 #define __ERRBUF_SIZE 128
52
53 static oval_pdtbl_t *oval_pdtbl_new(void);
54 static void oval_pdtbl_free(oval_pdtbl_t *table);
55 static int oval_pdtbl_add(oval_pdtbl_t *table, oval_subtype_t type, int sd, const char *uri);
56 static oval_pd_t *oval_pdtbl_get(oval_pdtbl_t *table, oval_subtype_t type);
57
58 /*
59 * oval_pext_
60 */
61 oval_pext_t *oval_pext_new(void)
62 {
63 oval_pext_t *pext = malloc(sizeof(oval_pext_t));
64
65 pext->do_init = true;
66 pthread_mutex_init(&pext->lock, NULL);
67 pext->pdtbl = NULL;
68
69 return(pext);
70 }
71
72 void oval_pext_free(oval_pext_t *pext)
73 {
74 if (!pext->do_init) {
75 /* free structs */
76 oval_pdtbl_free(pext->pdtbl);
77 }
78
79 pthread_mutex_destroy(&pext->lock);
80 free(pext);
81 }
82
83 /*
84 * oval_pdtbl_
85 */
86 static oval_pdtbl_t *oval_pdtbl_new(void)
87 {
88 oval_pdtbl_t *p_tbl = malloc(sizeof(oval_pdtbl_t));
89 p_tbl->memb = NULL;
90 p_tbl->count = 0;
91 p_tbl->ctx = SEAP_CTX_new();
92
93 return (p_tbl);
94 }
95
96 static void oval_pdtbl_free(oval_pdtbl_t *tbl)
97 {
98 register size_t i;
99
100 for (i = 0; i < tbl->count; ++i) {
101 SEAP_close(tbl->ctx, tbl->memb[i]->sd);
102 free(tbl->memb[i]->uri);
103 free(tbl->memb[i]);
104 }
105
106 free(tbl->memb);
107 SEAP_CTX_free(tbl->ctx);
108 free(tbl);
109
110 return;
111 }
112
113 static int oval_pdtbl_pdcmp(const oval_pd_t **a, const oval_pd_t **b)
114 {
115 if (*a == NULL || *b == NULL) {
116 return -1;
117 }
118 return ((*a)->subtype - (*b)->subtype);
119 }
120
121 static int oval_pdtbl_typecmp(oval_subtype_t *a, oval_pd_t **b)
122 {
123 if (a == NULL || *b == NULL) {
124 return -1;
125 }
126 return (*a - (*b)->subtype);
127 }
128
129 static int oval_pdtbl_add(oval_pdtbl_t *tbl, oval_subtype_t type, int sd, const char *uri)
130 {
131 if (tbl == NULL || uri == NULL) {
132 return -1;
133 }
134
135 oval_pd_t *pd = malloc(sizeof(oval_pd_t));
136 pd->subtype = type;
137 pd->sd = sd;
138 pd->uri = oscap_strdup(uri);
139
140 void *new_memb = realloc(tbl->memb, sizeof(oval_pd_t *) * (++tbl->count));
141 if (new_memb == NULL) {
142 free(pd->uri);
143 free(pd);
144 return -1;
145 }
146 tbl->memb = new_memb;
147
148 tbl->memb[tbl->count - 1] = pd;
149
150 qsort(tbl->memb, tbl->count, sizeof(oval_pd_t *),
151 (int (*)(const void *, const void *))oval_pdtbl_pdcmp);
152
153 return (0);
154 }
155
156 static oval_pd_t *oval_pdtbl_get(oval_pdtbl_t * tbl, oval_subtype_t type)
157 {
158 oval_pd_t **pdp;
159
160 pdp = oscap_bfind(tbl->memb, tbl->count, sizeof(oval_pd_t *),
161 &type, (int (*)(void *, void *))oval_pdtbl_typecmp);
162
163 return (pdp == NULL ? NULL : *pdp);
164 }
165
166 /*
167 * oval_probe_cmd_
168 */
169 static SEXP_t *oval_probe_cmd_obj_eval(SEXP_t *sexp, void *arg);
170 static SEXP_t *oval_probe_cmd_ste_fetch(SEXP_t *sexp, void *arg);
171 static int oval_probe_cmd_init(oval_pext_t *pext);
172
173 static int oval_probe_cmd_init(oval_pext_t *pext)
174 {
175 if (pext == NULL) {
176 return -1;
177 }
178
179 if (SEAP_cmd_register(pext->pdtbl->ctx, PROBECMD_OBJ_EVAL, SEAP_CMDREG_USEARG,
180 &oval_probe_cmd_obj_eval, (void *)pext) != 0)
181 {
182 dE("Can't register command: %s: errno=%u, %s.", "obj_eval", errno, strerror(errno));
183 return (-1);
184 }
185
186 if (SEAP_cmd_register(pext->pdtbl->ctx, PROBECMD_STE_FETCH, SEAP_CMDREG_USEARG,
187 &oval_probe_cmd_ste_fetch, (void *)pext) != 0) {
188 dE("Can't register command: %s: errno=%u, %s.", "ste_fetch", errno, strerror(errno));
189
190 /* FIXME: unregister the first command */
191
192 return (-1);
193 }
194
195 return (0);
196 }
197
198 static SEXP_t *oval_probe_cmd_obj_eval(SEXP_t *sexp, void *arg)
199 {
200 char *id_str;
201 struct oval_definition_model *defs;
202 struct oval_object *obj;
203 struct oval_syschar *res;
204 oval_pext_t *pext = (oval_pext_t *) arg;
205 SEXP_t *ret, *ret_code;
206 int r;
207
208 if (sexp == NULL || arg == NULL) {
209 return NULL;
210 }
211
212 if (!SEXP_stringp(sexp)) {
213 dE("Invalid argument: type=%s.", SEXP_strtype(sexp));
214 return (NULL);
215 }
216
217 id_str = SEXP_string_cstr(sexp);
218 defs = oval_syschar_model_get_definition_model(*(pext->model));
219 obj = oval_definition_model_get_object(defs, id_str);
220 ret = SEXP_list_new (sexp, NULL);
221
222 dD("Get_object: %s.", id_str);
223
224 if (obj == NULL) {
225 dE("Can't find obj: id=%s.", id_str);
226 free(id_str);
227 SEXP_free(ret);
228
229 return (NULL);
230 }
231
232 oscap_clearerr();
233 r = oval_probe_query_object(pext->sess_ptr, obj, OVAL_PDFLAG_NOREPLY|OVAL_PDFLAG_SLAVE, &res);
234 if (r < 0)
235 ret_code = SEXP_number_newu((unsigned int) SYSCHAR_FLAG_COMPLETE);
236 else
237 ret_code = SEXP_number_newu((unsigned int) oval_syschar_get_flag(res));
238
239 SEXP_list_add(ret, ret_code);
240 SEXP_free(ret_code);
241
242 if (oscap_err()) {
243 dE("Failed: id: %s, err: %d, %s.",
244 id_str, oscap_err_family(), oscap_err_desc());
245 oscap_clearerr();
246 free(id_str);
247 SEXP_free(ret);
248
249 return (NULL);
250 }
251
252 free(id_str);
253
254 return (ret);
255 }
256
257 static SEXP_t *oval_probe_cmd_ste_fetch(SEXP_t *sexp, void *arg)
258 {
259 SEXP_t *id, *ste_list, *ste_sexp;
260 char *id_str;
261 struct oval_state *ste;
262 struct oval_definition_model *definition_model;
263 oval_pext_t *pext = (oval_pext_t *)arg;
264 int ret;
265
266 if (sexp == NULL || arg == NULL) {
267 return NULL;
268 }
269
270 ste_list = SEXP_list_new(NULL);
271
272 SEXP_list_foreach(id, sexp) {
273 if (SEXP_stringp(id)) {
274 id_str = SEXP_string_cstr(id);
275 definition_model = oval_syschar_model_get_definition_model(*(pext->model));
276 ste = oval_definition_model_get_state(definition_model, id_str);
277
278 if (ste == NULL) {
279 dE("Can't find ste: id: %s.", id_str);
280 SEXP_list_free(ste_list);
281 free(id_str);
282 SEXP_free(id);
283
284 return (NULL);
285 }
286
287 ret = oval_state_to_sexp(pext->sess_ptr, ste, &ste_sexp);
288 if (ret !=0) {
289 dE("Failed to convert OVAL state to SEXP, id: %s.",
290 id_str);
291 SEXP_list_free(ste_list);
292 free(id_str);
293 SEXP_free(id);
294
295 return (NULL);
296 }
297
298 SEXP_list_add(ste_list, ste_sexp);
299 SEXP_free(ste_sexp);
300
301 free(id_str);
302 }
303 }
304
305 return (ste_list);
306 }
307
308 static inline const char *_probe_strerror(uint32_t error_code)
309 {
310 const char *codemsg;
311
312 /*
313 * Errors of type USER should all be from the probe "namespace" (i.e. only codes
314 * defined at public/probe-api.h.
315 */
316 switch (error_code) {
317 case PROBE_EINVAL: codemsg = "Invalid type, value or format";
318 break;
319 case PROBE_ENOELM: codemsg = "Missing element";
320 break;
321 case PROBE_ENOVAL: codemsg = "Missing value";
322 break;
323 case PROBE_ENOATTR: codemsg = "Missing attribute";
324 break;
325 case PROBE_EINIT: codemsg = "Initialization failed";
326 break;
327 case PROBE_ENOMEM: codemsg = "Insufficient memory";
328 break;
329 case PROBE_EOPNOTSUPP: codemsg = "Operation not supported";
330 break;
331 case PROBE_ERANGE: codemsg = "Value out of range";
332 break;
333 case PROBE_EDOM: codemsg = "Value out of domain";
334 break;
335 case PROBE_EFAULT: codemsg = "Memory fault or NULL value";
336 break;
337 case PROBE_EACCESS: codemsg = "Operation not permitted";
338 break;
339 case PROBE_ESETEVAL: codemsg = "Set evaluation failed";
340 break;
341 case PROBE_ENOENT: codemsg = "Missing entity";
342 break;
343 case PROBE_EFATAL: codemsg = "Unrecoverable error";
344 break;
345 case PROBE_EUNKNOWN:
346 default:
347 codemsg = "Unknown error";
348 }
349
350 return codemsg;
351 }
352
353 static inline int _handle_SEAP_receive_failure(SEAP_CTX_t *ctx, oval_pd_t *pd, SEAP_msg_t *s_omsg, int flags)
354 {
355 protect_errno {
356 dW("Can't receive message: %u, %s.", errno, strerror(errno));
357 }
358
359 if (errno == ECANCELED) {
360 SEAP_err_t *err = NULL;
361
362 switch(SEAP_recverr_byid(ctx, pd->sd, &err,
363 SEAP_msg_id(s_omsg)))
364 {
365 case 0:
366 break;
367 case 1: /* no error found */
368 dE("Internal error: An error was signaled on sd=%d but the error queue is empty.", pd->sd);
369 oscap_seterr(OSCAP_EFAMILY_OVAL, "SEAP_recverr_byid: internal error: empty error queue.");
370 return (-1);
371 case -1: /* internal error */
372 dE("Internal error: SEAP_recverr_byid returned -1");
373 oscap_seterr(OSCAP_EFAMILY_OVAL, "SEAP_recverr_byid: internal error.");
374 return (-1);
375 }
376
377 /*
378 * decide what to do based on the error code/type
379 */
380 switch (err->type) {
381 case SEAP_ETYPE_USER:
382 {
383 oscap_seterr(OSCAP_EFAMILY_OVAL, "Probe at sd=%d (%s) reported an error: %s",
384 pd->sd, oval_subtype_to_str(pd->subtype), _probe_strerror(err->code));
385 break;
386 }
387 case SEAP_ETYPE_INT:
388 oscap_seterr(OSCAP_EFAMILY_OVAL, "Internal error");
389 break;
390 }
391
392 SEAP_error_free(err);
393 return (-1);
394 }
395
396 if (flags & OVAL_PDFLAG_SLAVE) {
397 char errbuf[__ERRBUF_SIZE];
398
399 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) != 0)
400 oscap_seterr (OSCAP_EFAMILY_OVAL, "Unable to receive a message to probe");
401 else
402 oscap_seterr (OSCAP_EFAMILY_OVAL, "%s", errbuf);
403
404 return (-1);
405 }
406
407 if (SEAP_close(ctx, pd->sd) != 0) {
408 char errbuf[__ERRBUF_SIZE];
409
410 protect_errno {
411 dE("Can't close sd: %u, %s.", errno, strerror(errno));
412 }
413
414 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) != 0)
415 oscap_seterr (OSCAP_EFAMILY_OVAL, "Unable to close probe sd");
416 else
417 oscap_seterr (OSCAP_EFAMILY_OVAL, "%s", errbuf);
418 }
419
420 pd->sd = -1;
421 return (-1);
422 }
423
424 static int oval_probe_comm(SEAP_CTX_t *ctx, oval_pd_t *pd, const SEXP_t *s_iobj, int flags, SEXP_t **out_sexp)
425 {
426 int retry, ret;
427
428 SEAP_msg_t *s_imsg, *s_omsg;
429 SEXP_t *s_oobj;
430
431 if (pd == NULL || s_iobj == NULL) {
432 return -1;
433 }
434
435 ctx->subtype = pd->subtype;
436 for (retry = 0;;) {
437 /*
438 * Establish connection to probe. The connection may be
439 * already set up by previous calls to this function or
440 * by the probe context handling functions.
441 */
442 if (pd->sd == -1) {
443 pd->sd = SEAP_connect(ctx);
444
445 if (pd->sd < 0) {
446 protect_errno {
447 dW("Can't connect: %u, %s.", errno, strerror(errno));
448 }
449
450 if (++retry <= OVAL_PROBE_MAXRETRY) {
451 dD("Connect: retry %u/%u.", retry, OVAL_PROBE_MAXRETRY);
452 continue;
453 } else {
454 char errbuf[__ERRBUF_SIZE];
455
456 protect_errno {
457 dE("Connect: retry limit (%u) reached.", OVAL_PROBE_MAXRETRY);
458 }
459
460 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) != 0)
461 oscap_seterr (OSCAP_EFAMILY_OVAL, "Can't connect to the probe");
462 else
463 oscap_seterr (OSCAP_EFAMILY_OVAL, "%s", errbuf);
464
465 return (-1);
466 }
467 }
468 }
469
470 s_omsg = SEAP_msg_new();
471 SEAP_msg_set(s_omsg, (SEXP_t *) s_iobj);
472
473 if (flags & OVAL_PDFLAG_NOREPLY) {
474 if (SEAP_msgattr_set(s_omsg, "no-reply", NULL) != 0) {
475 protect_errno {
476 dE("Can't set no-reply attribute.");
477 }
478
479 SEAP_msg_free(s_omsg);
480 oscap_seterr (OSCAP_EFAMILY_OVAL, "OVAL_EPROBEUNKNOWN");
481
482 return (-1);
483 }
484 }
485
486 dD("Sending message.");
487
488 ret = SEAP_sendmsg(ctx, pd->sd, s_omsg);
489 if (ret != 0) {
490 protect_errno {
491 dW("Can't send message: %u, %s.", errno, strerror(errno));
492 }
493
494 if (flags & OVAL_PDFLAG_SLAVE) {
495 char errbuf[__ERRBUF_SIZE];
496
497 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) != 0)
498 oscap_seterr (OSCAP_EFAMILY_OVAL, "Unable to send a message to probe");
499 else
500 oscap_seterr (OSCAP_EFAMILY_OVAL, "%s", errbuf);
501
502 SEAP_msg_free(s_omsg);
503 return (-1);
504 }
505
506 if (SEAP_close(ctx, pd->sd) != 0) {
507 char errbuf[__ERRBUF_SIZE];
508
509 protect_errno {
510 dE("Can't close sd: %u, %s.", errno, strerror(errno));
511 SEAP_msg_free(s_omsg);
512 }
513
514 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) != 0)
515 oscap_seterr (OSCAP_EFAMILY_OVAL, "Can't close sd");
516 else
517 oscap_seterr (OSCAP_EFAMILY_OVAL, "%s", errbuf);
518
519 pd->sd = -1;
520 return (-1);
521 }
522
523 pd->sd = -1;
524
525 if (++retry <= OVAL_PROBE_MAXRETRY) {
526 dD("Send: retry %u/%u.", retry, OVAL_PROBE_MAXRETRY);
527 continue;
528 } else {
529 char errbuf[__ERRBUF_SIZE];
530
531 protect_errno {
532 dE("Send: retry limit (%u) reached.", OVAL_PROBE_MAXRETRY);
533 SEAP_msg_free(s_omsg);
534 }
535
536 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) != 0)
537 oscap_seterr (OSCAP_EFAMILY_OVAL, "Unable to send a message to probe");
538 else
539 oscap_seterr (OSCAP_EFAMILY_OVAL, "%s", errbuf);
540
541 return (ret);
542 }
543 }
544
545 dD("Waiting for reply.");
546
547 /* recv_retry: */
548 s_imsg = NULL;
549
550 ret = SEAP_recvmsg(ctx, pd->sd, &s_imsg);
551 if (ret != 0) {
552 protect_errno {
553 ret = _handle_SEAP_receive_failure(ctx, pd, s_omsg, flags);
554 SEAP_msg_free(s_imsg);
555 SEAP_msg_free(s_omsg);
556 }
557 if (errno == ECONNABORTED) {
558 dD("Connection was aborted.");
559 return (-2);
560 } else {
561 if (++retry <= OVAL_PROBE_MAXRETRY) {
562 dD("Recv: retry %u/%u.", retry, OVAL_PROBE_MAXRETRY);
563 continue;
564 } else {
565 protect_errno {
566 dE("Recv: retry limit (%u) reached.", OVAL_PROBE_MAXRETRY);
567 }
568
569 char errbuf[__ERRBUF_SIZE];
570 if (oscap_strerror_r (errno, errbuf, sizeof errbuf - 1) == 0)
571 oscap_seterr(OSCAP_EFAMILY_OVAL, "%s", errbuf);
572 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unable to receive a message from probe");
573
574 return ret;
575 }
576 }
577 }
578
579 dD("Message received.");
580 break;
581 }
582
583 s_oobj = SEAP_msg_get(s_imsg);
584
585 SEAP_msg_free(s_imsg);
586 SEAP_msg_free(s_omsg);
587
588 *out_sexp = s_oobj;
589 return (0);
590 }
591
592 static int oval_probe_sys_eval(SEAP_CTX_t *ctx, oval_pd_t *pd, struct oval_syschar_model *model, struct oval_sysinfo **out_sysinf)
593 {
594 struct oval_sysinfo *sysinf;
595 struct oval_sysint *ife;
596 SEXP_t *s_obj, *s_sinf, *ent, *r0, *r1;
597 int ret;
598
599 /*
600 * Prepare a dummy object. We can't simply send an empty object
601 * because the preprocessing machinery in probes need an id that
602 * is used as key in cache lookups.
603 */
604 {
605 SEXP_t *r2, *r3;
606
607 r0 = SEXP_list_new (r1 = SEXP_string_newf ("%s", "sysinfo_object"),
608 r2 = SEXP_string_newf (":%s", "id"),
609 r3 = SEXP_string_newf ("sysinfo:0"),
610 NULL);
611
612 SEXP_free(r1);
613 SEXP_free(r2);
614 SEXP_free(r3);
615 s_obj = SEXP_list_new (r0, NULL);
616 SEXP_free (r0);
617 }
618
619 ret = oval_probe_comm(ctx, pd, s_obj, 0, &r0);
620 SEXP_free(s_obj);
621
622 if (ret != 0)
623 return (ret);
624
625 r1 = probe_cobj_get_items(r0);
626 s_sinf = SEXP_list_first(r1);
627 SEXP_free(r0);
628 SEXP_free(r1);
629
630 if (s_sinf == NULL)
631 return (-1);
632
633 sysinf = oval_sysinfo_new(model);
634
635 /*
636 * Translate S-exp to sysinfo structure
637 */
638 #define SYSINF_EXT(obj, name, sysinf, fail) \
639 do { \
640 SEXP_t *val; \
641 char buf[128+1]; \
642 \
643 val = probe_obj_getentval (obj, #name, 1); \
644 \
645 if (val == NULL) { \
646 dD("No entity or value: %s", #name); \
647 goto fail; \
648 } \
649 \
650 if (SEXP_string_cstr_r (val, buf, sizeof buf) >= sizeof buf) { \
651 dD("Value too large: %s", #name); \
652 SEXP_free (val); \
653 goto fail; \
654 } \
655 \
656 oval_sysinfo_set_##name (sysinf, buf); \
657 SEXP_free (val); \
658 } while (0)
659
660 SYSINF_EXT(s_sinf, os_name, sysinf, fail_gen);
661 SYSINF_EXT(s_sinf, os_version, sysinf, fail_gen);
662 SYSINF_EXT(s_sinf, os_architecture, sysinf, fail_gen);
663 SYSINF_EXT(s_sinf, primary_host_name, sysinf, fail_gen);
664
665 /*
666 * Extract interface info
667 */
668 {
669 uint32_t n;
670
671 for (n = 1; (ent = probe_obj_getent(s_sinf, "interface", n)) != NULL; ++n) {
672 ife = oval_sysint_new(model);
673
674 #define SYSINF_IEXT(ent, name, sysint, fail) \
675 do { \
676 SEXP_t *val; \
677 char buf[128+1]; \
678 \
679 val = probe_ent_getattrval (ent, #name); \
680 \
681 if (val == NULL) { \
682 dD("No value: %s", #name); \
683 goto fail; \
684 } \
685 \
686 if (SEXP_string_cstr_r (val, buf, sizeof buf) >= sizeof buf) { \
687 dD("Value too large: %s", #name); \
688 SEXP_free (val); \
689 goto fail; \
690 } \
691 \
692 oval_sysint_set_##name (sysint, buf); \
693 SEXP_free (val); \
694 \
695 } while (0)
696
697 SYSINF_IEXT(ent, ip_address, ife, fail_int);
698 SYSINF_IEXT(ent, mac_address, ife, fail_int);
699 SYSINF_IEXT(ent, name, ife, fail_int);
700
701 oval_sysinfo_add_interface(sysinf, ife);
702 oval_sysint_free(ife);
703 SEXP_free(ent);
704 }
705 }
706
707 SEXP_free(s_sinf);
708
709 *out_sysinf = sysinf;
710 return (0);
711 fail_int:
712 SEXP_free(ent);
713 oval_sysint_free(ife);
714 fail_gen:
715 SEXP_free(s_sinf);
716 oval_sysinfo_free(sysinf);
717
718 return (-1);
719 }
720
721 int oval_probe_sys_handler(oval_subtype_t type, void *ptr, int act, ...)
722 {
723 int ret = 0;
724 oval_pext_t *pext = (oval_pext_t *)ptr;
725 va_list ap;
726 oval_pd_t *pd;
727
728 va_start(ap, act);
729
730 switch(act) {
731 case PROBE_HANDLER_ACT_EVAL:
732 {
733 struct oval_sysinfo **inf;
734 /* int flags = va_arg(ap, int); */
735
736 va_arg(ap, struct oval_object *);
737 inf = va_arg(ap, struct oval_sysinfo **);
738 pd = oval_pdtbl_get(pext->pdtbl, type);
739
740 if (pd == NULL) {
741 if (oval_probe_sys_handler(type, ptr, PROBE_HANDLER_ACT_OPEN) != 0) {
742 va_end(ap);
743 return(-1);
744 }
745
746 pd = oval_pdtbl_get(pext->pdtbl, type);
747 }
748
749 if (pd == NULL) {
750 va_end(ap);
751 return -1;
752 }
753 ret = oval_probe_sys_eval(pext->pdtbl->ctx, pd, *(pext->model), inf);
754 break;
755 }
756 case PROBE_HANDLER_ACT_OPEN:
757 {
758 char probe_uri[PATH_MAX + 1];
759 size_t probe_urilen;
760
761 if (!probe_table_exists(type)) {
762 oscap_seterr (OSCAP_EFAMILY_OVAL, "subtype %u not supported", type);
763
764 ret = -1;
765 break;
766 }
767
768 probe_urilen = snprintf(probe_uri, sizeof probe_uri, "%s://%s",
769 OVAL_PROBE_SCHEME, oval_subtype_get_text(type));
770
771 if (probe_urilen >= sizeof probe_uri) {
772 oscap_seterr (OSCAP_EFAMILY_GLIBC, "probe URI too long");
773
774 ret = -1;
775 } else {
776 dI("Starting probe on URI '%s'.", probe_uri);
777
778 if (oval_pdtbl_add(pext->pdtbl, type, -1, probe_uri) != 0) {
779 oscap_seterr(OSCAP_EFAMILY_OVAL, "%s probe not supported", oval_subtype_get_text(type));
780
781 ret = -1;
782 }
783 }
784 break;
785 }
786 case PROBE_HANDLER_ACT_INIT:
787 ret = oval_probe_ext_init(pext);
788 break;
789 default:
790 errno = EINVAL;
791 ret = -1;
792 }
793
794 va_end(ap);
795 return(ret);
796 }
797
798 int oval_probe_ext_handler(oval_subtype_t type, void *ptr, int act, ...)
799 {
800 int ret = 0;
801 va_list ap;
802 oval_pext_t *pext = (oval_pext_t *)ptr;
803 oval_pd_t *pd;
804
805 va_start(ap, act);
806
807 switch(act) {
808 case PROBE_HANDLER_ACT_EVAL:
809 {
810 struct oval_object *obj;
811 struct oval_syschar *sys;
812 int flags;
813
814 sys = va_arg(ap, struct oval_syschar *);
815 flags = va_arg(ap, int);
816 obj = oval_syschar_get_object(sys);
817 oval_subtype_t obj_subtype = oval_object_get_subtype(obj);
818 pd = oval_pdtbl_get(pext->pdtbl, obj_subtype);
819
820 if (pd == NULL) {
821 char probe_uri[PATH_MAX + 1];
822 size_t probe_urilen;
823
824 if (!probe_table_exists(obj_subtype)) {
825 oval_syschar_add_new_message(sys, "OVAL object not supported", OVAL_MESSAGE_LEVEL_WARNING);
826 oval_syschar_set_flag(sys, SYSCHAR_FLAG_NOT_COLLECTED);
827 va_end(ap);
828 return (1);
829 }
830
831 probe_urilen = snprintf(probe_uri, sizeof probe_uri, "%s://%s",
832 OVAL_PROBE_SCHEME, oval_subtype_get_text(obj_subtype));
833
834 if (probe_urilen >= sizeof probe_uri) {
835 oscap_seterr (OSCAP_EFAMILY_GLIBC, "probe URI too long");
836 va_end(ap);
837 return (-1);
838 }
839
840 dI("Starting probe on URI '%s'.", probe_uri);
841
842 if (oval_pdtbl_add(pext->pdtbl, obj_subtype, -1, probe_uri) != 0) {
843 oval_syschar_add_new_message(sys, "OVAL object not supported", OVAL_MESSAGE_LEVEL_WARNING);
844 oval_syschar_set_flag(sys, SYSCHAR_FLAG_NOT_COLLECTED);
845 va_end(ap);
846 return (1);
847 }
848
849 pd = oval_pdtbl_get(pext->pdtbl, obj_subtype);
850
851 if (pd == NULL) {
852 oscap_seterr (OSCAP_EFAMILY_OVAL, "internal error");
853 va_end(ap);
854 return (-1);
855 }
856 }
857
858 ret = oval_probe_ext_eval(pext->pdtbl->ctx, pd, pext, sys, flags);
859
860 if (ret >= 0)
861 ret = 0;
862
863 if (ret < 0 && errno == ECONNABORTED) {
864 if (!(flags & OVAL_PDFLAG_SLAVE)) {
865 if (!pext->do_init) {
866 oval_pdtbl_free(pext->pdtbl);
867 }
868
869 pext->do_init = true;
870 pext->pdtbl = NULL;
871
872 oval_probe_ext_init(pext);
873
874 errno = ECONNABORTED;
875 }
876 }
877
878 va_end(ap);
879 return ret;
880 }
881 case PROBE_HANDLER_ACT_OPEN:
882 break;
883 case PROBE_HANDLER_ACT_INIT:
884 ret = oval_probe_ext_init(pext);
885 break;
886 case PROBE_HANDLER_ACT_RESET:
887 case PROBE_HANDLER_ACT_ABORT:
888 {
889 if (type == OVAL_SUBTYPE_ALL) {
890 /*
891 * Iterate thru probe descriptor table and execute the reset operation
892 * for each probe descriptor.
893 */
894 for (size_t i = 0; i < pext->pdtbl->count; ++i) {
895 pd = pext->pdtbl->memb[i];
896
897 if (pd == NULL) {
898 va_end(ap);
899 return(0);
900 }
901
902 if (act == PROBE_HANDLER_ACT_RESET)
903 ret = oval_probe_ext_reset(pext->pdtbl->ctx, pd, pext);
904 else
905 ret = oval_probe_ext_abort(pext->pdtbl->ctx, pd, pext);
906
907 if (ret != 0) {
908 va_end(ap);
909 return (ret);
910 }
911 }
912
913 va_end(ap);
914 return(0);
915 } else {
916 /*
917 * Reset only the probe of specified subtype.
918 */
919 pd = oval_pdtbl_get(pext->pdtbl, type);
920
921 va_end(ap);
922 if (pd == NULL)
923 return(0);
924
925 if (act == PROBE_HANDLER_ACT_RESET)
926 return oval_probe_ext_reset(pext->pdtbl->ctx, pd, pext);
927 else
928 return oval_probe_ext_abort(pext->pdtbl->ctx, pd, pext);
929 }
930 break;
931 }
932 case PROBE_HANDLER_ACT_FREE:
933 case PROBE_HANDLER_ACT_CLOSE:
934 default:
935 va_end(ap);
936 errno = EINVAL;
937 return(-1);
938 }
939
940 va_end(ap);
941 return(ret);
942 }
943
944 int oval_probe_ext_init(oval_pext_t *pext)
945 {
946 int ret = 0;
947
948 pthread_mutex_lock(&pext->lock);
949
950 if (pext->do_init) {
951 pext->pdtbl = oval_pdtbl_new();
952
953 if (oval_probe_cmd_init(pext) != 0)
954 ret = -1;
955 else
956 pext->do_init = false;
957 }
958 pthread_mutex_unlock(&pext->lock);
959
960 return(ret);
961 }
962
963 int oval_probe_ext_eval(SEAP_CTX_t *ctx, oval_pd_t *pd, oval_pext_t *pext, struct oval_syschar *syschar, int flags)
964 {
965 SEXP_t *s_obj, *s_sys;
966 struct oval_object *object;
967 int ret;
968
969 if (syschar == NULL) {
970 oscap_seterr(OSCAP_EFAMILY_OVAL, "Internal error: syschar == NULL");
971 return (-1);
972 }
973
974 object = oval_syschar_get_object(syschar);
975 ret = oval_object_to_sexp(pext->sess_ptr, oval_subtype_to_str(oval_object_get_subtype(object)), syschar, &s_obj);
976
977 if (ret != 0)
978 return (1);
979
980 ret = oval_probe_comm(ctx, pd, s_obj, flags, &s_sys);
981 SEXP_free(s_obj);
982
983 if (ret != 0) {
984 switch (errno) {
985 case ECONNABORTED:
986 dD("Closing sd=%d (pd=%p) after abort", pd->sd, pd);
987
988 SEAP_close(ctx, pd->sd);
989 pd->sd = -1;
990 errno = ECONNABORTED;
991 }
992 return (ret);
993 }
994
995 if (flags & OVAL_PDFLAG_NOREPLY) {
996 if (s_sys != NULL) {
997 if (SEXP_typeof(s_sys) != SEXP_TYPE_LIST || SEXP_list_length(s_sys) != 0) {
998 /*
999 * The no-reply flag is set and oval_probe_comm returned some
1000 * data. This is considered a non-fatal error.
1001 */
1002 dW("Obtrusive data from probe!");
1003 }
1004 if (SEXP_typeof(s_sys) == SEXP_TYPE_LIST) {
1005 SEXP_list_free(s_sys);
1006 } else {
1007 SEXP_free(s_sys);
1008 }
1009 }
1010 return (0);
1011 }
1012
1013 /*
1014 * Convert the received S-exp to OVAL system characteristic.
1015 */
1016 ret = oval_sexp_to_sysch(s_sys, syschar);
1017 SEXP_free(s_sys);
1018
1019 return (ret);
1020 }
1021
1022 int oval_probe_ext_reset(SEAP_CTX_t *ctx, oval_pd_t *pd, oval_pext_t *pext)
1023 {
1024 SEAP_cmd_exec(ctx, pd->sd, SEAP_EXEC_RECV, PROBECMD_RESET, NULL, SEAP_CMDTYPE_SYNC, NULL, NULL);
1025
1026 return (0);
1027 }
1028
1029 #include <signal.h>
1030 #include "SEAP/_seap-types.h"
1031 #include "SEAP/seap-descriptor.h"
1032
1033 #ifdef OS_WINDOWS
1034
1035 int oval_probe_ext_abort(SEAP_CTX_t *ctx, oval_pd_t *pd, oval_pext_t *pext)
1036 {
1037 dE("Operation oval_probe_ext_abort is not supported on Windows!");
1038 return 0;
1039 }
1040
1041 #else
1042
1043 int oval_probe_ext_abort(SEAP_CTX_t *ctx, oval_pd_t *pd, oval_pext_t *pext)
1044 {
1045 return (0);
1046 }
1047
1048 #endif