inspec/lib/resources/passwd.rb
eramoto c7e87ca3e3 Unify method in which file content is read across all resources (#2359)
* Create file-check functionality into utility file

There are the similar issues as PR #2302. Almost resources return false
positives when a file does not exist or is not read.

* Replace to file-check functionality
* Fix dh_params and x509_certificate resources

If a file is empty, OpenSSL::PKey::DH and OpenSSL::X509::Certificate have
raised an exception and have skipped the inspection. Thus x509_certificate
and dh_params resources are not allowed to read a empty file.

* to_s of shadow expects filters is not nil
* Remove workaround of sshd_config

Removes the workaround of sshd_config since Travis CI fails due to a bug
of dev-sec/ssh-baseline and the PR #100 will fix it.

* Use init block variable in methods

Signed-off-by: ERAMOTO Masaya <eramoto.masaya@jp.fujitsu.com>
2018-03-22 08:25:45 -04:00

76 lines
2 KiB
Ruby

# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
# The file format consists of
# - username
# - password
# - userid
# - groupid
# - user id info
# - home directory
# - command
require 'utils/parser'
require 'utils/filter'
require 'utils/file_reader'
module Inspec::Resources
class Passwd < Inspec.resource(1)
name 'passwd'
supports platform: 'unix'
desc 'Use the passwd InSpec audit resource to test the contents of /etc/passwd, which contains the following information for users that may log into the system and/or as users that own running processes.'
example "
describe passwd do
its('users') { should_not include 'forbidden_user' }
end
describe passwd.uids(0) do
its('users') { should cmp 'root' }
end
describe passwd.shells(/nologin/) do
# find all users with a nologin shell
its('users') { should_not include 'my_login_user' }
end
"
include PasswdParser
include FileReader
attr_reader :params
attr_reader :content
attr_reader :lines
def initialize(path = nil, opts = nil)
opts ||= {}
@path = path || '/etc/passwd'
@content = opts[:content] || read_file_content(@path, allow_empty: true)
@lines = @content.to_s.split("\n")
@params = parse_passwd(@content)
end
filter = FilterTable.create
filter.add_accessor(:where)
.add_accessor(:entries)
.add(:users, field: 'user')
.add(:passwords, field: 'password')
.add(:uids, field: 'uid')
.add(:gids, field: 'gid')
.add(:descs, field: 'desc')
.add(:homes, field: 'home')
.add(:shells, field: 'shell')
# rebuild the passwd line from raw content
filter.add(:content) { |t, _|
t.entries.map do |e|
[e.user, e.password, e.uid, e.gid, e.desc, e.home, e.shell].join(':')
end.join("\n")
}
filter.connect(self, :params)
def to_s
'/etc/passwd'
end
end
end