xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/os-local.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: os-local.c,v 1.4 2010/03/08 03:47:40 lukem Exp $	*/
2 
3 /* os-local.c -- platform-specific domain socket code */
4 /* OpenLDAP: pkg/ldap/libraries/libldap/os-local.c,v 1.44.2.9 2009/08/12 23:48:32 quanah Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2009 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
19  * All rights reserved.
20  */
21 /* Portions (C) Copyright PADL Software Pty Ltd. 1999
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that this notice is preserved
24  * and that due credit is given to PADL Software Pty Ltd. This software
25  * is provided ``as is'' without express or implied warranty.
26  */
27 
28 #include "portable.h"
29 
30 #ifdef LDAP_PF_LOCAL
31 
32 #include <stdio.h>
33 
34 #include <ac/stdlib.h>
35 
36 #include <ac/errno.h>
37 #include <ac/socket.h>
38 #include <ac/string.h>
39 #include <ac/time.h>
40 #include <ac/unistd.h>
41 
42 #ifdef HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45 #ifdef HAVE_SYS_UIO_H
46 #include <sys/uio.h>
47 #endif
48 
49 #ifdef HAVE_IO_H
50 #include <io.h>
51 #endif /* HAVE_IO_H */
52 #ifdef HAVE_FCNTL_H
53 #include <fcntl.h>
54 #endif
55 
56 #include "ldap-int.h"
57 #include "ldap_defaults.h"
58 
59 #ifdef LDAP_DEBUG
60 
61 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
62 do { \
63 	ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
64 } while(0)
65 
66 #else
67 
68 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
69 
70 #endif /* LDAP_DEBUG */
71 
72 static void
73 ldap_pvt_set_errno(int err)
74 {
75 	errno = err;
76 }
77 
78 static int
79 ldap_pvt_ndelay_on(LDAP *ld, int fd)
80 {
81 	oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
82 	return ber_pvt_socket_set_nonblock( fd, 1 );
83 }
84 
85 static int
86 ldap_pvt_ndelay_off(LDAP *ld, int fd)
87 {
88 	oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
89 	return ber_pvt_socket_set_nonblock( fd, 0 );
90 }
91 
92 static ber_socket_t
93 ldap_pvt_socket(LDAP *ld)
94 {
95 	ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
96 	oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
97 #ifdef FD_CLOEXEC
98 	fcntl(s, F_SETFD, FD_CLOEXEC);
99 #endif
100 	return ( s );
101 }
102 
103 static int
104 ldap_pvt_close_socket(LDAP *ld, int s)
105 {
106 	oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
107 	return tcp_close(s);
108 }
109 
110 #undef TRACE
111 #define TRACE do { \
112 	char ebuf[128]; \
113 	oslocal_debug(ld, \
114 		"ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
115 		s, \
116 		errno, \
117 		AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
118 } while( 0 )
119 
120 /*
121  * check the socket for errors after select returned.
122  */
123 static int
124 ldap_pvt_is_socket_ready(LDAP *ld, int s)
125 {
126 	oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
127 
128 #if defined( notyet ) /* && defined( SO_ERROR ) */
129 {
130 	int so_errno;
131 	ber_socklen_t dummy = sizeof(so_errno);
132 	if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
133 		== AC_SOCKET_ERROR )
134 	{
135 		return -1;
136 	}
137 	if ( so_errno ) {
138 		ldap_pvt_set_errno(so_errno);
139 		TRACE;
140 		return -1;
141 	}
142 	return 0;
143 }
144 #else
145 {
146 	/* error slippery */
147 	struct sockaddr_un sa;
148 	char ch;
149 	ber_socklen_t dummy = sizeof(sa);
150 	if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
151 		== AC_SOCKET_ERROR )
152 	{
153 		/* XXX: needs to be replace with ber_stream_read() */
154 		(void)read(s, &ch, 1);
155 		TRACE;
156 		return -1;
157 	}
158 	return 0;
159 }
160 #endif
161 	return -1;
162 }
163 #undef TRACE
164 
165 #ifdef LDAP_PF_LOCAL_SENDMSG
166 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
167 	LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
168 #endif
169 
170 static int
171 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
172 {
173 	int rc;
174 	struct timeval	tv, *opt_tv = NULL;
175 
176 	if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
177 		tv = ld->ld_options.ldo_tm_net;
178 		opt_tv = &tv;
179 	}
180 
181 	oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
182 		s, opt_tv ? tv.tv_sec : -1L, async);
183 
184 	if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
185 
186 	if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
187 		!= AC_SOCKET_ERROR )
188 	{
189 		if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
190 
191 #ifdef LDAP_PF_LOCAL_SENDMSG
192 	/* Send a dummy message with access rights. Remote side will
193 	 * obtain our uid/gid by fstat'ing this descriptor. The
194 	 * descriptor permissions must match exactly, and we also
195 	 * send the socket name, which must also match.
196 	 */
197 sendcred:
198 		{
199 			int fds[2];
200 			ber_socklen_t salen = sizeof(*sa);
201 			if (pipe(fds) == 0) {
202 				/* Abandon, noop, has no reply */
203 				struct iovec iov;
204 				struct msghdr msg = {0};
205 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
206 # ifndef CMSG_SPACE
207 # define CMSG_SPACE(len)	(_CMSG_ALIGN( sizeof(struct cmsghdr)) + _CMSG_ALIGN(len) )
208 # endif
209 # ifndef CMSG_LEN
210 # define CMSG_LEN(len)		(_CMSG_ALIGN( sizeof(struct cmsghdr)) + (len) )
211 # endif
212 				union {
213 					struct cmsghdr cm;
214 					unsigned char control[CMSG_SPACE(sizeof(int))];
215 				} control_un;
216 				struct cmsghdr *cmsg;
217 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
218 				msg.msg_name = NULL;
219 				msg.msg_namelen = 0;
220 				iov.iov_base = (char *) abandonPDU;
221 				iov.iov_len = sizeof abandonPDU;
222 				msg.msg_iov = &iov;
223 				msg.msg_iovlen = 1;
224 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
225 				msg.msg_control = control_un.control;
226 				msg.msg_controllen = sizeof( control_un.control );
227 				msg.msg_flags = 0;
228 
229 				cmsg = CMSG_FIRSTHDR( &msg );
230 				cmsg->cmsg_len = CMSG_LEN( sizeof(int) );
231 				cmsg->cmsg_level = SOL_SOCKET;
232 				cmsg->cmsg_type = SCM_RIGHTS;
233 
234 				*((int *)CMSG_DATA(cmsg)) = fds[0];
235 # else
236 				msg.msg_accrights = (char *)fds;
237 				msg.msg_accrightslen = sizeof(int);
238 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
239 				getpeername( s, (struct sockaddr *) sa, &salen );
240 				fchmod( fds[0], S_ISUID|S_IRWXU );
241 				write( fds[1], sa, salen );
242 				sendmsg( s, &msg, 0 );
243 				close(fds[0]);
244 				close(fds[1]);
245 			}
246 		}
247 #endif
248 		return 0;
249 	}
250 
251 	if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
252 
253 #ifdef notyet
254 	if ( async ) return -2;
255 #endif
256 
257 #ifdef HAVE_POLL
258 	{
259 		struct pollfd fd;
260 		int timeout = INFTIM;
261 
262 		if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
263 
264 		fd.fd = s;
265 		fd.events = POLL_WRITE;
266 
267 		do {
268 			fd.revents = 0;
269 			rc = poll( &fd, 1, timeout );
270 		} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
271 			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
272 
273 		if( rc == AC_SOCKET_ERROR ) return rc;
274 
275 		if( fd.revents & POLL_WRITE ) {
276 			if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
277 			if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
278 #ifdef LDAP_PF_LOCAL_SENDMSG
279 			goto sendcred;
280 #else
281 			return ( 0 );
282 #endif
283 		}
284 	}
285 #else
286 	{
287 		fd_set wfds, *z=NULL;
288 
289 #ifdef FD_SETSIZE
290 		if ( s >= FD_SETSIZE ) {
291 			rc = AC_SOCKET_ERROR;
292 			tcp_close( s );
293 			ldap_pvt_set_errno( EMFILE );
294 			return rc;
295 		}
296 #endif
297 		do {
298 			FD_ZERO(&wfds);
299 			FD_SET(s, &wfds );
300 			rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
301 		} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
302 			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
303 
304 		if( rc == AC_SOCKET_ERROR ) return rc;
305 
306 		if ( FD_ISSET(s, &wfds) ) {
307 			if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
308 			if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
309 #ifdef LDAP_PF_LOCAL_SENDMSG
310 			goto sendcred;
311 #else
312 			return ( 0 );
313 #endif
314 		}
315 	}
316 #endif
317 
318 	oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
319 	ldap_pvt_set_errno( ETIMEDOUT );
320 	return ( -1 );
321 }
322 
323 int
324 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv, int async)
325 {
326 	struct sockaddr_un	server;
327 	ber_socket_t		s;
328 	int			rc;
329 	const char *path = srv->lud_host;
330 
331 	oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
332 
333 	s = ldap_pvt_socket( ld );
334 	if ( s == AC_SOCKET_INVALID ) {
335 		return -1;
336 	}
337 
338 	if ( path == NULL || path[0] == '\0' ) {
339 		path = LDAPI_SOCK;
340 	} else {
341 		if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
342 			ldap_pvt_set_errno( ENAMETOOLONG );
343 			return -1;
344 		}
345 	}
346 
347 	oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
348 
349 	memset( &server, '\0', sizeof(server) );
350 	server.sun_family = AF_LOCAL;
351 	strcpy( server.sun_path, path );
352 
353 	rc = ldap_pvt_connect(ld, s, &server, async);
354 
355 	if (rc == 0) {
356 		int err;
357 		err = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&server );
358 		if ( err )
359 			rc = err;
360 	}
361 	if ( rc ) {
362 		ldap_pvt_close_socket(ld, s);
363 	}
364 	return rc;
365 }
366 #else
367 static int dummy;
368 #endif /* LDAP_PF_LOCAL */
369