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