openldap/libraries/liblutil/getpeereid.c

216 lines
4.8 KiB
C
Raw Normal View History

/* getpeereid.c */
/* $OpenLDAP$ */
2003-11-26 07:17:08 +08:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2007-01-03 04:00:42 +08:00
* Copyright 2000-2007 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>.
*/
#include "portable.h"
#ifndef HAVE_GETPEEREID
#include <sys/types.h>
#include <ac/unistd.h>
2007-03-25 20:18:06 +08:00
#include <ac/socket.h>
#include <ac/errno.h>
#ifdef HAVE_GETPEERUCRED
#include <ucred.h>
#endif
#ifdef LDAP_PF_LOCAL_SENDMSG
#include <lber.h>
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_UCRED_H
#ifdef HAVE_GRP_H
#include <grp.h> /* for NGROUPS on Tru64 5.1 */
#endif
#include <sys/ucred.h>
#endif
#include <stdlib.h>
int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
#ifdef LDAP_PF_LOCAL_SENDMSG
, struct berval *peerbv
#endif
)
{
#ifdef LDAP_PF_LOCAL
2005-11-24 03:40:08 +08:00
#if defined( HAVE_GETPEERUCRED )
ucred_t *uc = NULL;
if( getpeerucred( s, &uc ) == 0 ) {
*euid = ucred_geteuid( uc );
*egid = ucred_getegid( uc );
ucred_free( uc );
}
#elif defined( SO_PEERCRED )
struct ucred peercred;
socklen_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;
}
#elif defined( LDAP_PF_LOCAL_SENDMSG )
int err, fd;
2003-03-05 05:49:21 +08:00
struct iovec iov;
struct msghdr msg = {0};
2005-08-13 10:17:12 +08:00
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
2005-03-16 10:22:46 +08:00
# ifndef CMSG_SPACE
# define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
# endif
# ifndef CMSG_LEN
# define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
# endif
struct {
2005-03-16 10:22:46 +08:00
struct cmsghdr cm;
int fd;
} control_st;
2005-03-16 10:22:46 +08:00
struct cmsghdr *cmsg;
2005-08-13 10:17:12 +08:00
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
struct stat st;
struct sockaddr_un lname, rname;
socklen_t llen, rlen;
rlen = sizeof(rname);
llen = sizeof(lname);
2007-03-26 20:44:11 +08:00
memset( &lname, 0, sizeof( lname ));
getsockname(s, (struct sockaddr *)&lname, &llen);
2003-03-05 05:49:21 +08:00
iov.iov_base = peerbv->bv_val;
iov.iov_len = peerbv->bv_len;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
2007-03-26 20:44:11 +08:00
peerbv->bv_len = 0;
2005-08-13 10:17:12 +08:00
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
msg.msg_control = &control_st;
msg.msg_controllen = sizeof( struct cmsghdr ) + sizeof( int ); /* no padding! */
2005-03-16 10:22:46 +08:00
cmsg = CMSG_FIRSTHDR( &msg );
# else
msg.msg_accrights = (char *)&fd;
msg.msg_accrightslen = sizeof(fd);
# endif
2005-03-16 10:22:46 +08:00
/*
* AIX returns a bogus file descriptor if recvmsg() is
* called with MSG_PEEK (is this a bug?). Hence we need
* to receive the Abandon PDU.
*/
2007-03-26 20:44:11 +08:00
err = recvmsg( s, &msg, MSG_WAITALL );
if( err >= 0 &&
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
2005-03-16 10:22:46 +08:00
cmsg->cmsg_len == CMSG_LEN( sizeof(int) ) &&
cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS
2005-03-16 10:22:46 +08:00
# else
msg.msg_accrightslen == sizeof(int)
2005-08-13 10:17:12 +08:00
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL*/
) {
2007-03-26 20:44:11 +08:00
int mode = S_IFIFO|S_ISUID|S_IRWXU;
2007-03-26 20:44:11 +08:00
/* We must receive a valid descriptor, it must be a pipe,
* it must only be accessible by its owner, and it must
* have the name of our socket written on it.
*/
2007-03-26 20:44:11 +08:00
peerbv->bv_len = err;
2005-08-13 10:17:12 +08:00
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
fd = (*(int *)CMSG_DATA( cmsg ));
2005-03-16 10:22:46 +08:00
# endif
err = fstat( fd, &st );
if ( err == 0 )
2007-03-26 20:44:11 +08:00
rlen = read(fd, &rname, rlen);
close(fd);
if( err == 0 && st.st_mode == mode &&
llen == rlen && !memcmp(&lname, &rname, llen))
{
*euid = st.st_uid;
*egid = st.st_gid;
return 0;
}
}
#elif defined(SOCKCREDSIZE)
2005-03-15 11:11:24 +08:00
struct msghdr msg;
socklen_t crmsgsize;
void *crmsg;
struct cmsghdr *cmp;
struct sockcred *sc;
memset(&msg, 0, sizeof msg);
crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
2005-03-15 11:11:24 +08:00
if (crmsgsize == 0) goto sc_err;
crmsg = malloc(crmsgsize);
2005-03-15 11:11:24 +08:00
if (crmsg == NULL) goto sc_err;
memset(crmsg, 0, crmsgsize);
msg.msg_control = crmsg;
msg.msg_controllen = crmsgsize;
if (recvmsg(s, &msg, 0) < 0) {
2005-03-15 11:11:24 +08:00
free(crmsg);
goto sc_err;
}
if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) {
2005-03-15 11:11:24 +08:00
free(crmsg);
goto sc_err;
}
cmp = CMSG_FIRSTHDR(&msg);
if (cmp->cmsg_level != SOL_SOCKET || cmp->cmsg_type != SCM_CREDS) {
2005-03-15 11:11:24 +08:00
printf("nocreds\n");
goto sc_err;
}
sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
*euid = sc->sc_euid;
*egid = sc->sc_egid;
free(crmsg);
return 0;
2005-03-15 11:11:24 +08:00
sc_err:
#endif
#endif /* LDAP_PF_LOCAL */
return -1;
}
#endif /* HAVE_GETPEEREID */