summaryrefslogtreecommitdiffstats
path: root/lib/puppet/file_serving/configuration.rb
blob: ccf0957d11d4bd1091475e68091e86e46dc7805e (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#
#  Created by Luke Kanies on 2007-10-16.
#  Copyright (c) 2007. All rights reserved.

require 'puppet'
require 'puppet/file_serving'
require 'puppet/file_serving/mount'

class Puppet::FileServing::Configuration
    require 'puppet/file_serving/configuration/parser'

    @config_fileuration = nil

    Mount = Puppet::FileServing::Mount

    # Remove our singleton instance.
    def self.clear_cache
        @config_fileuration = nil
    end

    # Create our singleton configuration.
    def self.create
        unless @config_fileuration
            @config_fileuration = new()
        end
        @config_fileuration
    end

    private_class_method  :new

    # Verify that the client is allowed access to this file.
    def authorized?(file, options = {})
        mount, file_path = split_path(file, options[:node])
        # If we're not serving this mount, then access is denied.
        return false unless mount
        return mount.allowed?(options[:node], options[:ipaddress])
    end

    # Search for a file.
    def file_path(key, options = {})
        mount, file_path = split_path(key, options[:node])

        return nil unless mount

        # The mount checks to see if the file exists, and returns nil
        # if not.
        return mount.file(file_path, options)
    end

    def initialize
        @mounts = {}
        @config_file = nil

        # We don't check to see if the file is modified the first time,
        # because we always want to parse at first.
        readconfig(false)
    end

    # Is a given mount available?
    def mounted?(name)
        @mounts.include?(name)
    end

    def umount(name)
        @mounts.delete(name) if @mounts.include? name
    end

    private

    # Deal with ignore parameters.
    def handleignore(children, path, ignore)            
        ignore.each { |ignore|                
            Dir.glob(File.join(path,ignore), File::FNM_DOTMATCH) { |match|
                children.delete(File.basename(match))
            }                
        }
        return children
    end  

    # Read the configuration file.
    def readconfig(check = true)
        config = Puppet[:fileserverconfig]

        return unless FileTest.exists?(config)

        @parser ||= Puppet::FileServing::Configuration::Parser.new(config)

        if check and ! @parser.changed?
            return
        end

        # Don't assign the mounts hash until we're sure the parsing succeeded.
        begin
            newmounts = @parser.parse
            @mounts = newmounts
        rescue => detail
            Puppet.err "Error parsing fileserver configuration: %s; using old configuration" % detail
        end
    end

    # Split the path into the separate mount point and path.
    def split_path(uri, node)
        # Reparse the configuration if necessary.
        readconfig

        raise(ArgumentError, "Cannot find file: Invalid path '%s'" % uri) unless uri =~ %r{/([-\w]+)/?}

        # the dir is based on one of the mounts
        # so first retrieve the mount path
        mount = path = nil
        # Strip off the mount name.
        mount_name, path = uri.sub(%r{^/}, '').split(File::Separator, 2)

        return nil unless mount = @mounts[mount_name]

        if path == ""
            path = nil
        elsif path
            # Remove any double slashes that might have occurred
            path = URI.unescape(path.gsub(/\/\//, "/"))
        end

        return mount, path
    end

    def to_s
        "fileserver"
    end
end