]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-local.c
bf4f6890c8a72a77518000d6e37f9db9e39cbdb2
[openldap] / libraries / libldap / os-local.c
1 /* os-local.c -- platform-specific domain socket code */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved. 
18  */
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.  
24  */
25
26 #include "portable.h"
27
28 #ifdef LDAP_PF_LOCAL
29
30 #include <stdio.h>
31
32 #include <ac/stdlib.h>
33
34 #include <ac/errno.h>
35 #include <ac/socket.h>
36 #include <ac/string.h>
37 #include <ac/time.h>
38 #include <ac/unistd.h>
39
40 #ifdef HAVE_SYS_STAT_H
41 #include <sys/stat.h>
42 #endif
43 #ifdef HAVE_SYS_UIO_H
44 #include <sys/uio.h>
45 #endif
46
47 #ifdef HAVE_IO_H
48 #include <io.h>
49 #endif /* HAVE_IO_H */
50
51 #include "ldap-int.h"
52 #include "ldap_defaults.h"
53
54 #ifdef LDAP_DEBUG
55
56 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
57 do { \
58         ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
59 } while(0)
60
61 #else
62
63 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
64
65 #endif /* LDAP_DEBUG */
66
67 static void
68 ldap_pvt_set_errno(int err)
69 {
70         errno = err;
71 }
72
73 static int
74 ldap_pvt_ndelay_on(LDAP *ld, int fd)
75 {
76         oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
77         return ber_pvt_socket_set_nonblock( fd, 1 );
78 }
79    
80 static int
81 ldap_pvt_ndelay_off(LDAP *ld, int fd)
82 {
83         oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
84         return ber_pvt_socket_set_nonblock( fd, 0 );
85 }
86
87 static ber_socket_t
88 ldap_pvt_socket(LDAP *ld)
89 {
90         ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
91         oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
92         return ( s );
93 }
94
95 static int
96 ldap_pvt_close_socket(LDAP *ld, int s)
97 {
98         oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
99         return tcp_close(s);
100 }
101
102 #undef TRACE
103 #define TRACE do { \
104         char ebuf[128]; \
105         oslocal_debug(ld, \
106                 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
107                 s, \
108                 errno, \
109                 AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
110 } while( 0 )
111
112 /*
113  * check the socket for errors after select returned.
114  */
115 static int
116 ldap_pvt_is_socket_ready(LDAP *ld, int s)
117 {
118         oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
119
120 #if defined( notyet ) /* && defined( SO_ERROR ) */
121 {
122         int so_errno;
123         socklen_t dummy = sizeof(so_errno);
124         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
125                 == AC_SOCKET_ERROR )
126         {
127                 return -1;
128         }
129         if ( so_errno ) {
130                 ldap_pvt_set_errno(so_errno);
131                 TRACE;
132                 return -1;
133         }
134         return 0;
135 }
136 #else
137 {
138         /* error slippery */
139         struct sockaddr_un sa;
140         char ch;
141         socklen_t dummy = sizeof(sa);
142         if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
143                 == AC_SOCKET_ERROR )
144         {
145                 /* XXX: needs to be replace with ber_stream_read() */
146                 read(s, &ch, 1);
147                 TRACE;
148                 return -1;
149         }
150         return 0;
151 }
152 #endif
153         return -1;
154 }
155 #undef TRACE
156
157 #if !defined(HAVE_GETPEEREID) && \
158         !defined(HAVE_GETPEERUCRED) && \
159         !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
160         defined(HAVE_SENDMSG) && (defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTSLEN) || \
161                 defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL))
162 #define DO_SENDMSG
163 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
164         LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
165 #endif
166
167 static int
168 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
169 {
170         int rc;
171         struct timeval  tv = { 0 },
172                         *opt_tv = NULL;
173
174         opt_tv = ld->ld_options.ldo_tm_net;
175         if ( opt_tv != NULL ) {
176                 tv = *opt_tv;
177         }
178
179         oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
180                 s, opt_tv ? tv.tv_sec : -1L, async);
181
182         if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
183
184         if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
185                 != AC_SOCKET_ERROR )
186         {
187                 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
188
189 #ifdef DO_SENDMSG
190         /* Send a dummy message with access rights. Remote side will
191          * obtain our uid/gid by fstat'ing this descriptor.
192          */
193 sendcred:
194                 {
195                         int fds[2];
196                         if (pipe(fds) == 0) {
197                                 /* Abandon, noop, has no reply */
198                                 struct iovec iov;
199                                 struct msghdr msg = {0};
200 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
201 # ifndef CMSG_SPACE
202 # define CMSG_SPACE(len)        (_CMSG_ALIGN( sizeof(struct cmsghdr)) + _CMSG_ALIGN(len) )
203 # endif
204 # ifndef CMSG_LEN
205 # define CMSG_LEN(len)          (_CMSG_ALIGN( sizeof(struct cmsghdr)) + (len) )
206 # endif
207                                 union {
208                                         struct cmsghdr cm;
209                                         unsigned char control[CMSG_SPACE(sizeof(int))];
210                                 } control_un;
211                                 struct cmsghdr *cmsg;
212 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
213                                 msg.msg_name = NULL;
214                                 msg.msg_namelen = 0;
215                                 iov.iov_base = (char *) abandonPDU;
216                                 iov.iov_len = sizeof abandonPDU;
217                                 msg.msg_iov = &iov;
218                                 msg.msg_iovlen = 1;
219 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
220                                 msg.msg_control = control_un.control;
221                                 msg.msg_controllen = sizeof( control_un.control );
222                                 msg.msg_flags = 0;
223
224                                 cmsg = CMSG_FIRSTHDR( &msg );
225                                 cmsg->cmsg_len = CMSG_LEN( sizeof(int) );
226                                 cmsg->cmsg_level = SOL_SOCKET;
227                                 cmsg->cmsg_type = SCM_RIGHTS;
228
229                                 *((int *)CMSG_DATA(cmsg)) = fds[0];
230 # else
231                                 msg.msg_accrights = (char *)fds;
232                                 msg.msg_accrightslen = sizeof(int);
233 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
234                                 sendmsg( s, &msg, 0 );
235                                 close(fds[0]);
236                                 close(fds[1]);
237                         }
238                 }
239 #endif
240                 return 0;
241         }
242
243         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
244         
245 #ifdef notyet
246         if ( async ) return -2;
247 #endif
248
249 #ifdef HAVE_POLL
250         {
251                 struct pollfd fd;
252                 int timeout = INFTIM;
253
254                 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
255
256                 fd.fd = s;
257                 fd.events = POLL_WRITE;
258
259                 do {
260                         fd.revents = 0;
261                         rc = poll( &fd, 1, timeout );
262                 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
263                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
264
265                 if( rc == AC_SOCKET_ERROR ) return rc;
266
267                 if( fd.revents & POLL_WRITE ) {
268                         if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
269                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
270 #ifdef DO_SENDMSG
271                         goto sendcred;
272 #else
273                         return ( 0 );
274 #endif
275                 }
276         }
277 #else
278         {
279                 fd_set wfds, *z=NULL;
280
281 #ifdef FD_SETSIZE
282                 if ( s >= FD_SETSIZE ) {
283                         rc = AC_SOCKET_ERROR;
284                         tcp_close( s );
285                         ldap_pvt_set_errno( EMFILE );
286                         return rc;
287                 }
288 #endif
289                 do { 
290                         FD_ZERO(&wfds);
291                         FD_SET(s, &wfds );
292                         rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
293                 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
294                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
295
296                 if( rc == AC_SOCKET_ERROR ) return rc;
297
298                 if ( FD_ISSET(s, &wfds) ) {
299                         if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
300                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
301 #ifdef DO_SENDMSG
302                         goto sendcred;
303 #else
304                         return ( 0 );
305 #endif
306                 }
307         }
308 #endif
309
310         oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
311         ldap_pvt_set_errno( ETIMEDOUT );
312         return ( -1 );
313 }
314
315 int
316 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
317 {
318         struct sockaddr_un      server;
319         ber_socket_t            s;
320         int                     rc;
321
322         oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
323
324         s = ldap_pvt_socket( ld );
325         if ( s == AC_SOCKET_INVALID ) {
326                 return -1;
327         }
328
329         if ( path == NULL || path[0] == '\0' ) {
330                 path = LDAPI_SOCK;
331         } else {
332                 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
333                         ldap_pvt_set_errno( ENAMETOOLONG );
334                         return -1;
335                 }
336         }
337
338         oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
339
340         memset( &server, '\0', sizeof(server) );
341         server.sun_family = AF_LOCAL;
342         strcpy( server.sun_path, path );
343
344         rc = ldap_pvt_connect(ld, s, &server, async);
345
346         if (rc == 0) {
347                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
348         } else {
349                 ldap_pvt_close_socket(ld, s);
350         }
351         return rc;
352 }
353 #else
354 static int dummy;
355 #endif /* LDAP_PF_LOCAL */