xref: /illumos-gate/usr/src/cmd/ypcmd/ypxfr.c (revision 48bbca81)
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  *
22a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
337c478bd9Sstevel@tonic-gate  * California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * This is a user command which gets a NIS data base from some running
387c478bd9Sstevel@tonic-gate  * server, and gets it to the local site by using the normal NIS client
397c478bd9Sstevel@tonic-gate  * enumeration functions.  The map is copied to a temp name, then the real
407c478bd9Sstevel@tonic-gate  * map is removed and the temp map is moved to the real name.  ypxfr then
41*48bbca81SDaniel Hoffman  * sends a "YPPROC_CLEAR" message to the local server to insure that it will
427c478bd9Sstevel@tonic-gate  * not hold a removed map open, so serving an obsolete version.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * ypxfr [ -h <host> ] [ -d <domainname> ]
457c478bd9Sstevel@tonic-gate  *		[ -s <domainname> ] [-f] [-c] [-C tid prot name] map
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * If the host is ommitted, ypxfr will attempt to discover the master by
487c478bd9Sstevel@tonic-gate  * using normal NIS services.  If it can't get the record, it will use
497c478bd9Sstevel@tonic-gate  * the address of the callback, if specified. If the host is specified
507c478bd9Sstevel@tonic-gate  * as an internet address, no NIS services need to be locally available.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * If the domain is not specified, the default domain of the local machine
537c478bd9Sstevel@tonic-gate  * is used.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * If the -f flag is used, the transfer will be done even if the master's
567c478bd9Sstevel@tonic-gate  * copy is not newer than the local copy.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * The -c flag suppresses the YPPROC_CLEAR request to the local ypserv.  It
597c478bd9Sstevel@tonic-gate  * may be used if ypserv isn't currently running to suppress the error message.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * The -C flag is used to pass callback information to ypxfr when it is
627c478bd9Sstevel@tonic-gate  * activated by ypserv.  The callback information is used to send a
637c478bd9Sstevel@tonic-gate  * yppushresp_xfr message with transaction id "tid" to a yppush process
647c478bd9Sstevel@tonic-gate  * speaking a transient protocol number "prot".  The yppush program is
657c478bd9Sstevel@tonic-gate  * running on the host "name".
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * The -s option is used to specify a source domain which may be
687c478bd9Sstevel@tonic-gate  * different from the destination domain, for transfer of maps
697c478bd9Sstevel@tonic-gate  * that are identical in different domains (e.g. services.byname)
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <ndbm.h>
747c478bd9Sstevel@tonic-gate #define	DATUM
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <stdio.h>
777c478bd9Sstevel@tonic-gate #include <errno.h>
787c478bd9Sstevel@tonic-gate #include <time.h>
797c478bd9Sstevel@tonic-gate #include <ctype.h>
807c478bd9Sstevel@tonic-gate #include <netdb.h>
817c478bd9Sstevel@tonic-gate #include <netconfig.h>
827c478bd9Sstevel@tonic-gate #include <netdir.h>
837c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
847c478bd9Sstevel@tonic-gate #include <sys/file.h>
857c478bd9Sstevel@tonic-gate #include <sys/stat.h>
867c478bd9Sstevel@tonic-gate #include <dirent.h>
877c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
887c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
897c478bd9Sstevel@tonic-gate #include <unistd.h>
907c478bd9Sstevel@tonic-gate #include <stdlib.h>
917c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
927c478bd9Sstevel@tonic-gate #include "ypdefs.h"
937c478bd9Sstevel@tonic-gate #include "yp_b.h"
947c478bd9Sstevel@tonic-gate #include "shim.h"
957c478bd9Sstevel@tonic-gate #include "yptol.h"
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate USE_YP_MASTER_NAME
987c478bd9Sstevel@tonic-gate USE_YP_SECURE
997c478bd9Sstevel@tonic-gate USE_YP_INTERDOMAIN
1007c478bd9Sstevel@tonic-gate USE_YP_LAST_MODIFIED
1017c478bd9Sstevel@tonic-gate USE_YPDBPATH
1027c478bd9Sstevel@tonic-gate USE_DBM
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	PARANOID 1	/* make sure maps have the right # entries */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	CALLINTER_TRY 10		/* Seconds between callback tries */
1077c478bd9Sstevel@tonic-gate #define	CALLTIMEOUT CALLINTER_TRY*6	/* Total timeout for callback */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate DBM *db;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* ypxfr never uses N2L mode */
1127c478bd9Sstevel@tonic-gate bool_t yptol_mode = FALSE;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int	debug = FALSE;
1157c478bd9Sstevel@tonic-gate int	treepush = FALSE;
1167c478bd9Sstevel@tonic-gate #define	TREEPUSH 1
1177c478bd9Sstevel@tonic-gate int	defwrite = TRUE;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate char *domain = NULL;
1207c478bd9Sstevel@tonic-gate char *source = NULL;
1217c478bd9Sstevel@tonic-gate char *map = NULL;
1227c478bd9Sstevel@tonic-gate char *master = NULL;
1237c478bd9Sstevel@tonic-gate char *pushhost = NULL;
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * The name of the xfer peer as specified as a
1267c478bd9Sstevel@tonic-gate  * -h option, -C name option or from querying the NIS
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate struct dom_binding master_server; /* To talk to above */
1297c478bd9Sstevel@tonic-gate unsigned int master_prog_vers;	/* YPVERS (barfs at YPOLDVERS !) */
1307c478bd9Sstevel@tonic-gate char *master_name = NULL;	/* Map's master as contained in the map */
1317c478bd9Sstevel@tonic-gate unsigned *master_version = NULL; /* Order number as contained in the map */
1327c478bd9Sstevel@tonic-gate char *master_ascii_version;	/* ASCII order number as contained in the map */
1337c478bd9Sstevel@tonic-gate bool fake_master_version = FALSE;
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * TRUE only if there's no order number in
1367c478bd9Sstevel@tonic-gate  *  the map, and the user specified -f
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate bool force = FALSE;		/* TRUE iff user specified -f flag */
1397c478bd9Sstevel@tonic-gate bool logging = FALSE;		/* TRUE iff no tty, but log file exists */
1407c478bd9Sstevel@tonic-gate bool check_count = FALSE;	/* TRUE causes counts to be checked */
1417c478bd9Sstevel@tonic-gate bool send_clear = TRUE;		/* FALSE iff user specified -c flag */
1427c478bd9Sstevel@tonic-gate bool callback = FALSE;
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * TRUE iff -C flag set.  tid, proto and name
1457c478bd9Sstevel@tonic-gate  * will be set to point to the command line args.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate bool secure_map = FALSE;	/* TRUE if there is yp_secure in the map */
1487c478bd9Sstevel@tonic-gate bool interdomain_map = FALSE;
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  * TRUE if there is yp_interdomain in either
1517c478bd9Sstevel@tonic-gate  * the local or the master version of the map
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate int interdomain_sz = 0;		/* Size of the interdomain value */
1547c478bd9Sstevel@tonic-gate #define	UDPINTER_TRY 10		/* Seconds between tries for udp */
1557c478bd9Sstevel@tonic-gate #define	UDPTIMEOUT UDPINTER_TRY*4	/* Total timeout for udp */
1567c478bd9Sstevel@tonic-gate #define	CALLINTER_TRY 10	/* Seconds between callback tries */
1577c478bd9Sstevel@tonic-gate #define	CALLTIMEOUT CALLINTER_TRY*6	/* Total timeout for callback */
1587c478bd9Sstevel@tonic-gate struct timeval udp_timeout = { UDPTIMEOUT, 0};
1597c478bd9Sstevel@tonic-gate struct timeval tcp_timeout = { 180, 0}; /* Timeout for map enumeration */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate char *interdomain_value; 	/* place to store the interdomain value */
1627c478bd9Sstevel@tonic-gate char *tid;
1637c478bd9Sstevel@tonic-gate char *proto;
1647c478bd9Sstevel@tonic-gate int entry_count;		/* counts entries in the map */
1657c478bd9Sstevel@tonic-gate char logfile[] = "/var/yp/ypxfr.log";
1667c478bd9Sstevel@tonic-gate static char err_usage[] =
1677c478bd9Sstevel@tonic-gate "Usage:\n\
1687c478bd9Sstevel@tonic-gate ypxfr [-f] [ -h host ] [ -d domainname ]\n\
1697c478bd9Sstevel@tonic-gate 	[ -s domainname ] [-c] [-C tid prot servname ] map\n\n\
1707c478bd9Sstevel@tonic-gate where\n\
1717c478bd9Sstevel@tonic-gate 	-f forces transfer even if the master's copy is not newer.\n\
1727c478bd9Sstevel@tonic-gate 	host is the server from where the map should be transfered\n\
1737c478bd9Sstevel@tonic-gate 	-d domainname is specified if other than the default domain\n\
1747c478bd9Sstevel@tonic-gate 	-s domainname is a source for the map that is same across domains\n\
1757c478bd9Sstevel@tonic-gate 	-c inhibits sending a \"Clear map\" message to the local ypserv.\n\
1767c478bd9Sstevel@tonic-gate 	-C is for use only by ypserv to pass callback information.\n";
1777c478bd9Sstevel@tonic-gate char err_bad_args[] =
1787c478bd9Sstevel@tonic-gate 	"%s argument is bad.\n";
1797c478bd9Sstevel@tonic-gate char err_cant_get_kname[] =
1807c478bd9Sstevel@tonic-gate 	"Can't get %s back from system call.\n";
1817c478bd9Sstevel@tonic-gate char err_null_kname[] =
1827c478bd9Sstevel@tonic-gate 	"%s hasn't been set on this machine.\n";
1837c478bd9Sstevel@tonic-gate char err_bad_hostname[] = "hostname";
1847c478bd9Sstevel@tonic-gate char err_bad_mapname[] = "mapname";
1857c478bd9Sstevel@tonic-gate char err_bad_domainname[] = "domainname";
1867c478bd9Sstevel@tonic-gate char err_udp_failure[] =
1877c478bd9Sstevel@tonic-gate 	"Can't set up a udp connection to ypserv on host %s.\n";
1887c478bd9Sstevel@tonic-gate char yptempname_prefix[] = "ypxfr_map.";
1897c478bd9Sstevel@tonic-gate char ypbkupname_prefix[] = "ypxfr_bkup.";
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate void get_command_line_args();
1927c478bd9Sstevel@tonic-gate bool bind_to_server();
1937c478bd9Sstevel@tonic-gate bool ping_server();
1947c478bd9Sstevel@tonic-gate bool  get_private_recs();
1957c478bd9Sstevel@tonic-gate bool get_order();
1967c478bd9Sstevel@tonic-gate bool get_v1order();
1977c478bd9Sstevel@tonic-gate bool get_v2order();
1987c478bd9Sstevel@tonic-gate bool get_misc_recs();
1997c478bd9Sstevel@tonic-gate bool get_master_name();
2007c478bd9Sstevel@tonic-gate bool get_v1master_name();
2017c478bd9Sstevel@tonic-gate bool get_v2master_name();
2027c478bd9Sstevel@tonic-gate void find_map_master();
2037c478bd9Sstevel@tonic-gate bool move_map();
2047c478bd9Sstevel@tonic-gate unsigned get_local_version();
2057c478bd9Sstevel@tonic-gate void mkfilename();
2067c478bd9Sstevel@tonic-gate void mk_tmpname();
2077c478bd9Sstevel@tonic-gate bool get_map();
2087c478bd9Sstevel@tonic-gate bool add_private_entries();
2097c478bd9Sstevel@tonic-gate bool new_mapfiles();
2107c478bd9Sstevel@tonic-gate void del_mapfiles();
2117c478bd9Sstevel@tonic-gate void set_output();
2127c478bd9Sstevel@tonic-gate void logprintf();
2137c478bd9Sstevel@tonic-gate bool send_ypclear();
2147c478bd9Sstevel@tonic-gate void xfr_exit();
2157c478bd9Sstevel@tonic-gate void send_callback();
2167c478bd9Sstevel@tonic-gate int ypall_callback();
2177c478bd9Sstevel@tonic-gate int map_yperr_to_pusherr();
2187c478bd9Sstevel@tonic-gate extern CLIENT *__yp_clnt_create_rsvdport();
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate bool_t is_yptol_mode();
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate extern int errno;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * This is the mainline for the ypxfr process.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate 
229a506a34cSth int
2307c478bd9Sstevel@tonic-gate main(argc, argv)
2317c478bd9Sstevel@tonic-gate 	int argc;
2327c478bd9Sstevel@tonic-gate 	char **argv;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	static char default_domain_name[YPMAXDOMAIN];
2377c478bd9Sstevel@tonic-gate 	static unsigned big = 0xffffffff;
2387c478bd9Sstevel@tonic-gate 	int status;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	set_output();
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Inter-process lock synchronization structure. Since slave servers
2447c478bd9Sstevel@tonic-gate 	 * get their maps from another NIS server rather than LDAP they can
2457c478bd9Sstevel@tonic-gate 	 * never run in N2L mode. We thus do not have to init the update
2467c478bd9Sstevel@tonic-gate 	 * locking mechanism.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	if (init_lock_map() == FALSE) {
2497c478bd9Sstevel@tonic-gate 		exit(1);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (!domain) {
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
2577c478bd9Sstevel@tonic-gate 			domain = default_domain_name;
2587c478bd9Sstevel@tonic-gate 		} else {
2597c478bd9Sstevel@tonic-gate 			logprintf(err_cant_get_kname,
2607c478bd9Sstevel@tonic-gate 			    err_bad_domainname);
2617c478bd9Sstevel@tonic-gate 			xfr_exit(YPPUSH_RSRC);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		if (strlen(domain) == 0) {
2657c478bd9Sstevel@tonic-gate 			logprintf(err_null_kname,
2667c478bd9Sstevel@tonic-gate 			    err_bad_domainname);
2677c478bd9Sstevel@tonic-gate 			xfr_exit(YPPUSH_RSRC);
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	if (!source)
2717c478bd9Sstevel@tonic-gate 		source = domain;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (!master) {
2747c478bd9Sstevel@tonic-gate 		find_map_master();
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * if we were unable to get the master name, either from
2787c478bd9Sstevel@tonic-gate 	 * the -h option or from -C "name" option or from NIS,
2797c478bd9Sstevel@tonic-gate 	 * we are doomed !
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	if (!master) {
2827c478bd9Sstevel@tonic-gate 		xfr_exit(YPPUSH_MADDR);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (!bind_to_server(master, &master_server,
2867c478bd9Sstevel@tonic-gate 	    &master_prog_vers, &status)) {
2877c478bd9Sstevel@tonic-gate 		xfr_exit(status);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (!get_private_recs(&status)) {
2917c478bd9Sstevel@tonic-gate 		xfr_exit(status);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (!master_version) {
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		if (force) {
2977c478bd9Sstevel@tonic-gate 			master_version = &big;
2987c478bd9Sstevel@tonic-gate 			fake_master_version = TRUE;
2997c478bd9Sstevel@tonic-gate 		} else {
3007c478bd9Sstevel@tonic-gate 			logprintf(
3017c478bd9Sstevel@tonic-gate "Can't get order number for map %s from server at %s: use the -f flag.\n",
3027c478bd9Sstevel@tonic-gate 			    map, master);
3037c478bd9Sstevel@tonic-gate 			xfr_exit(YPPUSH_FORCE);
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (!move_map(&status)) {
3087c478bd9Sstevel@tonic-gate 		xfr_exit(status);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (send_clear && !send_ypclear(&status)) {
3127c478bd9Sstevel@tonic-gate 		xfr_exit(status);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (logging) {
3167c478bd9Sstevel@tonic-gate 		logprintf("Transferred map %s from %s (%d entries).\n",
3177c478bd9Sstevel@tonic-gate 		    map, master, entry_count);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	xfr_exit(YPPUSH_SUCC);
321a506a34cSth 	return (0);
3227c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * This decides whether we're being run interactively or not, and, if not,
3277c478bd9Sstevel@tonic-gate  * whether we're supposed to be logging or not.  If we are logging, it sets
3287c478bd9Sstevel@tonic-gate  * up stderr to point to the log file, and sets the "logging"
3297c478bd9Sstevel@tonic-gate  * variable.  If there's no logging, the output goes in the bit bucket.
3307c478bd9Sstevel@tonic-gate  * Logging output differs from interactive output in the presence of a
3317c478bd9Sstevel@tonic-gate  * timestamp, present only in the log file.  stderr is reset, too, because it
3327c478bd9Sstevel@tonic-gate  * it's used by various library functions, including clnt_perror.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate void
3357c478bd9Sstevel@tonic-gate set_output()
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	if (!isatty(1)) {
3387c478bd9Sstevel@tonic-gate 		if (access(logfile, W_OK)) {
3397c478bd9Sstevel@tonic-gate 			(void) freopen("/dev/null", "w", stderr);
3407c478bd9Sstevel@tonic-gate 		} else {
3417c478bd9Sstevel@tonic-gate 			(void) freopen(logfile, "a", stderr);
3427c478bd9Sstevel@tonic-gate 			logging = TRUE;
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * This constructs a logging record.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate void
3507c478bd9Sstevel@tonic-gate logprintf(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
3517c478bd9Sstevel@tonic-gate /*VARARGS*/
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	struct timeval t;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	fseek(stderr, 0, 2);
3567c478bd9Sstevel@tonic-gate 	if (logging) {
3577c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&t, NULL);
3587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%19.19s: ", ctime(&t.tv_sec));
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, (char *)arg1, arg2, arg3, arg4, arg5,
3617c478bd9Sstevel@tonic-gate 				arg6, arg7);
3627c478bd9Sstevel@tonic-gate 	fflush(stderr);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate void
3697c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
3707c478bd9Sstevel@tonic-gate 	int argc;
3717c478bd9Sstevel@tonic-gate 	char **argv;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	argv++;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3777c478bd9Sstevel@tonic-gate 		logprintf(err_usage);
3787c478bd9Sstevel@tonic-gate 		xfr_exit(YPPUSH_BADARGS);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	while (--argc) {
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		if ((*argv)[0] == '-') {
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 			switch ((*argv)[1]) {
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 			case 'f': {
3887c478bd9Sstevel@tonic-gate 				force = TRUE;
3897c478bd9Sstevel@tonic-gate 				argv++;
3907c478bd9Sstevel@tonic-gate 				break;
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 			case 'D': {
3947c478bd9Sstevel@tonic-gate 				debug = TRUE;
3957c478bd9Sstevel@tonic-gate 				argv++;
3967c478bd9Sstevel@tonic-gate 				break;
3977c478bd9Sstevel@tonic-gate 			}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 			case 'T': {
4007c478bd9Sstevel@tonic-gate 				treepush = TRUE;
4017c478bd9Sstevel@tonic-gate 				argv++;
4027c478bd9Sstevel@tonic-gate 				break;
4037c478bd9Sstevel@tonic-gate 			}
4047c478bd9Sstevel@tonic-gate 			case 'P': {
4057c478bd9Sstevel@tonic-gate 				check_count = TRUE;
4067c478bd9Sstevel@tonic-gate 				argv++;
4077c478bd9Sstevel@tonic-gate 				break;
4087c478bd9Sstevel@tonic-gate 			}
4097c478bd9Sstevel@tonic-gate 			case 'W': {
4107c478bd9Sstevel@tonic-gate 				defwrite = FALSE;
4117c478bd9Sstevel@tonic-gate 				argv++;
4127c478bd9Sstevel@tonic-gate 				break;
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 			case 'c': {
4157c478bd9Sstevel@tonic-gate 				send_clear = FALSE;
4167c478bd9Sstevel@tonic-gate 				argv++;
4177c478bd9Sstevel@tonic-gate 				break;
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			case 'h': {
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 				if (argc > 1) {
4237c478bd9Sstevel@tonic-gate 					argv++;
4247c478bd9Sstevel@tonic-gate 					argc--;
4257c478bd9Sstevel@tonic-gate 					master = *argv;
4267c478bd9Sstevel@tonic-gate 					argv++;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 					if (strlen(master) > 256) {
4297c478bd9Sstevel@tonic-gate 						logprintf(
4307c478bd9Sstevel@tonic-gate 						    err_bad_args,
4317c478bd9Sstevel@tonic-gate 						    err_bad_hostname);
4327c478bd9Sstevel@tonic-gate 						xfr_exit(YPPUSH_BADARGS);
4337c478bd9Sstevel@tonic-gate 					}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 				} else {
4367c478bd9Sstevel@tonic-gate 					logprintf(err_usage);
4377c478bd9Sstevel@tonic-gate 					xfr_exit(YPPUSH_BADARGS);
4387c478bd9Sstevel@tonic-gate 				}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 				break;
4417c478bd9Sstevel@tonic-gate 			}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 			case 'd':
4447c478bd9Sstevel@tonic-gate 				if (argc > 1) {
4457c478bd9Sstevel@tonic-gate 					argv++;
4467c478bd9Sstevel@tonic-gate 					argc--;
4477c478bd9Sstevel@tonic-gate 					domain = *argv;
4487c478bd9Sstevel@tonic-gate 					argv++;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 					if (strlen(domain) > YPMAXDOMAIN) {
4517c478bd9Sstevel@tonic-gate 						logprintf(
4527c478bd9Sstevel@tonic-gate 						    err_bad_args,
4537c478bd9Sstevel@tonic-gate 						    err_bad_domainname);
4547c478bd9Sstevel@tonic-gate 						xfr_exit(YPPUSH_BADARGS);
4557c478bd9Sstevel@tonic-gate 					}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 				} else {
4587c478bd9Sstevel@tonic-gate 					logprintf(err_usage);
4597c478bd9Sstevel@tonic-gate 					xfr_exit(YPPUSH_BADARGS);
4607c478bd9Sstevel@tonic-gate 				}
4617c478bd9Sstevel@tonic-gate 				break;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			case 's':
4647c478bd9Sstevel@tonic-gate 				if (argc > 1) {
4657c478bd9Sstevel@tonic-gate 					argv++;
4667c478bd9Sstevel@tonic-gate 					argc--;
4677c478bd9Sstevel@tonic-gate 					source = *argv;
4687c478bd9Sstevel@tonic-gate 					argv++;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 					if (strlen(source) > YPMAXDOMAIN) {
4717c478bd9Sstevel@tonic-gate 						logprintf(
4727c478bd9Sstevel@tonic-gate 						    err_bad_args,
4737c478bd9Sstevel@tonic-gate 						    err_bad_domainname);
4747c478bd9Sstevel@tonic-gate 						xfr_exit(YPPUSH_BADARGS);
4757c478bd9Sstevel@tonic-gate 					}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 				} else {
4787c478bd9Sstevel@tonic-gate 					logprintf(err_usage);
4797c478bd9Sstevel@tonic-gate 					xfr_exit(YPPUSH_BADARGS);
4807c478bd9Sstevel@tonic-gate 				}
4817c478bd9Sstevel@tonic-gate 				break;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 			case 'C':
4847c478bd9Sstevel@tonic-gate 			    if (argc > 3) {
4857c478bd9Sstevel@tonic-gate 				callback = TRUE;
4867c478bd9Sstevel@tonic-gate 				tid = *(++argv);
4877c478bd9Sstevel@tonic-gate 				proto = *(++argv);
4887c478bd9Sstevel@tonic-gate 				pushhost = *(++argv);
4897c478bd9Sstevel@tonic-gate 				if (strlen(pushhost) > 256) {
4907c478bd9Sstevel@tonic-gate 				    logprintf(err_bad_args, err_bad_hostname);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 				    xfr_exit(YPPUSH_BADARGS);
4937c478bd9Sstevel@tonic-gate 				}
4947c478bd9Sstevel@tonic-gate 				argc -= 3;
4957c478bd9Sstevel@tonic-gate 				argv++;
4967c478bd9Sstevel@tonic-gate 			    } else {
4977c478bd9Sstevel@tonic-gate 				logprintf(err_usage);
4987c478bd9Sstevel@tonic-gate 				xfr_exit(YPPUSH_BADARGS);
4997c478bd9Sstevel@tonic-gate 			    }
5007c478bd9Sstevel@tonic-gate 			    break;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 			case 'b': {
5037c478bd9Sstevel@tonic-gate 				interdomain_map = TRUE;
5047c478bd9Sstevel@tonic-gate 				interdomain_value = "";
5057c478bd9Sstevel@tonic-gate 				interdomain_sz = 0;
5067c478bd9Sstevel@tonic-gate 				argv++;
5077c478bd9Sstevel@tonic-gate 				break;
5087c478bd9Sstevel@tonic-gate 			}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 			default: {
5127c478bd9Sstevel@tonic-gate 				logprintf(err_usage);
5137c478bd9Sstevel@tonic-gate 				xfr_exit(YPPUSH_BADARGS);
5147c478bd9Sstevel@tonic-gate 			}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		} else {
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 			if (!map) {
5217c478bd9Sstevel@tonic-gate 				map = *argv;
5227c478bd9Sstevel@tonic-gate 				argv++;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 				if (strlen(map) > YPMAXMAP) {
5257c478bd9Sstevel@tonic-gate 					logprintf(err_bad_args,
5267c478bd9Sstevel@tonic-gate 					err_bad_mapname);
5277c478bd9Sstevel@tonic-gate 					xfr_exit(YPPUSH_BADARGS);
5287c478bd9Sstevel@tonic-gate 				}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 			} else {
5317c478bd9Sstevel@tonic-gate 				logprintf(err_usage);
5327c478bd9Sstevel@tonic-gate 				xfr_exit(YPPUSH_BADARGS);
5337c478bd9Sstevel@tonic-gate 			}
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (!map) {
5387c478bd9Sstevel@tonic-gate 		logprintf(err_usage);
5397c478bd9Sstevel@tonic-gate 		xfr_exit(YPPUSH_BADARGS);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * This tries to get the master name for the named map, from any
5457c478bd9Sstevel@tonic-gate  * server's version, using the vanilla NIS client interface.  If we get a
5467c478bd9Sstevel@tonic-gate  * name back, the global "master" gets pointed to it.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate void
5497c478bd9Sstevel@tonic-gate find_map_master()
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	int err;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (err = __yp_master_rsvdport(source, map, &master)) {
5547c478bd9Sstevel@tonic-gate 		logprintf("Can't get master of %s. Reason: %s.\n", map,
5557c478bd9Sstevel@tonic-gate 		    yperr_string(err));
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	yp_unbind(source);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate #ifdef TREEPUSH
562a506a34cSth int
5637c478bd9Sstevel@tonic-gate chk_treepush(name)
5647c478bd9Sstevel@tonic-gate char *name;
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	char inmap[256];
5677c478bd9Sstevel@tonic-gate 	char inkey[256];
5687c478bd9Sstevel@tonic-gate 	int inkeylen;
5697c478bd9Sstevel@tonic-gate 	char *outval;
5707c478bd9Sstevel@tonic-gate 	int outvallen;
5717c478bd9Sstevel@tonic-gate 	int err;
5727c478bd9Sstevel@tonic-gate 	outval = NULL;
5737c478bd9Sstevel@tonic-gate 	inkey[0] = 0;
5747c478bd9Sstevel@tonic-gate 	strcpy(inmap, "ypslaves.");
5757c478bd9Sstevel@tonic-gate 	strcat(inmap, name);
5767c478bd9Sstevel@tonic-gate 	gethostname(inkey, 256);
5777c478bd9Sstevel@tonic-gate 	inkeylen = strlen(inkey);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	err = yp_match(source, inmap, inkey, inkeylen, &outval, &outvallen);
5807c478bd9Sstevel@tonic-gate 	yp_unbind(source);
5817c478bd9Sstevel@tonic-gate 	return (err);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate #endif
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * This sets up a udp connection to speak the correct program and version
5877c478bd9Sstevel@tonic-gate  * to a NIS server.  vers is set to YPVERS, doesn't give a damn about
5887c478bd9Sstevel@tonic-gate  * YPOLDVERS.
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate bool
5917c478bd9Sstevel@tonic-gate bind_to_server(host, pdomb, vers, status)
5927c478bd9Sstevel@tonic-gate 	char *host;
5937c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
5947c478bd9Sstevel@tonic-gate 	unsigned int *vers;
5957c478bd9Sstevel@tonic-gate 	int *status;
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	if (ping_server(host, pdomb, YPVERS, status)) {
5987c478bd9Sstevel@tonic-gate 		*vers = YPVERS;
5997c478bd9Sstevel@tonic-gate 		return (TRUE);
6007c478bd9Sstevel@tonic-gate 	} else
6017c478bd9Sstevel@tonic-gate 		return (FALSE);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  * This sets up a UDP channel to a server which is assumed to speak an input
6067c478bd9Sstevel@tonic-gate  * version of YPPROG.  The channel is tested by pinging the server.  In all
6077c478bd9Sstevel@tonic-gate  * error cases except "Program Version Number Mismatch", the error is
6087c478bd9Sstevel@tonic-gate  * reported, and in all error cases, the client handle is destroyed and the
6097c478bd9Sstevel@tonic-gate  * socket associated with the channel is closed.
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate bool
6127c478bd9Sstevel@tonic-gate ping_server(host, pdomb, vers, status)
6137c478bd9Sstevel@tonic-gate 	char *host;
6147c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
6157c478bd9Sstevel@tonic-gate 	unsigned int vers;
6167c478bd9Sstevel@tonic-gate 	int *status;
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if ((pdomb->dom_client = __yp_clnt_create_rsvdport(host, YPPROG, vers,
6217c478bd9Sstevel@tonic-gate 							0, 0, 0)) != 0) {
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		/*
6247c478bd9Sstevel@tonic-gate 		 * if we are on a c2 system, we should only accept data
6257c478bd9Sstevel@tonic-gate 		 * from a server which is on a reserved port.
6267c478bd9Sstevel@tonic-gate 		 */
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * NUKE this for 5.0DR.
6297c478bd9Sstevel@tonic-gate 	 *
6307c478bd9Sstevel@tonic-gate 	 *	if (issecure() &&
6317c478bd9Sstevel@tonic-gate 	 *	    (pdomb->dom_server_addr.sin_family != AF_INET ||
6327c478bd9Sstevel@tonic-gate 	 *	    pdomb->dom_server_addr.sin_port >= IPPORT_RESERVED)) {
6337c478bd9Sstevel@tonic-gate 	 *		clnt_destroy(pdomb->dom_client);
6347c478bd9Sstevel@tonic-gate 	 *		close(pdomb->dom_socket);
6357c478bd9Sstevel@tonic-gate 	 *		(void) logprintf("bind_to_server: \
6367c478bd9Sstevel@tonic-gate 	 *			server is not using a privileged port\n");
6377c478bd9Sstevel@tonic-gate 	 *		*status = YPPUSH_YPERR;
6387c478bd9Sstevel@tonic-gate 	 *		return (FALSE);
6397c478bd9Sstevel@tonic-gate 	 *	}
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		rpc_stat = clnt_call(pdomb->dom_client, YPBINDPROC_NULL,
6437c478bd9Sstevel@tonic-gate 		    xdr_void, 0, xdr_void, 0, udp_timeout);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		if (rpc_stat == RPC_SUCCESS) {
6467c478bd9Sstevel@tonic-gate 			return (TRUE);
6477c478bd9Sstevel@tonic-gate 		} else {
6487c478bd9Sstevel@tonic-gate 			clnt_destroy(pdomb->dom_client);
6497c478bd9Sstevel@tonic-gate 			if (rpc_stat != RPC_PROGVERSMISMATCH) {
6507c478bd9Sstevel@tonic-gate 				(void) clnt_perror(pdomb->dom_client,
6517c478bd9Sstevel@tonic-gate 				    "ypxfr: bind_to_server clnt_call error");
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 			*status = YPPUSH_RPC;
6557c478bd9Sstevel@tonic-gate 			return (FALSE);
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 	} else {
6587c478bd9Sstevel@tonic-gate 		logprintf("bind_to_server __clnt_create_rsvd error");
6597c478bd9Sstevel@tonic-gate 		(void) clnt_pcreateerror("");
6607c478bd9Sstevel@tonic-gate 		fflush(stderr);
6617c478bd9Sstevel@tonic-gate 		*status = YPPUSH_RPC;
6627c478bd9Sstevel@tonic-gate 		return (FALSE);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * This gets values for the YP_LAST_MODIFIED and YP_MASTER_NAME keys from the
6687c478bd9Sstevel@tonic-gate  * master server's version of the map.  Values are held in static variables
6697c478bd9Sstevel@tonic-gate  * here.  In the success cases, global pointer variables are set to point at
6707c478bd9Sstevel@tonic-gate  * the local statics.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate bool
6737c478bd9Sstevel@tonic-gate get_private_recs(pushstat)
6747c478bd9Sstevel@tonic-gate 	int *pushstat;
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	static char anumber[20];
6777c478bd9Sstevel@tonic-gate 	static unsigned number;
6787c478bd9Sstevel@tonic-gate 	static char name[YPMAXPEER + 1];
6797c478bd9Sstevel@tonic-gate 	int status;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	status = 0;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (get_order(anumber, &number, &status)) {
6847c478bd9Sstevel@tonic-gate 		master_version = &number;
6857c478bd9Sstevel@tonic-gate 		master_ascii_version = anumber;
6867c478bd9Sstevel@tonic-gate 		if (debug) fprintf(stderr,
6877c478bd9Sstevel@tonic-gate 			"ypxfr: Master Version is %s\n", master_ascii_version);
6887c478bd9Sstevel@tonic-gate 	} else {
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		if (status != 0) {
6917c478bd9Sstevel@tonic-gate 			*pushstat = status;
6927c478bd9Sstevel@tonic-gate 			if (debug) fprintf(stderr,
6937c478bd9Sstevel@tonic-gate 		"ypxfr: Couldn't get map's master version number, \
6947c478bd9Sstevel@tonic-gate 		status was %d\n", status);
6957c478bd9Sstevel@tonic-gate 			return (FALSE);
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (get_master_name(name, &status)) {
7007c478bd9Sstevel@tonic-gate 		master_name = name;
7017c478bd9Sstevel@tonic-gate 		if (debug) fprintf(stderr,
7027c478bd9Sstevel@tonic-gate 			"ypxfr: Maps master is '%s'\n", master_name);
7037c478bd9Sstevel@tonic-gate 	} else {
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		if (status != 0) {
7067c478bd9Sstevel@tonic-gate 			*pushstat = status;
7077c478bd9Sstevel@tonic-gate 			if (debug) fprintf(stderr,
7087c478bd9Sstevel@tonic-gate 		"ypxfr: Couldn't get map's master name, status was %d\n",
7097c478bd9Sstevel@tonic-gate 			status);
7107c478bd9Sstevel@tonic-gate 			return (FALSE);
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 		master_name = master;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (debug)
7167c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7177c478bd9Sstevel@tonic-gate 			"ypxfr: Getting private records from master.\n");
7187c478bd9Sstevel@tonic-gate 	if (get_misc_recs(&status)) {
7197c478bd9Sstevel@tonic-gate 		if (debug)
7207c478bd9Sstevel@tonic-gate 			fprintf(stderr,
7217c478bd9Sstevel@tonic-gate 		    "ypxfr: Masters map %s secure and %s an interdomain map.\n",
7227c478bd9Sstevel@tonic-gate 			(secure_map) ? "is" : "is not",
7237c478bd9Sstevel@tonic-gate 			(interdomain_map) ? "is" : "is not");
7247c478bd9Sstevel@tonic-gate 	} else {
7257c478bd9Sstevel@tonic-gate 		if (status != 0) {
7267c478bd9Sstevel@tonic-gate 			*pushstat = status;
7277c478bd9Sstevel@tonic-gate 			if (debug)
7287c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7297c478bd9Sstevel@tonic-gate 	"ypxfr: Couldn't get state of secure and interdomain flags in map.\n");
7307c478bd9Sstevel@tonic-gate 			return (FALSE);
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	return (TRUE);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate  * This gets the map's order number from the master server
7397c478bd9Sstevel@tonic-gate  */
7407c478bd9Sstevel@tonic-gate bool
7417c478bd9Sstevel@tonic-gate get_order(an, n, pushstat)
7427c478bd9Sstevel@tonic-gate 	char *an;
7437c478bd9Sstevel@tonic-gate 	unsigned *n;
7447c478bd9Sstevel@tonic-gate 	int *pushstat;
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	if (master_prog_vers == YPVERS) {
7477c478bd9Sstevel@tonic-gate 		return (get_v2order(an, n, pushstat));
7487c478bd9Sstevel@tonic-gate 	} else
7497c478bd9Sstevel@tonic-gate 		return (FALSE);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate bool
7537c478bd9Sstevel@tonic-gate get_v2order(an, n, pushstat)
7547c478bd9Sstevel@tonic-gate 	char *an;
7557c478bd9Sstevel@tonic-gate 	unsigned *n;
7567c478bd9Sstevel@tonic-gate 	int *pushstat;
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	struct ypreq_nokey req;
7597c478bd9Sstevel@tonic-gate 	struct ypresp_order resp;
7607c478bd9Sstevel@tonic-gate 	int retval;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	req.domain = source;
7637c478bd9Sstevel@tonic-gate 	req.map = map;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/*
7667c478bd9Sstevel@tonic-gate 	 * Get the map''s order number, null-terminate it and store it,
7677c478bd9Sstevel@tonic-gate 	 * and convert it to binary and store it again.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	retval = FALSE;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	if ((enum clnt_stat) clnt_call(master_server.dom_client,
7727c478bd9Sstevel@tonic-gate 	    YPPROC_ORDER, (xdrproc_t)xdr_ypreq_nokey, (char *)&req,
7737c478bd9Sstevel@tonic-gate 	    (xdrproc_t)xdr_ypresp_order, (char *)&resp,
7747c478bd9Sstevel@tonic-gate 	    udp_timeout) == RPC_SUCCESS) {
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		if (resp.status == YP_TRUE) {
7777c478bd9Sstevel@tonic-gate 			sprintf(an, "%d", resp.ordernum);
7787c478bd9Sstevel@tonic-gate 			*n = resp.ordernum;
7797c478bd9Sstevel@tonic-gate 			retval = TRUE;
7807c478bd9Sstevel@tonic-gate 		} else if (resp.status != YP_BADDB) {
7817c478bd9Sstevel@tonic-gate 			*pushstat = ypprot_err(resp.status);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 			if (!logging) {
7847c478bd9Sstevel@tonic-gate 				logprintf(
7857c478bd9Sstevel@tonic-gate 	"(info) Can't get order number from ypserv at %s.  Reason: %s.\n",
7867c478bd9Sstevel@tonic-gate 				    master, yperr_string(
7877c478bd9Sstevel@tonic-gate 				    ypprot_err(resp.status)));
7887c478bd9Sstevel@tonic-gate 			}
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		CLNT_FREERES(master_server.dom_client,
7927c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_order,
7937c478bd9Sstevel@tonic-gate 		    (char *)&resp);
7947c478bd9Sstevel@tonic-gate 	} else {
7957c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_RPC;
7967c478bd9Sstevel@tonic-gate 		logprintf("ypxfr(get_v2order) RPC call to %s failed", master);
7977c478bd9Sstevel@tonic-gate 		clnt_perror(master_server.dom_client, "");
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	return (retval);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * Pick up the state of the YP_SECURE and YP_INTERDOMAIN records from the
8057c478bd9Sstevel@tonic-gate  * master. Only works on 4.0 V2 masters that will match a YP_ private key
8067c478bd9Sstevel@tonic-gate  * when asked to explicitly.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate bool
8097c478bd9Sstevel@tonic-gate get_misc_recs(pushstat)
8107c478bd9Sstevel@tonic-gate 	int *pushstat;
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	struct ypreq_key req;
8137c478bd9Sstevel@tonic-gate 	struct ypresp_val resp;
8147c478bd9Sstevel@tonic-gate 	int retval;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	req.domain = source;
8177c478bd9Sstevel@tonic-gate 	req.map = map;
8187c478bd9Sstevel@tonic-gate 	req.keydat.dptr   = yp_secure;
8197c478bd9Sstevel@tonic-gate 	req.keydat.dsize  = yp_secure_sz;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	resp.valdat.dptr = NULL;
8227c478bd9Sstevel@tonic-gate 	resp.valdat.dsize = 0;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Get the value of the IS_SECURE key in the map.
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	retval = FALSE;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (debug)
8307c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ypxfr: Checking masters secure key.\n");
8317c478bd9Sstevel@tonic-gate 	if ((enum clnt_stat) clnt_call(master_server.dom_client,
8327c478bd9Sstevel@tonic-gate 	    YPPROC_MATCH, (xdrproc_t)xdr_ypreq_key, (char *)&req,
8337c478bd9Sstevel@tonic-gate 	(xdrproc_t)xdr_ypresp_val, (char *)&resp,
8347c478bd9Sstevel@tonic-gate 	    udp_timeout) == RPC_SUCCESS) {
8357c478bd9Sstevel@tonic-gate 		if (resp.status == YP_TRUE) {
8367c478bd9Sstevel@tonic-gate 			if (debug)
8377c478bd9Sstevel@tonic-gate 				fprintf(stderr, "ypxfr: SECURE\n");
8387c478bd9Sstevel@tonic-gate 			secure_map = TRUE;
8397c478bd9Sstevel@tonic-gate 			retval = TRUE;
8407c478bd9Sstevel@tonic-gate 		} else if ((resp.status != YP_NOKEY) &&
8417c478bd9Sstevel@tonic-gate 			    (resp.status != YP_VERS) &&
8427c478bd9Sstevel@tonic-gate 			    (resp.status != YP_NOMORE)) {
8437c478bd9Sstevel@tonic-gate 			*pushstat = ypprot_err(resp.status);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			if (!logging) {
8467c478bd9Sstevel@tonic-gate 				logprintf(
8477c478bd9Sstevel@tonic-gate 	"(info) Can't get secure flag from ypserv at %s.  Reason: %s.\n",
8487c478bd9Sstevel@tonic-gate 				    master, yperr_string(
8497c478bd9Sstevel@tonic-gate 				    ypprot_err(resp.status)));
8507c478bd9Sstevel@tonic-gate 			}
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		CLNT_FREERES(master_server.dom_client,
8547c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_val,
8557c478bd9Sstevel@tonic-gate 		    (char *)&resp);
8567c478bd9Sstevel@tonic-gate 	} else {
8577c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_RPC;
8587c478bd9Sstevel@tonic-gate 		logprintf("ypxfr(get_misc_recs) RPC call to %s failed", master);
8597c478bd9Sstevel@tonic-gate 		clnt_perror(master_server.dom_client, "");
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if (debug)
8637c478bd9Sstevel@tonic-gate 		fprintf(stderr, "ypxfr: Checking masters INTERDOMAIN key.\n");
8647c478bd9Sstevel@tonic-gate 	req.keydat.dptr   = yp_interdomain;
8657c478bd9Sstevel@tonic-gate 	req.keydat.dsize  = yp_interdomain_sz;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	resp.valdat.dptr = NULL;
8687c478bd9Sstevel@tonic-gate 	resp.valdat.dsize = 0;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/*
8717c478bd9Sstevel@tonic-gate 	 * Get the value of the INTERDOMAIN key in the map.
8727c478bd9Sstevel@tonic-gate 	 */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if ((enum clnt_stat) clnt_call(master_server.dom_client,
8757c478bd9Sstevel@tonic-gate 	    YPPROC_MATCH, (xdrproc_t)xdr_ypreq_key, (char *)&req,
8767c478bd9Sstevel@tonic-gate 	(xdrproc_t)xdr_ypresp_val, (char *)&resp,
8777c478bd9Sstevel@tonic-gate 	    udp_timeout) == RPC_SUCCESS) {
8787c478bd9Sstevel@tonic-gate 		if (resp.status == YP_TRUE) {
8797c478bd9Sstevel@tonic-gate 			if (debug)
8807c478bd9Sstevel@tonic-gate 				fprintf(stderr, "ypxfr: INTERDOMAIN\n");
8817c478bd9Sstevel@tonic-gate 			interdomain_map = TRUE;
8827c478bd9Sstevel@tonic-gate 			interdomain_value = (char *)malloc(resp.valdat.dsize+1);
8837c478bd9Sstevel@tonic-gate 			(void) memmove(interdomain_value, resp.valdat.dptr,
8847c478bd9Sstevel@tonic-gate 					resp.valdat.dsize);
8857c478bd9Sstevel@tonic-gate 			*(interdomain_value+resp.valdat.dsize) = '\0';
8867c478bd9Sstevel@tonic-gate 			interdomain_sz = resp.valdat.dsize;
8877c478bd9Sstevel@tonic-gate 			retval = TRUE;
8887c478bd9Sstevel@tonic-gate 		} else if ((resp.status != YP_NOKEY) &&
8897c478bd9Sstevel@tonic-gate 			    (resp.status != YP_VERS) &&
8907c478bd9Sstevel@tonic-gate 			    (resp.status != YP_NOMORE)) {
8917c478bd9Sstevel@tonic-gate 			*pushstat = ypprot_err(resp.status);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 			if (!logging) {
8947c478bd9Sstevel@tonic-gate 				logprintf(
8957c478bd9Sstevel@tonic-gate 	"(info) Can't get interdomain flag from ypserv at %s.  Reason: %s.\n",
8967c478bd9Sstevel@tonic-gate 				    master, yperr_string(
8977c478bd9Sstevel@tonic-gate 				    ypprot_err(resp.status)));
8987c478bd9Sstevel@tonic-gate 			}
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		CLNT_FREERES(master_server.dom_client,
9027c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_val,
9037c478bd9Sstevel@tonic-gate 		    (char *)&resp);
9047c478bd9Sstevel@tonic-gate 	} else {
9057c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_RPC;
9067c478bd9Sstevel@tonic-gate 		logprintf("ypxfr(get_misc_recs) RPC call to %s failed", master);
9077c478bd9Sstevel@tonic-gate 		clnt_perror(master_server.dom_client, "");
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	return (retval);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate  * This gets the map's master name from the master server
9167c478bd9Sstevel@tonic-gate  */
9177c478bd9Sstevel@tonic-gate bool
9187c478bd9Sstevel@tonic-gate get_master_name(name, pushstat)
9197c478bd9Sstevel@tonic-gate 	char *name;
9207c478bd9Sstevel@tonic-gate 	int *pushstat;
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	if (master_prog_vers == YPVERS) {
9237c478bd9Sstevel@tonic-gate 		return (get_v2master_name(name, pushstat));
9247c478bd9Sstevel@tonic-gate 	} else
9257c478bd9Sstevel@tonic-gate 		return (FALSE);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate bool
9297c478bd9Sstevel@tonic-gate get_v2master_name(name, pushstat)
9307c478bd9Sstevel@tonic-gate 	char *name;
9317c478bd9Sstevel@tonic-gate 	int *pushstat;
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	struct ypreq_nokey req;
9347c478bd9Sstevel@tonic-gate 	struct ypresp_master resp;
9357c478bd9Sstevel@tonic-gate 	int retval;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	req.domain = source;
9387c478bd9Sstevel@tonic-gate 	req.map = map;
9397c478bd9Sstevel@tonic-gate 	resp.master = NULL;
9407c478bd9Sstevel@tonic-gate 	retval = FALSE;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if ((enum clnt_stat) clnt_call(master_server.dom_client,
9437c478bd9Sstevel@tonic-gate 	    YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey, (char *)&req,
9447c478bd9Sstevel@tonic-gate 	    (xdrproc_t)xdr_ypresp_master, (char *)&resp,
9457c478bd9Sstevel@tonic-gate 	    udp_timeout) == RPC_SUCCESS) {
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		if (resp.status == YP_TRUE) {
9487c478bd9Sstevel@tonic-gate 			strcpy(name, resp.master);
9497c478bd9Sstevel@tonic-gate 			retval = TRUE;
9507c478bd9Sstevel@tonic-gate 		} else if (resp.status != YP_BADDB) {
9517c478bd9Sstevel@tonic-gate 			*pushstat = ypprot_err(resp.status);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 			if (!logging) {
9547c478bd9Sstevel@tonic-gate 				logprintf(
9557c478bd9Sstevel@tonic-gate "(info) Can't get master name from ypserv at %s. Reason: %s.\n",
9567c478bd9Sstevel@tonic-gate 				    master, yperr_string(
9577c478bd9Sstevel@tonic-gate 				    ypprot_err(resp.status)));
9587c478bd9Sstevel@tonic-gate 			}
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		CLNT_FREERES(master_server.dom_client,
9627c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_master,
9637c478bd9Sstevel@tonic-gate 		    (char *)&resp);
9647c478bd9Sstevel@tonic-gate 	} else {
9657c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_RPC;
9667c478bd9Sstevel@tonic-gate 		logprintf(
9677c478bd9Sstevel@tonic-gate 		    "ypxfr(get_v2master_name) RPC call to %s failed", master);
9687c478bd9Sstevel@tonic-gate 		clnt_perror(master_server.dom_client, "");
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	return (retval);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate /*
9757c478bd9Sstevel@tonic-gate  * This does the work of transferring the map.
9767c478bd9Sstevel@tonic-gate  */
9777c478bd9Sstevel@tonic-gate bool
9787c478bd9Sstevel@tonic-gate move_map(pushstat)
9797c478bd9Sstevel@tonic-gate 	int *pushstat;
9807c478bd9Sstevel@tonic-gate {
9817c478bd9Sstevel@tonic-gate 	unsigned local_version;
9827c478bd9Sstevel@tonic-gate 	char map_name[MAXNAMLEN + 1];
9837c478bd9Sstevel@tonic-gate 	char tmp_name[MAXNAMLEN + 1];
9847c478bd9Sstevel@tonic-gate 	char bkup_name[MAXNAMLEN + 1];
9857c478bd9Sstevel@tonic-gate 	char an[11];
9867c478bd9Sstevel@tonic-gate 	unsigned n;
9877c478bd9Sstevel@tonic-gate 	datum key;
9887c478bd9Sstevel@tonic-gate 	datum val;
9897c478bd9Sstevel@tonic-gate 	int  hgstatus;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	mkfilename(map_name);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	if (!force) {
9947c478bd9Sstevel@tonic-gate 		local_version = get_local_version(map_name);
9957c478bd9Sstevel@tonic-gate 		if (debug) fprintf(stderr,
9967c478bd9Sstevel@tonic-gate 			"ypxfr: Local version of map '%s' is %d\n",
9977c478bd9Sstevel@tonic-gate 			map_name, local_version);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		if (local_version >= *master_version) {
10007c478bd9Sstevel@tonic-gate 			logprintf(
10017c478bd9Sstevel@tonic-gate 			    "Map %s at %s is not more recent than local.\n",
10027c478bd9Sstevel@tonic-gate 			    map, master);
10037c478bd9Sstevel@tonic-gate 			*pushstat = YPPUSH_AGE;
10047c478bd9Sstevel@tonic-gate 			return (FALSE);
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	mk_tmpname(yptempname_prefix, tmp_name);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (!new_mapfiles(tmp_name)) {
10117c478bd9Sstevel@tonic-gate 		logprintf(
10127c478bd9Sstevel@tonic-gate 		    "Can't create temp map %s.\n", tmp_name);
10137c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_FILE;
10147c478bd9Sstevel@tonic-gate 		return (FALSE);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if ((hgstatus = ypxfrd_getdbm(tmp_name, master, source, map)) < 0)
10187c478bd9Sstevel@tonic-gate 	{
10197c478bd9Sstevel@tonic-gate 	logprintf(
10207c478bd9Sstevel@tonic-gate "(info) %s %s %s ypxfrd getdbm failed (reason = %d) -- using ypxfr\n",
10217c478bd9Sstevel@tonic-gate 		master, domain, map, hgstatus);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	    db = dbm_open(tmp_name, O_RDWR + O_CREAT + O_TRUNC, 0644);
10247c478bd9Sstevel@tonic-gate 	if (db == NULL) {
10257c478bd9Sstevel@tonic-gate 		logprintf(
10267c478bd9Sstevel@tonic-gate 		    "Can't dbm init temp map %s.\n", tmp_name);
10277c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10287c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
10297c478bd9Sstevel@tonic-gate 		return (FALSE);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 	if (defwrite) dbm_setdefwrite(db);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (!get_map(tmp_name, pushstat)) {
10347c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10357c478bd9Sstevel@tonic-gate 		return (FALSE);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (!add_private_entries(tmp_name)) {
10397c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10407c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
10417c478bd9Sstevel@tonic-gate 		return (FALSE);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * Decide whether the map just transferred is a secure map.
10467c478bd9Sstevel@tonic-gate 	 * If we already know the local version was secure, we do not
10477c478bd9Sstevel@tonic-gate 	 * need to check this version.
10487c478bd9Sstevel@tonic-gate 	 */
10497c478bd9Sstevel@tonic-gate 	if (!secure_map) {
10507c478bd9Sstevel@tonic-gate 		key.dptr = yp_secure;
10517c478bd9Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
10527c478bd9Sstevel@tonic-gate 		val = dbm_fetch(db, key);
10537c478bd9Sstevel@tonic-gate 		if (val.dptr != NULL) {
10547c478bd9Sstevel@tonic-gate 			secure_map = TRUE;
10557c478bd9Sstevel@tonic-gate 		}
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (dbm_close_status(db) < 0) {
10597c478bd9Sstevel@tonic-gate 		logprintf(
10607c478bd9Sstevel@tonic-gate 		    "Can't do dbm close operation on temp map %s.\n",
10617c478bd9Sstevel@tonic-gate 		    tmp_name);
10627c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10637c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
10647c478bd9Sstevel@tonic-gate 		return (FALSE);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (!get_order(an, &n, pushstat)) {
10687c478bd9Sstevel@tonic-gate 		return (FALSE);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	if (n != *master_version) {
10717c478bd9Sstevel@tonic-gate 		logprintf(
10727c478bd9Sstevel@tonic-gate 		    "Version skew at %s while transferring map %s.\n",
10737c478bd9Sstevel@tonic-gate 		    master, map);
10747c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10757c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_SKEW;
10767c478bd9Sstevel@tonic-gate 		return (FALSE);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (check_count)
10807c478bd9Sstevel@tonic-gate 	if (!count_mismatch(tmp_name, entry_count)) {
10817c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10827c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
10837c478bd9Sstevel@tonic-gate 		return (FALSE);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 	} else {
10867c478bd9Sstevel@tonic-gate 	/* touch up the map */
10877c478bd9Sstevel@tonic-gate 	    db = dbm_open(tmp_name, 2, 0644);
10887c478bd9Sstevel@tonic-gate 	if (db == NULL) {
10897c478bd9Sstevel@tonic-gate 		logprintf(
10907c478bd9Sstevel@tonic-gate 		    "Can't dbm init temp map %s.\n", tmp_name);
10917c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10927c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
10937c478bd9Sstevel@tonic-gate 		return (FALSE);
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (!add_private_entries(tmp_name)) {
10987c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
10997c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
11007c478bd9Sstevel@tonic-gate 		return (FALSE);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/*
11047c478bd9Sstevel@tonic-gate 	 * Decide whether the map just transferred is a secure map.
11057c478bd9Sstevel@tonic-gate 	 * If we already know the local version was secure, we do not
11067c478bd9Sstevel@tonic-gate 	 * need to check this version.
11077c478bd9Sstevel@tonic-gate 	 */
11087c478bd9Sstevel@tonic-gate 	if (!secure_map) {
11097c478bd9Sstevel@tonic-gate 		key.dptr = yp_secure;
11107c478bd9Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
11117c478bd9Sstevel@tonic-gate 		val = dbm_fetch(db, key);
11127c478bd9Sstevel@tonic-gate 		if (val.dptr != NULL) {
11137c478bd9Sstevel@tonic-gate 			secure_map = TRUE;
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if (dbm_close_status(db) < 0) {
11187c478bd9Sstevel@tonic-gate 		logprintf(
11197c478bd9Sstevel@tonic-gate 		    "Can't do dbm close operation on temp map %s.\n",
11207c478bd9Sstevel@tonic-gate 		    tmp_name);
11217c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
11227c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_DBM;
11237c478bd9Sstevel@tonic-gate 		return (FALSE);
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (lock_map(map_name) == 0) {
11297c478bd9Sstevel@tonic-gate 		del_mapfiles(tmp_name);
11307c478bd9Sstevel@tonic-gate 		logprintf("Lock error on %s\n", map_name);
11317c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_FILE;
11327c478bd9Sstevel@tonic-gate 		return (FALSE);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 	if (!check_map_existence(map_name)) {
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		if (!rename_map(tmp_name, map_name, secure_map)) {
11377c478bd9Sstevel@tonic-gate 			del_mapfiles(tmp_name);
11387c478bd9Sstevel@tonic-gate 			logprintf(
11397c478bd9Sstevel@tonic-gate 			    "Rename error:  couldn't mv %s to %s.\n",
11407c478bd9Sstevel@tonic-gate 			    tmp_name, map_name);
11417c478bd9Sstevel@tonic-gate 			*pushstat = YPPUSH_FILE;
11427c478bd9Sstevel@tonic-gate 			unlock_map(map_name);
11437c478bd9Sstevel@tonic-gate 			return (FALSE);
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	} else {
11477c478bd9Sstevel@tonic-gate 		mk_tmpname(ypbkupname_prefix, bkup_name);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 		if (!rename_map(map_name, bkup_name, secure_map)) {
11507c478bd9Sstevel@tonic-gate 			(void) rename_map(bkup_name, map_name, secure_map);
11517c478bd9Sstevel@tonic-gate 			logprintf(
11527c478bd9Sstevel@tonic-gate 		"Rename error:  check that old %s is still intact.\n",
11537c478bd9Sstevel@tonic-gate 		map_name);
11547c478bd9Sstevel@tonic-gate 			del_mapfiles(tmp_name);
11557c478bd9Sstevel@tonic-gate 			*pushstat = YPPUSH_FILE;
11567c478bd9Sstevel@tonic-gate 			unlock_map(map_name);
11577c478bd9Sstevel@tonic-gate 			return (FALSE);
11587c478bd9Sstevel@tonic-gate 		}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 		if (rename_map(tmp_name, map_name, secure_map)) {
11617c478bd9Sstevel@tonic-gate 			del_mapfiles(bkup_name);
11627c478bd9Sstevel@tonic-gate 		} else {
11637c478bd9Sstevel@tonic-gate 			del_mapfiles(tmp_name);
11647c478bd9Sstevel@tonic-gate 			(void) rename_map(bkup_name, map_name, secure_map);
11657c478bd9Sstevel@tonic-gate 				logprintf(
11667c478bd9Sstevel@tonic-gate 		"Rename error:  check that old %s is still intact.\n",
11677c478bd9Sstevel@tonic-gate 			    map_name);
11687c478bd9Sstevel@tonic-gate 			*pushstat = YPPUSH_FILE;
11697c478bd9Sstevel@tonic-gate 			unlock_map(map_name);
11707c478bd9Sstevel@tonic-gate 			return (FALSE);
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 	if (unlock_map(map_name) == 0)
11747c478bd9Sstevel@tonic-gate 		return (FALSE);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	return (TRUE);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate /*
11807c478bd9Sstevel@tonic-gate  * This tries to get the order number out of the local version of the map.
11817c478bd9Sstevel@tonic-gate  * If the attempt fails for any version, the function will return "0"
11827c478bd9Sstevel@tonic-gate  */
11837c478bd9Sstevel@tonic-gate unsigned
11847c478bd9Sstevel@tonic-gate get_local_version(name)
11857c478bd9Sstevel@tonic-gate 	char *name;
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	datum key;
11887c478bd9Sstevel@tonic-gate 	datum val;
11897c478bd9Sstevel@tonic-gate 	char number[11];
11907c478bd9Sstevel@tonic-gate 	DBM *db;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (!check_map_existence(name)) {
11937c478bd9Sstevel@tonic-gate 		return (0);
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	if (debug) fprintf(stderr,
11967c478bd9Sstevel@tonic-gate 		"ypxfr: Map does exist, checking version now.\n");
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	if ((db = dbm_open(name, 0, 0)) == 0) {
11997c478bd9Sstevel@tonic-gate 		return (0);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	key.dptr = yp_last_modified;
12037c478bd9Sstevel@tonic-gate 	key.dsize = yp_last_modified_sz;
12047c478bd9Sstevel@tonic-gate 	val = dbm_fetch(db, key);
12057c478bd9Sstevel@tonic-gate 	if (!val.dptr) {	/* Check to see if dptr is NULL */
12067c478bd9Sstevel@tonic-gate 		return (0);
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 	if (val.dsize == 0 || val.dsize > 10) {
12097c478bd9Sstevel@tonic-gate 		return (0);
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 	/* Now save this value while we have it available */
12127c478bd9Sstevel@tonic-gate 	(void) memmove(number, val.dptr, val.dsize);
12137c478bd9Sstevel@tonic-gate 	number[val.dsize] = '\0';
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/*
12167c478bd9Sstevel@tonic-gate 	 * Now check to see if it is 'secure'. If we haven't already
12177c478bd9Sstevel@tonic-gate 	 * determined that it is secure in get_private_recs() then we check
12187c478bd9Sstevel@tonic-gate 	 * the local map here.
12197c478bd9Sstevel@tonic-gate 	 */
12207c478bd9Sstevel@tonic-gate 	if (!secure_map) {
12217c478bd9Sstevel@tonic-gate 		key.dptr = yp_secure;
12227c478bd9Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
12237c478bd9Sstevel@tonic-gate 		val = dbm_fetch(db, key);
12247c478bd9Sstevel@tonic-gate 		secure_map = (val.dptr != NULL);
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/*
12287c478bd9Sstevel@tonic-gate 	 * Now check to see if interdomain requests are made of the local
12297c478bd9Sstevel@tonic-gate 	 * map. Keep the value around if they are.
12307c478bd9Sstevel@tonic-gate 	 */
12317c478bd9Sstevel@tonic-gate 	if (!interdomain_map) {
12327c478bd9Sstevel@tonic-gate 		key.dptr = yp_interdomain;
12337c478bd9Sstevel@tonic-gate 		key.dsize = yp_interdomain_sz;
12347c478bd9Sstevel@tonic-gate 		val = dbm_fetch(db, key);
12357c478bd9Sstevel@tonic-gate 		if (interdomain_map = (val.dptr != NULL)) {
12367c478bd9Sstevel@tonic-gate 			interdomain_value = (char *)malloc(val.dsize+1);
12377c478bd9Sstevel@tonic-gate 			(void) memmove(interdomain_value, val.dptr, val.dsize);
12387c478bd9Sstevel@tonic-gate 			*(interdomain_value+val.dsize) = '\0';
12397c478bd9Sstevel@tonic-gate 			interdomain_sz = val.dsize;
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/* finish up */
12447c478bd9Sstevel@tonic-gate 	(void) dbm_close_status(db);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	return ((unsigned)atoi(number));
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate  * This constructs a file name for a map, minus its dbm_dir
12517c478bd9Sstevel@tonic-gate  * or dbm_pag extensions
12527c478bd9Sstevel@tonic-gate  */
12537c478bd9Sstevel@tonic-gate void
12547c478bd9Sstevel@tonic-gate mkfilename(ppath)
12557c478bd9Sstevel@tonic-gate 	char *ppath;
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate 	bool_t yptol_mode;
12587c478bd9Sstevel@tonic-gate 	int len;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/* Work out if we are in yptol mode */
12617c478bd9Sstevel@tonic-gate 	yptol_mode = is_yptol_mode();
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	len = strlen(domain) + strlen(map) + strlen(ypdbpath) + 3;
12647c478bd9Sstevel@tonic-gate 	if (yptol_mode)
12657c478bd9Sstevel@tonic-gate 		len += strlen(NTOL_PREFIX);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (len > (MAXNAMLEN + 1)) {
12687c478bd9Sstevel@tonic-gate 		logprintf("Map name string too long.\n");
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	(void) strcpy(ppath, ypdbpath);
12727c478bd9Sstevel@tonic-gate 	(void) strcat(ppath, "/");
12737c478bd9Sstevel@tonic-gate 	(void) strcat(ppath, domain);
12747c478bd9Sstevel@tonic-gate 	(void) strcat(ppath, "/");
12757c478bd9Sstevel@tonic-gate 	if (yptol_mode)
12767c478bd9Sstevel@tonic-gate 		(void) strcat(ppath, NTOL_PREFIX);
12777c478bd9Sstevel@tonic-gate 	(void) strcat(ppath, map);
12787c478bd9Sstevel@tonic-gate }
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate /*
12817c478bd9Sstevel@tonic-gate  * This returns a temporary name for a map transfer minus its dbm_dir or
12827c478bd9Sstevel@tonic-gate  * dbm_pag extensions.
12837c478bd9Sstevel@tonic-gate  */
12847c478bd9Sstevel@tonic-gate void
12857c478bd9Sstevel@tonic-gate mk_tmpname(prefix, xfr_name)
12867c478bd9Sstevel@tonic-gate 	char *prefix;
12877c478bd9Sstevel@tonic-gate 	char *xfr_name;
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate 	char xfr_anumber[10];
12907c478bd9Sstevel@tonic-gate 	long xfr_number;
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	if (!xfr_name) {
12937c478bd9Sstevel@tonic-gate 		return;
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	xfr_number = getpid();
12977c478bd9Sstevel@tonic-gate 	(void) sprintf(xfr_anumber, "%d", xfr_number);
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	(void) strcpy(xfr_name, ypdbpath);
13007c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, "/");
13017c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, domain);
13027c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, "/");
13037c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, prefix);
13047c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, map);
13057c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, ".");
13067c478bd9Sstevel@tonic-gate 	(void) strcat(xfr_name, xfr_anumber);
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate /*
13107c478bd9Sstevel@tonic-gate  * This deletes the .pag and .dir files which implement a map.
13117c478bd9Sstevel@tonic-gate  *
13127c478bd9Sstevel@tonic-gate  * Note:  No error checking is done here for a garbage input file name or for
13137c478bd9Sstevel@tonic-gate  * failed unlink operations.
13147c478bd9Sstevel@tonic-gate  */
13157c478bd9Sstevel@tonic-gate void
13167c478bd9Sstevel@tonic-gate del_mapfiles(basename)
13177c478bd9Sstevel@tonic-gate 	char *basename;
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	char dbfilename[MAXNAMLEN + 1];
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (!basename) {
13227c478bd9Sstevel@tonic-gate 		return;
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	strcpy(dbfilename, basename);
13267c478bd9Sstevel@tonic-gate 	strcat(dbfilename, dbm_pag);
13277c478bd9Sstevel@tonic-gate 	unlink(dbfilename);
13287c478bd9Sstevel@tonic-gate 	strcpy(dbfilename, basename);
13297c478bd9Sstevel@tonic-gate 	strcat(dbfilename, dbm_dir);
13307c478bd9Sstevel@tonic-gate 	unlink(dbfilename);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate /*
13347c478bd9Sstevel@tonic-gate  * This creates <pname>.dir and <pname>.pag
13357c478bd9Sstevel@tonic-gate  */
13367c478bd9Sstevel@tonic-gate bool
13377c478bd9Sstevel@tonic-gate new_mapfiles(pname)
13387c478bd9Sstevel@tonic-gate 	char *pname;
13397c478bd9Sstevel@tonic-gate {
13407c478bd9Sstevel@tonic-gate 	char dbfile[MAXNAMLEN + 1];
13417c478bd9Sstevel@tonic-gate 	int f;
13427c478bd9Sstevel@tonic-gate 	int len;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	if (!pname || ((len = strlen(pname)) == 0) ||
13457c478bd9Sstevel@tonic-gate 	    (len + 5) > (MAXNAMLEN + 1)) {
13467c478bd9Sstevel@tonic-gate 		return (FALSE);
13477c478bd9Sstevel@tonic-gate 	}
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	errno = 0;
13507c478bd9Sstevel@tonic-gate 	(void) strcpy(dbfile, pname);
13517c478bd9Sstevel@tonic-gate 	(void) strcat(dbfile, dbm_dir);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if ((f = open(dbfile, (O_WRONLY | O_CREAT | O_TRUNC), 0600)) >= 0) {
13547c478bd9Sstevel@tonic-gate 		(void) close(f);
13557c478bd9Sstevel@tonic-gate 		(void) strcpy(dbfile, pname);
13567c478bd9Sstevel@tonic-gate 		(void) strcat(dbfile, dbm_pag);
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 		if ((f = open(dbfile, (O_WRONLY | O_CREAT | O_TRUNC),
13597c478bd9Sstevel@tonic-gate 		    0600)) >= 0) {
13607c478bd9Sstevel@tonic-gate 			(void) close(f);
13617c478bd9Sstevel@tonic-gate 			return (TRUE);
13627c478bd9Sstevel@tonic-gate 		} else {
13637c478bd9Sstevel@tonic-gate 			return (FALSE);
13647c478bd9Sstevel@tonic-gate 		}
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	} else {
13677c478bd9Sstevel@tonic-gate 		return (FALSE);
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
1371a506a34cSth int
13727c478bd9Sstevel@tonic-gate count_callback(status)
13737c478bd9Sstevel@tonic-gate 	int status;
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate 	if (status != YP_TRUE) {
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 		if (status != YP_NOMORE) {
13787c478bd9Sstevel@tonic-gate 			logprintf(
13797c478bd9Sstevel@tonic-gate 			    "Error from ypserv on %s (ypall_callback) = %s.\n",
13807c478bd9Sstevel@tonic-gate 			    master, yperr_string(ypprot_err(status)));
13817c478bd9Sstevel@tonic-gate 		}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		return (TRUE);
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	entry_count++;
13877c478bd9Sstevel@tonic-gate 	return (FALSE);
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate /*
13917c478bd9Sstevel@tonic-gate  * This counts the entries in the dbm file after the transfer to
13927c478bd9Sstevel@tonic-gate  * make sure that the dbm file was built correctly.
13937c478bd9Sstevel@tonic-gate  * Returns TRUE if everything is OK, FALSE if they mismatch.
13947c478bd9Sstevel@tonic-gate  */
1395a506a34cSth int
13967c478bd9Sstevel@tonic-gate count_mismatch(pname, oldcount)
13977c478bd9Sstevel@tonic-gate 	char *pname;
13987c478bd9Sstevel@tonic-gate 	int oldcount;
13997c478bd9Sstevel@tonic-gate {
14007c478bd9Sstevel@tonic-gate 	datum key;
14017c478bd9Sstevel@tonic-gate 	DBM *db;
14027c478bd9Sstevel@tonic-gate #ifdef REALLY_PARANOID
14037c478bd9Sstevel@tonic-gate 	struct ypall_callback cbinfo;
14047c478bd9Sstevel@tonic-gate 	struct ypreq_nokey allreq;
14057c478bd9Sstevel@tonic-gate 	enum clnt_stat s;
14067c478bd9Sstevel@tonic-gate 	struct dom_binding domb;
14077c478bd9Sstevel@tonic-gate 	datum value;
14087c478bd9Sstevel@tonic-gate #endif /* REALLY_PARANOID */
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	entry_count = 0;
14117c478bd9Sstevel@tonic-gate 	db = dbm_open(pname, 0, 0);
14127c478bd9Sstevel@tonic-gate 	if (db) {
14137c478bd9Sstevel@tonic-gate 	    for (key = dbm_firstkey(db);
14147c478bd9Sstevel@tonic-gate 				key.dptr != NULL; key = dbm_nextkey(db))
14157c478bd9Sstevel@tonic-gate 		entry_count++;
14167c478bd9Sstevel@tonic-gate 	    dbm_close_status(db);
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	if (oldcount != entry_count) {
14207c478bd9Sstevel@tonic-gate 	logprintf(
14217c478bd9Sstevel@tonic-gate 		    "*** Count mismatch in dbm file %s: old=%d, new=%d ***\n",
14227c478bd9Sstevel@tonic-gate 		    map, oldcount, entry_count);
14237c478bd9Sstevel@tonic-gate 	return (FALSE);
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate #ifdef REALLY_PARANOID
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if ((domb.dom_client = __yp_clnt_create_rsvdport(master, YPPROG,
14297c478bd9Sstevel@tonic-gate 						    master_prog_vers,
14307c478bd9Sstevel@tonic-gate 						    "tcp6", 0, 0)) == 0 &&
14317c478bd9Sstevel@tonic-gate 		(domb.dom_client = __yp_clnt_create_rsvdport(master, YPPROG,
14327c478bd9Sstevel@tonic-gate 						    master_prog_vers,
14337c478bd9Sstevel@tonic-gate 						    "tcp", 0, 0)) == 0) {
14347c478bd9Sstevel@tonic-gate 		clnt_pcreateerror("ypxfr (mismatch) - TCP channel "
14357c478bd9Sstevel@tonic-gate 					"create failure");
14367c478bd9Sstevel@tonic-gate 		return (FALSE);
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (master_prog_vers == YPVERS) {
14407c478bd9Sstevel@tonic-gate 		int tmpstat;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		allreq.domain = source;
14437c478bd9Sstevel@tonic-gate 		allreq.map = map;
14447c478bd9Sstevel@tonic-gate 		cbinfo.foreach = count_callback;
14457c478bd9Sstevel@tonic-gate 		tmpstat = 0;
14467c478bd9Sstevel@tonic-gate 		cbinfo.data = (char *)&tmpstat;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 		entry_count = 0;
14497c478bd9Sstevel@tonic-gate 		s = clnt_call(domb.dom_client, YPPROC_ALL, xdr_ypreq_nokey,
14507c478bd9Sstevel@tonic-gate 		    &allreq, xdr_ypall, &cbinfo, tcp_timeout);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 		if (tmpstat == 0) {
14537c478bd9Sstevel@tonic-gate 			if (s == RPC_SUCCESS) {
14547c478bd9Sstevel@tonic-gate 			} else {
14557c478bd9Sstevel@tonic-gate 				clnt_perror(domb.dom_client,
14567c478bd9Sstevel@tonic-gate 		"ypxfr (get_map/all) - RPC clnt_call (TCP) failure");
14577c478bd9Sstevel@tonic-gate 		    return (FALSE);
14587c478bd9Sstevel@tonic-gate 			}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		} else {
14617c478bd9Sstevel@tonic-gate 		    return (FALSE);
14627c478bd9Sstevel@tonic-gate 		}
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	} else {
14657c478bd9Sstevel@tonic-gate 	    logprintf("Wrong version number!\n");
14667c478bd9Sstevel@tonic-gate 	    return (FALSE);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 	clnt_destroy(domb.dom_client);
14697c478bd9Sstevel@tonic-gate 	close(domb.dom_socket);
14707c478bd9Sstevel@tonic-gate 	entry_count += 2;			/* add in YP_entries */
14717c478bd9Sstevel@tonic-gate 	if (oldcount != entry_count) {
14727c478bd9Sstevel@tonic-gate 		logprintf(
14737c478bd9Sstevel@tonic-gate 	"*** Count mismatch after enumerate %s: old=%d, new=%d ***\n",
14747c478bd9Sstevel@tonic-gate 		    map, oldcount, entry_count);
14757c478bd9Sstevel@tonic-gate 		return (FALSE);
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate #endif /* REALLY_PARANOID */
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	return (TRUE);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate  * This sets up a TCP connection to the master server, and either gets
14847c478bd9Sstevel@tonic-gate  * ypall_callback to do all the work of writing it to the local dbm file
14857c478bd9Sstevel@tonic-gate  * (if the ypserv is current version), or does it itself for an old ypserv.
14867c478bd9Sstevel@tonic-gate  */
14877c478bd9Sstevel@tonic-gate bool
14887c478bd9Sstevel@tonic-gate get_map(pname, pushstat)
14897c478bd9Sstevel@tonic-gate 	char *pname;
14907c478bd9Sstevel@tonic-gate 	int *pushstat;
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate 	struct dom_binding domb;
14937c478bd9Sstevel@tonic-gate 	enum clnt_stat s;
14947c478bd9Sstevel@tonic-gate 	struct ypreq_nokey allreq;
14957c478bd9Sstevel@tonic-gate 	struct ypall_callback cbinfo;
14967c478bd9Sstevel@tonic-gate 	bool retval = FALSE;
14977c478bd9Sstevel@tonic-gate 	int tmpstat;
14987c478bd9Sstevel@tonic-gate 	int	recvsiz = 24 * 1024;
14997c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
15007c478bd9Sstevel@tonic-gate 	int fd;
15017c478bd9Sstevel@tonic-gate 	struct netbuf *svcaddr;
15027c478bd9Sstevel@tonic-gate 	char *netid[] = { "tcp6", "tcp" };
15037c478bd9Sstevel@tonic-gate 	int i, lastnetid = (sizeof (netid)/sizeof (netid[0])) - 1;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	svcaddr = (struct netbuf *)calloc(1, sizeof (struct netbuf));
15067c478bd9Sstevel@tonic-gate 	if (! svcaddr)
15077c478bd9Sstevel@tonic-gate 		return (FALSE);
15087c478bd9Sstevel@tonic-gate 	svcaddr->maxlen = 32;
15097c478bd9Sstevel@tonic-gate 	svcaddr->len = 32;
15107c478bd9Sstevel@tonic-gate 	svcaddr->buf = (char *)malloc(32);
15117c478bd9Sstevel@tonic-gate 	if (! svcaddr->buf) {
15127c478bd9Sstevel@tonic-gate 		free(svcaddr);
15137c478bd9Sstevel@tonic-gate 		return (FALSE);
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	for (i = 0; i <= lastnetid; i++) {
15177c478bd9Sstevel@tonic-gate 		fd = RPC_ANYFD;
15187c478bd9Sstevel@tonic-gate 		if ((nconf = getnetconfigent(netid[i])) == NULL) {
15197c478bd9Sstevel@tonic-gate 			if (i != lastnetid)
15207c478bd9Sstevel@tonic-gate 				continue;
15217c478bd9Sstevel@tonic-gate 			logprintf("ypxfr: tcp transport not supported\n");
15227c478bd9Sstevel@tonic-gate 			free(svcaddr->buf);
15237c478bd9Sstevel@tonic-gate 			free(svcaddr);
15247c478bd9Sstevel@tonic-gate 			return (FALSE);
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 		if (rpcb_getaddr(YPPROG, master_prog_vers, nconf, svcaddr,
15277c478bd9Sstevel@tonic-gate 				master) == FALSE) {
15287c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
15297c478bd9Sstevel@tonic-gate 			if (i != lastnetid)
15307c478bd9Sstevel@tonic-gate 				continue;
15317c478bd9Sstevel@tonic-gate 			logprintf("ypxfr: could not get %s address\n", master);
15327c478bd9Sstevel@tonic-gate 			free(svcaddr->buf);
15337c478bd9Sstevel@tonic-gate 			free(svcaddr);
15347c478bd9Sstevel@tonic-gate 			return (FALSE);
15357c478bd9Sstevel@tonic-gate 		}
15367c478bd9Sstevel@tonic-gate 		if ((domb.dom_client = __nis_clnt_create(fd, nconf, 0, svcaddr,
15377c478bd9Sstevel@tonic-gate 			0, YPPROG, master_prog_vers, recvsiz, 0)) == 0) {
15387c478bd9Sstevel@tonic-gate 			freenetconfigent(nconf);
15397c478bd9Sstevel@tonic-gate 			if (i != lastnetid)
15407c478bd9Sstevel@tonic-gate 				continue;
15417c478bd9Sstevel@tonic-gate 			clnt_pcreateerror(
15427c478bd9Sstevel@tonic-gate 				"ypxfr (get_map) - TCP channel create failure");
15437c478bd9Sstevel@tonic-gate 			*pushstat = YPPUSH_RPC;
15447c478bd9Sstevel@tonic-gate 			free(svcaddr->buf);
15457c478bd9Sstevel@tonic-gate 			free(svcaddr);
15467c478bd9Sstevel@tonic-gate 			return (FALSE);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 		break;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	entry_count = 0;
15527c478bd9Sstevel@tonic-gate 	if (master_prog_vers == YPVERS) {
15537c478bd9Sstevel@tonic-gate 		allreq.domain = source;
15547c478bd9Sstevel@tonic-gate 		allreq.map = map;
15557c478bd9Sstevel@tonic-gate 		cbinfo.foreach = ypall_callback;
15567c478bd9Sstevel@tonic-gate 		tmpstat = 0;
15577c478bd9Sstevel@tonic-gate 		cbinfo.data = (char *)&tmpstat;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 		s = clnt_call(domb.dom_client, YPPROC_ALL,
15607c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypreq_nokey,
15617c478bd9Sstevel@tonic-gate 			(char *)&allreq, (xdrproc_t)xdr_ypall, (char *)&cbinfo,
15627c478bd9Sstevel@tonic-gate 			tcp_timeout);
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		if (tmpstat == 0) {
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 			if (s == RPC_SUCCESS) {
15677c478bd9Sstevel@tonic-gate 				retval = TRUE;
15687c478bd9Sstevel@tonic-gate 			} else {
15697c478bd9Sstevel@tonic-gate 				clnt_perror(domb.dom_client,
15707c478bd9Sstevel@tonic-gate 			"ypxfr (get_map/all) - RPC clnt_call (TCP) failure");
15717c478bd9Sstevel@tonic-gate 				*pushstat = YPPUSH_RPC;
15727c478bd9Sstevel@tonic-gate 			}
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 		} else {
15757c478bd9Sstevel@tonic-gate 			*pushstat = tmpstat;
15767c478bd9Sstevel@tonic-gate 		}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	} else
15797c478bd9Sstevel@tonic-gate 		retval = FALSE; /* barf again at YPOLDVERS */
15807c478bd9Sstevel@tonic-gate cleanup:
15817c478bd9Sstevel@tonic-gate 	clnt_destroy(domb.dom_client);
15827c478bd9Sstevel@tonic-gate 	return (retval);
15837c478bd9Sstevel@tonic-gate }
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate /*
15867c478bd9Sstevel@tonic-gate  * This sticks each key-value pair into the current map.  It returns FALSE as
15877c478bd9Sstevel@tonic-gate  * long as it wants to keep getting called back, and TRUE on error conditions
15887c478bd9Sstevel@tonic-gate  * and "No more k-v pairs".
15897c478bd9Sstevel@tonic-gate  */
15907c478bd9Sstevel@tonic-gate int
15917c478bd9Sstevel@tonic-gate ypall_callback(status, key, kl, val, vl, pushstat)
15927c478bd9Sstevel@tonic-gate 	int status;
15937c478bd9Sstevel@tonic-gate 	char *key;
15947c478bd9Sstevel@tonic-gate 	int kl;
15957c478bd9Sstevel@tonic-gate 	char *val;
15967c478bd9Sstevel@tonic-gate 	int vl;
15977c478bd9Sstevel@tonic-gate 	int *pushstat;
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	datum keydat;
16007c478bd9Sstevel@tonic-gate 	datum valdat;
16017c478bd9Sstevel@tonic-gate 	datum test;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (status != YP_TRUE) {
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 		if (status != YP_NOMORE) {
16067c478bd9Sstevel@tonic-gate 			logprintf(
16077c478bd9Sstevel@tonic-gate 			    "Error from ypserv on %s (ypall_callback) = %s.\n",
16087c478bd9Sstevel@tonic-gate 			    master, yperr_string(ypprot_err(status)));
16097c478bd9Sstevel@tonic-gate 			*pushstat = map_yperr_to_pusherr(status);
16107c478bd9Sstevel@tonic-gate 		}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 		return (TRUE);
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	keydat.dptr = key;
16167c478bd9Sstevel@tonic-gate 	keydat.dsize = kl;
16177c478bd9Sstevel@tonic-gate 	valdat.dptr = val;
16187c478bd9Sstevel@tonic-gate 	valdat.dsize = vl;
16197c478bd9Sstevel@tonic-gate 	entry_count++;
16207c478bd9Sstevel@tonic-gate /* way too many fetches */
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate #ifdef PARANOID
16237c478bd9Sstevel@tonic-gate 	test = dbm_fetch(db, keydat);
16247c478bd9Sstevel@tonic-gate 	if (test.dptr != NULL) {
16257c478bd9Sstevel@tonic-gate 		logprintf("Duplicate key %s in map %s\n", key, map);
16267c478bd9Sstevel@tonic-gate 		*pushstat  = YPPUSH_DBM;
16277c478bd9Sstevel@tonic-gate 		return (TRUE);
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate #endif /* PARANOID */
16307c478bd9Sstevel@tonic-gate 	if (dbm_store(db, keydat, valdat, 0) < 0) {
16317c478bd9Sstevel@tonic-gate 		logprintf(
16327c478bd9Sstevel@tonic-gate 		    "Can't do dbm store into temp map %s.\n", map);
16337c478bd9Sstevel@tonic-gate 		*pushstat  = YPPUSH_DBM;
16347c478bd9Sstevel@tonic-gate 		return (TRUE);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate #ifdef PARANOID
16377c478bd9Sstevel@tonic-gate 	test = dbm_fetch(db, keydat);
16387c478bd9Sstevel@tonic-gate 	if (test.dptr == NULL) {
16397c478bd9Sstevel@tonic-gate 		logprintf("Key %s was not inserted into dbm file %s\n",
16407c478bd9Sstevel@tonic-gate 			key, map);
16417c478bd9Sstevel@tonic-gate 		*pushstat  = YPPUSH_DBM;
16427c478bd9Sstevel@tonic-gate 		return (TRUE);
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate #endif /* PARANOID */
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	if (dbm_error(db)) {
16477c478bd9Sstevel@tonic-gate 		logprintf("Key %s dbm_error raised in file %s\n",
16487c478bd9Sstevel@tonic-gate 			key, map);
16497c478bd9Sstevel@tonic-gate 		*pushstat  = YPPUSH_DBM;
16507c478bd9Sstevel@tonic-gate 		return (TRUE);
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 	return (FALSE);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate  * This maps a YP_xxxx error code into a YPPUSH_xxxx error code
16577c478bd9Sstevel@tonic-gate  */
16587c478bd9Sstevel@tonic-gate int
16597c478bd9Sstevel@tonic-gate map_yperr_to_pusherr(yperr)
16607c478bd9Sstevel@tonic-gate 	int yperr;
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	int reason;
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	switch (yperr) {
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	case YP_NOMORE:
16677c478bd9Sstevel@tonic-gate 		reason = YPPUSH_SUCC;
16687c478bd9Sstevel@tonic-gate 		break;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	case YP_NOMAP:
16717c478bd9Sstevel@tonic-gate 		reason = YPPUSH_NOMAP;
16727c478bd9Sstevel@tonic-gate 		break;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	case YP_NODOM:
16757c478bd9Sstevel@tonic-gate 		reason = YPPUSH_NODOM;
16767c478bd9Sstevel@tonic-gate 		break;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	case YP_NOKEY:
16797c478bd9Sstevel@tonic-gate 		reason = YPPUSH_YPERR;
16807c478bd9Sstevel@tonic-gate 		break;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	case YP_BADARGS:
16837c478bd9Sstevel@tonic-gate 		reason = YPPUSH_BADARGS;
16847c478bd9Sstevel@tonic-gate 		break;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	case YP_BADDB:
16877c478bd9Sstevel@tonic-gate 		reason = YPPUSH_YPERR;
16887c478bd9Sstevel@tonic-gate 		break;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	default:
16917c478bd9Sstevel@tonic-gate 		reason = YPPUSH_XFRERR;
16927c478bd9Sstevel@tonic-gate 		break;
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	return (reason);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /*
16997c478bd9Sstevel@tonic-gate  * This writes the last-modified and master entries into the new dbm file
17007c478bd9Sstevel@tonic-gate  */
17017c478bd9Sstevel@tonic-gate bool
17027c478bd9Sstevel@tonic-gate add_private_entries(pname)
17037c478bd9Sstevel@tonic-gate 	char *pname;
17047c478bd9Sstevel@tonic-gate {
17057c478bd9Sstevel@tonic-gate 	datum key;
17067c478bd9Sstevel@tonic-gate 	datum val;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	if (!fake_master_version) {
17097c478bd9Sstevel@tonic-gate 		key.dptr = yp_last_modified;
17107c478bd9Sstevel@tonic-gate 		key.dsize = yp_last_modified_sz;
17117c478bd9Sstevel@tonic-gate 		val.dptr = master_ascii_version;
17127c478bd9Sstevel@tonic-gate 		val.dsize = strlen(master_ascii_version);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 		if (dbm_store(db, key, val, 1) < 0) {
17157c478bd9Sstevel@tonic-gate 			logprintf(
17167c478bd9Sstevel@tonic-gate 			    "Can't do dbm store into temp map %s.\n",
17177c478bd9Sstevel@tonic-gate 			    pname);
17187c478bd9Sstevel@tonic-gate 			return (FALSE);
17197c478bd9Sstevel@tonic-gate 		}
17207c478bd9Sstevel@tonic-gate 		entry_count++;
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	if (master_name) {
17247c478bd9Sstevel@tonic-gate 		key.dptr = yp_master_name;
17257c478bd9Sstevel@tonic-gate 		key.dsize = yp_master_name_sz;
17267c478bd9Sstevel@tonic-gate 		val.dptr = master_name;
17277c478bd9Sstevel@tonic-gate 		val.dsize = strlen(master_name);
17287c478bd9Sstevel@tonic-gate 		if (dbm_store(db, key, val, 1) < 0) {
17297c478bd9Sstevel@tonic-gate 			logprintf(
17307c478bd9Sstevel@tonic-gate 			    "Can't do dbm store into temp map %s.\n",
17317c478bd9Sstevel@tonic-gate 			    pname);
17327c478bd9Sstevel@tonic-gate 			return (FALSE);
17337c478bd9Sstevel@tonic-gate 		}
17347c478bd9Sstevel@tonic-gate 		entry_count++;
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	if (interdomain_map) {
17387c478bd9Sstevel@tonic-gate 		key.dptr = yp_interdomain;
17397c478bd9Sstevel@tonic-gate 		key.dsize = yp_interdomain_sz;
17407c478bd9Sstevel@tonic-gate 		val.dptr = interdomain_value;
17417c478bd9Sstevel@tonic-gate 		val.dsize = interdomain_sz;
17427c478bd9Sstevel@tonic-gate 		if (dbm_store(db, key, val, 1) < 0) {
17437c478bd9Sstevel@tonic-gate 			logprintf(
17447c478bd9Sstevel@tonic-gate 			    "Can't do dbm store into temp map %s.\n",
17457c478bd9Sstevel@tonic-gate 			    pname);
17467c478bd9Sstevel@tonic-gate 			return (FALSE);
17477c478bd9Sstevel@tonic-gate 		}
17487c478bd9Sstevel@tonic-gate 		entry_count++;
17497c478bd9Sstevel@tonic-gate 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	if (secure_map) {
17527c478bd9Sstevel@tonic-gate 		key.dptr = yp_secure;
17537c478bd9Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
17547c478bd9Sstevel@tonic-gate 		val.dptr = yp_secure;
17557c478bd9Sstevel@tonic-gate 		val.dsize = yp_secure_sz;
17567c478bd9Sstevel@tonic-gate 		if (dbm_store(db, key, val, 1) < 0) {
17577c478bd9Sstevel@tonic-gate 			logprintf(
17587c478bd9Sstevel@tonic-gate 			    "Can't do dbm store into temp map %s.\n",
17597c478bd9Sstevel@tonic-gate 			    pname);
17607c478bd9Sstevel@tonic-gate 			return (FALSE);
17617c478bd9Sstevel@tonic-gate 		}
17627c478bd9Sstevel@tonic-gate 		entry_count++;
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	return (TRUE);
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate /*
17707c478bd9Sstevel@tonic-gate  * This sends a YPPROC_CLEAR message to the local ypserv process.
17717c478bd9Sstevel@tonic-gate  */
17727c478bd9Sstevel@tonic-gate bool
17737c478bd9Sstevel@tonic-gate send_ypclear(pushstat)
17747c478bd9Sstevel@tonic-gate 	int *pushstat;
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	struct dom_binding domb;
17777c478bd9Sstevel@tonic-gate 	char local_host_name[256];
17787c478bd9Sstevel@tonic-gate 	unsigned int progvers;
17797c478bd9Sstevel@tonic-gate 	int status;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	if (gethostname(local_host_name, 256)) {
17827c478bd9Sstevel@tonic-gate 		logprintf("Can't get local machine name.\n");
17837c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_RSRC;
17847c478bd9Sstevel@tonic-gate 		return (FALSE);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	if (!bind_to_server(local_host_name, &domb,
17887c478bd9Sstevel@tonic-gate 	    &progvers, &status)) {
17897c478bd9Sstevel@tonic-gate 		*pushstat = YPPUSH_CLEAR;
17907c478bd9Sstevel@tonic-gate 		return (FALSE);
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if ((enum clnt_stat) clnt_call(domb.dom_client,
17947c478bd9Sstevel@tonic-gate 	    YPPROC_CLEAR, xdr_void, 0, xdr_void, 0,
17957c478bd9Sstevel@tonic-gate 	    udp_timeout) != RPC_SUCCESS) {
17967c478bd9Sstevel@tonic-gate 		logprintf(
17977c478bd9Sstevel@tonic-gate 		"Can't send ypclear message to ypserv on the local machine.\n");
17987c478bd9Sstevel@tonic-gate 		xfr_exit(YPPUSH_CLEAR);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	return (TRUE);
18027c478bd9Sstevel@tonic-gate }
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate /*
18057c478bd9Sstevel@tonic-gate  * This decides if send_callback has to get called, and does the process exit.
18067c478bd9Sstevel@tonic-gate  */
18077c478bd9Sstevel@tonic-gate void
18087c478bd9Sstevel@tonic-gate xfr_exit(status)
18097c478bd9Sstevel@tonic-gate 	int status;
18107c478bd9Sstevel@tonic-gate {
18117c478bd9Sstevel@tonic-gate 	if (callback) {
18127c478bd9Sstevel@tonic-gate 		send_callback(&status);
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if (status == YPPUSH_SUCC) {
18167c478bd9Sstevel@tonic-gate #ifdef TREEPUSH
18177c478bd9Sstevel@tonic-gate 		if (treepush) {
18187c478bd9Sstevel@tonic-gate 		if (debug)
18197c478bd9Sstevel@tonic-gate 		execlp("./yppush", "yppush", "-T", map, 0);
18207c478bd9Sstevel@tonic-gate 		execlp("/usr/etc/yp/yppush", "yppush", "-T", map, 0);
18217c478bd9Sstevel@tonic-gate 		perror("yppush");
18227c478bd9Sstevel@tonic-gate 		}
18237c478bd9Sstevel@tonic-gate #endif
18247c478bd9Sstevel@tonic-gate 		exit(0);
18257c478bd9Sstevel@tonic-gate 	} else {
18267c478bd9Sstevel@tonic-gate 		exit(1);
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate /*
18317c478bd9Sstevel@tonic-gate  * This sets up a UDP connection to the yppush process which contacted our
1832*48bbca81SDaniel Hoffman  * parent ypserv, and sends it a status on the requested transfer.
18337c478bd9Sstevel@tonic-gate  */
18347c478bd9Sstevel@tonic-gate void
18357c478bd9Sstevel@tonic-gate send_callback(status)
18367c478bd9Sstevel@tonic-gate 	int *status;
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate 	struct yppushresp_xfr resp;
18397c478bd9Sstevel@tonic-gate 	struct dom_binding domb;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	resp.transid = (unsigned long) atoi(tid);
18427c478bd9Sstevel@tonic-gate 	resp.status = (unsigned long) *status;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	udp_timeout.tv_sec = CALLTIMEOUT;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	if ((domb.dom_client = __yp_clnt_create_rsvdport(pushhost,
18477c478bd9Sstevel@tonic-gate 						    (ulong_t)atoi(proto),
18487c478bd9Sstevel@tonic-gate 						    YPPUSHVERS,
18497c478bd9Sstevel@tonic-gate 						    0, 0, 0)) == NULL) {
18507c478bd9Sstevel@tonic-gate 		*status = YPPUSH_RPC;
18517c478bd9Sstevel@tonic-gate 		return;
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if ((enum clnt_stat) clnt_call(domb.dom_client,
18557c478bd9Sstevel@tonic-gate 	    YPPUSHPROC_XFRRESP, (xdrproc_t)xdr_yppushresp_xfr,
18567c478bd9Sstevel@tonic-gate 	    (char *)&resp, xdr_void, 0,
18577c478bd9Sstevel@tonic-gate 	    udp_timeout) != RPC_SUCCESS) {
18587c478bd9Sstevel@tonic-gate 		*status = YPPUSH_RPC;
18597c478bd9Sstevel@tonic-gate 		return;
18607c478bd9Sstevel@tonic-gate 	}
18617c478bd9Sstevel@tonic-gate }
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate /*
18647c478bd9Sstevel@tonic-gate  * FUNCTION:    is_yptol_mode();
18657c478bd9Sstevel@tonic-gate  *
18667c478bd9Sstevel@tonic-gate  * DESCRIPTION: Determines if we should run in N2L or traditional mode based
18677c478bd9Sstevel@tonic-gate  *              on the presence of the N2L mapping file.
18687c478bd9Sstevel@tonic-gate  *
18697c478bd9Sstevel@tonic-gate  *		This is a copy of a function from libnisdb. If more than this
18707c478bd9Sstevel@tonic-gate  *		one function become required it may be worth linking the
18717c478bd9Sstevel@tonic-gate  *		entire lib.
18727c478bd9Sstevel@tonic-gate  *
18737c478bd9Sstevel@tonic-gate  * INPUTS:      Nothing
18747c478bd9Sstevel@tonic-gate  *
18757c478bd9Sstevel@tonic-gate  * OUTPUTS:     TRUE = Run in N2L mode
18767c478bd9Sstevel@tonic-gate  *              FALSE = Run in traditional mode.
18777c478bd9Sstevel@tonic-gate  */
18787c478bd9Sstevel@tonic-gate bool_t
18797c478bd9Sstevel@tonic-gate is_yptol_mode()
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	struct stat filestat;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	if (stat(NTOL_MAP_FILE, &filestat) != -1)
18847c478bd9Sstevel@tonic-gate 		return (TRUE);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	return (FALSE);
18877c478bd9Sstevel@tonic-gate }
1888