summaryrefslogtreecommitdiff
path: root/lib/chef/file_content_management/deploy/mv_windows.rb
blob: e2951dba4c8e173f85dc7f78a8ef66a92e4b047f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
97
98
99
100
101
102
103
104
105
#
# Author:: Lamont Granquist (<lamont@opscode.com>)
# Copyright:: Copyright (c) 2013 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.
#

#
# We update the contents of the file, using mv for atomicity, while maintaining all the
# ACL information on the dst file.
#

require 'chef/platform/query_helpers'
if Chef::Platform.windows?
  require 'chef/win32/security'
end

class Chef
  class FileContentManagement
    class Deploy
      class MvWindows

        Security = Chef::ReservedNames::Win32::Security
        ACL = Security::ACL

        def create(file)
          Chef::Log.debug("Touching #{file} to create it")
          FileUtils.touch(file)
        end

        def deploy(src, dst)
          #
          # At the time of deploy ACLs are correctly configured on the
          # dst. This would be a simple atomic move operations in
          # windows was not converting inherited ACLs of src to
          # non-inherited ACLs in certain cases.See:
          # http://blogs.msdn.com/b/oldnewthing/archive/2006/08/24/717181.aspx
          #

          #
          # First cache the ACLs of dst file
          #

          dst_so = Security::SecurableObject.new(dst)
          begin
            # get the sd with the SACL
            dst_sd = dst_so.security_descriptor(true)
          rescue Chef::Exceptions::Win32APIError
            # Catch and raise if the user is not elevated enough.
            # At this point we can't configure the file as expected so
            # we're failing action on the resource.
            raise Chef::Exceptions::WindowsNotAdmin, "can not get the security information for '#{dst}' due to missing Administrator privileges."
          end

          dacl_present = dst_sd.dacl_present?
          if dacl_present
            if dst_sd.dacl.nil?
              apply_dacl = nil
            else
              apply_dacl = ACL.create(dst_sd.dacl.select { |ace| !ace.inherited? })
            end
          end

          sacl_present = dst_sd.sacl_present?
          if sacl_present
            if dst_sd.sacl.nil?
              apply_sacl = nil
            else
              apply_sacl = ACL.create(dst_sd.sacl.select { |ace| !ace.inherited? })
            end
          end

          #
          # Then deploy the file
          #

          FileUtils.mv(src, dst)

          #
          # Then apply the cached acls to the new dst file
          #

          dst_so = Security::SecurableObject.new(dst)
          dst_so.group = dst_sd.group
          dst_so.owner = dst_sd.owner
          dst_so.set_dacl(apply_dacl, dst_sd.dacl_inherits?) if dacl_present
          dst_so.set_sacl(apply_sacl, dst_sd.sacl_inherits?) if sacl_present

        end
      end
    end
  end
end