xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/os-local.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /* os-local.c -- platform-specific domain socket code */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap/os-local.c,v 1.44.2.3 2008/02/11 23:26:41 kurt 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 
51 #include "ldap-int.h"
52 #include "ldap_defaults.h"
53 
54 #ifdef LDAP_DEBUG
55 
56 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
57 do { \
58 	ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
59 } while(0)
60 
61 #else
62 
63 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
64 
65 #endif /* LDAP_DEBUG */
66 
67 static void
68 ldap_pvt_set_errno(int err)
69 {
70 	errno = err;
71 }
72 
73 static int
74 ldap_pvt_ndelay_on(LDAP *ld, int fd)
75 {
76 	oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
77 	return ber_pvt_socket_set_nonblock( fd, 1 );
78 }
79 
80 static int
81 ldap_pvt_ndelay_off(LDAP *ld, int fd)
82 {
83 	oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
84 	return ber_pvt_socket_set_nonblock( fd, 0 );
85 }
86 
87 static ber_socket_t
88 ldap_pvt_socket(LDAP *ld)
89 {
90 	ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
91 	oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
92 	return ( s );
93 }
94 
95 static int
96 ldap_pvt_close_socket(LDAP *ld, int s)
97 {
98 	oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
99 	return tcp_close(s);
100 }
101 
102 #undef TRACE
103 #define TRACE do { \
104 	char ebuf[128]; \
105 	oslocal_debug(ld, \
106 		"ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
107 		s, \
108 		errno, \
109 		AC_STRERROR_R(errno, ebuf, sizeof ebuf)); \
110 } while( 0 )
111 
112 /*
113  * check the socket for errors after select returned.
114  */
115 static int
116 ldap_pvt_is_socket_ready(LDAP *ld, int s)
117 {
118 	oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
119 
120 #if defined( notyet ) /* && defined( SO_ERROR ) */
121 {
122 	int so_errno;
123 	ber_socklen_t dummy = sizeof(so_errno);
124 	if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
125 		== AC_SOCKET_ERROR )
126 	{
127 		return -1;
128 	}
129 	if ( so_errno ) {
130 		ldap_pvt_set_errno(so_errno);
131 		TRACE;
132 		return -1;
133 	}
134 	return 0;
135 }
136 #else
137 {
138 	/* error slippery */
139 	struct sockaddr_un sa;
140 	char ch;
141 	ber_socklen_t dummy = sizeof(sa);
142 	if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
143 		== AC_SOCKET_ERROR )
144 	{
145 		/* XXX: needs to be replace with ber_stream_read() */
146 		read(s, &ch, 1);
147 		TRACE;
148 		return -1;
149 	}
150 	return 0;
151 }
152 #endif
153 	return -1;
154 }
155 #undef TRACE
156 
157 #ifdef LDAP_PF_LOCAL_SENDMSG
158 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
159 	LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
160 #endif
161 
162 static int
163 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
164 {
165 	int rc;
166 	struct timeval	tv, *opt_tv = NULL;
167 
168 	if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
169 		tv = ld->ld_options.ldo_tm_net;
170 		opt_tv = &tv;
171 	}
172 
173 	oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
174 		s, opt_tv ? tv.tv_sec : -1L, async);
175 
176 	if ( ldap_pvt_ndelay_on(ld, s) == -1 ) 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 ) return -1;
182 
183 #ifdef LDAP_PF_LOCAL_SENDMSG
184 	/* Send a dummy message with access rights. Remote side will
185 	 * obtain our uid/gid by fstat'ing this descriptor. The
186 	 * descriptor permissions must match exactly, and we also
187 	 * send the socket name, which must also match.
188 	 */
189 sendcred:
190 		{
191 			int fds[2];
192 			ber_socklen_t salen = sizeof(*sa);
193 			if (pipe(fds) == 0) {
194 				/* Abandon, noop, has no reply */
195 				struct iovec iov;
196 				struct msghdr msg = {0};
197 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
198 # ifndef CMSG_SPACE
199 # define CMSG_SPACE(len)	(_CMSG_ALIGN( sizeof(struct cmsghdr)) + _CMSG_ALIGN(len) )
200 # endif
201 # ifndef CMSG_LEN
202 # define CMSG_LEN(len)		(_CMSG_ALIGN( sizeof(struct cmsghdr)) + (len) )
203 # endif
204 				union {
205 					struct cmsghdr cm;
206 					unsigned char control[CMSG_SPACE(sizeof(int))];
207 				} control_un;
208 				struct cmsghdr *cmsg;
209 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
210 				msg.msg_name = NULL;
211 				msg.msg_namelen = 0;
212 				iov.iov_base = (char *) abandonPDU;
213 				iov.iov_len = sizeof abandonPDU;
214 				msg.msg_iov = &iov;
215 				msg.msg_iovlen = 1;
216 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
217 				msg.msg_control = control_un.control;
218 				msg.msg_controllen = sizeof( control_un.control );
219 				msg.msg_flags = 0;
220 
221 				cmsg = CMSG_FIRSTHDR( &msg );
222 				cmsg->cmsg_len = CMSG_LEN( sizeof(int) );
223 				cmsg->cmsg_level = SOL_SOCKET;
224 				cmsg->cmsg_type = SCM_RIGHTS;
225 
226 				*((int *)CMSG_DATA(cmsg)) = fds[0];
227 # else
228 				msg.msg_accrights = (char *)fds;
229 				msg.msg_accrightslen = sizeof(int);
230 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
231 				getpeername( s, (struct sockaddr *) sa, &salen );
232 				fchmod( fds[0], S_ISUID|S_IRWXU );
233 				write( fds[1], sa, salen );
234 				sendmsg( s, &msg, 0 );
235 				close(fds[0]);
236 				close(fds[1]);
237 			}
238 		}
239 #endif
240 		return 0;
241 	}
242 
243 	if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
244 
245 #ifdef notyet
246 	if ( async ) return -2;
247 #endif
248 
249 #ifdef HAVE_POLL
250 	{
251 		struct pollfd fd;
252 		int timeout = INFTIM;
253 
254 		if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
255 
256 		fd.fd = s;
257 		fd.events = POLL_WRITE;
258 
259 		do {
260 			fd.revents = 0;
261 			rc = poll( &fd, 1, timeout );
262 		} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
263 			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
264 
265 		if( rc == AC_SOCKET_ERROR ) return rc;
266 
267 		if( fd.revents & POLL_WRITE ) {
268 			if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
269 			if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
270 #ifdef LDAP_PF_LOCAL_SENDMSG
271 			goto sendcred;
272 #else
273 			return ( 0 );
274 #endif
275 		}
276 	}
277 #else
278 	{
279 		fd_set wfds, *z=NULL;
280 
281 #ifdef FD_SETSIZE
282 		if ( s >= FD_SETSIZE ) {
283 			rc = AC_SOCKET_ERROR;
284 			tcp_close( s );
285 			ldap_pvt_set_errno( EMFILE );
286 			return rc;
287 		}
288 #endif
289 		do {
290 			FD_ZERO(&wfds);
291 			FD_SET(s, &wfds );
292 			rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
293 		} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
294 			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
295 
296 		if( rc == AC_SOCKET_ERROR ) return rc;
297 
298 		if ( FD_ISSET(s, &wfds) ) {
299 			if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
300 			if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
301 #ifdef LDAP_PF_LOCAL_SENDMSG
302 			goto sendcred;
303 #else
304 			return ( 0 );
305 #endif
306 		}
307 	}
308 #endif
309 
310 	oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
311 	ldap_pvt_set_errno( ETIMEDOUT );
312 	return ( -1 );
313 }
314 
315 int
316 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
317 {
318 	struct sockaddr_un	server;
319 	ber_socket_t		s;
320 	int			rc;
321 
322 	oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
323 
324 	s = ldap_pvt_socket( ld );
325 	if ( s == AC_SOCKET_INVALID ) {
326 		return -1;
327 	}
328 
329 	if ( path == NULL || path[0] == '\0' ) {
330 		path = LDAPI_SOCK;
331 	} else {
332 		if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
333 			ldap_pvt_set_errno( ENAMETOOLONG );
334 			return -1;
335 		}
336 	}
337 
338 	oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
339 
340 	memset( &server, '\0', sizeof(server) );
341 	server.sun_family = AF_LOCAL;
342 	strcpy( server.sun_path, path );
343 
344 	rc = ldap_pvt_connect(ld, s, &server, async);
345 
346 	if (rc == 0) {
347 		ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
348 	} else {
349 		ldap_pvt_close_socket(ld, s);
350 	}
351 	return rc;
352 }
353 #else
354 static int dummy;
355 #endif /* LDAP_PF_LOCAL */
356