]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-local.c
6cbc8a10c2d11cc3f592589f38d369699111b0a5
[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 /* int ldap_int_tblsize = 0; */
55
56 #ifdef LDAP_DEBUG
57
58 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
59 do { \
60         ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
61 } while(0)
62
63 #else
64
65 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
66
67 #endif /* LDAP_DEBUG */
68
69 static void
70 ldap_pvt_set_errno(int err)
71 {
72         errno = err;
73 }
74
75 static int
76 ldap_pvt_ndelay_on(LDAP *ld, int fd)
77 {
78         oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
79         return ber_pvt_socket_set_nonblock( fd, 1 );
80 }
81    
82 static int
83 ldap_pvt_ndelay_off(LDAP *ld, int fd)
84 {
85         oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
86         return ber_pvt_socket_set_nonblock( fd, 0 );
87 }
88
89 static ber_socket_t
90 ldap_pvt_socket(LDAP *ld)
91 {
92         ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
93         oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
94         return ( s );
95 }
96
97 static int
98 ldap_pvt_close_socket(LDAP *ld, int s)
99 {
100         oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
101         return tcp_close(s);
102 }
103
104 #undef TRACE
105 #define TRACE do { \
106         char ebuf[128]; \
107         oslocal_debug(ld, \
108                 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
109                 s, \
110                 errno, \
111                 AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
112 } while( 0 )
113
114 /*
115  * check the socket for errors after select returned.
116  */
117 static int
118 ldap_pvt_is_socket_ready(LDAP *ld, int s)
119 {
120         oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
121
122 #if defined( notyet ) /* && defined( SO_ERROR ) */
123 {
124         int so_errno;
125         socklen_t dummy = sizeof(so_errno);
126         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
127                 == AC_SOCKET_ERROR )
128         {
129                 return -1;
130         }
131         if ( so_errno ) {
132                 ldap_pvt_set_errno(so_errno);
133                 TRACE;
134                 return -1;
135         }
136         return 0;
137 }
138 #else
139 {
140         /* error slippery */
141         struct sockaddr_un sa;
142         char ch;
143         socklen_t dummy = sizeof(sa);
144         if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
145                 == AC_SOCKET_ERROR )
146         {
147                 /* XXX: needs to be replace with ber_stream_read() */
148                 read(s, &ch, 1);
149                 TRACE;
150                 return -1;
151         }
152         return 0;
153 }
154 #endif
155         return -1;
156 }
157 #undef TRACE
158
159 #if !defined(HAVE_GETPEEREID) && \
160         !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
161         defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
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, *opt_tv=NULL;
172         fd_set          wfds, *z=NULL;
173
174         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
175                 tv.tv_usec = opt_tv->tv_usec;
176                 tv.tv_sec = opt_tv->tv_sec;
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 )
183                 return ( -1 );
184
185         if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
186                 != AC_SOCKET_ERROR )
187         {
188                 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
189                         return ( -1 );
190                 }
191 #ifdef DO_SENDMSG
192         /* Send a dummy message with access rights. Remote side will
193          * obtain our uid/gid by fstat'ing this descriptor.
194          */
195 sendcred:
196                 {
197                         int fds[2];
198                         if (pipe(fds) == 0) {
199                                 /* Abandon, noop, has no reply */
200                                 struct iovec iov;
201                                 struct msghdr msg = {0};
202                                 iov.iov_base = (char *) abandonPDU;
203                                 iov.iov_len = sizeof abandonPDU;
204                                 msg.msg_iov = &iov;
205                                 msg.msg_iovlen = 1;
206                                 msg.msg_accrights = (char *)fds;
207                                 msg.msg_accrightslen = sizeof(int);
208                                 sendmsg( s, &msg, 0 );
209                                 close(fds[0]);
210                                 close(fds[1]);
211                         }
212                 }
213 #endif
214                 return ( 0 );
215         }
216
217         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
218                 return ( -1 );
219         }
220         
221 #ifdef notyet
222         if ( async ) return ( -2 );
223 #endif
224
225         FD_ZERO(&wfds);
226         FD_SET(s, &wfds );
227
228         do { 
229                 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
230         } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
231                 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
232
233         if( rc == AC_SOCKET_ERROR ) return rc;
234
235         if ( FD_ISSET(s, &wfds) ) {
236                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
237                         return ( -1 );
238                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
239                         return ( -1 );
240 #ifdef DO_SENDMSG
241                 goto sendcred;
242 #else
243                 return ( 0 );
244 #endif
245         }
246         oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
247         ldap_pvt_set_errno( ETIMEDOUT );
248         return ( -1 );
249 }
250
251 int
252 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
253 {
254         struct sockaddr_un      server;
255         ber_socket_t            s;
256         int                     rc;
257
258         oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
259
260         s = ldap_pvt_socket( ld );
261         if ( s == AC_SOCKET_INVALID ) {
262                 return -1;
263         }
264
265         if ( path == NULL || path[0] == '\0' ) {
266                 path = LDAPI_SOCK;
267         } else {
268                 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
269                         ldap_pvt_set_errno( ENAMETOOLONG );
270                         return -1;
271                 }
272         }
273
274         oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
275
276         memset( &server, '\0', sizeof(server) );
277         server.sun_family = AF_LOCAL;
278         strcpy( server.sun_path, path );
279
280         rc = ldap_pvt_connect(ld, s, &server, async);
281
282         if (rc == 0) {
283                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
284         } else {
285                 ldap_pvt_close_socket(ld, s);
286         }
287         return rc;
288 }
289 #else
290 static int dummy;
291 #endif /* LDAP_PF_LOCAL */