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