List Info

Thread: Impersonating a user (p4perl 2007.3)




Impersonating a user (p4perl 2007.3)
user name
2008-07-09 12:15:33
Hi P4Perlers,

How can users who have password-protected accounts be
impersonated,
using p4perl 2007.3 ?

I get a "password invalid" error when running
$p4->RunLogin($user_to_impersonate) even if a superuser
has been
specified with SetUser, so no ticket gets generated.

Another related question: the only method I found to
impersonate users
(without password protection), is a bit convoluted: for each
user, I
have to use P4::SetUser($user_to_impersonate), which means
have to
P4::Connect() and therefore P4:isconnec
t(). The latter seems to act
as a destructor, so I end up having to re-instance a P4
object inside
a foreach loop, i.e.:

use P4;
our $p4 = new P4;

$p4->Connect();
my $usersref = $p4->RunUsers();
$p4->Disconnect();

foreach my $userref ($usersref) {
    my $user = $userref->{'User'};
    $p4 = new P4;
    $p4->SetUser($user);
    $p4->Connect();
    my info = $p4->RunInfo();
    # Do something with info
    $p4->Disconnect();
}

Surely there must be a better way ! Any ideas ?

Cheers,
Guillaume
_______________________________________________
p4perl mailing list
p4perlperforce.com

http://maillist.perforce.com/mailman/listinfo/p4perl

Re: Impersonating a user (p4perl 2007.3)
country flaguser name
United Kingdom
2008-07-10 05:12:32
Hi Guillaume,

> How can users who have password-protected accounts be
impersonated,
> using p4perl 2007.3 ?
>
> I get a "password invalid" error when
running
> $p4->RunLogin($user_to_impersonate) even if a
superuser has been
> specified with SetUser, so no ticket gets generated.

Have you got a valid ticket for the super-user? You have to
prove you are the 
super-user before you can run 'p4 login <otheruser>'.
Can you run other 
commands as that super-user?

> Another related question: the only method I found to
impersonate users
> (without password protection), is a bit convoluted: for
each user, I
> have to use P4::SetUser($user_to_impersonate), which
means have to
> P4::Connect() and therefore P4:isconnec
t(). The latter seems to act
> as a destructor, so I end up having to re-instance a P4
object inside
> a foreach loop, i.e.:
>
> use P4;
> our $p4 = new P4;
>
> $p4->Connect();
> my $usersref = $p4->RunUsers();
> $p4->Disconnect();
>
> foreach my $userref ($usersref) {
>     my $user = $userref->{'User'};
>     $p4 = new P4;
>     $p4->SetUser($user);
>     $p4->Connect();
>     my info = $p4->RunInfo();
>     # Do something with info
>     $p4->Disconnect();
> }
>
> Surely there must be a better way ! Any ideas ?

No, that's the right way to do it - authentication is done
once per session, 
not once per command (otherwise that would be slow), so you
need a new 
session for each user you want to impersonate.

Regards,

Tony
_______________________________________________
p4perl mailing list
p4perlperforce.com

http://maillist.perforce.com/mailman/listinfo/p4perl

Re: Impersonating a user (p4perl 2007.3)
user name
2008-07-14 09:31:39
>> I get a "password invalid" error when
running
>> $p4->RunLogin($user_to_impersonate) even if a
superuser has been
>> specified with SetUser, so no ticket gets
generated.
>
> Have you got a valid ticket for the super-user? You
have to prove you are the
> super-user before you can run 'p4 login
<otheruser>'. Can you run other
> commands as that super-user?

Sorry for the silence, but I have been sidetracked. To reply
to your
question, yes I believe I had a valid ticket for superuser
and could
execute commands as that superuser. However, I am failing to
display
the ticket expiry time. So I seem to have 2 issues with
P4PERL/LINUX26I386/2007.3/151416 (2007.3 API) (2008/04/09).

1- I can't seem to output a users ticket expiry time:
$p4->RunLogin("-s") is mute, so I have to
resort to other means to
determine if the current user (for instance the superuser)
has a valid
ticket. In the example below, I run "p4 depots" as
the superuser is
the only one with access to one of the depots.
2- RunLogin($user) doesn't seem to honor even though the
current user
is superuser. I get Password Invalid.

Unless I am doing something wrong of course, so here's the
script I
used as a test (p4adm is superuser):
In the mean time, I solved my problem by not using the API.
Guillaume

#!/usr/local/bin/perl -w

use strict;
use P4;

our $p4 = new P4;

my users = ('testo', 'testp');

$p4->SetUser("p4adm");
$p4->Connect() or die ("Couldn't initialise p4
connection.nn",
$p4->Errors, "n");

my $inforef = $p4->RunInfo();
print "user is ",
$inforef->[0]->{'userName'}, "n";  # does
say that
user is p4adm
print P4::Identify(), "n";
my $depotsref = $p4->RunDepots(); # works so running
ticket is valid
foreach my $depot ($depotsref) {
        print "p4adm seeing depot : ",
$depot->{'name'}, "n";
}
foreach my $user (users) {
        $p4->RunLogin($user);
        print $p4->Errors(), "n" if
$p4->ErrorCount();  # prints
Password Invalid everytime
}
$p4->Disconnect();

foreach my $user (users) {
        $p4 = new P4;
        $p4->SetUser($user);
        $p4->Connect();
        $inforef = $p4->RunInfo();
        print "now user is ",
$inforef->[0]->{'userName'}, "n";
        $depotsref = $p4->RunDepots(); # Only works if
user has a
valid running ticket before script is invoked
        foreach my $depot ($depotsref) {
                print "user ", $user, "
seeing depot : ",
$depot->{'name'}, "n";
        }
        $p4->Disconnect();
}

exit 0;
_______________________________________________
p4perl mailing list
p4perlperforce.com

http://maillist.perforce.com/mailman/listinfo/p4perl

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )