| 1 | /* |
| 2 | * Copyright 2016 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 | * Šimon Lukašík |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`! |
| 24 | #include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 25 | #endif |
| 26 | |
| 27 | #include "common/list.h"cxx . . $ `<file>/src/common/list.h`/ |
| 28 | #include "public/oscap_text.h"cxx . . $ `<file>/src/common/public/oscap_text.h`/ |
| 29 | #include "public/xccdf_benchmark.h"cxx . . $ `<file>/src/XCCDF/public/xccdf_benchmark.h`/ |
| 30 | #include "public/xccdf_policy.h"cxx . . $ `<file>/src/XCCDF_POLICY/public/xccdf_policy.h`/ |
| 31 | #include "xccdf_policy_model_priv.h"cxx . . $ `<file>/src/XCCDF_POLICY/xccdf_policy_model_priv.h`/ |
| 32 | #include "xccdf_policy_priv.h"cxx . . $ `<file>/src/XCCDF_POLICY/xccdf_policy_priv.h`/ |
| 33 | #include "XCCDF/item.h"cxx . . $ `<file>/src/XCCDF/item.h`/ |
| 34 | #include "XCCDF/helpers.h"cxx . . $ `<file>/src/XCCDF/helpers.h`/ |
| 35 | |
| 36 | struct xccdf_policycxx . . $ xccdf_policy# *xccdf_policy_model_get_existing_policy_by_idcxx . . $ xccdf_policy_model_get_existing_policy_by_id(3a48227fe1c1225a).(struct xccdf_policy_modelcxx . . $ xccdf_policy_model# *policy_model, const char *profile_id) |
| 37 | { |
| 38 | struct xccdf_policy_iteratorcxx . . $ xccdf_policy_iterator# *policy_it = xccdf_policy_model_get_policiescxx . . $ xccdf_policy_model_get_policies(be74bc7f05eb02a1).(policy_model); |
| 39 | while (xccdf_policy_iterator_has_morecxx . . $ xccdf_policy_iterator_has_more(b3fcdd1f80d76e47).(policy_it)) { |
| 40 | struct xccdf_policycxx . . $ xccdf_policy# *policy = xccdf_policy_iterator_nextcxx . . $ xccdf_policy_iterator_next(7fa9cb6b6a9fe806).(policy_it); |
| 41 | if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(xccdf_policy_get_idcxx . . $ xccdf_policy_get_id(6eb33604fdb7ae98).(policy), profile_id)) { |
| 42 | xccdf_policy_iterator_freecxx . . $ xccdf_policy_iterator_free(114314e6e2b7097).(policy_it); |
| 43 | return policy; |
| 44 | } |
| 45 | } |
| 46 | xccdf_policy_iterator_freecxx . . $ xccdf_policy_iterator_free(114314e6e2b7097).(policy_it); |
| 47 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 48 | } |
| 49 | |
| 50 | static void _add_selectors_for_all_xccdf_itemscxx . . $ _add_selectors_for_all_xccdf_items(a14812176f99f405).(struct xccdf_profilecxx . . $ xccdf_profile# *profile, struct xccdf_itemcxx . . $ xccdf_item# *item) |
| 51 | { |
| 52 | struct xccdf_item_iteratorcxx . . $ xccdf_item_iterator# *children = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 53 | if (xccdf_item_get_typecxx . . $ xccdf_item_get_type(3d8d48c673cd637b).(item) == XCCDF_BENCHMARKcxx . . $ XCCDF_BENCHMARK.) { |
| 54 | children = xccdf_benchmark_get_contentcxx . . $ xccdf_benchmark_get_content(630a032521212bb1).(XBENCHMARKcxx . . $ `src/XCCDF/helpers.h:31:9`!(item)); |
| 55 | } else if (xccdf_item_get_typecxx . . $ xccdf_item_get_type(3d8d48c673cd637b).(item) == XCCDF_GROUPcxx . . $ XCCDF_GROUP.) { |
| 56 | children = xccdf_group_get_contentcxx . . $ xccdf_group_get_content(cff1b1d0a9b9d63).(XGROUPcxx . . $ `src/XCCDF/helpers.h:33:9`!(item)); |
| 57 | } |
| 58 | |
| 59 | if (xccdf_item_get_typecxx . . $ xccdf_item_get_type(3d8d48c673cd637b).(item) == XCCDF_RULEcxx . . $ XCCDF_RULE. || |
| 60 | xccdf_item_get_typecxx . . $ xccdf_item_get_type(3d8d48c673cd637b).(item) == XCCDF_GROUPcxx . . $ XCCDF_GROUP.) |
| 61 | { |
| 62 | struct xccdf_selectcxx . . $ xccdf_select# *select = xccdf_select_newcxx . . $ xccdf_select_new(b5dc88cd3a862c53).(); |
| 63 | xccdf_select_set_itemcxx . . $ xccdf_select_set_item(efa6a679d289dde7).(select, xccdf_item_get_idcxx . . $ xccdf_item_get_id(b87246382a73d514).(item)); |
| 64 | xccdf_select_set_selectedcxx . . $ xccdf_select_set_selected(d8fd08941cc98481).(select, truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!); |
| 65 | xccdf_profile_add_selectcxx . . $ xccdf_profile_add_select(4e3e50f53c63f7f4).(profile, select); |
| 66 | } |
| 67 | |
| 68 | if (children) { |
| 69 | while (xccdf_item_iterator_has_morecxx . . $ xccdf_item_iterator_has_more(c8a83ac60f16d388).(children)) { |
| 70 | struct xccdf_itemcxx . . $ xccdf_item# *current = xccdf_item_iterator_nextcxx . . $ xccdf_item_iterator_next(73e3c3f4fb48321d).(children); |
| 71 | _add_selectors_for_all_xccdf_itemscxx . . $ _add_selectors_for_all_xccdf_items(a14812176f99f405).(profile, current); |
| 72 | } |
| 73 | xccdf_item_iterator_freecxx . . $ xccdf_item_iterator_free(5a27e088a1e93c63).(children); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | struct xccdf_policycxx . . $ xccdf_policy# *xccdf_policy_model_create_policy_by_idcxx . . $ xccdf_policy_model_create_policy_by_id(3a48227fe1c1225a).(struct xccdf_policy_modelcxx . . $ xccdf_policy_model# *policy_model, const char *id) |
| 78 | { |
| 79 | struct xccdf_profilecxx . . $ xccdf_profile# *profile = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 80 | struct xccdf_tailoringcxx . . $ xccdf_tailoring# *tailoring = xccdf_policy_model_get_tailoringcxx . . $ xccdf_policy_model_get_tailoring(635dafb53c61c9dd).(policy_model); |
| 81 | |
| 82 | // Tailoring profiles take precedence over Benchmark profiles. |
| 83 | if (tailoring) { |
| 84 | profile = xccdf_tailoring_get_profile_by_idcxx . . $ xccdf_tailoring_get_profile_by_id(a3cce76bafe7a7e1).(tailoring, id); |
| 85 | } |
| 86 | |
| 87 | // The (default) and (all) profiles are de-facto owned by the xccdf_policy |
| 88 | // and will be freed by it when it's freed. See xccdf_policy_free. |
| 89 | |
| 90 | if (!profile) { |
| 91 | if (id == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 92 | profile = xccdf_profile_newcxx . . $ xccdf_profile_new(11099779a351e242).(); |
| 93 | xccdf_profile_set_idcxx . . $ xccdf_profile_set_id(512554f4e1cc87f7).(profile, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!); |
| 94 | struct oscap_textcxx . . $ oscap_text# * title = oscap_text_newcxx . . $ oscap_text_new(b563511806f9674e).(); |
| 95 | oscap_text_set_textcxx . . $ oscap_text_set_text(8aa69a4864e2c57).(title, "No profile (default benchmark)"); |
| 96 | oscap_text_set_langcxx . . $ oscap_text_set_lang(8aa69a4864e2c57).(title, "en"); |
| 97 | xccdf_profile_add_titlecxx . . $ xccdf_profile_add_title(553bf65be3b28573).(profile, title); |
| 98 | } else { |
| 99 | struct xccdf_benchmarkcxx . . $ xccdf_benchmark# *benchmark = xccdf_policy_model_get_benchmarkcxx . . $ xccdf_policy_model_get_benchmark(a911a8e37770e756).(policy_model); |
| 100 | if (benchmark == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 101 | assertcxx . . $ `/usr/include/assert.h:50:10`!(benchmark != NULL); |
| 102 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 103 | } |
| 104 | |
| 105 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(id, "(all)") == 0) { |
| 106 | profile = xccdf_profile_newcxx . . $ xccdf_profile_new(11099779a351e242).(); |
| 107 | xccdf_profile_set_idcxx . . $ xccdf_profile_set_id(512554f4e1cc87f7).(profile, "(all)"); |
| 108 | struct oscap_textcxx . . $ oscap_text# *title = oscap_text_newcxx . . $ oscap_text_new(b563511806f9674e).(); |
| 109 | oscap_text_set_textcxx . . $ oscap_text_set_text(8aa69a4864e2c57).(title, "(all) profile (all rules selected)"); |
| 110 | oscap_text_set_langcxx . . $ oscap_text_set_lang(8aa69a4864e2c57).(title, "en"); |
| 111 | xccdf_profile_add_titlecxx . . $ xccdf_profile_add_title(553bf65be3b28573).(profile, title); |
| 112 | |
| 113 | _add_selectors_for_all_xccdf_itemscxx . . $ _add_selectors_for_all_xccdf_items(a14812176f99f405).(profile, XITEMcxx . . $ `src/XCCDF/helpers.h:35:9`!(benchmark)); |
| 114 | } else { |
| 115 | profile = xccdf_benchmark_get_profile_by_idcxx . . $ xccdf_benchmark_get_profile_by_id(34bf61e7e9f7ecd5).(benchmark, id); |
| 116 | if (profile == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) |
| 117 | return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return xccdf_policy_newcxx . . $ xccdf_policy_new(975c97c2cbf6e97).(policy_model, profile); |
| 123 | } |
| 124 | |
| 125 | static int _xccdf_policy_model_create_policy_if_usefulcxx . . $ _xccdf_policy_model_create_policy_if_useful(89dabf3d19053a61).(struct xccdf_policy_modelcxx . . $ xccdf_policy_model# *policy_model, const char *profile_id) |
| 126 | { |
| 127 | if (xccdf_policy_model_get_existing_policy_by_idcxx . . $ xccdf_policy_model_get_existing_policy_by_id(3a48227fe1c1225a).(policy_model, profile_id) == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 128 | struct xccdf_policycxx . . $ xccdf_policy# *policy = xccdf_policy_model_create_policy_by_idcxx . . $ xccdf_policy_model_create_policy_by_id(3a48227fe1c1225a).(policy_model, profile_id); |
| 129 | if (policy == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 130 | return -1; |
| 131 | } |
| 132 | if (xccdf_policy_get_selected_rules_countcxx . . $ xccdf_policy_get_selected_rules_count(6f00885c5a9cd26).(policy) > 0) { |
| 133 | xccdf_policy_model_add_policycxx . . $ xccdf_policy_model_add_policy(1e50a67d9f9f5e2f).(policy_model, policy); |
| 134 | } |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int _xccdf_policy_model_create_policies_if_usefulcxx . . $ _xccdf_policy_model_create_policies_if_useful(d700e8e99fdcb475).(struct xccdf_policy_modelcxx . . $ xccdf_policy_model# *policy_model, struct xccdf_profile_iteratorcxx . . $ xccdf_profile_iterator# *profit) |
| 140 | { |
| 141 | while (xccdf_profile_iterator_has_morecxx . . $ xccdf_profile_iterator_has_more(adef36931cdf4bdd).(profit)) { |
| 142 | struct xccdf_profilecxx . . $ xccdf_profile# *profile = xccdf_profile_iterator_nextcxx . . $ xccdf_profile_iterator_next(4851320650739603).(profit); |
| 143 | const char *profile_id = xccdf_profile_get_idcxx . . $ xccdf_profile_get_id(d143a4aa05fc37ce).(profile); |
| 144 | if (_xccdf_policy_model_create_policy_if_usefulcxx . . $ _xccdf_policy_model_create_policy_if_useful(89dabf3d19053a61).(policy_model, profile_id) != 0) { |
| 145 | xccdf_profile_iterator_freecxx . . $ xccdf_profile_iterator_free(e346a4b111941870).(profit); |
| 146 | return -1; |
| 147 | } |
| 148 | } |
| 149 | xccdf_profile_iterator_freecxx . . $ xccdf_profile_iterator_free(e346a4b111941870).(profit); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int xccdf_policy_model_build_all_useful_policiescxx . . $ xccdf_policy_model_build_all_useful_policies(126aaa904dc3a390).(struct xccdf_policy_modelcxx . . $ xccdf_policy_model# *policy_model) |
| 154 | { |
| 155 | if (_xccdf_policy_model_create_policy_if_usefulcxx . . $ _xccdf_policy_model_create_policy_if_useful(89dabf3d19053a61).(policy_model, NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) != 0) { |
| 156 | return -1; |
| 157 | } |
| 158 | struct xccdf_tailoringcxx . . $ xccdf_tailoring# *tailoring = xccdf_policy_model_get_tailoringcxx . . $ xccdf_policy_model_get_tailoring(635dafb53c61c9dd).(policy_model); |
| 159 | if (tailoring != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 160 | struct xccdf_profile_iteratorcxx . . $ xccdf_profile_iterator# *profit = xccdf_tailoring_get_profilescxx . . $ xccdf_tailoring_get_profiles(41ce0c0fc277fbfc).(tailoring); |
| 161 | if (_xccdf_policy_model_create_policies_if_usefulcxx . . $ _xccdf_policy_model_create_policies_if_useful(d700e8e99fdcb475).(policy_model, profit) != 0) { |
| 162 | return -1; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | struct xccdf_benchmarkcxx . . $ xccdf_benchmark# *benchmark = xccdf_policy_model_get_benchmarkcxx . . $ xccdf_policy_model_get_benchmark(a911a8e37770e756).(policy_model); |
| 167 | if (benchmark == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) { |
| 168 | return -1; |
| 169 | } |
| 170 | struct xccdf_profile_iteratorcxx . . $ xccdf_profile_iterator# *profit = xccdf_benchmark_get_profilescxx . . $ xccdf_benchmark_get_profiles(c49c4abda143290c).(benchmark); |
| 171 | if (_xccdf_policy_model_create_policies_if_usefulcxx . . $ _xccdf_policy_model_create_policies_if_useful(d700e8e99fdcb475).(policy_model, profit) != 0) { |
| 172 | return -1; |
| 173 | } |
| 174 | return 0; |
| 175 | } |