2002-12-04 14:17:32 +08:00
|
|
|
/* getpeereid.c */
|
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2004-01-02 03:15:16 +08:00
|
|
|
* Copyright 2000-2004 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
2002-12-04 14:17:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#ifndef HAVE_GETPEEREID
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <ac/socket.h>
|
2003-03-02 15:46:33 +08:00
|
|
|
#include <ac/errno.h>
|
2002-12-04 14:17:32 +08:00
|
|
|
|
|
|
|
#if HAVE_SYS_UCRED_H
|
2003-05-06 01:29:26 +08:00
|
|
|
#if HAVE_GRP_H
|
|
|
|
#include <grp.h> /* for NGROUPS on Tru64 5.1 */
|
|
|
|
#endif
|
2002-12-04 14:17:32 +08:00
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
|
|
|
|
2003-03-03 05:46:40 +08:00
|
|
|
#if !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
|
|
|
|
defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
|
2003-03-02 15:46:33 +08:00
|
|
|
#define DO_SENDMSG
|
2003-03-03 05:21:36 +08:00
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
2003-03-02 15:46:33 +08:00
|
|
|
#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;
|
|
|
|
}
|
2003-03-02 15:46:33 +08:00
|
|
|
#elif defined( DO_SENDMSG )
|
|
|
|
int dummy, fd[2];
|
2003-03-05 05:49:21 +08:00
|
|
|
struct iovec iov;
|
2003-03-02 15:46:33 +08:00
|
|
|
struct msghdr msg = {0};
|
|
|
|
struct stat st;
|
2003-03-05 05:49:21 +08:00
|
|
|
|
|
|
|
iov.iov_base = (char*) &dummy;
|
|
|
|
iov.iov_len = 1;
|
2003-03-02 15:46:33 +08:00
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_accrights = (char *)fd;
|
|
|
|
msg.msg_accrightslen = sizeof(fd);
|
|
|
|
if( recvmsg( s, &msg, MSG_PEEK) >= 0 && msg.msg_accrightslen == sizeof(int) )
|
|
|
|
{
|
|
|
|
/* We must receive a valid descriptor, it must be a pipe,
|
|
|
|
* and it must only be accessible by its owner.
|
|
|
|
*/
|
|
|
|
dummy = fstat( fd[0], &st );
|
|
|
|
close(fd[0]);
|
|
|
|
if( dummy == 0 && S_ISFIFO(st.st_mode) &&
|
|
|
|
((st.st_mode & (S_IRWXG|S_IRWXO)) == 0))
|
|
|
|
{
|
|
|
|
*euid = st.st_uid;
|
|
|
|
*egid = st.st_gid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2002-12-04 14:17:32 +08:00
|
|
|
#endif
|
2003-03-02 15:46:33 +08:00
|
|
|
#endif /* LDAP_PF_LOCAL */
|
2002-12-04 14:17:32 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-03-02 15:46:33 +08:00
|
|
|
#endif /* HAVE_GETPEEREID */
|