xref: /onnv-gate/usr/src/lib/libnisdb/yptol/shim.c (revision 2189:bbaf8c7f3afd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2189Ssdussud  * Common Development and Distribution License (the "License").
6*2189Ssdussud  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2189Ssdussud  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * DESCRIPTION: Contains the top level shim hook functions. These must have
300Sstevel@tonic-gate  *		identical interfaces to the equivalent standard dbm calls.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *		Unfortunately many of these will do a copy of a datum structure
330Sstevel@tonic-gate  *		on return. This is a side effect of the original DBM function
340Sstevel@tonic-gate  *		being written to pass structures rather than pointers.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * NOTE :	There is a major bug/feature in dbm. A key obtained by
370Sstevel@tonic-gate  *		dbm_nextkey() of dbm_firstkey() cannot be passed to dbm_store().
380Sstevel@tonic-gate  *		When the store occurs dbm's internal memory get's reorganized
390Sstevel@tonic-gate  *		and the static strings pointed to by the key are destroyed. The
400Sstevel@tonic-gate  *		data is then stored in the wrong place. We attempt to get round
410Sstevel@tonic-gate  *		this by dbm_firstkey() and dbm_nextkey() making a copy of the
420Sstevel@tonic-gate  *		key data in malloced memory. This is freed when map_ctrl is
430Sstevel@tonic-gate  *		freed.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate #include <ndbm.h>
490Sstevel@tonic-gate #include <strings.h>
500Sstevel@tonic-gate #include "ypsym.h"
510Sstevel@tonic-gate #include "ypdefs.h"
520Sstevel@tonic-gate #include "shim.h"
530Sstevel@tonic-gate #include "yptol.h"
540Sstevel@tonic-gate #include "../ldap_parse.h"
550Sstevel@tonic-gate #include "../ldap_util.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Globals
590Sstevel@tonic-gate  */
60702Sth160488 bool_t yptol_mode = FALSE;	/* Set if in N2L mode */
61*2189Ssdussud bool_t yptol_newlock = FALSE;
62*2189Ssdussud 				/*
63*2189Ssdussud 				 * Set if in N2L mode and we want to use the new
64*2189Ssdussud 				 * lock mapping mechanism
65*2189Ssdussud 				 */
66702Sth160488 bool_t ypxfrd_flag = FALSE;	/* Set if called from ypxfrd */
670Sstevel@tonic-gate pid_t parent_pid;			/* ID of calling parent process */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Decs
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate void check_old_map_date(map_ctrl *);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Constants
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate /* Number of times to try to update a map before giving up */
790Sstevel@tonic-gate /* #define MAX_UPDATE_ATTEMPTS 3 */
800Sstevel@tonic-gate #define	MAX_UPDATE_ATTEMPTS 1
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * FUNCTION:    shim_dbm_close();
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate void
shim_dbm_close(DBM * db)910Sstevel@tonic-gate shim_dbm_close(DBM *db)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	map_ctrl *map;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/* Lock the map */
960Sstevel@tonic-gate 	map = get_map_ctrl(db);
970Sstevel@tonic-gate 	if (map == NULL)
980Sstevel@tonic-gate 		return;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	free_map_ctrl(map);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * FUNCTION:    shim_dbm_delete();
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * DESCRIPTION:	This function is currently unused but is present so that the
1070Sstevel@tonic-gate  *		set of shim_dbm_xxx() interfaces is complete if required in
1080Sstevel@tonic-gate  *		future.
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate int
shim_dbm_delete(DBM * db,datum key)1160Sstevel@tonic-gate shim_dbm_delete(DBM *db, datum key)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int ret;
1190Sstevel@tonic-gate 	map_ctrl *map;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	/* Lock the map */
1220Sstevel@tonic-gate 	map = get_map_ctrl(db);
1230Sstevel@tonic-gate 	if (map == NULL)
1240Sstevel@tonic-gate 		return (FAILURE);
1250Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
1260Sstevel@tonic-gate 		return (FAILURE);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (yptol_mode) {
1290Sstevel@tonic-gate 		/* Delete from and ttl map. Not a huge disaster if it fails. */
1300Sstevel@tonic-gate 		dbm_delete(map->ttl, key);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	ret = dbm_delete(map->entries, key);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	unlock_map_ctrl(map);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (ret);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * FUNCTION:    shim_dbm_fetch()
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * DESCRIPTION:	N2L function used to handle 'normal' dbm_fetch() operations.
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  * INPUTS:      First two identical to equivalent dbm call.
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
1490Sstevel@tonic-gate  *
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate datum
shim_dbm_fetch(DBM * db,datum key)1520Sstevel@tonic-gate shim_dbm_fetch(DBM *db, datum key)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	datum ret = {0, NULL};
1550Sstevel@tonic-gate 	map_ctrl *map;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/* Lock the map */
1580Sstevel@tonic-gate 	map = get_map_ctrl(db);
1590Sstevel@tonic-gate 	if (map == NULL)
1600Sstevel@tonic-gate 		return (ret);
1610Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
1620Sstevel@tonic-gate 		return (ret);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (yptol_mode) {
1650Sstevel@tonic-gate 		if (SUCCESS == update_entry_if_required(map, &key)) {
1660Sstevel@tonic-gate 			/* Update thinks we should return something */
1670Sstevel@tonic-gate 			ret = dbm_fetch(map->entries, key);
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 	} else {
1700Sstevel@tonic-gate 		/* Non yptol mode do a normal fetch */
1710Sstevel@tonic-gate 		ret = dbm_fetch(map->entries, key);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	unlock_map_ctrl(map);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	return (ret);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * FUNCTION:    shim_dbm_fetch_noupdate()
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  * DESCRIPTION:	A special version of shim_dbm_fetch() that never checks TTLs
1830Sstevel@tonic-gate  *		or updates entries.
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate datum
shim_dbm_fetch_noupdate(DBM * db,datum key)1910Sstevel@tonic-gate shim_dbm_fetch_noupdate(DBM *db, datum key)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	datum ret = {0, NULL};
1940Sstevel@tonic-gate 	map_ctrl *map;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/* Get the map control block */
1970Sstevel@tonic-gate 	map = get_map_ctrl(db);
1980Sstevel@tonic-gate 	if (map == NULL)
1990Sstevel@tonic-gate 		return (ret);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/* Not updating so no need to lock */
2020Sstevel@tonic-gate 	ret = dbm_fetch(map->entries, key);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	return (ret);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * FUNCTION:    shim_dbm_firstkey()
2090Sstevel@tonic-gate  *
2100Sstevel@tonic-gate  * DESCRIPTION: Get firstkey in an enumeration. If the map is out of date then
2110Sstevel@tonic-gate  *	      this is the time to scan it and see if any new entries have been
2120Sstevel@tonic-gate  *	      created.
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate datum
shim_dbm_firstkey(DBM * db)2200Sstevel@tonic-gate shim_dbm_firstkey(DBM *db)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	int count;
2230Sstevel@tonic-gate 	bool_t wait_flag;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	datum ret = {0, NULL};
2260Sstevel@tonic-gate 	map_ctrl *map;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/* Lock the map */
2290Sstevel@tonic-gate 	map = get_map_ctrl(db);
2300Sstevel@tonic-gate 	if (map == NULL)
2310Sstevel@tonic-gate 		return (ret);
2320Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
2330Sstevel@tonic-gate 		return (ret);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (yptol_mode) {
2360Sstevel@tonic-gate 		/*
2370Sstevel@tonic-gate 		 * Due to the limitations in the hashing algorithm ypxfrd
2380Sstevel@tonic-gate 		 * may end up waiting on the wrong update. It must thus loop
2390Sstevel@tonic-gate 		 * until the right map has been updated.
2400Sstevel@tonic-gate 		 */
2410Sstevel@tonic-gate 		for (count = 0; has_map_expired(map) &&
2420Sstevel@tonic-gate 				(MAX_UPDATE_ATTEMPTS > count); count++) {
2430Sstevel@tonic-gate 			/*
2440Sstevel@tonic-gate 			 * Ideally ypxfr should wait for the map update
2450Sstevel@tonic-gate 			 * to complete i.e. pass ypxfrd_flag into
2460Sstevel@tonic-gate 			 * update_map_if_required(). This cannot be done
2470Sstevel@tonic-gate 			 * because if there is a large map update the client
2480Sstevel@tonic-gate 			 * side, ypxfr, can time out while waiting.
2490Sstevel@tonic-gate 			 */
2500Sstevel@tonic-gate 			wait_flag = FALSE;
2510Sstevel@tonic-gate 			update_map_if_required(map, wait_flag);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 			if (wait_flag) {
2540Sstevel@tonic-gate 				/*
2550Sstevel@tonic-gate 				 * Because ypxfrd does weird things with DBMs
2560Sstevel@tonic-gate 				 * internal structures it's a good idea to
2570Sstevel@tonic-gate 				 * reopen here. (Code that uses the real DBM
2580Sstevel@tonic-gate 				 * API appears not to need this.)
2590Sstevel@tonic-gate 				 *
2600Sstevel@tonic-gate 				 * This should not be necessary all we have
2610Sstevel@tonic-gate 				 * done is 'mv' the new file over the old one.
2620Sstevel@tonic-gate 				 * Open handles should get the old data but if
2630Sstevel@tonic-gate 				 * these lines are removed the first ypxfrd
2640Sstevel@tonic-gate 				 * read access fail with bad file handle.
2650Sstevel@tonic-gate 				 *
2660Sstevel@tonic-gate 				 * NOTE : If we don't wait, because of the
2670Sstevel@tonic-gate 				 * ypxfr timeout problem, there is no point
2680Sstevel@tonic-gate 				 * doing this.
2690Sstevel@tonic-gate 				 */
2700Sstevel@tonic-gate 				dbm_close(map->entries);
2710Sstevel@tonic-gate 				dbm_close(map->ttl);
2720Sstevel@tonic-gate 				if (FAILURE == open_yptol_files(map)) {
2730Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
2740Sstevel@tonic-gate 						"Could not reopen DBM files");
2750Sstevel@tonic-gate 				}
2760Sstevel@tonic-gate 			} else {
2770Sstevel@tonic-gate 				/* For daemons that don't wait just try once */
2780Sstevel@tonic-gate 				break;
2790Sstevel@tonic-gate 			}
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		if (MAX_UPDATE_ATTEMPTS < count)
2830Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
2840Sstevel@tonic-gate 					"Cannot update map %s", map->map_name);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	ret = dbm_firstkey(map->entries);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/* Move key data out of static memory. See NOTE in file header above */
2900Sstevel@tonic-gate 	if (yptol_mode) {
2910Sstevel@tonic-gate 		set_key_data(map, &ret);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	unlock_map_ctrl(map);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	return (ret);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate  * FUNCTION:    shim_dbm_nextkey()
3000Sstevel@tonic-gate  *
3010Sstevel@tonic-gate  * DESCRIPTION: Get next key in an enumeration. Since updating an entry would
3020Sstevel@tonic-gate  *	      invalidate the enumeration we never do it.
3030Sstevel@tonic-gate  *
3040Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
3050Sstevel@tonic-gate  *
3060Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
3070Sstevel@tonic-gate  *
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate datum
shim_dbm_nextkey(DBM * db)3100Sstevel@tonic-gate shim_dbm_nextkey(DBM *db)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	datum ret;
3130Sstevel@tonic-gate 	map_ctrl *map;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/* Lock the map */
3160Sstevel@tonic-gate 	map = get_map_ctrl(db);
3170Sstevel@tonic-gate 	if (map == NULL)
3180Sstevel@tonic-gate 		return (ret);
3190Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
3200Sstevel@tonic-gate 		return (ret);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	ret = dbm_nextkey(map->entries);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/* Move key data out of static memory. See NOTE in file header above */
3250Sstevel@tonic-gate 	if (yptol_mode) {
3260Sstevel@tonic-gate 		set_key_data(map, &ret);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	unlock_map_ctrl(map);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	return (ret);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * FUNCTION:    shim_dbm_do_nextkey()
3360Sstevel@tonic-gate  *
3370Sstevel@tonic-gate  * DESCRIPTION: Get next key in an enumeration. Since updating an entry would
3380Sstevel@tonic-gate  *	      invalidate the enumeration we never do it.
3390Sstevel@tonic-gate  *
3400Sstevel@tonic-gate  * NOTE :	dbm_do_nextkey is not a documented or legal DBM API.
3410Sstevel@tonic-gate  *		Despite this the existing NIS code calls it. One gross hack
3420Sstevel@tonic-gate  *		deserves another so we have this extra shim function to handle
3430Sstevel@tonic-gate  *		the illegal call.
3440Sstevel@tonic-gate  *
3450Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
3460Sstevel@tonic-gate  *
3470Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate datum
shim_dbm_do_nextkey(DBM * db,datum inkey)3510Sstevel@tonic-gate shim_dbm_do_nextkey(DBM *db, datum inkey)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	datum ret;
3540Sstevel@tonic-gate 	map_ctrl *map;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* Lock the map */
3570Sstevel@tonic-gate 	map = get_map_ctrl(db);
3580Sstevel@tonic-gate 	if (map == NULL)
3590Sstevel@tonic-gate 		return (ret);
3600Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
3610Sstevel@tonic-gate 		return (ret);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	ret = dbm_do_nextkey(map->entries, inkey);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/* Move key data out of static memory. See NOTE in file header above */
3660Sstevel@tonic-gate 	if (yptol_mode) {
3670Sstevel@tonic-gate 		set_key_data(map, &ret);
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	unlock_map_ctrl(map);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	return (ret);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate  * FUNCTION:    shim_dbm_open()
3760Sstevel@tonic-gate  *
3770Sstevel@tonic-gate  * INPUTS:      Identical to equivalent dbm call.
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * OUTPUTS:     Identical to equivalent dbm call.
3800Sstevel@tonic-gate  *
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate DBM *
shim_dbm_open(const char * file,int open_flags,mode_t file_mode)3830Sstevel@tonic-gate shim_dbm_open(const char *file, int open_flags, mode_t file_mode)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	map_ctrl *map;
3860Sstevel@tonic-gate 	suc_code ret = FAILURE;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/* Find or create map_ctrl for this map */
3890Sstevel@tonic-gate 	map = create_map_ctrl((char *)file);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (map == NULL)
3920Sstevel@tonic-gate 		return (NULL);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/* Lock map */
3950Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
3960Sstevel@tonic-gate 		return (NULL);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/* Remember flags and mode in case we have to reopen */
3990Sstevel@tonic-gate 	map->open_flags = open_flags;
4000Sstevel@tonic-gate 	map->open_mode = file_mode;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (yptol_mode) {
4030Sstevel@tonic-gate 		ret = open_yptol_files(map);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		/*
4060Sstevel@tonic-gate 		 * This is a good place to check that the
4070Sstevel@tonic-gate 		 * equivalent old style map file has not been
4080Sstevel@tonic-gate 		 * updated.
4090Sstevel@tonic-gate 		 */
4100Sstevel@tonic-gate 		if (SUCCESS == ret)
4110Sstevel@tonic-gate 			check_old_map_date(map);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	} else {
4140Sstevel@tonic-gate 		/* Open entries map */
4150Sstevel@tonic-gate 		map->entries = dbm_open(map->map_path, map->open_flags,
4160Sstevel@tonic-gate 								map->open_mode);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		if (NULL != map->entries)
4190Sstevel@tonic-gate 			ret = SUCCESS;
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/* If we were not successful unravel what we have done so far */
4230Sstevel@tonic-gate 	if (ret != SUCCESS) {
4240Sstevel@tonic-gate 		unlock_map_ctrl(map);
4250Sstevel@tonic-gate 		free_map_ctrl(map);
4260Sstevel@tonic-gate 		return (NULL);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	unlock_map_ctrl(map);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/* Return map_ctrl pointer as a DBM *. To the outside world it is */
4320Sstevel@tonic-gate 	/* opaque. */
4330Sstevel@tonic-gate 	return ((DBM *)map);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * FUNCTION: 	shim_dbm_store()
4380Sstevel@tonic-gate  *
4390Sstevel@tonic-gate  * DESCRIPTION:	Shim for dbm_store.
4400Sstevel@tonic-gate  *
4410Sstevel@tonic-gate  *		In N2L mode if we are asked to store in DBM_INSERT mode
4420Sstevel@tonic-gate  *		then first an attempt is made to write to the DIT (in the same
4430Sstevel@tonic-gate  *		mode). If this is successful then the value is forced into DBM
4440Sstevel@tonic-gate  *		using DBM_REPLACE. This is because the DIT is authoritative.
4450Sstevel@tonic-gate  *		The success of failure of an 'insert' is determined by the
4460Sstevel@tonic-gate  *		presence or otherwise of an entry in the DIT not DBM.
4470Sstevel@tonic-gate  *
4480Sstevel@tonic-gate  * INPUTS:	Identical to equivalent dbm call.
4490Sstevel@tonic-gate  *
4500Sstevel@tonic-gate  * OUTPUTS:	Identical to equivalent dbm call.
4510Sstevel@tonic-gate  *
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate int
shim_dbm_store(DBM * db,datum key,datum content,int store_mode)4540Sstevel@tonic-gate shim_dbm_store(DBM  *db,  datum  key,  datum  content, int store_mode)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	int ret;
4570Sstevel@tonic-gate 	map_ctrl *map;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/* Get map name */
4600Sstevel@tonic-gate 	map = get_map_ctrl(db);
4610Sstevel@tonic-gate 	if (map == NULL)
4620Sstevel@tonic-gate 		return (FAILURE);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (yptol_mode) {
4650Sstevel@tonic-gate 		/* Write to the DIT before doing anything else */
4660Sstevel@tonic-gate 		if (!write_to_dit(map->map_name, map->domain, key, content,
4670Sstevel@tonic-gate 					DBM_REPLACE == store_mode, FALSE))
4680Sstevel@tonic-gate 			return (FAILURE);
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* Lock the map */
4720Sstevel@tonic-gate 	if (1 != lock_map_ctrl(map))
4730Sstevel@tonic-gate 		return (FAILURE);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (yptol_mode) {
4760Sstevel@tonic-gate 		if (!is_map_updating(map)) {
4770Sstevel@tonic-gate 			ret = dbm_store(map->entries, key, content,
4780Sstevel@tonic-gate 								DBM_REPLACE);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 			if (SUCCESS == ret)
4810Sstevel@tonic-gate 				/* Update TTL */
4820Sstevel@tonic-gate 				update_entry_ttl(map, &key, TTL_RAND);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	} else {
4850Sstevel@tonic-gate 		ret = dbm_store(map->entries, key, content, store_mode);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	unlock_map_ctrl(map);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	return (ret);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * FUNCTION :	shim_exit()
4950Sstevel@tonic-gate  *
4960Sstevel@tonic-gate  * DESCRIPTION:	Intercepts exit() calls made by N2L compatible NIS components.
4970Sstevel@tonic-gate  *		This is required because any call to the shim_dbm... series
4980Sstevel@tonic-gate  *		of functions may have started an update thread. If the process
4990Sstevel@tonic-gate  *		exits normally then this thread may be killed before it can
5000Sstevel@tonic-gate  *		complete its work. We thus wait here for the thread to complete.
5010Sstevel@tonic-gate  *
5020Sstevel@tonic-gate  * GIVEN :	Same arg as exit()
5030Sstevel@tonic-gate  *
5040Sstevel@tonic-gate  * RETURNS :	Never
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate void
shim_exit(int code)5070Sstevel@tonic-gate shim_exit(int code)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	thr_join(NULL, NULL, NULL);
5100Sstevel@tonic-gate 	exit(code);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate  * FUNCTION :	init_yptol_flag()
5150Sstevel@tonic-gate  *
5160Sstevel@tonic-gate  * DESCRIPTION: Initializes two flags these are similar but their function is
5170Sstevel@tonic-gate  *		subtly different.
5180Sstevel@tonic-gate  *
5190Sstevel@tonic-gate  *		yp2ldap tells the mapping system if it is to work in NIS or
5200Sstevel@tonic-gate  *		NIS+ mode. For N2L this is always set to NIS mode.
5210Sstevel@tonic-gate  *
5220Sstevel@tonic-gate  *		yptol tells the shim if it is to work in N2L or traditional
5230Sstevel@tonic-gate  *		NIS mode. For N2L this is turned on if the N2L mapping file
5240Sstevel@tonic-gate  *		is found to be present. In NIS+ mode it is meaningless.
5250Sstevel@tonic-gate  */
5260Sstevel@tonic-gate void
init_yptol_flag()5270Sstevel@tonic-gate init_yptol_flag()
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * yp2ldap is used to switch appropriate code in the
5310Sstevel@tonic-gate 	 * common libnisdb library used by rpc.nisd and ypserv.
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 	yp2ldap = 1;
5340Sstevel@tonic-gate 	yptol_mode = is_yptol_mode();
535*2189Ssdussud 	/*
536*2189Ssdussud 	 * Use the new lock mapping mechanism
537*2189Ssdussud 	 * if in N2L mode.
538*2189Ssdussud 	 */
539*2189Ssdussud 	yptol_newlock = yptol_mode;
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate  * FUNCTION :	set_yxfrd_flag()
5440Sstevel@tonic-gate  */
5450Sstevel@tonic-gate void
set_ypxfrd_flag()5460Sstevel@tonic-gate set_ypxfrd_flag()
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	ypxfrd_flag = TRUE;
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  * FUNCTION :	check_old_map_date()
5530Sstevel@tonic-gate  *
5540Sstevel@tonic-gate  * DESCRIPTION:	Checks that an old style map has not been updated. If it has
5550Sstevel@tonic-gate  *		then ypmake has probably erroneously been run and an error is
5560Sstevel@tonic-gate  *		logged.
5570Sstevel@tonic-gate  *
5580Sstevel@tonic-gate  * GIVEN :	A map_ctrl containing details of the NEW STYLE map.
5590Sstevel@tonic-gate  *
5600Sstevel@tonic-gate  * RETURNS :	Nothing
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate void
check_old_map_date(map_ctrl * map)5630Sstevel@tonic-gate check_old_map_date(map_ctrl *map)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	datum key;
5660Sstevel@tonic-gate 	datum value;
5670Sstevel@tonic-gate 	struct stat stats;
5680Sstevel@tonic-gate 	time_t old_time;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/* Get date of last update */
5710Sstevel@tonic-gate 	if (0 != stat(map->trad_map_path, &stats)) {
5720Sstevel@tonic-gate 		/*
5730Sstevel@tonic-gate 		 * No problem. We have a new style map but no old style map
5740Sstevel@tonic-gate 		 * this will occur if the original data came from native LDAP
5750Sstevel@tonic-gate 		 * instead of NIS.
5760Sstevel@tonic-gate 		 */
5770Sstevel@tonic-gate 		return;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/* Set up datum with key for recorded old map update time */
5810Sstevel@tonic-gate 	key.dsize = strlen(MAP_OLD_MAP_DATE_KEY);
5820Sstevel@tonic-gate 	key.dptr = MAP_OLD_MAP_DATE_KEY;
5830Sstevel@tonic-gate 	value = dbm_fetch(map->ttl, key);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (NULL != value.dptr) {
5860Sstevel@tonic-gate 		/*
5870Sstevel@tonic-gate 		 * Because dptr may not be int aligned need to build an int
5880Sstevel@tonic-gate 		 * out of what it points to or will get a bus error.
5890Sstevel@tonic-gate 		 */
5900Sstevel@tonic-gate 		bcopy(value.dptr, &old_time, sizeof (time_t));
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		/* Do the comparison */
5940Sstevel@tonic-gate 		if (stats.st_mtime <= old_time) {
5950Sstevel@tonic-gate 			/* All is well, has not been updated */
5960Sstevel@tonic-gate 			return;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		/* If we get here the file has been updated */
6000Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
6010Sstevel@tonic-gate 			"Caution. ypmake may have been run in N2L "
6020Sstevel@tonic-gate 			"mode. This will NOT initiate a NIS map push. In "
6030Sstevel@tonic-gate 			"this mode pushes should be initiated with yppush");
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * If we get here then either the file was updated or there was not
6080Sstevel@tonic-gate 	 * a valid old map date (no problem, maybe this is the first time we
6090Sstevel@tonic-gate 	 * checked). In either case the old map date entry must be update.
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	value.dptr = (char *)&(stats.st_mtime);
6120Sstevel@tonic-gate 	value.dsize = sizeof (time_t);
6130Sstevel@tonic-gate 	dbm_store(map->ttl, key, value, DBM_REPLACE);
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate /*
6170Sstevel@tonic-gate  * FUNCTION :	init_lock_system()
6180Sstevel@tonic-gate  *
6190Sstevel@tonic-gate  * DESCRIPTION:	Initializes all the systems related to map locking. This must
6200Sstevel@tonic-gate  *		be called before any access to the shim functions.
6210Sstevel@tonic-gate  *
6220Sstevel@tonic-gate  * GIVEN :	A flag indicating if we are being called from ypserv, which does
6230Sstevel@tonic-gate  *		not wait for map updates to complete, or other NIS components
6240Sstevel@tonic-gate  *		which do.
6250Sstevel@tonic-gate  *
6260Sstevel@tonic-gate  * RETURNS :	TRUE = Everything worked
6270Sstevel@tonic-gate  *		FALSE = There were problems
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate bool_t
init_lock_system(bool_t ypxfrd)6300Sstevel@tonic-gate init_lock_system(bool_t ypxfrd)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	/* Remember what called us */
6330Sstevel@tonic-gate 	if (ypxfrd)
6340Sstevel@tonic-gate 		set_ypxfrd_flag();
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * Remember PID of process which called us. This enables update threads
6380Sstevel@tonic-gate 	 * created by YP children to be handled differently to those created
6390Sstevel@tonic-gate 	 * by YP parents.
6400Sstevel@tonic-gate 	 */
6410Sstevel@tonic-gate 	parent_pid = getpid();
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/* Init map locks */
6440Sstevel@tonic-gate 	if (!init_lock_map()) {
6450Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
6460Sstevel@tonic-gate 				"Failed to init process synchronization");
6470Sstevel@tonic-gate 		return (FALSE);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/* If we are in yptol mode set flag indicating the fact */
6510Sstevel@tonic-gate 	init_yptol_flag();
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * If boot random number system. For now go for reproducible
6550Sstevel@tonic-gate 	 * random numbers.
6560Sstevel@tonic-gate 	 */
6570Sstevel@tonic-gate 	srand48(0x12345678);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * If not N2L mode then no error but do not bother initializing update
6610Sstevel@tonic-gate 	 * flags.
6620Sstevel@tonic-gate 	 */
6630Sstevel@tonic-gate 	if (yptol_mode) {
6640Sstevel@tonic-gate 		if (!init_update_lock_map()) {
6650Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
6660Sstevel@tonic-gate 				"Failed to init update synchronization");
6670Sstevel@tonic-gate 			return (FALSE);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	return (TRUE);
6720Sstevel@tonic-gate }
673