Hello guys, I'm writing a simple UNIX socket daemon and I want the daemon to be able to read the UID/GID/PID of the connecting process. Since this is defined in unix(7) I was expecting to find some easy way of doing it in perl. However I don't seam to find any examples on how I can implement it in Perl. Here is the most significant bit of my perl socket server:
use constant SOCK_PATH => '/tmp/catsock'; socket(Server, AF_UNIX, SOCK_STREAM,0) or die "socket: $!"; setsockopt(Server, SOL_SOCKET, SO_PASSCRED, 1) or die "SO_PASSCRED: $! +\n"; unlink(SOCK_PATH); bind (Server, sockaddr_un(SOCK_PATH)) or die "bind: $!"; listen(Server, SOMAXCONN) or die "listen: $!"; for ( $waitedpid = 0; accept(Client,Server) || $waitedpid; $waitedpid += 0, close Client) { next if $waitedpid; my $n = unpack_sockaddr_un(getsockname(Client)); logmsg 'peer: ' . $n . "\n" . <Client>; }
I wish to have something similar to this C function:
int svc_recv(int fd, char *buffer, size_t size, struct ucred *cred) { char control[1024]; struct msghdr msg; struct cmsghdr *cmsg; struct iovec iov; int result; memset(&msg, 0, sizeof(msg)); iov.iov_base = buffer; iov.iov_len = size; msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = control; msg.msg_controllen = sizeof(control); if (recvmsg(fd, &msg, 0) < 0) return -1; result = -1; cmsg = CMSG_FIRSTHDR(&msg); while (cmsg != NULL) { if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_CREDENTIALS) { memcpy(cred, CMSG_DATA(cmsg), sizeof(*cred)); result = iov.iov_len; } else if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { dispose_fds((int *) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0))/sizeof( +int)); } cmsg = CMSG_NXTHDR(&msg, cmsg); } return result; }
One Planet, One Internet... We Are All Connected...

In reply to 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.