2000-01-02 09:21:25 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
2000-05-13 10:36:07 +08:00
|
|
|
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
|
2000-01-02 09:21:25 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
|
|
|
* Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 1999 PADL Software Pty Ltd.
|
|
|
|
* os-ip.c -- platform-specific domain socket code
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
/* XXX non-portable */
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif /* HAVE_IO_H */
|
|
|
|
|
|
|
|
#include "ldap-int.h"
|
2000-06-25 08:35:17 +08:00
|
|
|
#include "ldap_defaults.h"
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
/* int ldap_int_tblsize = 0; */
|
|
|
|
|
|
|
|
#define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
|
|
|
|
do { \
|
|
|
|
ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
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);
|
|
|
|
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 { \
|
|
|
|
oslocal_debug(ld, \
|
|
|
|
"ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
|
|
|
|
s, \
|
|
|
|
errno, \
|
2000-07-06 09:24:52 +08:00
|
|
|
STRERROR(errno) ); \
|
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;
|
|
|
|
int dummy = sizeof(so_errno);
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
|
|
|
|
== SOCKET_ERROR )
|
|
|
|
{
|
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;
|
2000-07-05 01:57:21 +08:00
|
|
|
int dummy = sizeof(sa);
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
|
|
|
|
== SOCKET_ERROR )
|
|
|
|
{
|
2000-01-02 09:21:25 +08:00
|
|
|
/* XXX: needs to be replace with ber_stream_read() */
|
|
|
|
read(s, &ch, 1);
|
|
|
|
TRACE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#undef TRACE
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
struct timeval tv, *opt_tv=NULL;
|
|
|
|
fd_set wfds, *z=NULL;
|
|
|
|
|
|
|
|
if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
|
|
|
|
tv.tv_usec = opt_tv->tv_usec;
|
|
|
|
tv.tv_sec = opt_tv->tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
|
|
|
|
s, opt_tv ? tv.tv_sec : -1L, async);
|
|
|
|
|
|
|
|
if ( ldap_pvt_ndelay_on(ld, s) == -1 )
|
|
|
|
return ( -1 );
|
|
|
|
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
|
|
|
|
!= SOCKET_ERROR )
|
2000-01-02 09:21:25 +08:00
|
|
|
{
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
|
2000-01-02 09:21:25 +08:00
|
|
|
return ( -1 );
|
2000-10-18 08:27:31 +08:00
|
|
|
}
|
2000-01-02 09:21:25 +08:00
|
|
|
return ( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
|
|
|
|
return ( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef notyet
|
|
|
|
if ( async ) return ( -2 );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(s, &wfds );
|
|
|
|
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL)
|
|
|
|
== SOCKET_ERROR )
|
|
|
|
{
|
2000-01-02 09:21:25 +08:00
|
|
|
return ( -1 );
|
2000-10-18 08:27:31 +08:00
|
|
|
}
|
2000-01-02 09:21:25 +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 );
|
|
|
|
return ( 0 );
|
|
|
|
}
|
|
|
|
oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
|
|
|
|
ldap_pvt_set_errno( ETIMEDOUT );
|
|
|
|
return ( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
|
|
|
|
{
|
|
|
|
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;
|
2000-01-02 09:21:25 +08:00
|
|
|
|
|
|
|
oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
|
|
|
|
|
2000-10-18 08:27:31 +08:00
|
|
|
s = ldap_pvt_socket( ld );
|
|
|
|
if ( s == AC_SOCKET_INVALID ) {
|
2000-01-02 09:21:25 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
|
2000-01-02 09:21:25 +08:00
|
|
|
} else {
|
|
|
|
ldap_pvt_close_socket(ld, s);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int dummy;
|
2000-07-10 05:49:36 +08:00
|
|
|
#endif /* LDAP_PF_LOCAL */
|