From: Henry Maddocks Date: 2012-06-27T19:26:30+09:00 Subject: Re: how about adding method hook --Boundary_(ID_2sUctM1Rb+6r+lUkEXQR8w) Content-type: text/plain; CHARSET=US-ASCII Content-transfer-encoding: 7BIT On 27/06/2012, at 8:12 PM, salamond wrote: > Hi, guys. > > I want An agent on machine, so that it can fake user actions. > > ... > > My problem is, I have a very long list of actions, like mv, touch, mkdir, rm, etc. > I don't want to add save and restore euid for all methods. First thing I'd do is remove all the duplication, maybe something like this... class Agent def initialize(user, dir) @user = user @dir = dir end def as_me_do bak = Process.euid yield ensure # hat tip Robert Process.euid = bak end def mkdir(dir) as_me_do {`mkdir`} end def create_file(file, content) as_me_do {`echo #{content} > #{file}`} end end The next step might be more obvious. > I kind of guess here I should use some meta programming skills. here's what i tried. Defining methods is going to be tricky, because you don't have consistent method signatures and system calls. Maybe a have a hash of methods => system calls and method missing? Also, why back ticks, why not use ruby File methods? Henry --Boundary_(ID_2sUctM1Rb+6r+lUkEXQR8w) Content-type: text/html; CHARSET=US-ASCII Content-transfer-encoding: quoted-printable

On 27/06/20= 12, at 8:12 PM, salamond <jarodzz@gm= ail.com> wrote:

Hi, guys.

I want An agent on machine, so that it can fa= ke user actions.

...

My pr= oblem is, I have a very long list of actions, like mv, touch, mkdir, rm, etc= .
I don't want to add save and restore euid for all methods.

First thing I'd do is remove all the duplication,= maybe something like this...

class Agent
  def initialize(user, dir)
    @user =3D user
    @dir =3D dir
  end

<= div>  def as_me_do
    bak =3D Process.euid
      yield

  ensure # hat t= ip Robert
    Process.euid =3D bak
  end<= /div>

  def mkdir(dir)
    as_me= _do {`echo #{content} &= gt; #{file}`}
  end
end
The next step might be more obvious.

I kind of guess here I should use some meta programming ski= lls. here's what i tried.

Defining methods is going to be tricky, because y= ou don't have consistent method signatures and system calls. Maybe a have a h= ash of methods =3D> system calls and method missing?

=
Also, why back ticks, why not use ruby File methods?

Henry

= --Boundary_(ID_2sUctM1Rb+6r+lUkEXQR8w)--