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