xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/os-local.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1*549b59edSchristos /*	$NetBSD: os-local.c,v 1.12 2021/08/14 16:14:56 christos Exp $	*/
260134bf0Slukem 
3098ab8e3Slukem /* os-local.c -- platform-specific domain socket code */
4cb54be06Stron /* $OpenLDAP$ */
5098ab8e3Slukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6098ab8e3Slukem  *
7*549b59edSchristos  * Copyright 1998-2021 The OpenLDAP Foundation.
8098ab8e3Slukem  * All rights reserved.
9098ab8e3Slukem  *
10098ab8e3Slukem  * Redistribution and use in source and binary forms, with or without
11098ab8e3Slukem  * modification, are permitted only as authorized by the OpenLDAP
12098ab8e3Slukem  * Public License.
13098ab8e3Slukem  *
14098ab8e3Slukem  * A copy of this license is available in the file LICENSE in the
15098ab8e3Slukem  * top-level directory of the distribution or, alternatively, at
16098ab8e3Slukem  * <http://www.OpenLDAP.org/license.html>.
17098ab8e3Slukem  */
18098ab8e3Slukem /* Portions Copyright (c) 1995 Regents of the University of Michigan.
19098ab8e3Slukem  * All rights reserved.
20098ab8e3Slukem  */
21098ab8e3Slukem /* Portions (C) Copyright PADL Software Pty Ltd. 1999
22098ab8e3Slukem  * Redistribution and use in source and binary forms, with or without
23098ab8e3Slukem  * modification, are permitted provided that this notice is preserved
24098ab8e3Slukem  * and that due credit is given to PADL Software Pty Ltd. This software
25098ab8e3Slukem  * is provided ``as is'' without express or implied warranty.
26098ab8e3Slukem  */
27098ab8e3Slukem 
28915bea73Schristos #include <sys/cdefs.h>
29*549b59edSchristos __RCSID("$NetBSD: os-local.c,v 1.12 2021/08/14 16:14:56 christos Exp $");
30915bea73Schristos 
31098ab8e3Slukem #include "portable.h"
32098ab8e3Slukem 
33098ab8e3Slukem #ifdef LDAP_PF_LOCAL
34098ab8e3Slukem 
35098ab8e3Slukem #include <stdio.h>
36098ab8e3Slukem 
37098ab8e3Slukem #include <ac/stdlib.h>
38098ab8e3Slukem 
39098ab8e3Slukem #include <ac/errno.h>
40098ab8e3Slukem #include <ac/socket.h>
41098ab8e3Slukem #include <ac/string.h>
42098ab8e3Slukem #include <ac/time.h>
43098ab8e3Slukem #include <ac/unistd.h>
44098ab8e3Slukem 
45098ab8e3Slukem #ifdef HAVE_SYS_STAT_H
46098ab8e3Slukem #include <sys/stat.h>
47098ab8e3Slukem #endif
48098ab8e3Slukem #ifdef HAVE_SYS_UIO_H
49098ab8e3Slukem #include <sys/uio.h>
50098ab8e3Slukem #endif
51098ab8e3Slukem 
52098ab8e3Slukem #ifdef HAVE_IO_H
53098ab8e3Slukem #include <io.h>
54098ab8e3Slukem #endif /* HAVE_IO_H */
555e376103Slukem #ifdef HAVE_FCNTL_H
565e376103Slukem #include <fcntl.h>
575e376103Slukem #endif
58098ab8e3Slukem 
59098ab8e3Slukem #include "ldap-int.h"
60098ab8e3Slukem #include "ldap_defaults.h"
61098ab8e3Slukem 
62098ab8e3Slukem static void
ldap_pvt_set_errno(int err)63098ab8e3Slukem ldap_pvt_set_errno(int err)
64098ab8e3Slukem {
65098ab8e3Slukem 	errno = err;
66098ab8e3Slukem }
67098ab8e3Slukem 
68098ab8e3Slukem static int
ldap_pvt_ndelay_on(LDAP * ld,int fd)69098ab8e3Slukem ldap_pvt_ndelay_on(LDAP *ld, int fd)
70098ab8e3Slukem {
71*549b59edSchristos 	Debug1(LDAP_DEBUG_TRACE, "ldap_ndelay_on: %d\n",fd );
72098ab8e3Slukem 	return ber_pvt_socket_set_nonblock( fd, 1 );
73098ab8e3Slukem }
74098ab8e3Slukem 
75098ab8e3Slukem static int
ldap_pvt_ndelay_off(LDAP * ld,int fd)76098ab8e3Slukem ldap_pvt_ndelay_off(LDAP *ld, int fd)
77098ab8e3Slukem {
78*549b59edSchristos 	Debug1(LDAP_DEBUG_TRACE, "ldap_ndelay_off: %d\n",fd );
79098ab8e3Slukem 	return ber_pvt_socket_set_nonblock( fd, 0 );
80098ab8e3Slukem }
81098ab8e3Slukem 
82098ab8e3Slukem static ber_socket_t
ldap_pvt_socket(LDAP * ld)83098ab8e3Slukem ldap_pvt_socket(LDAP *ld)
84098ab8e3Slukem {
85098ab8e3Slukem 	ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
86*549b59edSchristos 	Debug1(LDAP_DEBUG_TRACE, "ldap_new_socket: %d\n",s );
875e376103Slukem #ifdef FD_CLOEXEC
885e376103Slukem 	fcntl(s, F_SETFD, FD_CLOEXEC);
895e376103Slukem #endif
90098ab8e3Slukem 	return ( s );
91098ab8e3Slukem }
92098ab8e3Slukem 
93098ab8e3Slukem static int
ldap_pvt_close_socket(LDAP * ld,int s)94098ab8e3Slukem ldap_pvt_close_socket(LDAP *ld, int s)
95098ab8e3Slukem {
96*549b59edSchristos 	Debug1(LDAP_DEBUG_TRACE, "ldap_close_socket: %d\n",s );
97098ab8e3Slukem 	return tcp_close(s);
98098ab8e3Slukem }
99098ab8e3Slukem 
100098ab8e3Slukem #undef TRACE
101098ab8e3Slukem #define TRACE do { \
102098ab8e3Slukem 	char ebuf[128]; \
103*549b59edSchristos 	int saved_errno = errno; \
104*549b59edSchristos 	Debug3(LDAP_DEBUG_TRACE, "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
105098ab8e3Slukem 		s, \
106*549b59edSchristos 		saved_errno, \
107*549b59edSchristos 		AC_STRERROR_R(saved_errno, ebuf, sizeof ebuf)); \
108098ab8e3Slukem } while( 0 )
109098ab8e3Slukem 
110098ab8e3Slukem /*
111098ab8e3Slukem  * check the socket for errors after select returned.
112098ab8e3Slukem  */
113098ab8e3Slukem static int
ldap_pvt_is_socket_ready(LDAP * ld,int s)114098ab8e3Slukem ldap_pvt_is_socket_ready(LDAP *ld, int s)
115098ab8e3Slukem {
116*549b59edSchristos 	Debug1(LDAP_DEBUG_TRACE, "ldap_is_sock_ready: %d\n",s );
117098ab8e3Slukem 
118098ab8e3Slukem #if defined( notyet ) /* && defined( SO_ERROR ) */
119098ab8e3Slukem {
120098ab8e3Slukem 	int so_errno;
121098ab8e3Slukem 	ber_socklen_t dummy = sizeof(so_errno);
122098ab8e3Slukem 	if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
123098ab8e3Slukem 		== AC_SOCKET_ERROR )
124098ab8e3Slukem 	{
125098ab8e3Slukem 		return -1;
126098ab8e3Slukem 	}
127098ab8e3Slukem 	if ( so_errno ) {
128098ab8e3Slukem 		ldap_pvt_set_errno(so_errno);
129098ab8e3Slukem 		TRACE;
130098ab8e3Slukem 		return -1;
131098ab8e3Slukem 	}
132098ab8e3Slukem 	return 0;
133098ab8e3Slukem }
134098ab8e3Slukem #else
135098ab8e3Slukem {
136098ab8e3Slukem 	/* error slippery */
137098ab8e3Slukem 	struct sockaddr_un sa;
138098ab8e3Slukem 	char ch;
139098ab8e3Slukem 	ber_socklen_t dummy = sizeof(sa);
140098ab8e3Slukem 	if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
141098ab8e3Slukem 		== AC_SOCKET_ERROR )
142098ab8e3Slukem 	{
143098ab8e3Slukem 		/* XXX: needs to be replace with ber_stream_read() */
14460134bf0Slukem 		(void)read(s, &ch, 1);
145098ab8e3Slukem 		TRACE;
146098ab8e3Slukem 		return -1;
147098ab8e3Slukem 	}
148098ab8e3Slukem 	return 0;
149098ab8e3Slukem }
150098ab8e3Slukem #endif
151098ab8e3Slukem 	return -1;
152098ab8e3Slukem }
153098ab8e3Slukem #undef TRACE
154098ab8e3Slukem 
155098ab8e3Slukem #ifdef LDAP_PF_LOCAL_SENDMSG
156098ab8e3Slukem static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
157098ab8e3Slukem 	LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
158098ab8e3Slukem #endif
159098ab8e3Slukem 
160098ab8e3Slukem static int
ldap_pvt_connect(LDAP * ld,ber_socket_t s,struct sockaddr_un * sa,int async)161098ab8e3Slukem ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
162098ab8e3Slukem {
163098ab8e3Slukem 	int rc;
164098ab8e3Slukem 	struct timeval	tv, *opt_tv = NULL;
165098ab8e3Slukem 
166098ab8e3Slukem 	if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
167098ab8e3Slukem 		tv = ld->ld_options.ldo_tm_net;
168098ab8e3Slukem 		opt_tv = &tv;
169098ab8e3Slukem 	}
170098ab8e3Slukem 
171*549b59edSchristos 	Debug3(LDAP_DEBUG_TRACE,
172*549b59edSchristos 		"ldap_connect_timeout: fd: %d tm: %jd async: %d\n",
173*549b59edSchristos 		s, (intmax_t)(opt_tv ? tv.tv_sec : -1), async);
174098ab8e3Slukem 
175098ab8e3Slukem 	if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
176098ab8e3Slukem 
177098ab8e3Slukem 	if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
178098ab8e3Slukem 		!= AC_SOCKET_ERROR )
179098ab8e3Slukem 	{
180098ab8e3Slukem 		if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
181098ab8e3Slukem 
182098ab8e3Slukem #ifdef LDAP_PF_LOCAL_SENDMSG
183098ab8e3Slukem 	/* Send a dummy message with access rights. Remote side will
184098ab8e3Slukem 	 * obtain our uid/gid by fstat'ing this descriptor. The
185098ab8e3Slukem 	 * descriptor permissions must match exactly, and we also
186098ab8e3Slukem 	 * send the socket name, which must also match.
187098ab8e3Slukem 	 */
188098ab8e3Slukem sendcred:
189098ab8e3Slukem 		{
190098ab8e3Slukem 			int fds[2];
191098ab8e3Slukem 			ber_socklen_t salen = sizeof(*sa);
192098ab8e3Slukem 			if (pipe(fds) == 0) {
193098ab8e3Slukem 				/* Abandon, noop, has no reply */
194098ab8e3Slukem 				struct iovec iov;
195098ab8e3Slukem 				struct msghdr msg = {0};
196098ab8e3Slukem # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
197098ab8e3Slukem # ifndef CMSG_SPACE
198098ab8e3Slukem # define CMSG_SPACE(len)	(_CMSG_ALIGN( sizeof(struct cmsghdr)) + _CMSG_ALIGN(len) )
199098ab8e3Slukem # endif
200098ab8e3Slukem # ifndef CMSG_LEN
201098ab8e3Slukem # define CMSG_LEN(len)		(_CMSG_ALIGN( sizeof(struct cmsghdr)) + (len) )
202098ab8e3Slukem # endif
203098ab8e3Slukem 				union {
204098ab8e3Slukem 					struct cmsghdr cm;
205098ab8e3Slukem 					unsigned char control[CMSG_SPACE(sizeof(int))];
206098ab8e3Slukem 				} control_un;
207098ab8e3Slukem 				struct cmsghdr *cmsg;
208098ab8e3Slukem # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
209098ab8e3Slukem 				msg.msg_name = NULL;
210098ab8e3Slukem 				msg.msg_namelen = 0;
211098ab8e3Slukem 				iov.iov_base = (char *) abandonPDU;
212098ab8e3Slukem 				iov.iov_len = sizeof abandonPDU;
213098ab8e3Slukem 				msg.msg_iov = &iov;
214098ab8e3Slukem 				msg.msg_iovlen = 1;
215098ab8e3Slukem # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
216098ab8e3Slukem 				msg.msg_control = control_un.control;
217098ab8e3Slukem 				msg.msg_controllen = sizeof( control_un.control );
218098ab8e3Slukem 				msg.msg_flags = 0;
219098ab8e3Slukem 
220098ab8e3Slukem 				cmsg = CMSG_FIRSTHDR( &msg );
221098ab8e3Slukem 				cmsg->cmsg_len = CMSG_LEN( sizeof(int) );
222098ab8e3Slukem 				cmsg->cmsg_level = SOL_SOCKET;
223098ab8e3Slukem 				cmsg->cmsg_type = SCM_RIGHTS;
224098ab8e3Slukem 
225098ab8e3Slukem 				*((int *)CMSG_DATA(cmsg)) = fds[0];
226098ab8e3Slukem # else
227098ab8e3Slukem 				msg.msg_accrights = (char *)fds;
228098ab8e3Slukem 				msg.msg_accrightslen = sizeof(int);
229098ab8e3Slukem # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
230098ab8e3Slukem 				getpeername( s, (struct sockaddr *) sa, &salen );
231098ab8e3Slukem 				fchmod( fds[0], S_ISUID|S_IRWXU );
232098ab8e3Slukem 				write( fds[1], sa, salen );
233098ab8e3Slukem 				sendmsg( s, &msg, 0 );
234098ab8e3Slukem 				close(fds[0]);
235098ab8e3Slukem 				close(fds[1]);
236098ab8e3Slukem 			}
237098ab8e3Slukem 		}
238098ab8e3Slukem #endif
239098ab8e3Slukem 		return 0;
240098ab8e3Slukem 	}
241098ab8e3Slukem 
242098ab8e3Slukem 	if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
243098ab8e3Slukem 
244098ab8e3Slukem #ifdef notyet
245098ab8e3Slukem 	if ( async ) return -2;
246098ab8e3Slukem #endif
247098ab8e3Slukem 
248098ab8e3Slukem #ifdef HAVE_POLL
249098ab8e3Slukem 	{
250098ab8e3Slukem 		struct pollfd fd;
251098ab8e3Slukem 		int timeout = INFTIM;
252098ab8e3Slukem 
253098ab8e3Slukem 		if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
254098ab8e3Slukem 
255098ab8e3Slukem 		fd.fd = s;
256098ab8e3Slukem 		fd.events = POLL_WRITE;
257098ab8e3Slukem 
258098ab8e3Slukem 		do {
259098ab8e3Slukem 			fd.revents = 0;
260098ab8e3Slukem 			rc = poll( &fd, 1, timeout );
261098ab8e3Slukem 		} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
262098ab8e3Slukem 			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
263098ab8e3Slukem 
264098ab8e3Slukem 		if( rc == AC_SOCKET_ERROR ) return rc;
265098ab8e3Slukem 
266098ab8e3Slukem 		if( fd.revents & POLL_WRITE ) {
267098ab8e3Slukem 			if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
268098ab8e3Slukem 			if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
269098ab8e3Slukem #ifdef LDAP_PF_LOCAL_SENDMSG
270098ab8e3Slukem 			goto sendcred;
271098ab8e3Slukem #else
272098ab8e3Slukem 			return ( 0 );
273098ab8e3Slukem #endif
274098ab8e3Slukem 		}
275098ab8e3Slukem 	}
276098ab8e3Slukem #else
277098ab8e3Slukem 	{
278098ab8e3Slukem 		fd_set wfds, *z=NULL;
279098ab8e3Slukem 
280098ab8e3Slukem #ifdef FD_SETSIZE
281098ab8e3Slukem 		if ( s >= FD_SETSIZE ) {
282098ab8e3Slukem 			rc = AC_SOCKET_ERROR;
283098ab8e3Slukem 			tcp_close( s );
284098ab8e3Slukem 			ldap_pvt_set_errno( EMFILE );
285098ab8e3Slukem 			return rc;
286098ab8e3Slukem 		}
287098ab8e3Slukem #endif
288098ab8e3Slukem 		do {
289098ab8e3Slukem 			FD_ZERO(&wfds);
290098ab8e3Slukem 			FD_SET(s, &wfds );
291098ab8e3Slukem 			rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
292098ab8e3Slukem 		} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
293098ab8e3Slukem 			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
294098ab8e3Slukem 
295098ab8e3Slukem 		if( rc == AC_SOCKET_ERROR ) return rc;
296098ab8e3Slukem 
297098ab8e3Slukem 		if ( FD_ISSET(s, &wfds) ) {
298098ab8e3Slukem 			if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
299098ab8e3Slukem 			if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
300098ab8e3Slukem #ifdef LDAP_PF_LOCAL_SENDMSG
301098ab8e3Slukem 			goto sendcred;
302098ab8e3Slukem #else
303098ab8e3Slukem 			return ( 0 );
304098ab8e3Slukem #endif
305098ab8e3Slukem 		}
306098ab8e3Slukem 	}
307098ab8e3Slukem #endif
308098ab8e3Slukem 
309*549b59edSchristos 	Debug0(LDAP_DEBUG_TRACE, "ldap_connect_timeout: timed out\n" );
310098ab8e3Slukem 	ldap_pvt_set_errno( ETIMEDOUT );
311098ab8e3Slukem 	return ( -1 );
312098ab8e3Slukem }
313098ab8e3Slukem 
314098ab8e3Slukem int
ldap_connect_to_path(LDAP * ld,Sockbuf * sb,LDAPURLDesc * srv,int async)31560134bf0Slukem ldap_connect_to_path(LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv, int async)
316098ab8e3Slukem {
317098ab8e3Slukem 	struct sockaddr_un	server;
318098ab8e3Slukem 	ber_socket_t		s;
319098ab8e3Slukem 	int			rc;
32060134bf0Slukem 	const char *path = srv->lud_host;
321098ab8e3Slukem 
322*549b59edSchristos 	Debug0(LDAP_DEBUG_TRACE, "ldap_connect_to_path\n" );
323098ab8e3Slukem 
324098ab8e3Slukem 	if ( path == NULL || path[0] == '\0' ) {
325098ab8e3Slukem 		path = LDAPI_SOCK;
326098ab8e3Slukem 	} else {
327098ab8e3Slukem 		if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
328098ab8e3Slukem 			ldap_pvt_set_errno( ENAMETOOLONG );
329098ab8e3Slukem 			return -1;
330098ab8e3Slukem 		}
331098ab8e3Slukem 	}
332098ab8e3Slukem 
333cb54be06Stron 	s = ldap_pvt_socket( ld );
334cb54be06Stron 	if ( s == AC_SOCKET_INVALID ) {
335cb54be06Stron 		return -1;
336cb54be06Stron 	}
337cb54be06Stron 
338*549b59edSchristos 	Debug1(LDAP_DEBUG_TRACE, "ldap_connect_to_path: Trying %s\n", path );
339098ab8e3Slukem 
340098ab8e3Slukem 	memset( &server, '\0', sizeof(server) );
341098ab8e3Slukem 	server.sun_family = AF_LOCAL;
342098ab8e3Slukem 	strcpy( server.sun_path, path );
343098ab8e3Slukem 
344098ab8e3Slukem 	rc = ldap_pvt_connect(ld, s, &server, async);
345098ab8e3Slukem 
346098ab8e3Slukem 	if (rc == 0) {
347cb54be06Stron 		rc = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&server );
34860134bf0Slukem 	}
34960134bf0Slukem 	if ( rc ) {
350098ab8e3Slukem 		ldap_pvt_close_socket(ld, s);
351098ab8e3Slukem 	}
352098ab8e3Slukem 	return rc;
353098ab8e3Slukem }
354098ab8e3Slukem #else
355c8e4d354Schristos static int dummy; /* generate also a warning: 'dummy' defined but not used (at least here) */
356098ab8e3Slukem #endif /* LDAP_PF_LOCAL */
357