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-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
|
|
|
read_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@type
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
2015-08-29 09:10:36 +00:00
|
|
|
@conf.content
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def params(*opts)
|
2015-04-09 20:01:23 +00:00
|
|
|
res = @params
|
|
|
|
opts.each do |opt|
|
|
|
|
res = res[opt] unless res.nil?
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2015-09-03 18:43:58 +00:00
|
|
|
def method_missing(name)
|
2015-09-08 23:07:08 +00:00
|
|
|
return nil if @params.nil?
|
2015-09-05 15:06:28 +00:00
|
|
|
param = @params[name.to_s]
|
|
|
|
# extract first value if we have only one value in array
|
|
|
|
param = param[0] if !param.nil? && param.length == 1
|
|
|
|
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-08-30 02:33:15 +00:00
|
|
|
@conf = vulcano.file(@conf_path)
|
2015-06-21 14:33:08 +00:00
|
|
|
# read the file
|
2015-08-30 00:13:53 +00:00
|
|
|
if !@conf.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-08-29 09:10:36 +00:00
|
|
|
if @conf.content.empty? && @conf.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-06-21 14:33:08 +00:00
|
|
|
# parse the file
|
2015-09-09 16:37:16 +00:00
|
|
|
@params = SimpleConfig.new(
|
|
|
|
@conf.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-04-17 13:36:55 +00:00
|
|
|
).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
|