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