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; } }

In reply to Re^4: How to read UNIX socket credentials? by sayotte
in thread How to read UNIX socket credentials? by hackman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.