1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <stdio.h>
32 #include <synch.h>
33 #include <sys/param.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include "ns_cache_door.h"
37 #include <door.h>
38 
39 #if defined(PIC) || defined(lint)
40 
41 /*
42  *
43  * Routine that actually performs the door call.
44  * Note that we cache a file descriptor.  We do
45  * the following to prevent disasters:
46  *
47  * 1) Never use 0,1 or 2; if we get this from the open
48  *    we dup it upwards.
49  *
50  * 2) Set the close on exec flags so descriptor remains available
51  *    to child processes.
52  *
53  * 3) Verify that the door is still the same one we had before
54  *    by using door_info on the client side.
55  *
56  *	Note that we never close the file descriptor if it isn't one
57  *	we allocated; we check this with door info.  The rather tricky
58  *	logic is designed to be fast in the normal case (fd is already
59  *	allocated and is ok) while handling the case where the application
60  *	closed it underneath us or where the nscd dies or re-execs itself
61  *	and we're a multi-threaded application.  Note that we cannot protect
62  *	the application if it closes the fd and it is multi-threaded.
63  *
64  *  int _cache_trydoorcall(void *dptr, int *bufsize, int *actualsize);
65  *
66  *      *dptr           IN: points to arg buffer OUT: points to results buffer
67  *      *bufsize        IN: overall size of buffer OUT: overall size of buffer
68  *      *actualsize     IN: size of call data OUT: size of return data
69  *
70  *  Note that *dptr may change if provided space as defined by *bufsize is
71  *  inadequate.  In this case the door call mmaps more space and places
72  *  the answer there and sets dptr to contain a pointer to the space, which
73  *  should be freed with munmap.
74  *
75  *  Returns 0 if the door call reached the server, -1 if contact was not made.
76  *
77  */
78 
79 extern int errno;
80 static mutex_t	_door_lock = DEFAULTMUTEX;
81 static	int 		doorfd = -1;
82 
83 int
84 __ns_ldap_trydoorcall(ldap_data_t **dptr, int *ndata, int *adata)
85 {
86 	static	door_info_t 	real_door;
87 	door_info_t 		my_door;
88 	door_arg_t		param;
89 
90 	/*
91 	 * the first time in we try and open and validate the door.
92 	 * the validations are that the door must have been
93 	 * created with the name service door cookie and
94 	 * that the file attached to the door is owned by root
95 	 * and readonly by user, group and other.  If any of these
96 	 * validations fail we refuse to use the door.
97 	 */
98 
99 	(void) mutex_lock(&_door_lock);
100 
101 try_again:
102 
103 	if (doorfd == -1) {
104 
105 		int		tbc[3];
106 		int		i;
107 		if ((doorfd = open(LDAP_CACHE_DOOR, O_RDONLY, 0))
108 		    == -1) {
109 			(void) mutex_unlock(&_door_lock);
110 			return (NOSERVER);
111 		}
112 
113 		/*
114 		 * dup up the file descriptor if we have 0 - 2
115 		 * to avoid problems with shells stdin/out/err
116 		 */
117 		i = 0;
118 
119 		while (doorfd < 3) { /* we have a reserved fd */
120 			tbc[i++] = doorfd;
121 			if ((doorfd = dup(doorfd)) < 0) {
122 				while (i--)
123 				    (void) close(tbc[i]);
124 				doorfd = -1;
125 				(void) mutex_unlock(&_door_lock);
126 				return (NOSERVER);
127 			}
128 		}
129 
130 		while (i--)
131 		    (void) close(tbc[i]);
132 
133 		/*
134 		 * mark this door descriptor as close on exec
135 		 */
136 		(void) fcntl(doorfd, F_SETFD, FD_CLOEXEC);
137 		if (door_info(doorfd, &real_door) == -1 ||
138 		    (real_door.di_attributes & DOOR_REVOKED) ||
139 		    real_door.di_data != (uintptr_t)LDAP_CACHE_DOOR_COOKIE) {
140 			/*
141 			 * we should close doorfd because we just opened it
142 			 */
143 			(void) close(doorfd);
144 			doorfd = -1;
145 			(void) mutex_unlock(&_door_lock);
146 			return (NOSERVER);
147 		}
148 	} else {
149 		if (door_info(doorfd, &my_door) == -1 ||
150 		    my_door.di_data != (uintptr_t)LDAP_CACHE_DOOR_COOKIE ||
151 		    my_door.di_uniquifier != real_door.di_uniquifier) {
152 			/*
153 			 * don't close it -
154 			 * someone else has clobbered fd
155 			 */
156 			doorfd = -1;
157 			goto try_again;
158 		}
159 
160 		if (my_door.di_attributes & DOOR_REVOKED) {
161 			(void) close(doorfd);
162 			doorfd = -1;	/* try and restart connection */
163 			goto try_again;
164 		}
165 	}
166 
167 	(void) mutex_unlock(&_door_lock);
168 
169 	param.rbuf = (char *)*dptr;
170 	param.rsize = *ndata;
171 	param.data_ptr = (char *)*dptr;
172 	param.data_size = *adata;
173 	param.desc_ptr = NULL;
174 	param.desc_num = 0;
175 	if (door_call(doorfd, &param) == -1) {
176 		return (NOSERVER);
177 	}
178 	*adata = (int)param.data_size;
179 	*ndata = (int)param.rsize;
180 	*dptr = (ldap_data_t *)param.data_ptr;
181 	if (*adata == 0 || *dptr == NULL) {
182 		return (NOSERVER);
183 	}
184 
185 	return ((*dptr)->ldap_ret.ldap_return_code);
186 }
187 
188 #pragma fini(_doorfd_close)
189 static void
190 _doorfd_close()
191 {
192 	(void) mutex_lock(&_door_lock);
193 	if (doorfd != -1) {
194 		(void) close(doorfd);
195 	}
196 	(void) mutex_unlock(&_door_lock);
197 }
198 
199 /*
200  *  routine to check if server is already running
201  */
202 
203 int
204 __ns_ldap_cache_ping()
205 {
206 	ldap_data_t data;
207 	ldap_data_t *dptr;
208 	int ndata;
209 	int adata;
210 
211 	data.ldap_call.ldap_callnumber = NULLCALL;
212 	ndata = sizeof (data);
213 	adata = sizeof (data);
214 	dptr = &data;
215 	return (__ns_ldap_trydoorcall(&dptr, &ndata, &adata));
216 }
217 
218 #endif /* PIC */
219