From 1e6ba6f9ff74bc1204c59457911621a3ad12e8ae Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 27 Nov 2007 21:10:52 -0600 Subject: Fixing #781, from what I can tell. I'm leaving it with no tests for now, since it's a very small chunk of code and it's *insanely* difficult to test this kind problem. --- lib/puppet/util/filetype.rb | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/puppet/util/filetype.rb b/lib/puppet/util/filetype.rb index 2f1dabe62..81d93a924 100755 --- a/lib/puppet/util/filetype.rb +++ b/lib/puppet/util/filetype.rb @@ -198,26 +198,44 @@ class Puppet::Util::FileType newfiletype(:suntab) do # Read a specific @path's cron tab. def read - Puppet::Util::SUIDManager.asuser(@path) { - %x{crontab -l 2>/dev/null} - } + begin + output = Puppet::Util.execute(%w{crontab -l}, :uid => @path) + return "" if output.include?("can't open your crontab") + return output + rescue + # If there's a failure, treat it like an empty file. + return "" + end end # Remove a specific @path's cron tab. def remove - Puppet::Util::SUIDManager.asuser(@path) { - %x{crontab -r 2>/dev/null} - } + begin + Puppet::Util.execute(%w{crontab -r}, :uid => @path) + rescue => detail + raise Puppet::Error, "Could not remove crontab for %s: %s" % [@path, detail] + end end # Overwrite a specific @path's cron tab; must be passed the @path name # and the text with which to create the cron tab. def write(text) - Puppet::Util::SUIDManager.asuser(@path) { - IO.popen("crontab", "w") { |p| - p.print text - } - } + puts text + require "tempfile" + output_file = Tempfile.new("puppet") + fh = output_file.open + fh.print text + fh.close + + # We have to chown the stupid file to the user. + File.chown(Puppet::Util.uid(@path), nil, output_file.path) + + begin + Puppet::Util.execute(["crontab", output_file.path], :uid => @path) + rescue => detail + raise Puppet::Error, "Could not write crontab for %s: %s" % [@path, detail] + end + output_file.delete end end -- cgit