Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: How to read UNIX socket credentials?

by ikegami (Patriarch)
on Jan 07, 2011 at 09:14 UTC ( [id://881027]=note: print w/replies, xml ) Need Help??


in reply to Re: How to read UNIX socket credentials?
in thread How to read UNIX socket credentials?

Pretty sure it'll work with an unblessed UNIX socket handle too.

my ($pid, $uid, $gid) = IO::Handle::Record::peercred($client);

If not, it should be easy to code in pure Perl since the above function is simply

void smh_peercred(s) PerlIO* s; PROTOTYPE: $ PPCODE: { # ifdef SO_PEERCRED struct ucred uc; socklen_t uc_len=sizeof(uc); if( !getsockopt(PerlIO_fileno(s), SOL_SOCKET, SO_PEERCRED, &uc, &uc_ +len) ) { EXTEND(SP, 3); PUSHs(sv_2mortal(newSViv(uc.pid))); PUSHs(sv_2mortal(newSViv(uc.uid))); PUSHs(sv_2mortal(newSViv(uc.gid))); } # else SETERRNO(EOPNOTSUPP, RMS_IFI); # endif }

and fileno (builtin), getsockopt (builtin), SOL_SOCKET (Socket) and SO_PEERCRED (Socket) are all in core Perl.

Mind you, IO::Socket::UNIX is just a thin layer that greatly simplifies socket calls with no loss of flexibility, so I wonder why you want to try to avoid it.

Replies are listed 'Best First'.
Re^3: How to read UNIX socket credentials?
by hackman (Acolyte) on Jan 08, 2011 at 10:32 UTC
    my ($pid, $uid, $gid) = IO::Handle::Record::peercred($client);
    Thank you for the solution! It works like a charm on Linux and FreeBSD.
    One Planet, One Internet... We Are All Connected...
      This won't work on Solaris / MacOS / OpenBSD (I think anyway), as they don't support the SO_PEERCRED getsockopt() sub-call.

      Solaris is of particular interest to me; I did some digging and found that it supports getting the credentials of the remote peer (to include PF_INET sockets, so long as the remote peer is on the same system e.g. a loopback connection, a process running in another zone, or running at a different level of trust). It does it through a completely different API though; to get started, see the manpages for ucred_get() (describes the whole family) and in particular getpeerucred().

      Sun supplies Sun::Solaris::Ucred.pm with the Sun-provided Perl packages, which exposes this API to Perl.

      Here's my slapdash implementation for getting peer credentials on both Linux and Solaris (disclaimer: this is a proof of concept, I /know/ there are several glaring problems):
      use IO::Socket::UNIX qw( SOCK_STREAM SOMAXCONN SOL_SOCKET SO_PEERCRED ); # Assume that $socket represents a connected UNIX-domain socket, # and use it like so: my ($pid, $uid, $gid) = socket_peercreds($socket); sub socket_peercreds { my $socket = shift; my($gid, $os, $packed, $pid, $ucred, $uid); chomp($os = `uname -s`); if($os eq 'Linux'){ $packed = getsockopt($socket, SOL_SOCKET, SO_PEERCRED) +; # these are lowercase Ls ($pid, $uid, $gid) = unpack('lll', $packed); return ($pid, $uid, $gid); } elsif($os eq 'SunOS'){ eval "use Sun::Solaris::Ucred qw(getpeerucred ucred_ge +t ucred_geteuid ucred_getegid ucred_getpid); "; $ucred = getpeerucred(fileno($socket)); if(! defined($ucred)){ print "ERROR: getpeerucred() failed: $!\n"; print "\$ucred: $ucred\n"; return undef; } $uid = ucred_geteuid($ucred); $gid = ucred_getegid($ucred); $pid = ucred_getpid($ucred); return ($pid, $uid, $gid); } else { print "ERROR: Unknown os, can't get peer's creds\n"; return undef; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://881027]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found