Tony A. wrote in post #955286:
I guess my question is what exactly are you trying to accomplish? Do you
want a small DSL of commands to work with files as root, or are you
expecting everything to be executed in the context of the setuid root
My immediate, practical need is to deal with files; but in the longer
term it would be nice to develop something more general, as I wrote in
my first post. Or something intermediate, as you will read later.
If it’s the former, try this:
cp “somefile”, “anotherfile”
Actually this works fine:
server.rb, run as root
File = DRbObject.new_with_uri(SERVER_URI)
puts Sudo::File.read ‘/etc/shadow’ # only readable by root
It also works with FileUtils instead of Files and probably other classes
But what if I want to distribute multiple classes/modules? In general,
what is the proper way to distribute multiple dRuby front objects?
The most obvious solution, to me, was an Array of objects as a front
I tried this:
DRb.start_service(URI, [File, FileUtils])
File, FileUtils = DRbObject.new_with_uri(SERVER_URI)
but, again, It doesn’t work:
<module:Sudo>': can't convert DRb::DRbObject to Array (DRb::DRbObject#to_ary gives DRb::DRbUnknown) (TypeError) from client.rb:7:in’
So I am compelled to run several DRb server instances?
That should operate as expected. Beyond that, you would need to use
ParseTree or ripper to extract the Ruby code you want executed on the
VM or something like that, but then you need to ensure that all the
classes/objects it’s using are actually loaded on the new VM.
I see… looks like a lot of work…
For practicality’s sake I’d suggest exposing a small DSL for doing what
want to do as root. FileUtils provides everything I’d think you need,
perhaps you have a use case I’m not envisioning.
As a more flexible alternative, you should be able to say if you want to
Sudo-ize FileUtils or other modules/classes.
A possible API might look like this:
Sudo.autoload :MyClass, ‘mygem/myclass’
Sudo.enable :File, :FileUtils, :MyClass
my_super_object = Sudo::MyClass.new
So you use superuser powers only explicitly when you really need them.