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