17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f3c070b2Ssp  * Common Development and Distribution License (the "License").
6f3c070b2Ssp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210ec57554Sraf 
227c478bd9Sstevel@tonic-gate /*
23*e1dd0a2fSth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <pwd.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <synch.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include "ns_cache_door.h"
377c478bd9Sstevel@tonic-gate #include <door.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #if defined(PIC) || defined(lint)
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  *
43*e1dd0a2fSth  * Routines that actually performs the door call.
447c478bd9Sstevel@tonic-gate  * Note that we cache a file descriptor.  We do
457c478bd9Sstevel@tonic-gate  * the following to prevent disasters:
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * 1) Never use 0,1 or 2; if we get this from the open
487c478bd9Sstevel@tonic-gate  *    we dup it upwards.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * 2) Set the close on exec flags so descriptor remains available
517c478bd9Sstevel@tonic-gate  *    to child processes.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * 3) Verify that the door is still the same one we had before
547c478bd9Sstevel@tonic-gate  *    by using door_info on the client side.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *	Note that we never close the file descriptor if it isn't one
577c478bd9Sstevel@tonic-gate  *	we allocated; we check this with door info.  The rather tricky
587c478bd9Sstevel@tonic-gate  *	logic is designed to be fast in the normal case (fd is already
597c478bd9Sstevel@tonic-gate  *	allocated and is ok) while handling the case where the application
607c478bd9Sstevel@tonic-gate  *	closed it underneath us or where the nscd dies or re-execs itself
617c478bd9Sstevel@tonic-gate  *	and we're a multi-threaded application.  Note that we cannot protect
627c478bd9Sstevel@tonic-gate  *	the application if it closes the fd and it is multi-threaded.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *  int _cache_trydoorcall(void *dptr, int *bufsize, int *actualsize);
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  *      *dptr           IN: points to arg buffer OUT: points to results buffer
677c478bd9Sstevel@tonic-gate  *      *bufsize        IN: overall size of buffer OUT: overall size of buffer
687c478bd9Sstevel@tonic-gate  *      *actualsize     IN: size of call data OUT: size of return data
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *  Note that *dptr may change if provided space as defined by *bufsize is
717c478bd9Sstevel@tonic-gate  *  inadequate.  In this case the door call mmaps more space and places
727c478bd9Sstevel@tonic-gate  *  the answer there and sets dptr to contain a pointer to the space, which
737c478bd9Sstevel@tonic-gate  *  should be freed with munmap.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  *  Returns 0 if the door call reached the server, -1 if contact was not made.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate extern int errno;
807c478bd9Sstevel@tonic-gate static mutex_t	_door_lock = DEFAULTMUTEX;
81f3c070b2Ssp static	int 		doorfd = -1;
827c478bd9Sstevel@tonic-gate 
83*e1dd0a2fSth /*
84*e1dd0a2fSth  * This function does the first part: ensures a file descriptor is
85*e1dd0a2fSth  * cached and usable.
86*e1dd0a2fSth  */
877c478bd9Sstevel@tonic-gate int
__ns_ldap_trydoorcall_getfd()88*e1dd0a2fSth __ns_ldap_trydoorcall_getfd()
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	static	door_info_t 	real_door;
917c478bd9Sstevel@tonic-gate 	door_info_t 		my_door;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * the first time in we try and open and validate the door.
957c478bd9Sstevel@tonic-gate 	 * the validations are that the door must have been
967c478bd9Sstevel@tonic-gate 	 * created with the name service door cookie and
977c478bd9Sstevel@tonic-gate 	 * that the file attached to the door is owned by root
987c478bd9Sstevel@tonic-gate 	 * and readonly by user, group and other.  If any of these
997c478bd9Sstevel@tonic-gate 	 * validations fail we refuse to use the door.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_door_lock);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate try_again:
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (doorfd == -1) {
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		int		tbc[3];
1097c478bd9Sstevel@tonic-gate 		int		i;
1107c478bd9Sstevel@tonic-gate 		if ((doorfd = open(LDAP_CACHE_DOOR, O_RDONLY, 0))
1117c478bd9Sstevel@tonic-gate 		    == -1) {
1127c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&_door_lock);
113*e1dd0a2fSth 			return (NS_CACHE_NOSERVER);
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		/*
1177c478bd9Sstevel@tonic-gate 		 * dup up the file descriptor if we have 0 - 2
1187c478bd9Sstevel@tonic-gate 		 * to avoid problems with shells stdin/out/err
1197c478bd9Sstevel@tonic-gate 		 */
1207c478bd9Sstevel@tonic-gate 		i = 0;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		while (doorfd < 3) { /* we have a reserved fd */
1237c478bd9Sstevel@tonic-gate 			tbc[i++] = doorfd;
1247c478bd9Sstevel@tonic-gate 			if ((doorfd = dup(doorfd)) < 0) {
1257c478bd9Sstevel@tonic-gate 				while (i--)
126*e1dd0a2fSth 					(void) close(tbc[i]);
1277c478bd9Sstevel@tonic-gate 				doorfd = -1;
1287c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&_door_lock);
129*e1dd0a2fSth 				return (NS_CACHE_NOSERVER);
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		while (i--)
134*e1dd0a2fSth 			(void) close(tbc[i]);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * mark this door descriptor as close on exec
1387c478bd9Sstevel@tonic-gate 		 */
1397c478bd9Sstevel@tonic-gate 		(void) fcntl(doorfd, F_SETFD, FD_CLOEXEC);
1400ec57554Sraf 		if (door_info(doorfd, &real_door) == -1 ||
1410ec57554Sraf 		    (real_door.di_attributes & DOOR_REVOKED) ||
1420ec57554Sraf 		    real_door.di_data != (uintptr_t)LDAP_CACHE_DOOR_COOKIE) {
1437c478bd9Sstevel@tonic-gate 			/*
1447c478bd9Sstevel@tonic-gate 			 * we should close doorfd because we just opened it
1457c478bd9Sstevel@tonic-gate 			 */
1467c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
1477c478bd9Sstevel@tonic-gate 			doorfd = -1;
1487c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&_door_lock);
149*e1dd0a2fSth 			return (NS_CACHE_NOSERVER);
1507c478bd9Sstevel@tonic-gate 		}
1510ec57554Sraf 	} else {
1520ec57554Sraf 		if (door_info(doorfd, &my_door) == -1 ||
1530ec57554Sraf 		    my_door.di_data != (uintptr_t)LDAP_CACHE_DOOR_COOKIE ||
1540ec57554Sraf 		    my_door.di_uniquifier != real_door.di_uniquifier) {
1550ec57554Sraf 			/*
1560ec57554Sraf 			 * don't close it -
1570ec57554Sraf 			 * someone else has clobbered fd
1580ec57554Sraf 			 */
1597c478bd9Sstevel@tonic-gate 			doorfd = -1;
1600ec57554Sraf 			goto try_again;
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		if (my_door.di_attributes & DOOR_REVOKED) {
1647c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
1657c478bd9Sstevel@tonic-gate 			doorfd = -1;	/* try and restart connection */
1667c478bd9Sstevel@tonic-gate 			goto try_again;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_door_lock);
171*e1dd0a2fSth 	return (NS_CACHE_SUCCESS);
172*e1dd0a2fSth }
173*e1dd0a2fSth 
174*e1dd0a2fSth /*
175*e1dd0a2fSth  * This function does the second part: sends a door request to
176*e1dd0a2fSth  * the ldap_cachemgr daemon.
177*e1dd0a2fSth  */
178*e1dd0a2fSth int
__ns_ldap_trydoorcall_send(ldap_data_t ** dptr,int * ndata,int * adata)179*e1dd0a2fSth __ns_ldap_trydoorcall_send(ldap_data_t **dptr, int *ndata, int *adata)
180*e1dd0a2fSth {
181*e1dd0a2fSth 	door_arg_t		param;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	param.rbuf = (char *)*dptr;
1847c478bd9Sstevel@tonic-gate 	param.rsize = *ndata;
1857c478bd9Sstevel@tonic-gate 	param.data_ptr = (char *)*dptr;
1867c478bd9Sstevel@tonic-gate 	param.data_size = *adata;
1877c478bd9Sstevel@tonic-gate 	param.desc_ptr = NULL;
1887c478bd9Sstevel@tonic-gate 	param.desc_num = 0;
1897c478bd9Sstevel@tonic-gate 	if (door_call(doorfd, &param) == -1) {
190*e1dd0a2fSth 		return (NS_CACHE_NOSERVER);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	*adata = (int)param.data_size;
1937c478bd9Sstevel@tonic-gate 	*ndata = (int)param.rsize;
1947c478bd9Sstevel@tonic-gate 	*dptr = (ldap_data_t *)param.data_ptr;
1957c478bd9Sstevel@tonic-gate 	if (*adata == 0 || *dptr == NULL) {
196*e1dd0a2fSth 		return (NS_CACHE_NOSERVER);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	return ((*dptr)->ldap_ret.ldap_return_code);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
202*e1dd0a2fSth /*
203*e1dd0a2fSth  * This function does part 1 and 2: makes sure a file descriptor is
204*e1dd0a2fSth  * available and sends a door request to the ldap_cachemgr daemon.
205*e1dd0a2fSth  */
206*e1dd0a2fSth int
__ns_ldap_trydoorcall(ldap_data_t ** dptr,int * ndata,int * adata)207*e1dd0a2fSth __ns_ldap_trydoorcall(ldap_data_t **dptr, int *ndata, int *adata)
208*e1dd0a2fSth {
209*e1dd0a2fSth 	int rc;
210*e1dd0a2fSth 
211*e1dd0a2fSth 	if ((rc = __ns_ldap_trydoorcall_getfd()) == NS_CACHE_SUCCESS)
212*e1dd0a2fSth 		return (__ns_ldap_trydoorcall_send(dptr, ndata, adata));
213*e1dd0a2fSth 	else
214*e1dd0a2fSth 		return (rc);
215*e1dd0a2fSth }
216*e1dd0a2fSth 
217*e1dd0a2fSth void
__ns_ldap_doorfd_close()218*e1dd0a2fSth __ns_ldap_doorfd_close()
219f3c070b2Ssp {
220f3c070b2Ssp 	(void) mutex_lock(&_door_lock);
221f3c070b2Ssp 	if (doorfd != -1) {
222f3c070b2Ssp 		(void) close(doorfd);
223f3c070b2Ssp 	}
224f3c070b2Ssp 	(void) mutex_unlock(&_door_lock);
225f3c070b2Ssp }
226f3c070b2Ssp 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  *  routine to check if server is already running
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
__ns_ldap_cache_ping()2327c478bd9Sstevel@tonic-gate __ns_ldap_cache_ping()
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	ldap_data_t data;
2357c478bd9Sstevel@tonic-gate 	ldap_data_t *dptr;
2367c478bd9Sstevel@tonic-gate 	int ndata;
2377c478bd9Sstevel@tonic-gate 	int adata;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	data.ldap_call.ldap_callnumber = NULLCALL;
2407c478bd9Sstevel@tonic-gate 	ndata = sizeof (data);
2417c478bd9Sstevel@tonic-gate 	adata = sizeof (data);
2427c478bd9Sstevel@tonic-gate 	dptr = &data;
2437c478bd9Sstevel@tonic-gate 	return (__ns_ldap_trydoorcall(&dptr, &ndata, &adata));
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate #endif /* PIC */
247