Class: OpenvasCli::VasPreference

Inherits:
VasBase
  • Object
show all
Defined in:
lib/openvas-cli/vas_preference.rb

Overview

A name-value pair for OpenVAS configuration preferences. If inspecting a specific configuration set, config_id will be populated. If config_id is non-existant, then the preference is a system-wide default.

Instance Attribute Summary collapse

Attributes inherited from VasBase

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VasBase

#delete_record, #destroy, #destroy!, #initialize, #new_record?, #reset_changes, #save, #save!, #to_key, #to_param, #update_attributes

Methods included from ConnAddin

included

Methods included from XmlAddin

included

Constructor Details

This class inherits a constructor from OpenvasCli::VasBase

Instance Attribute Details

#config_idObject

Configuration Identifier. If nil, the preference is a system-wide default.



16
17
18
# File 'lib/openvas-cli/vas_preference.rb', line 16

def config_id
  @config_id
end

#nvt_idObject

NVT id



19
20
21
# File 'lib/openvas-cli/vas_preference.rb', line 19

def nvt_id
  @nvt_id
end

#nvt_nameObject

Returns the value of attribute nvt_name.



21
22
23
# File 'lib/openvas-cli/vas_preference.rb', line 21

def nvt_name
  @nvt_name
end

#val_typeObject

Type of value expected :boolean, :choice, :text



10
11
12
# File 'lib/openvas-cli/vas_preference.rb', line 10

def val_type
  @val_type
end

#val_type_descObject

Returns the value of attribute val_type_desc.



12
13
14
# File 'lib/openvas-cli/vas_preference.rb', line 12

def val_type_desc
  @val_type_desc
end

Class Method Details

.from_xml_node(node, config_id = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/openvas-cli/vas_preference.rb', line 156

def self.from_xml_node(node, config_id=nil)
  pref               = VasPreference.new
  pref.name          = extract_value_from("name", node)
  pref.value         = extract_value_from("value", node)
  pref.nvt_id        = extract_value_from("nvt/@oid", node)
  pref.nvt_name      = extract_value_from("nvt/name", node)
  pref.val_type_desc = extract_value_from("type", node)
  pref.config_id     = config_id
  case pref.val_type_desc
    when "checkbox"
      pref.val_type = :boolean
      pref.value = pref.value == "yes" ? true : false
    when "radio"
      pref.val_type = :choice
      pref.val_choices << pref.value
      node.xpath("alt").each { |alt|
        pref.val_choices << alt.text
      }
    else
      pref.val_type = :text
  end
  pref
end

.get_all(options = {}) ⇒ Object

Pulls Vas preferences.

Options:

:config_id

> [configuration_id]] pulls preferences associated with the provided configuration id

:nvt_oid => [oid]

pulls preferences associated associated with the provided VasNVT.oid

:name => [name]

pulls the preference with the specified name

:sort_by => [field_name]

filters the results by the provided field name. Valid symbols are, :name, :nvt_name, :nvt_id.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openvas-cli/vas_preference.rb', line 115

def self.get_all(options={})
  manual_filter = false
  params = {}
  params[:config_id]  = options[:config_id] if options[:config_id]
  params[:nvt_oid]    = options[:nvt_oid]   if options[:nvt_oid]
  if options[:name]
    if options[:nvt_oid]
      params[:preference] = options[:name]
    else
      manual_filter = true 
    end
  end
  
  req = Nokogiri::XML::Builder.new { |xml|
    if params.empty?
      xml.get_preferences
    else
      xml.get_preferences(params)
    end
  }
  ret = []
  begin
    prefs = connection.send_receive(req.doc)
    
    prefs.xpath("/get_preferences_response/preference").each { |p|
      pref = from_xml_node(p, options[:config_id])
      if manual_filter
        ret << pref if pref.name == options[:name] && pref.nvt_id.empty?
      else
        ret << pref
      end
    }
    
    ret.sort!{ |a,b| a.name      <=> b.name }      if options[:sort_by] == :name
    ret.sort!{ |a,b| a.nvt_id    <=> b.nvt_id }    if options[:sort_by] == :nvt_id
    ret.sort!{ |a,b| a.nvt_name  <=> b.nvt_name }  if options[:sort_by] == :nvt_name
  rescue VasExceptions::CommandException => e
  end
  ret    
end

.get_by_id(id) ⇒ Object



97
98
99
# File 'lib/openvas-cli/vas_preference.rb', line 97

def self.get_by_id(id)
  nil
end

.get_by_name(config_id, name, nvt_id = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/openvas-cli/vas_preference.rb', line 101

def self.get_by_name(config_id, name, nvt_id=nil)
  if nvt_id
    get_all(:config_id => config_id, :name => name, :nvt_oid => nvt_id).first
  else
    get_all(:config_id => config_id, :name => name).first
  end
end

Instance Method Details

#configObject



45
46
47
# File 'lib/openvas-cli/vas_preference.rb', line 45

def config
  @config ||= VasConfig.get_by_id(@config_id)
end

#create_or_updateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/openvas-cli/vas_preference.rb', line 65

def create_or_update
  unless @config_id
    errors[:config_id] << "Config_id required to save"
    return
  end
  req = Nokogiri::XML::Builder.new { |xml|
    xml.modify_config(:config_id => @config_id) {
      xml.preference {
        xml.nvt(:oid=>@nvt_id) if @nvt_id && !@nvt_id.empty?
        xml.name  { xml.text(full_name) }
        xml.value { 
          if @val_type == :boolean
            xml.text(Base64.encode64(@value ? "yes" : "no"))
          else
            xml.text(Base64.encode64(@value))
          end 
        }
      }
    } 
  }

  begin
    VasPreference.connection.send_receive(req.doc)
    
      true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end
end

#full_nameObject



57
58
59
60
61
62
63
# File 'lib/openvas-cli/vas_preference.rb', line 57

def full_name
  if @nvt_id && !@nvt_id.empty?
    "#{nvt_name}[#{@val_type_desc}]:#{@name}"
  else
    @name
  end
end

#nameObject



27
28
29
# File 'lib/openvas-cli/vas_preference.rb', line 27

def name
  @name
end

#name=(val) ⇒ Object



31
32
33
34
# File 'lib/openvas-cli/vas_preference.rb', line 31

def name=(val)
  name_will_change! unless val == @name
  @name = val
end

#nvtObject



49
50
51
# File 'lib/openvas-cli/vas_preference.rb', line 49

def nvt
  @nvt ||= VasNVT.get_by_id(@nvt_id)
end

#val_choicesObject



53
54
55
# File 'lib/openvas-cli/vas_preference.rb', line 53

def val_choices
  @val_choices ||= []
end

#valueObject



36
37
38
# File 'lib/openvas-cli/vas_preference.rb', line 36

def value
  @value
end

#value=(val) ⇒ Object



40
41
42
43
# File 'lib/openvas-cli/vas_preference.rb', line 40

def value=(val)
  value_will_change! unless val == @value
  @value = val
end