class Chef::Provider::User::Solaris

Constants

UNIVERSAL_OPTIONS

Attributes

password_file[W]

Public Class Methods

new(new_resource, run_context) click to toggle source
Calls superclass method Chef::Provider::User.new
# File lib/chef/provider/user/solaris.rb, line 33
def initialize(new_resource, run_context)
  @password_file = "/etc/shadow"
  super
end

Public Instance Methods

check_lock() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 48
def check_lock
  user = IO.read(@password_file).match(/^#{Regexp.escape(new_resource.username)}:([^:]*):/)

  # If we're in whyrun mode, and the user is not created, we assume it will be
  return false if whyrun_mode? && user.nil?

  raise Chef::Exceptions::User, "Cannot determine if #{new_resource} is locked!" if user.nil?

  @locked = user[1].start_with?("*LK*")
end
create_user() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 38
def create_user
  super
  manage_password
end
lock_user() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 59
def lock_user
  shell_out_compact!("passwd", "-l", new_resource.username)
end
manage_user() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 43
def manage_user
  manage_password
  super
end
unlock_user() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 63
def unlock_user
  shell_out_compact!("passwd", "-u", new_resource.username)
end

Private Instance Methods

days_since_epoch() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 123
def days_since_epoch
  (Time.now.to_i / 86400).floor
end
manage_password() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 84
def manage_password
  return unless current_resource.password != new_resource.password && new_resource.password
  logger.trace("#{new_resource} setting password to #{new_resource.password}")
  write_shadow_file
end
updated_password(entry) click to toggle source
# File lib/chef/provider/user/solaris.rb, line 116
def updated_password(entry)
  fields = entry.split(":")
  fields[1] = new_resource.password
  fields[2] = days_since_epoch
  fields.join(":")
end
useradd_options() click to toggle source

Override the version from {#Useradd} because Solaris doesn't support system users and therefore has no `-r` option. This also inverts the logic for manage_home as Solaris defaults to no-manage-home and only offers `-m`.

@since 12.15 @api private @see Chef::Provider::User::Useradd#useradd_options @return [Array<String>]

# File lib/chef/provider/user/solaris.rb, line 78
def useradd_options
  opts = []
  opts << "-m" if new_resource.manage_home
  opts
end
write_shadow_file() click to toggle source
# File lib/chef/provider/user/solaris.rb, line 90
def write_shadow_file
  buffer = Tempfile.new("shadow", "/etc")
  ::File.open(@password_file) do |shadow_file|
    shadow_file.each do |entry|
      user = entry.split(":").first
      if user == new_resource.username
        buffer.write(updated_password(entry))
      else
        buffer.write(entry)
      end
    end
  end
  buffer.close

  # FIXME: mostly duplicates code with file provider deploying a file
  s = ::File.stat(@password_file)
  mode = s.mode & 0o7777
  uid  = s.uid
  gid  = s.gid

  FileUtils.chown uid, gid, buffer.path
  FileUtils.chmod mode, buffer.path

  FileUtils.mv buffer.path, @password_file
end