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
5*4a190493Ssdussud * Common Development and Distribution License (the "License").
6*4a190493Ssdussud * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*4a190493Ssdussud * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * DESCRIPTION: Contains the top level shim hook functions. These must have
307c478bd9Sstevel@tonic-gate * identical interfaces to the equivalent standard dbm calls.
317c478bd9Sstevel@tonic-gate *
327c478bd9Sstevel@tonic-gate * Unfortunately many of these will do a copy of a datum structure
337c478bd9Sstevel@tonic-gate * on return. This is a side effect of the original DBM function
347c478bd9Sstevel@tonic-gate * being written to pass structures rather than pointers.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * NOTE : There is a major bug/feature in dbm. A key obtained by
377c478bd9Sstevel@tonic-gate * dbm_nextkey() of dbm_firstkey() cannot be passed to dbm_store().
387c478bd9Sstevel@tonic-gate * When the store occurs dbm's internal memory get's reorganized
397c478bd9Sstevel@tonic-gate * and the static strings pointed to by the key are destroyed. The
407c478bd9Sstevel@tonic-gate * data is then stored in the wrong place. We attempt to get round
417c478bd9Sstevel@tonic-gate * this by dbm_firstkey() and dbm_nextkey() making a copy of the
427c478bd9Sstevel@tonic-gate * key data in malloced memory. This is freed when map_ctrl is
437c478bd9Sstevel@tonic-gate * freed.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate #include <syslog.h>
487c478bd9Sstevel@tonic-gate #include <ndbm.h>
497c478bd9Sstevel@tonic-gate #include <strings.h>
507c478bd9Sstevel@tonic-gate #include "ypsym.h"
517c478bd9Sstevel@tonic-gate #include "ypdefs.h"
527c478bd9Sstevel@tonic-gate #include "shim.h"
537c478bd9Sstevel@tonic-gate #include "yptol.h"
547c478bd9Sstevel@tonic-gate #include "../ldap_parse.h"
557c478bd9Sstevel@tonic-gate #include "../ldap_util.h"
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * Globals
597c478bd9Sstevel@tonic-gate */
60a506a34cSth160488 bool_t yptol_mode = FALSE; /* Set if in N2L mode */
61*4a190493Ssdussud bool_t yptol_newlock = FALSE;
62*4a190493Ssdussud /*
63*4a190493Ssdussud * Set if in N2L mode and we want to use the new
64*4a190493Ssdussud * lock mapping mechanism
65*4a190493Ssdussud */
66a506a34cSth160488 bool_t ypxfrd_flag = FALSE; /* Set if called from ypxfrd */
677c478bd9Sstevel@tonic-gate pid_t parent_pid; /* ID of calling parent process */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * Decs
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate void check_old_map_date(map_ctrl *);
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * Constants
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate /* Number of times to try to update a map before giving up */
797c478bd9Sstevel@tonic-gate /* #define MAX_UPDATE_ATTEMPTS 3 */
807c478bd9Sstevel@tonic-gate #define MAX_UPDATE_ATTEMPTS 1
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_close();
847c478bd9Sstevel@tonic-gate *
857c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
867c478bd9Sstevel@tonic-gate *
877c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
887c478bd9Sstevel@tonic-gate *
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate void
shim_dbm_close(DBM * db)917c478bd9Sstevel@tonic-gate shim_dbm_close(DBM *db)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate map_ctrl *map;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /* Lock the map */
967c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
977c478bd9Sstevel@tonic-gate if (map == NULL)
987c478bd9Sstevel@tonic-gate return;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate free_map_ctrl(map);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_delete();
1057c478bd9Sstevel@tonic-gate *
1067c478bd9Sstevel@tonic-gate * DESCRIPTION: This function is currently unused but is present so that the
1077c478bd9Sstevel@tonic-gate * set of shim_dbm_xxx() interfaces is complete if required in
1087c478bd9Sstevel@tonic-gate * future.
1097c478bd9Sstevel@tonic-gate *
1107c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
1117c478bd9Sstevel@tonic-gate *
1127c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
1137c478bd9Sstevel@tonic-gate *
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate int
shim_dbm_delete(DBM * db,datum key)1167c478bd9Sstevel@tonic-gate shim_dbm_delete(DBM *db, datum key)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate int ret;
1197c478bd9Sstevel@tonic-gate map_ctrl *map;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* Lock the map */
1227c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
1237c478bd9Sstevel@tonic-gate if (map == NULL)
1247c478bd9Sstevel@tonic-gate return (FAILURE);
1257c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
1267c478bd9Sstevel@tonic-gate return (FAILURE);
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate if (yptol_mode) {
1297c478bd9Sstevel@tonic-gate /* Delete from and ttl map. Not a huge disaster if it fails. */
1307c478bd9Sstevel@tonic-gate dbm_delete(map->ttl, key);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate ret = dbm_delete(map->entries, key);
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate return (ret);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_fetch()
1437c478bd9Sstevel@tonic-gate *
1447c478bd9Sstevel@tonic-gate * DESCRIPTION: N2L function used to handle 'normal' dbm_fetch() operations.
1457c478bd9Sstevel@tonic-gate *
1467c478bd9Sstevel@tonic-gate * INPUTS: First two identical to equivalent dbm call.
1477c478bd9Sstevel@tonic-gate *
1487c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
1497c478bd9Sstevel@tonic-gate *
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate datum
shim_dbm_fetch(DBM * db,datum key)1527c478bd9Sstevel@tonic-gate shim_dbm_fetch(DBM *db, datum key)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate datum ret = {0, NULL};
1557c478bd9Sstevel@tonic-gate map_ctrl *map;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /* Lock the map */
1587c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
1597c478bd9Sstevel@tonic-gate if (map == NULL)
1607c478bd9Sstevel@tonic-gate return (ret);
1617c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
1627c478bd9Sstevel@tonic-gate return (ret);
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if (yptol_mode) {
1657c478bd9Sstevel@tonic-gate if (SUCCESS == update_entry_if_required(map, &key)) {
1667c478bd9Sstevel@tonic-gate /* Update thinks we should return something */
1677c478bd9Sstevel@tonic-gate ret = dbm_fetch(map->entries, key);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate } else {
1707c478bd9Sstevel@tonic-gate /* Non yptol mode do a normal fetch */
1717c478bd9Sstevel@tonic-gate ret = dbm_fetch(map->entries, key);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate return (ret);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_fetch_noupdate()
1817c478bd9Sstevel@tonic-gate *
1827c478bd9Sstevel@tonic-gate * DESCRIPTION: A special version of shim_dbm_fetch() that never checks TTLs
1837c478bd9Sstevel@tonic-gate * or updates entries.
1847c478bd9Sstevel@tonic-gate *
1857c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
1867c478bd9Sstevel@tonic-gate *
1877c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
1887c478bd9Sstevel@tonic-gate *
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate datum
shim_dbm_fetch_noupdate(DBM * db,datum key)1917c478bd9Sstevel@tonic-gate shim_dbm_fetch_noupdate(DBM *db, datum key)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate datum ret = {0, NULL};
1947c478bd9Sstevel@tonic-gate map_ctrl *map;
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /* Get the map control block */
1977c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
1987c478bd9Sstevel@tonic-gate if (map == NULL)
1997c478bd9Sstevel@tonic-gate return (ret);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /* Not updating so no need to lock */
2027c478bd9Sstevel@tonic-gate ret = dbm_fetch(map->entries, key);
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate return (ret);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_firstkey()
2097c478bd9Sstevel@tonic-gate *
2107c478bd9Sstevel@tonic-gate * DESCRIPTION: Get firstkey in an enumeration. If the map is out of date then
2117c478bd9Sstevel@tonic-gate * this is the time to scan it and see if any new entries have been
2127c478bd9Sstevel@tonic-gate * created.
2137c478bd9Sstevel@tonic-gate *
2147c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
2157c478bd9Sstevel@tonic-gate *
2167c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
2177c478bd9Sstevel@tonic-gate *
2187c478bd9Sstevel@tonic-gate */
2197c478bd9Sstevel@tonic-gate datum
shim_dbm_firstkey(DBM * db)2207c478bd9Sstevel@tonic-gate shim_dbm_firstkey(DBM *db)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate int count;
2237c478bd9Sstevel@tonic-gate bool_t wait_flag;
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate datum ret = {0, NULL};
2267c478bd9Sstevel@tonic-gate map_ctrl *map;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate /* Lock the map */
2297c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
2307c478bd9Sstevel@tonic-gate if (map == NULL)
2317c478bd9Sstevel@tonic-gate return (ret);
2327c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
2337c478bd9Sstevel@tonic-gate return (ret);
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate if (yptol_mode) {
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * Due to the limitations in the hashing algorithm ypxfrd
2387c478bd9Sstevel@tonic-gate * may end up waiting on the wrong update. It must thus loop
2397c478bd9Sstevel@tonic-gate * until the right map has been updated.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate for (count = 0; has_map_expired(map) &&
2427c478bd9Sstevel@tonic-gate (MAX_UPDATE_ATTEMPTS > count); count++) {
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * Ideally ypxfr should wait for the map update
2457c478bd9Sstevel@tonic-gate * to complete i.e. pass ypxfrd_flag into
2467c478bd9Sstevel@tonic-gate * update_map_if_required(). This cannot be done
2477c478bd9Sstevel@tonic-gate * because if there is a large map update the client
2487c478bd9Sstevel@tonic-gate * side, ypxfr, can time out while waiting.
2497c478bd9Sstevel@tonic-gate */
2507c478bd9Sstevel@tonic-gate wait_flag = FALSE;
2517c478bd9Sstevel@tonic-gate update_map_if_required(map, wait_flag);
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate if (wait_flag) {
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * Because ypxfrd does weird things with DBMs
2567c478bd9Sstevel@tonic-gate * internal structures it's a good idea to
2577c478bd9Sstevel@tonic-gate * reopen here. (Code that uses the real DBM
2587c478bd9Sstevel@tonic-gate * API appears not to need this.)
2597c478bd9Sstevel@tonic-gate *
2607c478bd9Sstevel@tonic-gate * This should not be necessary all we have
2617c478bd9Sstevel@tonic-gate * done is 'mv' the new file over the old one.
2627c478bd9Sstevel@tonic-gate * Open handles should get the old data but if
2637c478bd9Sstevel@tonic-gate * these lines are removed the first ypxfrd
2647c478bd9Sstevel@tonic-gate * read access fail with bad file handle.
2657c478bd9Sstevel@tonic-gate *
2667c478bd9Sstevel@tonic-gate * NOTE : If we don't wait, because of the
2677c478bd9Sstevel@tonic-gate * ypxfr timeout problem, there is no point
2687c478bd9Sstevel@tonic-gate * doing this.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate dbm_close(map->entries);
2717c478bd9Sstevel@tonic-gate dbm_close(map->ttl);
2727c478bd9Sstevel@tonic-gate if (FAILURE == open_yptol_files(map)) {
2737c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
2747c478bd9Sstevel@tonic-gate "Could not reopen DBM files");
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate } else {
2777c478bd9Sstevel@tonic-gate /* For daemons that don't wait just try once */
2787c478bd9Sstevel@tonic-gate break;
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (MAX_UPDATE_ATTEMPTS < count)
2837c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
2847c478bd9Sstevel@tonic-gate "Cannot update map %s", map->map_name);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate ret = dbm_firstkey(map->entries);
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate /* Move key data out of static memory. See NOTE in file header above */
2907c478bd9Sstevel@tonic-gate if (yptol_mode) {
2917c478bd9Sstevel@tonic-gate set_key_data(map, &ret);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate return (ret);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_nextkey()
3007c478bd9Sstevel@tonic-gate *
3017c478bd9Sstevel@tonic-gate * DESCRIPTION: Get next key in an enumeration. Since updating an entry would
3027c478bd9Sstevel@tonic-gate * invalidate the enumeration we never do it.
3037c478bd9Sstevel@tonic-gate *
3047c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
3057c478bd9Sstevel@tonic-gate *
3067c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
3077c478bd9Sstevel@tonic-gate *
3087c478bd9Sstevel@tonic-gate */
3097c478bd9Sstevel@tonic-gate datum
shim_dbm_nextkey(DBM * db)3107c478bd9Sstevel@tonic-gate shim_dbm_nextkey(DBM *db)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate datum ret;
3137c478bd9Sstevel@tonic-gate map_ctrl *map;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate /* Lock the map */
3167c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
3177c478bd9Sstevel@tonic-gate if (map == NULL)
3187c478bd9Sstevel@tonic-gate return (ret);
3197c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
3207c478bd9Sstevel@tonic-gate return (ret);
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate ret = dbm_nextkey(map->entries);
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate /* Move key data out of static memory. See NOTE in file header above */
3257c478bd9Sstevel@tonic-gate if (yptol_mode) {
3267c478bd9Sstevel@tonic-gate set_key_data(map, &ret);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate return (ret);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_do_nextkey()
3367c478bd9Sstevel@tonic-gate *
3377c478bd9Sstevel@tonic-gate * DESCRIPTION: Get next key in an enumeration. Since updating an entry would
3387c478bd9Sstevel@tonic-gate * invalidate the enumeration we never do it.
3397c478bd9Sstevel@tonic-gate *
3407c478bd9Sstevel@tonic-gate * NOTE : dbm_do_nextkey is not a documented or legal DBM API.
3417c478bd9Sstevel@tonic-gate * Despite this the existing NIS code calls it. One gross hack
3427c478bd9Sstevel@tonic-gate * deserves another so we have this extra shim function to handle
3437c478bd9Sstevel@tonic-gate * the illegal call.
3447c478bd9Sstevel@tonic-gate *
3457c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
3467c478bd9Sstevel@tonic-gate *
3477c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
3487c478bd9Sstevel@tonic-gate *
3497c478bd9Sstevel@tonic-gate */
3507c478bd9Sstevel@tonic-gate datum
shim_dbm_do_nextkey(DBM * db,datum inkey)3517c478bd9Sstevel@tonic-gate shim_dbm_do_nextkey(DBM *db, datum inkey)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate datum ret;
3547c478bd9Sstevel@tonic-gate map_ctrl *map;
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate /* Lock the map */
3577c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
3587c478bd9Sstevel@tonic-gate if (map == NULL)
3597c478bd9Sstevel@tonic-gate return (ret);
3607c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
3617c478bd9Sstevel@tonic-gate return (ret);
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate ret = dbm_do_nextkey(map->entries, inkey);
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate /* Move key data out of static memory. See NOTE in file header above */
3667c478bd9Sstevel@tonic-gate if (yptol_mode) {
3677c478bd9Sstevel@tonic-gate set_key_data(map, &ret);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate return (ret);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_open()
3767c478bd9Sstevel@tonic-gate *
3777c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
3787c478bd9Sstevel@tonic-gate *
3797c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
3807c478bd9Sstevel@tonic-gate *
3817c478bd9Sstevel@tonic-gate */
3827c478bd9Sstevel@tonic-gate DBM *
shim_dbm_open(const char * file,int open_flags,mode_t file_mode)3837c478bd9Sstevel@tonic-gate shim_dbm_open(const char *file, int open_flags, mode_t file_mode)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate map_ctrl *map;
3867c478bd9Sstevel@tonic-gate suc_code ret = FAILURE;
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate /* Find or create map_ctrl for this map */
3897c478bd9Sstevel@tonic-gate map = create_map_ctrl((char *)file);
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate if (map == NULL)
3927c478bd9Sstevel@tonic-gate return (NULL);
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate /* Lock map */
3957c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
3967c478bd9Sstevel@tonic-gate return (NULL);
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate /* Remember flags and mode in case we have to reopen */
3997c478bd9Sstevel@tonic-gate map->open_flags = open_flags;
4007c478bd9Sstevel@tonic-gate map->open_mode = file_mode;
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate if (yptol_mode) {
4037c478bd9Sstevel@tonic-gate ret = open_yptol_files(map);
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate * This is a good place to check that the
4077c478bd9Sstevel@tonic-gate * equivalent old style map file has not been
4087c478bd9Sstevel@tonic-gate * updated.
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate if (SUCCESS == ret)
4117c478bd9Sstevel@tonic-gate check_old_map_date(map);
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate } else {
4147c478bd9Sstevel@tonic-gate /* Open entries map */
4157c478bd9Sstevel@tonic-gate map->entries = dbm_open(map->map_path, map->open_flags,
4167c478bd9Sstevel@tonic-gate map->open_mode);
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate if (NULL != map->entries)
4197c478bd9Sstevel@tonic-gate ret = SUCCESS;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /* If we were not successful unravel what we have done so far */
4237c478bd9Sstevel@tonic-gate if (ret != SUCCESS) {
4247c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
4257c478bd9Sstevel@tonic-gate free_map_ctrl(map);
4267c478bd9Sstevel@tonic-gate return (NULL);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate /* Return map_ctrl pointer as a DBM *. To the outside world it is */
4327c478bd9Sstevel@tonic-gate /* opaque. */
4337c478bd9Sstevel@tonic-gate return ((DBM *)map);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_store()
4387c478bd9Sstevel@tonic-gate *
4397c478bd9Sstevel@tonic-gate * DESCRIPTION: Shim for dbm_store.
4407c478bd9Sstevel@tonic-gate *
4417c478bd9Sstevel@tonic-gate * In N2L mode if we are asked to store in DBM_INSERT mode
4427c478bd9Sstevel@tonic-gate * then first an attempt is made to write to the DIT (in the same
4437c478bd9Sstevel@tonic-gate * mode). If this is successful then the value is forced into DBM
4447c478bd9Sstevel@tonic-gate * using DBM_REPLACE. This is because the DIT is authoritative.
4457c478bd9Sstevel@tonic-gate * The success of failure of an 'insert' is determined by the
4467c478bd9Sstevel@tonic-gate * presence or otherwise of an entry in the DIT not DBM.
4477c478bd9Sstevel@tonic-gate *
4487c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call.
4497c478bd9Sstevel@tonic-gate *
4507c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call.
4517c478bd9Sstevel@tonic-gate *
4527c478bd9Sstevel@tonic-gate */
4537c478bd9Sstevel@tonic-gate int
shim_dbm_store(DBM * db,datum key,datum content,int store_mode)4547c478bd9Sstevel@tonic-gate shim_dbm_store(DBM *db, datum key, datum content, int store_mode)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate int ret;
4577c478bd9Sstevel@tonic-gate map_ctrl *map;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate /* Get map name */
4607c478bd9Sstevel@tonic-gate map = get_map_ctrl(db);
4617c478bd9Sstevel@tonic-gate if (map == NULL)
4627c478bd9Sstevel@tonic-gate return (FAILURE);
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate if (yptol_mode) {
4657c478bd9Sstevel@tonic-gate /* Write to the DIT before doing anything else */
4667c478bd9Sstevel@tonic-gate if (!write_to_dit(map->map_name, map->domain, key, content,
4677c478bd9Sstevel@tonic-gate DBM_REPLACE == store_mode, FALSE))
4687c478bd9Sstevel@tonic-gate return (FAILURE);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate /* Lock the map */
4727c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map))
4737c478bd9Sstevel@tonic-gate return (FAILURE);
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate if (yptol_mode) {
4767c478bd9Sstevel@tonic-gate if (!is_map_updating(map)) {
4777c478bd9Sstevel@tonic-gate ret = dbm_store(map->entries, key, content,
4787c478bd9Sstevel@tonic-gate DBM_REPLACE);
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate if (SUCCESS == ret)
4817c478bd9Sstevel@tonic-gate /* Update TTL */
4827c478bd9Sstevel@tonic-gate update_entry_ttl(map, &key, TTL_RAND);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate } else {
4857c478bd9Sstevel@tonic-gate ret = dbm_store(map->entries, key, content, store_mode);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate unlock_map_ctrl(map);
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate return (ret);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * FUNCTION : shim_exit()
4957c478bd9Sstevel@tonic-gate *
4967c478bd9Sstevel@tonic-gate * DESCRIPTION: Intercepts exit() calls made by N2L compatible NIS components.
4977c478bd9Sstevel@tonic-gate * This is required because any call to the shim_dbm... series
4987c478bd9Sstevel@tonic-gate * of functions may have started an update thread. If the process
4997c478bd9Sstevel@tonic-gate * exits normally then this thread may be killed before it can
5007c478bd9Sstevel@tonic-gate * complete its work. We thus wait here for the thread to complete.
5017c478bd9Sstevel@tonic-gate *
5027c478bd9Sstevel@tonic-gate * GIVEN : Same arg as exit()
5037c478bd9Sstevel@tonic-gate *
5047c478bd9Sstevel@tonic-gate * RETURNS : Never
5057c478bd9Sstevel@tonic-gate */
5067c478bd9Sstevel@tonic-gate void
shim_exit(int code)5077c478bd9Sstevel@tonic-gate shim_exit(int code)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate thr_join(NULL, NULL, NULL);
5107c478bd9Sstevel@tonic-gate exit(code);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate * FUNCTION : init_yptol_flag()
5157c478bd9Sstevel@tonic-gate *
5167c478bd9Sstevel@tonic-gate * DESCRIPTION: Initializes two flags these are similar but their function is
5177c478bd9Sstevel@tonic-gate * subtly different.
5187c478bd9Sstevel@tonic-gate *
5197c478bd9Sstevel@tonic-gate * yp2ldap tells the mapping system if it is to work in NIS or
5207c478bd9Sstevel@tonic-gate * NIS+ mode. For N2L this is always set to NIS mode.
5217c478bd9Sstevel@tonic-gate *
5227c478bd9Sstevel@tonic-gate * yptol tells the shim if it is to work in N2L or traditional
5237c478bd9Sstevel@tonic-gate * NIS mode. For N2L this is turned on if the N2L mapping file
5247c478bd9Sstevel@tonic-gate * is found to be present. In NIS+ mode it is meaningless.
5257c478bd9Sstevel@tonic-gate */
5267c478bd9Sstevel@tonic-gate void
init_yptol_flag()5277c478bd9Sstevel@tonic-gate init_yptol_flag()
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate * yp2ldap is used to switch appropriate code in the
5317c478bd9Sstevel@tonic-gate * common libnisdb library used by rpc.nisd and ypserv.
5327c478bd9Sstevel@tonic-gate */
5337c478bd9Sstevel@tonic-gate yp2ldap = 1;
5347c478bd9Sstevel@tonic-gate yptol_mode = is_yptol_mode();
535*4a190493Ssdussud /*
536*4a190493Ssdussud * Use the new lock mapping mechanism
537*4a190493Ssdussud * if in N2L mode.
538*4a190493Ssdussud */
539*4a190493Ssdussud yptol_newlock = yptol_mode;
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate * FUNCTION : set_yxfrd_flag()
5447c478bd9Sstevel@tonic-gate */
5457c478bd9Sstevel@tonic-gate void
set_ypxfrd_flag()5467c478bd9Sstevel@tonic-gate set_ypxfrd_flag()
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate ypxfrd_flag = TRUE;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate /*
5527c478bd9Sstevel@tonic-gate * FUNCTION : check_old_map_date()
5537c478bd9Sstevel@tonic-gate *
5547c478bd9Sstevel@tonic-gate * DESCRIPTION: Checks that an old style map has not been updated. If it has
5557c478bd9Sstevel@tonic-gate * then ypmake has probably erroneously been run and an error is
5567c478bd9Sstevel@tonic-gate * logged.
5577c478bd9Sstevel@tonic-gate *
5587c478bd9Sstevel@tonic-gate * GIVEN : A map_ctrl containing details of the NEW STYLE map.
5597c478bd9Sstevel@tonic-gate *
5607c478bd9Sstevel@tonic-gate * RETURNS : Nothing
5617c478bd9Sstevel@tonic-gate */
5627c478bd9Sstevel@tonic-gate void
check_old_map_date(map_ctrl * map)5637c478bd9Sstevel@tonic-gate check_old_map_date(map_ctrl *map)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate datum key;
5667c478bd9Sstevel@tonic-gate datum value;
5677c478bd9Sstevel@tonic-gate struct stat stats;
5687c478bd9Sstevel@tonic-gate time_t old_time;
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate /* Get date of last update */
5717c478bd9Sstevel@tonic-gate if (0 != stat(map->trad_map_path, &stats)) {
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate * No problem. We have a new style map but no old style map
5747c478bd9Sstevel@tonic-gate * this will occur if the original data came from native LDAP
5757c478bd9Sstevel@tonic-gate * instead of NIS.
5767c478bd9Sstevel@tonic-gate */
5777c478bd9Sstevel@tonic-gate return;
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /* Set up datum with key for recorded old map update time */
5817c478bd9Sstevel@tonic-gate key.dsize = strlen(MAP_OLD_MAP_DATE_KEY);
5827c478bd9Sstevel@tonic-gate key.dptr = MAP_OLD_MAP_DATE_KEY;
5837c478bd9Sstevel@tonic-gate value = dbm_fetch(map->ttl, key);
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate if (NULL != value.dptr) {
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate * Because dptr may not be int aligned need to build an int
5887c478bd9Sstevel@tonic-gate * out of what it points to or will get a bus error.
5897c478bd9Sstevel@tonic-gate */
5907c478bd9Sstevel@tonic-gate bcopy(value.dptr, &old_time, sizeof (time_t));
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate /* Do the comparison */
5947c478bd9Sstevel@tonic-gate if (stats.st_mtime <= old_time) {
5957c478bd9Sstevel@tonic-gate /* All is well, has not been updated */
5967c478bd9Sstevel@tonic-gate return;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate /* If we get here the file has been updated */
6007c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
6017c478bd9Sstevel@tonic-gate "Caution. ypmake may have been run in N2L "
6027c478bd9Sstevel@tonic-gate "mode. This will NOT initiate a NIS map push. In "
6037c478bd9Sstevel@tonic-gate "this mode pushes should be initiated with yppush");
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate * If we get here then either the file was updated or there was not
6087c478bd9Sstevel@tonic-gate * a valid old map date (no problem, maybe this is the first time we
6097c478bd9Sstevel@tonic-gate * checked). In either case the old map date entry must be update.
6107c478bd9Sstevel@tonic-gate */
6117c478bd9Sstevel@tonic-gate value.dptr = (char *)&(stats.st_mtime);
6127c478bd9Sstevel@tonic-gate value.dsize = sizeof (time_t);
6137c478bd9Sstevel@tonic-gate dbm_store(map->ttl, key, value, DBM_REPLACE);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate * FUNCTION : init_lock_system()
6187c478bd9Sstevel@tonic-gate *
6197c478bd9Sstevel@tonic-gate * DESCRIPTION: Initializes all the systems related to map locking. This must
6207c478bd9Sstevel@tonic-gate * be called before any access to the shim functions.
6217c478bd9Sstevel@tonic-gate *
6227c478bd9Sstevel@tonic-gate * GIVEN : A flag indicating if we are being called from ypserv, which does
6237c478bd9Sstevel@tonic-gate * not wait for map updates to complete, or other NIS components
6247c478bd9Sstevel@tonic-gate * which do.
6257c478bd9Sstevel@tonic-gate *
6267c478bd9Sstevel@tonic-gate * RETURNS : TRUE = Everything worked
6277c478bd9Sstevel@tonic-gate * FALSE = There were problems
6287c478bd9Sstevel@tonic-gate */
6297c478bd9Sstevel@tonic-gate bool_t
init_lock_system(bool_t ypxfrd)6307c478bd9Sstevel@tonic-gate init_lock_system(bool_t ypxfrd)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate /* Remember what called us */
6337c478bd9Sstevel@tonic-gate if (ypxfrd)
6347c478bd9Sstevel@tonic-gate set_ypxfrd_flag();
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate * Remember PID of process which called us. This enables update threads
6387c478bd9Sstevel@tonic-gate * created by YP children to be handled differently to those created
6397c478bd9Sstevel@tonic-gate * by YP parents.
6407c478bd9Sstevel@tonic-gate */
6417c478bd9Sstevel@tonic-gate parent_pid = getpid();
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate /* Init map locks */
6447c478bd9Sstevel@tonic-gate if (!init_lock_map()) {
6457c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
6467c478bd9Sstevel@tonic-gate "Failed to init process synchronization");
6477c478bd9Sstevel@tonic-gate return (FALSE);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate /* If we are in yptol mode set flag indicating the fact */
6517c478bd9Sstevel@tonic-gate init_yptol_flag();
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate * If boot random number system. For now go for reproducible
6557c478bd9Sstevel@tonic-gate * random numbers.
6567c478bd9Sstevel@tonic-gate */
6577c478bd9Sstevel@tonic-gate srand48(0x12345678);
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate * If not N2L mode then no error but do not bother initializing update
6617c478bd9Sstevel@tonic-gate * flags.
6627c478bd9Sstevel@tonic-gate */
6637c478bd9Sstevel@tonic-gate if (yptol_mode) {
6647c478bd9Sstevel@tonic-gate if (!init_update_lock_map()) {
6657c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
6667c478bd9Sstevel@tonic-gate "Failed to init update synchronization");
6677c478bd9Sstevel@tonic-gate return (FALSE);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate return (TRUE);
6727c478bd9Sstevel@tonic-gate }
673