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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
require 'puppet'
require 'webrick/httpstatus'
require 'cgi'
module Puppet
class Server
class FileServerError < Puppet::Error; end
class FileServer < Handler
attr_accessor :local
Puppet.setdefault(:fileserverconfig, [:puppetconf, "fileserver.conf"])
#CHECKPARAMS = %w{checksum type mode owner group}
CHECKPARAMS = [:mode, :type, :owner, :group, :checksum]
@interface = XMLRPC::Service::Interface.new("fileserver") { |iface|
iface.add_method("string describe(string)")
iface.add_method("string list(string, boolean, array)")
iface.add_method("string retrieve(string)")
}
def check(dir)
unless FileTest.exists?(dir)
Puppet.notice "File source %s does not exist" % dir
return nil
end
obj = nil
unless obj = Puppet::Type::PFile[dir]
obj = Puppet::Type::PFile.create(
:name => dir,
:check => CHECKPARAMS
)
end
# we should really have a timeout here -- we don't
# want to actually check on every connection, maybe no more
# than every 60 seconds or something
#@files[mount].evaluate
obj.evaluate
return obj
end
def describe(file, client = nil, clientip = nil)
readconfig
mount, path = splitpath(file)
unless @mounts[mount].allowed?(client, clientip)
raise Puppet::Server::AuthorizationError, "Cannot access %s" % mount
end
sdir = nil
unless sdir = subdir(mount, path)
Puppet.notice "Could not find subdirectory %s" %
"//%s/%s" % [mount, path]
return ""
end
obj = nil
unless obj = self.check(sdir)
return ""
end
desc = []
CHECKPARAMS.each { |check|
if state = obj.state(check)
unless state.is
Puppet.notice "Manually retrieving info for %s" % check
state.retrieve
end
desc << state.is
else
if check == "checksum" and obj.state(:type).is == "file"
Puppet.notice "File %s does not have data for %s" %
[obj.name, check]
end
desc << nil
end
}
return desc.join("\t")
end
def handleignore(children, path, ignore)
ignore.each { |ignore|
ignored = []
Dir.glob(File.join(path,ignore), File::FNM_DOTMATCH) { |match|
ignored.push(File.basename(match))
}
children = children - ignored
}
return children
end
def initialize(hash = {})
@mounts = {}
@files = {}
if hash[:Local]
@local = hash[:Local]
else
@local = false
end
if hash[:Config] == false
@noreadconfig = true
else
@config = hash[:Config] || Puppet[:fileserverconfig]
@noreadconfig = false
end
@configtimeout = hash[:ConfigTimeout] || 60
@configstamp = nil
@congigstatted = nil
if hash.include?(:Mount)
@passedconfig = true
unless hash[:Mount].is_a?(Hash)
raise Puppet::DevError, "Invalid mount hash %s" %
hash[:Mount].inspect
end
hash[:Mount].each { |dir, name|
if FileTest.exists?(dir)
self.mount(dir, name)
end
}
else
@passedconfig = false
readconfig
end
end
def list(dir, recurse = false, ignore = false, client = nil, clientip = nil)
readconfig
mount, path = splitpath(dir)
unless @mounts[mount].allowed?(client, clientip)
raise Puppet::Server::AuthorizationError, "Cannot access %s" % mount
end
subdir = nil
unless subdir = subdir(mount, path)
Puppet.notice "Could not find subdirectory %s" %
"//%s/%s" % [mount, path]
return ""
end
obj = nil
unless FileTest.exists?(subdir)
return ""
end
#rmdir = File.dirname(File.join(@mounts[mount], path))
rmdir = nameswap(dir, mount)
desc = reclist(rmdir, subdir, recurse, ignore)
if desc.length == 0
Puppet.notice "Got no information on //%s/%s" %
[mount, path]
return ""
end
desc.collect { |sub|
sub.join("\t")
}.join("\n")
end
def mount(path, name)
if @mounts.include?(name)
if @mounts[name] != path
raise FileServerError, "%s is already mounted at %s" %
[@mounts[name].path, name]
else
# it's already mounted; no problem
return
end
end
if FileTest.directory?(path)
if FileTest.readable?(path)
@mounts[name] = Mount.new(name, path)
Puppet.info "Mounted %s at %s" % [path, name]
else
raise FileServerError, "%s is not readable" % path
end
else
raise FileServerError, "%s is not a directory" % path
end
end
def readconfig
return if @noreadconfig
if @configstamp and FileTest.exists?(@config)
if @configtimeout and @configstatted
if Time.now - @configstatted > @configtimeout
@configstatted = Time.now
tmp = File.stat(@config).ctime
if tmp == @configstamp
return
end
else
return
end
end
end
@mounts.clear
begin
File.open(@config) { |f|
mount = nil
count = 1
f.each { |line|
case line
when /^\s*#/: next # skip comments
when /^\s*$/: next # skip blank lines
when /\[(\w+)\]/:
name = $1
if mount
unless mount.path
raise Puppet::Error, "Mount %s has no path specified" %
mount.name
end
end
if @mounts.include?(name)
raise FileServerError, "%s is already mounted at %s" %
[@mounts[name], name]
end
mount = Mount.new(name)
@mounts[name] = mount
when /\s*(\w+)\s+(.+)$/:
var = $1
value = $2
case var
when "path":
mount.path = value
when "allow":
value.split(/\s*,\s*/).each { |val|
begin
Puppet.info "Allowing %s access to %s" %
[val, mount.name]
mount.allow(val)
rescue AuthStoreError => detail
raise Puppet::Error, "%s at line %s of %s" %
[detail.to_s, count, @config]
end
}
when "deny":
value.split(/\s*,\s*/).each { |val|
begin
Puppet.info "Denying %s access to %s" %
[val, mount.name]
mount.deny(val)
rescue AuthStoreError => detail
raise Puppet::Error, "%s at line %s of %s" %
[detail.to_s, count, @config]
end
}
else
raise Puppet::Error,
"Invalid argument %s at line %s" % [var, count]
end
else
raise Puppet::Error,
"Invalid line %s: %s" % [count, line]
end
count += 1
}
}
rescue Errno::EACCES => detail
raise Puppet::Error, "Cannot read %s" % @config
rescue Errno::ENOENT => detail
raise Puppet::Error, "%s does not exit" % @config
end
@configstamp = File.stat(@config).ctime
@configstatted = Time.now
end
def retrieve(file, client = nil, clientip = nil)
readconfig
mount, path = splitpath(file)
unless (@mounts.include?(mount))
raise Puppet::Server::FileServerError, "%s not mounted" % mount
end
unless @mounts[mount].allowed?(client, clientip)
raise Puppet::Server::AuthorizationError, "Cannot access %s" % mount
end
fpath = nil
if path
fpath = File.join(@mounts[mount].path, path)
else
fpath = @mounts[mount].path
end
unless FileTest.exists?(fpath)
return ""
end
str = File.read(fpath)
if @local
return str
else
return CGI.escape(str)
end
end
private
def nameswap(name, mount)
name.sub(/\/#{mount}/, @mounts[mount].path).gsub(%r{//}, '/').sub(
%r{/$}, ''
)
#Puppet.info "Swapped %s to %s" % [name, newname]
#newname
end
# recursive listing function
def reclist(root, path, recurse, ignore)
#desc = [obj.name.sub(%r{#{root}/?}, '')]
name = path.sub(root, '')
if name == ""
name = "/"
end
if name == path
raise Puppet::FileServerError, "Could not match %s in %s" %
[root, path]
end
desc = [name]
ftype = File.stat(path).ftype
desc << ftype
if recurse.is_a?(Integer)
recurse -= 1
end
ary = [desc]
if recurse == true or (recurse.is_a?(Integer) and recurse > -1)
if ftype == "directory"
children = Dir.entries(path)
if ignore
children = handleignore(children, path, ignore)
end
children.each { |child|
next if child =~ /^\.\.?$/
reclist(root, File.join(path, child), recurse, ignore).each { |cobj|
ary << cobj
}
}
end
end
return ary.reject { |c| c.nil? }
end
def splitpath(dir)
# the dir is based on one of the mounts
# so first retrieve the mount path
mount = nil
path = nil
if dir =~ %r{/(\w+)/?}
mount = $1
path = dir.sub(%r{/#{mount}/?}, '')
unless @mounts.include?(mount)
raise FileServerError, "%s not mounted" % mount
end
unless @mounts[mount].path
raise FileServerError, "Mount %s does not have a path set" % mount
end
else
raise FileServerError, "Invalid path '%s'" % dir
end
if path == ""
path = nil
end
return mount, path
end
def subdir(mount, dir)
basedir = @mounts[mount].path
dirname = nil
if dir
dirname = File.join(basedir, dir.split("/").join(File::SEPARATOR))
else
dirname = basedir
end
return dirname
end
class Mount < AuthStore
attr_reader :path, :name
def initialize(name, path = nil)
unless name =~ %r{^\w+$}
raise FileServerError, "Invalid name format '%s'" % name
end
@name = name
if path
self.path = path
end
super()
end
def path=(path)
unless FileTest.exists?(path)
raise FileServerError, "%s does not exist" % path
end
@path = path
end
def to_s
@path
end
end
end
end
end
# $Id$
|