1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Contains the top level shim hook functions. These must have 31*7c478bd9Sstevel@tonic-gate * identical interfaces to the equivalent standard dbm calls. 32*7c478bd9Sstevel@tonic-gate * 33*7c478bd9Sstevel@tonic-gate * Unfortunately many of these will do a copy of a datum structure 34*7c478bd9Sstevel@tonic-gate * on return. This is a side effect of the original DBM function 35*7c478bd9Sstevel@tonic-gate * being written to pass structures rather than pointers. 36*7c478bd9Sstevel@tonic-gate * 37*7c478bd9Sstevel@tonic-gate * NOTE : There is a major bug/feature in dbm. A key obtained by 38*7c478bd9Sstevel@tonic-gate * dbm_nextkey() of dbm_firstkey() cannot be passed to dbm_store(). 39*7c478bd9Sstevel@tonic-gate * When the store occurs dbm's internal memory get's reorganized 40*7c478bd9Sstevel@tonic-gate * and the static strings pointed to by the key are destroyed. The 41*7c478bd9Sstevel@tonic-gate * data is then stored in the wrong place. We attempt to get round 42*7c478bd9Sstevel@tonic-gate * this by dbm_firstkey() and dbm_nextkey() making a copy of the 43*7c478bd9Sstevel@tonic-gate * key data in malloced memory. This is freed when map_ctrl is 44*7c478bd9Sstevel@tonic-gate * freed. 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate #include <unistd.h> 48*7c478bd9Sstevel@tonic-gate #include <syslog.h> 49*7c478bd9Sstevel@tonic-gate #include <ndbm.h> 50*7c478bd9Sstevel@tonic-gate #include <strings.h> 51*7c478bd9Sstevel@tonic-gate #include "ypsym.h" 52*7c478bd9Sstevel@tonic-gate #include "ypdefs.h" 53*7c478bd9Sstevel@tonic-gate #include "shim.h" 54*7c478bd9Sstevel@tonic-gate #include "yptol.h" 55*7c478bd9Sstevel@tonic-gate #include "../ldap_parse.h" 56*7c478bd9Sstevel@tonic-gate #include "../ldap_util.h" 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate /* 59*7c478bd9Sstevel@tonic-gate * Switch on DBM support 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate USE_DBM 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate /* 64*7c478bd9Sstevel@tonic-gate * Globals 65*7c478bd9Sstevel@tonic-gate */ 66*7c478bd9Sstevel@tonic-gate extern bool_t yptol_mode = FALSE; /* Set if in N2L mode */ 67*7c478bd9Sstevel@tonic-gate extern bool_t ypxfrd_flag = FALSE; /* Set if called from ypxfrd */ 68*7c478bd9Sstevel@tonic-gate pid_t parent_pid; /* ID of calling parent process */ 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate /* 72*7c478bd9Sstevel@tonic-gate * Decs 73*7c478bd9Sstevel@tonic-gate */ 74*7c478bd9Sstevel@tonic-gate void check_old_map_date(map_ctrl *); 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate /* 77*7c478bd9Sstevel@tonic-gate * Constants 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate /* Number of times to try to update a map before giving up */ 80*7c478bd9Sstevel@tonic-gate /* #define MAX_UPDATE_ATTEMPTS 3 */ 81*7c478bd9Sstevel@tonic-gate #define MAX_UPDATE_ATTEMPTS 1 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* 84*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_close(); 85*7c478bd9Sstevel@tonic-gate * 86*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 87*7c478bd9Sstevel@tonic-gate * 88*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 89*7c478bd9Sstevel@tonic-gate * 90*7c478bd9Sstevel@tonic-gate */ 91*7c478bd9Sstevel@tonic-gate void 92*7c478bd9Sstevel@tonic-gate shim_dbm_close(DBM *db) 93*7c478bd9Sstevel@tonic-gate { 94*7c478bd9Sstevel@tonic-gate map_ctrl *map; 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate /* Lock the map */ 97*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 98*7c478bd9Sstevel@tonic-gate if (map == NULL) 99*7c478bd9Sstevel@tonic-gate return; 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate free_map_ctrl(map); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate /* 105*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_delete(); 106*7c478bd9Sstevel@tonic-gate * 107*7c478bd9Sstevel@tonic-gate * DESCRIPTION: This function is currently unused but is present so that the 108*7c478bd9Sstevel@tonic-gate * set of shim_dbm_xxx() interfaces is complete if required in 109*7c478bd9Sstevel@tonic-gate * future. 110*7c478bd9Sstevel@tonic-gate * 111*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 112*7c478bd9Sstevel@tonic-gate * 113*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 114*7c478bd9Sstevel@tonic-gate * 115*7c478bd9Sstevel@tonic-gate */ 116*7c478bd9Sstevel@tonic-gate int 117*7c478bd9Sstevel@tonic-gate shim_dbm_delete(DBM *db, datum key) 118*7c478bd9Sstevel@tonic-gate { 119*7c478bd9Sstevel@tonic-gate int ret; 120*7c478bd9Sstevel@tonic-gate map_ctrl *map; 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate /* Lock the map */ 123*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 124*7c478bd9Sstevel@tonic-gate if (map == NULL) 125*7c478bd9Sstevel@tonic-gate return (FAILURE); 126*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 127*7c478bd9Sstevel@tonic-gate return (FAILURE); 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 130*7c478bd9Sstevel@tonic-gate /* Delete from and ttl map. Not a huge disaster if it fails. */ 131*7c478bd9Sstevel@tonic-gate dbm_delete(map->ttl, key); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate ret = dbm_delete(map->entries, key); 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate return (ret); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate /* 143*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_fetch() 144*7c478bd9Sstevel@tonic-gate * 145*7c478bd9Sstevel@tonic-gate * DESCRIPTION: N2L function used to handle 'normal' dbm_fetch() operations. 146*7c478bd9Sstevel@tonic-gate * 147*7c478bd9Sstevel@tonic-gate * INPUTS: First two identical to equivalent dbm call. 148*7c478bd9Sstevel@tonic-gate * 149*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 150*7c478bd9Sstevel@tonic-gate * 151*7c478bd9Sstevel@tonic-gate */ 152*7c478bd9Sstevel@tonic-gate datum 153*7c478bd9Sstevel@tonic-gate shim_dbm_fetch(DBM *db, datum key) 154*7c478bd9Sstevel@tonic-gate { 155*7c478bd9Sstevel@tonic-gate datum ret = {0, NULL}; 156*7c478bd9Sstevel@tonic-gate map_ctrl *map; 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate /* Lock the map */ 159*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 160*7c478bd9Sstevel@tonic-gate if (map == NULL) 161*7c478bd9Sstevel@tonic-gate return (ret); 162*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 163*7c478bd9Sstevel@tonic-gate return (ret); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 166*7c478bd9Sstevel@tonic-gate if (SUCCESS == update_entry_if_required(map, &key)) { 167*7c478bd9Sstevel@tonic-gate /* Update thinks we should return something */ 168*7c478bd9Sstevel@tonic-gate ret = dbm_fetch(map->entries, key); 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate } else { 171*7c478bd9Sstevel@tonic-gate /* Non yptol mode do a normal fetch */ 172*7c478bd9Sstevel@tonic-gate ret = dbm_fetch(map->entries, key); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate return (ret); 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate /* 181*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_fetch_noupdate() 182*7c478bd9Sstevel@tonic-gate * 183*7c478bd9Sstevel@tonic-gate * DESCRIPTION: A special version of shim_dbm_fetch() that never checks TTLs 184*7c478bd9Sstevel@tonic-gate * or updates entries. 185*7c478bd9Sstevel@tonic-gate * 186*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 187*7c478bd9Sstevel@tonic-gate * 188*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 189*7c478bd9Sstevel@tonic-gate * 190*7c478bd9Sstevel@tonic-gate */ 191*7c478bd9Sstevel@tonic-gate datum 192*7c478bd9Sstevel@tonic-gate shim_dbm_fetch_noupdate(DBM *db, datum key) 193*7c478bd9Sstevel@tonic-gate { 194*7c478bd9Sstevel@tonic-gate datum ret = {0, NULL}; 195*7c478bd9Sstevel@tonic-gate map_ctrl *map; 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate /* Get the map control block */ 198*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 199*7c478bd9Sstevel@tonic-gate if (map == NULL) 200*7c478bd9Sstevel@tonic-gate return (ret); 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate /* Not updating so no need to lock */ 203*7c478bd9Sstevel@tonic-gate ret = dbm_fetch(map->entries, key); 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate return (ret); 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate /* 209*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_firstkey() 210*7c478bd9Sstevel@tonic-gate * 211*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Get firstkey in an enumeration. If the map is out of date then 212*7c478bd9Sstevel@tonic-gate * this is the time to scan it and see if any new entries have been 213*7c478bd9Sstevel@tonic-gate * created. 214*7c478bd9Sstevel@tonic-gate * 215*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 216*7c478bd9Sstevel@tonic-gate * 217*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 218*7c478bd9Sstevel@tonic-gate * 219*7c478bd9Sstevel@tonic-gate */ 220*7c478bd9Sstevel@tonic-gate datum 221*7c478bd9Sstevel@tonic-gate shim_dbm_firstkey(DBM *db) 222*7c478bd9Sstevel@tonic-gate { 223*7c478bd9Sstevel@tonic-gate int count; 224*7c478bd9Sstevel@tonic-gate bool_t wait_flag; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate datum ret = {0, NULL}; 227*7c478bd9Sstevel@tonic-gate map_ctrl *map; 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate /* Lock the map */ 230*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 231*7c478bd9Sstevel@tonic-gate if (map == NULL) 232*7c478bd9Sstevel@tonic-gate return (ret); 233*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 234*7c478bd9Sstevel@tonic-gate return (ret); 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 237*7c478bd9Sstevel@tonic-gate /* 238*7c478bd9Sstevel@tonic-gate * Due to the limitations in the hashing algorithm ypxfrd 239*7c478bd9Sstevel@tonic-gate * may end up waiting on the wrong update. It must thus loop 240*7c478bd9Sstevel@tonic-gate * until the right map has been updated. 241*7c478bd9Sstevel@tonic-gate */ 242*7c478bd9Sstevel@tonic-gate for (count = 0; has_map_expired(map) && 243*7c478bd9Sstevel@tonic-gate (MAX_UPDATE_ATTEMPTS > count); count++) { 244*7c478bd9Sstevel@tonic-gate /* 245*7c478bd9Sstevel@tonic-gate * Ideally ypxfr should wait for the map update 246*7c478bd9Sstevel@tonic-gate * to complete i.e. pass ypxfrd_flag into 247*7c478bd9Sstevel@tonic-gate * update_map_if_required(). This cannot be done 248*7c478bd9Sstevel@tonic-gate * because if there is a large map update the client 249*7c478bd9Sstevel@tonic-gate * side, ypxfr, can time out while waiting. 250*7c478bd9Sstevel@tonic-gate */ 251*7c478bd9Sstevel@tonic-gate wait_flag = FALSE; 252*7c478bd9Sstevel@tonic-gate update_map_if_required(map, wait_flag); 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate if (wait_flag) { 255*7c478bd9Sstevel@tonic-gate /* 256*7c478bd9Sstevel@tonic-gate * Because ypxfrd does weird things with DBMs 257*7c478bd9Sstevel@tonic-gate * internal structures it's a good idea to 258*7c478bd9Sstevel@tonic-gate * reopen here. (Code that uses the real DBM 259*7c478bd9Sstevel@tonic-gate * API appears not to need this.) 260*7c478bd9Sstevel@tonic-gate * 261*7c478bd9Sstevel@tonic-gate * This should not be necessary all we have 262*7c478bd9Sstevel@tonic-gate * done is 'mv' the new file over the old one. 263*7c478bd9Sstevel@tonic-gate * Open handles should get the old data but if 264*7c478bd9Sstevel@tonic-gate * these lines are removed the first ypxfrd 265*7c478bd9Sstevel@tonic-gate * read access fail with bad file handle. 266*7c478bd9Sstevel@tonic-gate * 267*7c478bd9Sstevel@tonic-gate * NOTE : If we don't wait, because of the 268*7c478bd9Sstevel@tonic-gate * ypxfr timeout problem, there is no point 269*7c478bd9Sstevel@tonic-gate * doing this. 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate dbm_close(map->entries); 272*7c478bd9Sstevel@tonic-gate dbm_close(map->ttl); 273*7c478bd9Sstevel@tonic-gate if (FAILURE == open_yptol_files(map)) { 274*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, 275*7c478bd9Sstevel@tonic-gate "Could not reopen DBM files"); 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate } else { 278*7c478bd9Sstevel@tonic-gate /* For daemons that don't wait just try once */ 279*7c478bd9Sstevel@tonic-gate break; 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate if (MAX_UPDATE_ATTEMPTS < count) 284*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, 285*7c478bd9Sstevel@tonic-gate "Cannot update map %s", map->map_name); 286*7c478bd9Sstevel@tonic-gate } 287*7c478bd9Sstevel@tonic-gate 288*7c478bd9Sstevel@tonic-gate ret = dbm_firstkey(map->entries); 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate /* Move key data out of static memory. See NOTE in file header above */ 291*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 292*7c478bd9Sstevel@tonic-gate set_key_data(map, &ret); 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate return (ret); 297*7c478bd9Sstevel@tonic-gate } 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate /* 300*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_nextkey() 301*7c478bd9Sstevel@tonic-gate * 302*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Get next key in an enumeration. Since updating an entry would 303*7c478bd9Sstevel@tonic-gate * invalidate the enumeration we never do it. 304*7c478bd9Sstevel@tonic-gate * 305*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 306*7c478bd9Sstevel@tonic-gate * 307*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 308*7c478bd9Sstevel@tonic-gate * 309*7c478bd9Sstevel@tonic-gate */ 310*7c478bd9Sstevel@tonic-gate datum 311*7c478bd9Sstevel@tonic-gate shim_dbm_nextkey(DBM *db) 312*7c478bd9Sstevel@tonic-gate { 313*7c478bd9Sstevel@tonic-gate datum ret; 314*7c478bd9Sstevel@tonic-gate map_ctrl *map; 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate /* Lock the map */ 317*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 318*7c478bd9Sstevel@tonic-gate if (map == NULL) 319*7c478bd9Sstevel@tonic-gate return (ret); 320*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 321*7c478bd9Sstevel@tonic-gate return (ret); 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate ret = dbm_nextkey(map->entries); 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate /* Move key data out of static memory. See NOTE in file header above */ 326*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 327*7c478bd9Sstevel@tonic-gate set_key_data(map, &ret); 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 331*7c478bd9Sstevel@tonic-gate 332*7c478bd9Sstevel@tonic-gate return (ret); 333*7c478bd9Sstevel@tonic-gate } 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate /* 336*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_do_nextkey() 337*7c478bd9Sstevel@tonic-gate * 338*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Get next key in an enumeration. Since updating an entry would 339*7c478bd9Sstevel@tonic-gate * invalidate the enumeration we never do it. 340*7c478bd9Sstevel@tonic-gate * 341*7c478bd9Sstevel@tonic-gate * NOTE : dbm_do_nextkey is not a documented or legal DBM API. 342*7c478bd9Sstevel@tonic-gate * Despite this the existing NIS code calls it. One gross hack 343*7c478bd9Sstevel@tonic-gate * deserves another so we have this extra shim function to handle 344*7c478bd9Sstevel@tonic-gate * the illegal call. 345*7c478bd9Sstevel@tonic-gate * 346*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 347*7c478bd9Sstevel@tonic-gate * 348*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 349*7c478bd9Sstevel@tonic-gate * 350*7c478bd9Sstevel@tonic-gate */ 351*7c478bd9Sstevel@tonic-gate datum 352*7c478bd9Sstevel@tonic-gate shim_dbm_do_nextkey(DBM *db, datum inkey) 353*7c478bd9Sstevel@tonic-gate { 354*7c478bd9Sstevel@tonic-gate datum ret; 355*7c478bd9Sstevel@tonic-gate map_ctrl *map; 356*7c478bd9Sstevel@tonic-gate 357*7c478bd9Sstevel@tonic-gate /* Lock the map */ 358*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 359*7c478bd9Sstevel@tonic-gate if (map == NULL) 360*7c478bd9Sstevel@tonic-gate return (ret); 361*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 362*7c478bd9Sstevel@tonic-gate return (ret); 363*7c478bd9Sstevel@tonic-gate 364*7c478bd9Sstevel@tonic-gate ret = dbm_do_nextkey(map->entries, inkey); 365*7c478bd9Sstevel@tonic-gate 366*7c478bd9Sstevel@tonic-gate /* Move key data out of static memory. See NOTE in file header above */ 367*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 368*7c478bd9Sstevel@tonic-gate set_key_data(map, &ret); 369*7c478bd9Sstevel@tonic-gate } 370*7c478bd9Sstevel@tonic-gate 371*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 372*7c478bd9Sstevel@tonic-gate 373*7c478bd9Sstevel@tonic-gate return (ret); 374*7c478bd9Sstevel@tonic-gate } 375*7c478bd9Sstevel@tonic-gate /* 376*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_open() 377*7c478bd9Sstevel@tonic-gate * 378*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 379*7c478bd9Sstevel@tonic-gate * 380*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 381*7c478bd9Sstevel@tonic-gate * 382*7c478bd9Sstevel@tonic-gate */ 383*7c478bd9Sstevel@tonic-gate DBM * 384*7c478bd9Sstevel@tonic-gate shim_dbm_open(const char *file, int open_flags, mode_t file_mode) 385*7c478bd9Sstevel@tonic-gate { 386*7c478bd9Sstevel@tonic-gate map_ctrl *map; 387*7c478bd9Sstevel@tonic-gate DBM *dbm_ptr; 388*7c478bd9Sstevel@tonic-gate suc_code ret = FAILURE; 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate /* Find or create map_ctrl for this map */ 391*7c478bd9Sstevel@tonic-gate map = create_map_ctrl((char *)file); 392*7c478bd9Sstevel@tonic-gate 393*7c478bd9Sstevel@tonic-gate if (map == NULL) 394*7c478bd9Sstevel@tonic-gate return (NULL); 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate /* Lock map */ 397*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 398*7c478bd9Sstevel@tonic-gate return (NULL); 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate /* Remember flags and mode in case we have to reopen */ 401*7c478bd9Sstevel@tonic-gate map->open_flags = open_flags; 402*7c478bd9Sstevel@tonic-gate map->open_mode = file_mode; 403*7c478bd9Sstevel@tonic-gate 404*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 405*7c478bd9Sstevel@tonic-gate ret = open_yptol_files(map); 406*7c478bd9Sstevel@tonic-gate 407*7c478bd9Sstevel@tonic-gate /* 408*7c478bd9Sstevel@tonic-gate * This is a good place to check that the 409*7c478bd9Sstevel@tonic-gate * equivalent old style map file has not been 410*7c478bd9Sstevel@tonic-gate * updated. 411*7c478bd9Sstevel@tonic-gate */ 412*7c478bd9Sstevel@tonic-gate if (SUCCESS == ret) 413*7c478bd9Sstevel@tonic-gate check_old_map_date(map); 414*7c478bd9Sstevel@tonic-gate 415*7c478bd9Sstevel@tonic-gate } else { 416*7c478bd9Sstevel@tonic-gate /* Open entries map */ 417*7c478bd9Sstevel@tonic-gate map->entries = dbm_open(map->map_path, map->open_flags, 418*7c478bd9Sstevel@tonic-gate map->open_mode); 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate if (NULL != map->entries) 421*7c478bd9Sstevel@tonic-gate ret = SUCCESS; 422*7c478bd9Sstevel@tonic-gate } 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate /* If we were not successful unravel what we have done so far */ 425*7c478bd9Sstevel@tonic-gate if (ret != SUCCESS) { 426*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 427*7c478bd9Sstevel@tonic-gate free_map_ctrl(map); 428*7c478bd9Sstevel@tonic-gate return (NULL); 429*7c478bd9Sstevel@tonic-gate } 430*7c478bd9Sstevel@tonic-gate 431*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 432*7c478bd9Sstevel@tonic-gate 433*7c478bd9Sstevel@tonic-gate /* Return map_ctrl pointer as a DBM *. To the outside world it is */ 434*7c478bd9Sstevel@tonic-gate /* opaque. */ 435*7c478bd9Sstevel@tonic-gate return ((DBM *)map); 436*7c478bd9Sstevel@tonic-gate } 437*7c478bd9Sstevel@tonic-gate 438*7c478bd9Sstevel@tonic-gate /* 439*7c478bd9Sstevel@tonic-gate * FUNCTION: shim_dbm_store() 440*7c478bd9Sstevel@tonic-gate * 441*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Shim for dbm_store. 442*7c478bd9Sstevel@tonic-gate * 443*7c478bd9Sstevel@tonic-gate * In N2L mode if we are asked to store in DBM_INSERT mode 444*7c478bd9Sstevel@tonic-gate * then first an attempt is made to write to the DIT (in the same 445*7c478bd9Sstevel@tonic-gate * mode). If this is successful then the value is forced into DBM 446*7c478bd9Sstevel@tonic-gate * using DBM_REPLACE. This is because the DIT is authoritative. 447*7c478bd9Sstevel@tonic-gate * The success of failure of an 'insert' is determined by the 448*7c478bd9Sstevel@tonic-gate * presence or otherwise of an entry in the DIT not DBM. 449*7c478bd9Sstevel@tonic-gate * 450*7c478bd9Sstevel@tonic-gate * INPUTS: Identical to equivalent dbm call. 451*7c478bd9Sstevel@tonic-gate * 452*7c478bd9Sstevel@tonic-gate * OUTPUTS: Identical to equivalent dbm call. 453*7c478bd9Sstevel@tonic-gate * 454*7c478bd9Sstevel@tonic-gate */ 455*7c478bd9Sstevel@tonic-gate int 456*7c478bd9Sstevel@tonic-gate shim_dbm_store(DBM *db, datum key, datum content, int store_mode) 457*7c478bd9Sstevel@tonic-gate { 458*7c478bd9Sstevel@tonic-gate int ret; 459*7c478bd9Sstevel@tonic-gate map_ctrl *map; 460*7c478bd9Sstevel@tonic-gate 461*7c478bd9Sstevel@tonic-gate /* Get map name */ 462*7c478bd9Sstevel@tonic-gate map = get_map_ctrl(db); 463*7c478bd9Sstevel@tonic-gate if (map == NULL) 464*7c478bd9Sstevel@tonic-gate return (FAILURE); 465*7c478bd9Sstevel@tonic-gate 466*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 467*7c478bd9Sstevel@tonic-gate /* Write to the DIT before doing anything else */ 468*7c478bd9Sstevel@tonic-gate if (!write_to_dit(map->map_name, map->domain, key, content, 469*7c478bd9Sstevel@tonic-gate DBM_REPLACE == store_mode, FALSE)) 470*7c478bd9Sstevel@tonic-gate return (FAILURE); 471*7c478bd9Sstevel@tonic-gate } 472*7c478bd9Sstevel@tonic-gate 473*7c478bd9Sstevel@tonic-gate /* Lock the map */ 474*7c478bd9Sstevel@tonic-gate if (1 != lock_map_ctrl(map)) 475*7c478bd9Sstevel@tonic-gate return (FAILURE); 476*7c478bd9Sstevel@tonic-gate 477*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 478*7c478bd9Sstevel@tonic-gate if (!is_map_updating(map)) { 479*7c478bd9Sstevel@tonic-gate ret = dbm_store(map->entries, key, content, 480*7c478bd9Sstevel@tonic-gate DBM_REPLACE); 481*7c478bd9Sstevel@tonic-gate 482*7c478bd9Sstevel@tonic-gate if (SUCCESS == ret) 483*7c478bd9Sstevel@tonic-gate /* Update TTL */ 484*7c478bd9Sstevel@tonic-gate update_entry_ttl(map, &key, TTL_RAND); 485*7c478bd9Sstevel@tonic-gate } 486*7c478bd9Sstevel@tonic-gate } else { 487*7c478bd9Sstevel@tonic-gate ret = dbm_store(map->entries, key, content, store_mode); 488*7c478bd9Sstevel@tonic-gate } 489*7c478bd9Sstevel@tonic-gate 490*7c478bd9Sstevel@tonic-gate unlock_map_ctrl(map); 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate return (ret); 493*7c478bd9Sstevel@tonic-gate } 494*7c478bd9Sstevel@tonic-gate 495*7c478bd9Sstevel@tonic-gate /* 496*7c478bd9Sstevel@tonic-gate * FUNCTION : shim_exit() 497*7c478bd9Sstevel@tonic-gate * 498*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Intercepts exit() calls made by N2L compatible NIS components. 499*7c478bd9Sstevel@tonic-gate * This is required because any call to the shim_dbm... series 500*7c478bd9Sstevel@tonic-gate * of functions may have started an update thread. If the process 501*7c478bd9Sstevel@tonic-gate * exits normally then this thread may be killed before it can 502*7c478bd9Sstevel@tonic-gate * complete its work. We thus wait here for the thread to complete. 503*7c478bd9Sstevel@tonic-gate * 504*7c478bd9Sstevel@tonic-gate * GIVEN : Same arg as exit() 505*7c478bd9Sstevel@tonic-gate * 506*7c478bd9Sstevel@tonic-gate * RETURNS : Never 507*7c478bd9Sstevel@tonic-gate */ 508*7c478bd9Sstevel@tonic-gate void 509*7c478bd9Sstevel@tonic-gate shim_exit(int code) 510*7c478bd9Sstevel@tonic-gate { 511*7c478bd9Sstevel@tonic-gate thr_join(NULL, NULL, NULL); 512*7c478bd9Sstevel@tonic-gate exit(code); 513*7c478bd9Sstevel@tonic-gate } 514*7c478bd9Sstevel@tonic-gate 515*7c478bd9Sstevel@tonic-gate /* 516*7c478bd9Sstevel@tonic-gate * FUNCTION : init_yptol_flag() 517*7c478bd9Sstevel@tonic-gate * 518*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Initializes two flags these are similar but their function is 519*7c478bd9Sstevel@tonic-gate * subtly different. 520*7c478bd9Sstevel@tonic-gate * 521*7c478bd9Sstevel@tonic-gate * yp2ldap tells the mapping system if it is to work in NIS or 522*7c478bd9Sstevel@tonic-gate * NIS+ mode. For N2L this is always set to NIS mode. 523*7c478bd9Sstevel@tonic-gate * 524*7c478bd9Sstevel@tonic-gate * yptol tells the shim if it is to work in N2L or traditional 525*7c478bd9Sstevel@tonic-gate * NIS mode. For N2L this is turned on if the N2L mapping file 526*7c478bd9Sstevel@tonic-gate * is found to be present. In NIS+ mode it is meaningless. 527*7c478bd9Sstevel@tonic-gate */ 528*7c478bd9Sstevel@tonic-gate void 529*7c478bd9Sstevel@tonic-gate init_yptol_flag() 530*7c478bd9Sstevel@tonic-gate { 531*7c478bd9Sstevel@tonic-gate /* 532*7c478bd9Sstevel@tonic-gate * yp2ldap is used to switch appropriate code in the 533*7c478bd9Sstevel@tonic-gate * common libnisdb library used by rpc.nisd and ypserv. 534*7c478bd9Sstevel@tonic-gate */ 535*7c478bd9Sstevel@tonic-gate yp2ldap = 1; 536*7c478bd9Sstevel@tonic-gate yptol_mode = is_yptol_mode(); 537*7c478bd9Sstevel@tonic-gate } 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate /* 540*7c478bd9Sstevel@tonic-gate * FUNCTION : set_yxfrd_flag() 541*7c478bd9Sstevel@tonic-gate */ 542*7c478bd9Sstevel@tonic-gate void 543*7c478bd9Sstevel@tonic-gate set_ypxfrd_flag() 544*7c478bd9Sstevel@tonic-gate { 545*7c478bd9Sstevel@tonic-gate ypxfrd_flag = TRUE; 546*7c478bd9Sstevel@tonic-gate } 547*7c478bd9Sstevel@tonic-gate 548*7c478bd9Sstevel@tonic-gate /* 549*7c478bd9Sstevel@tonic-gate * FUNCTION : check_old_map_date() 550*7c478bd9Sstevel@tonic-gate * 551*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Checks that an old style map has not been updated. If it has 552*7c478bd9Sstevel@tonic-gate * then ypmake has probably erroneously been run and an error is 553*7c478bd9Sstevel@tonic-gate * logged. 554*7c478bd9Sstevel@tonic-gate * 555*7c478bd9Sstevel@tonic-gate * GIVEN : A map_ctrl containing details of the NEW STYLE map. 556*7c478bd9Sstevel@tonic-gate * 557*7c478bd9Sstevel@tonic-gate * RETURNS : Nothing 558*7c478bd9Sstevel@tonic-gate */ 559*7c478bd9Sstevel@tonic-gate void 560*7c478bd9Sstevel@tonic-gate check_old_map_date(map_ctrl *map) 561*7c478bd9Sstevel@tonic-gate { 562*7c478bd9Sstevel@tonic-gate datum key; 563*7c478bd9Sstevel@tonic-gate datum value; 564*7c478bd9Sstevel@tonic-gate struct stat stats; 565*7c478bd9Sstevel@tonic-gate time_t old_time; 566*7c478bd9Sstevel@tonic-gate 567*7c478bd9Sstevel@tonic-gate /* Get date of last update */ 568*7c478bd9Sstevel@tonic-gate if (0 != stat(map->trad_map_path, &stats)) { 569*7c478bd9Sstevel@tonic-gate /* 570*7c478bd9Sstevel@tonic-gate * No problem. We have a new style map but no old style map 571*7c478bd9Sstevel@tonic-gate * this will occur if the original data came from native LDAP 572*7c478bd9Sstevel@tonic-gate * instead of NIS. 573*7c478bd9Sstevel@tonic-gate */ 574*7c478bd9Sstevel@tonic-gate return; 575*7c478bd9Sstevel@tonic-gate } 576*7c478bd9Sstevel@tonic-gate 577*7c478bd9Sstevel@tonic-gate /* Set up datum with key for recorded old map update time */ 578*7c478bd9Sstevel@tonic-gate key.dsize = strlen(MAP_OLD_MAP_DATE_KEY); 579*7c478bd9Sstevel@tonic-gate key.dptr = MAP_OLD_MAP_DATE_KEY; 580*7c478bd9Sstevel@tonic-gate value = dbm_fetch(map->ttl, key); 581*7c478bd9Sstevel@tonic-gate 582*7c478bd9Sstevel@tonic-gate if (NULL != value.dptr) { 583*7c478bd9Sstevel@tonic-gate /* 584*7c478bd9Sstevel@tonic-gate * Because dptr may not be int aligned need to build an int 585*7c478bd9Sstevel@tonic-gate * out of what it points to or will get a bus error. 586*7c478bd9Sstevel@tonic-gate */ 587*7c478bd9Sstevel@tonic-gate bcopy(value.dptr, &old_time, sizeof (time_t)); 588*7c478bd9Sstevel@tonic-gate 589*7c478bd9Sstevel@tonic-gate 590*7c478bd9Sstevel@tonic-gate /* Do the comparison */ 591*7c478bd9Sstevel@tonic-gate if (stats.st_mtime <= old_time) { 592*7c478bd9Sstevel@tonic-gate /* All is well, has not been updated */ 593*7c478bd9Sstevel@tonic-gate return; 594*7c478bd9Sstevel@tonic-gate } 595*7c478bd9Sstevel@tonic-gate 596*7c478bd9Sstevel@tonic-gate /* If we get here the file has been updated */ 597*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, 598*7c478bd9Sstevel@tonic-gate "Caution. ypmake may have been run in N2L " 599*7c478bd9Sstevel@tonic-gate "mode. This will NOT initiate a NIS map push. In " 600*7c478bd9Sstevel@tonic-gate "this mode pushes should be initiated with yppush"); 601*7c478bd9Sstevel@tonic-gate } 602*7c478bd9Sstevel@tonic-gate 603*7c478bd9Sstevel@tonic-gate /* 604*7c478bd9Sstevel@tonic-gate * If we get here then either the file was updated or there was not 605*7c478bd9Sstevel@tonic-gate * a valid old map date (no problem, maybe this is the first time we 606*7c478bd9Sstevel@tonic-gate * checked). In either case the old map date entry must be update. 607*7c478bd9Sstevel@tonic-gate */ 608*7c478bd9Sstevel@tonic-gate value.dptr = (char *)&(stats.st_mtime); 609*7c478bd9Sstevel@tonic-gate value.dsize = sizeof (time_t); 610*7c478bd9Sstevel@tonic-gate dbm_store(map->ttl, key, value, DBM_REPLACE); 611*7c478bd9Sstevel@tonic-gate } 612*7c478bd9Sstevel@tonic-gate 613*7c478bd9Sstevel@tonic-gate /* 614*7c478bd9Sstevel@tonic-gate * FUNCTION : init_lock_system() 615*7c478bd9Sstevel@tonic-gate * 616*7c478bd9Sstevel@tonic-gate * DESCRIPTION: Initializes all the systems related to map locking. This must 617*7c478bd9Sstevel@tonic-gate * be called before any access to the shim functions. 618*7c478bd9Sstevel@tonic-gate * 619*7c478bd9Sstevel@tonic-gate * GIVEN : A flag indicating if we are being called from ypserv, which does 620*7c478bd9Sstevel@tonic-gate * not wait for map updates to complete, or other NIS components 621*7c478bd9Sstevel@tonic-gate * which do. 622*7c478bd9Sstevel@tonic-gate * 623*7c478bd9Sstevel@tonic-gate * RETURNS : TRUE = Everything worked 624*7c478bd9Sstevel@tonic-gate * FALSE = There were problems 625*7c478bd9Sstevel@tonic-gate */ 626*7c478bd9Sstevel@tonic-gate bool_t 627*7c478bd9Sstevel@tonic-gate init_lock_system(bool_t ypxfrd) 628*7c478bd9Sstevel@tonic-gate { 629*7c478bd9Sstevel@tonic-gate /* Remember what called us */ 630*7c478bd9Sstevel@tonic-gate if (ypxfrd) 631*7c478bd9Sstevel@tonic-gate set_ypxfrd_flag(); 632*7c478bd9Sstevel@tonic-gate 633*7c478bd9Sstevel@tonic-gate /* 634*7c478bd9Sstevel@tonic-gate * Remember PID of process which called us. This enables update threads 635*7c478bd9Sstevel@tonic-gate * created by YP children to be handled differently to those created 636*7c478bd9Sstevel@tonic-gate * by YP parents. 637*7c478bd9Sstevel@tonic-gate */ 638*7c478bd9Sstevel@tonic-gate parent_pid = getpid(); 639*7c478bd9Sstevel@tonic-gate 640*7c478bd9Sstevel@tonic-gate /* Init map locks */ 641*7c478bd9Sstevel@tonic-gate if (!init_lock_map()) { 642*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, 643*7c478bd9Sstevel@tonic-gate "Failed to init process synchronization"); 644*7c478bd9Sstevel@tonic-gate return (FALSE); 645*7c478bd9Sstevel@tonic-gate } 646*7c478bd9Sstevel@tonic-gate 647*7c478bd9Sstevel@tonic-gate /* If we are in yptol mode set flag indicating the fact */ 648*7c478bd9Sstevel@tonic-gate init_yptol_flag(); 649*7c478bd9Sstevel@tonic-gate 650*7c478bd9Sstevel@tonic-gate /* 651*7c478bd9Sstevel@tonic-gate * If boot random number system. For now go for reproducible 652*7c478bd9Sstevel@tonic-gate * random numbers. 653*7c478bd9Sstevel@tonic-gate */ 654*7c478bd9Sstevel@tonic-gate srand48(0x12345678); 655*7c478bd9Sstevel@tonic-gate 656*7c478bd9Sstevel@tonic-gate /* 657*7c478bd9Sstevel@tonic-gate * If not N2L mode then no error but do not bother initializing update 658*7c478bd9Sstevel@tonic-gate * flags. 659*7c478bd9Sstevel@tonic-gate */ 660*7c478bd9Sstevel@tonic-gate if (yptol_mode) { 661*7c478bd9Sstevel@tonic-gate if (!init_update_lock_map()) { 662*7c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR, 663*7c478bd9Sstevel@tonic-gate "Failed to init update synchronization"); 664*7c478bd9Sstevel@tonic-gate return (FALSE); 665*7c478bd9Sstevel@tonic-gate } 666*7c478bd9Sstevel@tonic-gate } 667*7c478bd9Sstevel@tonic-gate 668*7c478bd9Sstevel@tonic-gate return (TRUE); 669*7c478bd9Sstevel@tonic-gate } 670