2003-11-26 15:16:36 +08:00
|
|
|
/* os-local.c -- platform-specific domain socket code */
|
2000-01-02 09:21:25 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2012-01-01 23:07:45 +08:00
|
|
|
* Copyright 1998-2012 The OpenLDAP Foundation.
|
2003-11-26 15:16:36 +08:00
|
|
|
* All rights reserved.
|
2003-05-24 17:30:48 +08:00
|
|
|
*
|
2003-11-26 15:16:36 +08:00
|
|
|
* 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>.
|
|
|
|
*/
|
|
|
|
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
2003-05-24 17:30:48 +08:00
|
|
|
*/
|
2003-11-26 15:16:36 +08:00
|
|
|
/* Portions (C) Copyright PADL Software Pty Ltd. 1999
|
2003-05-25 12:48:49 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that this notice is preserved
|
|
|
|
* and that due credit is given to PADL Software Pty Ltd. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
2000-01-02 09:21:25 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
2000-07-10 05:49:36 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
2003-03-03 05:21:36 +08:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2000-01-02 09:21:25 +08:00
|
|
|
#include <sys/stat.h>
|
2003-03-03 05:21:36 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif /* HAVE_IO_H */
|
2008-05-16 04:56:41 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
#include "ldap-int.h"
|
2000-06-25 08:35:17 +08:00
|
|
|
#include "ldap_defaults.h"
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2003-06-06 21:56:18 +08:00
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
#define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
|
|
|
|
do { \
|
|
|
|
ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
|
|
|
|
} while(0)
|
|
|
|
|
2003-06-06 21:56:18 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
|
|
|
|
|
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
static void
|
|
|
|
ldap_pvt_set_errno(int err)
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldap_pvt_ndelay_on(LDAP *ld, int fd)
|
|
|
|
{
|
|
|
|
oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
|
|
|
|
return ber_pvt_socket_set_nonblock( fd, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldap_pvt_ndelay_off(LDAP *ld, int fd)
|
|
|
|
{
|
|
|
|
oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
|
|
|
|
return ber_pvt_socket_set_nonblock( fd, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static ber_socket_t
|
|
|
|
ldap_pvt_socket(LDAP *ld)
|
|
|
|
{
|
2000-07-10 05:49:36 +08:00
|
|
|
ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
|
2000-01-02 09:21:25 +08:00
|
|
|
oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
|
2008-05-16 04:56:41 +08:00
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
fcntl(s, F_SETFD, FD_CLOEXEC);
|
|
|
|
#endif
|
2000-01-02 09:21:25 +08:00
|
|
|
return ( s );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldap_pvt_close_socket(LDAP *ld, int s)
|
|
|
|
{
|
|
|
|
oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
|
|
|
|
return tcp_close(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TRACE
|
|
|
|
#define TRACE do { \
|
2004-04-25 09:09:40 +08:00
|
|
|
char ebuf[128]; \
|
2000-01-02 09:21:25 +08:00
|
|
|
oslocal_debug(ld, \
|
|
|
|
"ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
|
|
|
|
s, \
|
|
|
|
errno, \
|
2004-04-25 09:09:40 +08:00
|
|
|
AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
|
2000-01-02 09:21:25 +08:00
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check the socket for errors after select returned.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ldap_pvt_is_socket_ready(LDAP *ld, int s)
|
|
|
|
{
|
|
|
|
oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
|
|
|
|
|
|
|
|
#if defined( notyet ) /* && defined( SO_ERROR ) */
|
|
|
|
{
|
|
|
|
int so_errno;
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t dummy = sizeof(so_errno);
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
|
2000-10-18 11:24:50 +08:00
|
|
|
== AC_SOCKET_ERROR )
|
2000-10-18 08:27:31 +08:00
|
|
|
{
|
2000-01-02 09:21:25 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( so_errno ) {
|
|
|
|
ldap_pvt_set_errno(so_errno);
|
|
|
|
TRACE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
/* error slippery */
|
2000-07-05 01:57:21 +08:00
|
|
|
struct sockaddr_un sa;
|
2000-01-02 09:21:25 +08:00
|
|
|
char ch;
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t dummy = sizeof(sa);
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
|
2000-10-18 11:24:50 +08:00
|
|
|
== AC_SOCKET_ERROR )
|
2000-10-18 08:27:31 +08:00
|
|
|
{
|
2000-01-02 09:21:25 +08:00
|
|
|
/* XXX: needs to be replace with ber_stream_read() */
|
2009-02-16 05:59:16 +08:00
|
|
|
(void)read(s, &ch, 1);
|
2000-01-02 09:21:25 +08:00
|
|
|
TRACE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#undef TRACE
|
|
|
|
|
2007-03-25 12:40:22 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL_SENDMSG
|
2003-03-05 05:45:59 +08:00
|
|
|
static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
|
|
|
|
LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
|
2003-03-02 15:46:33 +08:00
|
|
|
#endif
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
static int
|
2000-07-05 01:57:21 +08:00
|
|
|
ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
2002-07-28 13:30:39 +08:00
|
|
|
int rc;
|
2007-02-06 04:32:44 +08:00
|
|
|
struct timeval tv, *opt_tv = NULL;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2007-02-06 04:32:44 +08:00
|
|
|
if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
|
|
|
|
tv = ld->ld_options.ldo_tm_net;
|
|
|
|
opt_tv = &tv;
|
2000-01-02 09:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
|
2004-07-10 01:50:22 +08:00
|
|
|
s, opt_tv ? tv.tv_sec : -1L, async);
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
|
2000-10-18 11:24:50 +08:00
|
|
|
!= AC_SOCKET_ERROR )
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
2004-07-10 01:50:22 +08:00
|
|
|
if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
|
|
|
|
|
2007-03-25 12:40:22 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL_SENDMSG
|
2003-03-02 15:46:33 +08:00
|
|
|
/* Send a dummy message with access rights. Remote side will
|
2007-03-26 20:44:11 +08:00
|
|
|
* obtain our uid/gid by fstat'ing this descriptor. The
|
|
|
|
* descriptor permissions must match exactly, and we also
|
|
|
|
* send the socket name, which must also match.
|
2003-03-02 15:46:33 +08:00
|
|
|
*/
|
2003-03-03 05:21:36 +08:00
|
|
|
sendcred:
|
|
|
|
{
|
2007-03-26 20:44:11 +08:00
|
|
|
int fds[2];
|
2007-06-10 07:35:20 +08:00
|
|
|
ber_socklen_t salen = sizeof(*sa);
|
2007-03-26 20:44:11 +08:00
|
|
|
if (pipe(fds) == 0) {
|
|
|
|
/* Abandon, noop, has no reply */
|
|
|
|
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
|
2007-03-26 20:44:11 +08:00
|
|
|
union {
|
|
|
|
struct cmsghdr cm;
|
|
|
|
unsigned char control[CMSG_SPACE(sizeof(int))];
|
|
|
|
} control_un;
|
|
|
|
struct cmsghdr *cmsg;
|
2005-08-13 10:17:12 +08:00
|
|
|
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
|
2007-03-26 20:44:11 +08:00
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
iov.iov_base = (char *) abandonPDU;
|
|
|
|
iov.iov_len = sizeof abandonPDU;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
2005-08-13 10:17:12 +08:00
|
|
|
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
|
2007-03-26 20:44:11 +08:00
|
|
|
msg.msg_control = control_un.control;
|
|
|
|
msg.msg_controllen = sizeof( control_un.control );
|
|
|
|
msg.msg_flags = 0;
|
2005-03-16 10:22:46 +08:00
|
|
|
|
2007-03-26 20:44:11 +08:00
|
|
|
cmsg = CMSG_FIRSTHDR( &msg );
|
|
|
|
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
|
|
|
|
2007-03-26 20:44:11 +08:00
|
|
|
*((int *)CMSG_DATA(cmsg)) = fds[0];
|
2005-03-16 10:22:46 +08:00
|
|
|
# else
|
2007-03-26 20:44:11 +08:00
|
|
|
msg.msg_accrights = (char *)fds;
|
|
|
|
msg.msg_accrightslen = sizeof(int);
|
2005-08-13 10:17:12 +08:00
|
|
|
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
|
2009-06-12 00:21:52 +08:00
|
|
|
getpeername( s, (struct sockaddr *) sa, &salen );
|
2007-03-26 20:44:11 +08:00
|
|
|
fchmod( fds[0], S_ISUID|S_IRWXU );
|
|
|
|
write( fds[1], sa, salen );
|
|
|
|
sendmsg( s, &msg, 0 );
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
}
|
|
|
|
}
|
2007-03-25 23:20:50 +08:00
|
|
|
#endif
|
2007-03-26 20:44:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
#ifdef notyet
|
2004-07-10 01:50:22 +08:00
|
|
|
if ( async ) return -2;
|
2000-01-02 09:21:25 +08:00
|
|
|
#endif
|
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
#ifdef HAVE_POLL
|
2004-07-10 03:29:46 +08:00
|
|
|
{
|
|
|
|
struct pollfd fd;
|
|
|
|
int timeout = INFTIM;
|
|
|
|
|
|
|
|
if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
|
|
|
|
|
|
|
|
fd.fd = s;
|
2004-10-19 06:08:37 +08:00
|
|
|
fd.events = POLL_WRITE;
|
2004-07-10 03:29:46 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
fd.revents = 0;
|
|
|
|
rc = poll( &fd, 1, timeout );
|
|
|
|
} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
|
|
|
|
LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
|
|
|
|
|
|
|
|
if( rc == AC_SOCKET_ERROR ) return rc;
|
|
|
|
|
2004-10-19 06:08:37 +08:00
|
|
|
if( fd.revents & POLL_WRITE ) {
|
2004-07-10 03:29:46 +08:00
|
|
|
if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
|
|
|
|
if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
|
2007-03-25 12:40:22 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL_SENDMSG
|
2004-07-10 03:29:46 +08:00
|
|
|
goto sendcred;
|
|
|
|
#else
|
|
|
|
return ( 0 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2004-07-10 01:50:22 +08:00
|
|
|
#else
|
|
|
|
{
|
2004-07-10 03:29:46 +08:00
|
|
|
fd_set wfds, *z=NULL;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2004-11-18 04:04:18 +08:00
|
|
|
#ifdef FD_SETSIZE
|
|
|
|
if ( s >= FD_SETSIZE ) {
|
|
|
|
rc = AC_SOCKET_ERROR;
|
|
|
|
tcp_close( s );
|
|
|
|
ldap_pvt_set_errno( EMFILE );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
2004-07-10 01:50:22 +08:00
|
|
|
do {
|
2004-07-10 03:29:46 +08:00
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(s, &wfds );
|
2004-07-10 01:50:22 +08:00
|
|
|
rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
|
|
|
|
} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
|
|
|
|
LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
|
2002-07-28 13:30:39 +08:00
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
if( rc == AC_SOCKET_ERROR ) return rc;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
if ( FD_ISSET(s, &wfds) ) {
|
|
|
|
if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
|
|
|
|
if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
|
2007-03-25 12:40:22 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL_SENDMSG
|
2004-07-10 01:50:22 +08:00
|
|
|
goto sendcred;
|
2002-12-05 00:03:02 +08:00
|
|
|
#else
|
2004-07-10 01:50:22 +08:00
|
|
|
return ( 0 );
|
2002-12-05 00:03:02 +08:00
|
|
|
#endif
|
2004-07-10 01:50:22 +08:00
|
|
|
}
|
2000-01-02 09:21:25 +08:00
|
|
|
}
|
2004-07-10 01:50:22 +08:00
|
|
|
#endif
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
|
|
|
|
ldap_pvt_set_errno( ETIMEDOUT );
|
|
|
|
return ( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-08-16 06:53:47 +08:00
|
|
|
ldap_connect_to_path(LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv, int async)
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_un server;
|
2000-10-18 08:27:31 +08:00
|
|
|
ber_socket_t s;
|
2000-09-15 11:04:46 +08:00
|
|
|
int rc;
|
2008-08-16 06:53:47 +08:00
|
|
|
const char *path = srv->lud_host;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
|
|
|
|
|
|
|
|
if ( path == NULL || path[0] == '\0' ) {
|
2000-06-25 08:35:17 +08:00
|
|
|
path = LDAPI_SOCK;
|
2000-01-02 09:21:25 +08:00
|
|
|
} else {
|
|
|
|
if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
|
|
|
|
ldap_pvt_set_errno( ENAMETOOLONG );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 05:00:24 +08:00
|
|
|
s = ldap_pvt_socket( ld );
|
|
|
|
if ( s == AC_SOCKET_INVALID ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-01-02 09:21:25 +08:00
|
|
|
oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
|
|
|
|
|
2000-06-11 06:39:30 +08:00
|
|
|
memset( &server, '\0', sizeof(server) );
|
2000-07-10 05:49:36 +08:00
|
|
|
server.sun_family = AF_LOCAL;
|
2000-01-02 09:21:25 +08:00
|
|
|
strcpy( server.sun_path, path );
|
|
|
|
|
|
|
|
rc = ldap_pvt_connect(ld, s, &server, async);
|
|
|
|
|
|
|
|
if (rc == 0) {
|
2011-04-21 05:00:24 +08:00
|
|
|
rc = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&server );
|
2008-08-15 18:23:29 +08:00
|
|
|
}
|
|
|
|
if ( rc ) {
|
2000-01-02 09:21:25 +08:00
|
|
|
ldap_pvt_close_socket(ld, s);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int dummy;
|
2000-07-10 05:49:36 +08:00
|
|
|
#endif /* LDAP_PF_LOCAL */
|