File: oscap-tool.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 (306 lines) | stat: -rw-r--r-- 9,282 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * 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:
 *      Lukas Kuklinek <lkuklinek@redhat.com>
 */

#include "oscap-tool.h"
#include <assert.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>

#ifdef ENABLE_CVSS
#include <cvss.h>
#endif

static void oscap_action_init(struct oscap_action *action)
{
    assert(action != NULL);
    memset(action, 0, sizeof(*action));
    action->verbosity = true;
    action->validate = 1;
}

static void oscap_action_release(struct oscap_action *action)
{
	assert(action != NULL);
	free(action->f_ovals);
#ifdef ENABLE_CVSS
	cvss_impact_free(action->cvss_impact);
#endif
}


static bool oscap_action_postprocess(struct oscap_action *action)
{
    return true;
}

static size_t paramlist_size(const char **p) { size_t s = 0; if (!p) return s; while (p[s]) s += 2; return s; }

static size_t paramlist_cpy(const char **to, const char **p) {
    size_t s = 0;
    if (!p) return s;
    for (;p && p[s]; s += 2) to[s] = p[s], to[s+1] = p[s+1];
    to[s] = p[s];
    return s;
}

int app_xslt(const char *infile, const char *xsltfile, const char *outfile, const char **params)
{
    const char *stdparams[] = { "oscap-version", oscap_get_version(), "pwd", getenv("PWD"), NULL };
    const char *par[paramlist_size(params) + paramlist_size(stdparams) + 1];
    size_t s  = paramlist_cpy(par    , params);
           s += paramlist_cpy(par + s, stdparams);

    int ret = OSCAP_ERROR;
    if (oscap_apply_xslt(infile, xsltfile, outfile, par)) ret = OSCAP_OK;
    else fprintf(stderr, "ERROR: %s\n", oscap_err_desc());
    return ret;
}


static void check_module(struct oscap_module *module)
{
    assert(module != NULL);
    assert(module->name != NULL);
    if (module != &OSCAP_ROOT_MODULE)
        assert(module->parent != NULL);
    if (module->submodules)
        for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub))
            assert((*sub)->parent == module);
}

static void oscap_module_backtrace(struct oscap_module *module, FILE *out, const char *delim)
{
    assert(module != NULL);
    if (module->parent) {
        oscap_module_backtrace(module->parent, out, delim);
        fprintf(out, "%s", delim);
    }
    fprintf(out, "%s", module->name);
}

static void oscap_module_print_usage(struct oscap_module *module, FILE *out, bool full)
{
    assert(module != NULL);
    if (module->parent != NULL) {
        oscap_module_print_usage(module->parent, out, false);
        fprintf(out, " ");
    }
    fprintf(out, "%s", module->name);
    if (module->usage) fprintf(out, " %s", module->usage);
    if (full) {
        if (module->usage_extra) fprintf(out, " %s", module->usage_extra);
        else if (module->submodules) fprintf(out, " command");
    }
}

static inline void oscap_module_print_summary(struct oscap_module *module, FILE *out)
{
    assert(module != NULL);
    if (!module->hidden) {
        fprintf(out, "    %s", module->name);
        if (module->summary) fprintf(out, "\r\t\t\t\t - %s", module->summary);
        fprintf(out, "\n");
    }
}

static int oscap_print_submodules(struct oscap_module *module, FILE *out, const char *delim, bool all)
{
    if (module->submodules) {
        for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub))
            if (all || !(*sub)->hidden)
                fprintf(out, "%s%s", (*sub)->name, delim);
        return OSCAP_OK;
    }
    return OSCAP_BADMODULE;
}

bool oscap_module_usage(struct oscap_module *module, FILE *out, const char *err, ...)
{
    assert(module != NULL);

    if (err) {
        va_list ap;
        fprintf(out, "ERROR:    ");
        va_start(ap, err);
        vfprintf(out, err, ap);
        va_end(ap);
        fprintf(out, "\n");
    }
    fprintf(out, "Usage:    ");
    oscap_module_print_usage(module, out, true);
    if (module->submodules) { fprintf(out, "\nCommands: "); oscap_print_submodules(module, out, " ", false); }
    fprintf(out, "\nHelp:     ");
    oscap_module_backtrace(module, out, " ");
    fprintf(out, " -h\n");
    return (out != stderr);
}

static void oscap_module_print_help(struct oscap_module *module, FILE *out)
{
    assert(module != NULL);
    assert(out != NULL);

    oscap_module_backtrace(module, out, " -> ");
    fprintf(out, "\n\n");

    if (module->summary) fprintf(out, "%s\n\n", module->summary);

    fprintf(out, "Usage: ");
    oscap_module_print_usage(module, out, true);
    fprintf(out, "\n\n");

    if (module->help) fprintf(out, "%s\n\n", module->help);

    if (module->submodules) {
        fprintf(out, "Commands:\n");
        for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub))
            oscap_module_print_summary(*sub, out);
        fprintf(out, "\n");
    }
}

static void oscap_module_tree(struct oscap_module *module, FILE* out, int indent)
{
    assert(module);
    if (module->hidden) return;
    for (int i = 0; i < indent; ++i) fprintf(out, "    ");
    fprintf(out, "%s - %s\n", module->name, module->summary);
    if (module->submodules) {
        for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub))
            oscap_module_tree(*sub, out, indent + 1);
    }
}

static struct oscap_module *oscap_module_find(struct oscap_module **list, const char *name)
{
    if (name == NULL) return NULL;
    if (list == NULL) return NULL;
    while (*list != NULL && strcmp((*list)->name, name) != 0) ++list;
    return *list;
}

enum oscap_common_opts {
    OPT_NONE,
    OPT_LISTMODS,
    OPT_LISTALLMODS,
    OPT_MODTREE,
    OPT_HELP = 'h'
};

static enum oscap_common_opts oscap_parse_common_opts(int argc, char **argv)
{
    static const struct option opts[] = {
        { "help",                0, 0, OPT_HELP        },
        { "list-submodules",     0, 0, OPT_LISTMODS    },
        { "list-all-submodules", 0, 0, OPT_LISTALLMODS },
        { "module-tree",         0, 0, OPT_MODTREE     },
        { 0, 0, 0, 0 }
    };

    int optind_bak = optind;
    int opterr_bak = opterr;
    opterr = 0;
    int r = getopt_long(argc, argv, "+h", opts, NULL);
    opterr = opterr_bak;
    switch (r) {
        case -1: case '?': optind = optind_bak; return OPT_NONE;
        default: return r;
    }
}

int oscap_module_call(struct oscap_action *action)
{
    assert(action != NULL);
    assert(action->module != NULL);

    if (action->module->func) {
        if (oscap_action_postprocess(action))
            return action->module->func(action);
        else return OSCAP_ERR_FETCH;
    }
    return OSCAP_UNIMPL_MOD;
}

int oscap_module_process(struct oscap_module *module, int argc, char **argv)
{
    assert(module != NULL);
    assert(argv != NULL);

    int ret = OSCAP_OK;
    struct oscap_action action;
    oscap_action_init(&action);
    optind = 0;

    while (module) {
        check_module(module);

        ++optind; // skip current module key index
        action.module = module; // update current module

        switch (oscap_parse_common_opts(argc, argv)) {
            case OPT_HELP: oscap_module_print_help(module, stdout); goto cleanup;
            case OPT_LISTMODS: oscap_print_submodules(module, stdout, "\n", false); goto cleanup;
            case OPT_LISTALLMODS: oscap_print_submodules(module, stdout, "\n", true); goto cleanup;
            case OPT_MODTREE: oscap_module_tree(module, stdout, 0); goto cleanup;
            default: break;
        }

        if (module->opt_parser) {
            if (!module->opt_parser(argc, argv, &action)) {
                ret = OSCAP_BADARGS;
                goto cleanup;
            }
            module = action.module; // module might have changed
        }

        if (module->func) {
            ret = oscap_module_call(&action);
            goto cleanup;
        }
        else if (module->submodules) {
            struct oscap_module *old_mod = module;
            module = oscap_module_find(module->submodules, argv[optind]);
            if (module == NULL) {
                if (argv[optind] != NULL)
                    fprintf(stderr, "No such module: %s\n", argv[optind]);
                else oscap_module_usage(old_mod, stderr, NULL);
                ret = OSCAP_BADMODULE;
            }
        }
        else {
            fprintf(stderr, "Module %s does not do anything\n", module->name);
            ret = OSCAP_UNIMPL_MOD;
            goto cleanup;
        }
    }

cleanup:
    oscap_action_release(&action);
    return ret;
}