]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-local.c
9c6e5f9f29b495498dbbf82a0a36d14f9828f8c2
[openldap] / libraries / libldap / os-local.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *  Copyright (c) 1999 PADL Software Pty Ltd.
10  *  os-ip.c -- platform-specific domain socket code
11  */
12
13
14 #include "portable.h"
15
16 #ifdef LDAP_PF_LOCAL
17
18 #include <stdio.h>
19
20 #include <ac/stdlib.h>
21
22 #include <ac/errno.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26 #include <ac/unistd.h>
27
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_UIO_H
32 #include <sys/uio.h>
33 #endif
34
35 #ifdef HAVE_IO_H
36 #include <io.h>
37 #endif /* HAVE_IO_H */
38
39 #include "ldap-int.h"
40 #include "ldap_defaults.h"
41
42 /* int ldap_int_tblsize = 0; */
43
44 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
45 do { \
46         ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
47 } while(0)
48
49 static void
50 ldap_pvt_set_errno(int err)
51 {
52         errno = err;
53 }
54
55 static int
56 ldap_pvt_ndelay_on(LDAP *ld, int fd)
57 {
58         oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
59         return ber_pvt_socket_set_nonblock( fd, 1 );
60 }
61    
62 static int
63 ldap_pvt_ndelay_off(LDAP *ld, int fd)
64 {
65         oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
66         return ber_pvt_socket_set_nonblock( fd, 0 );
67 }
68
69 static ber_socket_t
70 ldap_pvt_socket(LDAP *ld)
71 {
72         ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
73         oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
74         return ( s );
75 }
76
77 static int
78 ldap_pvt_close_socket(LDAP *ld, int s)
79 {
80         oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
81         return tcp_close(s);
82 }
83
84 #undef TRACE
85 #define TRACE do { \
86         oslocal_debug(ld, \
87                 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
88                 s, \
89                 errno, \
90                 STRERROR(errno) ); \
91 } while( 0 )
92
93 /*
94  * check the socket for errors after select returned.
95  */
96 static int
97 ldap_pvt_is_socket_ready(LDAP *ld, int s)
98 {
99         oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
100
101 #if defined( notyet ) /* && defined( SO_ERROR ) */
102 {
103         int so_errno;
104         socklen_t dummy = sizeof(so_errno);
105         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
106                 == AC_SOCKET_ERROR )
107         {
108                 return -1;
109         }
110         if ( so_errno ) {
111                 ldap_pvt_set_errno(so_errno);
112                 TRACE;
113                 return -1;
114         }
115         return 0;
116 }
117 #else
118 {
119         /* error slippery */
120         struct sockaddr_un sa;
121         char ch;
122         socklen_t dummy = sizeof(sa);
123         if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
124                 == AC_SOCKET_ERROR )
125         {
126                 /* XXX: needs to be replace with ber_stream_read() */
127                 read(s, &ch, 1);
128                 TRACE;
129                 return -1;
130         }
131         return 0;
132 }
133 #endif
134         return -1;
135 }
136 #undef TRACE
137
138 #if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && defined(HAVE_SENDMSG)
139 #define DO_SENDMSG
140 #endif
141
142 static int
143 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
144 {
145         int rc;
146         struct timeval  tv, *opt_tv=NULL;
147         fd_set          wfds, *z=NULL;
148
149         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
150                 tv.tv_usec = opt_tv->tv_usec;
151                 tv.tv_sec = opt_tv->tv_sec;
152         }
153
154         oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
155                         s, opt_tv ? tv.tv_sec : -1L, async);
156
157         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
158                 return ( -1 );
159
160         if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
161                 != AC_SOCKET_ERROR )
162         {
163                 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
164                         return ( -1 );
165                 }
166 #ifdef DO_SENDMSG
167         /* Send a dummy message with access rights. Remote side will
168          * obtain our uid/gid by fstat'ing this descriptor.
169          */
170 sendcred:
171                 {
172                         int fds[2];
173                         /* Abandon, noop, has no reply */
174                         char txt[] = {LDAP_TAG_MESSAGE, 6,
175                                 LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
176                         struct iovec iov = {txt, sizeof(txt)};
177                         struct msghdr msg = {0};
178                         if (pipe(fds) == 0) {
179                                 msg.msg_iov = &iov;
180                                 msg.msg_iovlen = 1;
181                                 msg.msg_accrights = (char *)fds;
182                                 msg.msg_accrightslen = sizeof(int);
183                                 sendmsg( s, &msg, 0 );
184                                 close(fds[0]);
185                                 close(fds[1]);
186                         }
187                 }
188 #endif
189                 return ( 0 );
190         }
191
192         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
193                 return ( -1 );
194         }
195         
196 #ifdef notyet
197         if ( async ) return ( -2 );
198 #endif
199
200         FD_ZERO(&wfds);
201         FD_SET(s, &wfds );
202
203         do { 
204                 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
205         } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
206                 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
207
208         if( rc == AC_SOCKET_ERROR ) return rc;
209
210         if ( FD_ISSET(s, &wfds) ) {
211                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
212                         return ( -1 );
213                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
214                         return ( -1 );
215 #ifdef DO_SENDMSG
216                 goto sendcred;
217 #else
218                 return ( 0 );
219 #endif
220         }
221         oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
222         ldap_pvt_set_errno( ETIMEDOUT );
223         return ( -1 );
224 }
225
226 int
227 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
228 {
229         struct sockaddr_un      server;
230         ber_socket_t            s;
231         int                     rc;
232
233         oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
234
235         s = ldap_pvt_socket( ld );
236         if ( s == AC_SOCKET_INVALID ) {
237                 return -1;
238         }
239
240         if ( path == NULL || path[0] == '\0' ) {
241                 path = LDAPI_SOCK;
242         } else {
243                 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
244                         ldap_pvt_set_errno( ENAMETOOLONG );
245                         return -1;
246                 }
247         }
248
249         oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
250
251         memset( &server, '\0', sizeof(server) );
252         server.sun_family = AF_LOCAL;
253         strcpy( server.sun_path, path );
254
255         rc = ldap_pvt_connect(ld, s, &server, async);
256
257         if (rc == 0) {
258                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
259         } else {
260                 ldap_pvt_close_socket(ld, s);
261         }
262         return rc;
263 }
264 #else
265 static int dummy;
266 #endif /* LDAP_PF_LOCAL */