| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifdef HAVE_CONFIG_H |
| 12 |
#include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <cpe_dict.h> |
| 16 |
#include <cpe_name.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#define OSCAP_FOREACH_GENERIC(itype, vtype, val, init_val, code) \ |
| 20 |
{ \ |
| 21 |
struct itype##_iterator *val##_iter = (init_val); \ |
| 22 |
vtype val; \ |
| 23 |
while (itype##_iterator_has_more(val##_iter)) { \ |
| 24 |
val = itype##_iterator_next(val##_iter); \ |
| 25 |
code \ |
| 26 |
} \ |
| 27 |
itype##_iterator_free(val##_iter); \ |
| 28 |
} |
| 29 |
|
| 30 |
#define OSCAP_FOREACH(type, val, init_val, code) \ |
| 31 |
OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code) |
| 32 |
|
| 33 |
void print_usage(const char *, FILE *); |
| 34 |
|
| 35 |
int main(int argc, char **argv) |
| 36 |
{ |
| 37 |
struct cpe_dict_model *dict_model; |
| 38 |
struct cpe_generator *generator; |
| 39 |
struct cpe_vendor *vendor; |
| 40 |
struct cpe_product *product; |
| 41 |
struct cpe_version *version; |
| 42 |
struct cpe_update *update; |
| 43 |
struct cpe_edition *edition; |
| 44 |
struct cpe_language *language; |
| 45 |
struct cpe_check *check; |
| 46 |
struct cpe_item *item; |
| 47 |
struct cpe_reference *reference; |
| 48 |
struct cpe_vendor_iterator *vendors_it; |
| 49 |
struct cpe_product_iterator *products_it; |
| 50 |
|
| 51 |
|
| 52 |
struct cpe_version_iterator *versions_it; |
| 53 |
struct cpe_update_iterator *updates_it; |
| 54 |
struct cpe_edition_iterator *editions_it; |
| 55 |
struct cpe_language_iterator *languages_it; |
| 56 |
struct oscap_text *title; |
| 57 |
struct oscap_text_iterator *titles_it; |
| 58 |
|
| 59 |
struct cpe_name *name = NULL; |
| 60 |
int ret_val = 0; |
| 61 |
bool ret_val_1 = false; |
| 62 |
|
| 63 |
if (argc == 2 && !strcmp(argv[1], "--help")) |
| 64 |
print_usage(argv[0], stdout); |
| 65 |
|
| 66 |
else if (argc == 4 && !strcmp(argv[1], "--list-cpe-names")) { |
| 67 |
|
| 68 |
struct oscap_source *source = oscap_source_new_from_file(argv[2]); |
| 69 |
if ((dict_model = cpe_dict_model_import_source(source)) == NULL) { |
| 70 |
oscap_source_free(source); |
| 71 |
return 2; |
| 72 |
} |
| 73 |
|
| 74 |
OSCAP_FOREACH(cpe_item, local_item, |
| 75 |
cpe_dict_model_get_items(dict_model), |
| 76 |
cpe_name_write(cpe_item_get_name(local_item), |
| 77 |
stdout); |
| 78 |
putchar('\n');) |
| 79 |
|
| 80 |
cpe_dict_model_free(dict_model); |
| 81 |
oscap_source_free(source); |
| 82 |
} |
| 83 |
|
| 84 |
else if (argc == 4 && !strcmp(argv[1], "--list")) { |
| 85 |
|
| 86 |
struct oscap_source *source = oscap_source_new_from_file(argv[2]); |
| 87 |
if ((dict_model = cpe_dict_model_import_source(source)) == NULL) { |
| 88 |
oscap_source_free(source); |
| 89 |
return 2; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
generator = cpe_dict_model_get_generator(dict_model); |
| 94 |
printf("%s:%s:%s:%s\n", |
| 95 |
cpe_generator_get_timestamp(generator), |
| 96 |
cpe_generator_get_product_name(generator), |
| 97 |
cpe_generator_get_product_version(generator), |
| 98 |
cpe_generator_get_schema_version(generator)); |
| 99 |
|
| 100 |
|
| 101 |
vendors_it = cpe_dict_model_get_vendors(dict_model); |
| 102 |
while (cpe_vendor_iterator_has_more(vendors_it)) { |
| 103 |
vendor = cpe_vendor_iterator_next(vendors_it); |
| 104 |
printf("%s:(", cpe_vendor_get_value(vendor)); |
| 105 |
|
| 106 |
|
| 107 |
titles_it = cpe_vendor_get_titles(vendor); |
| 108 |
while (oscap_text_iterator_has_more(titles_it)) { |
| 109 |
title = oscap_text_iterator_next(titles_it); |
| 110 |
printf("%s,", oscap_text_get_text(title)); |
| 111 |
} |
| 112 |
oscap_text_iterator_free(titles_it); |
| 113 |
putchar(')'); |
| 114 |
putchar(':'); |
| 115 |
|
| 116 |
|
| 117 |
products_it = cpe_vendor_get_products(vendor); |
| 118 |
while (cpe_product_iterator_has_more(products_it)) { |
| 119 |
product = |
| 120 |
cpe_product_iterator_next(products_it); |
| 121 |
printf("(%s,%d,(", |
| 122 |
cpe_product_get_value(product), |
| 123 |
cpe_product_get_part(product)); |
| 124 |
|
| 125 |
|
| 126 |
versions_it = cpe_product_get_versions(product); |
| 127 |
while (cpe_version_iterator_has_more |
| 128 |
(versions_it)) { |
| 129 |
version = |
| 130 |
cpe_version_iterator_next |
| 131 |
(versions_it); |
| 132 |
printf("%s,(", |
| 133 |
cpe_version_get_value(version)); |
| 134 |
|
| 135 |
|
| 136 |
updates_it = |
| 137 |
cpe_version_get_updates(version); |
| 138 |
while (cpe_update_iterator_has_more |
| 139 |
(updates_it)) { |
| 140 |
update = |
| 141 |
cpe_update_iterator_next |
| 142 |
(updates_it); |
| 143 |
printf("%s,(", |
| 144 |
cpe_update_get_value |
| 145 |
(update)); |
| 146 |
|
| 147 |
|
| 148 |
editions_it = |
| 149 |
cpe_update_get_editions |
| 150 |
(update); |
| 151 |
while |
| 152 |
(cpe_edition_iterator_has_more |
| 153 |
(editions_it)) { |
| 154 |
edition = |
| 155 |
cpe_edition_iterator_next |
| 156 |
(editions_it); |
| 157 |
printf("%s,(", |
| 158 |
cpe_edition_get_value |
| 159 |
(edition)); |
| 160 |
|
| 161 |
|
| 162 |
languages_it = |
| 163 |
cpe_edition_get_languages |
| 164 |
(edition); |
| 165 |
while |
| 166 |
(cpe_language_iterator_has_more |
| 167 |
(languages_it)) { |
| 168 |
language = |
| 169 |
cpe_language_iterator_next |
| 170 |
(languages_it); |
| 171 |
printf("%s,(", |
| 172 |
cpe_language_get_value |
| 173 |
(language)); |
| 174 |
} |
| 175 |
putchar(')'); |
| 176 |
} |
| 177 |
putchar(')'); |
| 178 |
} |
| 179 |
putchar(')'); |
| 180 |
} |
| 181 |
putchar(')'); |
| 182 |
} |
| 183 |
putchar(')'); |
| 184 |
putchar('\n'); |
| 185 |
} |
| 186 |
cpe_dict_model_free(dict_model); |
| 187 |
oscap_source_free(source); |
| 188 |
} |
| 189 |
|
| 190 |
else if (argc == 5 && !strcmp(argv[1], "--match")) { |
| 191 |
|
| 192 |
struct oscap_source *source = oscap_source_new_from_file(argv[2]); |
| 193 |
if ((dict_model = cpe_dict_model_import_source(source)) == NULL) { |
| 194 |
oscap_source_free(source); |
| 195 |
return 2; |
| 196 |
} |
| 197 |
|
| 198 |
name = cpe_name_new(argv[4]); |
| 199 |
|
| 200 |
ret_val_1 = cpe_name_match_dict(name, dict_model); |
| 201 |
|
| 202 |
ret_val = (ret_val_1 == false) ? 1 : 0; |
| 203 |
|
| 204 |
cpe_name_free(name); |
| 205 |
cpe_dict_model_free(dict_model); |
| 206 |
oscap_source_free(source); |
| 207 |
} |
| 208 |
|
| 209 |
else if (argc == 5 && !strcmp(argv[1], "--remove")) { |
| 210 |
|
| 211 |
struct oscap_source *source = oscap_source_new_from_file(argv[2]); |
| 212 |
if ((dict_model = cpe_dict_model_import_source(source)) == NULL) { |
| 213 |
oscap_source_free(source); |
| 214 |
return 2; |
| 215 |
} |
| 216 |
|
| 217 |
name = cpe_name_new(argv[4]); |
| 218 |
|
| 219 |
OSCAP_FOREACH(cpe_item, local_item, |
| 220 |
cpe_dict_model_get_items(dict_model), |
| 221 |
|
| 222 |
if (cpe_name_match_one |
| 223 |
(name, cpe_item_get_name(local_item))) |
| 224 |
cpe_item_iterator_remove(local_item_iter);) |
| 225 |
|
| 226 |
OSCAP_FOREACH(cpe_item, local_item, |
| 227 |
cpe_dict_model_get_items(dict_model), |
| 228 |
|
| 229 |
cpe_name_write(cpe_item_get_name |
| 230 |
(local_item), stdout); |
| 231 |
putchar('\n');) |
| 232 |
|
| 233 |
cpe_name_free(name); |
| 234 |
cpe_dict_model_free(dict_model); |
| 235 |
oscap_source_free(source); |
| 236 |
} |
| 237 |
|
| 238 |
else if (argc == 6 && !strcmp(argv[1], "--export")) { |
| 239 |
struct oscap_source *source = oscap_source_new_from_file(argv[2]); |
| 240 |
if ((dict_model = cpe_dict_model_import_source(source)) == NULL) { |
| 241 |
oscap_source_free(source); |
| 242 |
return 2; |
| 243 |
} |
| 244 |
|
| 245 |
cpe_dict_model_export(dict_model, argv[4]); |
| 246 |
|
| 247 |
cpe_dict_model_free(dict_model); |
| 248 |
oscap_source_free(source); |
| 249 |
} |
| 250 |
|
| 251 |
else if (argc == 2 && !strcmp(argv[1], "--smoke-test")) { |
| 252 |
if ((dict_model = cpe_dict_model_new()) != NULL) |
| 253 |
cpe_dict_model_free(dict_model); |
| 254 |
|
| 255 |
if ((generator = cpe_generator_new()) != NULL) |
| 256 |
cpe_generator_free(generator); |
| 257 |
|
| 258 |
if ((check = cpe_check_new()) != NULL) |
| 259 |
cpe_check_free(check); |
| 260 |
|
| 261 |
if ((reference = cpe_reference_new()) != NULL) |
| 262 |
cpe_reference_free(reference); |
| 263 |
|
| 264 |
if ((item = cpe_item_new()) != NULL) |
| 265 |
cpe_item_free(item); |
| 266 |
|
| 267 |
if ((vendor = cpe_vendor_new()) != NULL) |
| 268 |
cpe_vendor_free(vendor); |
| 269 |
|
| 270 |
if ((product = cpe_product_new()) != NULL) |
| 271 |
cpe_product_free(product); |
| 272 |
|
| 273 |
if ((version = cpe_version_new()) != NULL) |
| 274 |
cpe_version_free(version); |
| 275 |
|
| 276 |
if ((update = cpe_update_new()) != NULL) |
| 277 |
cpe_update_free(update); |
| 278 |
|
| 279 |
if ((edition = cpe_edition_new()) != NULL) |
| 280 |
cpe_edition_free(edition); |
| 281 |
|
| 282 |
if ((language = cpe_language_new()) != NULL) |
| 283 |
cpe_language_free(language); |
| 284 |
} |
| 285 |
|
| 286 |
else { |
| 287 |
print_usage(argv[0], stderr); |
| 288 |
ret_val = 3; |
| 289 |
} |
| 290 |
|
| 291 |
return ret_val; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
void print_usage(const char *program_name, FILE * out) |
| 296 |
{ |
| 297 |
fprintf(out, |
| 298 |
"Usage: \n\n" |
| 299 |
" %s --help\n" |
| 300 |
" %s --list-cpe-names CPE_DICT_XML ENCODING\n" |
| 301 |
" %s --list CPE_DICT_XML ENCODING\n" |
| 302 |
" %s --match CPE_DICT_XML ENCODING CPE_URI\n" |
| 303 |
" %s --remove CPE_DICT_XML ENCODING CPE_URI\n" |
| 304 |
" %s --export CPE_DICT_XML ENCODING CPE_DICT_XML ENCODING\n" |
| 305 |
" %s --smoke-test\n", |
| 306 |
program_name, program_name, program_name, program_name, |
| 307 |
program_name, program_name, program_name); |
| 308 |
} |