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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
require 'puppet'
require 'webrick/httpstatus'
require 'cgi'
require 'delegate'
module Puppet
class FileServerError < Puppet::Error; end
class Server
class FileServer < Handler
attr_accessor :local
Puppet.setdefaults("fileserver",
:fileserverconfig => ["$confdir/fileserver.conf",
"Where the fileserver configuration is stored."])
CHECKPARAMS = [:mode, :type, :owner, :group, :checksum]
@interface = XMLRPC::Service::Interface.new("fileserver") { |iface|
iface.add_method("string describe(string, string)")
iface.add_method("string list(string, string, boolean, array)")
iface.add_method("string retrieve(string, string)")
}
def authcheck(file, mount, client, clientip)
unless mount.allowed?(client, clientip)
mount.warning "%s cannot access %s" %
[client, file]
raise Puppet::Server::AuthorizationError, "Cannot access %s" % mount
end
end
# Describe a given file. This returns all of the manageable aspects
# of that file.
def describe(file, links = :ignore, client = nil, clientip = nil)
readconfig
links = links.intern if links.is_a? String
if links == :manage
raise Puppet::FileServerError, "Cannot currently copy links"
end
mount, path = splitpath(file, client)
authcheck(file, mount, client, clientip)
if client
mount.debug "Describing %s for %s" % [file, client]
end
sdir = nil
unless sdir = mount.subdir(path)
mount.notice "Could not find subdirectory %s" %
"//%s/%s" % [mount, path]
return ""
end
obj = nil
unless obj = mount.check(sdir, links)
return ""
end
desc = []
CHECKPARAMS.each { |check|
if state = obj.state(check)
unless state.is
mount.debug "Manually retrieving info for %s" % check
state.retrieve
end
desc << state.is
else
if check == "checksum" and obj.state(:type).is == "file"
mount.notice "File %s does not have data for %s" %
[obj.name, check]
end
desc << nil
end
}
return desc.join("\t")
end
# 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
# Create a new fileserving module.
def initialize(hash = {})
@mounts = {}
@files = {}
if hash[:Local]
@local = hash[:Local]
else
@local = false
end
if hash[:Config] == false
@noreadconfig = true
else
@config = Puppet::LoadedFile.new(
hash[:Config] || Puppet[:fileserverconfig]
)
@noreadconfig = false
end
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(false) # don't check the file the first time.
end
end
# List a specific directory's contents.
def list(dir, links = :ignore, recurse = false, ignore = false, client = nil, clientip = nil)
readconfig
mount, path = splitpath(dir, client)
authcheck(dir, mount, client, clientip)
if client
mount.debug "Listing %s for %s" % [dir, client]
end
subdir = nil
unless subdir = mount.subdir(path)
mount.notice "Could not find subdirectory %s" %
"%s:%s" % [mount, path]
return ""
end
obj = nil
unless FileTest.exists?(subdir)
return ""
end
rmdir = expand_mount(dir, mount)
desc = reclist(mount, rmdir, subdir, recurse, ignore)
if desc.length == 0
mount.notice "Got no information on //%s/%s" %
[mount, path]
return ""
end
desc.collect { |sub|
sub.join("\t")
}.join("\n")
end
# Mount a new directory with a name.
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
# Let the mounts do their own error-checking.
@mounts[name] = Mount.new(name, path)
@mounts[name].info "Mounted %s" % path
return @mounts[name]
end
# Read the configuration file.
def readconfig(check = true)
return if @noreadconfig
if check and ! @config.changed?
return
end
newmounts = {}
begin
File.open(@config.file) { |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 newmounts.include?(name)
raise FileServerError, "%s is already mounted at %s" %
[newmounts[name], name]
end
mount = Mount.new(name)
newmounts[name] = mount
when /^\s*(\w+)\s+(.+)$/:
var = $1
value = $2
case var
when "path":
begin
mount.path = value
rescue FileServerError => detail
Puppet.err "Removing mount %s: %s" %
[mount.name, detail]
newmounts.delete(mount.name)
end
when "allow":
value.split(/\s*,\s*/).each { |val|
begin
mount.info "allowing %s access" % val
mount.allow(val)
rescue AuthStoreError => detail
raise FileServerError, "%s at line %s of %s" %
[detail.to_s, count, @config]
end
}
when "deny":
value.split(/\s*,\s*/).each { |val|
begin
mount.info "denying %s access" % val
mount.deny(val)
rescue AuthStoreError => detail
raise FileServerError, "%s at line %s of %s" %
[detail.to_s, count, @config]
end
}
else
raise FileServerError,
"Invalid argument '%s' at line %s" % [var, count]
end
else
raise FileServerError, "Invalid line %s: %s" % [count, line]
end
count += 1
}
}
rescue Errno::EACCES => detail
Puppet.err "FileServer error: Cannot read %s; cannot serve" % @config
#raise Puppet::Error, "Cannot read %s" % @config
rescue Errno::ENOENT => detail
Puppet.err "FileServer error: '%s' does not exist; cannot serve" %
@config
#raise Puppet::Error, "%s does not exit" % @config
#rescue FileServerError => detail
# Puppet.err "FileServer error: %s" % detail
end
# Verify each of the mounts are valid.
# We let the check raise an error, so that it can raise an error
# pointing to the specific problem.
newmounts.each { |name, mount|
mount.valid?
}
@mounts = newmounts
end
# Retrieve a file from the local disk and pass it to the remote
# client.
def retrieve(file, links = :ignore, client = nil, clientip = nil)
readconfig
links = links.intern if links.is_a? String
mount, path = splitpath(file, client)
authcheck(file, mount, client, clientip)
if client
mount.info "Sending %s to %s" % [file, client]
end
fpath = nil
if path
fpath = File.join(mount.path, path)
else
fpath = mount.path
end
unless FileTest.exists?(fpath)
return ""
end
links = links.intern if links.is_a? String
if links == :ignore and FileTest.symlink?(fpath)
return ""
end
str = nil
if links == :manage
raise Puppet::Error, "Cannot copy links yet."
else
str = File.read(fpath)
end
if @local
return str
else
return CGI.escape(str)
end
end
private
# Convert from the '/mount/path' form to the real path on disk.
def expand_mount(name, mount)
# Note that the user could have passed a path with multiple /'s
# in it, and we are likely to result in multiples, so we have to
# get rid of all of them.
CGI.unescape name.sub(/\/#{mount.name}/, mount.path).gsub(%r{/+}, '/').sub(
%r{/$}, ''
)
end
# Recursively list the directory. FIXME This should be using
# puppet objects, not directly listing.
def reclist(mount, root, path, recurse, ignore)
# Take out the root of the path.
name = path.sub(root, '')
if name == ""
name = "/"
end
if name == path
raise 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(mount, root, File.join(path, child), recurse, ignore).each { |cobj|
ary << cobj
}
}
end
end
return ary.reject { |c| c.nil? }
end
# Split the path into the separate mount point and path.
def splitpath(dir, client)
# 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, "Fileserver module '%s' not mounted" % mount
end
unless @mounts[mount].path
raise FileServerError,
"Fileserver error: Mount '%s' does not have a path set" % mount
end
# And now replace the name with the actual object.
mount = @mounts[mount]
else
raise FileServerError, "Fileserver error: Invalid path '%s'" % dir
end
if path == ""
path = nil
else
# Remove any double slashes that might have occurred
path = URI.unescape(path.gsub(/\/\//, "/"))
end
return mount, path
end
def to_s
"fileserver"
end
# A simple class for wrapping mount points. Instances of this class
# don't know about the enclosing object; they're mainly just used for
# authorization.
class Mount < AuthStore
attr_reader :name
Puppet::Util.logmethods(self, true)
# Create a map for a specific client.
def self.clientmap(client)
{
"h" => client.sub(/\..*$/, ""),
"H" => client,
"d" => client.sub(/[^.]+\./, "") # domain name
}
end
# Replace % patterns as appropriate.
def self.expand(path, client = nil)
# This map should probably be moved into a method.
map = nil
if client
map = clientmap(client)
else
# Else, use the local information
map = localmap()
end
path.gsub(/%(.)/) do |v|
key = $1
if key == "%"
"%"
else
map[key] || v
end
end
end
# Cache this manufactured map, since if it's used it's likely
# to get used a lot.
def self.localmap
unless defined? @localmap
@localmap = {
"h" => Facter["hostname"].value,
"H" => [Facter["hostname"].value,
Facter["domain"].value].join("."),
"d" => Facter["domain"].value,
}
end
@localmap
end
# Run 'retrieve' on a file. This gets the actual parameters, so
# we can pass them to the client.
def check(dir, links)
unless FileTest.exists?(dir)
self.notice "File source %s does not exist" % dir
return nil
end
obj = fileobj(dir, links)
# FIXME 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. It'd be nice if we
# could use the builtin scheduling to do this.
# Retrieval is enough here, because we don't want to cache
# any information in the state file, and we don't want to generate
# any state changes or anything. We don't even need to sync
# the checksum, because we're always going to hit the disk
# directly.
obj.retrieve
return obj
end
# Do we have any patterns in our path, yo?
def expandable?
if defined? @expandable
@expandable
else
false
end
end
# Create out object. It must have a 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
else
@path = nil
end
@comp = Puppet.type(:component).create(
:name => "mount[#{name}]"
)
#@comp.type = "mount"
#@comp.name = name
super()
end
def fileobj(path, links)
obj = nil
if obj = Puppet.type(:file)[path]
# This can only happen in local fileserving, but it's an
# important one. It'd be nice if we didn't just set
# the check params every time, but I'm not sure it's worth
# the effort.
obj[:check] = CHECKPARAMS
else
obj = Puppet.type(:file).create(
:name => path,
:check => CHECKPARAMS
)
@comp.push(obj)
end
if links == :manage
links = :follow
end
# This, ah, might be completely redundant
unless obj[:links] == links
obj[:links] = links
end
return obj
end
# Return the path as appropriate, expanding as necessary.
def path(client = nil)
if expandable?
return self.class.expand(@path, client)
else
return @path
end
end
# Set the path.
def path=(path)
# FIXME: For now, just don't validate paths with replacement
# patterns in them.
if path =~ /%./
# Mark that we're expandable.
@expandable = true
else
unless FileTest.exists?(path)
raise FileServerError, "%s does not exist" % path
end
unless FileTest.directory?(path)
raise FileServerError, "%s is not a directory" % path
end
unless FileTest.readable?(path)
raise FileServerError, "%s is not readable" % path
end
@expandable = false
end
@path = path
end
# Retrieve a specific directory relative to a mount point.
# If they pass in a client, then expand as necessary.
def subdir(dir = nil, client = nil)
basedir = self.path(client)
dirname = if dir
File.join(basedir, dir.split("/").join(File::SEPARATOR))
else
basedir
end
dirname
end
def to_s
"mount[#{@name}]"
end
# Verify our configuration is valid. This should really check to
# make sure at least someone will be allowed, but, eh.
def valid?
unless @path
raise FileServerError, "No path specified"
end
end
end
end
end
end
# $Id$
|