Re: [Wtr-general] launch broswer as another user ?

2006-08-23 Thread Lonny Eachus

I know of two relatively simple means of starting IE under separate 
processes using different user credentials. Exactly how you do it 
depends on your requirements and preferences.

(1) You can use the Windows RunAs command to start a program under 
different user credentials. RunAs is well-docmented, you can find more 
than you want to know about it with a simple internet search. Of course, 
you have to have the separate user accounts already set up first!  : o )

There is a  quirk with Windows' RunAs. Microsoft, in their infinite 
wisdom, has refused to put a command in their command-line interface 
that will allow you to enter a user's password directly. So when you try 
to run something as a different user (via whatever means), you will be 
prompted to enter the password manually. Obviously, that creates 
problems for scripting. Their workaround is the /savecred command-line 
switch for RunAs. In effect, it lets you run something with another's 
credentials... once they have been saved by someone who knows that 
password. A full discussion would take up too much space... you can 
easily look this one up. Suffice it to say that you can shell from Ruby 
using RunAs, and start a process under a different user's credentials. 
While it takes a little preparation (very little), this is MUCH easier 
than some of the other methods I have seen.  Just look up RunAs  
/savecred on the internet.

(2) You can shell to an AutoIt script that uses AutoIt's RunAs 
command, which takes the username and password as arguments and starts a 
process under a different user's credentials.

We have used both methods and they work. The funny thing is, Microsoft's 
savecred switch can be a massive security hole... the only practical 
difference between that and allowing you to script a password is that 
you have to have the password entered at least once...  which is goofy. 
Obviously there is a way to send the password to the operating system 
through an API call or some such, because AutoIt does just that.

Microsoft: Security through obfuscation.

Lonny Eachus
==

 In my case i needed to start up several browsers concurrently and in this 
 scenario, the only way to attach to them reliably and cleanly is to know the 
 process id of each browser. I stole most of the code from Alex Verk, so it 
 wasn't really a lot of coding on my part.

 If you know of an easier way (Two short lines of code) to help Marco, i'm 
 sure he and others would appreciate seeing it.

 Bret
   


___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general


Re: [Wtr-general] launch broswer as another user ?

2006-08-23 Thread Lonny Eachus

I should add that using this method, once you have your IE started, of 
course you still have to attach to the IE window.

Lonny Eachus

___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general


Re: [Wtr-general] launch broswer as another user ?

2006-08-21 Thread Bret Pettichord
Marco wrote:
 Hi

 has anyone had to come up with a way of launching an instance of ie from 
 within a script but run as another user context?

 I tried using cpau and I can launch a browser using a ruby script. I then run 
 that script using 

 cpau -u user -p password -ex cmd /K \launchbrowser.rb params -enc 
 -file launchbrowser.job

 cpau -dec -file launchbrowser.job

 whilst this works fine to run the script standalone when I try to run this 
 script from within a testcase using

 puts(Foo1)
 exec cpau -dec -file launchbrowser.job
 puts(Foo2)

 the browser launches but the testcase dosen't run through to completition 
 drops out to cmd prompt as soon as the exec finishes.

 the plan was then to use ie.attach to attach to this browser window

 I'm hoping someone else has an easier / better idea to do this sort of thing?
   
The code below should be a step in the right direction. I have been 
using this and expect contribute it to the main watir code base at some 
point.

Bret


# based on http://svn.instiki.org/instiki/trunk/test/watir/e2e.rb
# and http://rubyforge.org/pipermail/wtr-general/2005-November/004108.html

require 'watir'

class IEProcess
  def self.start
startup_info = [68].pack('lx64')
process_info = [0, 0, 0, 0].pack('')

# TODO: make this portable
startup_command = 'C:\Program Files\Internet Explorer\IEXPLORE.EXE'

result = Win32API.new('kernel32.dll', 'CreateProcess', 'pplppp', 
'l').
  call(
   nil,
   startup_command,
   0, 0, 1, 0, 0, '.', startup_info, process_info)

# TODO print the error code, or better yet a text message
raise Failed to start IEXPLORE. if result == 0

process_id = process_info.unpack('')[2]
puts Process ID: #{process_id}
self.new process_id
  end
 
  def initialize process_id
@process_id = process_id
  end
  attr_reader :process_id

  def stop
right_to_terminate_process = 1
handle = Win32API.new('kernel32.dll', 'OpenProcess', 'lil', 'l').
  call(right_to_terminate_process, 0, @process_id)
Win32API.new('kernel32.dll', 'TerminateProcess', 'll', 
'l').call(handle, 0)
  end
 
  def window
shell = WIN32OLE.new 'Shell.Application'
while true do
  shell.windows.each do |window|

process_id = Watir.process_id_from_hwnd window.hwnd

# puts Window Name: #{window.name}, Process ID: #{process_id}
return window if process_id == @process_id

  end
end
  end
 
end

module Watir
  def self.process_id_from_hwnd hwnd
pid_info = ' ' * 32
Win32API.new(user32, GetWindowThreadProcessId, 'ip', 'i').
  call(hwnd, pid_info)
process_id =  pid_info.unpack(L)[0]
  end

  def IE.new_process
iep = IEProcess.start
ie = IE.bind iep.window
ie.process_id = iep.process_id
ie
  end
 
  class IE
def process_id
  @process_id ||= Watir.process_id_from_hwnd @ie.hwnd
end
attr_writer :process_id
def kill
  iep = IEProcess.new process_id
  iep.stop
end
  end
end

___
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general