3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
19 #ifndef HAVE_GETPEEREID
21 #include <sys/types.h>
22 #include <ac/unistd.h>
24 #include <ac/socket.h>
29 #include <grp.h> /* for NGROUPS on Tru64 5.1 */
31 #include <sys/ucred.h>
34 #if !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
35 defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
43 int getpeereid( int s, uid_t *euid, gid_t *egid )
46 #if defined( SO_PEERCRED )
47 struct ucred peercred;
48 size_t peercredlen = sizeof peercred;
50 if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
51 (void *)&peercred, &peercredlen ) == 0 )
52 && ( peercredlen == sizeof peercred ))
59 #elif defined( LOCAL_PEERCRED )
60 struct xucred peercred;
61 socklen_t peercredlen = sizeof peercred;
63 if(( getsockopt( s, LOCAL_PEERCRED, 1,
64 (void *)&peercred, &peercredlen ) == 0 )
65 && ( peercred.cr_version == XUCRED_VERSION ))
67 *euid = peercred.cr_uid;
68 *egid = peercred.cr_gid;
71 #elif defined( DO_SENDMSG )
74 struct msghdr msg = {0};
77 iov.iov_base = (char*) &dummy;
81 msg.msg_accrights = (char *)fd;
82 msg.msg_accrightslen = sizeof(fd);
83 if( recvmsg( s, &msg, MSG_PEEK) >= 0 && msg.msg_accrightslen == sizeof(int) )
85 /* We must receive a valid descriptor, it must be a pipe,
86 * and it must only be accessible by its owner.
88 dummy = fstat( fd[0], &st );
90 if( dummy == 0 && S_ISFIFO(st.st_mode) &&
91 ((st.st_mode & (S_IRWXG|S_IRWXO)) == 0))
99 #endif /* LDAP_PF_LOCAL */
104 #endif /* HAVE_GETPEEREID */