openscap 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 074803ceb32fdfbe49ce4a347e166b33ae2737d7
4
- data.tar.gz: 628f26e6b604746130063faf1eecec3aaba118c0
3
+ metadata.gz: 22bf07db627e929232b8e13998a9dcc1697c6499
4
+ data.tar.gz: 44893a70952b97513875e80270852f3bdd5e40b4
5
5
  SHA512:
6
- metadata.gz: 6fdb0130e40eab32d48982683e0f01363936f8b5ad01d7be8ff7b41b86081529354ab7732f0dca12ea4c397547eb0deab5f080043c64176c534a651306568933
7
- data.tar.gz: 4e90b1f1ea0fc9e905879125001357e06f1c8ff64c3684f437193193e44cafb3607d7719bed4a04a4730230631b188582808a9f1e1ca6003184d3ac48648fd17
6
+ metadata.gz: f9ae19a6ec32d41bd800a656457417d14e5360b5a9ba394258eb7d2d13ff95b7c32f4d9d793d3cce0ccefec580376a68840767f1855b8a8ff0ee2be1119babf5
7
+ data.tar.gz: 246fe898e061abca50b86328d1ed89a5a360cec810d3624f45d72890e10e5be710cc03356b5d3f4c35208dec973ac93ed50b1aba6f909a9a620bfb82757375c0
data/README.md CHANGED
@@ -7,7 +7,8 @@ A FFI wrapper around the OpenSCAP library.
7
7
 
8
8
  Features/problems
9
9
  -------------
10
- Current version supports minimal set of functions needed to build own scanner.
10
+ Current version supports minimal set of functions needed to build own scanner. This module
11
+ is self documented by its test suite.
11
12
 
12
13
  Sample Scanner Implementation
13
14
  -------------
@@ -9,15 +9,24 @@
9
9
  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
10
10
  #
11
11
 
12
+ require 'openscap/ds/sds'
12
13
  require 'openscap/source'
14
+ require 'openscap/xccdf/testresult'
13
15
  require 'openscap/libc'
14
16
 
15
17
  module OpenSCAP
16
18
  module DS
17
19
  class Arf
18
- def initialize(input_filename)
19
- @source = OpenSCAP::Source.new(input_filename)
20
- @session = OpenSCAP.ds_rds_session_new_from_source @source.raw
20
+ attr_reader :source
21
+
22
+ def initialize(param)
23
+ case param
24
+ when String, Hash
25
+ @source = OpenSCAP::Source.new(param)
26
+ @session = OpenSCAP.ds_rds_session_new_from_source @source.raw
27
+ else
28
+ raise OpenSCAP::OpenSCAPError, "Cannot initialize OpenSCAP::DS:Arf with '#{param}'"
29
+ end
21
30
  if @session.null?
22
31
  OpenSCAP.raise!
23
32
  end
@@ -29,6 +38,23 @@ module OpenSCAP
29
38
  @source.destroy()
30
39
  end
31
40
 
41
+ def test_result(id=nil)
42
+ source = OpenSCAP.ds_rds_session_select_report(@session, id)
43
+ OpenSCAP.raise!() if source.nil?
44
+ OpenSCAP::Xccdf::TestResult.new(source)
45
+ end
46
+
47
+ def test_result=(tr)
48
+ source = tr.source
49
+ OpenSCAP.raise! unless OpenSCAP.ds_rds_session_replace_report_with_source(@session, source.raw) == 0
50
+ end
51
+
52
+ def report_request(id=nil)
53
+ source_p = OpenSCAP.ds_rds_session_select_report_request(@session, id)
54
+ source = OpenSCAP::Source.new source_p
55
+ OpenSCAP::DS::Sds.new(source)
56
+ end
57
+
32
58
  def html
33
59
  html_p = OpenSCAP.ds_rds_session_get_html_report @session
34
60
  OpenSCAP.raise! if OpenSCAP.error?
@@ -42,5 +68,8 @@ module OpenSCAP
42
68
 
43
69
  attach_function :ds_rds_session_new_from_source, [:pointer], :pointer
44
70
  attach_function :ds_rds_session_free, [:pointer], :void
71
+ attach_function :ds_rds_session_select_report, [:pointer, :string], :pointer
72
+ attach_function :ds_rds_session_replace_report_with_source, [:pointer, :pointer], :int
73
+ attach_function :ds_rds_session_select_report_request, [:pointer, :string], :pointer
45
74
  attach_function :ds_rds_session_get_html_report, [:pointer], :pointer
46
75
  end
@@ -0,0 +1,58 @@
1
+ #
2
+ # Copyright (c) 2014 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
+
14
+ module OpenSCAP
15
+ module DS
16
+ class Sds
17
+ attr_reader :raw
18
+
19
+ def initialize(param)
20
+ @raw = case param
21
+ when OpenSCAP::Source
22
+ @raw = OpenSCAP.ds_sds_session_new_from_source param.raw
23
+ when Hash
24
+ @raw = OpenSCAP.ds_sds_session_new_from_source param[:source].raw
25
+ end
26
+ OpenSCAP.raise! if @raw.null?
27
+ end
28
+
29
+ def select_checklist(p={})
30
+ source_p = OpenSCAP.ds_sds_session_select_checklist(@raw, p[:datastream_id], p[:component_id], nil)
31
+ OpenSCAP::Source.new source_p
32
+ end
33
+
34
+ def select_checklist!(p={})
35
+ checklist = select_checklist(p)
36
+ OpenSCAP.raise! if checklist.nil? or checklist.raw.null?
37
+ return checklist
38
+ end
39
+
40
+ def html_guide(profile=nil)
41
+ html = OpenSCAP.ds_sds_session_get_html_guide(@raw, profile)
42
+ OpenSCAP.raise! if html.nil?
43
+ return html
44
+ end
45
+
46
+ def destroy
47
+ OpenSCAP.ds_sds_session_free(@raw)
48
+ @raw = nil
49
+ end
50
+ end
51
+ end
52
+
53
+ attach_function :ds_sds_session_new_from_source, [:pointer], :pointer
54
+ attach_function :ds_sds_session_free, [:pointer], :void
55
+ attach_function :ds_sds_session_select_checklist, [:pointer, :string, :string, :string], :pointer
56
+ attach_function :ds_sds_session_get_html_guide, [:pointer, :string], :string
57
+
58
+ end
@@ -24,10 +24,12 @@ module OpenSCAP
24
24
  return err.null? ? nil : err.read_string()
25
25
  end
26
26
 
27
- def self.raise!
27
+ def self.raise!(msg=nil)
28
28
  err = get_full_error
29
29
  if err.nil?
30
- err = '(unknown error)'
30
+ err = msg.nil? ? '(unknown error)' : msg
31
+ else
32
+ err += "\n#{msg}"
31
33
  end
32
34
  raise OpenSCAPError, err
33
35
  end
@@ -36,6 +38,8 @@ module OpenSCAP
36
38
  attach_function :oscap_cleanup, [], :void
37
39
  attach_function :oscap_get_version, [], :string
38
40
 
41
+ attach_function :oscap_document_type_to_string, [:int], :string
42
+
39
43
  attach_function :oscap_err, [], :bool
40
44
  attach_function :oscap_err_get_full_error, [], :pointer
41
45
  private_class_method :oscap_err, :oscap_err_get_full_error
@@ -11,18 +11,42 @@
11
11
 
12
12
  module OpenSCAP
13
13
  class Source
14
- def initialize(input_filename)
15
- raise OpenSCAPError, "No filename specified!" unless input_filename
16
- @s = OpenSCAP.oscap_source_new_from_file(input_filename)
17
- if @s.null?
18
- OpenSCAP.raise!
14
+ def initialize(param)
15
+ case param
16
+ when nil
17
+ raise OpenSCAPError, "No filename specified!"
18
+ when String
19
+ @s = OpenSCAP.oscap_source_new_from_file(param)
20
+ when Hash
21
+ @s = OpenSCAP.oscap_source_new_from_memory param[:content], param[:content].length, param[:path]
22
+ when FFI::Pointer
23
+ @s = param
24
+ else
25
+ raise OpenSCAP::OpenSCAPError, "Cannot initialize OpenSCAP::Source with '#{param}'"
19
26
  end
27
+ OpenSCAP.raise! if @s.null?
28
+ end
29
+
30
+ def type
31
+ OpenSCAP.oscap_document_type_to_string(OpenSCAP.oscap_source_get_scap_type(@s))
32
+ end
33
+
34
+ def validate!
35
+ e = FFI::MemoryPointer.new(:char, 4096)
36
+ if 0 != OpenSCAP.oscap_source_validate(@s, XmlReporterCallback, e)
37
+ OpenSCAP.raise! e.read_string
38
+ end
39
+
20
40
  end
21
41
 
22
42
  def raw
23
43
  @s
24
44
  end
25
45
 
46
+ def save(filepath=nil)
47
+ OpenSCAP.raise! unless OpenSCAP.oscap_source_save_as(raw, filepath) == 0
48
+ end
49
+
26
50
  def destroy
27
51
  OpenSCAP.oscap_source_free(@s)
28
52
  @s = nil
@@ -30,5 +54,21 @@ module OpenSCAP
30
54
  end
31
55
 
32
56
  attach_function :oscap_source_new_from_file, [:string], :pointer
57
+ attach_function :oscap_source_new_from_memory, [:string, :int, :string], :pointer
58
+ attach_function :oscap_source_get_scap_type, [:pointer], :int
33
59
  attach_function :oscap_source_free, [:pointer], :void
60
+ attach_function :oscap_source_save_as, [:pointer, :string], :int
61
+
62
+ callback :xml_reporter, [:string, :int, :string, :pointer], :int
63
+ attach_function :oscap_source_validate, [:pointer, :xml_reporter, :pointer], :int
64
+ XmlReporterCallback = Proc.new do |filename, line_number, error_message, e|
65
+ offset = e.get_string(0).length
66
+ msg = "#{filename}:#{line_number}: #{error_message}"
67
+ if msg.length + offset + 1 < e.size
68
+ e.put_string(offset, msg)
69
+ 0
70
+ else
71
+ 1
72
+ end
73
+ end
34
74
  end
@@ -0,0 +1,55 @@
1
+ #
2
+ # Copyright (c) 2014 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
+ module OpenSCAP
13
+ class Text
14
+ attr_reader :raw
15
+
16
+ def initialize
17
+ @raw = OpenSCAP.oscap_text_new
18
+ end
19
+
20
+ def text=(str)
21
+ OpenSCAP.raise! unless OpenSCAP.oscap_text_set_text(raw, str)
22
+ end
23
+
24
+ def text
25
+ OpenSCAP.oscap_text_get_text(raw)
26
+ end
27
+
28
+ def destroy
29
+ OpenSCAP.oscap_text_free(raw)
30
+ raw = nil
31
+ end
32
+ end
33
+
34
+ class TextList
35
+ def initialize(oscap_text_iterator)
36
+ @raw = oscap_text_iterator
37
+ end
38
+
39
+ def plaintext(lang=nil)
40
+ OpenSCAP.oscap_textlist_get_preferred_plaintext @raw, lang
41
+ end
42
+
43
+ def destroy
44
+ OpenSCAP.oscap_text_iterator_free @raw
45
+ end
46
+ end
47
+
48
+ attach_function :oscap_text_new, [], :pointer
49
+ attach_function :oscap_text_set_text, [:pointer, :string], :bool
50
+ attach_function :oscap_text_get_text, [:pointer], :string
51
+ attach_function :oscap_text_free, [:pointer], :void
52
+
53
+ attach_function :oscap_textlist_get_preferred_plaintext, [:pointer, :string], :string
54
+ attach_function :oscap_text_iterator_free, [:pointer], :void
55
+ end
@@ -10,5 +10,5 @@
10
10
  #
11
11
 
12
12
  module OpenSCAP
13
- VERSION = "0.3.0"
13
+ VERSION = "0.4.0"
14
14
  end
@@ -9,10 +9,10 @@
9
9
  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
10
10
  #
11
11
 
12
- module OpenSCAP::Helper
13
- def self.get_html_report_for_arf
14
- # This is workaround function for the installation with
15
- # old OpenSCAP without the oscap_source functionality
16
-
12
+ require 'openscap/openscap'
13
+
14
+ module OpenSCAP
15
+ module Xccdf
16
+ NUMERIC = :float
17
17
  end
18
- end
18
+ end
@@ -0,0 +1,62 @@
1
+ #
2
+ # Copyright (c) 2014 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 Benchmark
18
+ attr_reader :raw
19
+
20
+ def initialize(p)
21
+ case p
22
+ when OpenSCAP::Source
23
+ @raw = OpenSCAP.xccdf_benchmark_import_source p.raw
24
+ else
25
+ raise OpenSCAP::OpenSCAPError,
26
+ "Cannot initialize OpenSCAP::Xccdf::Benchmark with '#{p}'"
27
+ end
28
+ OpenSCAP.raise! if @raw.null?
29
+ end
30
+
31
+ def profiles
32
+ @profiles ||= profiles_init
33
+ end
34
+
35
+ def destroy
36
+ OpenSCAP.xccdf_benchmark_free @raw
37
+ @raw = nil
38
+ end
39
+
40
+ private
41
+ def profiles_init
42
+ profiles = {}
43
+ profit = OpenSCAP.xccdf_benchmark_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_benchmark_import_source, [:pointer], :pointer
56
+ attach_function :xccdf_benchmark_free, [:pointer], :void
57
+
58
+ attach_function :xccdf_benchmark_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,0 +1,43 @@
1
+ #
2
+ # Copyright (c) 2014 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/text'
13
+
14
+ module OpenSCAP
15
+ module Xccdf
16
+ class Profile
17
+ attr_reader :raw
18
+
19
+ def initialize(p)
20
+ case p
21
+ when FFI::Pointer
22
+ @raw = p
23
+ else
24
+ raise OpenSCAP::OpenSCAPError, "Cannot initialize OpenSCAP::Xccdf::Profile with #{p}"
25
+ end
26
+ end
27
+
28
+ def id
29
+ OpenSCAP.xccdf_profile_get_id raw
30
+ end
31
+
32
+ def title(prefered_lang=nil)
33
+ textlist = OpenSCAP::TextList.new(OpenSCAP.xccdf_profile_get_title(@raw))
34
+ title = textlist.plaintext(prefered_lang)
35
+ textlist.destroy
36
+ return title
37
+ end
38
+ end
39
+ end
40
+
41
+ attach_function :xccdf_profile_get_id, [:pointer], :string
42
+ attach_function :xccdf_profile_get_title, [:pointer], :pointer
43
+ end
@@ -0,0 +1,71 @@
1
+ #
2
+ # Copyright (c) 2014 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/exceptions'
13
+ require 'openscap/text'
14
+
15
+ module OpenSCAP
16
+ module Xccdf
17
+ class RuleResult
18
+ def initialize(t)
19
+ case t
20
+ when FFI::Pointer
21
+ @rr = t
22
+ else
23
+ raise OpenSCAP::OpenSCAPError, "Cannot initialize TestResult with #{t}"
24
+ end
25
+ end
26
+
27
+ def id
28
+ OpenSCAP.xccdf_rule_result_get_idref @rr
29
+ end
30
+
31
+ def result
32
+ OpenSCAP.xccdf_test_result_type_get_text \
33
+ OpenSCAP.xccdf_rule_result_get_result(@rr)
34
+ end
35
+
36
+ def override!(param)
37
+ if OpenSCAP::XccdfResult[param[:new_result]] > OpenSCAP::XccdfResult[:fixed]
38
+ raise OpenSCAPError, "Could not recognize result type: '#{param[:new_result]}'"
39
+ end
40
+ t = OpenSCAP::Text.new
41
+ t.text = param[:raw_text]
42
+ if !OpenSCAP.xccdf_rule_result_override(@rr,
43
+ OpenSCAP::XccdfResult[param[:new_result]],
44
+ param[:time], param[:authority], t.raw)
45
+ OpenSCAP.raise!
46
+ end
47
+ end
48
+
49
+ def destroy
50
+ OpenSCAP.xccdf_rule_result_free @rr
51
+ end
52
+ end
53
+ end
54
+
55
+ attach_function :xccdf_rule_result_get_idref, [:pointer], :string
56
+ attach_function :xccdf_rule_result_free, [:pointer], :void
57
+ attach_function :xccdf_rule_result_get_result, [:pointer], :int
58
+ attach_function :xccdf_test_result_type_get_text, [:int], :string
59
+
60
+ XccdfResult = enum(:pass, 1,
61
+ :fail,
62
+ :error,
63
+ :unknown,
64
+ :notapplicable,
65
+ :notchecked,
66
+ :notselected,
67
+ :informational,
68
+ :fixed)
69
+ attach_function :xccdf_rule_result_override,
70
+ [:pointer, XccdfResult, :string, :string, :pointer], :bool
71
+ end