Class: Inspec::Resources::UnixFilePermissions

Inherits:
FilePermissions show all
Defined in:
lib/inspec/resources/file.rb

Instance Attribute Summary

Attributes inherited from FilePermissions

#inspec

Instance Method Summary collapse

Methods inherited from FilePermissions

#initialize

Constructor Details

This class inherits a constructor from Inspec::Resources::FilePermissions

Instance Method Details

#check_file_permission_by_mask(file, access_type, usergroup, specific_user) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/inspec/resources/file.rb', line 219

def check_file_permission_by_mask(file, access_type, usergroup, specific_user)
  usergroup = usergroup_for(usergroup, specific_user)
  flag = permission_flag(access_type)
  mask = file.unix_mode_mask(usergroup, flag)
  raise "Invalid usergroup/owner provided" if mask.nil?

  (file.mode & mask) != 0
end

#check_file_permission_by_user(access_type, user, path) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/inspec/resources/file.rb', line 228

def check_file_permission_by_user(access_type, user, path)
  flag = permission_flag(access_type)
  if inspec.os.linux?
    perm_cmd = "su -s /bin/sh -c \"test -#{flag} #{path}\" #{user}"
  elsif inspec.os.bsd? || inspec.os.solaris?
    perm_cmd = "sudo -u #{user} test -#{flag} #{path}"
  elsif inspec.os.aix?
    perm_cmd = "su #{user} -c test -#{flag} #{path}"
  elsif inspec.os.hpux?
    perm_cmd = "su #{user} -c \"test -#{flag} #{path}\""
  else
    return skip_resource "The `file` resource does not support `by_user` on your OS."
  end

  cmd = inspec.command(perm_cmd)
  cmd.exit_status == 0 ? true : false
end

#permission_flag(access_type) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/inspec/resources/file.rb', line 196

def permission_flag(access_type)
  case access_type
  when "read"
    "r"
  when "write"
    "w"
  when "execute"
    "x"
  else
    raise "Invalid access_type provided"
  end
end

#usergroup_for(usergroup, specific_user) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/inspec/resources/file.rb', line 209

def usergroup_for(usergroup, specific_user)
  if usergroup == "others"
    "other"
  elsif (usergroup.nil? || usergroup.empty?) && specific_user.nil?
    "all"
  else
    usergroup
  end
end