1e1dd0a2fSth /*
2e1dd0a2fSth  * CDDL HEADER START
3e1dd0a2fSth  *
4e1dd0a2fSth  * The contents of this file are subject to the terms of the
5e1dd0a2fSth  * Common Development and Distribution License (the "License").
6e1dd0a2fSth  * You may not use this file except in compliance with the License.
7e1dd0a2fSth  *
8e1dd0a2fSth  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e1dd0a2fSth  * or http://www.opensolaris.org/os/licensing.
10e1dd0a2fSth  * See the License for the specific language governing permissions
11e1dd0a2fSth  * and limitations under the License.
12e1dd0a2fSth  *
13e1dd0a2fSth  * When distributing Covered Code, include this CDDL HEADER in each
14e1dd0a2fSth  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e1dd0a2fSth  * If applicable, add the following below this CDDL HEADER, with the
16e1dd0a2fSth  * fields enclosed by brackets "[]" replaced with your own identifying
17e1dd0a2fSth  * information: Portions Copyright [yyyy] [name of copyright owner]
18e1dd0a2fSth  *
19e1dd0a2fSth  * CDDL HEADER END
20e1dd0a2fSth  */
21e1dd0a2fSth /*
22b57459abSJulian Pullen  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23e1dd0a2fSth  * Use is subject to license terms.
24*ab618543SJohn Levon  *
25*ab618543SJohn Levon  * Copyright 2018 Joyent, Inc.
26e1dd0a2fSth  */
27e1dd0a2fSth 
28e1dd0a2fSth #include <strings.h>
29e1dd0a2fSth #include <stdlib.h>
30e1dd0a2fSth #include <syslog.h>
31e1dd0a2fSth #include <errno.h>
32e1dd0a2fSth #include <libintl.h>
33e1dd0a2fSth #include <door.h>
34e1dd0a2fSth #include <sys/types.h>
35e1dd0a2fSth #include <sys/stat.h>
36e1dd0a2fSth #include <fcntl.h>
37e1dd0a2fSth #include <procfs.h>
38*ab618543SJohn Levon #include <pthread.h>
39e1dd0a2fSth #include "cachemgr.h"
40e1dd0a2fSth 
41e1dd0a2fSth extern admin_t	current_admin;
42e1dd0a2fSth 
43e1dd0a2fSth #define	CLEANUP_WAIT_TIME 60
44e1dd0a2fSth 
45e1dd0a2fSth typedef enum cleanup_type {
46e1dd0a2fSth 	CLEANUP_ALL	= 1,
47e1dd0a2fSth 	CLEANUP_BY_PID	= 2
48e1dd0a2fSth } cleanup_type_t;
49e1dd0a2fSth 
50e1dd0a2fSth typedef struct cleanup_op {
51e1dd0a2fSth 	pid_t		pid;
52e1dd0a2fSth 	cleanup_type_t	type;
53e1dd0a2fSth } cleanup_op_t;
54e1dd0a2fSth 
55e1dd0a2fSth typedef struct main_nscd_struct {
56e1dd0a2fSth 	pid_t		pid;			/* main nscd pid */
57e1dd0a2fSth 	thread_t	tid;			/* main nscd tid */
58e1dd0a2fSth 	int		in_progress;		/* A main nscd thread is */
59e1dd0a2fSth 						/* waiting for change or */
60e1dd0a2fSth 						/* copying data */
61e1dd0a2fSth 	int		is_waiting_cleanup;	/* A main nscd thread is */
62e1dd0a2fSth 						/* waiting for another main */
63e1dd0a2fSth 						/* nscd thread to be cleaned */
64e1dd0a2fSth 						/* up */
65e1dd0a2fSth } main_nscd_t;
66e1dd0a2fSth 
67e1dd0a2fSth static chg_info_t chg = { DEFAULTMUTEX, DEFAULTCV, 0, NULL, NULL, NULL, 0 };
68e1dd0a2fSth 
69e1dd0a2fSth static main_nscd_t chg_main_nscd = {0, 0, 0, 0};
70e1dd0a2fSth static mutex_t chg_main_nscd_lock = DEFAULTMUTEX;
71e1dd0a2fSth static cond_t chg_main_nscd_cv = DEFAULTCV;
72e1dd0a2fSth 
73e1dd0a2fSth /*
74e1dd0a2fSth  * The cookie of the configuration and its mutex
75e1dd0a2fSth  */
76e1dd0a2fSth static ldap_get_chg_cookie_t config_cookie = {0, 0};
77e1dd0a2fSth static mutex_t config_cookie_lock = DEFAULTMUTEX;
78e1dd0a2fSth 
79e1dd0a2fSth static void cleanup_thread_by_pid(pid_t pid);
80e1dd0a2fSth 
81e1dd0a2fSth ldap_get_chg_cookie_t
chg_config_cookie_get(void)82e1dd0a2fSth chg_config_cookie_get(void)
83e1dd0a2fSth {
84e1dd0a2fSth 	ldap_get_chg_cookie_t cookie;
85e1dd0a2fSth 	(void) mutex_lock(&config_cookie_lock);
86e1dd0a2fSth 	cookie = config_cookie;
87e1dd0a2fSth 	(void) mutex_unlock(&config_cookie_lock);
88e1dd0a2fSth 	return (cookie);
89e1dd0a2fSth }
90e1dd0a2fSth 
91e1dd0a2fSth static void
chg_config_cookie_increment_seq_num(void)92e1dd0a2fSth chg_config_cookie_increment_seq_num(void)
93e1dd0a2fSth {
94e1dd0a2fSth 	(void) mutex_lock(&config_cookie_lock);
95e1dd0a2fSth 	config_cookie.seq_num++;
96e1dd0a2fSth 	(void) mutex_unlock(&config_cookie_lock);
97e1dd0a2fSth }
98e1dd0a2fSth 
99e1dd0a2fSth void
chg_config_cookie_set(ldap_get_chg_cookie_t * cookie)100e1dd0a2fSth chg_config_cookie_set(ldap_get_chg_cookie_t *cookie)
101e1dd0a2fSth {
102e1dd0a2fSth 	(void) mutex_lock(&config_cookie_lock);
103e1dd0a2fSth 	config_cookie.mgr_pid = cookie->mgr_pid;
104e1dd0a2fSth 	config_cookie.seq_num = cookie->seq_num;
105e1dd0a2fSth 	(void) mutex_unlock(&config_cookie_lock);
106e1dd0a2fSth }
107e1dd0a2fSth static boolean_t
chg_cookie_equal(ldap_get_chg_cookie_t * c1,ldap_get_chg_cookie_t * c2)108e1dd0a2fSth chg_cookie_equal(ldap_get_chg_cookie_t *c1, ldap_get_chg_cookie_t *c2)
109e1dd0a2fSth {
110e1dd0a2fSth 	if (c1->mgr_pid == c2->mgr_pid && c1->seq_num == c2->seq_num)
111e1dd0a2fSth 		return (B_TRUE);
112e1dd0a2fSth 	else
113e1dd0a2fSth 		return (B_FALSE);
114e1dd0a2fSth }
115e1dd0a2fSth /*
116e1dd0a2fSth  * Create a node in the list and output the node. The caller can NOT free it.
117e1dd0a2fSth  */
118e1dd0a2fSth static  int
waiting_list_add(chg_info_t * info,pid_t pid,thread_t tid,waiting_list_t ** wlp)119e1dd0a2fSth waiting_list_add(chg_info_t *info, pid_t pid, thread_t tid,
120e1dd0a2fSth     waiting_list_t **wlp)
121e1dd0a2fSth {
122e1dd0a2fSth 
123e1dd0a2fSth 	waiting_list_t	*wl;
124e1dd0a2fSth 
125e1dd0a2fSth 	*wlp = NULL;
126e1dd0a2fSth 
127e1dd0a2fSth 	if ((wl = (waiting_list_t *)calloc(1, sizeof (waiting_list_t)))
128e1dd0a2fSth 	    == NULL) {
129e1dd0a2fSth 		logit("waiting_list_add: No memory. pid %ld tid %d\n",
130e1dd0a2fSth 		    pid, tid);
131e1dd0a2fSth 		return (CHG_NO_MEMORY);
132e1dd0a2fSth 	}
133e1dd0a2fSth 
134e1dd0a2fSth 	wl->pid = pid;
135e1dd0a2fSth 	wl->tid = tid;
136e1dd0a2fSth 
137e1dd0a2fSth 	if (info->chg_w_first == NULL) {
138e1dd0a2fSth 		info->chg_w_first = wl;
139e1dd0a2fSth 		info->chg_w_last = wl;
140e1dd0a2fSth 	} else {
141e1dd0a2fSth 		info->chg_w_last->next = wl;
142e1dd0a2fSth 		wl->prev = info->chg_w_last;
143e1dd0a2fSth 		info->chg_w_last = wl;
144e1dd0a2fSth 	}
145e1dd0a2fSth 	*wlp = wl;
146e1dd0a2fSth 	return (CHG_SUCCESS);
147e1dd0a2fSth }
148e1dd0a2fSth 
149e1dd0a2fSth /*
150e1dd0a2fSth  * Find a node with matching tid in the list and remove it from the list.
151e1dd0a2fSth  */
152e1dd0a2fSth static int
waiting_list_delete(chg_info_t * info,thread_t tid)153e1dd0a2fSth waiting_list_delete(chg_info_t *info, thread_t tid)
154e1dd0a2fSth {
155e1dd0a2fSth 	waiting_list_t	*wl;
156e1dd0a2fSth 
157e1dd0a2fSth 	for (wl = info->chg_w_first; wl != NULL; wl = wl->next) {
158e1dd0a2fSth 		if (wl->tid == tid) {
159e1dd0a2fSth 			if (wl->next == NULL) {
160e1dd0a2fSth 				if (wl->prev == NULL) {
161e1dd0a2fSth 					info->chg_w_first = NULL;
162e1dd0a2fSth 					info->chg_w_last = NULL;
163e1dd0a2fSth 				} else {
164e1dd0a2fSth 					wl->prev->next = NULL;
165e1dd0a2fSth 					info->chg_w_last =  wl->prev;
166e1dd0a2fSth 				}
167e1dd0a2fSth 			} else {
168e1dd0a2fSth 				if (wl->prev == NULL) {
169e1dd0a2fSth 					wl->next->prev = NULL;
170e1dd0a2fSth 					info->chg_w_first = wl->next;
171e1dd0a2fSth 				} else {
172e1dd0a2fSth 					wl->prev->next = wl->next;
173e1dd0a2fSth 					wl->next->prev = wl->prev;
174e1dd0a2fSth 				}
175e1dd0a2fSth 			}
176e1dd0a2fSth 			free(wl);
177e1dd0a2fSth 			return (CHG_SUCCESS);
178e1dd0a2fSth 		}
179e1dd0a2fSth 	}
180e1dd0a2fSth 	return (CHG_NOT_FOUND_IN_WAITING_LIST);
181e1dd0a2fSth }
182e1dd0a2fSth 
183e1dd0a2fSth /*
184e1dd0a2fSth  * Delete the thread from the waiting list and remove data when the list
185e1dd0a2fSth  * is empty.
186e1dd0a2fSth  */
187e1dd0a2fSth static void
waiting_list_cleanup(chg_info_t * chg,thread_t tid)188e1dd0a2fSth waiting_list_cleanup(chg_info_t *chg, thread_t tid)
189e1dd0a2fSth {
190e1dd0a2fSth 	int	rc;
191e1dd0a2fSth 
192e1dd0a2fSth 	rc = waiting_list_delete(chg, tid);
193e1dd0a2fSth 
194e1dd0a2fSth 	if (rc == CHG_SUCCESS && chg->chg_w_first == NULL) {
195e1dd0a2fSth 		free(chg->chg_data);
196e1dd0a2fSth 		chg->chg_data = NULL;
197e1dd0a2fSth 		chg->chg_wakeup = 0;
198e1dd0a2fSth 	}
199e1dd0a2fSth }
200e1dd0a2fSth 
201e1dd0a2fSth /*
202e1dd0a2fSth  * Set flag by pid so it can be cleaned up.
203e1dd0a2fSth  */
204e1dd0a2fSth static void
waiting_list_set_cleanup(chg_info_t * info,pid_t pid)205e1dd0a2fSth waiting_list_set_cleanup(chg_info_t *info, pid_t pid)
206e1dd0a2fSth {
207e1dd0a2fSth 	waiting_list_t	*wl;
208e1dd0a2fSth 
209e1dd0a2fSth 	for (wl = info->chg_w_first; wl != NULL; wl = wl->next) {
210e1dd0a2fSth 		if (wl->pid == pid) {
211e1dd0a2fSth 			wl->cleanup = 1;
212e1dd0a2fSth 			break;
213e1dd0a2fSth 		}
214e1dd0a2fSth 	}
215e1dd0a2fSth }
216e1dd0a2fSth 
217e1dd0a2fSth /*
218e1dd0a2fSth  * Return: 1 - door client is dead, 0 - door client is alive
219e1dd0a2fSth  */
220e1dd0a2fSth static int
door_client_dead(void)221e1dd0a2fSth door_client_dead(void)
222e1dd0a2fSth {
223e1dd0a2fSth 	ucred_t *uc = NULL;
224e1dd0a2fSth 	int	rc;
225e1dd0a2fSth 
226e1dd0a2fSth 	if (door_ucred(&uc) == -1 && errno == EINVAL) {
227e1dd0a2fSth 		rc = 1;
228e1dd0a2fSth 	} else {
229e1dd0a2fSth 		rc = 0;
230e1dd0a2fSth 	}
231e1dd0a2fSth 	if (uc)
232e1dd0a2fSth 		ucred_free(uc);
233e1dd0a2fSth 
234e1dd0a2fSth 	return (rc);
235e1dd0a2fSth }
236e1dd0a2fSth 
237e1dd0a2fSth /*
238e1dd0a2fSth  * This function handles GETSTATUSCHANGE call from main nscd.
239e1dd0a2fSth  * The call can be a START op or STOP op. A cookie is sent from main nscd too.
240e1dd0a2fSth  * The static global variable main_nscd keeps record of pid, tid and some flags.
241e1dd0a2fSth  * If the thread is door_return(), main_nscd.pid, main_nscd.tid are set to 0.
242e1dd0a2fSth  * When the call is START op, it checks if main_nscd.pid is 0. If it is, it
243e1dd0a2fSth  * proceeds to wait for the change notification. If it's not, which means
244e1dd0a2fSth  * another main nscd handling thread is still around. It sends broadcast to
245e1dd0a2fSth  * clean up that thread and wait until the cleanup is done then proceeds to
246e1dd0a2fSth  * wait for the change notification. If same main nscd sends START op
247e1dd0a2fSth  * repeatedly, it'll be rejected.
248e1dd0a2fSth  * It also checks the cookie from main nscd. If it's not the same as
249e1dd0a2fSth  * ldap_cachemgr's cookie, door returns config change.
250e1dd0a2fSth  * If the door call is STOP op, it creates a thread to clean up main nscd START
251e1dd0a2fSth  * thread so it won't be blocking.
252e1dd0a2fSth  * In waiting for the change notification phase, the thread is waken up by
253e1dd0a2fSth  * the notification threads or by the cleanup threads.
254e1dd0a2fSth  * If it's a notification, it copies data to the stack then door return.
255e1dd0a2fSth  * If it's a cleanup, door_client_dead() is called to verify it then
256e1dd0a2fSth  * door return.
257e1dd0a2fSth  */
258e1dd0a2fSth int
chg_get_statusChange(LineBuf * info,ldap_call_t * in,pid_t nscd_pid)259e1dd0a2fSth chg_get_statusChange(LineBuf *info, ldap_call_t *in, pid_t nscd_pid)
260e1dd0a2fSth {
261e1dd0a2fSth 	int	rc = CHG_SUCCESS, another_main_nscd_thread_alive = 0;
262e1dd0a2fSth 	int	len, return_now;
263e1dd0a2fSth 	thread_t this_tid = thr_self();
264e1dd0a2fSth 	waiting_list_t	*wl = NULL;
265e1dd0a2fSth 	ldap_get_change_out_t *cout;
266e1dd0a2fSth 	ldap_get_chg_cookie_t cookie;
267e1dd0a2fSth 
268e1dd0a2fSth 	info->str = NULL;
269e1dd0a2fSth 	info->len = 0;
270e1dd0a2fSth 
271e1dd0a2fSth 	if (in->ldap_u.get_change.op == NS_STATUS_CHANGE_OP_START) {
272e1dd0a2fSth 
273e1dd0a2fSth 		(void) mutex_lock(&chg_main_nscd_lock);
274e1dd0a2fSth 		if (chg_main_nscd.pid != 0) {
275e1dd0a2fSth 			if (nscd_pid != chg_main_nscd.pid) {
276e1dd0a2fSth 				/*
277e1dd0a2fSth 				 * This is the case that nscd doesn't shut down
278e1dd0a2fSth 				 * properly(e.g. core) and STOP op is not sent,
279e1dd0a2fSth 				 * the thread handling it is still around and
280e1dd0a2fSth 				 * not cleaned up yet.
281e1dd0a2fSth 				 * Test if the thread is still alive.
282e1dd0a2fSth 				 * If it is, clean it up.
283e1dd0a2fSth 				 * For thr_kill, if sig is 0, a validity check
284e1dd0a2fSth 				 * is done for the existence of the target
285e1dd0a2fSth 				 * thread; no signal is sent.
286e1dd0a2fSth 				 */
287e1dd0a2fSth 				if (thr_kill(chg_main_nscd.tid, 0) == 0) {
288e1dd0a2fSth 					another_main_nscd_thread_alive = 1;
289e1dd0a2fSth 					cleanup_thread_by_pid(
290e1dd0a2fSth 					    chg_main_nscd.pid);
291e1dd0a2fSth 				}
292e1dd0a2fSth 			} else if (chg_main_nscd.in_progress ||
293e1dd0a2fSth 			    chg_main_nscd.is_waiting_cleanup) {
294e1dd0a2fSth 				/*
295e1dd0a2fSth 				 * Same nscd pid can only send door call
296e1dd0a2fSth 				 * one at a time and wait for ldap_cachemgr to
297e1dd0a2fSth 				 * return change data. If it's the same pid
298e1dd0a2fSth 				 * again, it's an nscd error.
299e1dd0a2fSth 				 */
300e1dd0a2fSth 				(void) mutex_unlock(&chg_main_nscd_lock);
301e1dd0a2fSth 				return (CHG_NSCD_REPEATED_CALL);
302e1dd0a2fSth 			}
303e1dd0a2fSth 		}
304e1dd0a2fSth 		/*
305e1dd0a2fSth 		 * Wait for another thread to be cleaned up if it's alive.
306e1dd0a2fSth 		 * After that this cond var is waken up.
307e1dd0a2fSth 		 */
308e1dd0a2fSth 		if (another_main_nscd_thread_alive) {
309e1dd0a2fSth 			while (chg_main_nscd.in_progress) {
310e1dd0a2fSth 				chg_main_nscd.is_waiting_cleanup = 1;
311e1dd0a2fSth 				(void) cond_wait(&chg_main_nscd_cv,
312e1dd0a2fSth 				    &chg_main_nscd_lock);
313e1dd0a2fSth 			}
314e1dd0a2fSth 		}
315e1dd0a2fSth 
316e1dd0a2fSth 		/*
317e1dd0a2fSth 		 * Replace pid and tid and set the flag.
318e1dd0a2fSth 		 */
319e1dd0a2fSth 		chg_main_nscd.is_waiting_cleanup = 0;
320e1dd0a2fSth 		chg_main_nscd.pid = nscd_pid;
321e1dd0a2fSth 		chg_main_nscd.tid = this_tid;
322e1dd0a2fSth 		chg_main_nscd.in_progress = 1;
323e1dd0a2fSth 		(void) mutex_unlock(&chg_main_nscd_lock);
324e1dd0a2fSth 
325e1dd0a2fSth 		cookie = chg_config_cookie_get();
326e1dd0a2fSth 
327e1dd0a2fSth 		if (!chg_cookie_equal(&cookie, &in->ldap_u.get_change.cookie)) {
328e1dd0a2fSth 			/*
329e1dd0a2fSth 			 * different cookie, set new cookie and
330e1dd0a2fSth 			 * return door call right away
331e1dd0a2fSth 			 */
332e1dd0a2fSth 			len = sizeof (ldap_get_change_out_t);
333e1dd0a2fSth 			if ((cout = calloc(1, len)) == NULL) {
334e1dd0a2fSth 				rc = CHG_NO_MEMORY;
335e1dd0a2fSth 			} else {
336e1dd0a2fSth 				cout->type = NS_STATUS_CHANGE_TYPE_CONFIG;
337e1dd0a2fSth 				cout->cookie = cookie;
338e1dd0a2fSth 				info->str = (char *)cout;
339e1dd0a2fSth 				info->len = len;
340e1dd0a2fSth 			}
341e1dd0a2fSth 
342e1dd0a2fSth 		} else {
343e1dd0a2fSth 			(void) mutex_lock(&chg.chg_lock);
344e1dd0a2fSth 
345e1dd0a2fSth 			/* wait for the change notification */
346e1dd0a2fSth 			rc = waiting_list_add(&chg, nscd_pid, this_tid, &wl);
347e1dd0a2fSth 			if (rc == CHG_SUCCESS) {
348e1dd0a2fSth 				return_now = 0;
349e1dd0a2fSth 				while (!chg.chg_wakeup) {
350e1dd0a2fSth 					if (wl->cleanup ||
351e1dd0a2fSth 					    door_client_dead()) {
352e1dd0a2fSth 						return_now = 1;
353e1dd0a2fSth 						break;
354e1dd0a2fSth 					}
355e1dd0a2fSth 					(void) cond_wait(&chg.chg_cv,
356e1dd0a2fSth 					    &chg.chg_lock);
357e1dd0a2fSth 				}
358e1dd0a2fSth 				/* Check if door client is still alive again */
359e1dd0a2fSth 				if (!return_now && !wl->cleanup &&
360e1dd0a2fSth 				    !door_client_dead()) {
361e1dd0a2fSth 					/* copy data to buffer */
362e1dd0a2fSth 					if ((info->str = malloc(
363e1dd0a2fSth 					    chg.chg_data_size)) == NULL) {
364e1dd0a2fSth 						rc = CHG_NO_MEMORY;
365e1dd0a2fSth 					} else {
366e1dd0a2fSth 						(void) memcpy(info->str,
367e1dd0a2fSth 						    chg.chg_data,
368e1dd0a2fSth 						    chg.chg_data_size);
369e1dd0a2fSth 						info->len = chg.chg_data_size;
370e1dd0a2fSth 					}
371e1dd0a2fSth 				}
372e1dd0a2fSth 				waiting_list_cleanup(&chg, this_tid);
373e1dd0a2fSth 			}
374e1dd0a2fSth 			(void) mutex_unlock(&chg.chg_lock);
375e1dd0a2fSth 		}
376e1dd0a2fSth 
377e1dd0a2fSth 
378e1dd0a2fSth 		/*
379e1dd0a2fSth 		 * Reset pid, tid and flag, send wakeup signal.
380e1dd0a2fSth 		 */
381e1dd0a2fSth 		(void) mutex_lock(&chg_main_nscd_lock);
382e1dd0a2fSth 		chg_main_nscd.pid = 0;
383e1dd0a2fSth 		chg_main_nscd.tid = 0;
384e1dd0a2fSth 		chg_main_nscd.in_progress = 0;
385e1dd0a2fSth 		if (chg_main_nscd.is_waiting_cleanup)
386e1dd0a2fSth 			(void) cond_broadcast(&chg_main_nscd_cv);
387e1dd0a2fSth 
388e1dd0a2fSth 		(void) mutex_unlock(&chg_main_nscd_lock);
389e1dd0a2fSth 
390e1dd0a2fSth 	} else if (in->ldap_u.get_change.op == NS_STATUS_CHANGE_OP_STOP) {
391e1dd0a2fSth 
392e1dd0a2fSth 		cleanup_thread_by_pid(nscd_pid);
393e1dd0a2fSth 		rc = CHG_SUCCESS;
394e1dd0a2fSth 
395e1dd0a2fSth 	} else {
396e1dd0a2fSth 		rc = CHG_INVALID_PARAM;
397e1dd0a2fSth 	}
398e1dd0a2fSth 	if (rc == CHG_EXCEED_MAX_THREADS)
399e1dd0a2fSth 		cleanup_thread_by_pid(0);
400e1dd0a2fSth 
401e1dd0a2fSth 	return (rc);
402e1dd0a2fSth }
403e1dd0a2fSth 
404e1dd0a2fSth /*
405e1dd0a2fSth  * This function copies the header and data stream to the buffer
406e1dd0a2fSth  * then send broadcast to wake up the chg_get_statusChange() threads.
407e1dd0a2fSth  */
408e1dd0a2fSth int
chg_notify_statusChange(char * str)409e1dd0a2fSth chg_notify_statusChange(char *str)
410e1dd0a2fSth {
411e1dd0a2fSth 	ldap_get_change_out_t *cout = (ldap_get_change_out_t *)str;
412e1dd0a2fSth 
413e1dd0a2fSth 	cout->cookie = chg_config_cookie_get();
414e1dd0a2fSth 
415e1dd0a2fSth 	(void) mutex_lock(&chg.chg_lock);
416e1dd0a2fSth 	if (chg.chg_w_first != NULL && chg.chg_wakeup == 0) {
417e1dd0a2fSth 
418e1dd0a2fSth 		if (chg.chg_data) {
419e1dd0a2fSth 			free(chg.chg_data);
420e1dd0a2fSth 			chg.chg_data = NULL;
421e1dd0a2fSth 		}
422e1dd0a2fSth 
423e1dd0a2fSth 		chg.chg_data = str;
424e1dd0a2fSth 
425e1dd0a2fSth 		if (cout->type == NS_STATUS_CHANGE_TYPE_CONFIG)
426e1dd0a2fSth 			chg.chg_data_size = sizeof (ldap_get_change_out_t);
427e1dd0a2fSth 		else
428e1dd0a2fSth 			/* NS_STATUS_CHANGE_TYPE_SERVER */
429e1dd0a2fSth 			chg.chg_data_size = sizeof (ldap_get_change_out_t) -
430e1dd0a2fSth 			    sizeof (int) + cout->data_size;
431e1dd0a2fSth 
432e1dd0a2fSth 		chg.chg_wakeup = 1;
433e1dd0a2fSth 		(void) cond_broadcast(&chg.chg_cv);
434e1dd0a2fSth 	}
435e1dd0a2fSth 	(void) mutex_unlock(&chg.chg_lock);
436e1dd0a2fSth 
437e1dd0a2fSth 	return (CHG_SUCCESS);
438e1dd0a2fSth }
439e1dd0a2fSth 
440e1dd0a2fSth /*
441e1dd0a2fSth  * This is called when the configuration is refreshed.
442e1dd0a2fSth  * The new configuration is different from the current one, a notification
443e1dd0a2fSth  * is sent tochg_get_statusChange() threads.
444e1dd0a2fSth  */
445e1dd0a2fSth void
chg_test_config_change(ns_config_t * new,int * change_status)446e1dd0a2fSth chg_test_config_change(ns_config_t *new, int *change_status)
447e1dd0a2fSth {
448e1dd0a2fSth 	int	changed = 0;
449e1dd0a2fSth 	LineBuf	new_cfg, cur_cfg;
450e1dd0a2fSth 	ns_ldap_error_t *errp = NULL;
451e1dd0a2fSth 	ldap_config_out_t *new_out, *cur_out;
452e1dd0a2fSth 	ldap_get_change_out_t	*cout;
453e1dd0a2fSth 
454e1dd0a2fSth 	(void) memset(&new_cfg, 0, sizeof (LineBuf));
455e1dd0a2fSth 	(void) memset(&cur_cfg, 0, sizeof (LineBuf));
456e1dd0a2fSth 	/*
457e1dd0a2fSth 	 * Flatten the config data of the newly downloaded config and
458e1dd0a2fSth 	 * current default config and compare both.
459e1dd0a2fSth 	 */
460b57459abSJulian Pullen 	if ((errp = __ns_ldap_LoadDoorInfo(&new_cfg, NULL, new, 0)) != NULL) {
461e1dd0a2fSth 		__ns_ldap_freeError(&errp);
462e1dd0a2fSth 		/* error, assume the config is changed */
463e1dd0a2fSth 		changed = 1;
464b57459abSJulian Pullen 	} else if ((errp = __ns_ldap_LoadDoorInfo(&cur_cfg, NULL, NULL, 0))
465e1dd0a2fSth 	    != NULL) {
466e1dd0a2fSth 		__ns_ldap_freeError(&errp);
467e1dd0a2fSth 		/* error, assume the config is changed */
468e1dd0a2fSth 		changed = 1;
469e1dd0a2fSth 	}
470e1dd0a2fSth 	if (changed == 0) {
471e1dd0a2fSth 		new_out = (ldap_config_out_t *)new_cfg.str;
472e1dd0a2fSth 		cur_out = (ldap_config_out_t *)cur_cfg.str;
473e1dd0a2fSth 		if (strcmp(new_out->config_str, cur_out->config_str) != 0) {
474e1dd0a2fSth 			changed = 1;
475e1dd0a2fSth 			if (current_admin.debug_level >= DBG_PROFILE_REFRESH) {
476e1dd0a2fSth 				logit("config changed.\n");
477e1dd0a2fSth 			}
478e1dd0a2fSth 		}
479e1dd0a2fSth 	}
480e1dd0a2fSth 	if (cur_cfg.str)
481e1dd0a2fSth 		free(cur_cfg.str);
482e1dd0a2fSth 	if (new_cfg.str)
483e1dd0a2fSth 		free(new_cfg.str);
484e1dd0a2fSth 
485e1dd0a2fSth 	if (changed) {
486e1dd0a2fSth 
487e1dd0a2fSth 		if ((cout = calloc(1, sizeof (ldap_get_change_out_t)))
488e1dd0a2fSth 		    == NULL) {
489e1dd0a2fSth 			logit("chg_test_config_change: No Memory\n");
490e1dd0a2fSth 		} else {
491e1dd0a2fSth 			/*
492e1dd0a2fSth 			 * Replace the currentdefault config with the new
493e1dd0a2fSth 			 * config
494e1dd0a2fSth 			 */
495e1dd0a2fSth 			__s_api_init_config(new);
496e1dd0a2fSth 			chg_config_cookie_increment_seq_num();
497e1dd0a2fSth 			cout->type = NS_STATUS_CHANGE_TYPE_CONFIG;
498e1dd0a2fSth 			/*
499e1dd0a2fSth 			 * cout->cookie is set by
500e1dd0a2fSth 			 * chg_notify_statusChange
501e1dd0a2fSth 			 */
502e1dd0a2fSth 			(void) chg_notify_statusChange((char *)cout);
503e1dd0a2fSth 		}
504e1dd0a2fSth 	} else {
505e1dd0a2fSth 		__s_api_destroy_config(new);
506e1dd0a2fSth 	}
507e1dd0a2fSth 
508e1dd0a2fSth 	*change_status = changed;
509e1dd0a2fSth }
510e1dd0a2fSth 
511e1dd0a2fSth /*
512e1dd0a2fSth  * Wake up chg_get_statusChange() threads to clean up the threads
513e1dd0a2fSth  * that main nscd doesn't exist on the other of door anymore or
514e1dd0a2fSth  * the thread is marked as cleanup.
515e1dd0a2fSth  */
516e1dd0a2fSth static void
cleanup_threads(chg_info_t * chg,pid_t pid,cleanup_type_t type)517e1dd0a2fSth cleanup_threads(chg_info_t *chg, pid_t pid, cleanup_type_t type)
518e1dd0a2fSth {
519e1dd0a2fSth 	(void) mutex_lock(&chg->chg_lock);
520e1dd0a2fSth 	if (type == CLEANUP_BY_PID)
521e1dd0a2fSth 		waiting_list_set_cleanup(chg, pid);
522e1dd0a2fSth 	/*
523e1dd0a2fSth 	 * wake up threads without setting chg.chg_wakeup.
524e1dd0a2fSth 	 * It's for cleanup purpose, not for notifying changes.
525e1dd0a2fSth 	 */
526e1dd0a2fSth 	(void) cond_broadcast(&chg->chg_cv);
527e1dd0a2fSth 	(void) mutex_unlock(&chg->chg_lock);
528e1dd0a2fSth }
529e1dd0a2fSth /*
530e1dd0a2fSth  * If arg is NULL, it loops forever,
531e1dd0a2fSth  * else it calls cleanup_threads once and exits.
532e1dd0a2fSth  */
533e1dd0a2fSth void *
chg_cleanup_waiting_threads(void * arg)534e1dd0a2fSth chg_cleanup_waiting_threads(void *arg)
535e1dd0a2fSth {
536e1dd0a2fSth 	cleanup_op_t *op = (cleanup_op_t *)arg;
537e1dd0a2fSth 	cleanup_type_t type = 0;
538e1dd0a2fSth 	pid_t	pid;
539e1dd0a2fSth 	int	always = 1, waiting;
540e1dd0a2fSth 
541*ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "chg_cleanup_thr");
542*ab618543SJohn Levon 
543e1dd0a2fSth 	if (op == NULL) {
544e1dd0a2fSth 		waiting = 1;
545e1dd0a2fSth 		type = CLEANUP_ALL;
546e1dd0a2fSth 		pid = 0;
547e1dd0a2fSth 	} else {
548e1dd0a2fSth 		waiting = 0;
549e1dd0a2fSth 		type = op->type;
550e1dd0a2fSth 		pid = op->pid;
551e1dd0a2fSth 	}
552e1dd0a2fSth 
553e1dd0a2fSth 	while (always) {
554e1dd0a2fSth 		if (waiting)
555e1dd0a2fSth 			(void) sleep(CLEANUP_WAIT_TIME);
556e1dd0a2fSth 		cleanup_threads(&chg, pid, type);
557e1dd0a2fSth 		if (!waiting)
558e1dd0a2fSth 			break;
559e1dd0a2fSth 	}
560e1dd0a2fSth 
561e1dd0a2fSth 	if (op)
562e1dd0a2fSth 		free(op);
563e1dd0a2fSth 
564e1dd0a2fSth 	thr_exit(NULL);
565e1dd0a2fSth 	return (NULL);
566e1dd0a2fSth }
567e1dd0a2fSth /*
568e1dd0a2fSth  * The door server thead which has the door client pid will be marked
569e1dd0a2fSth  * as to be clean up. If pid is 0, no marking and just clean up all.
570e1dd0a2fSth  */
571e1dd0a2fSth static void
cleanup_thread_by_pid(pid_t pid)572e1dd0a2fSth cleanup_thread_by_pid(pid_t pid)
573e1dd0a2fSth {
574e1dd0a2fSth 	cleanup_op_t *op;
575e1dd0a2fSth 
576e1dd0a2fSth 	if ((op = malloc(sizeof (cleanup_op_t))) == NULL)
577e1dd0a2fSth 		return;
578e1dd0a2fSth 
579e1dd0a2fSth 	op->pid = pid;
580e1dd0a2fSth 	/* clean up all if pid is 0 */
581e1dd0a2fSth 	if (pid == 0)
582e1dd0a2fSth 		op->type = CLEANUP_ALL;
583e1dd0a2fSth 	else
584e1dd0a2fSth 		op->type = CLEANUP_BY_PID;
585e1dd0a2fSth 
586e1dd0a2fSth 	if (thr_create(NULL, 0, chg_cleanup_waiting_threads,
587e1dd0a2fSth 	    (void *)op, THR_BOUND|THR_DETACHED, NULL) != 0) {
588e1dd0a2fSth 		free(op);
589e1dd0a2fSth 		logit("thr_create failed for cleanup_thread_by_pid(%ld)\n",
590e1dd0a2fSth 		    pid);
591e1dd0a2fSth 	}
592e1dd0a2fSth }
593e1dd0a2fSth 
594e1dd0a2fSth /*
595e1dd0a2fSth  * Output a psinfo of an nscd process with process id pid
596e1dd0a2fSth  * Return: 0  - Can't find the process or it's not nscd
597e1dd0a2fSth  *         1  - psinfo found
598e1dd0a2fSth  * Note: If info is NULL, returns 0 or 1 only and no output from info.
599e1dd0a2fSth  */
600e1dd0a2fSth static int
get_nscd_psinfo(pid_t pid,psinfo_t * info)601e1dd0a2fSth get_nscd_psinfo(pid_t pid, psinfo_t *info)
602e1dd0a2fSth {
603e1dd0a2fSth 	psinfo_t	pinfo;
604e1dd0a2fSth 	char		fname[MAXPATHLEN];
605e1dd0a2fSth 	ssize_t		ret;
606e1dd0a2fSth 	int		fd;
607e1dd0a2fSth 
608e1dd0a2fSth 	if (snprintf(fname, MAXPATHLEN, "/proc/%d/psinfo", pid) > 0) {
609e1dd0a2fSth 		if ((fd = open(fname,  O_RDONLY)) >= 0) {
610e1dd0a2fSth 			ret = read(fd, &pinfo, sizeof (psinfo_t));
611e1dd0a2fSth 			(void) close(fd);
612e1dd0a2fSth 			if ((ret == sizeof (psinfo_t)) &&
613e1dd0a2fSth 			    (strcmp(pinfo.pr_fname, "nscd") == 0)) {
614e1dd0a2fSth 				if (info)
615e1dd0a2fSth 					*info = pinfo;
616e1dd0a2fSth 				return (1);
617e1dd0a2fSth 			}
618e1dd0a2fSth 		}
619e1dd0a2fSth 	}
620e1dd0a2fSth 	return (0);
621e1dd0a2fSth }
622e1dd0a2fSth /*
623e1dd0a2fSth  * If the parent process is nscd and euid is 0, it's a peruser nscd.
624e1dd0a2fSth  */
625e1dd0a2fSth static int
is_peruser_nscd(pid_t pid)626e1dd0a2fSth is_peruser_nscd(pid_t pid)
627e1dd0a2fSth {
628e1dd0a2fSth 	pid_t	ppid;
629e1dd0a2fSth 	psinfo_t pinfo;
630e1dd0a2fSth 
631e1dd0a2fSth 	if (get_nscd_psinfo(pid, &pinfo)) {
632e1dd0a2fSth 		ppid = pinfo.pr_ppid;
633e1dd0a2fSth 		if (get_nscd_psinfo(ppid, &pinfo) && pinfo.pr_euid == 0)
634e1dd0a2fSth 			/*
635e1dd0a2fSth 			 * get psinfo of parent forker nscd
636e1dd0a2fSth 			 */
637e1dd0a2fSth 			return (1);
638e1dd0a2fSth 		else
639e1dd0a2fSth 			return (0);
640e1dd0a2fSth 	} else {
641e1dd0a2fSth 		return (0);
642e1dd0a2fSth 	}
643e1dd0a2fSth }
644e1dd0a2fSth /*
645e1dd0a2fSth  * Check if the door client making door call is a nscd or peruser nscd and
646e1dd0a2fSth  * output door client's pid.
647e1dd0a2fSth  */
648e1dd0a2fSth int
chg_is_called_from_nscd_or_peruser_nscd(char * dc_str,pid_t * pidp)649e1dd0a2fSth chg_is_called_from_nscd_or_peruser_nscd(char *dc_str, pid_t *pidp)
650e1dd0a2fSth {
651e1dd0a2fSth 	int	rc;
652e1dd0a2fSth 	uid_t	euid;
653e1dd0a2fSth 	pid_t	pid;
654e1dd0a2fSth 	ucred_t	*uc = NULL;
655e1dd0a2fSth 
656e1dd0a2fSth 	if (door_ucred(&uc) != 0) {
657e1dd0a2fSth 		rc = errno;
658e1dd0a2fSth 		logit("door_ucred() call failed %s\n", strerror(rc));
659e1dd0a2fSth 		return (0);
660e1dd0a2fSth 	}
661e1dd0a2fSth 	euid = ucred_geteuid(uc);
662e1dd0a2fSth 	pid = *pidp = ucred_getpid(uc);
663e1dd0a2fSth 
664e1dd0a2fSth 	if ((euid == 0 && is_called_from_nscd(pid)) ||
665e1dd0a2fSth 	    is_peruser_nscd(pid)) {
666e1dd0a2fSth 		if (current_admin.debug_level >= DBG_ALL)
667e1dd0a2fSth 			logit("ldap_cachemgr received %s call from pid %ld, "
668e1dd0a2fSth 			    "uid %u, euid %u\n", dc_str, pid,
669e1dd0a2fSth 			    ucred_getruid(uc), euid);
670e1dd0a2fSth 		rc = 1;
671e1dd0a2fSth 	} else {
672e1dd0a2fSth 		if (current_admin.debug_level >= DBG_CANT_FIND)
673e1dd0a2fSth 			logit("%s call failed(cred): caller pid %ld, uid %u, "
674e1dd0a2fSth 			    "euid %u\n", dc_str, pid,
675e1dd0a2fSth 			    ucred_getruid(uc), euid);
676e1dd0a2fSth 
677e1dd0a2fSth 		rc = 0;
678e1dd0a2fSth 	}
679e1dd0a2fSth 
680e1dd0a2fSth 	ucred_free(uc);
681e1dd0a2fSth 
682e1dd0a2fSth 	return (rc);
683e1dd0a2fSth }
684