2015-04-09 20:01:23 +00:00
|
|
|
# encoding: utf-8
|
2015-07-15 13:15:18 +00:00
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
2015-04-09 20:01:23 +00:00
|
|
|
# license: All rights reserved
|
|
|
|
|
|
|
|
require 'utils/simpleconfig'
|
|
|
|
|
2015-08-28 17:10:03 +00:00
|
|
|
class SshConf < Vulcano.resource(1)
|
|
|
|
name 'ssh_config'
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def initialize(conf_path = nil, type = nil)
|
2015-08-30 00:18:37 +00:00
|
|
|
@conf_path = conf_path || '/etc/ssh/ssh_config'
|
2015-09-05 14:07:54 +00:00
|
|
|
typename = (@conf_path.include?('sshd') ? 'Server' : 'Client')
|
2015-05-14 16:30:38 +00:00
|
|
|
@type = type || "SSH #{typename} configuration #{conf_path}"
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@type
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
2015-09-17 15:06:39 +00:00
|
|
|
read_content
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def params(*opts)
|
2015-09-17 15:06:39 +00:00
|
|
|
opts.inject(read_params) do |res, nxt|
|
|
|
|
res.respond_to?(:key) ? res[nxt] : nil
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def method_missing(name)
|
2015-09-17 15:06:39 +00:00
|
|
|
param = read_params[name.to_s]
|
2015-09-17 18:35:14 +00:00
|
|
|
return nil if param.nil?
|
2015-09-05 15:06:28 +00:00
|
|
|
# extract first value if we have only one value in array
|
2015-09-17 15:06:39 +00:00
|
|
|
return param[0] if param.length == 1
|
2015-09-05 15:06:28 +00:00
|
|
|
param
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
private
|
|
|
|
|
2015-04-09 20:01:23 +00:00
|
|
|
def read_content
|
2015-10-01 13:25:08 +00:00
|
|
|
return @content if defined?(@content)
|
2015-09-17 15:06:39 +00:00
|
|
|
file = vulcano.file(@conf_path)
|
|
|
|
if !file.file?
|
2015-06-21 14:33:08 +00:00
|
|
|
return skip_resource "Can't find file \"#{@conf_path}\""
|
|
|
|
end
|
2015-08-28 03:02:38 +00:00
|
|
|
|
2015-09-17 15:06:39 +00:00
|
|
|
@content = file.content
|
|
|
|
if @content.empty? && file.size > 0
|
2015-06-21 14:33:08 +00:00
|
|
|
return skip_resource "Can't read file \"#{@conf_path}\""
|
|
|
|
end
|
2015-08-28 03:02:38 +00:00
|
|
|
|
2015-09-17 15:06:39 +00:00
|
|
|
@content
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_params
|
2015-10-01 13:25:08 +00:00
|
|
|
return @params if defined?(@params)
|
2015-09-17 15:06:39 +00:00
|
|
|
return @params = {} if read_content.nil?
|
|
|
|
conf = SimpleConfig.new(
|
|
|
|
read_content,
|
2015-04-17 13:36:55 +00:00
|
|
|
assignment_re: /^\s*(\S+?)\s+(.*?)\s*$/,
|
2015-09-09 16:37:16 +00:00
|
|
|
multiple_values: true,
|
2015-09-17 15:06:39 +00:00
|
|
|
)
|
|
|
|
@params = conf.params
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-28 17:10:03 +00:00
|
|
|
class SshdConf < SshConf
|
|
|
|
name 'sshd_config'
|
|
|
|
|
|
|
|
def initialize(path = nil)
|
2015-08-28 03:02:38 +00:00
|
|
|
super(path || '/etc/ssh/sshd_config')
|
2015-08-01 07:43:38 +00:00
|
|
|
end
|
|
|
|
end
|