xref: /onnv-gate/usr/src/lib/libnisdb/yptol/ttl_utils.c (revision 0:68f95e015346)
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 2004 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 utilities relating to TTL calculation.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <syslog.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <strings.h>
36*0Sstevel@tonic-gate #include <ndbm.h>
37*0Sstevel@tonic-gate #include "ypsym.h"
38*0Sstevel@tonic-gate #include "ypdefs.h"
39*0Sstevel@tonic-gate #include "shim.h"
40*0Sstevel@tonic-gate #include "yptol.h"
41*0Sstevel@tonic-gate #include "../ldap_util.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate  * Constants used in time calculations
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate #define	MILLION 1000000
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * Decs
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate suc_code is_greater_timeval(struct timeval *, struct timeval *);
52*0Sstevel@tonic-gate suc_code add_to_timeval(struct timeval *, int);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /*
55*0Sstevel@tonic-gate  * FUNCTION:	has_entry_expired()
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  * DESCRIPTION:	Determines if an individual entry has expired.
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * INPUTS:	Map control structure for an open map
60*0Sstevel@tonic-gate  *		Entry key
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * OUTPUTS:	TRUE =  Entry has expired or cannot be found this will cause
63*0Sstevel@tonic-gate  *			missing entries to be pulled out of the DIT.
64*0Sstevel@tonic-gate  *		FALSE = Entry has not expired
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate bool_t
has_entry_expired(map_ctrl * map,datum * key)68*0Sstevel@tonic-gate has_entry_expired(map_ctrl *map, datum *key)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	datum ttl;
71*0Sstevel@tonic-gate 	struct timeval	now;
72*0Sstevel@tonic-gate 	struct timeval	old_time;
73*0Sstevel@tonic-gate 	char	*key_name;
74*0Sstevel@tonic-gate 	char *myself = "has_entry_expired";
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if ((map == NULL) || (map->ttl == NULL))
77*0Sstevel@tonic-gate 		return (FALSE);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	/* Get expiry time entry for key */
80*0Sstevel@tonic-gate 	ttl = dbm_fetch(map->ttl, *key);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	if (NULL == ttl.dptr) {
83*0Sstevel@tonic-gate 		/*
84*0Sstevel@tonic-gate 		 * If we failed to get a map expiry key, which must always be
85*0Sstevel@tonic-gate 		 * present, then something is seriously wrong. Try to recreate
86*0Sstevel@tonic-gate 		 * the map.
87*0Sstevel@tonic-gate 		 */
88*0Sstevel@tonic-gate 		if ((key->dsize == strlen(MAP_EXPIRY_KEY)) &&
89*0Sstevel@tonic-gate 			(0 == strncmp(key->dptr, MAP_EXPIRY_KEY, key->dsize))) {
90*0Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR, "Cannot find %s TTL "
91*0Sstevel@tonic-gate 				"for map %s. Will attempt to recreate map",
92*0Sstevel@tonic-gate 				MAP_EXPIRY_KEY, map->map_name);
93*0Sstevel@tonic-gate 			return (TRUE);
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		/*
97*0Sstevel@tonic-gate 		 * Not a problem just no TTL entry for this entry. Maybe it has
98*0Sstevel@tonic-gate 		 * not yet been downloaded. Maybe it will be handled by a
99*0Sstevel@tonic-gate 		 * service other than NIS. Check if the entire map has expired.
100*0Sstevel@tonic-gate 		 * This prevents repeated LDAP reads when requests are made for
101*0Sstevel@tonic-gate 		 * nonexistant entries.
102*0Sstevel@tonic-gate 		 */
103*0Sstevel@tonic-gate 		if (has_map_expired(map)) {
104*0Sstevel@tonic-gate 			/* Kick of a map update */
105*0Sstevel@tonic-gate 			update_map_if_required(map, FALSE);
106*0Sstevel@tonic-gate 		}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		/* Don't update the entry */
109*0Sstevel@tonic-gate 		return (FALSE);
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	if (ttl.dsize != sizeof (struct timeval)) {
113*0Sstevel@tonic-gate 		/*
114*0Sstevel@tonic-gate 		 * Need to malloc some memory before can syslog the key name
115*0Sstevel@tonic-gate 		 * but this may fail. Solution log a simple message first THEn
116*0Sstevel@tonic-gate 		 * a more detailed one if it works.
117*0Sstevel@tonic-gate 		 */
118*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
119*0Sstevel@tonic-gate 			"Invalid TTL key in map %s. error %d",
120*0Sstevel@tonic-gate 					map->map_name, dbm_error(map->ttl));
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		/* Log the key name */
123*0Sstevel@tonic-gate 		key_name = (char *)am(myself, key->dsize + 1);
124*0Sstevel@tonic-gate 		if (NULL == key_name) {
125*0Sstevel@tonic-gate 			logmsg(MSG_NOMEM, LOG_ERR,
126*0Sstevel@tonic-gate 					"Could not alloc memory for keyname");
127*0Sstevel@tonic-gate 		} else {
128*0Sstevel@tonic-gate 			strncpy(key_name, key->dptr, key->dsize);
129*0Sstevel@tonic-gate 			key_name[key->dsize] = '\0';
130*0Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
131*0Sstevel@tonic-gate 						"Key name was %s", key_name);
132*0Sstevel@tonic-gate 			sfree(key_name);
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		/* Update it Anyway */
135*0Sstevel@tonic-gate 		return (TRUE);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/* Get current time */
139*0Sstevel@tonic-gate 	gettimeofday(&now, NULL);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * Because dptr may not be int aligned need to build an int
143*0Sstevel@tonic-gate 	 * out of what it points to or will get a bus error
144*0Sstevel@tonic-gate 	 */
145*0Sstevel@tonic-gate 	bcopy(ttl.dptr, &old_time, sizeof (struct timeval));
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	return (is_greater_timeval(&now, &old_time));
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate /*
151*0Sstevel@tonic-gate  * FUNCTION:	has_map_expired()
152*0Sstevel@tonic-gate  *
153*0Sstevel@tonic-gate  * DESCRIPTION:	Determines if an entire map has expire
154*0Sstevel@tonic-gate  *
155*0Sstevel@tonic-gate  * INPUTS:	Map control structure for an open map
156*0Sstevel@tonic-gate  *
157*0Sstevel@tonic-gate  * OUTPUTS:	TRUE = Map has expired
158*0Sstevel@tonic-gate  *		FALSE  Map has not expired
159*0Sstevel@tonic-gate  *
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate bool_t
has_map_expired(map_ctrl * map)162*0Sstevel@tonic-gate has_map_expired(map_ctrl *map)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	datum key;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	/* Set up datum with magic expiry key */
167*0Sstevel@tonic-gate 	key.dsize = strlen(MAP_EXPIRY_KEY);
168*0Sstevel@tonic-gate 	key.dptr = MAP_EXPIRY_KEY;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/* Call has_entry_expired() with magic map expiry key */
171*0Sstevel@tonic-gate 	return (has_entry_expired(map, &key));
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate  * FUNCTION:	update_entry_ttl()
176*0Sstevel@tonic-gate  *
177*0Sstevel@tonic-gate  * DESCRIPTION:	Updates the TTL for one map entry
178*0Sstevel@tonic-gate  *
179*0Sstevel@tonic-gate  * INPUTS:	Map control structure for an open map
180*0Sstevel@tonic-gate  *		Entry key
181*0Sstevel@tonic-gate  *		Flag indication if TTL should be max, min or random
182*0Sstevel@tonic-gate  *
183*0Sstevel@tonic-gate  * OUTPUTS:	SUCCESS = TTL updated
184*0Sstevel@tonic-gate  *		FAILURE = TTL not updated
185*0Sstevel@tonic-gate  *
186*0Sstevel@tonic-gate  */
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate suc_code
update_entry_ttl(map_ctrl * map,datum * key,TTL_TYPE type)189*0Sstevel@tonic-gate update_entry_ttl(map_ctrl *map, datum *key, TTL_TYPE type)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	datum expire;
192*0Sstevel@tonic-gate 	struct timeval	now;
193*0Sstevel@tonic-gate 	int	ttl;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	/* Get current time */
196*0Sstevel@tonic-gate 	gettimeofday(&now, NULL);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	/* Get TTL from mapping file */
199*0Sstevel@tonic-gate 	ttl = get_ttl_value(map, type);
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (FAILURE == add_to_timeval(&now, ttl))
202*0Sstevel@tonic-gate 		return (FAILURE);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/* Convert time into a datum */
205*0Sstevel@tonic-gate 	expire.dsize = sizeof (struct timeval);
206*0Sstevel@tonic-gate 	expire.dptr = (char *)&now;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* Set expiry time entry for key */
209*0Sstevel@tonic-gate 	errno = 0;
210*0Sstevel@tonic-gate 	if (0 > dbm_store(map->ttl, *key, expire, DBM_REPLACE)) {
211*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not write TTL entry "
212*0Sstevel@tonic-gate 						"(errno=%d)", errno);
213*0Sstevel@tonic-gate 		return (FAILURE);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	return (SUCCESS);
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate /*
220*0Sstevel@tonic-gate  * FUNCTION:	update_map_ttl()
221*0Sstevel@tonic-gate  *
222*0Sstevel@tonic-gate  * DESCRIPTION:	Updates the TTL for entire map. This can be called either with
223*0Sstevel@tonic-gate  *		the map open (map_ctrl DBM pointer set up) or the map closed
224*0Sstevel@tonic-gate  *		(map_ctrl DBM pointers not set). The latter case will occur
225*0Sstevel@tonic-gate  *		when we have just created a new map.
226*0Sstevel@tonic-gate  *
227*0Sstevel@tonic-gate  *		This function must open the TTL map but, in either case, must
228*0Sstevel@tonic-gate  *		return with the map_ctrl in it's original state.
229*0Sstevel@tonic-gate  *
230*0Sstevel@tonic-gate  * INPUTS:	Map control structure for an open map
231*0Sstevel@tonic-gate  *
232*0Sstevel@tonic-gate  * OUTPUTS:	SUCCESS = TTL updated
233*0Sstevel@tonic-gate  *		FAILURE = TTL not updated
234*0Sstevel@tonic-gate  *
235*0Sstevel@tonic-gate  */
236*0Sstevel@tonic-gate suc_code
update_map_ttl(map_ctrl * map)237*0Sstevel@tonic-gate update_map_ttl(map_ctrl *map)
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate 	datum key;
240*0Sstevel@tonic-gate 	bool_t map_was_open = TRUE;
241*0Sstevel@tonic-gate 	suc_code ret;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/* Set up datum with magic expiry key */
244*0Sstevel@tonic-gate 	key.dsize = strlen(MAP_EXPIRY_KEY);
245*0Sstevel@tonic-gate 	key.dptr = MAP_EXPIRY_KEY;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	/* If TTL not open open it */
248*0Sstevel@tonic-gate 	if (NULL == map->ttl) {
249*0Sstevel@tonic-gate 		map->ttl = dbm_open(map->ttl_path, O_RDWR, 0644);
250*0Sstevel@tonic-gate 		if (NULL == map->ttl)
251*0Sstevel@tonic-gate 			return (FAILURE);
252*0Sstevel@tonic-gate 		map_was_open = FALSE;
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	/* Call update_entry_ttl() with magic map expiry key */
256*0Sstevel@tonic-gate 	ret = update_entry_ttl(map, &key, TTL_MIN);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/* If we had to open TTL file close it */
259*0Sstevel@tonic-gate 	if (!map_was_open) {
260*0Sstevel@tonic-gate 		dbm_close(map->ttl);
261*0Sstevel@tonic-gate 		map->ttl_path = NULL;
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	return (ret);
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate  * FUNCTION:	add_to_timeval()
269*0Sstevel@tonic-gate  *
270*0Sstevel@tonic-gate  * DESCRIPTION:	Adds an int to a timeval
271*0Sstevel@tonic-gate  *
272*0Sstevel@tonic-gate  * NOTE :	Seems strange that there is not a library function to do this
273*0Sstevel@tonic-gate  *		if one exists then this function can be removed.
274*0Sstevel@tonic-gate  *
275*0Sstevel@tonic-gate  * NOTE :	Does not handle UNIX clock wrap round but this is a much bigger
276*0Sstevel@tonic-gate  *		problem.
277*0Sstevel@tonic-gate  *
278*0Sstevel@tonic-gate  * INPUTS:	Time value to add to
279*0Sstevel@tonic-gate  *		Time value to add in seconds
280*0Sstevel@tonic-gate  *
281*0Sstevel@tonic-gate  * OUTPUTS:	SUCCESS = Addition successful
282*0Sstevel@tonic-gate  *		FAILURE = Addition failed (probably wrapped)
283*0Sstevel@tonic-gate  *
284*0Sstevel@tonic-gate  */
285*0Sstevel@tonic-gate suc_code
add_to_timeval(struct timeval * t1,int t2)286*0Sstevel@tonic-gate add_to_timeval(struct timeval *t1, int t2)
287*0Sstevel@tonic-gate {
288*0Sstevel@tonic-gate 	long usec;
289*0Sstevel@tonic-gate 	struct timeval oldval;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	oldval.tv_sec = t1->tv_sec;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	/* Add seconds part */
294*0Sstevel@tonic-gate 	t1->tv_sec += t2;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	/* Check for clock wrap */
297*0Sstevel@tonic-gate 	if (!(t1->tv_sec >= oldval.tv_sec)) {
298*0Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
299*0Sstevel@tonic-gate 			"Wrap when adding %d to %d", t2, oldval.tv_sec);
300*0Sstevel@tonic-gate 		return (FAILURE);
301*0Sstevel@tonic-gate 	}
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	return (SUCCESS);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate /*
307*0Sstevel@tonic-gate  * FUNCTION:	is_greater_timeval()
308*0Sstevel@tonic-gate  *
309*0Sstevel@tonic-gate  * DESCRIPTION:	Compares two timevals
310*0Sstevel@tonic-gate  *
311*0Sstevel@tonic-gate  * NOTE :	Seems strange that there is not a library function to do this
312*0Sstevel@tonic-gate  *		if one exists then this function can be removed.
313*0Sstevel@tonic-gate  *
314*0Sstevel@tonic-gate  * INPUTS:	First time value
315*0Sstevel@tonic-gate  *		Time value to compare it with
316*0Sstevel@tonic-gate  *
317*0Sstevel@tonic-gate  * OUTPUTS:	TRUE t1 > t2
318*0Sstevel@tonic-gate  *		FALSE t1 <= t2
319*0Sstevel@tonic-gate  *
320*0Sstevel@tonic-gate  */
321*0Sstevel@tonic-gate suc_code
is_greater_timeval(struct timeval * t1,struct timeval * t2)322*0Sstevel@tonic-gate is_greater_timeval(struct timeval *t1, struct timeval *t2)
323*0Sstevel@tonic-gate {
324*0Sstevel@tonic-gate 	if (t1->tv_sec > t2->tv_sec)
325*0Sstevel@tonic-gate 		return (TRUE);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	if (t1->tv_sec == t2->tv_sec) {
328*0Sstevel@tonic-gate 		if (t1->tv_usec > t2->tv_usec)
329*0Sstevel@tonic-gate 			return (TRUE);
330*0Sstevel@tonic-gate 		else
331*0Sstevel@tonic-gate 			return (FALSE);
332*0Sstevel@tonic-gate 	}
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	return (FALSE);
335*0Sstevel@tonic-gate }
336