]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-local.c
ITS#3208: fix matchedValues parsing
[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         oslocal_debug(ld, \
107                 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
108                 s, \
109                 errno, \
110                 STRERROR(errno) ); \
111 } while( 0 )
112
113 /*
114  * check the socket for errors after select returned.
115  */
116 static int
117 ldap_pvt_is_socket_ready(LDAP *ld, int s)
118 {
119         oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
120
121 #if defined( notyet ) /* && defined( SO_ERROR ) */
122 {
123         int so_errno;
124         socklen_t dummy = sizeof(so_errno);
125         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
126                 == AC_SOCKET_ERROR )
127         {
128                 return -1;
129         }
130         if ( so_errno ) {
131                 ldap_pvt_set_errno(so_errno);
132                 TRACE;
133                 return -1;
134         }
135         return 0;
136 }
137 #else
138 {
139         /* error slippery */
140         struct sockaddr_un sa;
141         char ch;
142         socklen_t dummy = sizeof(sa);
143         if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
144                 == AC_SOCKET_ERROR )
145         {
146                 /* XXX: needs to be replace with ber_stream_read() */
147                 read(s, &ch, 1);
148                 TRACE;
149                 return -1;
150         }
151         return 0;
152 }
153 #endif
154         return -1;
155 }
156 #undef TRACE
157
158 #if !defined(HAVE_GETPEEREID) && \
159         !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
160         defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
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         fd_set          wfds, *z=NULL;
172
173         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
174                 tv.tv_usec = opt_tv->tv_usec;
175                 tv.tv_sec = opt_tv->tv_sec;
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 )
182                 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 ) {
188                         return ( -1 );
189                 }
190 #ifdef DO_SENDMSG
191         /* Send a dummy message with access rights. Remote side will
192          * obtain our uid/gid by fstat'ing this descriptor.
193          */
194 sendcred:
195                 {
196                         int fds[2];
197                         if (pipe(fds) == 0) {
198                                 /* Abandon, noop, has no reply */
199                                 struct iovec iov;
200                                 struct msghdr msg = {0};
201                                 iov.iov_base = (char *) abandonPDU;
202                                 iov.iov_len = sizeof abandonPDU;
203                                 msg.msg_iov = &iov;
204                                 msg.msg_iovlen = 1;
205                                 msg.msg_accrights = (char *)fds;
206                                 msg.msg_accrightslen = sizeof(int);
207                                 sendmsg( s, &msg, 0 );
208                                 close(fds[0]);
209                                 close(fds[1]);
210                         }
211                 }
212 #endif
213                 return ( 0 );
214         }
215
216         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
217                 return ( -1 );
218         }
219         
220 #ifdef notyet
221         if ( async ) return ( -2 );
222 #endif
223
224         FD_ZERO(&wfds);
225         FD_SET(s, &wfds );
226
227         do { 
228                 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
229         } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
230                 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
231
232         if( rc == AC_SOCKET_ERROR ) return rc;
233
234         if ( FD_ISSET(s, &wfds) ) {
235                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
236                         return ( -1 );
237                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
238                         return ( -1 );
239 #ifdef DO_SENDMSG
240                 goto sendcred;
241 #else
242                 return ( 0 );
243 #endif
244         }
245         oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
246         ldap_pvt_set_errno( ETIMEDOUT );
247         return ( -1 );
248 }
249
250 int
251 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
252 {
253         struct sockaddr_un      server;
254         ber_socket_t            s;
255         int                     rc;
256
257         oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
258
259         s = ldap_pvt_socket( ld );
260         if ( s == AC_SOCKET_INVALID ) {
261                 return -1;
262         }
263
264         if ( path == NULL || path[0] == '\0' ) {
265                 path = LDAPI_SOCK;
266         } else {
267                 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
268                         ldap_pvt_set_errno( ENAMETOOLONG );
269                         return -1;
270                 }
271         }
272
273         oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
274
275         memset( &server, '\0', sizeof(server) );
276         server.sun_family = AF_LOCAL;
277         strcpy( server.sun_path, path );
278
279         rc = ldap_pvt_connect(ld, s, &server, async);
280
281         if (rc == 0) {
282                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
283         } else {
284                 ldap_pvt_close_socket(ld, s);
285         }
286         return rc;
287 }
288 #else
289 static int dummy;
290 #endif /* LDAP_PF_LOCAL */