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);
107 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
114 * check the socket for errors after select returned.
117 ldap_pvt_is_socket_ready(LDAP *ld, int s)
119 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
121 #if defined( notyet ) /* && defined( SO_ERROR ) */
124 socklen_t dummy = sizeof(so_errno);
125 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
131 ldap_pvt_set_errno(so_errno);
140 struct sockaddr_un sa;
142 socklen_t dummy = sizeof(sa);
143 if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
146 /* XXX: needs to be replace with ber_stream_read() */
158 #if !defined(HAVE_GETPEEREID) && \
159 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
160 defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
162 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
163 LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
167 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
170 struct timeval tv, *opt_tv=NULL;
171 fd_set wfds, *z=NULL;
173 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
174 tv.tv_usec = opt_tv->tv_usec;
175 tv.tv_sec = opt_tv->tv_sec;
178 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
179 s, opt_tv ? tv.tv_sec : -1L, async);
181 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
184 if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
187 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
191 /* Send a dummy message with access rights. Remote side will
192 * obtain our uid/gid by fstat'ing this descriptor.
197 if (pipe(fds) == 0) {
198 /* Abandon, noop, has no reply */
200 struct msghdr msg = {0};
201 iov.iov_base = (char *) abandonPDU;
202 iov.iov_len = sizeof abandonPDU;
205 msg.msg_accrights = (char *)fds;
206 msg.msg_accrightslen = sizeof(int);
207 sendmsg( s, &msg, 0 );
216 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
221 if ( async ) return ( -2 );
228 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
229 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
230 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
232 if( rc == AC_SOCKET_ERROR ) return rc;
234 if ( FD_ISSET(s, &wfds) ) {
235 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
237 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
245 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
246 ldap_pvt_set_errno( ETIMEDOUT );
251 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
253 struct sockaddr_un server;
257 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
259 s = ldap_pvt_socket( ld );
260 if ( s == AC_SOCKET_INVALID ) {
264 if ( path == NULL || path[0] == '\0' ) {
267 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
268 ldap_pvt_set_errno( ENAMETOOLONG );
273 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
275 memset( &server, '\0', sizeof(server) );
276 server.sun_family = AF_LOCAL;
277 strcpy( server.sun_path, path );
279 rc = ldap_pvt_connect(ld, s, &server, async);
282 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
284 ldap_pvt_close_socket(ld, s);
290 #endif /* LDAP_PF_LOCAL */