1 /* os-local.c -- platform-specific domain socket code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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"
56 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
58 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
63 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
65 #endif /* LDAP_DEBUG */
68 ldap_pvt_set_errno(int err)
74 ldap_pvt_ndelay_on(LDAP *ld, int fd)
76 oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
77 return ber_pvt_socket_set_nonblock( fd, 1 );
81 ldap_pvt_ndelay_off(LDAP *ld, int fd)
83 oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
84 return ber_pvt_socket_set_nonblock( fd, 0 );
88 ldap_pvt_socket(LDAP *ld)
90 ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
91 oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
96 ldap_pvt_close_socket(LDAP *ld, int s)
98 oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
106 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
109 AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
113 * check the socket for errors after select returned.
116 ldap_pvt_is_socket_ready(LDAP *ld, int s)
118 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
120 #if defined( notyet ) /* && defined( SO_ERROR ) */
123 socklen_t dummy = sizeof(so_errno);
124 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
130 ldap_pvt_set_errno(so_errno);
139 struct sockaddr_un sa;
141 socklen_t dummy = sizeof(sa);
142 if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
145 /* XXX: needs to be replace with ber_stream_read() */
157 #if !defined(HAVE_GETPEEREID) && \
158 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
159 defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
161 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
162 LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
166 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
169 struct timeval tv, *opt_tv=NULL;
171 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
172 tv.tv_usec = opt_tv->tv_usec;
173 tv.tv_sec = opt_tv->tv_sec;
176 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
177 s, opt_tv ? tv.tv_sec : -1L, async);
179 if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
181 if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
184 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
187 /* Send a dummy message with access rights. Remote side will
188 * obtain our uid/gid by fstat'ing this descriptor.
193 if (pipe(fds) == 0) {
194 /* Abandon, noop, has no reply */
196 struct msghdr msg = {0};
197 iov.iov_base = (char *) abandonPDU;
198 iov.iov_len = sizeof abandonPDU;
201 msg.msg_accrights = (char *)fds;
202 msg.msg_accrightslen = sizeof(int);
203 sendmsg( s, &msg, 0 );
212 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
215 if ( async ) return -2;
221 int timeout = INFTIM;
223 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
226 fd.events = POLL_WRITE;
230 rc = poll( &fd, 1, timeout );
231 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
232 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
234 if( rc == AC_SOCKET_ERROR ) return rc;
236 if( fd.revents & POLL_WRITE ) {
237 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
238 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
248 fd_set wfds, *z=NULL;
251 if ( s >= FD_SETSIZE ) {
252 rc = AC_SOCKET_ERROR;
254 ldap_pvt_set_errno( EMFILE );
261 rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
262 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
263 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
265 if( rc == AC_SOCKET_ERROR ) return rc;
267 if ( FD_ISSET(s, &wfds) ) {
268 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
269 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
279 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
280 ldap_pvt_set_errno( ETIMEDOUT );
285 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
287 struct sockaddr_un server;
291 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
293 s = ldap_pvt_socket( ld );
294 if ( s == AC_SOCKET_INVALID ) {
298 if ( path == NULL || path[0] == '\0' ) {
301 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
302 ldap_pvt_set_errno( ENAMETOOLONG );
307 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
309 memset( &server, '\0', sizeof(server) );
310 server.sun_family = AF_LOCAL;
311 strcpy( server.sun_path, path );
313 rc = ldap_pvt_connect(ld, s, &server, async);
316 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
318 ldap_pvt_close_socket(ld, s);
324 #endif /* LDAP_PF_LOCAL */