diff options
author | Lamont Granquist <lamont@opscode.com> | 2013-04-17 14:56:59 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@opscode.com> | 2013-04-17 18:29:25 -0700 |
commit | 257c32fb57d1a94d36057f88589f4d888250eee4 (patch) | |
tree | 0e80781818e87ecbd1fc612a963aa20ed29601be /lib | |
parent | 305058b7b7eca03019248bd88ca20426a0f0cc11 (diff) | |
download | chef-257c32fb57d1a94d36057f88589f4d888250eee4.tar.gz |
port SNSs solaris user provider
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider/user/solaris.rb | 75 | ||||
-rw-r--r-- | lib/chef/provider/user/useradd.rb | 46 | ||||
-rw-r--r-- | lib/chef/providers.rb | 1 |
3 files changed, 105 insertions, 17 deletions
diff --git a/lib/chef/provider/user/solaris.rb b/lib/chef/provider/user/solaris.rb new file mode 100644 index 0000000000..25d3f997db --- /dev/null +++ b/lib/chef/provider/user/solaris.rb @@ -0,0 +1,75 @@ +# +# Author:: Stephen Nelson-Smith (<sns@opscode.com>) +# Author:: Jon Ramsey (<jonathon.ramsey@gmail.com>) +# Copyright:: Copyright (c) 2012 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +class Chef + class Provider + class User + class Solaris < Chef::Provider::User::Useradd + attr_writer :password_file + + def initialize(new_resource, run_context) + @password_file = "/etc/shadow" + super + end + + def password_option + if @current_resource.password != @new_resource.password && @new_resource.password + Chef::Log.debug("#{@new_resource} setting password to #{@new_resource.password}") + write_shadow_file + end + "" + end + + def write_shadow_file + buffer = Tempfile.new("shadow") + ::File.open(@password_file, ::File::RDWR|::File::CREAT) 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.rewind + make_it_real(buffer) + end + + private + + def updated_password(entry) + fields = entry.split(":") + fields[1] = @new_resource.password + fields[2] = days_since_epoch + fields.join(":") + "\n" + end + + def make_it_real(buffer) + FileUtils.link buffer.path, @password_file, :force => true + buffer.unlink + end + + def days_since_epoch + (Time.now.to_i / 86400).floor + end + end + end + end +end + diff --git a/lib/chef/provider/user/useradd.rb b/lib/chef/provider/user/useradd.rb index 489632f722..4ec8e2f8fb 100644 --- a/lib/chef/provider/user/useradd.rb +++ b/lib/chef/provider/user/useradd.rb @@ -6,9 +6,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,32 +21,34 @@ require 'chef/provider/user' class Chef class Provider - class User + class User class Useradd < Chef::Provider::User - UNIVERSAL_OPTIONS = [[:comment, "-c"], [:gid, "-g"], [:password, "-p"], [:shell, "-s"], [:uid, "-u"]] + UNIVERSAL_OPTIONS = [[:comment, "-c"], [:gid, "-g"], [:shell, "-s"], [:uid, "-u"]] def create_user command = compile_command("useradd") do |useradd| useradd << universal_options + useradd << password_option useradd << useradd_options end run_command(:command => command) end - + def manage_user command = compile_command("usermod") do |u| u << universal_options + u << password_option end run_command(:command => command) end - + def remove_user command = "userdel" command << " -r" if managing_home_dir? command << " #{@new_resource.username}" run_command(:command => command) end - + def check_lock status = popen4("passwd -S #{@new_resource.username}") do |pid, stdin, stdout, stderr| status_line = stdout.gets.split(' ') @@ -80,11 +82,11 @@ class Chef @locked end - + def lock_user run_command(:command => "usermod -L #{@new_resource.username}") end - + def unlock_user run_command(:command => "usermod -U #{@new_resource.username}") end @@ -94,17 +96,12 @@ class Chef base_command << " #{@new_resource.username}" base_command end - + def universal_options opts = '' - + UNIVERSAL_OPTIONS.each do |field, option| - if @current_resource.send(field) != @new_resource.send(field) - if @new_resource.send(field) - Chef::Log.debug("#{@new_resource} setting #{field} to #{@new_resource.send(field)}") - opts << " #{option} '#{@new_resource.send(field)}'" - end - end + update_options(field, option, opts) end if updating_home? if managing_home_dir? @@ -119,6 +116,21 @@ class Chef opts end + def password_option + opts = '' + update_options(:password, "-p", opts) + opts + end + + def update_options(field, option, opts) + if @current_resource.send(field) != @new_resource.send(field) + if @new_resource.send(field) + Chef::Log.debug("#{@new_resource} setting #{field} to #{@new_resource.send(field)}") + opts << " #{option} '#{@new_resource.send(field)}'" + end + end + end + def useradd_options opts = '' opts << " -r" if @new_resource.system diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index ae95632eaa..aa14b92d28 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -84,6 +84,7 @@ require 'chef/provider/user/dscl' require 'chef/provider/user/pw' require 'chef/provider/user/useradd' require 'chef/provider/user/windows' +require 'chef/provider/user/solaris' require 'chef/provider/group/aix' require 'chef/provider/group/dscl' |