2015-04-09 22:01:23 +02:00
# encoding: utf-8
2015-07-15 15:15:18 +02:00
# copyright: 2015, Vulcano Security GmbH
2015-10-06 18:55:44 +02:00
# author: Dominik Richter
# author: Christoph Hartmann
2015-04-09 22:01:23 +02:00
# license: All rights reserved
require 'utils/simpleconfig'
2015-10-26 04:04:18 +01:00
class SshConf < Inspec . resource ( 1 )
2015-08-28 10:10:03 -07:00
name 'ssh_config'
2015-11-27 14:02:38 +01:00
desc 'Use the sshd_config InSpec audit resource to test configuration data for the Open SSH daemon located at /etc/ssh/sshd_config on Linux and UNIX platforms. sshd---the Open SSH daemon---listens on dedicated ports, starts a daemon for each incoming connection, and then handles encryption, authentication, key exchanges, command executation, and data exchanges.'
example "
describe sshd_config do
its ( 'Protocol' ) { should eq '2' }
end
"
2015-04-09 22:01:23 +02:00
2015-09-03 20:43:58 +02:00
def initialize ( conf_path = nil , type = nil )
2015-08-29 17:18:37 -07:00
@conf_path = conf_path || '/etc/ssh/ssh_config'
2015-09-05 16:07:54 +02:00
typename = ( @conf_path . include? ( 'sshd' ) ? 'Server' : 'Client' )
2015-05-14 18:30:38 +02:00
@type = type || " SSH #{ typename } configuration #{ conf_path } "
2015-04-09 22:01:23 +02:00
end
def content
2015-09-17 17:06:39 +02:00
read_content
2015-04-09 22:01:23 +02:00
end
2015-09-03 20:43:58 +02:00
def params ( * opts )
2015-09-17 17:06:39 +02:00
opts . inject ( read_params ) do | res , nxt |
res . respond_to? ( :key ) ? res [ nxt ] : nil
2015-04-09 22:01:23 +02:00
end
end
2015-09-03 20:43:58 +02:00
def method_missing ( name )
2015-09-17 17:06:39 +02:00
param = read_params [ name . to_s ]
2015-09-17 20:35:14 +02:00
return nil if param . nil?
2015-09-05 17:06:28 +02:00
# extract first value if we have only one value in array
2015-09-17 17:06:39 +02:00
return param [ 0 ] if param . length == 1
2015-09-05 17:06:28 +02:00
param
2015-04-09 22:01:23 +02:00
end
2015-10-12 13:01:58 +02:00
def to_s
'SSH Configuration'
end
2015-09-02 00:50:52 +02:00
private
2015-04-09 22:01:23 +02:00
def read_content
2015-10-01 15:25:08 +02:00
return @content if defined? ( @content )
2015-10-26 04:04:18 +01:00
file = inspec . file ( @conf_path )
2015-09-17 17:06:39 +02:00
if ! file . file?
2015-06-21 16:33:08 +02:00
return skip_resource " Can't find file \" #{ @conf_path } \" "
end
2015-08-27 20:02:38 -07:00
2015-09-17 17:06:39 +02:00
@content = file . content
if @content . empty? && file . size > 0
2015-06-21 16:33:08 +02:00
return skip_resource " Can't read file \" #{ @conf_path } \" "
end
2015-08-27 20:02:38 -07:00
2015-09-17 17:06:39 +02:00
@content
end
def read_params
2015-10-01 15:25:08 +02:00
return @params if defined? ( @params )
2015-09-17 17:06:39 +02:00
return @params = { } if read_content . nil?
conf = SimpleConfig . new (
read_content ,
2015-04-17 15:36:55 +02:00
assignment_re : / ^ \ s*( \ S+?) \ s+(.*?) \ s*$ / ,
2015-09-09 18:37:16 +02:00
multiple_values : true ,
2015-09-17 17:06:39 +02:00
)
@params = conf . params
2015-04-09 22:01:23 +02:00
end
end
2015-08-28 10:10:03 -07:00
class SshdConf < SshConf
name 'sshd_config'
def initialize ( path = nil )
2015-08-27 20:02:38 -07:00
super ( path || '/etc/ssh/sshd_config' )
2015-08-01 09:43:38 +02:00
end
end