Skip to content

Commit fc032dc

Browse files
committed
Merge branch 'CHEF-1262'
2 parents d455ed5 + edfa9ae commit fc032dc

9 files changed

Lines changed: 854 additions & 3 deletions

File tree

chef/lib/chef/platform.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,25 @@ def platforms
130130
:mswin => {
131131
:default => {
132132
:env => Chef::Provider::Env::Windows,
133-
:service => Chef::Provider::Service::Windows
133+
:service => Chef::Provider::Service::Windows,
134+
:user => Chef::Provider::User::Windows,
135+
:group => Chef::Provider::Group::Windows
134136
}
135137
},
136138
:mingw32 => {
137139
:default => {
138140
:env => Chef::Provider::Env::Windows,
139-
:service => Chef::Provider::Service::Windows
141+
:service => Chef::Provider::Service::Windows,
142+
:user => Chef::Provider::User::Windows,
143+
:group => Chef::Provider::Group::Windows
140144
}
141145
},
142146
:windows => {
143147
:default => {
144148
:env => Chef::Provider::Env::Windows,
145-
:service => Chef::Provider::Service::Windows
149+
:service => Chef::Provider::Service::Windows,
150+
:user => Chef::Provider::User::Windows,
151+
:group => Chef::Provider::Group::Windows
146152
}
147153
},
148154
:solaris => {},
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#
2+
# Author:: Doug MacEachern (<dougm@vmware.com>)
3+
# Copyright:: Copyright (c) 2010 VMware, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/provider/user'
20+
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
21+
require 'chef/util/windows/net_group'
22+
end
23+
24+
class Chef
25+
class Provider
26+
class Group
27+
class Windows < Chef::Provider::Group
28+
29+
def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
30+
super(node, new_resource, collection, definitions, cookbook_loader)
31+
@net_group = Chef::Util::Windows::NetGroup.new(@new_resource.name)
32+
end
33+
34+
def load_current_resource
35+
@current_resource = Chef::Resource::Group.new(@new_resource.name)
36+
@current_resource.group_name(@new_resource.group_name)
37+
38+
members = nil
39+
begin
40+
members = @net_group.local_get_members
41+
rescue => e
42+
@group_exists = false
43+
Chef::Log.debug("#{@new_resource}: group does not exist")
44+
end
45+
46+
if members
47+
@current_resource.members(members)
48+
end
49+
50+
@current_resource
51+
end
52+
53+
def create_group
54+
@net_group.local_add
55+
manage_group
56+
end
57+
58+
def manage_group
59+
if @new_resource.append
60+
begin
61+
#ERROR_MEMBER_IN_ALIAS if a member already exists in the group
62+
@net_group.local_add_members(@new_resource.members)
63+
rescue
64+
members = @new_resource.members + @current_resource.members
65+
@net_group.local_set_members(members.uniq)
66+
end
67+
else
68+
@net_group.local_set_members(@new_resource.members)
69+
end
70+
end
71+
72+
def remove_group
73+
@net_group.local_delete
74+
end
75+
76+
end
77+
end
78+
end
79+
end
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#
2+
# Author:: Doug MacEachern (<dougm@vmware.com>)
3+
# Copyright:: Copyright (c) 2010 VMware, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'chef/provider/user'
20+
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
21+
require 'chef/util/windows/net_user'
22+
end
23+
24+
class Chef
25+
class Provider
26+
class User
27+
class Windows < Chef::Provider::User
28+
29+
def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
30+
super(node, new_resource, collection, definitions, cookbook_loader)
31+
@net_user = Chef::Util::Windows::NetUser.new(@new_resource.name)
32+
end
33+
34+
def load_current_resource
35+
@current_resource = Chef::Resource::User.new(@new_resource.name)
36+
@current_resource.username(@new_resource.username)
37+
user_info = nil
38+
begin
39+
user_info = @net_user.get_info
40+
rescue
41+
@user_exists = false
42+
Chef::Log.debug("User #{@new_resource.username} does not exist")
43+
end
44+
45+
if user_info
46+
@current_resource.uid(user_info[:user_id])
47+
@current_resource.gid(user_info[:primary_group_id])
48+
@current_resource.comment(user_info[:full_name])
49+
@current_resource.home(user_info[:home_dir])
50+
@current_resource.shell(user_info[:script_path])
51+
end
52+
53+
@current_resource
54+
end
55+
56+
# Check to see if the user needs any changes
57+
#
58+
# === Returns
59+
# <true>:: If a change is required
60+
# <false>:: If the users are identical
61+
def compare_user
62+
unless @net_user.validate_credentials(@new_resource.password)
63+
Chef::Log.debug("User #{@new_resource.username} password has changed")
64+
return true
65+
end
66+
[ :uid, :gid, :comment, :home, :shell ].any? do |user_attrib|
67+
!@new_resource.send(user_attrib).nil? && @new_resource.send(user_attrib) != @current_resource.send(user_attrib)
68+
end
69+
end
70+
71+
def create_user
72+
@net_user.add(set_options)
73+
end
74+
75+
def manage_user
76+
@net_user.update(set_options)
77+
end
78+
79+
def remove_user
80+
@net_user.delete
81+
end
82+
83+
def check_lock
84+
@net_user.check_enabled
85+
end
86+
87+
def lock_user
88+
@net_user.disable_account
89+
end
90+
91+
def unlock_user
92+
@net_user.enable_account
93+
end
94+
95+
def set_options
96+
opts = {:name => @new_resource.username}
97+
98+
field_list = {
99+
'comment' => 'full_name',
100+
'home' => 'home_dir',
101+
'gid' => 'primary_group_id',
102+
'uid' => 'user_id',
103+
'shell' => 'script_path',
104+
'password' => 'password'
105+
}
106+
107+
field_list.sort{ |a,b| a[0] <=> b[0] }.each do |field, option|
108+
field_symbol = field.to_sym
109+
if @current_resource.send(field_symbol) != @new_resource.send(field_symbol)
110+
if @new_resource.send(field_symbol)
111+
unless field_symbol == :password
112+
Chef::Log.debug("Setting #{@new_resource} #{field} to #{@new_resource.send(field_symbol)}")
113+
end
114+
opts[option.to_sym] = @new_resource.send(field_symbol)
115+
end
116+
end
117+
end
118+
opts
119+
end
120+
121+
end
122+
end
123+
end
124+
end

chef/lib/chef/providers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@
7171
require 'chef/provider/user/dscl'
7272
require 'chef/provider/user/pw'
7373
require 'chef/provider/user/useradd'
74+
require 'chef/provider/user/windows'
7475

7576
require 'chef/provider/group/dscl'
7677
require 'chef/provider/group/gpasswd'
7778
require 'chef/provider/group/groupadd'
7879
require 'chef/provider/group/pw'
7980
require 'chef/provider/group/usermod'
81+
require 'chef/provider/group/windows'
8082

8183
require 'chef/provider/mount/mount'
8284

chef/lib/chef/util/windows.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Author:: Doug MacEachern (<dougm@vmware.com>)
3+
# Copyright:: Copyright (c) 2010 VMware, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
#requires: gem install windows-pr
19+
require 'windows/api'
20+
require 'windows/error'
21+
require 'windows/handle'
22+
require 'windows/unicode'
23+
require 'windows/msvcrt/buffer'
24+
require 'windows/msvcrt/string'
25+
require 'windows/network/management'
26+
27+
class Chef
28+
class Util
29+
class Windows
30+
protected
31+
32+
include ::Windows::Error
33+
include ::Windows::Unicode
34+
include ::Windows::MSVCRT::Buffer
35+
include ::Windows::MSVCRT::String
36+
include ::Windows::Network::Management
37+
38+
PTR_SIZE = 4 #XXX 64-bit
39+
40+
def lpwstr_to_s(buffer, offset)
41+
str = 0.chr * (256 * 2) #XXX unhardcode this length (*2 for WCHAR)
42+
wcscpy str, buffer[offset*PTR_SIZE,PTR_SIZE].unpack('L')[0]
43+
wide_to_multi str
44+
end
45+
46+
def dword_to_i(buffer, offset)
47+
buffer[offset*PTR_SIZE,PTR_SIZE].unpack('i')[0] || 0
48+
end
49+
50+
#return pointer for use with pack('L')
51+
def str_to_ptr(v)
52+
[v].pack('p*').unpack('L')[0]
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)