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