| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifdef HAVE_CONFIG_H |
| 12 |
#include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
#include <cpe_lang.h> |
| 18 |
#include <cpe_name.h> |
| 19 |
#include <oscap.h> |
| 20 |
|
| 21 |
#define OSCAP_FOREACH_GENERIC(itype, vtype, val, init_val, code) \ |
| 22 |
{ \ |
| 23 |
struct itype##_iterator *val##_iter = (init_val); \ |
| 24 |
vtype val; \ |
| 25 |
while (itype##_iterator_has_more(val##_iter)) { \ |
| 26 |
val = itype##_iterator_next(val##_iter); \ |
| 27 |
code \ |
| 28 |
} \ |
| 29 |
itype##_iterator_free(val##_iter); \ |
| 30 |
} |
| 31 |
|
| 32 |
#define OSCAP_FOREACH(type, val, init_val, code) \ |
| 33 |
OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code) |
| 34 |
|
| 35 |
|
| 36 |
const char *CPE_OPER_STRS[] = { "", "AND", "OR", "" }; |
| 37 |
|
| 38 |
void print_usage(const char *, FILE *); |
| 39 |
|
| 40 |
int print_expr_prefix_form(const struct cpe_testexpr *); |
| 41 |
|
| 42 |
void print_platform(struct cpe_platform *); |
| 43 |
|
| 44 |
int main (int argc, char *argv[]) |
| 45 |
{ |
| 46 |
struct cpe_lang_model *lang_model = NULL; |
| 47 |
struct cpe_platform *platform = NULL, *new_platform = NULL; |
| 48 |
struct cpe_testexpr *testexpr = NULL; |
| 49 |
struct cpe_platform_iterator *platform_it = NULL; |
| 50 |
struct oscap_text_iterator *title_it = NULL; |
| 51 |
struct oscap_text *title = NULL; |
| 52 |
int ret_val = 0, i; |
| 53 |
|
| 54 |
struct oscap_source *source = oscap_source_new_from_file(argv[2]); |
| 55 |
|
| 56 |
if (argc == 2 && !strcmp(argv[1], "--help")) { |
| 57 |
print_usage(argv[0], stdout); |
| 58 |
ret_val = 0; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
else if (argc == 4 && !strcmp(argv[1], "--get-all")) { |
| 63 |
|
| 64 |
if ((lang_model = cpe_lang_model_import_source(source)) == NULL) { |
| 65 |
oscap_source_free(source); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
platform_it = cpe_lang_model_get_platforms(lang_model); |
| 72 |
while (cpe_platform_iterator_has_more(platform_it)) { |
| 73 |
print_platform(cpe_platform_iterator_next(platform_it)); |
| 74 |
} |
| 75 |
cpe_platform_iterator_free(platform_it); |
| 76 |
cpe_lang_model_free(lang_model); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
else if (argc == 5 && !strcmp(argv[1], "--get-key")) { |
| 81 |
|
| 82 |
if ((lang_model = cpe_lang_model_import_source(source)) == NULL) { |
| 83 |
oscap_source_free(source); |
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
if ((platform = cpe_lang_model_get_item(lang_model, argv[4])) == NULL) { |
| 88 |
oscap_source_free(source); |
| 89 |
return 2; |
| 90 |
} |
| 91 |
|
| 92 |
print_platform(platform); |
| 93 |
|
| 94 |
cpe_lang_model_free(lang_model); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
else if (argc >= 6 && !strcmp(argv[1], "--set-all")) { |
| 99 |
if ((lang_model = cpe_lang_model_import_source(source)) == NULL) { |
| 100 |
oscap_source_free(source); |
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
for (i = 6; i < argc; i++) { |
| 105 |
if ((new_platform = cpe_platform_new()) == NULL) { |
| 106 |
oscap_source_free(source); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
cpe_platform_set_id(new_platform, argv[i]); |
| 110 |
if (!cpe_lang_model_add_platform(lang_model, new_platform)) { |
| 111 |
oscap_source_free(source); |
| 112 |
return 2; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
cpe_lang_model_export(lang_model, argv[2]); |
| 117 |
cpe_lang_model_free(lang_model); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
else if (argc >= 6 && !strcmp(argv[1], "--set-key")) { |
| 122 |
if ((lang_model = cpe_lang_model_import_source(source)) == NULL) { |
| 123 |
oscap_source_free(source); |
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
if ((platform = cpe_lang_model_get_item(lang_model, argv[4])) == NULL) { |
| 128 |
oscap_source_free(source); |
| 129 |
return 2; |
| 130 |
} |
| 131 |
|
| 132 |
if (strcmp(argv[5], "-")) |
| 133 |
cpe_platform_set_id(platform, argv[5]); |
| 134 |
|
| 135 |
i = 6; |
| 136 |
title_it = cpe_platform_get_titles(platform); |
| 137 |
while (i < argc && oscap_text_iterator_has_more(title_it)) { |
| 138 |
title = oscap_text_iterator_next(title_it); |
| 139 |
if (strcmp(argv[i], "-")) |
| 140 |
oscap_text_set_text(title, argv[i]); |
| 141 |
i++; |
| 142 |
} |
| 143 |
|
| 144 |
cpe_lang_model_export(lang_model, argv[2]); |
| 145 |
cpe_lang_model_free(lang_model); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
else if (argc >= 6 && !strcmp(argv[1], "--set-new")) { |
| 150 |
if ((lang_model = cpe_lang_model_new()) == NULL) { |
| 151 |
oscap_source_free(source); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
for (i = 6; i < argc; i++) { |
| 156 |
if ((new_platform = cpe_platform_new()) == NULL) { |
| 157 |
oscap_source_free(source); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
cpe_platform_set_id(new_platform, argv[i]); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (!cpe_lang_model_add_platform(lang_model, new_platform)) { |
| 168 |
oscap_source_free(source); |
| 169 |
return 2; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
cpe_lang_model_export(lang_model, argv[2]); |
| 174 |
cpe_lang_model_free(lang_model); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
else if (argc == 2 && !strcmp(argv[1], "--smoke-test")) { |
| 179 |
|
| 180 |
if ((lang_model = cpe_lang_model_new()) == NULL) { |
| 181 |
oscap_source_free(source); |
| 182 |
return 1; |
| 183 |
} else { |
| 184 |
cpe_lang_model_free(lang_model); |
| 185 |
} |
| 186 |
|
| 187 |
if ((new_platform = cpe_platform_new()) == NULL) { |
| 188 |
oscap_source_free(source); |
| 189 |
return 1; |
| 190 |
} |
| 191 |
else |
| 192 |
cpe_platform_free(new_platform); |
| 193 |
|
| 194 |
if ((testexpr = cpe_testexpr_new()) == NULL) { |
| 195 |
oscap_source_free(source); |
| 196 |
return 1; |
| 197 |
} |
| 198 |
else |
| 199 |
cpe_testexpr_free(testexpr); |
| 200 |
} |
| 201 |
|
| 202 |
else if (argc == 6 && !strcmp(argv[1], "--export-all")) { |
| 203 |
if ((lang_model = cpe_lang_model_import_source(source)) == NULL) { |
| 204 |
oscap_source_free(source); |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
cpe_lang_model_export(lang_model, argv[4]); |
| 209 |
cpe_lang_model_free(lang_model); |
| 210 |
} |
| 211 |
else { |
| 212 |
print_usage(argv[0], stderr); |
| 213 |
ret_val = 1; |
| 214 |
} |
| 215 |
|
| 216 |
oscap_source_free(source); |
| 217 |
oscap_cleanup(); |
| 218 |
|
| 219 |
return ret_val; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
void print_usage(const char *program_name, FILE * out) |
| 224 |
{ |
| 225 |
fprintf(out, |
| 226 |
"Usage: \n\n" |
| 227 |
" %s --export-all CPE_LANG_XML ENCODING CPE_EXPORT_XML ENCODING\n" |
| 228 |
" %s --get-all CPE_LANG_XML ENCODING\n" |
| 229 |
" %s --get-key CPE_LANG_XML ENCODING KEY\n" |
| 230 |
" %s --set-all CPE_LANG_XML ENCODING NS_PREFIX NS_HREF (PLATFORM_ID)*\n" |
| 231 |
" %s --set-key CPE_LANG_XML ENCODING KEY ID (TITLE)*\n" |
| 232 |
" %s --set-new CPE_LANG_XML ENCODING NS_PREFIX NS_HREF (PLATFORM_ID)*\n" |
| 233 |
" %s --smoke-test\n", |
| 234 |
program_name, program_name, program_name, program_name, |
| 235 |
program_name, program_name, program_name); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
int print_expr_prefix_form(const struct cpe_testexpr *expr) |
| 240 |
{ |
| 241 |
|
| 242 |
char *name = NULL; |
| 243 |
|
| 244 |
putchar('('); |
| 245 |
|
| 246 |
if (cpe_testexpr_get_oper(expr) & CPE_LANG_OPER_NOT) |
| 247 |
printf("!"); |
| 248 |
|
| 249 |
switch (cpe_testexpr_get_oper(expr) & CPE_LANG_OPER_MASK) { |
| 250 |
case CPE_LANG_OPER_AND: |
| 251 |
case CPE_LANG_OPER_OR: |
| 252 |
printf("%s", |
| 253 |
CPE_OPER_STRS[cpe_testexpr_get_oper(expr) & |
| 254 |
CPE_LANG_OPER_MASK]); |
| 255 |
|
| 256 |
OSCAP_FOREACH(cpe_testexpr, sub, |
| 257 |
cpe_testexpr_get_meta_expr(expr), |
| 258 |
print_expr_prefix_form(sub); |
| 259 |
); |
| 260 |
break; |
| 261 |
case CPE_LANG_OPER_MATCH: |
| 262 |
name = cpe_name_get_as_str(cpe_testexpr_get_meta_cpe(expr)); |
| 263 |
printf("%s", name); |
| 264 |
free(name); |
| 265 |
break; |
| 266 |
default: |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
putchar(')'); |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
void print_platform(struct cpe_platform *platform) |
| 277 |
{ |
| 278 |
struct oscap_text *title = NULL; |
| 279 |
struct oscap_text_iterator *title_it = NULL; |
| 280 |
const char *remark, *id, *content, *language; |
| 281 |
|
| 282 |
id = cpe_platform_get_id(platform); |
| 283 |
printf("%s:", id == NULL ? "" : id); |
| 284 |
|
| 285 |
remark = cpe_platform_get_remark(platform); |
| 286 |
printf("%s:", remark == NULL ? "" : remark); |
| 287 |
|
| 288 |
title_it = cpe_platform_get_titles(platform); |
| 289 |
while (oscap_text_iterator_has_more(title_it)) { |
| 290 |
title = oscap_text_iterator_next(title_it); |
| 291 |
|
| 292 |
content = oscap_text_get_text(title); |
| 293 |
printf("%s.", content == NULL ? "" : content); |
| 294 |
|
| 295 |
language = oscap_text_get_lang(title); |
| 296 |
printf("%s,", language == NULL ? "" : language); |
| 297 |
} |
| 298 |
putchar(':'); |
| 299 |
oscap_text_iterator_free(title_it); |
| 300 |
print_expr_prefix_form(cpe_platform_get_expr(platform)); |
| 301 |
putchar('\n'); |
| 302 |
} |