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/>.
|
|
|
|
*
|
2021-01-12 03:25:53 +08:00
|
|
|
* Copyright 2000-2021 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
|
|
|
*/
|
|
|
|
|
2009-04-17 02:02:01 +08:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE 1 /* Needed for glibc struct ucred */
|
|
|
|
#endif
|
2009-04-15 15:36:56 +08:00
|
|
|
|
2002-12-04 14:17:32 +08:00
|
|
|
#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
|
|
|
|
|
2007-03-25 12:40:22 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL_SENDMSG
|
|
|
|
#include <lber.h>
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-24 02:55:31 +08:00
|
|
|
#ifdef HAVE_SYS_UCRED_H
|
|
|
|
#ifdef HAVE_GRP_H
|
2003-05-06 01:29:26 +08:00
|
|
|
#include <grp.h> /* for NGROUPS on Tru64 5.1 */
|
|
|
|
#endif
|
2002-12-04 14:17:32 +08:00
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
|
|
|
|
2005-02-03 17:36:25 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-03-25 12:40:22 +08:00
|
|
|
int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
|
|
|
|
#ifdef LDAP_PF_LOCAL_SENDMSG
|
|
|
|
, struct berval *peerbv
|
|
|
|
#endif
|
|
|
|
)
|
2002-12-04 14:17:32 +08:00
|
|
|
{
|
|
|
|
#ifdef LDAP_PF_LOCAL
|
2005-11-24 03:40:08 +08:00
|
|
|
#if defined( HAVE_GETPEERUCRED )
|
2005-11-24 02:55:31 +08:00
|
|
|
ucred_t *uc = NULL;
|
|
|
|
if( getpeerucred( s, &uc ) == 0 ) {
|
|
|
|
*euid = ucred_geteuid( uc );
|
|
|
|
*egid = ucred_getegid( uc );
|
|
|
|
ucred_free( uc );
|
2008-07-16 04:07:36 +08:00
|
|
|
return 0;
|
2005-11-24 02:55:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined( SO_PEERCRED )
|
2002-12-04 14:17:32 +08:00
|
|
|
struct ucred peercred;
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t peercredlen = sizeof peercred;
|
2002-12-04 14:17:32 +08:00
|
|
|
|
|
|
|
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;
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t peercredlen = sizeof peercred;
|
2002-12-04 14:17:32 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2007-06-10 07:43:02 +08:00
|
|
|
#elif defined( LDAP_PF_LOCAL_SENDMSG ) && defined( MSG_WAITALL )
|
2007-03-25 12:40:22 +08:00
|
|
|
int err, fd;
|
2003-03-05 05:49:21 +08:00
|
|
|
struct iovec iov;
|
2003-03-02 15:46:33 +08:00
|
|
|
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
|
2007-03-25 23:20:50 +08:00
|
|
|
struct {
|
2005-03-16 10:22:46 +08:00
|
|
|
struct cmsghdr cm;
|
2007-03-25 23:20:50 +08:00
|
|
|
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 */
|
2003-03-02 15:46:33 +08:00
|
|
|
struct stat st;
|
2007-03-25 23:20:50 +08:00
|
|
|
struct sockaddr_un lname, rname;
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t llen, rlen;
|
2007-03-25 23:20:50 +08:00
|
|
|
|
|
|
|
rlen = sizeof(rname);
|
|
|
|
llen = sizeof(lname);
|
2007-03-26 20:44:11 +08:00
|
|
|
memset( &lname, 0, sizeof( lname ));
|
2007-03-25 23:20:50 +08:00
|
|
|
getsockname(s, (struct sockaddr *)&lname, &llen);
|
2003-03-05 05:49:21 +08:00
|
|
|
|
2007-03-25 12:40:22 +08:00
|
|
|
iov.iov_base = peerbv->bv_val;
|
|
|
|
iov.iov_len = peerbv->bv_len;
|
2003-03-02 15:46:33 +08:00
|
|
|
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
|
2007-03-25 23:20:50 +08:00
|
|
|
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 );
|
2007-03-25 12:40:22 +08:00
|
|
|
# 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 &&
|
2007-03-25 12:40:22 +08:00
|
|
|
# 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 &&
|
2007-03-25 12:40:22 +08:00
|
|
|
cmsg->cmsg_type == SCM_RIGHTS
|
2005-03-16 10:22:46 +08:00
|
|
|
# else
|
2007-03-25 12:40:22 +08:00
|
|
|
msg.msg_accrightslen == sizeof(int)
|
2005-08-13 10:17:12 +08:00
|
|
|
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL*/
|
2007-03-25 12:40:22 +08:00
|
|
|
) {
|
2007-03-26 20:44:11 +08:00
|
|
|
int mode = S_IFIFO|S_ISUID|S_IRWXU;
|
2007-03-25 23:20:50 +08:00
|
|
|
|
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.
|
2003-03-02 15:46:33 +08:00
|
|
|
*/
|
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
|
2007-03-25 12:40:22 +08:00
|
|
|
fd = (*(int *)CMSG_DATA( cmsg ));
|
2005-03-16 10:22:46 +08:00
|
|
|
# endif
|
2007-03-25 12:40:22 +08:00
|
|
|
err = fstat( fd, &st );
|
2007-03-25 23:20:50 +08:00
|
|
|
if ( err == 0 )
|
2007-03-26 20:44:11 +08:00
|
|
|
rlen = read(fd, &rname, rlen);
|
2007-03-25 12:40:22 +08:00
|
|
|
close(fd);
|
2007-03-25 23:20:50 +08:00
|
|
|
if( err == 0 && st.st_mode == mode &&
|
|
|
|
llen == rlen && !memcmp(&lname, &rname, llen))
|
2003-03-02 15:46:33 +08:00
|
|
|
{
|
|
|
|
*euid = st.st_uid;
|
|
|
|
*egid = st.st_gid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-02-03 17:36:25 +08:00
|
|
|
#elif defined(SOCKCREDSIZE)
|
2005-03-15 11:11:24 +08:00
|
|
|
struct msghdr msg;
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t crmsgsize;
|
2005-03-15 11:11:24 +08:00
|
|
|
void *crmsg;
|
|
|
|
struct cmsghdr *cmp;
|
|
|
|
struct sockcred *sc;
|
2005-02-03 17:36:25 +08:00
|
|
|
|
|
|
|
memset(&msg, 0, sizeof msg);
|
|
|
|
crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
|
2005-03-15 11:11:24 +08:00
|
|
|
if (crmsgsize == 0) goto sc_err;
|
2005-02-03 17:36:25 +08:00
|
|
|
crmsg = malloc(crmsgsize);
|
2005-03-15 11:11:24 +08:00
|
|
|
if (crmsg == NULL) goto sc_err;
|
2005-02-03 17:36:25 +08:00
|
|
|
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;
|
2005-02-03 17:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) {
|
2005-03-15 11:11:24 +08:00
|
|
|
free(crmsg);
|
|
|
|
goto sc_err;
|
2005-02-03 17:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2005-02-03 17:36:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
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 */
|