chef 0.10.8 → 0.10.10.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. data/distro/arch/etc/rc.d/chef-client +15 -1
  2. data/distro/common/html/chef-client.8.html +4 -4
  3. data/distro/common/html/chef-expander.8.html +4 -4
  4. data/distro/common/html/chef-expanderctl.8.html +4 -4
  5. data/distro/common/html/chef-server-webui.8.html +4 -4
  6. data/distro/common/html/chef-server.8.html +4 -4
  7. data/distro/common/html/chef-solo.8.html +4 -4
  8. data/distro/common/html/chef-solr.8.html +4 -4
  9. data/distro/common/html/knife-bootstrap.1.html +6 -10
  10. data/distro/common/html/knife-client.1.html +4 -4
  11. data/distro/common/html/knife-configure.1.html +4 -4
  12. data/distro/common/html/knife-cookbook-site.1.html +6 -6
  13. data/distro/common/html/knife-cookbook.1.html +4 -4
  14. data/distro/common/html/knife-data-bag.1.html +4 -4
  15. data/distro/common/html/knife-environment.1.html +4 -4
  16. data/distro/common/html/knife-exec.1.html +4 -4
  17. data/distro/common/html/knife-index.1.html +4 -4
  18. data/distro/common/html/knife-node.1.html +5 -5
  19. data/distro/common/html/knife-role.1.html +4 -4
  20. data/distro/common/html/knife-search.1.html +4 -4
  21. data/distro/common/html/knife-ssh.1.html +5 -6
  22. data/distro/common/html/knife-status.1.html +4 -4
  23. data/distro/common/html/knife-tag.1.html +4 -4
  24. data/distro/common/html/knife.1.html +7 -8
  25. data/distro/common/html/shef.1.html +4 -4
  26. data/distro/common/man/man1/knife-bootstrap.1 +4 -4
  27. data/distro/common/man/man1/knife-client.1 +1 -1
  28. data/distro/common/man/man1/knife-configure.1 +1 -1
  29. data/distro/common/man/man1/knife-cookbook-site.1 +4 -4
  30. data/distro/common/man/man1/knife-cookbook.1 +1 -1
  31. data/distro/common/man/man1/knife-data-bag.1 +1 -1
  32. data/distro/common/man/man1/knife-environment.1 +1 -1
  33. data/distro/common/man/man1/knife-exec.1 +1 -1
  34. data/distro/common/man/man1/knife-index.1 +1 -1
  35. data/distro/common/man/man1/knife-node.1 +2 -2
  36. data/distro/common/man/man1/knife-role.1 +1 -1
  37. data/distro/common/man/man1/knife-search.1 +1 -1
  38. data/distro/common/man/man1/knife-ssh.1 +3 -7
  39. data/distro/common/man/man1/knife-status.1 +1 -1
  40. data/distro/common/man/man1/knife-tag.1 +1 -1
  41. data/distro/common/man/man1/knife.1 +5 -9
  42. data/distro/common/man/man1/shef.1 +1 -1
  43. data/distro/common/man/man8/chef-client.8 +1 -1
  44. data/distro/common/man/man8/chef-expander.8 +1 -1
  45. data/distro/common/man/man8/chef-expanderctl.8 +1 -1
  46. data/distro/common/man/man8/chef-server-webui.8 +1 -1
  47. data/distro/common/man/man8/chef-server.8 +1 -1
  48. data/distro/common/man/man8/chef-solo.8 +1 -1
  49. data/distro/common/man/man8/chef-solr.8 +1 -1
  50. data/distro/common/markdown/man1/knife-bootstrap.mkd +3 -7
  51. data/distro/common/markdown/man1/knife-cookbook-site.mkd +3 -3
  52. data/distro/common/markdown/man1/knife-node.mkd +2 -2
  53. data/distro/common/markdown/man1/knife-ssh.mkd +2 -5
  54. data/distro/common/markdown/man1/knife.mkd +7 -9
  55. data/distro/debian/etc/init.d/chef-client +22 -1
  56. data/distro/redhat/etc/init.d/chef-client +12 -1
  57. data/distro/windows/service_manager.rb +164 -0
  58. data/lib/chef/application.rb +12 -6
  59. data/lib/chef/application/client.rb +4 -3
  60. data/lib/chef/application/knife.rb +7 -12
  61. data/lib/chef/application/solo.rb +2 -1
  62. data/lib/chef/application/windows_service.rb +224 -0
  63. data/lib/chef/checksum_cache.rb +1 -0
  64. data/lib/chef/client.rb +3 -16
  65. data/lib/chef/config.rb +42 -13
  66. data/lib/chef/cookbook/metadata.rb +1 -1
  67. data/lib/chef/cookbook/syntax_check.rb +2 -2
  68. data/lib/chef/cookbook_version.rb +5 -0
  69. data/lib/chef/daemon.rb +1 -1
  70. data/lib/chef/exceptions.rb +7 -1
  71. data/lib/chef/file_access_control.rb +13 -87
  72. data/lib/chef/file_access_control/unix.rb +119 -0
  73. data/lib/chef/file_access_control/windows.rb +257 -0
  74. data/lib/chef/handler/json_file.rb +7 -1
  75. data/lib/chef/knife.rb +10 -16
  76. data/lib/chef/knife/bootstrap.rb +15 -8
  77. data/lib/chef/knife/bootstrap/centos5-gems.erb +1 -1
  78. data/lib/chef/knife/bootstrap/chef-full.erb +59 -0
  79. data/lib/chef/knife/bootstrap/ubuntu10.04-apt.erb +1 -0
  80. data/lib/chef/knife/configure.rb +2 -2
  81. data/lib/chef/knife/cookbook_site_download.rb +60 -21
  82. data/lib/chef/knife/cookbook_site_install.rb +16 -21
  83. data/lib/chef/knife/cookbook_upload.rb +77 -48
  84. data/lib/chef/knife/core/bootstrap_context.rb +3 -1
  85. data/lib/chef/knife/core/cookbook_scm_repo.rb +1 -1
  86. data/lib/chef/knife/core/node_editor.rb +1 -1
  87. data/lib/chef/knife/core/subcommand_loader.rb +1 -1
  88. data/lib/chef/knife/core/ui.rb +3 -2
  89. data/lib/chef/knife/help_topics.rb +1 -1
  90. data/lib/chef/knife/node_run_list_add.rb +14 -6
  91. data/lib/chef/knife/node_run_list_remove.rb +3 -3
  92. data/lib/chef/knife/ssh.rb +32 -13
  93. data/lib/chef/mash.rb +14 -0
  94. data/lib/chef/mixin/command.rb +1 -0
  95. data/lib/chef/mixin/command/unix.rb +5 -0
  96. data/lib/chef/mixin/convert_to_class_name.rb +2 -0
  97. data/lib/chef/mixin/deep_merge.rb +40 -18
  98. data/lib/chef/mixin/enforce_ownership_and_permissions.rb +39 -0
  99. data/lib/chef/mixin/language.rb +89 -3
  100. data/lib/chef/mixin/language_include_recipe.rb +8 -4
  101. data/lib/chef/mixin/path_sanity.rb +67 -0
  102. data/lib/chef/mixin/recipe_definition_dsl_core.rb +19 -11
  103. data/lib/chef/mixin/securable.rb +152 -0
  104. data/lib/chef/mixin/shell_out.rb +1 -1
  105. data/lib/chef/mixin/template.rb +8 -3
  106. data/lib/chef/mixins.rb +3 -0
  107. data/lib/chef/monkey_patches/moneta.rb +50 -0
  108. data/lib/chef/monkey_patches/string.rb +1 -1
  109. data/lib/chef/node.rb +2 -1
  110. data/lib/chef/platform.rb +34 -0
  111. data/lib/chef/provider.rb +23 -21
  112. data/lib/chef/provider/cron.rb +17 -12
  113. data/lib/chef/provider/cron/solaris.rb +6 -18
  114. data/lib/chef/provider/deploy.rb +14 -15
  115. data/lib/chef/provider/deploy/timestamped.rb +0 -1
  116. data/lib/chef/provider/directory.rb +1 -3
  117. data/lib/chef/provider/execute.rb +2 -2
  118. data/lib/chef/provider/file.rb +1 -75
  119. data/lib/chef/provider/git.rb +11 -9
  120. data/lib/chef/provider/group/gpasswd.rb +14 -9
  121. data/lib/chef/provider/link.rb +28 -59
  122. data/lib/chef/provider/mdadm.rb +2 -2
  123. data/lib/chef/provider/mount/mount.rb +1 -1
  124. data/lib/chef/provider/package.rb +10 -6
  125. data/lib/chef/provider/package/apt.rb +3 -1
  126. data/lib/chef/provider/package/dpkg.rb +1 -1
  127. data/lib/chef/provider/package/portage.rb +6 -3
  128. data/lib/chef/provider/package/rubygems.rb +75 -6
  129. data/lib/chef/provider/package/smartos.rb +84 -0
  130. data/lib/chef/provider/package/yum-dump.py +3 -2
  131. data/lib/chef/provider/package/yum.rb +51 -10
  132. data/lib/chef/provider/remote_directory.rb +24 -3
  133. data/lib/chef/provider/remote_file.rb +0 -6
  134. data/lib/chef/provider/route.rb +3 -3
  135. data/lib/chef/provider/service/debian.rb +2 -2
  136. data/lib/chef/provider/service/freebsd.rb +1 -1
  137. data/lib/chef/provider/service/macosx.rb +125 -0
  138. data/lib/chef/provider/service/windows.rb +5 -1
  139. data/lib/chef/provider/subversion.rb +10 -7
  140. data/lib/chef/providers.rb +3 -0
  141. data/lib/chef/resource.rb +181 -87
  142. data/lib/chef/resource/apt_package.rb +10 -1
  143. data/lib/chef/resource/chef_gem.rb +53 -0
  144. data/lib/chef/resource/conditional.rb +3 -0
  145. data/lib/chef/resource/cookbook_file.rb +12 -6
  146. data/lib/chef/resource/cron.rb +9 -0
  147. data/lib/chef/resource/directory.rb +14 -31
  148. data/lib/chef/resource/execute.rb +11 -9
  149. data/lib/chef/resource/file.rb +9 -33
  150. data/lib/chef/resource/link.rb +13 -8
  151. data/lib/chef/resource/mdadm.rb +10 -1
  152. data/lib/chef/resource/remote_directory.rb +13 -2
  153. data/lib/chef/resource/remote_file.rb +14 -7
  154. data/lib/chef/resource/smartos_package.rb +36 -0
  155. data/lib/chef/resource/template.rb +12 -5
  156. data/lib/chef/resource_platform_map.rb +153 -0
  157. data/lib/chef/resources.rb +2 -0
  158. data/lib/chef/rest.rb +55 -10
  159. data/lib/chef/rest/auth_credentials.rb +1 -0
  160. data/lib/chef/rest/rest_request.rb +24 -8
  161. data/lib/chef/role.rb +8 -2
  162. data/lib/chef/run_list.rb +1 -1
  163. data/lib/chef/run_list/run_list_expansion.rb +2 -2
  164. data/lib/chef/run_list/run_list_item.rb +7 -0
  165. data/lib/chef/runner.rb +4 -0
  166. data/lib/chef/shef.rb +2 -2
  167. data/lib/chef/shef/shef_session.rb +4 -5
  168. data/lib/chef/shell_out.rb +2 -245
  169. data/lib/chef/util/file_edit.rb +99 -89
  170. data/lib/chef/version.rb +1 -1
  171. data/lib/chef/win32/api.rb +349 -0
  172. data/lib/chef/win32/api/error.rb +921 -0
  173. data/lib/chef/win32/api/file.rb +289 -0
  174. data/lib/chef/win32/api/memory.rb +105 -0
  175. data/lib/chef/win32/api/process.rb +40 -0
  176. data/lib/chef/win32/api/psapi.rb +51 -0
  177. data/lib/chef/win32/api/security.rb +341 -0
  178. data/lib/chef/win32/api/system.rb +192 -0
  179. data/lib/chef/win32/api/unicode.rb +178 -0
  180. data/lib/chef/win32/error.rb +73 -0
  181. data/lib/chef/win32/file.rb +117 -0
  182. data/lib/chef/win32/file/info.rb +100 -0
  183. data/lib/chef/win32/handle.rb +48 -0
  184. data/lib/chef/win32/memory.rb +101 -0
  185. data/lib/chef/win32/process.rb +84 -0
  186. data/lib/chef/win32/security.rb +489 -0
  187. data/lib/chef/win32/security/ace.rb +125 -0
  188. data/lib/chef/win32/security/acl.rb +101 -0
  189. data/lib/chef/win32/security/securable_object.rb +109 -0
  190. data/lib/chef/win32/security/security_descriptor.rb +93 -0
  191. data/lib/chef/win32/security/sid.rb +199 -0
  192. data/lib/chef/win32/security/token.rb +64 -0
  193. data/lib/chef/win32/unicode.rb +43 -0
  194. data/lib/chef/win32/version.rb +119 -0
  195. metadata +104 -158
  196. data/lib/chef/shell_out/unix.rb +0 -223
  197. data/lib/chef/shell_out/windows.rb +0 -588
@@ -0,0 +1,48 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright 2011 Opscode, 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/win32/api/process'
20
+ require 'chef/win32/api/psapi'
21
+ require 'chef/win32/api/system'
22
+ require 'chef/win32/error'
23
+
24
+ class Chef
25
+ module Win32
26
+ class Handle
27
+ extend Chef::Win32::API::Process
28
+
29
+ def initialize(handle)
30
+ @handle = handle
31
+ ObjectSpace.define_finalizer(self, Handle.close_handle_finalizer(handle))
32
+ end
33
+
34
+ attr_reader :handle
35
+
36
+ def self.close_handle_finalizer(handle)
37
+ proc { close_handle(handle) }
38
+ end
39
+
40
+ def self.close_handle(handle)
41
+ unless CloseHandle(handle)
42
+ Chef::Win32::Error.raise!
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,101 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright 2011 Opscode, 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/win32/error'
20
+ require 'chef/win32/api/memory'
21
+
22
+ class Chef
23
+ module Win32
24
+ class Memory
25
+ include Chef::Win32::API::Memory
26
+ extend Chef::Win32::API::Memory
27
+
28
+ # local_alloc(length[, flags]) [BLOCK]
29
+ # Allocates memory using LocalAlloc
30
+ # If BLOCK is specified, the memory will be passed
31
+ # to the block and freed afterwards.
32
+ def self.local_alloc(length, flags = LPTR, &block)
33
+ result = LocalAlloc(flags, length)
34
+ if result.null?
35
+ Chef::Win32::Error.raise!
36
+ end
37
+ # If a block is passed, handle freeing the memory at the end
38
+ if block != nil
39
+ begin
40
+ yield result
41
+ ensure
42
+ local_free(result)
43
+ end
44
+ else
45
+ result
46
+ end
47
+ end
48
+
49
+ # local_discard(pointer)
50
+ # Discard memory. Equivalent to local_realloc(pointer, 0)
51
+ def self.local_discard(pointer)
52
+ local_realloc(pointer, 0, LMEM_MOVEABLE)
53
+ end
54
+
55
+ # local_flags(pointer)
56
+ # Get lock count and Windows flags for local_alloc allocated memory.
57
+ # Use: flags, lock_count = local_flags(pointer)
58
+ def self.local_flags(pointer)
59
+ result = LocalFlags(pointer)
60
+ if result == LMEM_INVALID_HANDLE
61
+ Chef::Win32::Error.raise!
62
+ end
63
+ [ result & ~LMEM_LOCKCOUNT, result & LMEM_LOCKCOUNT ]
64
+ end
65
+
66
+ # local_free(pointer)
67
+ # Free memory allocated using local_alloc
68
+ def self.local_free(pointer)
69
+ result = LocalFree(pointer)
70
+ if !result.null?
71
+ Chef::Win32::Error.raise!
72
+ end
73
+ end
74
+
75
+ # local_realloc(pointer, size[, flags])
76
+ # Resizes memory allocated using LocalAlloc.
77
+ def self.local_realloc(pointer, size, flags = LMEM_MOVEABLE | LMEM_ZEROINIT)
78
+ result = LocalReAlloc(pointer, size, flags)
79
+ if result.null?
80
+ Chef::Win32::Error.raise!
81
+ end
82
+ result
83
+ end
84
+
85
+ # local_size(pointer)
86
+ # Gets the size of memory allocated using LocalAlloc.
87
+ def self.local_size(pointer)
88
+ result = LocalSize(pointer)
89
+ if result == 0
90
+ Chef::Win32::Error.raise!
91
+ end
92
+ result
93
+ end
94
+
95
+ def self.local_free_finalizer(pointer)
96
+ proc { local_free(pointer) }
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,84 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright 2011 Opscode, 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/win32/api/process'
20
+ require 'chef/win32/api/psapi'
21
+ require 'chef/win32/error'
22
+ require 'chef/win32/handle'
23
+ require 'ffi'
24
+
25
+ class Chef
26
+ module Win32
27
+ class Process
28
+ include Chef::Win32::API::Process
29
+ extend Chef::Win32::API::Process
30
+ include Chef::Win32::API::PSAPI
31
+ extend Chef::Win32::API::PSAPI
32
+
33
+ def initialize(handle)
34
+ @handle = handle
35
+ end
36
+
37
+ attr_reader :handle
38
+
39
+ def id
40
+ Process.get_process_id(handle)
41
+ end
42
+
43
+ def handle_count
44
+ Process.get_process_handle_count(handle)
45
+ end
46
+
47
+ def memory_info
48
+ Process.get_process_memory_info(handle)
49
+ end
50
+
51
+ def self.get_current_process
52
+ Process.new(Handle.new(GetCurrentProcess()))
53
+ end
54
+
55
+ def self.get_process_handle_count(handle)
56
+ handle_count = FFI::MemoryPointer.new :uint32
57
+ unless GetProcessHandleCount(handle.handle, handle_count)
58
+ Chef::Win32::Error.raise!
59
+ end
60
+ handle_count.read_uint32
61
+ end
62
+
63
+ def self.get_process_id(handle)
64
+ # Must have PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION rights
65
+ result = GetProcessId(handle.handle)
66
+ if result == 0
67
+ Chef::Win32::Error.raise!
68
+ end
69
+ result
70
+ end
71
+
72
+ # Must have PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION rights,
73
+ # AND the PROCESS_VM_READ right
74
+ def self.get_process_memory_info(handle)
75
+ memory_info = PROCESS_MEMORY_COUNTERS.new
76
+ unless GetProcessMemoryInfo(handle.handle, memory_info, memory_info.size)
77
+ Chef::Win32::Error.raise!
78
+ end
79
+ memory_info
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,489 @@
1
+ #
2
+ # Author:: John Keiser (<jkeiser@opscode.com>)
3
+ # Copyright:: Copyright 2011 Opscode, 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/win32/api/security'
20
+ require 'chef/win32/error'
21
+ require 'chef/win32/memory'
22
+ require 'chef/win32/process'
23
+ require 'chef/win32/unicode'
24
+ require 'chef/win32/security/token'
25
+
26
+ class Chef
27
+ module Win32
28
+ class Security
29
+ include Chef::Win32::API::Error
30
+ extend Chef::Win32::API::Error
31
+ include Chef::Win32::API::Security
32
+ extend Chef::Win32::API::Security
33
+ extend Chef::Win32::API::Macros
34
+
35
+ def self.add_ace(acl, ace, insert_position = MAXDWORD, revision = ACL_REVISION)
36
+ acl = acl.pointer if acl.respond_to?(:pointer)
37
+ ace = ace.pointer if ace.respond_to?(:pointer)
38
+ ace_size = ACE_HEADER.new(ace)[:AceSize]
39
+ unless AddAce(acl, revision, insert_position, ace, ace_size)
40
+ Chef::Win32::Error.raise!
41
+ end
42
+ end
43
+
44
+ def self.add_access_allowed_ace(acl, sid, access_mask, revision = ACL_REVISION)
45
+ acl = acl.pointer if acl.respond_to?(:pointer)
46
+ sid = sid.pointer if sid.respond_to?(:pointer)
47
+ unless AddAccessAllowedAce(acl, revision, access_mask, sid)
48
+ Chef::Win32::Error.raise!
49
+ end
50
+ end
51
+
52
+ def self.add_access_allowed_ace_ex(acl, sid, access_mask, flags = 0, revision = ACL_REVISION)
53
+ acl = acl.pointer if acl.respond_to?(:pointer)
54
+ sid = sid.pointer if sid.respond_to?(:pointer)
55
+ unless AddAccessAllowedAceEx(acl, revision, flags, access_mask, sid)
56
+ Chef::Win32::Error.raise!
57
+ end
58
+ end
59
+
60
+ def self.add_access_denied_ace(acl, sid, access_mask, revision = ACL_REVISION)
61
+ acl = acl.pointer if acl.respond_to?(:pointer)
62
+ sid = sid.pointer if sid.respond_to?(:pointer)
63
+ unless AddAccessDeniedAce(acl, revision, access_mask, sid)
64
+ Chef::Win32::Error.raise!
65
+ end
66
+ end
67
+
68
+ def self.add_access_denied_ace_ex(acl, sid, access_mask, flags = 0, revision = ACL_REVISION)
69
+ acl = acl.pointer if acl.respond_to?(:pointer)
70
+ sid = sid.pointer if sid.respond_to?(:pointer)
71
+ unless AddAccessDeniedAceEx(acl, revision, flags, access_mask, sid)
72
+ Chef::Win32::Error.raise!
73
+ end
74
+ end
75
+
76
+ def self.adjust_token_privileges(token, privileges)
77
+ token = token.handle if token.respond_to?(:handle)
78
+ old_privileges_size = FFI::Buffer.new(:long).write_long(privileges.size_with_privileges)
79
+ old_privileges = TOKEN_PRIVILEGES.new(FFI::Buffer.new(old_privileges_size.read_long))
80
+ unless AdjustTokenPrivileges(token.handle, false, privileges, privileges.size_with_privileges, old_privileges, old_privileges_size)
81
+ Chef::Win32::Error.raise!
82
+ end
83
+
84
+ old_privileges
85
+ end
86
+
87
+ def self.convert_sid_to_string_sid(sid)
88
+ sid = sid.pointer if sid.respond_to?(:pointer)
89
+ result = FFI::MemoryPointer.new :pointer
90
+ # TODO: use the W version
91
+ unless ConvertSidToStringSidA(sid, result)
92
+ Chef::Win32::Error.raise!
93
+ end
94
+
95
+ result_string = result.read_pointer.read_string
96
+
97
+ Chef::Win32::Memory.local_free(result.read_pointer)
98
+
99
+ result_string
100
+ end
101
+
102
+ def self.convert_string_sid_to_sid(string_sid)
103
+ result = FFI::MemoryPointer.new :pointer
104
+ unless ConvertStringSidToSidW(string_sid.to_wstring, result)
105
+ Chef::Win32::Error.raise!
106
+ end
107
+
108
+ result_pointer = result.read_pointer
109
+ sid = SID.new(result_pointer)
110
+
111
+ # The result pointer must be freed with local_free
112
+ ObjectSpace.define_finalizer(sid, Memory.local_free_finalizer(result_pointer))
113
+
114
+ sid
115
+ end
116
+
117
+ def self.delete_ace(acl, index)
118
+ acl = acl.pointer if acl.respond_to?(:pointer)
119
+ unless DeleteAce(acl, index)
120
+ Chef::Win32::Error.raise!
121
+ end
122
+ end
123
+
124
+ def self.equal_sid(sid1, sid2)
125
+ sid1 = sid1.pointer if sid1.respond_to?(:pointer)
126
+ sid2 = sid2.pointer if sid2.respond_to?(:pointer)
127
+ EqualSid(sid1, sid2)
128
+ end
129
+
130
+ def self.free_sid(sid)
131
+ sid = sid.pointer if sid.respond_to?(:pointer)
132
+ unless FreeSid(sid).null?
133
+ Chef::Win32::Error.raise!
134
+ end
135
+ end
136
+
137
+ def self.get_ace(acl, index)
138
+ acl = acl.pointer if acl.respond_to?(:pointer)
139
+ ace = FFI::Buffer.new :pointer
140
+ unless GetAce(acl, index, ace)
141
+ Chef::Win32::Error.raise!
142
+ end
143
+ ACE.new(ace.read_pointer, acl)
144
+ end
145
+
146
+ def self.get_length_sid(sid)
147
+ sid = sid.pointer if sid.respond_to?(:pointer)
148
+ GetLengthSid(sid)
149
+ end
150
+
151
+ def self.get_named_security_info(path, type = :SE_FILE_OBJECT, info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION)
152
+ security_descriptor = FFI::MemoryPointer.new :pointer
153
+ hr = GetNamedSecurityInfoW(path.to_wstring, type, info, nil, nil, nil, nil, security_descriptor)
154
+ if hr != ERROR_SUCCESS
155
+ Chef::Win32::Error.raise!("get_named_security_info(#{path}, #{type}, #{info})")
156
+ end
157
+
158
+ result_pointer = security_descriptor.read_pointer
159
+ result = SecurityDescriptor.new(result_pointer)
160
+
161
+ # This memory has to be freed with LocalFree.
162
+ ObjectSpace.define_finalizer(result, Memory.local_free_finalizer(result_pointer))
163
+
164
+ result
165
+ end
166
+
167
+ def self.get_security_descriptor_control(security_descriptor)
168
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
169
+ result = FFI::Buffer.new :ushort
170
+ version = FFI::Buffer.new :uint32
171
+ unless GetSecurityDescriptorControl(security_descriptor, result, version)
172
+ Chef::Win32::Error.raise!
173
+ end
174
+ [ result.read_ushort, version.read_uint32 ]
175
+ end
176
+
177
+ def self.get_security_descriptor_dacl(security_descriptor)
178
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
179
+ present = FFI::Buffer.new :bool
180
+ defaulted = FFI::Buffer.new :bool
181
+ acl = FFI::Buffer.new :pointer
182
+ unless GetSecurityDescriptorDacl(security_descriptor, present, acl, defaulted)
183
+ Chef::Win32::Error.raise!
184
+ end
185
+ acl = acl.read_pointer
186
+ [ present.read_char != 0, acl.null? ? nil : ACL.new(acl, security_descriptor), defaulted.read_char != 0 ]
187
+ end
188
+
189
+ def self.get_security_descriptor_group(security_descriptor)
190
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
191
+ result = FFI::Buffer.new :pointer
192
+ defaulted = FFI::Buffer.new :long
193
+ unless GetSecurityDescriptorGroup(security_descriptor, result, defaulted)
194
+ Chef::Win32::Error.raise!
195
+ end
196
+
197
+ sid = SID.new(result.read_pointer, security_descriptor)
198
+ defaulted = defaulted.read_char != 0
199
+ [ sid, defaulted ]
200
+ end
201
+
202
+ def self.get_security_descriptor_owner(security_descriptor)
203
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
204
+ result = FFI::Buffer.new :pointer
205
+ defaulted = FFI::Buffer.new :long
206
+ unless GetSecurityDescriptorOwner(security_descriptor, result, defaulted)
207
+ Chef::Win32::Error.raise!
208
+ end
209
+
210
+ sid = SID.new(result.read_pointer, security_descriptor)
211
+ defaulted = defaulted.read_char != 0
212
+ [ sid, defaulted ]
213
+ end
214
+
215
+ def self.get_security_descriptor_sacl(security_descriptor)
216
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
217
+ present = FFI::Buffer.new :bool
218
+ defaulted = FFI::Buffer.new :bool
219
+ acl = FFI::Buffer.new :pointer
220
+ unless GetSecurityDescriptorSacl(security_descriptor, present, acl, defaulted)
221
+ Chef::Win32::Error.raise!
222
+ end
223
+ acl = acl.read_pointer
224
+ [ present.read_char != 0, acl.null? ? nil : ACL.new(acl, security_descriptor), defaulted.read_char != 0 ]
225
+ end
226
+
227
+ def self.initialize_acl(acl_size)
228
+ acl = FFI::MemoryPointer.new acl_size
229
+ unless InitializeAcl(acl, acl_size, ACL_REVISION)
230
+ Chef::Win32::Error.raise!
231
+ end
232
+ ACL.new(acl)
233
+ end
234
+
235
+ def self.initialize_security_descriptor(revision = SECURITY_DESCRIPTOR_REVISION)
236
+ security_descriptor = FFI::MemoryPointer.new SECURITY_DESCRIPTOR_MIN_LENGTH
237
+ unless InitializeSecurityDescriptor(security_descriptor, revision)
238
+ Chef::Win32::Error.raise!
239
+ end
240
+ SecurityDescriptor.new(security_descriptor)
241
+ end
242
+
243
+ def self.is_valid_acl(acl)
244
+ acl = acl.pointer if acl.respond_to?(:pointer)
245
+ IsValidAcl(acl) != 0
246
+ end
247
+
248
+ def self.is_valid_security_descriptor(security_descriptor)
249
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
250
+ IsValidSecurityDescriptor(security_descriptor) != 0
251
+ end
252
+
253
+ def self.is_valid_sid(sid)
254
+ sid = sid.pointer if sid.respond_to?(:pointer)
255
+ IsValidSid(sid) != 0
256
+ end
257
+
258
+ def self.lookup_account_name(name, system_name = nil)
259
+ # Figure out how big the buffers need to be
260
+ sid_size = FFI::Buffer.new(:long).write_long(0)
261
+ referenced_domain_name_size = FFI::Buffer.new(:long).write_long(0)
262
+ system_name = system_name.to_wstring if system_name
263
+ if LookupAccountNameW(system_name, name.to_wstring, nil, sid_size, nil, referenced_domain_name_size, nil)
264
+ raise "Expected ERROR_INSUFFICIENT_BUFFER from LookupAccountName, and got no error!"
265
+ elsif Chef::Win32::Error.get_last_error != ERROR_INSUFFICIENT_BUFFER
266
+ Chef::Win32::Error.raise!
267
+ end
268
+
269
+ sid = FFI::MemoryPointer.new :char, sid_size.read_long
270
+ referenced_domain_name = FFI::MemoryPointer.new :char, (referenced_domain_name_size.read_long*2)
271
+ use = FFI::Buffer.new(:long).write_long(0)
272
+ unless LookupAccountNameW(system_name, name.to_wstring, sid, sid_size, referenced_domain_name, referenced_domain_name_size, use)
273
+ Chef::Win32::Error.raise!
274
+ end
275
+
276
+ [ referenced_domain_name.read_wstring(referenced_domain_name_size.read_long), SID.new(sid), use.read_long ]
277
+ end
278
+
279
+ def self.lookup_account_sid(sid, system_name = nil)
280
+ sid = sid.pointer if sid.respond_to?(:pointer)
281
+ # Figure out how big the buffer needs to be
282
+ name_size = FFI::Buffer.new(:long).write_long(0)
283
+ referenced_domain_name_size = FFI::Buffer.new(:long).write_long(0)
284
+ system_name = system_name.to_wstring if system_name
285
+ if LookupAccountSidW(system_name, sid, nil, name_size, nil, referenced_domain_name_size, nil)
286
+ raise "Expected ERROR_INSUFFICIENT_BUFFER from LookupAccountSid, and got no error!"
287
+ elsif Chef::Win32::Error::get_last_error != ERROR_INSUFFICIENT_BUFFER
288
+ Chef::Win32::Error.raise!
289
+ end
290
+
291
+ name = FFI::MemoryPointer.new :char, (name_size.read_long*2)
292
+ referenced_domain_name = FFI::MemoryPointer.new :char, (referenced_domain_name_size.read_long*2)
293
+ use = FFI::Buffer.new(:long).write_long(0)
294
+ unless LookupAccountSidW(system_name, sid, name, name_size, referenced_domain_name, referenced_domain_name_size, use)
295
+ Chef::Win32::Error.raise!
296
+ end
297
+
298
+ [ referenced_domain_name.read_wstring(referenced_domain_name_size.read_long), name.read_wstring(name_size.read_long), use.read_long ]
299
+ end
300
+
301
+ def self.lookup_privilege_name(system_name, luid)
302
+ system_name = system_name.to_wstring if system_name
303
+ name_size = FFI::Buffer.new(:long).write_long(0)
304
+ if LookupPrivilegeNameW(system_name, luid, nil, name_size)
305
+ raise "Expected ERROR_INSUFFICIENT_BUFFER from LookupPrivilegeName, and got no error!"
306
+ elsif Chef::Win32::Error.get_last_error != ERROR_INSUFFICIENT_BUFFER
307
+ Chef::Win32::Error.raise!
308
+ end
309
+
310
+ name = FFI::MemoryPointer.new :char, (name_size.read_long*2)
311
+ unless LookupPrivilegeNameW(system_name, luid, name, name_size)
312
+ Chef::Win32::Error.raise!
313
+ end
314
+
315
+ name.read_wstring(name_size.read_long)
316
+ end
317
+
318
+ def self.lookup_privilege_display_name(system_name, name)
319
+ system_name = system_name.to_wstring if system_name
320
+ display_name_size = FFI::Buffer.new(:long).write_long(0)
321
+ language_id = FFI::Buffer.new(:long)
322
+ if LookupPrivilegeDisplayNameW(system_name, name.to_wstring, nil, display_name_size, language_id)
323
+ raise "Expected ERROR_INSUFFICIENT_BUFFER from LookupPrivilegeDisplayName, and got no error!"
324
+ elsif Chef::Win32::Error.get_last_error != ERROR_INSUFFICIENT_BUFFER
325
+ Chef::Win32::Error.raise!
326
+ end
327
+
328
+ display_name = FFI::MemoryPointer.new :char, (display_name_size.read_long*2)
329
+ unless LookupPrivilegeDisplayNameW(system_name, name.to_wstring, display_name, display_name_size, language_id)
330
+ Chef::Win32::Error.raise!
331
+ end
332
+
333
+ [ display_name.read_wstring(display_name_size.read_long), language_id.read_long ]
334
+ end
335
+
336
+ def self.lookup_privilege_value(system_name, name)
337
+ luid = FFI::Buffer.new(:uint64).write_uint64(0)
338
+ system_name = system_name.to_wstring if system_name
339
+ unless LookupPrivilegeValueW(system_name, name.to_wstring, luid)
340
+ Win32::Error.raise!
341
+ end
342
+ luid.read_uint64
343
+ end
344
+
345
+ def self.make_absolute_sd(security_descriptor)
346
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
347
+
348
+ # Figure out buffer sizes
349
+ absolute_sd_size = FFI::Buffer.new(:long).write_long(0)
350
+ dacl_size = FFI::Buffer.new(:long).write_long(0)
351
+ sacl_size = FFI::Buffer.new(:long).write_long(0)
352
+ owner_size = FFI::Buffer.new(:long).write_long(0)
353
+ group_size = FFI::Buffer.new(:long).write_long(0)
354
+ if MakeAbsoluteSD(security_descriptor, nil, absolute_sd_size, nil, dacl_size, nil, sacl_size, nil, owner_size, nil, group_size)
355
+ raise "Expected ERROR_INSUFFICIENT_BUFFER from MakeAbsoluteSD, and got no error!"
356
+ elsif Chef::Win32::Error.get_last_error != ERROR_INSUFFICIENT_BUFFER
357
+ Chef::Win32::Error.raise!
358
+ end
359
+
360
+ absolute_sd = FFI::MemoryPointer.new absolute_sd_size.read_long
361
+ owner = FFI::MemoryPointer.new owner_size.read_long
362
+ group = FFI::MemoryPointer.new group_size.read_long
363
+ dacl = FFI::MemoryPointer.new dacl_size.read_long
364
+ sacl = FFI::MemoryPointer.new sacl_size.read_long
365
+ unless MakeAbsoluteSD(security_descriptor, absolute_sd, absolute_sd_size, dacl, dacl_size, sacl, sacl_size, owner, owner_size, group, group_size)
366
+ Chef::Win32::Error.raise!
367
+ end
368
+
369
+ [ SecurityDescriptor.new(absolute_sd), SID.new(owner), SID.new(group), ACL.new(dacl), ACL.new(sacl) ]
370
+ end
371
+
372
+ def self.open_process_token(process, desired_access)
373
+ process = process.handle if process.respond_to?(:handle)
374
+ process = process.handle if process.respond_to?(:handle)
375
+ token = FFI::Buffer.new(:ulong)
376
+ unless OpenProcessToken(process, desired_access, token)
377
+ Chef::Win32::Error.raise!
378
+ end
379
+ Token.new(Handle.new(token.read_ulong))
380
+ end
381
+
382
+ def self.query_security_access_mask(security_information)
383
+ result = FFI::Buffer.new(:long)
384
+ QuerySecurityAccessMask(security_information, result)
385
+ result.read_long
386
+ end
387
+
388
+ def self.set_file_security(path, security_information, security_descriptor)
389
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
390
+ unless SetFileSecurityW(path.to_wstring, security_information, security_descriptor)
391
+ Chef::Win32::Error.raise!
392
+ end
393
+ end
394
+
395
+ def self.set_named_security_info(path, type, args)
396
+ owner = args[:owner]
397
+ group = args[:group]
398
+ dacl = args[:dacl]
399
+ sacl = args[:sacl]
400
+ owner = owner.pointer if owner && owner.respond_to?(:pointer)
401
+ group = group.pointer if group && group.respond_to?(:pointer)
402
+ dacl = dacl.pointer if dacl && dacl.respond_to?(:pointer)
403
+ sacl = sacl.pointer if sacl && sacl.respond_to?(:pointer)
404
+
405
+ # Determine the security_information flags
406
+ security_information = 0
407
+ security_information |= OWNER_SECURITY_INFORMATION if args.has_key?(:owner)
408
+ security_information |= GROUP_SECURITY_INFORMATION if args.has_key?(:group)
409
+ security_information |= DACL_SECURITY_INFORMATION if args.has_key?(:dacl)
410
+ security_information |= SACL_SECURITY_INFORMATION if args.has_key?(:sacl)
411
+ if args.has_key?(:dacl_inherits)
412
+ security_information |= (args[:dacl_inherits] ? UNPROTECTED_DACL_SECURITY_INFORMATION : PROTECTED_DACL_SECURITY_INFORMATION)
413
+ end
414
+ if args.has_key?(:sacl_inherits)
415
+ security_information |= (args[:sacl_inherits] ? UNPROTECTED_SACL_SECURITY_INFORMATION : PROTECTED_SACL_SECURITY_INFORMATION)
416
+ end
417
+
418
+ hr = SetNamedSecurityInfoW(path.to_wstring, type, security_information, owner, group, dacl, sacl)
419
+ if hr != ERROR_SUCCESS
420
+ Chef::Win32::Error.raise!
421
+ end
422
+ end
423
+
424
+ def self.set_security_access_mask(security_information)
425
+ result = FFI::Buffer.new(:long)
426
+ SetSecurityAccessMask(security_information, result)
427
+ result.read_long
428
+ end
429
+
430
+ def set_security_descriptor_dacl(security_descriptor, acl, defaulted = false, present = nil)
431
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
432
+ acl = acl.pointer if acl.respond_to?(:pointer)
433
+ present = !security_descriptor.null? if present == nil
434
+
435
+ unless SetSecurityDescriptorDacl(security_descriptor, present, acl, defaulted)
436
+ Chef::Win32::Error.raise!
437
+ end
438
+ end
439
+
440
+ def self.set_security_descriptor_group(security_descriptor, sid, defaulted = false)
441
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
442
+ sid = sid.pointer if sid.respond_to?(:pointer)
443
+
444
+ unless SetSecurityDescriptorGroup(security_descriptor, sid, defaulted)
445
+ Chef::Win32::Error.raise!
446
+ end
447
+ end
448
+
449
+ def self.set_security_descriptor_owner(security_descriptor, sid, defaulted = false)
450
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
451
+ sid = sid.pointer if sid.respond_to?(:pointer)
452
+
453
+ unless SetSecurityDescriptorOwner(security_descriptor, sid, defaulted)
454
+ Chef::Win32::Error.raise!
455
+ end
456
+ end
457
+
458
+ def self.set_security_descriptor_sacl(security_descriptor, acl, defaulted = false, present = nil)
459
+ security_descriptor = security_descriptor.pointer if security_descriptor.respond_to?(:pointer)
460
+ acl = acl.pointer if acl.respond_to?(:pointer)
461
+ present = !security_descriptor.null? if present == nil
462
+
463
+ unless SetSecurityDescriptorSacl(security_descriptor, present, acl, defaulted)
464
+ Chef::Win32::Error.raise!
465
+ end
466
+ end
467
+
468
+ def self.with_privileges(*privilege_names)
469
+ # Set privileges
470
+ token = open_process_token(Chef::Win32::Process.get_current_process, TOKEN_READ | TOKEN_ADJUST_PRIVILEGES)
471
+ old_privileges = token.enable_privileges(*privilege_names)
472
+
473
+ # Let the caller do their privileged stuff
474
+ begin
475
+ yield
476
+ ensure
477
+ # Set privileges back to what they were before
478
+ token.adjust_privileges(old_privileges)
479
+ end
480
+ end
481
+ end
482
+ end
483
+ end
484
+
485
+ require 'chef/win32/security/ace'
486
+ require 'chef/win32/security/acl'
487
+ require 'chef/win32/security/securable_object'
488
+ require 'chef/win32/security/security_descriptor'
489
+ require 'chef/win32/security/sid'