]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-local.c
For ITS#2424, move all SASL session management to ldap_int_sasl_bind.
[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) && \
139         !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
140         defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
141 #define DO_SENDMSG
142 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
143         LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
144 #endif
145
146 static int
147 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
148 {
149         int rc;
150         struct timeval  tv, *opt_tv=NULL;
151         fd_set          wfds, *z=NULL;
152
153         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
154                 tv.tv_usec = opt_tv->tv_usec;
155                 tv.tv_sec = opt_tv->tv_sec;
156         }
157
158         oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
159                         s, opt_tv ? tv.tv_sec : -1L, async);
160
161         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
162                 return ( -1 );
163
164         if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
165                 != AC_SOCKET_ERROR )
166         {
167                 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
168                         return ( -1 );
169                 }
170 #ifdef DO_SENDMSG
171         /* Send a dummy message with access rights. Remote side will
172          * obtain our uid/gid by fstat'ing this descriptor.
173          */
174 sendcred:
175                 {
176                         int fds[2];
177                         if (pipe(fds) == 0) {
178                                 /* Abandon, noop, has no reply */
179                                 struct iovec iov = {(char *)abandonPDU, sizeof(abandonPDU)};
180                                 struct msghdr msg = {0};
181                                 msg.msg_iov = &iov;
182                                 msg.msg_iovlen = 1;
183                                 msg.msg_accrights = (char *)fds;
184                                 msg.msg_accrightslen = sizeof(int);
185                                 sendmsg( s, &msg, 0 );
186                                 close(fds[0]);
187                                 close(fds[1]);
188                         }
189                 }
190 #endif
191                 return ( 0 );
192         }
193
194         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
195                 return ( -1 );
196         }
197         
198 #ifdef notyet
199         if ( async ) return ( -2 );
200 #endif
201
202         FD_ZERO(&wfds);
203         FD_SET(s, &wfds );
204
205         do { 
206                 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
207         } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
208                 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
209
210         if( rc == AC_SOCKET_ERROR ) return rc;
211
212         if ( FD_ISSET(s, &wfds) ) {
213                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
214                         return ( -1 );
215                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
216                         return ( -1 );
217 #ifdef DO_SENDMSG
218                 goto sendcred;
219 #else
220                 return ( 0 );
221 #endif
222         }
223         oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
224         ldap_pvt_set_errno( ETIMEDOUT );
225         return ( -1 );
226 }
227
228 int
229 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
230 {
231         struct sockaddr_un      server;
232         ber_socket_t            s;
233         int                     rc;
234
235         oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
236
237         s = ldap_pvt_socket( ld );
238         if ( s == AC_SOCKET_INVALID ) {
239                 return -1;
240         }
241
242         if ( path == NULL || path[0] == '\0' ) {
243                 path = LDAPI_SOCK;
244         } else {
245                 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
246                         ldap_pvt_set_errno( ENAMETOOLONG );
247                         return -1;
248                 }
249         }
250
251         oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
252
253         memset( &server, '\0', sizeof(server) );
254         server.sun_family = AF_LOCAL;
255         strcpy( server.sun_path, path );
256
257         rc = ldap_pvt_connect(ld, s, &server, async);
258
259         if (rc == 0) {
260                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
261         } else {
262                 ldap_pvt_close_socket(ld, s);
263         }
264         return rc;
265 }
266 #else
267 static int dummy;
268 #endif /* LDAP_PF_LOCAL */