Re: How to check UID of process on the other side of local TCP/UDP connection

Michael Bacarella <[email protected]> Tue, 28 Nov 2006 11:05:28 -0600
Newsgroups gmane.comp.security.linux
Message-ID <[email protected]>
/* 
 * On Mon, Nov 27, 2006 at 09:06:30PM +0100, Vladimir Mitiouchev wrote:
 * > On 11/24/06, [email protected] <[email protected]> 
 * > wrote:
 * > >Do you have any ideas how this local
 * > >authentication can be achieved in some
 * > >different way?
 * > identd
 * > fstat (BSD)
 * > lsof(Linux)
 * > 
 * > >Unix sockets (unless of course Unix sockets are
 * > >the only good way to
 * > >resolve my problems).
 * > SCM_CREDS (BSD)
 * > SO_PEERCRED (Linux)
 *
 *  Use getpeereuid()
 *
 *	Here's an implementation if your system doesn't provide it libc.
 */

#include <sys/socket.h>

uid_t getpeereuid(int sd)
{
        struct ucred cred;
        int len = sizeof (cred);

        if (getsockopt(sd,SOL_SOCKET,SO_PEERCRED,&cred,&len))
                return -1;

        return cred.uid;
}

/*
 *
 * -- 
 * Michael Bacarella <[email protected]>
 * 
 *  1-646-641-8662 (cell)
 * 
 * 545 Eighth Avenue * Suite 401
 * New York, NY 10018
 * 
 * http://michael.bacarella.com/
 * http://netgraft.com/
 * 
 */