1 /* os-local.c -- platform-specific domain socket code */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/os-local.c,v 1.44.2.4 2008/05/20 00:05:30 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 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 */
55 #include "ldap_defaults.h"
59 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
61 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
66 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
68 #endif /* LDAP_DEBUG */
71 ldap_pvt_set_errno(int err
)
77 ldap_pvt_ndelay_on(LDAP
*ld
, int fd
)
79 oslocal_debug(ld
, "ldap_ndelay_on: %d\n",fd
,0,0);
80 return ber_pvt_socket_set_nonblock( fd
, 1 );
84 ldap_pvt_ndelay_off(LDAP
*ld
, int fd
)
86 oslocal_debug(ld
, "ldap_ndelay_off: %d\n",fd
,0,0);
87 return ber_pvt_socket_set_nonblock( fd
, 0 );
91 ldap_pvt_socket(LDAP
*ld
)
93 ber_socket_t s
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
94 oslocal_debug(ld
, "ldap_new_socket: %d\n",s
,0,0);
96 fcntl(s
, F_SETFD
, FD_CLOEXEC
);
102 ldap_pvt_close_socket(LDAP
*ld
, int s
)
104 oslocal_debug(ld
, "ldap_close_socket: %d\n",s
,0,0);
112 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
115 AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
119 * check the socket for errors after select returned.
122 ldap_pvt_is_socket_ready(LDAP
*ld
, int s
)
124 oslocal_debug(ld
, "ldap_is_sock_ready: %d\n",s
,0,0);
126 #if defined( notyet ) /* && defined( SO_ERROR ) */
129 ber_socklen_t dummy
= sizeof(so_errno
);
130 if ( getsockopt( s
, SOL_SOCKET
, SO_ERROR
, &so_errno
, &dummy
)
136 ldap_pvt_set_errno(so_errno
);
145 struct sockaddr_un sa
;
147 ber_socklen_t dummy
= sizeof(sa
);
148 if ( getpeername( s
, (struct sockaddr
*) &sa
, &dummy
)
151 /* XXX: needs to be replace with ber_stream_read() */
163 #ifdef LDAP_PF_LOCAL_SENDMSG
164 static const char abandonPDU
[] = {LDAP_TAG_MESSAGE
, 6,
165 LDAP_TAG_MSGID
, 1, 0, LDAP_REQ_ABANDON
, 1, 0};
169 ldap_pvt_connect(LDAP
*ld
, ber_socket_t s
, struct sockaddr_un
*sa
, int async
)
172 struct timeval tv
, *opt_tv
= NULL
;
174 if ( ld
->ld_options
.ldo_tm_net
.tv_sec
>= 0 ) {
175 tv
= ld
->ld_options
.ldo_tm_net
;
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 ) return -1;
184 if ( connect(s
, (struct sockaddr
*) sa
, sizeof(struct sockaddr_un
))
187 if ( ldap_pvt_ndelay_off(ld
, s
) == -1 ) return -1;
189 #ifdef LDAP_PF_LOCAL_SENDMSG
190 /* Send a dummy message with access rights. Remote side will
191 * obtain our uid/gid by fstat'ing this descriptor. The
192 * descriptor permissions must match exactly, and we also
193 * send the socket name, which must also match.
198 ber_socklen_t salen
= sizeof(*sa
);
199 if (pipe(fds
) == 0) {
200 /* Abandon, noop, has no reply */
202 struct msghdr msg
= {0};
203 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
205 # define CMSG_SPACE(len) (_CMSG_ALIGN( sizeof(struct cmsghdr)) + _CMSG_ALIGN(len) )
208 # define CMSG_LEN(len) (_CMSG_ALIGN( sizeof(struct cmsghdr)) + (len) )
212 unsigned char control
[CMSG_SPACE(sizeof(int))];
214 struct cmsghdr
*cmsg
;
215 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
218 iov
.iov_base
= (char *) abandonPDU
;
219 iov
.iov_len
= sizeof abandonPDU
;
222 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
223 msg
.msg_control
= control_un
.control
;
224 msg
.msg_controllen
= sizeof( control_un
.control
);
227 cmsg
= CMSG_FIRSTHDR( &msg
);
228 cmsg
->cmsg_len
= CMSG_LEN( sizeof(int) );
229 cmsg
->cmsg_level
= SOL_SOCKET
;
230 cmsg
->cmsg_type
= SCM_RIGHTS
;
232 *((int *)CMSG_DATA(cmsg
)) = fds
[0];
234 msg
.msg_accrights
= (char *)fds
;
235 msg
.msg_accrightslen
= sizeof(int);
236 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
237 getpeername( s
, (struct sockaddr
*) sa
, &salen
);
238 fchmod( fds
[0], S_ISUID
|S_IRWXU
);
239 write( fds
[1], sa
, salen
);
240 sendmsg( s
, &msg
, 0 );
249 if ( errno
!= EINPROGRESS
&& errno
!= EWOULDBLOCK
) return -1;
252 if ( async
) return -2;
258 int timeout
= INFTIM
;
260 if( opt_tv
!= NULL
) timeout
= TV2MILLISEC( &tv
);
263 fd
.events
= POLL_WRITE
;
267 rc
= poll( &fd
, 1, timeout
);
268 } while( rc
== AC_SOCKET_ERROR
&& errno
== EINTR
&&
269 LDAP_BOOL_GET(&ld
->ld_options
, LDAP_BOOL_RESTART
));
271 if( rc
== AC_SOCKET_ERROR
) return rc
;
273 if( fd
.revents
& POLL_WRITE
) {
274 if ( ldap_pvt_is_socket_ready(ld
, s
) == -1 ) return -1;
275 if ( ldap_pvt_ndelay_off(ld
, s
) == -1 ) return -1;
276 #ifdef LDAP_PF_LOCAL_SENDMSG
285 fd_set wfds
, *z
=NULL
;
288 if ( s
>= FD_SETSIZE
) {
289 rc
= AC_SOCKET_ERROR
;
291 ldap_pvt_set_errno( EMFILE
);
298 rc
= select( ldap_int_tblsize
, z
, &wfds
, z
, opt_tv
? &tv
: NULL
);
299 } while( rc
== AC_SOCKET_ERROR
&& errno
== EINTR
&&
300 LDAP_BOOL_GET(&ld
->ld_options
, LDAP_BOOL_RESTART
));
302 if( rc
== AC_SOCKET_ERROR
) return rc
;
304 if ( FD_ISSET(s
, &wfds
) ) {
305 if ( ldap_pvt_is_socket_ready(ld
, s
) == -1 ) return -1;
306 if ( ldap_pvt_ndelay_off(ld
, s
) == -1 ) return -1;
307 #ifdef LDAP_PF_LOCAL_SENDMSG
316 oslocal_debug(ld
, "ldap_connect_timeout: timed out\n",0,0,0);
317 ldap_pvt_set_errno( ETIMEDOUT
);
322 ldap_connect_to_path(LDAP
*ld
, Sockbuf
*sb
, const char *path
, int async
)
324 struct sockaddr_un server
;
328 oslocal_debug(ld
, "ldap_connect_to_path\n",0,0,0);
330 s
= ldap_pvt_socket( ld
);
331 if ( s
== AC_SOCKET_INVALID
) {
335 if ( path
== NULL
|| path
[0] == '\0' ) {
338 if ( strlen(path
) > (sizeof( server
.sun_path
) - 1) ) {
339 ldap_pvt_set_errno( ENAMETOOLONG
);
344 oslocal_debug(ld
, "ldap_connect_to_path: Trying %s\n", path
, 0, 0);
346 memset( &server
, '\0', sizeof(server
) );
347 server
.sun_family
= AF_LOCAL
;
348 strcpy( server
.sun_path
, path
);
350 rc
= ldap_pvt_connect(ld
, s
, &server
, async
);
353 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_SET_FD
, (void *)&s
);
355 ldap_pvt_close_socket(ld
, s
);
361 #endif /* LDAP_PF_LOCAL */