1 /* os-local.c -- platform-specific domain socket code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 /* Portions (C) Copyright PADL Software Pty Ltd. 1999
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that this notice is preserved
22 * and that due credit is given to PADL Software Pty Ltd. This software
23 * is provided ``as is'' without express or implied warranty.
32 #include <ac/stdlib.h>
35 #include <ac/socket.h>
36 #include <ac/string.h>
38 #include <ac/unistd.h>
40 #ifdef HAVE_SYS_STAT_H
49 #endif /* HAVE_IO_H */
52 #include "ldap_defaults.h"
54 /* int ldap_int_tblsize = 0; */
58 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
60 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
65 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
67 #endif /* LDAP_DEBUG */
70 ldap_pvt_set_errno(int err)
76 ldap_pvt_ndelay_on(LDAP *ld, int fd)
78 oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
79 return ber_pvt_socket_set_nonblock( fd, 1 );
83 ldap_pvt_ndelay_off(LDAP *ld, int fd)
85 oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
86 return ber_pvt_socket_set_nonblock( fd, 0 );
90 ldap_pvt_socket(LDAP *ld)
92 ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
93 oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
98 ldap_pvt_close_socket(LDAP *ld, int s)
100 oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
108 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
111 AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
115 * check the socket for errors after select returned.
118 ldap_pvt_is_socket_ready(LDAP *ld, int s)
120 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
122 #if defined( notyet ) /* && defined( SO_ERROR ) */
125 socklen_t dummy = sizeof(so_errno);
126 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
132 ldap_pvt_set_errno(so_errno);
141 struct sockaddr_un sa;
143 socklen_t dummy = sizeof(sa);
144 if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
147 /* XXX: needs to be replace with ber_stream_read() */
159 #if !defined(HAVE_GETPEEREID) && \
160 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
161 defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
163 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
164 LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
168 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
171 struct timeval tv, *opt_tv=NULL;
172 fd_set wfds, *z=NULL;
174 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
175 tv.tv_usec = opt_tv->tv_usec;
176 tv.tv_sec = opt_tv->tv_sec;
179 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
180 s, opt_tv ? tv.tv_sec : -1L, async);
182 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
185 if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
188 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
192 /* Send a dummy message with access rights. Remote side will
193 * obtain our uid/gid by fstat'ing this descriptor.
198 if (pipe(fds) == 0) {
199 /* Abandon, noop, has no reply */
201 struct msghdr msg = {0};
202 iov.iov_base = (char *) abandonPDU;
203 iov.iov_len = sizeof abandonPDU;
206 msg.msg_accrights = (char *)fds;
207 msg.msg_accrightslen = sizeof(int);
208 sendmsg( s, &msg, 0 );
217 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
222 if ( async ) return ( -2 );
229 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
230 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
231 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
233 if( rc == AC_SOCKET_ERROR ) return rc;
235 if ( FD_ISSET(s, &wfds) ) {
236 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
238 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
246 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
247 ldap_pvt_set_errno( ETIMEDOUT );
252 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
254 struct sockaddr_un server;
258 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
260 s = ldap_pvt_socket( ld );
261 if ( s == AC_SOCKET_INVALID ) {
265 if ( path == NULL || path[0] == '\0' ) {
268 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
269 ldap_pvt_set_errno( ENAMETOOLONG );
274 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
276 memset( &server, '\0', sizeof(server) );
277 server.sun_family = AF_LOCAL;
278 strcpy( server.sun_path, path );
280 rc = ldap_pvt_connect(ld, s, &server, async);
283 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
285 ldap_pvt_close_socket(ld, s);
291 #endif /* LDAP_PF_LOCAL */