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