| 1 | /* |
| 2 | * Copyright 2010--2014 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 | * Peter Vrabec <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:411:9`! |
| 24 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 25 | #endif |
| 26 | |
| 27 | /* Standard header files */ |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #ifdef HAVE_GETOPT_Hcxx . . $ `build/config.h:79:9`! |
| 32 | #include <getopt.h> |
| 33 | #endif |
| 34 | |
| 35 | /* CPE */ |
| 36 | #include <cpe_name.h>cxx . . $ `<file>/src/CPE/public/cpe_name.h`/ |
| 37 | #include <cpe_dict.h>cxx . . $ `<file>/src/CPE/public/cpe_dict.h`/ |
| 38 | #include <cpe_lang.h>cxx . . $ `<file>/src/CPE/public/cpe_lang.h`/ |
| 39 | #include <oscap_source.h>cxx . . $ `<file>/src/source/public/oscap_source.h`/ |
| 40 | |
| 41 | #include "oscap-tool.h"cxx . . $ `<file>/utils/oscap-tool.h`/ |
| 42 | |
| 43 | #define CPE_SUBMODULES_NUMcxx . . $ `utils/oscap-cpe.c:43:9`! 4 /* See actual CPE_SUBMODULES array |
| 44 | initialization below. */ |
| 45 | static struct oscap_modulecxx . . $ oscap_module#* CPE_SUBMODULEScxx . . $ CPE_SUBMODULES.[CPE_SUBMODULES_NUMcxx . . $ `utils/oscap-cpe.c:43:9`!]; |
| 46 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! getopt_cpecxx . . $ getopt_cpe(93e8ba72609212ff).(int argc, char **argv, struct oscap_actioncxx . . $ oscap_action# *action); |
| 47 | int app_cpe_checkcxx . . $ app_cpe_check(d99c68634f61e371).(const struct oscap_actioncxx . . $ oscap_action# *action); |
| 48 | int app_cpe_matchcxx . . $ app_cpe_match(d99c68634f61e371).(const struct oscap_actioncxx . . $ oscap_action# *action); |
| 49 | int app_cpe_validatecxx . . $ app_cpe_validate(d99c68634f61e371).(const struct oscap_actioncxx . . $ oscap_action# *action); |
| 50 | |
| 51 | struct oscap_modulecxx . . $ oscap_module# OSCAP_CPE_MODULEcxx . . $ OSCAP_CPE_MODULE. = { |
| 52 | .name = "cpe", |
| 53 | .parent = &OSCAP_ROOT_MODULEcxx . . $ OSCAP_ROOT_MODULE., |
| 54 | .summary = "Common Platform Enumeration", |
| 55 | .submodules = CPE_SUBMODULEScxx . . $ CPE_SUBMODULES. |
| 56 | }; |
| 57 | |
| 58 | static struct oscap_modulecxx . . $ oscap_module# CPE_MATCH_MODULEcxx . . $ CPE_MATCH_MODULE. = { |
| 59 | .name = "match", |
| 60 | .parent = &OSCAP_CPE_MODULEcxx . . $ OSCAP_CPE_MODULE., |
| 61 | .summary = "Match CPE name against provided dictionary", |
| 62 | .usage = "name dictionary.xml", |
| 63 | .help = "Options:\n" |
| 64 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 65 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 66 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 67 | .opt_parser = getopt_cpecxx . . $ getopt_cpe(93e8ba72609212ff)., |
| 68 | .func = app_cpe_matchcxx . . $ app_cpe_match(d99c68634f61e371). |
| 69 | }; |
| 70 | |
| 71 | static struct oscap_modulecxx . . $ oscap_module# CPE_CHECK_MODULEcxx . . $ CPE_CHECK_MODULE. = { |
| 72 | .name = "check", |
| 73 | .parent = &OSCAP_CPE_MODULEcxx . . $ OSCAP_CPE_MODULE., |
| 74 | .summary = "Check if CPE name is valid", |
| 75 | .usage = "name", |
| 76 | .help = "Options:\n" |
| 77 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 78 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 79 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 80 | .opt_parser = getopt_cpecxx . . $ getopt_cpe(93e8ba72609212ff)., |
| 81 | .func = app_cpe_checkcxx . . $ app_cpe_check(d99c68634f61e371). |
| 82 | }; |
| 83 | |
| 84 | static struct oscap_modulecxx . . $ oscap_module# CPE_VALIDATEcxx . . $ CPE_VALIDATE. = { |
| 85 | .name = "validate", |
| 86 | .parent = &OSCAP_CPE_MODULEcxx . . $ OSCAP_CPE_MODULE., |
| 87 | .summary = "Validate CPE Dictionary content", |
| 88 | .usage = "cpe-dict.xml", |
| 89 | .help = "Options:\n" |
| 90 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 91 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 92 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 93 | .opt_parser = getopt_cpecxx . . $ getopt_cpe(93e8ba72609212ff)., |
| 94 | .func = app_cpe_validatecxx . . $ app_cpe_validate(d99c68634f61e371). |
| 95 | }; |
| 96 | |
| 97 | static struct oscap_modulecxx . . $ oscap_module#* CPE_SUBMODULEScxx . . $ CPE_SUBMODULES.[CPE_SUBMODULES_NUMcxx . . $ `utils/oscap-cpe.c:43:9`!] = { |
| 98 | &CPE_MATCH_MODULEcxx . . $ CPE_MATCH_MODULE., |
| 99 | &CPE_CHECK_MODULEcxx . . $ CPE_CHECK_MODULE., |
| 100 | &CPE_VALIDATEcxx . . $ CPE_VALIDATE., |
| 101 | NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`! |
| 102 | }; |
| 103 | |
| 104 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! getopt_cpecxx . . $ getopt_cpe(93e8ba72609212ff).(int argc, char **argv, struct oscap_actioncxx . . $ oscap_action# *action) { |
| 105 | enum oscap_cpe_optscxx . . $ getopt_cpe(93e8ba72609212ff).oscap_cpe_opts# { |
| 106 | OSCAP_CPE_OPT_VERBOSEcxx . . $ getopt_cpe(93e8ba72609212ff).OSCAP_CPE_OPT_VERBOSE. = 1, |
| 107 | OSCAP_CPE_OPT_VERBOSE_LOG_FILEcxx . . $ getopt_cpe(93e8ba72609212ff).OSCAP_CPE_OPT_VERBOSE_LOG_FILE., |
| 108 | }; |
| 109 | const struct optioncxx . . $ option# long_options[] = { |
| 110 | // options |
| 111 | {"verbose", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, OSCAP_CPE_OPT_VERBOSEcxx . . $ getopt_cpe(93e8ba72609212ff).OSCAP_CPE_OPT_VERBOSE.}, |
| 112 | {"verbose-log-file", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!, OSCAP_CPE_OPT_VERBOSE_LOG_FILEcxx . . $ getopt_cpe(93e8ba72609212ff).OSCAP_CPE_OPT_VERBOSE_LOG_FILE.}, |
| 113 | // end |
| 114 | {0, 0, 0, 0} |
| 115 | }; |
| 116 | int c; |
| 117 | while ((c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "o:i:", long_options, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!)) != -1) { |
| 118 | switch (c) { |
| 119 | case OSCAP_CPE_OPT_VERBOSEcxx . . $ getopt_cpe(93e8ba72609212ff).OSCAP_CPE_OPT_VERBOSE.: |
| 120 | action->verbosity_levelcxx . . $ oscap_action#verbosity_level. = optargcxx . . $ optarg.; |
| 121 | break; |
| 122 | case OSCAP_CPE_OPT_VERBOSE_LOG_FILEcxx . . $ getopt_cpe(93e8ba72609212ff).OSCAP_CPE_OPT_VERBOSE_LOG_FILE.: |
| 123 | action->f_verbose_logcxx . . $ oscap_action#f_verbose_log. = optargcxx . . $ optarg.; |
| 124 | break; |
| 125 | case 0: break; |
| 126 | default: return oscap_module_usagecxx . . $ oscap_module_usage(8d148a6468c518c9).(action->modulecxx . . $ oscap_action#module., stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 127 | } |
| 128 | } |
| 129 | if (optindcxx . . $ optind. >= argc) { |
| 130 | return oscap_module_usagecxx . . $ oscap_module_usage(8d148a6468c518c9).(action->modulecxx . . $ oscap_action#module., stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Wrong number of arguments!\n"); |
| 131 | } |
| 132 | action->cpe_actioncxx . . $ oscap_action#cpe_action. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(struct cpe_actioncxx . . $ cpe_action#)); |
| 133 | if (action->cpe_actioncxx . . $ oscap_action#cpe_action. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 134 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Memory allocation error.\n"); |
| 135 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 136 | } |
| 137 | if (action->modulecxx . . $ oscap_action#module. == &CPE_MATCH_MODULEcxx . . $ CPE_MATCH_MODULE.) { |
| 138 | action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name. = argv[optindcxx . . $ optind.]; |
| 139 | action->cpe_actioncxx . . $ oscap_action#cpe_action.->dictcxx . . $ cpe_action#dict. = argv[optindcxx . . $ optind. + 1]; |
| 140 | } |
| 141 | if (action->modulecxx . . $ oscap_action#module. == &CPE_CHECK_MODULEcxx . . $ CPE_CHECK_MODULE.) { |
| 142 | action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name. = argv[optindcxx . . $ optind.]; |
| 143 | } |
| 144 | if (action->modulecxx . . $ oscap_action#module. == &CPE_VALIDATEcxx . . $ CPE_VALIDATE.) { |
| 145 | action->cpe_actioncxx . . $ oscap_action#cpe_action.->dictcxx . . $ cpe_action#dict. = argv[optindcxx . . $ optind.]; |
| 146 | } |
| 147 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 148 | } |
| 149 | |
| 150 | int app_cpe_checkcxx . . $ app_cpe_check(d99c68634f61e371).(const struct oscap_actioncxx . . $ oscap_action# *action) { |
| 151 | int ret; |
| 152 | |
| 153 | if (!cpe_name_checkcxx . . $ cpe_name_check(c5f7da30cbc0f4e9).(action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name.)) { |
| 154 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!,"'%s' is NOT Valid CPE name.\n", action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name.); |
| 155 | ret = OSCAP_FAILcxx . . $ OSCAP_FAIL.; |
| 156 | } |
| 157 | else { |
| 158 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!,"'%s' is Valid CPE name.\n", action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name.); |
| 159 | ret = OSCAP_OKcxx . . $ OSCAP_OK.; |
| 160 | } |
| 161 | freecxx . . $ free(5d22bdacc48458e8).(action->cpe_actioncxx . . $ oscap_action#cpe_action.); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | int app_cpe_matchcxx . . $ app_cpe_match(d99c68634f61e371).(const struct oscap_actioncxx . . $ oscap_action# *action) { |
| 166 | |
| 167 | int ret; |
| 168 | struct cpe_namecxx . . $ cpe_name# *candidate_cpe = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 169 | struct cpe_dict_modelcxx . . $ cpe_dict_model# *dict = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 170 | |
| 171 | struct oscap_sourcecxx . . $ oscap_source# *source = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 172 | |
| 173 | /* is CPE well formated? */ |
| 174 | if( ! cpe_name_checkcxx . . $ cpe_name_check(c5f7da30cbc0f4e9).(action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name.) ) { |
| 175 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s is not in valid CPE format.\n", action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name.); |
| 176 | ret = OSCAP_ERRORcxx . . $ OSCAP_ERROR.; |
| 177 | goto clean; |
| 178 | } |
| 179 | candidate_cpe = cpe_name_newcxx . . $ cpe_name_new(dfbdfc9fcef857b).(action->cpe_actioncxx . . $ oscap_action#cpe_action.->namecxx . . $ cpe_action#name.); |
| 180 | |
| 181 | /* load dictionary */ |
| 182 | source = oscap_source_new_from_filecxx . . $ oscap_source_new_from_file(987f8b70a317ffb9).(action->cpe_actioncxx . . $ oscap_action#cpe_action.->dictcxx . . $ cpe_action#dict.); |
| 183 | if( (dict = cpe_dict_model_import_sourcecxx . . $ cpe_dict_model_import_source(855462c7c482ecd).(source)) == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`! ) { |
| 184 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "can't load CPE dictionary from: %s.\n", action->cpe_actioncxx . . $ oscap_action#cpe_action.->dictcxx . . $ cpe_action#dict.); |
| 185 | ret = OSCAP_ERRORcxx . . $ OSCAP_ERROR.; |
| 186 | goto clean; |
| 187 | } |
| 188 | |
| 189 | /* matching */ |
| 190 | if( cpe_name_match_dictcxx . . $ cpe_name_match_dict(d2c432d3e95f5fe5).(candidate_cpe, dict) ) { |
| 191 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "The exact CPE match is found.\n"); |
| 192 | ret = OSCAP_OKcxx . . $ OSCAP_OK.; |
| 193 | } |
| 194 | else { |
| 195 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "No match found.\n"); |
| 196 | ret = OSCAP_FAILcxx . . $ OSCAP_FAIL.; |
| 197 | } |
| 198 | |
| 199 | /* clean up */ |
| 200 | clean: |
| 201 | cpe_name_freecxx . . $ cpe_name_free(6799d02d05c0f5d7).(candidate_cpe); |
| 202 | cpe_dict_model_freecxx . . $ cpe_dict_model_free(99656294ab2c75d7).(dict); |
| 203 | freecxx . . $ free(5d22bdacc48458e8).(action->cpe_actioncxx . . $ oscap_action#cpe_action.); |
| 204 | oscap_source_freecxx . . $ oscap_source_free(1a322e80279420f6).(source); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | int app_cpe_validatecxx . . $ app_cpe_validate(d99c68634f61e371).(const struct oscap_actioncxx . . $ oscap_action# *action) { |
| 209 | |
| 210 | int ret; |
| 211 | int result; |
| 212 | |
| 213 | struct oscap_sourcecxx . . $ oscap_source# *source = oscap_source_new_from_filecxx . . $ oscap_source_new_from_file(987f8b70a317ffb9).(action->cpe_actioncxx . . $ oscap_action#cpe_action.->dictcxx . . $ cpe_action#dict.); |
| 214 | ret = oscap_source_validatecxx . . $ oscap_source_validate(58e1cfedb30f1c20).(source, reportercxx . . $ reporter(710d628f83af7da6)., (void *) action); |
| 215 | oscap_source_freecxx . . $ oscap_source_free(1a322e80279420f6).(source); |
| 216 | |
| 217 | if (ret==-1) { |
| 218 | result=OSCAP_ERRORcxx . . $ OSCAP_ERROR.; |
| 219 | goto cleanup; |
| 220 | } |
| 221 | else if (ret==1) { |
| 222 | result=OSCAP_FAILcxx . . $ OSCAP_FAIL.; |
| 223 | } |
| 224 | else { |
| 225 | result=OSCAP_OKcxx . . $ OSCAP_OK.; |
| 226 | } |
| 227 | |
| 228 | cleanup: |
| 229 | if (oscap_errcxx . . $ oscap_err(c9857a73d68e59ba).()) |
| 230 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s %s\n", OSCAP_ERR_MSGcxx . . $ `utils/oscap-tool.h:50:9`!, oscap_err_desccxx . . $ oscap_err_desc(33a1f659e94feb42).()); |
| 231 | |
| 232 | freecxx . . $ free(5d22bdacc48458e8).(action->cpe_actioncxx . . $ oscap_action#cpe_action.); |
| 233 | return result; |
| 234 | } |
| 235 |