OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
 
Re: How to check UID of process on the other side of local TCP/UDP connection

From: Michael Bacarella (mbacnetgraft.com)
Date: Tue Nov 28 2006 - 11:05:28 CST


/*
 * On Mon, Nov 27, 2006 at 09:06:30PM +0100, Vladimir Mitiouchev wrote:
 * > On 11/24/06, rainmailbox2001-olayahoo.ca <rainmailbox2001-olayahoo.ca>
 * > 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 <mbacnetgraft.com>
 *
 * 1-646-641-8662 (cell)
 *
 * 545 Eighth Avenue * Suite 401
 * New York, NY 10018
 *
 * http://michael.bacarella.com/
 * http://netgraft.com/
 *
 */