2015-07-15 13:15:18 +00:00
# encoding: utf-8
# copyright: 2015, Vulcano Security GmbH
2015-07-15 13:15:53 +00:00
# The file format consists of
# - username
# - password
# - userid
# - groupid
# - user id info
# - home directory
# - command
2015-10-04 15:59:13 +00:00
require 'utils/parser'
2016-04-26 12:27:21 +00:00
require 'utils/filter'
2018-03-22 12:25:45 +00:00
require 'utils/file_reader'
2015-10-04 15:59:13 +00:00
2016-03-08 18:06:55 +00:00
module Inspec::Resources
2016-04-26 12:27:21 +00:00
class Passwd < Inspec . resource ( 1 )
2016-03-08 18:06:55 +00:00
name 'passwd'
2018-02-19 14:26:49 +00:00
supports platform : 'unix'
2016-03-08 18:06:55 +00:00
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
2016-02-17 11:35:46 +00:00
2016-03-08 18:06:55 +00:00
describe passwd . uids ( 0 ) do
its ( 'users' ) { should cmp 'root' }
end
2016-02-17 11:35:46 +00:00
2016-03-08 18:06:55 +00:00
describe passwd . shells ( / nologin / ) do
# find all users with a nologin shell
its ( 'users' ) { should_not include 'my_login_user' }
end
"
include PasswdParser
2018-03-22 12:25:45 +00:00
include FileReader
2016-03-08 18:06:55 +00:00
attr_reader :params
attr_reader :content
attr_reader :lines
def initialize ( path = nil , opts = nil )
opts || = { }
@path = path || '/etc/passwd'
2018-03-22 12:25:45 +00:00
@content = opts [ :content ] || read_file_content ( @path , allow_empty : true )
2016-03-08 18:06:55 +00:00
@lines = @content . to_s . split ( " \n " )
@params = parse_passwd ( @content )
2016-02-17 11:35:46 +00:00
end
2015-08-28 19:27:35 +00:00
2016-04-26 12:27:21 +00:00
filter = FilterTable . create
2018-06-26 19:14:21 +00:00
filter . register_column ( :users , field : 'user' )
. register_column ( :passwords , field : 'password' )
. register_column ( :uids , field : 'uid' )
. register_column ( :gids , field : 'gid' )
. register_column ( :descs , field : 'desc' )
. register_column ( :homes , field : 'home' )
. register_column ( :shells , field : 'shell' )
2015-07-15 13:15:53 +00:00
2016-04-29 17:10:15 +00:00
# rebuild the passwd line from raw content
2018-06-26 19:14:21 +00:00
filter . register_custom_property ( :content ) { | t , _ |
2016-04-29 17:10:15 +00:00
t . entries . map do | e |
[ e . user , e . password , e . uid , e . gid , e . desc , e . home , e . shell ] . join ( ':' )
end . join ( " \n " )
}
2016-02-17 11:35:46 +00:00
2018-06-26 19:14:21 +00:00
filter . install_filter_methods_on_resource ( self , :params )
2015-10-12 11:01:58 +00:00
2016-04-26 12:27:21 +00:00
def to_s
'/etc/passwd'
2016-03-30 23:51:43 +00:00
end
2015-10-04 15:49:00 +00:00
end
2015-07-26 10:30:12 +00:00
end