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 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of 327c478bd9Sstevel@tonic-gate * California. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * DESCRIPTION: This file contains various functions used by more than one NIS 397c478bd9Sstevel@tonic-gate * components. A lot of this code started off in ypxfr and then 407c478bd9Sstevel@tonic-gate * got used by other components. Some of it has become a little 417c478bd9Sstevel@tonic-gate * 'quirky' and should probably be re-worked. 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <unistd.h> 457c478bd9Sstevel@tonic-gate #include <syslog.h> 467c478bd9Sstevel@tonic-gate #include <sys/mman.h> 477c478bd9Sstevel@tonic-gate #include <thread.h> 487c478bd9Sstevel@tonic-gate #include <synch.h> 497c478bd9Sstevel@tonic-gate #include <stdarg.h> 507c478bd9Sstevel@tonic-gate #include <ndbm.h> 517c478bd9Sstevel@tonic-gate #include "../ypsym.h" 527c478bd9Sstevel@tonic-gate #include "../ypdefs.h" 537c478bd9Sstevel@tonic-gate #include "shim.h" 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate USE_DBM 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * Globals 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * DESCRIPTION : Utility functions used by everything. 637c478bd9Sstevel@tonic-gate */ 647c478bd9Sstevel@tonic-gate bool check_map_existence(char *); 657c478bd9Sstevel@tonic-gate void logprintf2(char *format, ...); 66*4a190493Ssdussud extern bool ypcheck_map_existence_yptol(); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * This checks to see if the source map files exist, then renames them to the 707c478bd9Sstevel@tonic-gate * target names. This is a boolean function. The file names from.pag and 717c478bd9Sstevel@tonic-gate * from.dir will be changed to to.pag and to.dir in the success case. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * Note: If the second of the two renames fails, yprename_map will try to 747c478bd9Sstevel@tonic-gate * un-rename the first pair, and leave the world in the state it was on entry. 757c478bd9Sstevel@tonic-gate * This might fail, too, though... 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * GIVEN : Name of map to copy from 787c478bd9Sstevel@tonic-gate * Name of map to copy to 797c478bd9Sstevel@tonic-gate * Flag indicating if map is secure. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate bool 827c478bd9Sstevel@tonic-gate rename_map(from, to, secure_map) 837c478bd9Sstevel@tonic-gate char *from; 847c478bd9Sstevel@tonic-gate char *to; 857c478bd9Sstevel@tonic-gate bool_t secure_map; 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate char fromfile[MAXNAMLEN + 1]; 887c478bd9Sstevel@tonic-gate char tofile[MAXNAMLEN + 1]; 897c478bd9Sstevel@tonic-gate char savefile[MAXNAMLEN + 1]; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (!from || !to) { 927c478bd9Sstevel@tonic-gate return (FALSE); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (!check_map_existence(from)) { 967c478bd9Sstevel@tonic-gate return (FALSE); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate (void) strcpy(fromfile, from); 1007c478bd9Sstevel@tonic-gate (void) strcat(fromfile, dbm_pag); 1017c478bd9Sstevel@tonic-gate (void) strcpy(tofile, to); 1027c478bd9Sstevel@tonic-gate (void) strcat(tofile, dbm_pag); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate if (rename(fromfile, tofile)) { 1057c478bd9Sstevel@tonic-gate logprintf2("Can't mv %s to %s.\n", fromfile, 1067c478bd9Sstevel@tonic-gate tofile); 1077c478bd9Sstevel@tonic-gate return (FALSE); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate (void) strcpy(savefile, tofile); 1117c478bd9Sstevel@tonic-gate (void) strcpy(fromfile, from); 1127c478bd9Sstevel@tonic-gate (void) strcat(fromfile, dbm_dir); 1137c478bd9Sstevel@tonic-gate (void) strcpy(tofile, to); 1147c478bd9Sstevel@tonic-gate (void) strcat(tofile, dbm_dir); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate if (rename(fromfile, tofile)) { 1177c478bd9Sstevel@tonic-gate logprintf2("Can't mv %s to %s.\n", fromfile, 1187c478bd9Sstevel@tonic-gate tofile); 1197c478bd9Sstevel@tonic-gate (void) strcpy(fromfile, from); 1207c478bd9Sstevel@tonic-gate (void) strcat(fromfile, dbm_pag); 1217c478bd9Sstevel@tonic-gate (void) strcpy(tofile, to); 1227c478bd9Sstevel@tonic-gate (void) strcat(tofile, dbm_pag); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if (rename(tofile, fromfile)) { 1257c478bd9Sstevel@tonic-gate logprintf2( 1267c478bd9Sstevel@tonic-gate "Can't recover from rename failure.\n"); 1277c478bd9Sstevel@tonic-gate return (FALSE); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate return (FALSE); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (!secure_map) { 1347c478bd9Sstevel@tonic-gate chmod(savefile, 0644); 1357c478bd9Sstevel@tonic-gate chmod(tofile, 0644); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate return (TRUE); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Function : delete_map() 1437c478bd9Sstevel@tonic-gate * 1447c478bd9Sstevel@tonic-gate * Description: Deletes a map 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * Given : Map name 1477c478bd9Sstevel@tonic-gate * 1487c478bd9Sstevel@tonic-gate * Return : TRUE = Map deleted 1497c478bd9Sstevel@tonic-gate * FALSE = Map not completly deleted 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate bool 1527c478bd9Sstevel@tonic-gate delete_map(name) 1537c478bd9Sstevel@tonic-gate char *name; 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate char fromfile[MAXNAMLEN + 1]; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (!name) { 1587c478bd9Sstevel@tonic-gate return (FALSE); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate if (!check_map_existence(name)) { 1627c478bd9Sstevel@tonic-gate /* Already gone */ 1637c478bd9Sstevel@tonic-gate return (TRUE); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate (void) strcpy(fromfile, name); 1677c478bd9Sstevel@tonic-gate (void) strcat(fromfile, dbm_pag); 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate if (unlink(fromfile)) { 1707c478bd9Sstevel@tonic-gate logprintf2("Can't unlink %s.\n", fromfile); 1717c478bd9Sstevel@tonic-gate return (FALSE); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate (void) strcpy(fromfile, name); 1757c478bd9Sstevel@tonic-gate (void) strcat(fromfile, dbm_dir); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate if (unlink(fromfile)) { 1787c478bd9Sstevel@tonic-gate logprintf2("Can't unlink %s.\n", fromfile); 1797c478bd9Sstevel@tonic-gate return (FALSE); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate return (TRUE); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate * This performs an existence check on the dbm data base files <pname>.pag and 1877c478bd9Sstevel@tonic-gate * <pname>.dir. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate bool 1907c478bd9Sstevel@tonic-gate check_map_existence(pname) 1917c478bd9Sstevel@tonic-gate char *pname; 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate char dbfile[MAXNAMLEN + 1]; 1947c478bd9Sstevel@tonic-gate struct stat filestat; 1957c478bd9Sstevel@tonic-gate int len; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (!pname || ((len = strlen(pname)) == 0) || 1987c478bd9Sstevel@tonic-gate (len + 5) > (MAXNAMLEN + 1)) { 1997c478bd9Sstevel@tonic-gate return (FALSE); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate errno = 0; 2037c478bd9Sstevel@tonic-gate (void) strcpy(dbfile, pname); 2047c478bd9Sstevel@tonic-gate (void) strcat(dbfile, dbm_dir); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if (stat(dbfile, &filestat) != -1) { 2077c478bd9Sstevel@tonic-gate (void) strcpy(dbfile, pname); 2087c478bd9Sstevel@tonic-gate (void) strcat(dbfile, dbm_pag); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (stat(dbfile, &filestat) != -1) { 2117c478bd9Sstevel@tonic-gate return (TRUE); 2127c478bd9Sstevel@tonic-gate } else { 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 2157c478bd9Sstevel@tonic-gate logprintf2( 2167c478bd9Sstevel@tonic-gate "Stat error on map file %s.\n", 2177c478bd9Sstevel@tonic-gate dbfile); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate return (FALSE); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate } else { 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 2267c478bd9Sstevel@tonic-gate logprintf2( 2277c478bd9Sstevel@tonic-gate "Stat error on map file %s.\n", 2287c478bd9Sstevel@tonic-gate dbfile); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate return (FALSE); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate /* 2367c478bd9Sstevel@tonic-gate * FUNCTION : logprintf2() 2377c478bd9Sstevel@tonic-gate * 2387c478bd9Sstevel@tonic-gate * DESCRIPTION: The functions in this file were oringinaly shared between 2397c478bd9Sstevel@tonic-gate * ypxfr and ypserv. On error they called logprintf(). 2407c478bd9Sstevel@tonic-gate * Unfortunatly this had been implemented differently in the two 2417c478bd9Sstevel@tonic-gate * sources and not at all in some of the NIS components required 2427c478bd9Sstevel@tonic-gate * for N2L. 2437c478bd9Sstevel@tonic-gate * 2447c478bd9Sstevel@tonic-gate * This function is simplified version of logprinf() as/when 2457c478bd9Sstevel@tonic-gate * possible the other error calls should be migrated to use this 2467c478bd9Sstevel@tonic-gate * common version. If a common set of functionality can be found 2477c478bd9Sstevel@tonic-gate * this versions should be modified to support it. 2487c478bd9Sstevel@tonic-gate */ 2497c478bd9Sstevel@tonic-gate void 2507c478bd9Sstevel@tonic-gate logprintf2(char *format, ...) 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate va_list ap; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate va_start(ap, format); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate syslog(LOG_ERR, format, ap); 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate va_end(ap); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * This performs an existence check on the dbm data base files <name>.pag and 2637c478bd9Sstevel@tonic-gate * <name>.dir. pname is a ptr to the filename. This should be an absolute 2647c478bd9Sstevel@tonic-gate * path. 2657c478bd9Sstevel@tonic-gate * Returns TRUE if the map exists and is accessable; else FALSE. 2667c478bd9Sstevel@tonic-gate * 2677c478bd9Sstevel@tonic-gate * Note: The file name should be a "base" form, without a file "extension" of 2687c478bd9Sstevel@tonic-gate * .dir or .pag appended. See ypmkfilename for a function which will generate 2697c478bd9Sstevel@tonic-gate * the name correctly. Errors in the stat call will be reported at this level, 2707c478bd9Sstevel@tonic-gate * however, the non-existence of a file is not considered an error, and so will 2717c478bd9Sstevel@tonic-gate * not be reported. 272*4a190493Ssdussud * 273*4a190493Ssdussud * Calls ypcheck_map_existence_yptol() defined in 274*4a190493Ssdussud * usr/src/lib/libnisdb/yptol/shim_ancil.c 2757c478bd9Sstevel@tonic-gate */ 2767c478bd9Sstevel@tonic-gate bool 2777c478bd9Sstevel@tonic-gate ypcheck_map_existence(char *pname) 2787c478bd9Sstevel@tonic-gate { 279*4a190493Ssdussud return (ypcheck_map_existence_yptol(pname)); 2807c478bd9Sstevel@tonic-gate } 281