3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * os-local.c -- platform-specific domain socket code
13 * Portions (C) Copyright PADL Software Pty Ltd. 1999
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that this notice is preserved
16 * and that due credit is given to PADL Software Pty Ltd. This software
17 * is provided ``as is'' without express or implied warranty.
26 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
32 #include <ac/unistd.h>
34 #ifdef HAVE_SYS_STAT_H
43 #endif /* HAVE_IO_H */
46 #include "ldap_defaults.h"
48 /* int ldap_int_tblsize = 0; */
52 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
54 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
59 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
61 #endif /* LDAP_DEBUG */
64 ldap_pvt_set_errno(int err)
70 ldap_pvt_ndelay_on(LDAP *ld, int fd)
72 oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
73 return ber_pvt_socket_set_nonblock( fd, 1 );
77 ldap_pvt_ndelay_off(LDAP *ld, int fd)
79 oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
80 return ber_pvt_socket_set_nonblock( fd, 0 );
84 ldap_pvt_socket(LDAP *ld)
86 ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
87 oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
92 ldap_pvt_close_socket(LDAP *ld, int s)
94 oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
101 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
108 * check the socket for errors after select returned.
111 ldap_pvt_is_socket_ready(LDAP *ld, int s)
113 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
115 #if defined( notyet ) /* && defined( SO_ERROR ) */
118 socklen_t dummy = sizeof(so_errno);
119 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
125 ldap_pvt_set_errno(so_errno);
134 struct sockaddr_un sa;
136 socklen_t dummy = sizeof(sa);
137 if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
140 /* XXX: needs to be replace with ber_stream_read() */
152 #if !defined(HAVE_GETPEEREID) && \
153 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
154 defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
156 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
157 LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
161 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
164 struct timeval tv, *opt_tv=NULL;
165 fd_set wfds, *z=NULL;
167 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
168 tv.tv_usec = opt_tv->tv_usec;
169 tv.tv_sec = opt_tv->tv_sec;
172 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
173 s, opt_tv ? tv.tv_sec : -1L, async);
175 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
178 if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
181 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
185 /* Send a dummy message with access rights. Remote side will
186 * obtain our uid/gid by fstat'ing this descriptor.
191 if (pipe(fds) == 0) {
192 /* Abandon, noop, has no reply */
194 struct msghdr msg = {0};
195 iov.iov_base = (char *) abandonPDU;
196 iov.iov_len = sizeof abandonPDU;
199 msg.msg_accrights = (char *)fds;
200 msg.msg_accrightslen = sizeof(int);
201 sendmsg( s, &msg, 0 );
210 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
215 if ( async ) return ( -2 );
222 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
223 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
224 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
226 if( rc == AC_SOCKET_ERROR ) return rc;
228 if ( FD_ISSET(s, &wfds) ) {
229 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
231 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
239 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
240 ldap_pvt_set_errno( ETIMEDOUT );
245 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
247 struct sockaddr_un server;
251 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
253 s = ldap_pvt_socket( ld );
254 if ( s == AC_SOCKET_INVALID ) {
258 if ( path == NULL || path[0] == '\0' ) {
261 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
262 ldap_pvt_set_errno( ENAMETOOLONG );
267 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
269 memset( &server, '\0', sizeof(server) );
270 server.sun_family = AF_LOCAL;
271 strcpy( server.sun_path, path );
273 rc = ldap_pvt_connect(ld, s, &server, async);
276 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
278 ldap_pvt_close_socket(ld, s);
284 #endif /* LDAP_PF_LOCAL */