3

So, I've been working on a Ruby script that blocks reddit during my school hours (useful stuff). Here's the code:

require 'fileutils'

puts "-----------------------------------"
puts "Welcome to the hosts file modifier!"
puts "-----------------------------------"
puts "Option A: Use modified hosts"
puts "Option B: Use original hosts"
puts "Option C: Do nothing"
puts "Please enter your choice: "
input = gets.chomp.downcase

t = Time.now
# Time.now is used is conjunction with function 'original', in option 'b'

def modified
  # This function copies the modified (redditblocking) hosts file from Documents to /etc
  puts "Moving original hosts file out of /etc"
  FileUtils.mv('/etc/hosts', '/Users/(usernameobscured)/Documents/OriginalHosts/hosts')
  puts "Done. Now copying modified hosts to /etc"
  FileUtils.cp('/Users/(usernameobscured)/Documents/ModifiedHosts/hosts', '/etc/hosts')
  puts "Done"
end

def original
# This function deletes the modified hosts file from /etc (since we have a copy in Documents)
# and then moves the original hosts file back to /etc
  puts "Deleting modified hosts file from /etc"
  FileUtils.rm_rf('etc/hosts')
  puts "Done. Now copying original hosts to /etc"
  FileUtils.mv('/Users/(usernameobscured)/Documents/OriginalHosts/hosts', '/etc/hosts')
  puts "Done"
end

def nothing
  # This does... nothing. Literally.
  puts "Doing nothing"
end

if input == 'a'
  modified
end

if input == 'b'
  # Here's when using Time.now becomes helpful: if the hour of the day is less than 5PM,
  # then the original hosts file can't be moved back (don't wanna be on reddit during school hours!)
  if t.hour > 17
    original
  elsif t.hour < 17
    puts "Too early to use original hosts file. Come back at 5PM"
  end
end

if input == 'c'
  # Nothing...
  nothing
end

As you can see, it moves a modified hosts file from my Documents folder to /etc. The problem I'm having though, as per OS X/Unix security measures, is that I have to run the script via sudo or logged in as root. This is a minor nuisance, however, it's one that I believe can be fixed within the code. How can I get superuser privileges, OR write access to /etc temporarily, via my ruby script, so that I can simply run the script without sudo/root?

Casper
  • 33,403
  • 4
  • 84
  • 79
bluebunny
  • 295
  • 3
  • 13

2 Answers2

0

Per Unix security model, it is not possible to gain root access without some sort of external intervention (setuid set to the executable, running as root user). Otherwise we would have a gaping security hole.

I am not clear what is exactly your issue of using sudo or rvmsudo or against setting the script setuid (it is possible to configure sudo to not require password for narrowly defined set of commands).

I would just suggest making the various versions of host files group writable by a group that you are member of.

FooF
  • 4,323
  • 2
  • 31
  • 47
-1

According to this site : http://ruby.about.com/od/rubyversionmanager/qt/Rvm-And-Sudo.htm

you can start executing the script using the rvmsudo command. In your terminal window or shell script:

rvmsudo ruby blockreddit.rb
Kevin
  • 14,655
  • 24
  • 74
  • 124
  • That is assuming that @crashfocus is using RVM. There is no tag on the question nor is there any information in the post that hints this is the case. – vgoff Jul 28 '13 at 02:04
  • I am using RVM - sorry that I didn't mention that. However, I don't wish to use rvmsudo, I need something that gets superuser within the script. – bluebunny Jul 28 '13 at 02:21