2015-07-15 15:15:18 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-10-06 18:55:44 +02:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-07-15 15:15:18 +02:00
|
|
|
# license: All rights reserved
|
2015-07-15 15:15:53 +02:00
|
|
|
|
|
|
|
# The file format consists of
|
|
|
|
# - username
|
|
|
|
# - password
|
|
|
|
# - userid
|
|
|
|
# - groupid
|
|
|
|
# - user id info
|
|
|
|
# - home directory
|
|
|
|
# - command
|
|
|
|
|
2015-09-05 19:05:18 +02:00
|
|
|
# usage:
|
|
|
|
#
|
|
|
|
# describe passwd do
|
|
|
|
# its(:usernames) { should eq 'root' }
|
|
|
|
# its(:uids) { should eq 1 }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# describe passwd.uid(0) do
|
|
|
|
# its(:username) { should eq 'root' }
|
|
|
|
# its(:count) { should eq 1 }
|
|
|
|
# end
|
|
|
|
|
2015-10-04 17:59:13 +02:00
|
|
|
require 'utils/parser'
|
|
|
|
|
2015-10-26 04:04:18 +01:00
|
|
|
class Passwd < Inspec.resource(1)
|
2015-08-28 12:27:35 -07:00
|
|
|
name 'passwd'
|
2015-07-15 00:47:17 +02:00
|
|
|
|
2015-10-04 17:59:13 +02:00
|
|
|
include ContentParser
|
|
|
|
|
2015-09-05 19:05:18 +02:00
|
|
|
attr_reader :uid
|
|
|
|
attr_reader :parsed
|
2015-07-15 00:47:17 +02:00
|
|
|
|
2015-09-05 19:05:18 +02:00
|
|
|
def initialize(path = nil)
|
2015-08-28 12:27:35 -07:00
|
|
|
@path = path || '/etc/passwd'
|
2015-10-26 04:04:18 +01:00
|
|
|
@content = inspec.file(@path).content
|
2015-10-04 17:49:00 +02:00
|
|
|
@parsed = parse_passwd(@content)
|
2015-08-28 12:27:35 -07:00
|
|
|
end
|
|
|
|
|
2015-09-05 19:05:18 +02:00
|
|
|
# call passwd().uid(0)
|
|
|
|
# returns a seperate object with reference to this object
|
|
|
|
def uid(uid)
|
|
|
|
PasswdUid.new(self, uid)
|
2015-07-26 12:30:12 +02:00
|
|
|
end
|
2015-07-15 15:15:53 +02:00
|
|
|
|
2015-07-26 12:30:12 +02:00
|
|
|
def usernames
|
2015-10-04 17:49:00 +02:00
|
|
|
map_data('name')
|
2015-07-26 12:30:12 +02:00
|
|
|
end
|
2015-07-15 15:15:53 +02:00
|
|
|
|
2015-07-26 12:30:12 +02:00
|
|
|
def passwords
|
2015-10-04 17:49:00 +02:00
|
|
|
map_data('password')
|
2015-07-26 12:30:12 +02:00
|
|
|
end
|
2015-07-15 15:15:53 +02:00
|
|
|
|
2015-07-26 12:30:12 +02:00
|
|
|
def uids
|
2015-10-04 17:49:00 +02:00
|
|
|
map_data('uid')
|
2015-07-26 12:30:12 +02:00
|
|
|
end
|
2015-07-15 00:47:17 +02:00
|
|
|
|
2015-07-26 12:30:12 +02:00
|
|
|
def gids
|
2015-10-04 17:49:00 +02:00
|
|
|
map_data('gid')
|
2015-07-26 12:30:12 +02:00
|
|
|
end
|
2015-07-15 00:47:17 +02:00
|
|
|
|
2015-07-26 12:30:12 +02:00
|
|
|
def users
|
2015-08-28 12:27:35 -07:00
|
|
|
@parsed.map {|x|
|
2015-10-04 17:49:00 +02:00
|
|
|
x['name']
|
2015-07-26 12:30:12 +02:00
|
|
|
}
|
|
|
|
end
|
2015-07-15 00:47:17 +02:00
|
|
|
|
2015-10-12 13:01:58 +02:00
|
|
|
def to_s
|
|
|
|
'/etc/passwd'
|
|
|
|
end
|
|
|
|
|
2015-08-28 12:27:35 -07:00
|
|
|
private
|
|
|
|
|
2015-10-04 17:49:00 +02:00
|
|
|
def map_data(id)
|
|
|
|
@parsed.map {|x|
|
|
|
|
x[id]
|
|
|
|
}
|
|
|
|
end
|
2015-07-26 12:30:12 +02:00
|
|
|
end
|
2015-09-05 19:05:18 +02:00
|
|
|
|
|
|
|
# object that hold a specifc uid view on passwd
|
|
|
|
class PasswdUid
|
|
|
|
def initialize(passwd, uid)
|
|
|
|
@passwd = passwd
|
2015-10-04 17:49:00 +02:00
|
|
|
@users = @passwd.parsed.select { |x| x['uid'] == "#{uid}" }
|
2015-09-05 19:05:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2015-10-04 17:49:00 +02:00
|
|
|
@users.at(0)['name']
|
2015-09-05 19:05:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def count
|
2015-10-04 17:49:00 +02:00
|
|
|
@users.size
|
2015-09-05 19:05:18 +02:00
|
|
|
end
|
|
|
|
end
|