|
| 1 | +# |
| 2 | +# Copyright (c) 2016 Red Hat Inc. |
| 3 | +# |
| 4 | +# This software is licensed to you under the GNU General Public License, |
| 5 | +# version 2 (GPLv2). There is NO WARRANTY for this software, express or |
| 6 | +# implied, including the implied warranties of MERCHANTABILITY or FITNESS |
| 7 | +# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 |
| 8 | +# along with this software; if not, see |
| 9 | +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 10 | +# |
| 11 | + |
| 12 | +require 'openscap/source' |
| 13 | +require 'openscap/xccdf/profile' |
| 14 | + |
| 15 | +module OpenSCAP |
| 16 | + module Xccdf |
| 17 | + class Tailoring |
| 18 | + attr_reader :raw |
| 19 | + |
| 20 | + def initialize(source, benchmark) |
| 21 | + case source |
| 22 | + when OpenSCAP::Source |
| 23 | + @raw = OpenSCAP.xccdf_tailoring_import_source source.raw, benchmark |
| 24 | + else |
| 25 | + fail OpenSCAP::OpenSCAPError, "Cannot initialize OpenSCAP::Xccdf::Tailoring with '#{source}'" |
| 26 | + end |
| 27 | + OpenSCAP.raise! if @raw.null? |
| 28 | + end |
| 29 | + |
| 30 | + def profiles |
| 31 | + @profiles ||= profiles_init |
| 32 | + end |
| 33 | + |
| 34 | + def destroy |
| 35 | + OpenSCAP.xccdf_tailoring_free @raw |
| 36 | + @raw = nil |
| 37 | + end |
| 38 | + |
| 39 | + private |
| 40 | + |
| 41 | + def profiles_init |
| 42 | + profiles = {} |
| 43 | + profit = OpenSCAP.xccdf_tailoring_get_profiles raw |
| 44 | + while OpenSCAP.xccdf_profile_iterator_has_more profit |
| 45 | + profile_p = OpenSCAP.xccdf_profile_iterator_next profit |
| 46 | + profile = OpenSCAP::Xccdf::Profile.new profile_p |
| 47 | + profiles[profile.id] = profile |
| 48 | + end |
| 49 | + OpenSCAP.xccdf_profile_iterator_free profit |
| 50 | + profiles |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + attach_function :xccdf_tailoring_import_source, [:pointer, :pointer], :pointer |
| 56 | + attach_function :xccdf_tailoring_free, [:pointer], :void |
| 57 | + |
| 58 | + attach_function :xccdf_tailoring_get_profiles, [:pointer], :pointer |
| 59 | + attach_function :xccdf_profile_iterator_has_more, [:pointer], :bool |
| 60 | + attach_function :xccdf_profile_iterator_next, [:pointer], :pointer |
| 61 | + attach_function :xccdf_profile_iterator_free, [:pointer], :void |
| 62 | +end |
0 commit comments