xref: /titanic_52/usr/src/lib/libnisdb/db_table.cc (revision a87701e9837f8a9ee9e4c4d3186295c0e29f743f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	db_table.cc
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*a87701e9SGary Mills  *
28*a87701e9SGary Mills  * Copyright 2015 RackTop Systems.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <malloc.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>		/* srand48() */
357c478bd9Sstevel@tonic-gate #include <lber.h>
367c478bd9Sstevel@tonic-gate #include <ldap.h>
377c478bd9Sstevel@tonic-gate #include "db_headers.h"
387c478bd9Sstevel@tonic-gate #include "db_table.h"
397c478bd9Sstevel@tonic-gate #include "db_pickle.h"    /* for dump and load */
407c478bd9Sstevel@tonic-gate #include "db_entry.h"
417c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "ldap_parse.h"
447c478bd9Sstevel@tonic-gate #include "ldap_util.h"
457c478bd9Sstevel@tonic-gate #include "ldap_map.h"
467c478bd9Sstevel@tonic-gate #include "ldap_xdr.h"
477c478bd9Sstevel@tonic-gate #include "nis_hashitem.h"
487c478bd9Sstevel@tonic-gate #include "nisdb_ldap.h"
497c478bd9Sstevel@tonic-gate #include "nis_parse_ldap_conf.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static time_t	maxTimeT;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Find the largest (positive) value of time_t.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * If time_t is unsigned, the largest possible value is just ~0.
577c478bd9Sstevel@tonic-gate  * However, if it's signed, then ~0 is negative. Since lint (for
587c478bd9Sstevel@tonic-gate  * sure), and perhaps the compiler too, dislike comparing an
597c478bd9Sstevel@tonic-gate  * unsigned quantity to see if it's less than zero, we compare
607c478bd9Sstevel@tonic-gate  * to one instead. If negative, the largest possible value is
617c478bd9Sstevel@tonic-gate  * th inverse of 2**(N-1), where N is the number of bits in a
627c478bd9Sstevel@tonic-gate  * time_t.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate extern "C" {
657c478bd9Sstevel@tonic-gate static void
667c478bd9Sstevel@tonic-gate __setMaxTimeT(void)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	unsigned char	b[sizeof (time_t)];
697c478bd9Sstevel@tonic-gate 	int		i;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* Compute ~0 for an unknown length integer */
727c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (time_t); i++) {
737c478bd9Sstevel@tonic-gate 		b[i] = 0xff;
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	/* Set maxTimeT to ~0 of appropriate length */
767c478bd9Sstevel@tonic-gate 	(void) memcpy(&maxTimeT, b, sizeof (time_t));
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (maxTimeT < 1)
79*a87701e9SGary Mills 		maxTimeT = ~(1L<<((8*sizeof (maxTimeT))-1));
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate #pragma init(__setMaxTimeT)
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* How much to grow table by */
857c478bd9Sstevel@tonic-gate #define	DB_TABLE_GROWTH_INCREMENT 1024
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* 0'th not used; might be confusing. */
887c478bd9Sstevel@tonic-gate #define	DB_TABLE_START 1
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* prevents wrap around numbers from being passed */
917c478bd9Sstevel@tonic-gate #define	CALLOC_LIMIT 536870911
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Initial table sizes to use before using 1K increments. */
947c478bd9Sstevel@tonic-gate /* This helps conserve memory usage when there are lots of small tables. */
957c478bd9Sstevel@tonic-gate static int tabsizes[] = {
967c478bd9Sstevel@tonic-gate 	16,
977c478bd9Sstevel@tonic-gate 	128,
987c478bd9Sstevel@tonic-gate 	512,
997c478bd9Sstevel@tonic-gate 	DB_TABLE_GROWTH_INCREMENT,
1007c478bd9Sstevel@tonic-gate 	0
1017c478bd9Sstevel@tonic-gate 	};
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* Returns the next size to use for table */
1047c478bd9Sstevel@tonic-gate static long unsigned
1057c478bd9Sstevel@tonic-gate get_new_table_size(long unsigned oldsize)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	long unsigned newsize = 0, n;
1087c478bd9Sstevel@tonic-gate 	if (oldsize == 0)
1097c478bd9Sstevel@tonic-gate 		newsize = tabsizes[0];
1107c478bd9Sstevel@tonic-gate 	else {
1117c478bd9Sstevel@tonic-gate 		for (n = 0; newsize = tabsizes[n++]; )
1127c478bd9Sstevel@tonic-gate 			if (oldsize == newsize) {
1137c478bd9Sstevel@tonic-gate 				newsize = tabsizes[n];	/* get next size */
1147c478bd9Sstevel@tonic-gate 				break;
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		if (newsize == 0)
1177c478bd9Sstevel@tonic-gate 			newsize = oldsize + DB_TABLE_GROWTH_INCREMENT;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	return (newsize);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* destructor */
1247c478bd9Sstevel@tonic-gate db_free_list::~db_free_list()
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_free_list::~db_free_list");
1277c478bd9Sstevel@tonic-gate 	reset();   /* free list entries */
1287c478bd9Sstevel@tonic-gate 	DESTROYRW(free_list);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate void
1327c478bd9Sstevel@tonic-gate db_free_list::reset()
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	db_free_entry *current, *nextentry;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_free_list::reset");
1377c478bd9Sstevel@tonic-gate 	for (current = head; current != NULL; ) {
1387c478bd9Sstevel@tonic-gate 		nextentry = current->next;
1397c478bd9Sstevel@tonic-gate 		delete current;
1407c478bd9Sstevel@tonic-gate 		current = nextentry;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	head = NULL;
1437c478bd9Sstevel@tonic-gate 	count = 0;
1447c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_free_list::reset");
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* Returns the location of a free entry, or NULL, if there aren't any. */
1487c478bd9Sstevel@tonic-gate entryp
1497c478bd9Sstevel@tonic-gate db_free_list::pop()
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	WRITELOCK(this, NULL, "w db_free_list::pop");
1527c478bd9Sstevel@tonic-gate 	if (head == NULL) {
1537c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL, "wu db_free_list::pop");
1547c478bd9Sstevel@tonic-gate 		return (NULL);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	db_free_entry* old_head = head;
1577c478bd9Sstevel@tonic-gate 	entryp found = head->where;
1587c478bd9Sstevel@tonic-gate 	head = head->next;
1597c478bd9Sstevel@tonic-gate 	delete old_head;
1607c478bd9Sstevel@tonic-gate 	--count;
1617c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, found, "wu db_free_list::pop");
1627c478bd9Sstevel@tonic-gate 	return (found);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Adds given location to the free list.
1677c478bd9Sstevel@tonic-gate  * Returns TRUE if successful, FALSE otherwise (when out of memory).
1687c478bd9Sstevel@tonic-gate */
1697c478bd9Sstevel@tonic-gate bool_t
1707c478bd9Sstevel@tonic-gate db_free_list::push(entryp tabloc)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	db_free_entry * newentry = new db_free_entry;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	WRITELOCK(this, FALSE, "w db_free_list::push");
1757c478bd9Sstevel@tonic-gate 	if (newentry == NULL) {
1767c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE, "wu db_free_list::push");
1777c478bd9Sstevel@tonic-gate 	    FATAL3("db_free_list::push: cannot allocation space",
1787c478bd9Sstevel@tonic-gate 		    DB_MEMORY_LIMIT, FALSE);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	newentry->where = tabloc;
1817c478bd9Sstevel@tonic-gate 	newentry->next = head;
1827c478bd9Sstevel@tonic-gate 	head = newentry;
1837c478bd9Sstevel@tonic-gate 	++count;
1847c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, TRUE, "wu db_free_list::push");
1857c478bd9Sstevel@tonic-gate 	return (TRUE);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Returns in a vector the information in the free list.
1907c478bd9Sstevel@tonic-gate  * Vector returned is of form: [n free cells][n1][n2][loc1], ..[locn].
1917c478bd9Sstevel@tonic-gate  * Leave the first 'n' cells free.
1927c478bd9Sstevel@tonic-gate  * n1 is the number of entries that should be in the freelist.
1937c478bd9Sstevel@tonic-gate  * n2 is the number of entries actually found in the freelist.
1947c478bd9Sstevel@tonic-gate  * [loc1...locn] are the entries.   n2 <= n1 because we never count beyond n1.
1957c478bd9Sstevel@tonic-gate  * It is up to the caller to free the returned vector when he is through.
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate long *
1987c478bd9Sstevel@tonic-gate db_free_list::stats(int nslots)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	long	realcount = 0,
2017c478bd9Sstevel@tonic-gate 		i,
2027c478bd9Sstevel@tonic-gate 		liststart = nslots,		// start of freelist
2037c478bd9Sstevel@tonic-gate 		listend = nslots+count+2;	// end of freelist
2047c478bd9Sstevel@tonic-gate 	db_free_entry_p current = head;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_free_list::stats");
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	long *answer = (long *)malloc((int)(listend)*sizeof (long));
2097c478bd9Sstevel@tonic-gate 	if (answer == 0) {
2107c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru db_free_list::stats");
2117c478bd9Sstevel@tonic-gate 		FATAL3("db_free_list::stats:  cannot allocation space",
2127c478bd9Sstevel@tonic-gate 		    DB_MEMORY_LIMIT, NULL);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	answer[liststart] = count;  /* size of freelist */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	for (i = liststart+2; i < listend && current != NULL; i++) {
2187c478bd9Sstevel@tonic-gate 		answer[i] = current->where;
2197c478bd9Sstevel@tonic-gate 		current = current->next;
2207c478bd9Sstevel@tonic-gate 		++realcount;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	answer[liststart+1] = realcount;
2247c478bd9Sstevel@tonic-gate 	READUNLOCK(this, answer, "ru db_free_list::stats");
2257c478bd9Sstevel@tonic-gate 	return (answer);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /* Set default values for the mapping structure */
2307c478bd9Sstevel@tonic-gate void
2317c478bd9Sstevel@tonic-gate db_table::initMappingStruct(__nisdb_table_mapping_t *m) {
2327c478bd9Sstevel@tonic-gate 	if (m == 0)
2337c478bd9Sstevel@tonic-gate 		return;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	m->initTtlLo = (ldapDBTableMapping.initTtlLo > 0) ?
2367c478bd9Sstevel@tonic-gate 			ldapDBTableMapping.initTtlLo : (3600-1800);
2377c478bd9Sstevel@tonic-gate 	m->initTtlHi = (ldapDBTableMapping.initTtlHi > 0) ?
2387c478bd9Sstevel@tonic-gate 			ldapDBTableMapping.initTtlHi : (3600+1800);
2397c478bd9Sstevel@tonic-gate 	m->ttl = (ldapDBTableMapping.ttl > 0) ?
2407c478bd9Sstevel@tonic-gate 			ldapDBTableMapping.ttl : 3600;
2417c478bd9Sstevel@tonic-gate 	m->enumExpire = 0;
2427c478bd9Sstevel@tonic-gate 	m->fromLDAP = FALSE;
2437c478bd9Sstevel@tonic-gate 	m->toLDAP = FALSE;
2447c478bd9Sstevel@tonic-gate 	m->isMaster = FALSE;
2457c478bd9Sstevel@tonic-gate 	m->retrieveError = ldapDBTableMapping.retrieveError;
2467c478bd9Sstevel@tonic-gate 	m->retrieveErrorRetry.attempts =
2477c478bd9Sstevel@tonic-gate 		ldapDBTableMapping.retrieveErrorRetry.attempts;
2487c478bd9Sstevel@tonic-gate 	m->retrieveErrorRetry.timeout =
2497c478bd9Sstevel@tonic-gate 		ldapDBTableMapping.retrieveErrorRetry.timeout;
2507c478bd9Sstevel@tonic-gate 	m->storeError = ldapDBTableMapping.storeError;
2517c478bd9Sstevel@tonic-gate 	m->storeErrorRetry.attempts =
2527c478bd9Sstevel@tonic-gate 		ldapDBTableMapping.storeErrorRetry.attempts;
2537c478bd9Sstevel@tonic-gate 	m->storeErrorRetry.timeout =
2547c478bd9Sstevel@tonic-gate 		ldapDBTableMapping.storeErrorRetry.timeout;
2557c478bd9Sstevel@tonic-gate 	m->storeErrorDisp = ldapDBTableMapping.storeErrorDisp;
2567c478bd9Sstevel@tonic-gate 	m->refreshError = ldapDBTableMapping.refreshError;
2577c478bd9Sstevel@tonic-gate 	m->refreshErrorRetry.attempts =
2587c478bd9Sstevel@tonic-gate 		ldapDBTableMapping.refreshErrorRetry.attempts;
2597c478bd9Sstevel@tonic-gate 	m->refreshErrorRetry.timeout =
2607c478bd9Sstevel@tonic-gate 		ldapDBTableMapping.refreshErrorRetry.timeout;
2617c478bd9Sstevel@tonic-gate 	m->matchFetch = ldapDBTableMapping.matchFetch;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (mapping.expire != 0)
2647c478bd9Sstevel@tonic-gate 		free(mapping.expire);
2657c478bd9Sstevel@tonic-gate 	m->expire = 0;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (m->tm != 0)
2687c478bd9Sstevel@tonic-gate 		free(m->tm);
2697c478bd9Sstevel@tonic-gate 	m->tm = 0;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * The 'objType' field obviously indicates the type of object.
2737c478bd9Sstevel@tonic-gate 	 * However, we also use it to tell us if we've retrieved mapping
2747c478bd9Sstevel@tonic-gate 	 * data from LDAP or not; in the latter case, 'objType' is
2757c478bd9Sstevel@tonic-gate 	 * NIS_BOGUS_OBJ. For purposes of maintaining expiration times,
2767c478bd9Sstevel@tonic-gate 	 * we may need to know if the object is a table or a directory
2777c478bd9Sstevel@tonic-gate 	 * _before_ we've retrieved any mapping data. Hence the 'expireType'
2787c478bd9Sstevel@tonic-gate 	 * field, which starts as NIS_BOGUS_OBJ (meaning, don't know, assume
2797c478bd9Sstevel@tonic-gate 	 * directory for now), and later is set to NIS_DIRECTORY_OBJ
2807c478bd9Sstevel@tonic-gate 	 * (always keep expiration data, in case one of the dir entries
2817c478bd9Sstevel@tonic-gate 	 * is mapped) or NIS_TABLE_OBJ (only need expiration data if
2827c478bd9Sstevel@tonic-gate 	 * tha table is mapped).
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	m->objType = NIS_BOGUS_OBJ;
2857c478bd9Sstevel@tonic-gate 	m->expireType = NIS_BOGUS_OBJ;
2867c478bd9Sstevel@tonic-gate 	if (m->objName != 0)
2877c478bd9Sstevel@tonic-gate 		free(m->objName);
2887c478bd9Sstevel@tonic-gate 	m->objName = 0;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate void
2927c478bd9Sstevel@tonic-gate db_table::db_table_ldap_init(void) {
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	INITRW(table);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	enumMode.flag = 0;
2977c478bd9Sstevel@tonic-gate 	enumCount.flag = 0;
2987c478bd9Sstevel@tonic-gate 	enumIndex.ptr = 0;
2997c478bd9Sstevel@tonic-gate 	enumArray.ptr = 0;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	mapping.expire = 0;
3027c478bd9Sstevel@tonic-gate 	mapping.tm = 0;
3037c478bd9Sstevel@tonic-gate 	mapping.objName = 0;
3047c478bd9Sstevel@tonic-gate 	mapping.isDeferredTable = FALSE;
3057c478bd9Sstevel@tonic-gate 	(void) mutex_init(&mapping.enumLock, 0, 0);
3067c478bd9Sstevel@tonic-gate 	mapping.enumTid = 0;
3077c478bd9Sstevel@tonic-gate 	mapping.enumStat = -1;
3087c478bd9Sstevel@tonic-gate 	mapping.enumDeferred = 0;
3097c478bd9Sstevel@tonic-gate 	mapping.enumEntries = 0;
3107c478bd9Sstevel@tonic-gate 	mapping.enumTime = 0;
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /* db_table constructor */
3147c478bd9Sstevel@tonic-gate db_table::db_table() : freelist()
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	tab = NULL;
3177c478bd9Sstevel@tonic-gate 	table_size = 0;
3187c478bd9Sstevel@tonic-gate 	last_used = 0;
3197c478bd9Sstevel@tonic-gate 	count = 0;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	db_table_ldap_init();
3227c478bd9Sstevel@tonic-gate 	initMappingStruct(&mapping);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*  grow(); */
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * db_table destructor:
3297c478bd9Sstevel@tonic-gate  * 1.  Get rid of contents of freelist
3307c478bd9Sstevel@tonic-gate  * 2.  delete all entries hanging off table
3317c478bd9Sstevel@tonic-gate  * 3.  get rid of table itself
3327c478bd9Sstevel@tonic-gate */
3337c478bd9Sstevel@tonic-gate db_table::~db_table()
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_table::~db_table");
3367c478bd9Sstevel@tonic-gate 	reset();
3377c478bd9Sstevel@tonic-gate 	DESTROYRW(table);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /* reset size and pointers */
3417c478bd9Sstevel@tonic-gate void
3427c478bd9Sstevel@tonic-gate db_table::reset()
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	int i, done = 0;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_table::reset");
3477c478bd9Sstevel@tonic-gate 	freelist.reset();
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* Add sanity check in case of table corruption */
3507c478bd9Sstevel@tonic-gate 	if (tab != NULL) {
3517c478bd9Sstevel@tonic-gate 		for (i = 0;
3527c478bd9Sstevel@tonic-gate 			i <= last_used && i < table_size && done < count;
3537c478bd9Sstevel@tonic-gate 			i++) {
3547c478bd9Sstevel@tonic-gate 			if (tab[i]) {
3557c478bd9Sstevel@tonic-gate 				free_entry(tab[i]);
3567c478bd9Sstevel@tonic-gate 				++done;
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	delete tab;
3627c478bd9Sstevel@tonic-gate 	table_size = last_used = count = 0;
3637c478bd9Sstevel@tonic-gate 	tab = NULL;
3647c478bd9Sstevel@tonic-gate 	sfree(mapping.expire);
3657c478bd9Sstevel@tonic-gate 	mapping.expire = NULL;
3667c478bd9Sstevel@tonic-gate 	mapping.objType = NIS_BOGUS_OBJ;
3677c478bd9Sstevel@tonic-gate 	mapping.expireType = NIS_BOGUS_OBJ;
3687c478bd9Sstevel@tonic-gate 	sfree(mapping.objName);
3697c478bd9Sstevel@tonic-gate 	mapping.objName = 0;
3707c478bd9Sstevel@tonic-gate 	/* Leave other values of the mapping structure unchanged */
3717c478bd9Sstevel@tonic-gate 	enumMode.flag = 0;
3727c478bd9Sstevel@tonic-gate 	enumCount.flag = 0;
3737c478bd9Sstevel@tonic-gate 	sfree(enumIndex.ptr);
3747c478bd9Sstevel@tonic-gate 	enumIndex.ptr = 0;
3757c478bd9Sstevel@tonic-gate 	sfree(enumArray.ptr);
3767c478bd9Sstevel@tonic-gate 	enumArray.ptr = 0;
3777c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_table::reset");
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate db_status
3817c478bd9Sstevel@tonic-gate db_table::allocateExpire(long oldSize, long newSize) {
3827c478bd9Sstevel@tonic-gate 	time_t			*newExpire;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	newExpire = (time_t *)realloc(mapping.expire,
3857c478bd9Sstevel@tonic-gate 				newSize * sizeof (mapping.expire[0]));
3867c478bd9Sstevel@tonic-gate 	if (newExpire != NULL) {
3877c478bd9Sstevel@tonic-gate 		/* Initialize new portion */
3887c478bd9Sstevel@tonic-gate 		(void) memset(&newExpire[oldSize], 0,
3897c478bd9Sstevel@tonic-gate 				(newSize-oldSize) * sizeof (newExpire[0]));
3907c478bd9Sstevel@tonic-gate 		mapping.expire = newExpire;
3917c478bd9Sstevel@tonic-gate 	} else {
3927c478bd9Sstevel@tonic-gate 		return (DB_MEMORY_LIMIT);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate db_status
3997c478bd9Sstevel@tonic-gate db_table::allocateEnumArray(long oldSize, long newSize) {
4007c478bd9Sstevel@tonic-gate 	entry_object	**newEnumArray;
4018d0852b7SRichard Lowe 	const char	*myself = "db_table::allocateEnumArray";
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (enumCount.flag > 0) {
4047c478bd9Sstevel@tonic-gate 		if (enumIndex.ptr == 0) {
4057c478bd9Sstevel@tonic-gate 			enumIndex.ptr = (entryp *)am(myself, enumCount.flag *
4067c478bd9Sstevel@tonic-gate 						sizeof (entryp));
4077c478bd9Sstevel@tonic-gate 			if (enumIndex.ptr == 0)
4087c478bd9Sstevel@tonic-gate 				return (DB_MEMORY_LIMIT);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		oldSize = 0;
4117c478bd9Sstevel@tonic-gate 		newSize = enumCount.flag;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	newEnumArray = (entry_object **)realloc(enumArray.ptr,
4147c478bd9Sstevel@tonic-gate 			newSize * sizeof (entry_object *));
4157c478bd9Sstevel@tonic-gate 	if (newEnumArray != 0 && newSize > oldSize) {
4167c478bd9Sstevel@tonic-gate 		(void) memcpy(&newEnumArray[oldSize], &tab[oldSize],
4177c478bd9Sstevel@tonic-gate 			(newSize-oldSize) * sizeof (entry_object *));
4187c478bd9Sstevel@tonic-gate 		enumArray.ptr = newEnumArray;
4197c478bd9Sstevel@tonic-gate 	} else if (newEnumArray == 0) {
4207c478bd9Sstevel@tonic-gate 		return (DB_MEMORY_LIMIT);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /* Expand the table.  Fatal error if insufficient memory. */
4277c478bd9Sstevel@tonic-gate void
4287c478bd9Sstevel@tonic-gate db_table::grow()
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_table::grow");
4317c478bd9Sstevel@tonic-gate 	long oldsize = table_size;
4327c478bd9Sstevel@tonic-gate 	entry_object_p *oldtab = tab;
4337c478bd9Sstevel@tonic-gate 	long i;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	table_size = get_new_table_size(oldsize);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate #ifdef DEBUG
4387c478bd9Sstevel@tonic-gate 	fprintf(stderr, "db_table GROWING to %d\n", table_size);
4397c478bd9Sstevel@tonic-gate #endif
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (table_size > CALLOC_LIMIT) {
4427c478bd9Sstevel@tonic-gate 		table_size = oldsize;
4437c478bd9Sstevel@tonic-gate 		WRITEUNLOCKV(this, "wu db_table::grow");
4447c478bd9Sstevel@tonic-gate 		FATAL("db_table::grow: table size exceeds calloc limit",
4457c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate //  if ((tab = new entry_object_p[table_size]) == NULL)
4497c478bd9Sstevel@tonic-gate 	if ((tab = (entry_object_p*)
4507c478bd9Sstevel@tonic-gate 		calloc((unsigned int) table_size,
4517c478bd9Sstevel@tonic-gate 			sizeof (entry_object_p))) == NULL) {
4527c478bd9Sstevel@tonic-gate 		tab = oldtab;		// restore previous table info
4537c478bd9Sstevel@tonic-gate 		table_size = oldsize;
4547c478bd9Sstevel@tonic-gate 		WRITEUNLOCKV(this, "wu db_table::grow");
4557c478bd9Sstevel@tonic-gate 		FATAL("db_table::grow: cannot allocate space", DB_MEMORY_LIMIT);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/*
4597c478bd9Sstevel@tonic-gate 	 * For directories, we may need the expire time array even if the
4607c478bd9Sstevel@tonic-gate 	 * directory itself isn't mapped. If the objType and expireType both
4617c478bd9Sstevel@tonic-gate 	 * are bogus, we don't  know yet if this is a table or a directory,
4627c478bd9Sstevel@tonic-gate 	 * and must proceed accordingly.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	if (mapping.objType == NIS_DIRECTORY_OBJ ||
4657c478bd9Sstevel@tonic-gate 			mapping.expireType != NIS_TABLE_OBJ ||
4667c478bd9Sstevel@tonic-gate 			mapping.fromLDAP) {
4677c478bd9Sstevel@tonic-gate 		db_status stat = allocateExpire(oldsize, table_size);
4687c478bd9Sstevel@tonic-gate 		if (stat != DB_SUCCESS) {
4697c478bd9Sstevel@tonic-gate 			free(tab);
4707c478bd9Sstevel@tonic-gate 			tab = oldtab;
4717c478bd9Sstevel@tonic-gate 			table_size = oldsize;
4727c478bd9Sstevel@tonic-gate 			WRITEUNLOCKV(this, "wu db_table::grow expire");
4737c478bd9Sstevel@tonic-gate 			FATAL(
4747c478bd9Sstevel@tonic-gate 		"db_table::grow: cannot allocate space for expire", stat);
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (oldtab != NULL) {
4797c478bd9Sstevel@tonic-gate 		for (i = 0; i < oldsize; i++) { // transfer old to new
4807c478bd9Sstevel@tonic-gate 			tab[i] = oldtab[i];
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 		delete oldtab;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (enumMode.flag) {
4867c478bd9Sstevel@tonic-gate 		db_status stat = allocateEnumArray(oldsize, table_size);
4877c478bd9Sstevel@tonic-gate 		if (stat != DB_SUCCESS) {
4887c478bd9Sstevel@tonic-gate 			free(tab);
4897c478bd9Sstevel@tonic-gate 			tab = oldtab;
4907c478bd9Sstevel@tonic-gate 			table_size = oldsize;
4917c478bd9Sstevel@tonic-gate 			WRITEUNLOCKV(this, "wu db_table::grow enumArray");
4927c478bd9Sstevel@tonic-gate 			FATAL(
4937c478bd9Sstevel@tonic-gate 		"db_table::grow: cannot allocate space for enumArray", stat);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_table::grow");
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Return the first entry in table, also return its position in
5027c478bd9Sstevel@tonic-gate  * 'where'.  Return NULL in both if no next entry is found.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate entry_object*
5057c478bd9Sstevel@tonic-gate db_table::first_entry(entryp * where)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	ASSERTRHELD(table);
5087c478bd9Sstevel@tonic-gate 	if (count == 0 || tab == NULL) {  /* empty table */
5097c478bd9Sstevel@tonic-gate 		*where = NULL;
5107c478bd9Sstevel@tonic-gate 		return (NULL);
5117c478bd9Sstevel@tonic-gate 	} else {
5127c478bd9Sstevel@tonic-gate 		entryp i;
5137c478bd9Sstevel@tonic-gate 		for (i = DB_TABLE_START;
5147c478bd9Sstevel@tonic-gate 			i < table_size && i <= last_used; i++) {
5157c478bd9Sstevel@tonic-gate 			if (tab[i] != NULL) {
5167c478bd9Sstevel@tonic-gate 				*where = i;
5177c478bd9Sstevel@tonic-gate 				return (tab[i]);
5187c478bd9Sstevel@tonic-gate 			}
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	*where = NULL;
5227c478bd9Sstevel@tonic-gate 	return (NULL);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate  * Return the next entry in table from 'prev', also return its position in
5277c478bd9Sstevel@tonic-gate  * 'newentry'.  Return NULL in both if no next entry is found.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate entry_object *
5307c478bd9Sstevel@tonic-gate db_table::next_entry(entryp prev, entryp* newentry)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	long i;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	ASSERTRHELD(table);
5357c478bd9Sstevel@tonic-gate 	if (prev >= table_size || tab == NULL || tab[prev] == NULL)
5367c478bd9Sstevel@tonic-gate 		return (NULL);
5377c478bd9Sstevel@tonic-gate 	for (i = prev+1; i < table_size && i <= last_used; i++) {
5387c478bd9Sstevel@tonic-gate 		if (tab[i] != NULL) {
5397c478bd9Sstevel@tonic-gate 			*newentry = i;
5407c478bd9Sstevel@tonic-gate 			return (tab[i]);
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	*newentry = NULL;
5447c478bd9Sstevel@tonic-gate 	return (NULL);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /* Return entry at location 'where', NULL if location is invalid. */
5487c478bd9Sstevel@tonic-gate entry_object *
5497c478bd9Sstevel@tonic-gate db_table::get_entry(entryp where)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	ASSERTRHELD(table);
5527c478bd9Sstevel@tonic-gate 	if (where < table_size && tab != NULL && tab[where] != NULL)
5537c478bd9Sstevel@tonic-gate 		return (tab[where]);
5547c478bd9Sstevel@tonic-gate 	else
5557c478bd9Sstevel@tonic-gate 		return (NULL);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate void
5597c478bd9Sstevel@tonic-gate db_table::setEntryExp(entryp where, entry_obj *obj, int initialLoad) {
5607c478bd9Sstevel@tonic-gate 	nis_object		*o;
5618d0852b7SRichard Lowe 	const char		*myself = "db_table::setEntryExp";
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/*
5647c478bd9Sstevel@tonic-gate 	 * If we don't know what type of object this is yet, we
5657c478bd9Sstevel@tonic-gate 	 * can find out now. If it's a directory, the pseudo-object
5667c478bd9Sstevel@tonic-gate 	 * in column zero will have the type "IN_DIRECTORY";
5677c478bd9Sstevel@tonic-gate 	 * otherwise, it's a table object.
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	if (mapping.expireType == NIS_BOGUS_OBJ) {
5707c478bd9Sstevel@tonic-gate 		if (obj != 0) {
5717c478bd9Sstevel@tonic-gate 			if (obj->en_type != 0 &&
5727c478bd9Sstevel@tonic-gate 				strcmp(obj->en_type, "IN_DIRECTORY") == 0) {
5737c478bd9Sstevel@tonic-gate 				mapping.expireType = NIS_DIRECTORY_OBJ;
5747c478bd9Sstevel@tonic-gate 			} else {
5757c478bd9Sstevel@tonic-gate 				mapping.expireType = NIS_TABLE_OBJ;
5767c478bd9Sstevel@tonic-gate 				if (!mapping.fromLDAP) {
5777c478bd9Sstevel@tonic-gate 					free(mapping.expire);
5787c478bd9Sstevel@tonic-gate 					mapping.expire = 0;
5797c478bd9Sstevel@tonic-gate 				}
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/* Set the entry TTL */
5857c478bd9Sstevel@tonic-gate 	if (mapping.expire != NULL) {
5867c478bd9Sstevel@tonic-gate 		struct timeval	now;
5877c478bd9Sstevel@tonic-gate 		time_t		lo, hi, ttl;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&now, NULL);
5907c478bd9Sstevel@tonic-gate 		if (mapping.expireType == NIS_TABLE_OBJ) {
5917c478bd9Sstevel@tonic-gate 			lo = mapping.initTtlLo;
5927c478bd9Sstevel@tonic-gate 			hi = mapping.initTtlHi;
5937c478bd9Sstevel@tonic-gate 			ttl = mapping.ttl;
5947c478bd9Sstevel@tonic-gate 			/* TTL == 0 means always expired */
5957c478bd9Sstevel@tonic-gate 			if (ttl == 0)
5967c478bd9Sstevel@tonic-gate 				ttl = -1;
5977c478bd9Sstevel@tonic-gate 		} else {
5987c478bd9Sstevel@tonic-gate 			__nis_table_mapping_t	*t = 0;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 			o = unmakePseudoEntryObj(obj, 0);
6017c478bd9Sstevel@tonic-gate 			if (o != 0) {
6027c478bd9Sstevel@tonic-gate 				__nis_buffer_t	b = {0, 0};
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 				bp2buf(myself, &b, "%s.%s",
6057c478bd9Sstevel@tonic-gate 					o->zo_name, o->zo_domain);
6067c478bd9Sstevel@tonic-gate 				t = getObjMapping(b.buf, 0, 1, 0, 0);
6077c478bd9Sstevel@tonic-gate 				sfree(b.buf);
6087c478bd9Sstevel@tonic-gate 				nis_destroy_object(o);
6097c478bd9Sstevel@tonic-gate 			}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 			if (t != 0) {
6127c478bd9Sstevel@tonic-gate 				lo = t->initTtlLo;
6137c478bd9Sstevel@tonic-gate 				hi = t->initTtlHi;
6147c478bd9Sstevel@tonic-gate 				ttl = t->ttl;
6157c478bd9Sstevel@tonic-gate 				/* TTL == 0 means always expired */
6167c478bd9Sstevel@tonic-gate 				if (ttl == 0)
6177c478bd9Sstevel@tonic-gate 					ttl = -1;
6187c478bd9Sstevel@tonic-gate 			} else {
6197c478bd9Sstevel@tonic-gate 				/*
6207c478bd9Sstevel@tonic-gate 				 * No expiration time initialization
6217c478bd9Sstevel@tonic-gate 				 * data. Cook up values that will
6227c478bd9Sstevel@tonic-gate 				 * result in mapping.expire[where]
6237c478bd9Sstevel@tonic-gate 				 * set to maxTimeT.
6247c478bd9Sstevel@tonic-gate 				 */
6257c478bd9Sstevel@tonic-gate 				hi = lo = ttl = maxTimeT - now.tv_sec;
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		if (initialLoad) {
6307c478bd9Sstevel@tonic-gate 			int	interval = hi - lo + 1;
6317c478bd9Sstevel@tonic-gate 			if (interval <= 1) {
6327c478bd9Sstevel@tonic-gate 				mapping.expire[where] = now.tv_sec + lo;
6337c478bd9Sstevel@tonic-gate 			} else {
6347c478bd9Sstevel@tonic-gate 				srand48(now.tv_sec);
6357c478bd9Sstevel@tonic-gate 				mapping.expire[where] = now.tv_sec +
6367c478bd9Sstevel@tonic-gate 							(lrand48() % interval);
6377c478bd9Sstevel@tonic-gate 			}
6387c478bd9Sstevel@tonic-gate 			if (mapping.enumExpire == 0 ||
6397c478bd9Sstevel@tonic-gate 					mapping.expire[where] <
6407c478bd9Sstevel@tonic-gate 							mapping.enumExpire)
6417c478bd9Sstevel@tonic-gate 				mapping.enumExpire = mapping.expire[where];
6427c478bd9Sstevel@tonic-gate 		} else {
6437c478bd9Sstevel@tonic-gate 			mapping.expire[where] = now.tv_sec + ttl;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * Add given entry to table in first available slot (either look in freelist
6507c478bd9Sstevel@tonic-gate  * or add to end of table) and return the the position of where the record
6517c478bd9Sstevel@tonic-gate  * is placed. 'count' is incremented if entry is added. Table may grow
6527c478bd9Sstevel@tonic-gate  * as a side-effect of the addition. Copy is made of input.
6537c478bd9Sstevel@tonic-gate */
6547c478bd9Sstevel@tonic-gate entryp
6557c478bd9Sstevel@tonic-gate db_table::add_entry(entry_object *obj, int initialLoad) {
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * We're returning an index of the table array, so the caller
6587c478bd9Sstevel@tonic-gate 	 * should hold a lock until done with the index. To save us
6597c478bd9Sstevel@tonic-gate 	 * the bother of upgrading to a write lock, it might as well
6607c478bd9Sstevel@tonic-gate 	 * be a write lock to begin with.
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	ASSERTWHELD(table);
6637c478bd9Sstevel@tonic-gate 	entryp where = freelist.pop();
6647c478bd9Sstevel@tonic-gate 	if (where == NULL) {				/* empty freelist */
6657c478bd9Sstevel@tonic-gate 		if (last_used >= (table_size-1))	/* full (> is for 0) */
6667c478bd9Sstevel@tonic-gate 			grow();
6677c478bd9Sstevel@tonic-gate 		where = ++last_used;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	if (tab != NULL) {
6707c478bd9Sstevel@tonic-gate 		++count;
6717c478bd9Sstevel@tonic-gate 		setEntryExp(where, obj, initialLoad);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		if (enumMode.flag)
6747c478bd9Sstevel@tonic-gate 			enumTouch(where);
6757c478bd9Sstevel@tonic-gate 		tab[where] = new_entry(obj);
6767c478bd9Sstevel@tonic-gate 		return (where);
6777c478bd9Sstevel@tonic-gate 	} else {
6787c478bd9Sstevel@tonic-gate 		return (NULL);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * Replaces object at specified location by given entry.
6847c478bd9Sstevel@tonic-gate  * Returns TRUE if replacement successful; FALSE otherwise.
6857c478bd9Sstevel@tonic-gate  * There must something already at the specified location, otherwise,
6867c478bd9Sstevel@tonic-gate  * replacement fails. Copy is not made of the input.
6877c478bd9Sstevel@tonic-gate  * The pre-existing entry is freed.
6887c478bd9Sstevel@tonic-gate  */
6897c478bd9Sstevel@tonic-gate bool_t
6907c478bd9Sstevel@tonic-gate db_table::replace_entry(entryp where, entry_object * obj)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	ASSERTWHELD(table);
6937c478bd9Sstevel@tonic-gate 	if (where < DB_TABLE_START || where >= table_size ||
6947c478bd9Sstevel@tonic-gate 	    tab == NULL || tab[where] == NULL)
6957c478bd9Sstevel@tonic-gate 		return (FALSE);
6967c478bd9Sstevel@tonic-gate 	/* (Re-)set the entry TTL */
6977c478bd9Sstevel@tonic-gate 	setEntryExp(where, obj, 0);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (enumMode.flag)
7007c478bd9Sstevel@tonic-gate 		enumTouch(where);
7017c478bd9Sstevel@tonic-gate 	free_entry(tab[where]);
7027c478bd9Sstevel@tonic-gate 	tab[where] = obj;
7037c478bd9Sstevel@tonic-gate 	return (TRUE);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate /*
7077c478bd9Sstevel@tonic-gate  * Deletes entry at specified location.  Returns TRUE if location is valid;
7087c478bd9Sstevel@tonic-gate  * FALSE if location is invalid, or the freed location cannot be added to
7097c478bd9Sstevel@tonic-gate  * the freelist.  'count' is decremented if the deletion occurs.  The object
7107c478bd9Sstevel@tonic-gate  * at that location is freed.
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate bool_t
7137c478bd9Sstevel@tonic-gate db_table::delete_entry(entryp where)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	bool_t	ret = TRUE;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	ASSERTWHELD(table);
7187c478bd9Sstevel@tonic-gate 	if (where < DB_TABLE_START || where >= table_size ||
7197c478bd9Sstevel@tonic-gate 	    tab == NULL || tab[where] == NULL)
7207c478bd9Sstevel@tonic-gate 		return (FALSE);
7217c478bd9Sstevel@tonic-gate 	if (mapping.expire != NULL) {
7227c478bd9Sstevel@tonic-gate 		mapping.expire[where] = 0;
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 	if (enumMode.flag)
7257c478bd9Sstevel@tonic-gate 		enumTouch(where);
7267c478bd9Sstevel@tonic-gate 	free_entry(tab[where]);
7277c478bd9Sstevel@tonic-gate 	tab[where] = NULL;    /* very important to set it to null */
7287c478bd9Sstevel@tonic-gate 	--count;
7297c478bd9Sstevel@tonic-gate 	if (where == last_used) { /* simple case, deleting from end */
7307c478bd9Sstevel@tonic-gate 		--last_used;
7317c478bd9Sstevel@tonic-gate 		return (TRUE);
7327c478bd9Sstevel@tonic-gate 	} else {
7337c478bd9Sstevel@tonic-gate 		return (freelist.push(where));
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	return (ret);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate  * Returns statistics of table.
7407c478bd9Sstevel@tonic-gate  * [vector_size][table_size][last_used][count][freelist].
7417c478bd9Sstevel@tonic-gate  * It is up to the caller to free the returned vector when his is through.
7427c478bd9Sstevel@tonic-gate  * The free list is included if 'fl' is TRUE.
7437c478bd9Sstevel@tonic-gate */
7447c478bd9Sstevel@tonic-gate long *
7457c478bd9Sstevel@tonic-gate db_table::stats(bool_t include_freelist)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	long *answer;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_table::stats");
7507c478bd9Sstevel@tonic-gate 	if (include_freelist)
7517c478bd9Sstevel@tonic-gate 		answer = freelist.stats(3);
7527c478bd9Sstevel@tonic-gate 	else {
7537c478bd9Sstevel@tonic-gate 		answer = (long *)malloc(3*sizeof (long));
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (answer) {
7577c478bd9Sstevel@tonic-gate 		answer[0] = table_size;
7587c478bd9Sstevel@tonic-gate 		answer[1] = last_used;
7597c478bd9Sstevel@tonic-gate 		answer[2] = count;
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	READUNLOCK(this, answer, "ru db_table::stats");
7627c478bd9Sstevel@tonic-gate 	return (answer);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate bool_t
7667c478bd9Sstevel@tonic-gate db_table::configure(char *tablePath) {
7677c478bd9Sstevel@tonic-gate 	long		i;
7687c478bd9Sstevel@tonic-gate 	struct timeval	now;
7698d0852b7SRichard Lowe 	const char	*myself = "db_table::configure";
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	WRITELOCK(this, FALSE, "db_table::configure w");
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/* (Re-)initialize from global info */
7767c478bd9Sstevel@tonic-gate 	initMappingStruct(&mapping);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/* Retrieve table mapping for this table */
7797c478bd9Sstevel@tonic-gate 	mapping.tm = (__nis_table_mapping_t *)__nis_find_item_mt(
7807c478bd9Sstevel@tonic-gate 					tablePath, &ldapMappingList, 0, 0);
7817c478bd9Sstevel@tonic-gate 	if (mapping.tm != 0) {
7827c478bd9Sstevel@tonic-gate 		__nis_object_dn_t	*odn = mapping.tm->objectDN;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/*
7857c478bd9Sstevel@tonic-gate 		 * The mapping.fromLDAP and mapping.toLDAP fields serve as
7867c478bd9Sstevel@tonic-gate 		 * quick-references that tell us if mapping is enabled.
7877c478bd9Sstevel@tonic-gate 		 * Hence, initialize them appropriately from the table
7887c478bd9Sstevel@tonic-gate 		 * mapping objectDN.
7897c478bd9Sstevel@tonic-gate 		 */
7907c478bd9Sstevel@tonic-gate 		while (odn != 0 && (!mapping.fromLDAP || !mapping.toLDAP)) {
7917c478bd9Sstevel@tonic-gate 			if (odn->read.scope != LDAP_SCOPE_UNKNOWN)
7927c478bd9Sstevel@tonic-gate 				mapping.fromLDAP = TRUE;
7937c478bd9Sstevel@tonic-gate 			if (odn->write.scope != LDAP_SCOPE_UNKNOWN)
7947c478bd9Sstevel@tonic-gate 				mapping.toLDAP = TRUE;
7957c478bd9Sstevel@tonic-gate 			odn = (__nis_object_dn_t *)odn->next;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		/* Set the timeout values */
7997c478bd9Sstevel@tonic-gate 		mapping.initTtlLo = mapping.tm->initTtlLo;
8007c478bd9Sstevel@tonic-gate 		mapping.initTtlHi = mapping.tm->initTtlHi;
8017c478bd9Sstevel@tonic-gate 		mapping.ttl = mapping.tm->ttl;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		mapping.objName = sdup(myself, T, mapping.tm->objName);
8047c478bd9Sstevel@tonic-gate 		if (mapping.objName == 0 && mapping.tm->objName != 0) {
8057c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, FALSE,
8067c478bd9Sstevel@tonic-gate 				"db_table::configure wu objName");
8077c478bd9Sstevel@tonic-gate 			FATAL3("db_table::configure objName",
8087c478bd9Sstevel@tonic-gate 				DB_MEMORY_LIMIT, FALSE);
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	/*
8137c478bd9Sstevel@tonic-gate 	 * In order to initialize the expiration times, we need to know
8147c478bd9Sstevel@tonic-gate 	 * if 'this' represents a table or a directory. To that end, we
8157c478bd9Sstevel@tonic-gate 	 * find an entry in the table, and invoke setEntryExp() on it.
8167c478bd9Sstevel@tonic-gate 	 * As a side effect, setEntryExp() will examine the pseudo-object
8177c478bd9Sstevel@tonic-gate 	 * in the entry, and set the expireType accordingly.
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	if (tab != 0) {
8207c478bd9Sstevel@tonic-gate 		for (i = 0; i <= last_used; i++) {
8217c478bd9Sstevel@tonic-gate 			if (tab[i] != NULL) {
8227c478bd9Sstevel@tonic-gate 				setEntryExp(i, tab[i], 1);
8237c478bd9Sstevel@tonic-gate 				break;
8247c478bd9Sstevel@tonic-gate 			}
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * If mapping from an LDAP repository, make sure we have the
8307c478bd9Sstevel@tonic-gate 	 * expiration time array.
8317c478bd9Sstevel@tonic-gate 	 */
8327c478bd9Sstevel@tonic-gate 	if ((mapping.expireType != NIS_TABLE_OBJ || mapping.fromLDAP) &&
8337c478bd9Sstevel@tonic-gate 			mapping.expire == NULL && table_size > 0 && tab != 0) {
8347c478bd9Sstevel@tonic-gate 		db_status stat = allocateExpire(0, table_size);
8357c478bd9Sstevel@tonic-gate 		if (stat != DB_SUCCESS) {
8367c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, FALSE,
8377c478bd9Sstevel@tonic-gate 				"db_table::configure wu expire");
8387c478bd9Sstevel@tonic-gate 			FATAL3("db_table::configure expire",
8397c478bd9Sstevel@tonic-gate 				stat, FALSE);
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	} else if (mapping.expireType == NIS_TABLE_OBJ && !mapping.fromLDAP &&
8427c478bd9Sstevel@tonic-gate 			mapping.expire != NULL) {
8437c478bd9Sstevel@tonic-gate 		/* Not using expiration times */
8447c478bd9Sstevel@tonic-gate 		free(mapping.expire);
8457c478bd9Sstevel@tonic-gate 		mapping.expire = NULL;
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * Set initial expire times for entries that don't already have one.
8507c478bd9Sstevel@tonic-gate 	 * Establish the enumeration expiration time to be the minimum of
8517c478bd9Sstevel@tonic-gate 	 * all expiration times in the table, though no larger than current
8527c478bd9Sstevel@tonic-gate 	 * time plus initTtlHi.
8537c478bd9Sstevel@tonic-gate 	 */
8547c478bd9Sstevel@tonic-gate 	if (mapping.expire != NULL) {
8557c478bd9Sstevel@tonic-gate 		int	interval = mapping.initTtlHi - mapping.initTtlLo + 1;
8567c478bd9Sstevel@tonic-gate 		time_t	enumXp = now.tv_sec + mapping.initTtlHi;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		if (interval > 1)
8597c478bd9Sstevel@tonic-gate 			srand48(now.tv_sec);
8607c478bd9Sstevel@tonic-gate 		for (i = 0; i <= last_used; i++) {
8617c478bd9Sstevel@tonic-gate 			if (tab[i] != NULL && mapping.expire[i] == 0) {
8627c478bd9Sstevel@tonic-gate 				if (mapping.expireType == NIS_TABLE_OBJ) {
8637c478bd9Sstevel@tonic-gate 					if (interval > 1)
8647c478bd9Sstevel@tonic-gate 						mapping.expire[i] =
8657c478bd9Sstevel@tonic-gate 							now.tv_sec +
8667c478bd9Sstevel@tonic-gate 							(lrand48() % interval);
8677c478bd9Sstevel@tonic-gate 					else
8687c478bd9Sstevel@tonic-gate 						mapping.expire[i] =
8697c478bd9Sstevel@tonic-gate 							now.tv_sec +
8707c478bd9Sstevel@tonic-gate 							mapping.initTtlLo;
8717c478bd9Sstevel@tonic-gate 				} else {
8727c478bd9Sstevel@tonic-gate 					setEntryExp(i, tab[i], 1);
8737c478bd9Sstevel@tonic-gate 				}
8747c478bd9Sstevel@tonic-gate 			}
8757c478bd9Sstevel@tonic-gate 			if (enumXp > mapping.expire[i])
8767c478bd9Sstevel@tonic-gate 				enumXp = mapping.expire[i];
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 		mapping.enumExpire = enumXp;
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, FALSE, "db_table::configure wu");
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	return (TRUE);
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /* Return TRUE if the entry at 'loc' hasn't expired */
8877c478bd9Sstevel@tonic-gate bool_t
8887c478bd9Sstevel@tonic-gate db_table::cacheValid(entryp loc) {
8897c478bd9Sstevel@tonic-gate 	bool_t		ret;
8907c478bd9Sstevel@tonic-gate 	struct timeval	now;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, 0);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	READLOCK(this, FALSE, "db_table::cacheValid r");
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (loc < 0 || loc >= table_size || tab == 0 || tab[loc] == 0)
8977c478bd9Sstevel@tonic-gate 		ret = FALSE;
8987c478bd9Sstevel@tonic-gate 	else if (mapping.expire == 0 || mapping.expire[loc] >= now.tv_sec)
8997c478bd9Sstevel@tonic-gate 		ret = TRUE;
9007c478bd9Sstevel@tonic-gate 	else
9017c478bd9Sstevel@tonic-gate 		ret = FALSE;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "db_table::cacheValid ru");
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	return (ret);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * If the supplied object has the same content as the one at 'loc',
9107c478bd9Sstevel@tonic-gate  * update the expiration time for the latter, and return TRUE.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate bool_t
9137c478bd9Sstevel@tonic-gate db_table::dupEntry(entry_object *obj, entryp loc) {
9147c478bd9Sstevel@tonic-gate 	if (obj == 0 || loc < 0 || loc >= table_size || tab == 0 ||
9157c478bd9Sstevel@tonic-gate 			tab[loc] == 0)
9167c478bd9Sstevel@tonic-gate 		return (FALSE);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (sameEntry(obj, tab[loc])) {
9197c478bd9Sstevel@tonic-gate 		setEntryExp(loc, tab[loc], 0);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		if (enumMode.flag > 0)
9227c478bd9Sstevel@tonic-gate 			enumTouch(loc);
9237c478bd9Sstevel@tonic-gate 		return (TRUE);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	return (FALSE);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate  * If enumeration mode is enabled, we keep a shadow array that initially
9317c478bd9Sstevel@tonic-gate  * starts out the same as 'tab'. Any update activity (add, remove, replace,
9327c478bd9Sstevel@tonic-gate  * or update timestamp) for an entry in the table means we delete the shadow
9337c478bd9Sstevel@tonic-gate  * array pointer. When ending enumeration mode, we return the shadow array.
9347c478bd9Sstevel@tonic-gate  * Any non-NULL entries in the array have not been updated since the start
9357c478bd9Sstevel@tonic-gate  * of the enum mode.
9367c478bd9Sstevel@tonic-gate  *
9377c478bd9Sstevel@tonic-gate  * The indended use is for enumeration of an LDAP container, where we
9387c478bd9Sstevel@tonic-gate  * will update all entries that currently exist in LDAP. The entries we
9397c478bd9Sstevel@tonic-gate  * don't update are those that don't exist in LDAP, and thus should be
9407c478bd9Sstevel@tonic-gate  * removed.
9417c478bd9Sstevel@tonic-gate  *
9427c478bd9Sstevel@tonic-gate  * Note that any LDAP query strictly speaking can be a partial enumeration
9437c478bd9Sstevel@tonic-gate  * (i.e., return more than one match). Since the query might also have
9447c478bd9Sstevel@tonic-gate  * matched multiple local DB entries, we need to do the same work as for
9457c478bd9Sstevel@tonic-gate  * enumeration for any query. In order to avoid having to work on the
9467c478bd9Sstevel@tonic-gate  * whole 'tab' array for simple queries (which we expect usually will
9477c478bd9Sstevel@tonic-gate  * match just one or at most a few entries), we have a "reduced" enum mode,
9487c478bd9Sstevel@tonic-gate  * where the caller supplies a count of the number of DB entries (derived
9497c478bd9Sstevel@tonic-gate  * from db_mindex::satisfy_query() or similar), and then uses enumSetup()
9507c478bd9Sstevel@tonic-gate  * to specify which 'tab' entries we're interested in.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate void
9537c478bd9Sstevel@tonic-gate db_table::setEnumMode(long enumNum) {
9548d0852b7SRichard Lowe 	const char	*myself = "setEnumMode";
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	enumMode.flag++;
9577c478bd9Sstevel@tonic-gate 	if (enumMode.flag == 1) {
9587c478bd9Sstevel@tonic-gate 		db_status	stat;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		if (enumNum < 0)
9617c478bd9Sstevel@tonic-gate 			enumNum = 0;
9627c478bd9Sstevel@tonic-gate 		else if (enumNum >= table_size)
9637c478bd9Sstevel@tonic-gate 			enumNum = table_size;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		enumCount.flag = enumNum;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		stat = allocateEnumArray(0, table_size);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 		if (stat != DB_SUCCESS) {
9707c478bd9Sstevel@tonic-gate 			enumMode.flag = 0;
9717c478bd9Sstevel@tonic-gate 			enumCount.flag = 0;
9727c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
9737c478bd9Sstevel@tonic-gate 		"%s: No memory for enum check array; entry removal disabled",
9747c478bd9Sstevel@tonic-gate 				myself);
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate void
9807c478bd9Sstevel@tonic-gate db_table::clearEnumMode(void) {
9817c478bd9Sstevel@tonic-gate 	if (enumMode.flag > 0) {
9827c478bd9Sstevel@tonic-gate 		enumMode.flag--;
9837c478bd9Sstevel@tonic-gate 		if (enumMode.flag == 0) {
9847c478bd9Sstevel@tonic-gate 			sfree(enumArray.ptr);
9857c478bd9Sstevel@tonic-gate 			enumArray.ptr = 0;
9867c478bd9Sstevel@tonic-gate 			if (enumCount.flag > 0) {
9877c478bd9Sstevel@tonic-gate 				sfree(enumIndex.ptr);
9887c478bd9Sstevel@tonic-gate 				enumIndex.ptr = 0;
9897c478bd9Sstevel@tonic-gate 				enumCount.flag = 0;
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 		}
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate entry_object **
9967c478bd9Sstevel@tonic-gate db_table::endEnumMode(long *numEa) {
9977c478bd9Sstevel@tonic-gate 	if (enumMode.flag > 0) {
9987c478bd9Sstevel@tonic-gate 		enumMode.flag--;
9997c478bd9Sstevel@tonic-gate 		if (enumMode.flag == 0) {
10007c478bd9Sstevel@tonic-gate 			entry_obj	**ea = (entry_object **)enumArray.ptr;
10017c478bd9Sstevel@tonic-gate 			long		nea;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 			enumArray.ptr = 0;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 			if (enumCount.flag > 0) {
10067c478bd9Sstevel@tonic-gate 				nea = enumCount.flag;
10077c478bd9Sstevel@tonic-gate 				enumCount.flag = 0;
10087c478bd9Sstevel@tonic-gate 				sfree(enumIndex.ptr);
10097c478bd9Sstevel@tonic-gate 				enumIndex.ptr = 0;
10107c478bd9Sstevel@tonic-gate 			} else {
10117c478bd9Sstevel@tonic-gate 				nea = table_size;
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 			if (numEa != 0)
10157c478bd9Sstevel@tonic-gate 				*numEa = nea;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 			return (ea);
10187c478bd9Sstevel@tonic-gate 		}
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (numEa != 0)
10227c478bd9Sstevel@tonic-gate 		*numEa = 0;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	return (0);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate  * Set the appropriate entry in the enum array to NULL.
10297c478bd9Sstevel@tonic-gate  */
10307c478bd9Sstevel@tonic-gate void
10317c478bd9Sstevel@tonic-gate db_table::enumTouch(entryp loc) {
10327c478bd9Sstevel@tonic-gate 	if (loc < 0 || loc >= table_size)
10337c478bd9Sstevel@tonic-gate 		return;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	if (enumMode.flag > 0) {
10367c478bd9Sstevel@tonic-gate 		if (enumCount.flag < 1) {
10377c478bd9Sstevel@tonic-gate 			((entry_object **)enumArray.ptr)[loc] = 0;
10387c478bd9Sstevel@tonic-gate 		} else {
10397c478bd9Sstevel@tonic-gate 			int	i;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 			for (i = 0; i < enumCount.flag; i++) {
10427c478bd9Sstevel@tonic-gate 				if (loc == ((entryp *)enumIndex.ptr)[i]) {
10437c478bd9Sstevel@tonic-gate 					((entry_object **)enumArray.ptr)[i] = 0;
10447c478bd9Sstevel@tonic-gate 					break;
10457c478bd9Sstevel@tonic-gate 				}
10467c478bd9Sstevel@tonic-gate 			}
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  * Add the entry indicated by 'loc' to the enumIndex array, at 'index'.
10537c478bd9Sstevel@tonic-gate  */
10547c478bd9Sstevel@tonic-gate void
10557c478bd9Sstevel@tonic-gate db_table::enumSetup(entryp loc, long index) {
10567c478bd9Sstevel@tonic-gate 	if (enumMode.flag == 0 || loc < 0 || loc >= table_size ||
10577c478bd9Sstevel@tonic-gate 			index < 0 || index >= enumCount.flag)
10587c478bd9Sstevel@tonic-gate 		return;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	((entryp *)enumIndex.ptr)[index] = loc;
10617c478bd9Sstevel@tonic-gate 	((entry_object **)enumArray.ptr)[index] = tab[loc];
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * Touch, i.e., update the expiration time for the entry. Also, if enum
10667c478bd9Sstevel@tonic-gate  * mode is in effect, mark the entry used for enum purposes.
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate void
10697c478bd9Sstevel@tonic-gate db_table::touchEntry(entryp loc) {
10707c478bd9Sstevel@tonic-gate 	if (loc < 0 || loc >= table_size || tab == 0 || tab[loc] == 0)
10717c478bd9Sstevel@tonic-gate 		return;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	setEntryExp(loc, tab[loc], 0);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	enumTouch(loc);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate /* ************************* pickle_table ********************* */
10797c478bd9Sstevel@tonic-gate /* Does the actual writing to/from file specific for db_table structure. */
10807c478bd9Sstevel@tonic-gate /*
10817c478bd9Sstevel@tonic-gate  * This was a static earlier with the func name being transfer_aux. The
10827c478bd9Sstevel@tonic-gate  * backup and restore project needed this to copy files over.
10837c478bd9Sstevel@tonic-gate  */
10847c478bd9Sstevel@tonic-gate bool_t
10857c478bd9Sstevel@tonic-gate transfer_aux_table(XDR* x, pptr dp)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	return (xdr_db_table(x, (db_table*) dp));
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate class pickle_table: public pickle_file {
10917c478bd9Sstevel@tonic-gate     public:
10927c478bd9Sstevel@tonic-gate 	pickle_table(char *f, pickle_mode m) : pickle_file(f, m) {}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/* Transfers db_table structure pointed to by dp to/from file. */
10957c478bd9Sstevel@tonic-gate 	int transfer(db_table* dp)
10967c478bd9Sstevel@tonic-gate 	{ return (pickle_file::transfer((pptr) dp, &transfer_aux_table)); }
10977c478bd9Sstevel@tonic-gate };
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * Writes the contents of table, including the all the entries, into the
11017c478bd9Sstevel@tonic-gate  * specified file in XDR format.  May need to change this to use APPEND
11027c478bd9Sstevel@tonic-gate  * mode instead.
11037c478bd9Sstevel@tonic-gate  */
11047c478bd9Sstevel@tonic-gate int
11057c478bd9Sstevel@tonic-gate db_table::dump(char *file)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	int	ret;
11087c478bd9Sstevel@tonic-gate 	READLOCK(this, -1, "r db_table::dump");
11097c478bd9Sstevel@tonic-gate 	pickle_table f(file, PICKLE_WRITE);   /* may need to use APPEND mode */
11107c478bd9Sstevel@tonic-gate 	int status = f.transfer(this);
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	if (status == 1)
11137c478bd9Sstevel@tonic-gate 		ret = -1;
11147c478bd9Sstevel@tonic-gate 	else
11157c478bd9Sstevel@tonic-gate 		ret = status;
11167c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru db_table::dump");
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate /* Constructor that loads in the table from the given file */
11207c478bd9Sstevel@tonic-gate db_table::db_table(char *file)  : freelist()
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	pickle_table f(file, PICKLE_READ);
11237c478bd9Sstevel@tonic-gate 	tab = NULL;
11247c478bd9Sstevel@tonic-gate 	table_size = last_used = count = 0;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/* load  table */
11277c478bd9Sstevel@tonic-gate 	if (f.transfer(this) < 0) {
11287c478bd9Sstevel@tonic-gate 		/* fell through, something went wrong, initialize to null */
11297c478bd9Sstevel@tonic-gate 		tab = NULL;
11307c478bd9Sstevel@tonic-gate 		table_size = last_used = count = 0;
11317c478bd9Sstevel@tonic-gate 		freelist.init();
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	db_table_ldap_init();
11357c478bd9Sstevel@tonic-gate 	initMappingStruct(&mapping);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate /* Returns whether location is valid. */
11397c478bd9Sstevel@tonic-gate bool_t db_table::entry_exists_p(entryp i) {
11407c478bd9Sstevel@tonic-gate 	bool_t	ret = FALSE;
11417c478bd9Sstevel@tonic-gate 	READLOCK(this, FALSE, "r db_table::entry_exists_p");
11427c478bd9Sstevel@tonic-gate 	if (tab != NULL && i < table_size)
11437c478bd9Sstevel@tonic-gate 		ret = tab[i] != NULL;
11447c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru db_table::entry_exists_p");
11457c478bd9Sstevel@tonic-gate 	return (ret);
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate /* Empty free list */
11497c478bd9Sstevel@tonic-gate void db_free_list::init() {
11507c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w db_free_list::init");
11517c478bd9Sstevel@tonic-gate 	head = NULL;
11527c478bd9Sstevel@tonic-gate 	count = 0;
11537c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu db_free_list::init");
11547c478bd9Sstevel@tonic-gate }
1155