]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-local.c
ff2b93ed5182e1fb279195658938d08750e486e9
[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-2004 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(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
159         defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
160 #define DO_SENDMSG
161 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
162         LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
163 #endif
164
165 static int
166 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
167 {
168         int rc;
169         struct timeval  tv, *opt_tv=NULL;
170
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;
174         }
175
176         oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
177                 s, opt_tv ? tv.tv_sec : -1L, async);
178
179         if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
180
181         if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
182                 != AC_SOCKET_ERROR )
183         {
184                 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
185
186 #ifdef DO_SENDMSG
187         /* Send a dummy message with access rights. Remote side will
188          * obtain our uid/gid by fstat'ing this descriptor.
189          */
190 sendcred:
191                 {
192                         int fds[2];
193                         if (pipe(fds) == 0) {
194                                 /* Abandon, noop, has no reply */
195                                 struct iovec iov;
196                                 struct msghdr msg = {0};
197                                 iov.iov_base = (char *) abandonPDU;
198                                 iov.iov_len = sizeof abandonPDU;
199                                 msg.msg_iov = &iov;
200                                 msg.msg_iovlen = 1;
201                                 msg.msg_accrights = (char *)fds;
202                                 msg.msg_accrightslen = sizeof(int);
203                                 sendmsg( s, &msg, 0 );
204                                 close(fds[0]);
205                                 close(fds[1]);
206                         }
207                 }
208 #endif
209                 return 0;
210         }
211
212         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
213         
214 #ifdef notyet
215         if ( async ) return -2;
216 #endif
217
218 #ifdef HAVE_POLL
219         {
220                 struct pollfd fd;
221                 int timeout = INFTIM;
222
223                 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
224
225                 fd.fd = s;
226                 fd.events = POLL_WRITE;
227
228                 do {
229                         fd.revents = 0;
230                         rc = poll( &fd, 1, timeout );
231                 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
232                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
233
234                 if( rc == AC_SOCKET_ERROR ) return rc;
235
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;
239 #ifdef DO_SENDMSG
240                         goto sendcred;
241 #else
242                         return ( 0 );
243 #endif
244                 }
245         }
246 #else
247         {
248                 fd_set wfds, *z=NULL;
249
250 #ifdef FD_SETSIZE
251                 if ( s >= FD_SETSIZE ) {
252                         rc = AC_SOCKET_ERROR;
253                         tcp_close( s );
254                         ldap_pvt_set_errno( EMFILE );
255                         return rc;
256                 }
257 #endif
258                 do { 
259                         FD_ZERO(&wfds);
260                         FD_SET(s, &wfds );
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 ));
264
265                 if( rc == AC_SOCKET_ERROR ) return rc;
266
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;
270 #ifdef DO_SENDMSG
271                         goto sendcred;
272 #else
273                         return ( 0 );
274 #endif
275                 }
276         }
277 #endif
278
279         oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
280         ldap_pvt_set_errno( ETIMEDOUT );
281         return ( -1 );
282 }
283
284 int
285 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
286 {
287         struct sockaddr_un      server;
288         ber_socket_t            s;
289         int                     rc;
290
291         oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
292
293         s = ldap_pvt_socket( ld );
294         if ( s == AC_SOCKET_INVALID ) {
295                 return -1;
296         }
297
298         if ( path == NULL || path[0] == '\0' ) {
299                 path = LDAPI_SOCK;
300         } else {
301                 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
302                         ldap_pvt_set_errno( ENAMETOOLONG );
303                         return -1;
304                 }
305         }
306
307         oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
308
309         memset( &server, '\0', sizeof(server) );
310         server.sun_family = AF_LOCAL;
311         strcpy( server.sun_path, path );
312
313         rc = ldap_pvt_connect(ld, s, &server, async);
314
315         if (rc == 0) {
316                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
317         } else {
318                 ldap_pvt_close_socket(ld, s);
319         }
320         return rc;
321 }
322 #else
323 static int dummy;
324 #endif /* LDAP_PF_LOCAL */