xref: /onnv-gate/usr/src/cmd/ypcmd/shared/utils.c (revision 7035:f93a1cefda5f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52189Ssdussud  * Common Development and Distribution License (the "License").
62189Ssdussud  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7035Svt115884  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	  All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * DESCRIPTION:	This file contains various functions used by more than one NIS
390Sstevel@tonic-gate  *		components. A lot of this code started off in ypxfr and then
400Sstevel@tonic-gate  *		got used by other components. Some of it has become a little
410Sstevel@tonic-gate  *		'quirky' and should probably be re-worked.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <syslog.h>
460Sstevel@tonic-gate #include <sys/mman.h>
470Sstevel@tonic-gate #include <thread.h>
480Sstevel@tonic-gate #include <synch.h>
490Sstevel@tonic-gate #include <stdarg.h>
500Sstevel@tonic-gate #include <ndbm.h>
510Sstevel@tonic-gate #include "../ypsym.h"
520Sstevel@tonic-gate #include "../ypdefs.h"
530Sstevel@tonic-gate #include "shim.h"
540Sstevel@tonic-gate 
550Sstevel@tonic-gate USE_DBM
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Globals
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * DESCRIPTION : Utility functions used by everything.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate bool check_map_existence(char *);
650Sstevel@tonic-gate void logprintf2(char *format, ...);
662189Ssdussud extern bool ypcheck_map_existence_yptol();
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * This checks to see if the source map files exist, then renames them to the
700Sstevel@tonic-gate  * target names.  This is a boolean function.  The file names from.pag and
710Sstevel@tonic-gate  * from.dir will be changed to to.pag and to.dir in the success case.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * Note:  If the second of the two renames fails, yprename_map will try to
740Sstevel@tonic-gate  * un-rename the first pair, and leave the world in the state it was on entry.
750Sstevel@tonic-gate  * This might fail, too, though...
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * GIVEN :	Name of map to copy from
780Sstevel@tonic-gate  *		Name of map to copy to
790Sstevel@tonic-gate  *		Flag indicating if map is secure.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate bool
rename_map(from,to,secure_map)820Sstevel@tonic-gate rename_map(from, to, secure_map)
830Sstevel@tonic-gate 	char *from;
840Sstevel@tonic-gate 	char *to;
850Sstevel@tonic-gate 	bool_t secure_map;
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	char fromfile[MAXNAMLEN + 1];
880Sstevel@tonic-gate 	char tofile[MAXNAMLEN + 1];
890Sstevel@tonic-gate 	char savefile[MAXNAMLEN + 1];
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (!from || !to) {
920Sstevel@tonic-gate 		return (FALSE);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (!check_map_existence(from)) {
960Sstevel@tonic-gate 		return (FALSE);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	(void) strcpy(fromfile, from);
1000Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_pag);
1010Sstevel@tonic-gate 	(void) strcpy(tofile, to);
1020Sstevel@tonic-gate 	(void) strcat(tofile, dbm_pag);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (rename(fromfile, tofile)) {
1050Sstevel@tonic-gate 		logprintf2("Can't mv %s to %s.\n", fromfile,
1060Sstevel@tonic-gate 		    tofile);
1070Sstevel@tonic-gate 		return (FALSE);
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	(void) strcpy(savefile, tofile);
1110Sstevel@tonic-gate 	(void) strcpy(fromfile, from);
1120Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_dir);
1130Sstevel@tonic-gate 	(void) strcpy(tofile, to);
1140Sstevel@tonic-gate 	(void) strcat(tofile, dbm_dir);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (rename(fromfile, tofile)) {
1170Sstevel@tonic-gate 		logprintf2("Can't mv %s to %s.\n", fromfile,
1180Sstevel@tonic-gate 		    tofile);
1190Sstevel@tonic-gate 		(void) strcpy(fromfile, from);
1200Sstevel@tonic-gate 		(void) strcat(fromfile, dbm_pag);
1210Sstevel@tonic-gate 		(void) strcpy(tofile, to);
1220Sstevel@tonic-gate 		(void) strcat(tofile, dbm_pag);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		if (rename(tofile, fromfile)) {
1250Sstevel@tonic-gate 			logprintf2(
1260Sstevel@tonic-gate 			    "Can't recover from rename failure.\n");
1270Sstevel@tonic-gate 			return (FALSE);
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		return (FALSE);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (!secure_map) {
1340Sstevel@tonic-gate 		chmod(savefile, 0644);
1350Sstevel@tonic-gate 		chmod(tofile, 0644);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	return (TRUE);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Function :	delete_map()
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * Description:	Deletes a map
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  * Given :	Map name
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * Return :	TRUE = Map deleted
1490Sstevel@tonic-gate  *		FALSE = Map not completly deleted
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate bool
delete_map(name)1520Sstevel@tonic-gate delete_map(name)
1530Sstevel@tonic-gate 	char *name;
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	char fromfile[MAXNAMLEN + 1];
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (!name) {
1580Sstevel@tonic-gate 		return (FALSE);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (!check_map_existence(name)) {
1620Sstevel@tonic-gate 		/* Already gone */
1630Sstevel@tonic-gate 		return (TRUE);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	(void) strcpy(fromfile, name);
1670Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_pag);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (unlink(fromfile)) {
1700Sstevel@tonic-gate 		logprintf2("Can't unlink %s.\n", fromfile);
1710Sstevel@tonic-gate 		return (FALSE);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	(void) strcpy(fromfile, name);
1750Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_dir);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (unlink(fromfile)) {
1780Sstevel@tonic-gate 		logprintf2("Can't unlink %s.\n", fromfile);
1790Sstevel@tonic-gate 		return (FALSE);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (TRUE);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * This performs an existence check on the dbm data base files <pname>.pag and
1870Sstevel@tonic-gate  * <pname>.dir.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate bool
check_map_existence(pname)1900Sstevel@tonic-gate check_map_existence(pname)
1910Sstevel@tonic-gate 	char *pname;
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	char dbfile[MAXNAMLEN + 1];
194*7035Svt115884 	struct stat64 filestat;
1950Sstevel@tonic-gate 	int len;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (!pname || ((len = strlen(pname)) == 0) ||
1980Sstevel@tonic-gate 	    (len + 5) > (MAXNAMLEN + 1)) {
1990Sstevel@tonic-gate 		return (FALSE);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	errno = 0;
2030Sstevel@tonic-gate 	(void) strcpy(dbfile, pname);
2040Sstevel@tonic-gate 	(void) strcat(dbfile, dbm_dir);
2050Sstevel@tonic-gate 
206*7035Svt115884 	if (stat64(dbfile, &filestat) != -1) {
2070Sstevel@tonic-gate 		(void) strcpy(dbfile, pname);
2080Sstevel@tonic-gate 		(void) strcat(dbfile, dbm_pag);
2090Sstevel@tonic-gate 
210*7035Svt115884 		if (stat64(dbfile, &filestat) != -1) {
2110Sstevel@tonic-gate 			return (TRUE);
2120Sstevel@tonic-gate 		} else {
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 			if (errno != ENOENT) {
2150Sstevel@tonic-gate 				logprintf2(
2160Sstevel@tonic-gate 				    "Stat error on map file %s.\n",
2170Sstevel@tonic-gate 				    dbfile);
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 			return (FALSE);
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	} else {
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		if (errno != ENOENT) {
2260Sstevel@tonic-gate 			logprintf2(
2270Sstevel@tonic-gate 			    "Stat error on map file %s.\n",
2280Sstevel@tonic-gate 			    dbfile);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		return (FALSE);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  * FUNCTION :	logprintf2()
2370Sstevel@tonic-gate  *
2380Sstevel@tonic-gate  * DESCRIPTION:	The functions in this file were oringinaly shared between
2390Sstevel@tonic-gate  *		ypxfr and ypserv. On error they called logprintf().
2400Sstevel@tonic-gate  *		Unfortunatly this had been implemented differently in the two
2410Sstevel@tonic-gate  *		sources and not at all in some of the NIS components required
2420Sstevel@tonic-gate  *		for N2L.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  *		This function is simplified version of logprinf() as/when
2450Sstevel@tonic-gate  *		possible the other error calls should be migrated to use this
2460Sstevel@tonic-gate  *		common version. If a common set of functionality can be found
2470Sstevel@tonic-gate  *		this versions should be modified to support it.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate void
logprintf2(char * format,...)2500Sstevel@tonic-gate logprintf2(char *format, ...)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	va_list ap;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	va_start(ap, format);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	syslog(LOG_ERR, format, ap);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	va_end(ap);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * This performs an existence check on the dbm data base files <name>.pag and
2630Sstevel@tonic-gate  * <name>.dir.  pname is a ptr to the filename.  This should be an absolute
2640Sstevel@tonic-gate  * path.
2650Sstevel@tonic-gate  * Returns TRUE if the map exists and is accessable; else FALSE.
2660Sstevel@tonic-gate  *
2670Sstevel@tonic-gate  * Note:  The file name should be a "base" form, without a file "extension" of
2680Sstevel@tonic-gate  * .dir or .pag appended.  See ypmkfilename for a function which will generate
2690Sstevel@tonic-gate  * the name correctly.  Errors in the stat call will be reported at this level,
2700Sstevel@tonic-gate  * however, the non-existence of a file is not considered an error, and so will
2710Sstevel@tonic-gate  * not be reported.
2722189Ssdussud  *
2732189Ssdussud  * Calls ypcheck_map_existence_yptol() defined in
2742189Ssdussud  * usr/src/lib/libnisdb/yptol/shim_ancil.c
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate bool
ypcheck_map_existence(char * pname)2770Sstevel@tonic-gate ypcheck_map_existence(char *pname)
2780Sstevel@tonic-gate {
2792189Ssdussud 	return (ypcheck_map_existence_yptol(pname));
2800Sstevel@tonic-gate }
281