1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * DESCRIPTION: Contains the map update thread and related code.
31*0Sstevel@tonic-gate */
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <syslog.h>
35*0Sstevel@tonic-gate #include <ndbm.h>
36*0Sstevel@tonic-gate #include <thread.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <strings.h>
39*0Sstevel@tonic-gate #include "ypsym.h"
40*0Sstevel@tonic-gate #include "ypdefs.h"
41*0Sstevel@tonic-gate #include "shim.h"
42*0Sstevel@tonic-gate #include "yptol.h"
43*0Sstevel@tonic-gate #include "../ldap_util.h"
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate /* Enable standard YP code features defined in ypdefs.h */
46*0Sstevel@tonic-gate USE_YP_PREFIX
47*0Sstevel@tonic-gate USE_YP_MASTER_NAME
48*0Sstevel@tonic-gate USE_YP_LAST_MODIFIED
49*0Sstevel@tonic-gate USE_YP_INPUT_FILE
50*0Sstevel@tonic-gate USE_YP_OUTPUT_NAME
51*0Sstevel@tonic-gate USE_YP_DOMAIN_NAME
52*0Sstevel@tonic-gate USE_YP_SECURE
53*0Sstevel@tonic-gate USE_YP_INTERDOMAIN
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate * Decs
57*0Sstevel@tonic-gate */
58*0Sstevel@tonic-gate suc_code update_from_dit(map_ctrl *, datum *);
59*0Sstevel@tonic-gate void * update_thread(void *);
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate * Globals
63*0Sstevel@tonic-gate */
64*0Sstevel@tonic-gate extern pid_t parent_pid;
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate * FUNCTION: update_entry_if_required()
68*0Sstevel@tonic-gate *
69*0Sstevel@tonic-gate * DESCRIPTION: Determines if an entry is to be updated and if it is does the
70*0Sstevel@tonic-gate * update.
71*0Sstevel@tonic-gate *
72*0Sstevel@tonic-gate * GIVEN : Pointer to the open map ctrl
73*0Sstevel@tonic-gate * Pointer to the entry key
74*0Sstevel@tonic-gate *
75*0Sstevel@tonic-gate * RETURNS : SUCCESS = Entry is in a state to be returned to the client
76*0Sstevel@tonic-gate * i.e. either got updated, did not need to be updated or we are
77*0Sstevel@tonic-gate * in a mode where it is acceptable to return out of date
78*0Sstevel@tonic-gate * information.
79*0Sstevel@tonic-gate * FAILURE = Entry need an update but it could not be done.
80*0Sstevel@tonic-gate */
81*0Sstevel@tonic-gate suc_code
update_entry_if_required(map_ctrl * map,datum * key)82*0Sstevel@tonic-gate update_entry_if_required(map_ctrl *map, datum *key)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate /* Only update individual entries if entire map is */
86*0Sstevel@tonic-gate /* not being updated */
87*0Sstevel@tonic-gate if (is_map_updating(map))
88*0Sstevel@tonic-gate return (SUCCESS);
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate * If we are being asked for the order then need to check if
92*0Sstevel@tonic-gate * the map is in need of an update. If it is then fake a
93*0Sstevel@tonic-gate * recent order. The client will then read the map, using
94*0Sstevel@tonic-gate * dbm_firstkey and this will do the update.
95*0Sstevel@tonic-gate */
96*0Sstevel@tonic-gate if (0 == strncmp(key->dptr, yp_last_modified, yp_last_modified_sz)) {
97*0Sstevel@tonic-gate if (has_map_expired(map))
98*0Sstevel@tonic-gate update_timestamp(map->entries);
99*0Sstevel@tonic-gate return (SUCCESS);
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate /* Never update special keys. Have no TTLs */
103*0Sstevel@tonic-gate if (is_special_key(key))
104*0Sstevel@tonic-gate return (SUCCESS);
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate if (!has_entry_expired(map, key))
107*0Sstevel@tonic-gate /* Didn't need an update */
108*0Sstevel@tonic-gate return (SUCCESS);
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate /* Do the update */
111*0Sstevel@tonic-gate return (update_from_dit(map, key));
112*0Sstevel@tonic-gate }
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate * FUNCTION: update_from_dit()
116*0Sstevel@tonic-gate *
117*0Sstevel@tonic-gate * DESCRIPTION: Called to update an entry from the DIT
118*0Sstevel@tonic-gate *
119*0Sstevel@tonic-gate * INPUTS: Map control structure for an open map
120*0Sstevel@tonic-gate * Entry key
121*0Sstevel@tonic-gate *
122*0Sstevel@tonic-gate * OUTPUTS: SUCCESS = Update complete or we are in a mode where it is
123*0Sstevel@tonic-gate * acceptable to return out of date information.
124*0Sstevel@tonic-gate * FAILURE = Update failed
125*0Sstevel@tonic-gate *
126*0Sstevel@tonic-gate */
127*0Sstevel@tonic-gate suc_code
update_from_dit(map_ctrl * map,datum * key)128*0Sstevel@tonic-gate update_from_dit(map_ctrl *map, datum *key)
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate datum dat;
131*0Sstevel@tonic-gate int ret;
132*0Sstevel@tonic-gate suc_code res;
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate * Netgroup maps are a special case we cannot update just one entry so
136*0Sstevel@tonic-gate * update the entire map instead.
137*0Sstevel@tonic-gate */
138*0Sstevel@tonic-gate if ((0 == strcmp(map->map_name, NETGROUP_BYHOST)) ||
139*0Sstevel@tonic-gate (0 == strcmp(map->map_name, NETGROUP_BYUSER))) {
140*0Sstevel@tonic-gate return (update_map_if_required(map, FALSE));
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate /* Read entry from the DIT */
144*0Sstevel@tonic-gate ret = read_from_dit(map->map_name, map->domain, key, &dat);
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate /* Check that we got something */
147*0Sstevel@tonic-gate if (NULL == dat.dptr) {
148*0Sstevel@tonic-gate if (0 == ret) {
149*0Sstevel@tonic-gate /*
150*0Sstevel@tonic-gate * In a mode where it is acceptable to return out of
151*0Sstevel@tonic-gate * date information.
152*0Sstevel@tonic-gate */
153*0Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_INFO,
154*0Sstevel@tonic-gate "LDAP inaccessible returning old information");
155*0Sstevel@tonic-gate return (SUCCESS);
156*0Sstevel@tonic-gate } else {
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate * In a mode where it is not acceptable to return out
159*0Sstevel@tonic-gate * of date information.
160*0Sstevel@tonic-gate *
161*0Sstevel@tonic-gate * If the error positviely indicates that there is no
162*0Sstevel@tonic-gate * such entry delete it. For errors where object may
163*0Sstevel@tonic-gate * still exist in the DIT leave it.
164*0Sstevel@tonic-gate */
165*0Sstevel@tonic-gate if (MAP_NO_MATCHING_KEY == ret) {
166*0Sstevel@tonic-gate /*
167*0Sstevel@tonic-gate * Don't log errors. If the entry was not
168*0Sstevel@tonic-gate * already present then no problem. The user
169*0Sstevel@tonic-gate * just asked us for a non existant entry.
170*0Sstevel@tonic-gate */
171*0Sstevel@tonic-gate dbm_delete(map->entries, *key);
172*0Sstevel@tonic-gate dbm_delete(map->ttl, *key);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate return (FAILURE);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate /* Write it to DBM */
179*0Sstevel@tonic-gate res = dbm_store(map->entries, *key, dat, DBM_REPLACE);
180*0Sstevel@tonic-gate sfree(dat.dptr);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate if (SUCCESS != res)
183*0Sstevel@tonic-gate return (FAILURE);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /* Update TTL */
186*0Sstevel@tonic-gate update_entry_ttl(map, key, TTL_RUNNING);
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate return (SUCCESS);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate * FUNCTION: update_map_if_required()
193*0Sstevel@tonic-gate *
194*0Sstevel@tonic-gate * DESCRIPTION: Called to update an entire map if it is out of date. Map ctrl
195*0Sstevel@tonic-gate * must be locked before this is called. This handles checking if
196*0Sstevel@tonic-gate * the map is already being updated. It is important that this is
197*0Sstevel@tonic-gate * done atomically with obtaining the maps update lock.
198*0Sstevel@tonic-gate *
199*0Sstevel@tonic-gate * INPUTS: Map control structure for an open map
200*0Sstevel@tonic-gate * Flag indication if we should wait for completion
201*0Sstevel@tonic-gate *
202*0Sstevel@tonic-gate * OUTPUTS: SUCCESS = Map update initiated
203*0Sstevel@tonic-gate * FAILURE = Map update not initiated
204*0Sstevel@tonic-gate */
205*0Sstevel@tonic-gate suc_code
update_map_if_required(map_ctrl * map,bool_t wait)206*0Sstevel@tonic-gate update_map_if_required(map_ctrl *map, bool_t wait)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate thread_t tid;
209*0Sstevel@tonic-gate map_ctrl *new_map;
210*0Sstevel@tonic-gate suc_code res;
211*0Sstevel@tonic-gate long flags;
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate if (wait) {
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate * Actually get the lock
216*0Sstevel@tonic-gate *
217*0Sstevel@tonic-gate * May block so unlock map_ctrl while it is done
218*0Sstevel@tonic-gate */
219*0Sstevel@tonic-gate unlock_map_ctrl(map);
220*0Sstevel@tonic-gate res = lock_map_update(map);
221*0Sstevel@tonic-gate lock_map_ctrl(map);
222*0Sstevel@tonic-gate if (SUCCESS != res) {
223*0Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
224*0Sstevel@tonic-gate "Could not lock map %s for update",
225*0Sstevel@tonic-gate map->map_name);
226*0Sstevel@tonic-gate return (FAILURE);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate } else {
229*0Sstevel@tonic-gate /* If not waiting try to get the lock */
230*0Sstevel@tonic-gate switch (try_lock_map_update(map)) {
231*0Sstevel@tonic-gate case 0:
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate * We got the lock. Continue to start an update.
234*0Sstevel@tonic-gate */
235*0Sstevel@tonic-gate break;
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate case EBUSY:
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate * Some one else got the lock. OK they are
240*0Sstevel@tonic-gate * doing the update so we can just return.
241*0Sstevel@tonic-gate */
242*0Sstevel@tonic-gate return (SUCCESS);
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate default:
245*0Sstevel@tonic-gate /*
246*0Sstevel@tonic-gate * Some serious problem with lock.
247*0Sstevel@tonic-gate */
248*0Sstevel@tonic-gate return (FAILURE);
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate /*
253*0Sstevel@tonic-gate * If we get here are holding the update lock. Make a final check that
254*0Sstevel@tonic-gate * nobody beat us to the map update while we were getting it.
255*0Sstevel@tonic-gate */
256*0Sstevel@tonic-gate if (!has_map_expired(map)) {
257*0Sstevel@tonic-gate /* A big waste of time. Somebody else did the update */
258*0Sstevel@tonic-gate unlock_map_update(map);
259*0Sstevel@tonic-gate return (SUCCESS);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate * We got the lock and nobody beat us to doing the update. Start our
264*0Sstevel@tonic-gate * own update.
265*0Sstevel@tonic-gate *
266*0Sstevel@tonic-gate * Thread will free the update lock when update is complete.
267*0Sstevel@tonic-gate */
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate * Make a copy of the map_ctrl structure so the update thread has an
272*0Sstevel@tonic-gate * independent version to work with. Note: Must not be on stack.
273*0Sstevel@tonic-gate *
274*0Sstevel@tonic-gate * On exit the update thread must free this.
275*0Sstevel@tonic-gate */
276*0Sstevel@tonic-gate new_map = dup_map_ctrl(map);
277*0Sstevel@tonic-gate if (NULL == new_map) {
278*0Sstevel@tonic-gate unlock_map_update(map);
279*0Sstevel@tonic-gate return (FAILURE);
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate /*
283*0Sstevel@tonic-gate * While thread is running unlock map so other processes can
284*0Sstevel@tonic-gate * execute non update related accesses
285*0Sstevel@tonic-gate */
286*0Sstevel@tonic-gate unlock_map_ctrl(map);
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate flags = THR_BOUND | THR_NEW_LWP;
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate /*
291*0Sstevel@tonic-gate * If we are not going to thr_join then need to create detached.
292*0Sstevel@tonic-gate * This prevents a zombie being left when nobody joins us.
293*0Sstevel@tonic-gate */
294*0Sstevel@tonic-gate if (!wait && (getpid() == parent_pid))
295*0Sstevel@tonic-gate flags |= THR_DETACHED;
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate /* Kick off update thread */
298*0Sstevel@tonic-gate if (0 != thr_create(NULL, NULL, update_thread, new_map,
299*0Sstevel@tonic-gate flags, &tid)) {
300*0Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
301*0Sstevel@tonic-gate "Could not create NIS update thread");
302*0Sstevel@tonic-gate free_map_ctrl(new_map);
303*0Sstevel@tonic-gate unlock_map_update(map);
304*0Sstevel@tonic-gate if (SUCCESS != lock_map_ctrl(map))
305*0Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
306*0Sstevel@tonic-gate "Could not acquire update lock for %s", map->map_name);
307*0Sstevel@tonic-gate return (FAILURE);
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate if (wait) {
311*0Sstevel@tonic-gate /* May block but no problem map_ctrl is already unlocked. */
312*0Sstevel@tonic-gate thr_join(tid, NULL, NULL);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate
315*0Sstevel@tonic-gate /* Re acquire lock */
316*0Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) {
317*0Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
318*0Sstevel@tonic-gate "Could not re-acquire lock for %s", map->map_name);
319*0Sstevel@tonic-gate return (FAILURE);
320*0Sstevel@tonic-gate }
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gate return (SUCCESS);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate * FUNCTION: update_thread()
327*0Sstevel@tonic-gate *
328*0Sstevel@tonic-gate * DESCRIPTION: The update thread this is called to update an entire NIS map.
329*0Sstevel@tonic-gate * if several NIS maps are found to be out of date several
330*0Sstevel@tonic-gate * instances of this may be running at the same time.
331*0Sstevel@tonic-gate *
332*0Sstevel@tonic-gate * Since we are using a duplicate map_ctrl we do not have to lock
333*0Sstevel@tonic-gate * it. If we did would end up using the same mutex as the parent
334*0Sstevel@tonic-gate * map ctrl an possibly deadlocking.
335*0Sstevel@tonic-gate *
336*0Sstevel@tonic-gate * INPUTS: Map handle (because we need access to name and lock)
337*0Sstevel@tonic-gate *
338*0Sstevel@tonic-gate * OUTPUTS: None exits when finished.
339*0Sstevel@tonic-gate */
340*0Sstevel@tonic-gate
341*0Sstevel@tonic-gate void *
update_thread(void * arg)342*0Sstevel@tonic-gate update_thread(void *arg)
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate void *ret = (void *)-1;
345*0Sstevel@tonic-gate map_ctrl *map;
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate /* Cast argument pointer to correct type */
348*0Sstevel@tonic-gate map = (map_ctrl *)arg;
349*0Sstevel@tonic-gate
350*0Sstevel@tonic-gate /* Actually do the work */
351*0Sstevel@tonic-gate if (SUCCESS == update_map_from_dit(map, FALSE))
352*0Sstevel@tonic-gate ret = 0;
353*0Sstevel@tonic-gate
354*0Sstevel@tonic-gate /* Update complete or failed */
355*0Sstevel@tonic-gate unlock_map_update(map);
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate /* Free up duplicate copy of the map_ctrl */
358*0Sstevel@tonic-gate free_map_ctrl(map);
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate thr_exit(ret);
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate /*
364*0Sstevel@tonic-gate * FUNCTION : is_special_key()
365*0Sstevel@tonic-gate *
366*0Sstevel@tonic-gate * DESCRIPTION: Works out if a given key is one of the special ones. We just
367*0Sstevel@tonic-gate * check for the "YP_" prefix. This is not 100% safe but if
368*0Sstevel@tonic-gate * valid keys with a "YP_" prefix exist in the DIT then a lot of
369*0Sstevel@tonic-gate * other parts of NIS wont work.
370*0Sstevel@tonic-gate */
371*0Sstevel@tonic-gate bool_t
is_special_key(datum * key)372*0Sstevel@tonic-gate is_special_key(datum *key)
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate if (0 == strncmp(key->dptr, yp_prefix, yp_prefix_sz))
375*0Sstevel@tonic-gate return (TRUE);
376*0Sstevel@tonic-gate
377*0Sstevel@tonic-gate return (FALSE);
378*0Sstevel@tonic-gate }
379