File: oscap-cvss.c

package info (click to toggle)
openscap 0.8.0-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 38,092 kB
  • sloc: xml: 140,796; ansic: 75,509; sh: 17,874; makefile: 1,679; python: 536; perl: 442; cpp: 117
file content (136 lines) | stat: -rw-r--r-- 3,887 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
/*
 * Copyright 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:
 *      Maros Barabas  <mbarabas@redhat.com>
 */


/* Standard header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <math.h>

/* CVSS */
#include <cvss.h>

#include "oscap-tool.h"

static bool getopt_cvss(int argc, char **argv, struct oscap_action *action);
static int app_cvss_score(const struct oscap_action *action);
static int app_cvss_describe(const struct oscap_action *action);

static struct oscap_module* CVSS_SUBMODULES[];

struct oscap_module OSCAP_CVSS_MODULE = {
    .name = "cvss",
    .parent = &OSCAP_ROOT_MODULE,
    .summary = "Common Vulnerability Scoring System",
    .submodules = CVSS_SUBMODULES
};

static struct oscap_module CVSS_SCORE_MODULE = {
    .name = "score",
    .parent = &OSCAP_CVSS_MODULE,
    .summary = "CVSS score from a CVSS vector",
    .usage = "vector",
    .help = "Calculates CVSS score\n"
            "(base / temporal / environmental, depends on supplied metrics).",
    .opt_parser = getopt_cvss,
    .func = app_cvss_score
};

static struct oscap_module CVSS_DESCRIBE_MODULE = {
    .name = "describe",
    .parent = &OSCAP_CVSS_MODULE,
    .summary = "Describe a CVSS vector",
    .usage = "vector",
    .help = "Describes individual components of a CVSS vector\n",
    .opt_parser = getopt_cvss,
    .func = app_cvss_describe
};

static struct oscap_module* CVSS_SUBMODULES[] = {
    &CVSS_SCORE_MODULE,
    &CVSS_DESCRIBE_MODULE,
    NULL
};

static inline bool print_score(const char *type, float score)
{
    if (score >= 0.0 && score <= 10.0) {
        printf("%15s %4.1f\n", type, score);
        return true;
    }
    else return false;
}

int app_cvss_score(const struct oscap_action *action)
{
    assert(action->cvss_vector);

    bool ok = false;
    struct cvss_impact *impact = cvss_impact_new_from_vector(action->cvss_vector);

    if (impact == NULL) goto err;

    ok |= print_score("base",          cvss_impact_base_score(impact));
    ok |= print_score("temporal",      cvss_impact_temporal_score(impact));
    ok |= print_score("environmental", cvss_impact_environmental_score(impact));

    if (!ok) goto err;

    cvss_impact_free(impact);
    return OSCAP_OK;

err:
    cvss_impact_free(impact);
    fprintf(stderr, "Invalid input CVSS vector\n");
    return OSCAP_ERROR;
}

static int app_cvss_describe(const struct oscap_action *action)
{
    assert(action->cvss_vector);

    struct cvss_impact *impact = cvss_impact_new_from_vector(action->cvss_vector);

    if (impact) {
        cvss_impact_describe(impact, stdout);
        cvss_impact_free(impact);
        return OSCAP_OK;
    }
    else {
        fprintf(stderr, "Invalid input CVSS vector\n");
        return OSCAP_ERROR;
    }
}

bool getopt_cvss(int argc, char **argv, struct oscap_action *action)
{
    if (optind < argc) action->cvss_vector = argv[optind];

	if ((action->module == &CVSS_SCORE_MODULE) && action->cvss_vector == NULL)
        return oscap_module_usage(action->module, stderr, "CVSS vector not supplied");

	return true;
}