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