Class: Passwd
Instance Attribute Summary collapse
Instance Method Summary
collapse
#parse_passwd, #parse_passwd_line
Constructor Details
#initialize(path = nil, opts = nil) ⇒ Passwd
Returns a new instance of Passwd.
44
45
46
47
48
49
50
51
|
# File 'lib/resources/passwd.rb', line 44
def initialize(path = nil, opts = nil)
opts ||= {}
@path = path || '/etc/passwd'
@content = opts[:content] || inspec.file(@path).content
@lines = @content.to_s.split("\n")
@filters = opts[:filters] || ''
@params = parse_passwd(@content)
end
|
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
41
42
43
|
# File 'lib/resources/passwd.rb', line 41
def content
@content
end
|
#lines ⇒ Object
Returns the value of attribute lines.
42
43
44
|
# File 'lib/resources/passwd.rb', line 42
def lines
@lines
end
|
#params ⇒ Object
Returns the value of attribute params.
40
41
42
|
# File 'lib/resources/passwd.rb', line 40
def params
@params
end
|
#uid(x) ⇒ Object
Returns the value of attribute uid.
39
40
41
|
# File 'lib/resources/passwd.rb', line 39
def uid
@uid
end
|
Instance Method Details
#count ⇒ Object
117
118
119
|
# File 'lib/resources/passwd.rb', line 117
def count
@params.length
end
|
#filter(hm = {}) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/resources/passwd.rb', line 53
def filter(hm = {})
return self if hm.nil? || hm.empty?
res = @params
filters = ''
hm.each do |attr, condition|
condition = condition.to_s if condition.is_a? Integer
filters += " #{attr} = #{condition.inspect}"
res = res.find_all do |line|
case line[attr.to_s]
when condition
true
else
false
end
end
end
content = res.map { |x| x.values.join(':') }.join("\n")
Passwd.new(@path, content: content, filters: @filters + filters)
end
|
#gids(gid = nil) ⇒ Object
100
101
102
|
# File 'lib/resources/passwd.rb', line 100
def gids(gid = nil)
gid.nil? ? map_data('gid') : filter(gid: gid)
end
|
#homes(home = nil) ⇒ Object
104
105
106
|
# File 'lib/resources/passwd.rb', line 104
def homes(home = nil)
home.nil? ? map_data('home') : filter(home: home)
end
|
#passwords(password = nil) ⇒ Object
92
93
94
|
# File 'lib/resources/passwd.rb', line 92
def passwords(password = nil)
password.nil? ? map_data('password') : filter(password: password)
end
|
#shells(shell = nil) ⇒ Object
108
109
110
|
# File 'lib/resources/passwd.rb', line 108
def shells(shell = nil)
shell.nil? ? map_data('shell') : filter(shell: shell)
end
|
#to_s ⇒ Object
112
113
114
115
|
# File 'lib/resources/passwd.rb', line 112
def to_s
f = @filters.empty? ? '' : ' with'+@filters
"/etc/passwd#{f}"
end
|
#uids(uid = nil) ⇒ Object
96
97
98
|
# File 'lib/resources/passwd.rb', line 96
def uids(uid = nil)
uid.nil? ? map_data('uid') : filter(uid: uid)
end
|
#username ⇒ Object
78
79
80
81
|
# File 'lib/resources/passwd.rb', line 78
def username
warn '[DEPRECATION] `passwd.user` is deprecated. Please use `passwd.users` instead. It will be removed in version 1.0.0.'
users[0]
end
|
#usernames ⇒ Object
73
74
75
76
|
# File 'lib/resources/passwd.rb', line 73
def usernames
warn '[DEPRECATION] `passwd.usernames` is deprecated. Please use `passwd.users` instead. It will be removed in version 1.0.0.'
users
end
|
#users(name = nil) ⇒ Object
88
89
90
|
# File 'lib/resources/passwd.rb', line 88
def users(name = nil)
name.nil? ? map_data('user') : filter(user: name)
end
|