I have a program that is listening to a Unix Domain Socket.
When a client connects to the socket I’d like to find out which program connected and then decide if I allow the connection or not (based on the user/group settings).
Is this possible under Linux, and if so, how?
Yes, this is possible on Linux, but it won’t be very portable. It’s achieved using what is called “ancillary data” with
sendmsg/recvmsg.SO_PASSCREDwithsetsockoptSCM_CREDENTIALSand thestruct ucredstructureThis structure is defined in Linux:
Note you have to fill these in your
msghdr.control, and the kernel will check if they’re correct.The main portability hindrance is that this structure differs on other Unixes – for example on FreeBSD it’s: