diff --git a/lib/resources/limits_conf.rb b/lib/resources/limits_conf.rb new file mode 100644 index 000000000..df944da30 --- /dev/null +++ b/lib/resources/limits_conf.rb @@ -0,0 +1,44 @@ +# encoding: utf-8 +# copyright: 2015, Vulcano Security GmbH +# license: All rights reserved + +require 'utils/simpleconfig' + +class LimitsConf < Vulcano::Resource + + def initialize + @runner = Specinfra::Runner + @conf_path = '/etc/security/limits.conf' + @files_contents = {} + @content = nil + @params = nil + read_content + end + + def method_missing name + @params || read_content + @params[name.to_s] + end + + def read_content + # read the file + if !@runner.check_file_is_file(@conf_path) + return skip_resource "Can't find file \"#{@conf_path}\"" + end + @content = read_file(@conf_path) + if @content.empty? && @runner.get_file_size(@conf_path).stdout.strip.to_i > 0 + return skip_resource "Can't read file \"#{@conf_path}\"" + end + # parse the file + @params = SimpleConfig.new(@content, + assignment_re: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/, + key_vals: 3, + multiple_values: true + ).params + @content + end + + def read_file(path) + @files_contents[path] ||= @runner.get_file_content(path).stdout + end +end diff --git a/lib/resources/resources.rb b/lib/resources/resources.rb index 92e6915e9..2b58a69f0 100644 --- a/lib/resources/resources.rb +++ b/lib/resources/resources.rb @@ -8,6 +8,7 @@ require 'resources/env' require 'resources/etc_group' require 'resources/file' require 'resources/group_policy' +require 'resources/limits_conf' require 'resources/login_def' require 'resources/mysql_conf' require 'resources/mysql_session' @@ -46,6 +47,10 @@ module Serverspec GroupPolicy.new(policy_path) end + def limits_conf() + LimitsConf.new() + end + def login_def() LoginDef.new() end diff --git a/lib/utils/simpleconfig.rb b/lib/utils/simpleconfig.rb index f67e9826c..504b2363b 100644 --- a/lib/utils/simpleconfig.rb +++ b/lib/utils/simpleconfig.rb @@ -24,6 +24,23 @@ class SimpleConfig private + def parse_values (match, values) + start_idx = 2 + i = 0 + count = values - 1 + if (values == 1) then + return match[start_idx] + else + # iterate over expected parameters + values = Array.new + begin + values.push(match[start_idx+i]) + i +=1; + end until i > count + return values + end + end + def parse_rest( rest, opts ) idx_nl = rest.index("\n") idx_comment = rest.index('#') @@ -56,9 +73,9 @@ class SimpleConfig if !m.nil? if opts[:multiple_values] @params[m[1]] ||= [] - @params[m[1]].push(m[2]) + @params[m[1]].push(parse_values(m, opts[:key_vals])) else - @params[m[1]] = m[2] + @params[m[1]] = parse_values(m, opts[:key_vals]) end elsif !is_empty_line(line) if opts[:multiple_values] @@ -82,6 +99,7 @@ class SimpleConfig multiline: false, comment_char: '#', assignment_re: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/, + key_vals: 1, # default for key=value, may require for 'key val1 val2 val3' standalone_comments: false, multiple_values: true }