2002-12-04 14:17:32 +08:00
|
|
|
/* getpeereid.c */
|
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#ifndef HAVE_GETPEEREID
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#if HAVE_SYS_UCRED_H
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
|
|
|
|
2002-12-05 00:03:02 +08:00
|
|
|
#if !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && defined(HAVE_SENDMSG)
|
|
|
|
#define DO_SENDMSG
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2002-12-04 14:17:32 +08:00
|
|
|
int getpeereid( int s, uid_t *euid, gid_t *egid )
|
|
|
|
{
|
|
|
|
#ifdef LDAP_PF_LOCAL
|
|
|
|
#if defined( SO_PEERCRED )
|
|
|
|
struct ucred peercred;
|
|
|
|
size_t peercredlen = sizeof peercred;
|
|
|
|
|
|
|
|
if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
|
|
|
|
(void *)&peercred, &peercredlen ) == 0 )
|
|
|
|
&& ( peercredlen == sizeof peercred ))
|
|
|
|
{
|
|
|
|
*euid = peercred.uid;
|
|
|
|
*egid = peercred.gid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined( LOCAL_PEERCRED )
|
|
|
|
struct xucred peercred;
|
|
|
|
socklen_t peercredlen = sizeof peercred;
|
|
|
|
|
|
|
|
if(( getsockopt( s, LOCAL_PEERCRED, 1,
|
|
|
|
(void *)&peercred, &peercredlen ) == 0 )
|
|
|
|
&& ( peercred.cr_version == XUCRED_VERSION ))
|
|
|
|
{
|
|
|
|
*euid = peercred.cr_uid;
|
|
|
|
*egid = peercred.cr_gid;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-05 00:03:02 +08:00
|
|
|
#elif defined( DO_SENDMSG )
|
|
|
|
int dummy, fd[2];
|
|
|
|
struct iovec iov = {(char *)&dummy, sizeof(dummy)};
|
|
|
|
struct msghdr msg = {0};
|
|
|
|
struct stat st;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_accrights = (char *)fd;
|
|
|
|
msg.msg_accrightslen = sizeof(fd);
|
|
|
|
if( recvmsg( s, &msg, 0) >= 0 && msg.msg_accrightslen == sizeof(int) )
|
|
|
|
{
|
|
|
|
dummy = fstat( fd, &st );
|
|
|
|
close(fd[0]);
|
|
|
|
if( dummy == 0 )
|
|
|
|
{
|
|
|
|
*euid = st.st_uid;
|
|
|
|
*egid = st.st_gid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2002-12-04 14:17:32 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|