Class: Chef::Provider::User::Useradd

Inherits:
Chef::Provider::User show all
Defined in:
lib/chef/provider/user/useradd.rb

Constant Summary collapse

UNIVERSAL_OPTIONS =

the linux version of this has been forked off, this is the base class now of solaris and AIX and should be abandoned and those provider should be rewritten like the linux version.

[[:comment, "-c"], [:gid, "-g"], [:password, "-p"], [:shell, "-s"], [:uid, "-u"]].freeze

Instance Attribute Summary

Attributes inherited from Chef::Provider::User

#locked, #user_exists

Attributes inherited from Chef::Provider

#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider::User

#action_create, #action_lock, #action_manage, #action_modify, #action_remove, #action_unlock, #compare_user, #convert_group_name, #define_resource_requirements, #initialize, #load_current_resource

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #define_resource_requirements, #description, #events, include_resource_dsl?, include_resource_dsl_module, #initialize, #introduced, #load_current_resource, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

Methods included from Mixin::ShellOut

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::PowershellExec

#powershell_exec

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

This class inherits a constructor from Chef::Provider::User

Instance Method Details

#check_lockObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/chef/provider/user/useradd.rb', line 58

def check_lock
  # we can get an exit code of 1 even when it's successful on
  # rhel/centos (redhat bug 578534). See additional error checks below.
  passwd_s = shell_out_compact!("passwd", "-S", new_resource.username, returns: [0, 1])
  if whyrun_mode? && passwd_s.stdout.empty? && passwd_s.stderr.match(/does not exist/)
    # if we're in whyrun mode and the user is not yet created we assume it would be
    return false
  end

  raise Chef::Exceptions::User, "Cannot determine if #{new_resource} is locked!" if passwd_s.stdout.empty?

  status_line = passwd_s.stdout.split(" ")
  case status_line[1]
  when /^P/
    @locked = false
  when /^N/
    @locked = false
  when /^L/
    @locked = true
  end

  unless passwd_s.exitstatus == 0
    raise_lock_error = false
    if %w{redhat centos}.include?(node[:platform])
      passwd_version_check = shell_out_compact!("rpm", "-q", "passwd")
      passwd_version = passwd_version_check.stdout.chomp

      unless passwd_version == "passwd-0.73-1"
        raise_lock_error = true
      end
    else
      raise_lock_error = true
    end

    raise Chef::Exceptions::User, "Cannot determine if #{new_resource} is locked!" if raise_lock_error
  end

  @locked
end

#compile_command(base_command) {|base_command| ... } ⇒ Object

Yields:

  • (base_command)


106
107
108
109
110
111
# File 'lib/chef/provider/user/useradd.rb', line 106

def compile_command(base_command)
  base_command = Array(base_command)
  yield base_command
  base_command << new_resource.username
  base_command
end

#create_userObject



34
35
36
37
38
39
40
# File 'lib/chef/provider/user/useradd.rb', line 34

def create_user
  command = compile_command("useradd") do |useradd|
    useradd.concat(universal_options)
    useradd.concat(useradd_options)
  end
  shell_out_compact!(command)
end

#lock_userObject



98
99
100
# File 'lib/chef/provider/user/useradd.rb', line 98

def lock_user
  shell_out_compact!("usermod", "-L", new_resource.username)
end

#manage_userObject



42
43
44
45
46
47
48
# File 'lib/chef/provider/user/useradd.rb', line 42

def manage_user
  return if universal_options.empty?
  command = compile_command("usermod") do |u|
    u.concat(universal_options)
  end
  shell_out_compact!(command)
end

#remove_userObject



50
51
52
53
54
55
56
# File 'lib/chef/provider/user/useradd.rb', line 50

def remove_user
  command = [ "userdel" ]
  command << "-r" if new_resource.manage_home
  command << "-f" if new_resource.force
  command << new_resource.username
  shell_out_compact!(command)
end

#universal_optionsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/provider/user/useradd.rb', line 113

def universal_options
  @universal_options ||=
    begin
      opts = []
      # magic allows UNIVERSAL_OPTIONS to be overridden in a subclass
      self.class::UNIVERSAL_OPTIONS.each do |field, option|
        update_options(field, option, opts)
      end
      if updating_home?
        opts << "-d" << new_resource.home
        if new_resource.manage_home
          logger.trace("#{new_resource} managing the users home directory")
          opts << "-m"
        else
          logger.trace("#{new_resource} setting home to #{new_resource.home}")
        end
      end
      opts << "-o" if new_resource.non_unique
      opts
    end
end

#unlock_userObject



102
103
104
# File 'lib/chef/provider/user/useradd.rb', line 102

def unlock_user
  shell_out_compact!("usermod", "-U", new_resource.username)
end

#update_options(field, option, opts) ⇒ Object



135
136
137
138
139
140
# File 'lib/chef/provider/user/useradd.rb', line 135

def update_options(field, option, opts)
  return unless current_resource.send(field).to_s != new_resource.send(field).to_s
  return unless new_resource.send(field)
  logger.trace("#{new_resource} setting #{field} to #{new_resource.send(field)}")
  opts << option << new_resource.send(field).to_s
end

#updating_home?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
# File 'lib/chef/provider/user/useradd.rb', line 149

def updating_home?
  # will return false if paths are equivalent
  # Pathname#cleanpath does a better job than ::File::expand_path (on both unix and windows)
  # ::File.expand_path("///tmp") == ::File.expand_path("/tmp") => false
  # ::File.expand_path("\\tmp") => "C:/tmp"
  return true if current_resource.home.nil? && new_resource.home
  new_resource.home && Pathname.new(current_resource.home).cleanpath != Pathname.new(new_resource.home).cleanpath
end

#useradd_optionsObject



142
143
144
145
146
147
# File 'lib/chef/provider/user/useradd.rb', line 142

def useradd_options
  opts = []
  opts << "-r" if new_resource.system
  opts << "-M" unless new_resource.manage_home
  opts
end