~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to lib/puppet/type/pfile/uid.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module Puppet
2
 
    Puppet.type(:file).newstate(:owner) do
3
 
        require 'etc'
4
 
        desc "To whom the file should belong.  Argument can be user name or
5
 
            user ID."
6
 
        @event = :file_changed
7
 
 
8
 
        def id2name(id)
9
 
            if id.is_a?(Symbol)
10
 
                return id.to_s
11
 
            end
12
 
            begin
13
 
                user = Etc.getpwuid(id)
14
 
            rescue TypeError
15
 
                return nil
16
 
            rescue ArgumentError
17
 
                return nil
18
 
            end
19
 
            if user.uid == ""
20
 
                return nil
21
 
            else
22
 
                return user.name
23
 
            end
24
 
        end
25
 
 
26
 
        def name2id(value)
27
 
            if value.is_a?(Symbol)
28
 
                return value.to_s
29
 
            end
30
 
            begin
31
 
                user = Etc.getpwnam(value)
32
 
                if user.uid == ""
33
 
                    return nil
34
 
                end
35
 
                return user.uid
36
 
            rescue ArgumentError => detail
37
 
                return nil
38
 
            end
39
 
        end
40
 
 
41
 
        # Determine if the user is valid, and if so, return the UID
42
 
        def validuser?(value)
43
 
            if value =~ /^\d+$/
44
 
                value = value.to_i
45
 
            end
46
 
 
47
 
            if value.is_a?(Integer)
48
 
                # verify the user is a valid user
49
 
                if tmp = id2name(value)
50
 
                    return value
51
 
                else
52
 
                    return false
53
 
                end
54
 
            else
55
 
                if tmp = name2id(value)
56
 
                    return tmp
57
 
                else
58
 
                    return false
59
 
                end
60
 
            end
61
 
        end
62
 
 
63
 
        # We want to print names, not numbers
64
 
        def is_to_s
65
 
            id2name(@is) || @is
66
 
        end
67
 
 
68
 
        def should_to_s
69
 
            case self.should
70
 
            when Symbol
71
 
                self.should.to_s
72
 
            when Integer
73
 
                id2name(self.should) || self.should
74
 
            when String
75
 
                self.should
76
 
            else
77
 
                raise Puppet::DevError, "Invalid uid type %s(%s)" %
78
 
                    [self.should.class, self.should]
79
 
            end
80
 
        end
81
 
 
82
 
        def retrieve
83
 
            unless stat = @parent.stat(false)
84
 
                @is = :absent
85
 
                return
86
 
            end
87
 
 
88
 
            # Set our method appropriately, depending on links.
89
 
            if stat.ftype == "link" and @parent[:links] != :follow
90
 
                @method = :lchown
91
 
            else
92
 
                @method = :chown
93
 
            end
94
 
 
95
 
            self.is = stat.uid
96
 
 
97
 
            # On OS X, files that are owned by -2 get returned as really
98
 
            # large UIDs instead of negative ones.  This isn't a Ruby bug,
99
 
            # it's an OS X bug, since it shows up in perl, too.
100
 
            if @is > 120000
101
 
                self.warning "current state is silly: %s" % @is
102
 
                @is = :silly
103
 
            end
104
 
        end
105
 
 
106
 
        # If we're not root, we can check the values but we cannot change
107
 
        # them.  We can't really do any processing here, because users
108
 
        # might not exist yet.  FIXME There's still a bit of a problem here
109
 
        # if the user's UID changes at run time, but we're just going to
110
 
        # have to be okay with that for now, unfortunately.
111
 
        munge do |value|
112
 
            if tmp = self.validuser?(value)
113
 
                return tmp
114
 
            else
115
 
                return value
116
 
            end
117
 
        end
118
 
 
119
 
        def sync
120
 
            unless Puppet::SUIDManager.uid == 0
121
 
                unless defined? @@notifieduid
122
 
                    self.notice "Cannot manage ownership unless running as root"
123
 
                    #@parent.delete(self.name)
124
 
                    @@notifieduid = true
125
 
                end
126
 
                return nil
127
 
            end
128
 
 
129
 
            user = nil
130
 
            unless user = self.validuser?(self.should)
131
 
                tmp = self.should
132
 
                unless defined? @@usermissing
133
 
                    @@usermissing = {}
134
 
                end
135
 
 
136
 
                if @@usermissing.include?(tmp)
137
 
                    @@usermissing[tmp] += 1
138
 
                else
139
 
                    self.notice "user %s does not exist" % tmp
140
 
                    @@usermissing[tmp] = 1
141
 
                end
142
 
                return nil
143
 
            end
144
 
 
145
 
            if @is == :absent
146
 
                @parent.stat(true)
147
 
                self.retrieve
148
 
                if @is == :absent
149
 
                    self.debug "File does not exist; cannot set owner"
150
 
                    return nil
151
 
                end
152
 
                if self.insync?
153
 
                    return nil
154
 
                end
155
 
                #self.debug "%s: after refresh, is '%s'" % [self.class.name,@is]
156
 
            end
157
 
 
158
 
            begin
159
 
                File.send(@method, user, nil, @parent[:path])
160
 
            rescue => detail
161
 
                raise Puppet::Error, "Failed to set owner to '%s': %s" %
162
 
                    [user, detail]
163
 
            end
164
 
 
165
 
            return :file_changed
166
 
        end
167
 
    end
168
 
end
169
 
 
170
 
# $Id: uid.rb 1666 2006-09-22 17:19:02Z erikh $