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