Class: OpenvasCli::VasTarget

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

Instance Attribute Summary collapse

Attributes inherited from VasBase

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VasBase

#destroy, #destroy!, get_by_id, #new_record?, #reset_changes, #save, #save!, #to_key, #to_param, #update_attributes

Methods included from ConnAddin

included

Methods included from XmlAddin

included

Constructor Details

#initialize(params = {}) ⇒ VasTarget

Returns a new instance of VasTarget.



56
57
58
59
60
# File 'lib/openvas-cli/vas_target.rb', line 56

def initialize(params = {})
  super(params)    

  @org_hosts = @hosts.collect { |h| h } if @hosts
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



9
10
11
# File 'lib/openvas-cli/vas_target.rb', line 9

def comment
  @comment
end

#in_useObject

Returns the value of attribute in_use.



11
12
13
# File 'lib/openvas-cli/vas_target.rb', line 11

def in_use
  @in_use
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/openvas-cli/vas_target.rb', line 8

def name
  @name
end

#port_rangeObject

Returns the value of attribute port_range.



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

def port_range
  @port_range
end

Class Method Details

.create!(params = {}) ⇒ Object



161
162
163
164
165
166
# File 'lib/openvas-cli/vas_target.rb', line 161

def self.create!(params={})
  t = VasTarget.new(params)
  t.save!
  
  t
end

.get_all(options = {}) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/openvas-cli/vas_target.rb', line 168

def self.get_all(options = {})
  params = {:tasks => 1}
  params[:target_id] = options[:id] if options[:id]
  
  req = Nokogiri::XML::Builder.new { |xml|
    xml.get_targets(params)
  }
  ret = []
  
  begin    
    targets = connection.send_receive(req.doc)
    targets.xpath('//target').each { |t|
      targ                       = VasTarget.new
      targ.id                    = extract_value_from("@id", t)
      targ.name                  = extract_value_from("name", t)
      host_string                = extract_value_from("hosts", t)
      all_hosts = host_string.split(/,/)
      all_hosts.each { |hst| hst.strip! }
      targ.hosts                 = all_hosts
      targ.comment               = extract_value_from("comment", t)
      targ.port_range            = extract_value_from("port_range", t)
      targ.in_use                = extract_value_from("in_use", t).to_i > 0
      targ.credential_keys[:ssh] = extract_value_from("ssh_lsc_credential/@id", t)
      targ.credential_keys[:smb] = extract_value_from("smb_lsc_credential/@id", t)
      
      
      t.xpath('tasks/task').each { |task|
        targ.task_keys << extract_value_from("@id", task)
      }
      
      targ.reset_changes
      
      ret << targ
    }
  
  rescue VasExceptions::CommandException => e
    raise e unless e.message =~ /Failed to find target/i
  end
  
  ret
end

.get_local_subnetsObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/openvas-cli/vas_target.rb', line 45

def self.get_local_subnets
  adrs = []
  `ifconfig`.split("\n").each { |cmd|
    if cmd =~ /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+.*Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/i
      adrs << IPAddress("#{$1}/#{$2}") unless $1 == '127.0.0.1'
    end
  }
  
  adrs.collect{ |a| a.network.to_string }    
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/openvas-cli/vas_target.rb', line 30

def changed?
  local_changes = false
  
  local_changes = true unless @org_hosts && @org_hosts.eql?(@hosts)  
  
  unless local_changes == true
    local_changes = credential_changed?(:ssh) || credential_changed?(:smb)
  end
        
  local_changes ||  super
end

#create_or_updateObject



113
114
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
# File 'lib/openvas-cli/vas_target.rb', line 113

def create_or_update
  if @id
    return unless destroy
  end
  req = Nokogiri::XML::Builder.new { |xml|
    xml.create_target {
      xml.name       { xml.text(@name) }
      xml.comment    { xml.text(@comment) } if @comment
      xml.hosts      { xml.text(hosts_string) }
      xml.ssh_lsc_credential(:id => credentials[:ssh].id) if credentials[:ssh]
      xml.smb_lsc_credential(:id => credentials[:smb].id) if credentials[:smb]
      xml.port_range { xml.text(@port_range) } if @port_range
    }
  }
  
  begin
    resp = VasTarget.connection.send_receive(req.doc)
    @id = VasTarget.extract_value_from("create_target_response/@id", resp)
    reset_changes
    
    true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end      
  
end

#credential_keysObject



66
67
68
# File 'lib/openvas-cli/vas_target.rb', line 66

def credential_keys
  @credential_keys ||= { :smb => nil, :ssh => nil }
end

#credentialsObject



62
63
64
# File 'lib/openvas-cli/vas_target.rb', line 62

def credentials
  @credentials ||= get_credentials
end

#delete_recordObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/openvas-cli/vas_target.rb', line 142

def delete_record
  return unless @id
  
  req = Nokogiri::XML::Builder.new { |xml|
    xml.delete_target(:target_id => @id)
  }
  
  begin
    VasTarget.connection.send_receive(req.doc)
    @id = nil
    
    true
  rescue VasExceptions::CommandException => e
    errors[:command_failure] << e.message
    
    nil
  end  
end

#hostsObject



94
95
96
# File 'lib/openvas-cli/vas_target.rb', line 94

def hosts
  @hosts ||= []
end

#hosts=(val) ⇒ Object



98
99
100
101
102
103
# File 'lib/openvas-cli/vas_target.rb', line 98

def hosts=(val)
  hosts_will_change! unless @hosts.eql?(val)
  @hosts = val
  
  @org_hosts = val.collect { |h| h } if val
end

#hosts_stringObject



105
106
107
# File 'lib/openvas-cli/vas_target.rb', line 105

def hosts_string
  hosts.join(", ")
end

#hosts_string=(val) ⇒ Object



109
110
111
# File 'lib/openvas-cli/vas_target.rb', line 109

def hosts_string=(val)
  self.hosts = val.split(/, ?/)
end

#smb_credential_idObject



70
71
72
# File 'lib/openvas-cli/vas_target.rb', line 70

def smb_credential_id
  credentials[:smb].id if credentials[:smb]
end

#smb_credential_id=(val) ⇒ Object



74
75
76
# File 'lib/openvas-cli/vas_target.rb', line 74

def smb_credential_id=(val)
  credentials[:smb] = VasLscCredential.get_by_id(val)
end

#ssh_credential_idObject



78
79
80
# File 'lib/openvas-cli/vas_target.rb', line 78

def ssh_credential_id
  credentials[:ssh].id if credentials[:ssh]
end

#ssh_credential_id=(val) ⇒ Object



82
83
84
# File 'lib/openvas-cli/vas_target.rb', line 82

def ssh_credential_id=(val)
  credentials[:ssh] = VasLscCredential.get_by_id(val)
end

#task_keysObject



90
91
92
# File 'lib/openvas-cli/vas_target.rb', line 90

def task_keys
  @task_keys ||= []
end

#tasksObject



86
87
88
# File 'lib/openvas-cli/vas_target.rb', line 86

def tasks
  @tasks ||= get_tasks
end