I'm trying to check file ownership of every file in every home directory on a Linux machine as an Inspec test written in ruby, utilizing Linux commands. I have an array of home directory owners called ownerArray (containing users like bin, root, daemon, ...), and I'm trying to use the Dir.glob class to get every file in every directory for every owner contained in my array, but when I run my code it only loops through bin. There are about 15 owners all from /etc/passwd that I would like to loop through, but the following only checks bin (I can tell when I print the name of each file as it loops). Is this a permissions issue, or is there I different way I should use Dir.glob to get every file of every owner of a home directory on my machine. Thank you for your time and help.
ownerArray = %w()
command("cut -d : -f1 /etc/passwd").stdout.each_line do |line|
if (command("echo ~#{line}").stdout != "/\n")
user = "/" + line.chomp()
ownerArray.push user
end
end
ownerArray.each do |owner|
Dir.glob("#{owner}/**/*", File::FNM_DOTMATCH).each do |file|
next if File.basename(file) == '.' || File.basename(file) == '..'
group = command("stat -c %G #{file}").stdout
describe command("id -Gn #{owner} | grep -c '\b{group}\b'") do
its ('stdout') {should_not match "0\n"}
end
end
end
See ownerArray added above. It's just grabbing users from /etc/passwd. For output I expect the test to fail every time files exist in a home directory for which the group owning those files is not a group of which the home directory owner is a member. The grep returns 0 if the home directory owner is not a member of the group owning a given file in the users home directory.