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