xref: /illumos-gate/usr/src/cmd/krb5/slave/kprop.c (revision ef150c2b)
17c478bd9Sstevel@tonic-gate /*
22ea18f2bSPeter Shoults  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * slave/kprop.c
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
97c478bd9Sstevel@tonic-gate  * All Rights Reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
127c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
137c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
147c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
15ff67a31bSToomas Soome  *
167c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
177c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
187c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
197c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
207c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
217c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
227c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
237c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
247c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
257c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
267c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
277c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
287c478bd9Sstevel@tonic-gate  * or implied warranty.
29ff67a31bSToomas Soome  *
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/socket.h>
447c478bd9Sstevel@tonic-gate #include <netinet/in.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <netdb.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <libintl.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <k5-int.h>
517c478bd9Sstevel@tonic-gate #include "com_err.h"
527c478bd9Sstevel@tonic-gate #include "kprop.h"
537c478bd9Sstevel@tonic-gate static char *kprop_version = KPROP_PROT_VERSION;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate char	*progname = 0;
567c478bd9Sstevel@tonic-gate int     debug = 0;
577c478bd9Sstevel@tonic-gate char	*srvtab = 0;
587c478bd9Sstevel@tonic-gate char	*slave_host;
597c478bd9Sstevel@tonic-gate char	*realm = 0;
607c478bd9Sstevel@tonic-gate char	*file = KPROP_DEFAULT_FILE;
617c478bd9Sstevel@tonic-gate short	port = 0;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate krb5_principal	my_principal;		/* The Kerberos principal we'll be */
647c478bd9Sstevel@tonic-gate 				/* running under, initialized in */
657c478bd9Sstevel@tonic-gate 				/* get_tickets() */
667c478bd9Sstevel@tonic-gate krb5_ccache	ccache;		/* Credentials cache which we'll be using */
677c478bd9Sstevel@tonic-gate krb5_creds	creds;
687c478bd9Sstevel@tonic-gate krb5_address	sender_addr;
697c478bd9Sstevel@tonic-gate krb5_address	receiver_addr;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void	PRS
7256a424ccSmp 	(int, char **);
737c478bd9Sstevel@tonic-gate void	get_tickets
7456a424ccSmp 	(krb5_context);
75ff67a31bSToomas Soome static void usage
7656a424ccSmp 	(void);
77ff67a31bSToomas Soome krb5_error_code open_connection
7856a424ccSmp 	(char *, int *, char *, unsigned int);
79ff67a31bSToomas Soome void	kerberos_authenticate
80ff67a31bSToomas Soome 	(krb5_context, krb5_auth_context *,
817c478bd9Sstevel@tonic-gate 		   int, krb5_principal, krb5_creds **);
82ff67a31bSToomas Soome int	open_database
8356a424ccSmp 	(krb5_context, char *, int *);
84ff67a31bSToomas Soome void	close_database
8556a424ccSmp 	(krb5_context, int);
86ff67a31bSToomas Soome void	xmit_database
87ff67a31bSToomas Soome 	(krb5_context, krb5_auth_context, krb5_creds *,
887c478bd9Sstevel@tonic-gate 		   int, int, int);
89ff67a31bSToomas Soome void	send_error
9056a424ccSmp 	(krb5_context, krb5_creds *, int, char *, krb5_error_code);
91ff67a31bSToomas Soome void	update_last_prop_file
9256a424ccSmp 	(char *, char *);
937c478bd9Sstevel@tonic-gate 
usage()947c478bd9Sstevel@tonic-gate static void usage()
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	fprintf(stderr,
977c478bd9Sstevel@tonic-gate 		gettext
987c478bd9Sstevel@tonic-gate 		("\nUsage: %s [-r realm] [-f file] [-d] [-P port] [-s srvtab] slave_host\n\n"),
997c478bd9Sstevel@tonic-gate 		progname);
1007c478bd9Sstevel@tonic-gate 	exit(1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
main(argc,argv)1047c478bd9Sstevel@tonic-gate main(argc, argv)
1057c478bd9Sstevel@tonic-gate 	int	argc;
1067c478bd9Sstevel@tonic-gate 	char	**argv;
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	int	fd, database_fd, database_size;
1097c478bd9Sstevel@tonic-gate 	krb5_error_code	retval;
1107c478bd9Sstevel@tonic-gate 	krb5_context context;
1117c478bd9Sstevel@tonic-gate 	krb5_creds *my_creds;
1127c478bd9Sstevel@tonic-gate 	krb5_auth_context auth_context;
1137c478bd9Sstevel@tonic-gate #define	ERRMSGSIZ	256
1147c478bd9Sstevel@tonic-gate 	char	Errmsg[ERRMSGSIZ];
115ff67a31bSToomas Soome 
1167c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
1197c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"KPROP_TEST"	/* Use this only if it weren't */
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	retval = krb5_init_context(&context);
1257c478bd9Sstevel@tonic-gate 	if (retval) {
1267c478bd9Sstevel@tonic-gate 		com_err(argv[0], retval, gettext("while initializing krb5"));
1277c478bd9Sstevel@tonic-gate 		exit(1);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	PRS(argc, argv);
1307c478bd9Sstevel@tonic-gate 	get_tickets(context);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	database_fd = open_database(context, file, &database_size);
13356a424ccSmp 	retval = open_connection(slave_host, &fd, Errmsg, sizeof(Errmsg));
13456a424ccSmp 	if (retval) {
1357c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("%s while opening connection to %s"),
1367c478bd9Sstevel@tonic-gate 			Errmsg, slave_host);
1377c478bd9Sstevel@tonic-gate 		exit(1);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (fd < 0) {
140ff67a31bSToomas Soome 		fprintf(stderr,
1417c478bd9Sstevel@tonic-gate 			gettext("%s: %s while opening connection to %s\n"),
1427c478bd9Sstevel@tonic-gate 			progname, Errmsg, slave_host);
1437c478bd9Sstevel@tonic-gate 		exit(1);
1447c478bd9Sstevel@tonic-gate 	}
145ff67a31bSToomas Soome 	kerberos_authenticate(context, &auth_context, fd, my_principal,
1467c478bd9Sstevel@tonic-gate 			      &my_creds);
147ff67a31bSToomas Soome 	xmit_database(context, auth_context, my_creds, fd, database_fd,
1487c478bd9Sstevel@tonic-gate 		      database_size);
1497c478bd9Sstevel@tonic-gate 	update_last_prop_file(slave_host, file);
1507c478bd9Sstevel@tonic-gate 	printf(gettext("Database propagation to %s: SUCCEEDED\n"), slave_host);
1517c478bd9Sstevel@tonic-gate 	krb5_free_cred_contents(context, my_creds);
1527c478bd9Sstevel@tonic-gate 	close_database(context, database_fd);
1537c478bd9Sstevel@tonic-gate 	exit(0);
1547c478bd9Sstevel@tonic-gate }
15556a424ccSmp 
PRS(argc,argv)1567c478bd9Sstevel@tonic-gate void PRS(argc, argv)
1577c478bd9Sstevel@tonic-gate 	int	argc;
1587c478bd9Sstevel@tonic-gate 	char	**argv;
1597c478bd9Sstevel@tonic-gate {
160ff67a31bSToomas Soome 	int c;
1617c478bd9Sstevel@tonic-gate 	register char	*word, ch;
162ff67a31bSToomas Soome 	extern int optind;
1637c478bd9Sstevel@tonic-gate 	extern char *optarg;
164ff67a31bSToomas Soome 
1657c478bd9Sstevel@tonic-gate 	progname = argv[0];
166*ef150c2bSRichard Lowe 	while ((c = getopt(argc, argv, "r:f:dP:s:h:")) != EOF) {
1677c478bd9Sstevel@tonic-gate 		switch (c) {
1687c478bd9Sstevel@tonic-gate 				case 'r':
1697c478bd9Sstevel@tonic-gate 			realm = optarg;
1707c478bd9Sstevel@tonic-gate 					if (!realm)
1717c478bd9Sstevel@tonic-gate 						usage();
1727c478bd9Sstevel@tonic-gate 					break;
1737c478bd9Sstevel@tonic-gate 				case 'f':
1747c478bd9Sstevel@tonic-gate 			file = optarg;
1757c478bd9Sstevel@tonic-gate 					if (!file)
1767c478bd9Sstevel@tonic-gate 						usage();
1777c478bd9Sstevel@tonic-gate 					break;
1787c478bd9Sstevel@tonic-gate 				case 'd':
1797c478bd9Sstevel@tonic-gate 					debug++;
1807c478bd9Sstevel@tonic-gate 					break;
1817c478bd9Sstevel@tonic-gate 				case 'P':
18245526e97Ssemery 			port = atoi(optarg);
1837c478bd9Sstevel@tonic-gate 					if (!port)
1847c478bd9Sstevel@tonic-gate 						usage();
1857c478bd9Sstevel@tonic-gate 					break;
1867c478bd9Sstevel@tonic-gate 				case 's':
1877c478bd9Sstevel@tonic-gate 			srvtab = optarg;
1887c478bd9Sstevel@tonic-gate 					if (!srvtab)
1897c478bd9Sstevel@tonic-gate 						usage();
1907c478bd9Sstevel@tonic-gate 					break;
1917c478bd9Sstevel@tonic-gate 		case '?':
1927c478bd9Sstevel@tonic-gate 				default:
1937c478bd9Sstevel@tonic-gate 			printf (gettext("Error \n"));
1947c478bd9Sstevel@tonic-gate 					usage();
1957c478bd9Sstevel@tonic-gate 				}
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 	argc -= optind;
1987c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1997c478bd9Sstevel@tonic-gate 	if (*argv)
2007c478bd9Sstevel@tonic-gate 		slave_host = *argv;
2017c478bd9Sstevel@tonic-gate 			else
2027c478bd9Sstevel@tonic-gate 		usage();
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
get_tickets(context)2057c478bd9Sstevel@tonic-gate void get_tickets(context)
2067c478bd9Sstevel@tonic-gate     krb5_context context;
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	char   buf[BUFSIZ];
2097c478bd9Sstevel@tonic-gate 	krb5_error_code retval;
2107c478bd9Sstevel@tonic-gate 	static char tkstring[] = "/tmp/kproptktXXXXXX";
2117c478bd9Sstevel@tonic-gate 	krb5_keytab keytab = NULL;
2127c478bd9Sstevel@tonic-gate 	krb5_get_init_creds_opt opt;
2137c478bd9Sstevel@tonic-gate 	char *svcname = NULL;
2147c478bd9Sstevel@tonic-gate 	char *def_realm = NULL;
2157c478bd9Sstevel@tonic-gate 	char *master_host = NULL;
216ff67a31bSToomas Soome 
217ff67a31bSToomas Soome 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Figure out what tickets we'll be using to send stuff
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	if (realm) {
2227c478bd9Sstevel@tonic-gate 	    if ((def_realm = strdup(realm)) == NULL) {
2237c478bd9Sstevel@tonic-gate 	      com_err(progname, ENOMEM,
2247c478bd9Sstevel@tonic-gate 		      gettext("while allocating default realm name '%s'"),
2257c478bd9Sstevel@tonic-gate 		      realm);
2267c478bd9Sstevel@tonic-gate 	      exit(1);
2277c478bd9Sstevel@tonic-gate 	    }
2287c478bd9Sstevel@tonic-gate 	} else {
2297c478bd9Sstevel@tonic-gate 	    retval = krb5_get_default_realm(context, &def_realm);
2307c478bd9Sstevel@tonic-gate 	    if (retval) {
2317c478bd9Sstevel@tonic-gate 	        com_err(progname, retval,
2327c478bd9Sstevel@tonic-gate 			gettext("while getting default realm"));
2337c478bd9Sstevel@tonic-gate 	        exit(1);
2347c478bd9Sstevel@tonic-gate 	    }
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Always pick up the master hostname from krb5.conf, as
2397c478bd9Sstevel@tonic-gate 	 * opposed to picking up the localhost, so we do not get bit
2407c478bd9Sstevel@tonic-gate 	 * if the master KDC is HA and hence points to a logicalhost.
241ff67a31bSToomas Soome 	 */
242ff67a31bSToomas Soome 	retval = kadm5_get_master(context, def_realm, &master_host);
2437c478bd9Sstevel@tonic-gate 	if (retval) {
2447c478bd9Sstevel@tonic-gate 	    free(def_realm);
2457c478bd9Sstevel@tonic-gate 	    com_err(progname, retval,
2467c478bd9Sstevel@tonic-gate 		gettext("while getting admin server fqdn"));
2477c478bd9Sstevel@tonic-gate 	    exit(1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	retval = krb5_sname_to_principal(context, master_host, NULL,
2517c478bd9Sstevel@tonic-gate 					 KRB5_NT_SRV_HST, &my_principal);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	free(def_realm);
2547c478bd9Sstevel@tonic-gate 	free(master_host);
2557c478bd9Sstevel@tonic-gate 	if (retval) {
2567c478bd9Sstevel@tonic-gate 	    com_err(progname, errno, gettext("while setting client principal name"));
2577c478bd9Sstevel@tonic-gate 	    exit(1);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	if (realm) {
26056a424ccSmp 	    retval = krb5_set_principal_realm(context, my_principal, realm);
26156a424ccSmp 	    if (retval) {
26256a424ccSmp 	        com_err(progname, errno,
26356a424ccSmp 			 gettext("while setting client principal realm"));
26456a424ccSmp 		exit(1);
26556a424ccSmp 	    }
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate #if 0
2687c478bd9Sstevel@tonic-gate 	krb5_princ_type(context, my_principal) = KRB5_NT_PRINCIPAL;
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Initialize cache file which we're going to be using
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	(void) mktemp(tkstring);
2752ea18f2bSPeter Shoults 	snprintf(buf, sizeof (buf), "FILE:%s", tkstring);
27656a424ccSmp 
27756a424ccSmp 	retval = krb5_cc_resolve(context, buf, &ccache);
27856a424ccSmp 	if (retval) {
2797c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("while opening credential cache %s"),
2807c478bd9Sstevel@tonic-gate 			buf);
2817c478bd9Sstevel@tonic-gate 		exit(1);
2827c478bd9Sstevel@tonic-gate 	}
28356a424ccSmp 
28456a424ccSmp 	retval = krb5_cc_initialize(context, ccache, my_principal);
28556a424ccSmp 	if (retval) {
2867c478bd9Sstevel@tonic-gate 		com_err (progname, retval, gettext("when initializing cache %s"),
2877c478bd9Sstevel@tonic-gate 			 buf);
2887c478bd9Sstevel@tonic-gate 		exit(1);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/*
2927c478bd9Sstevel@tonic-gate 	 * Get the tickets we'll need.
2937c478bd9Sstevel@tonic-gate 	 *
2947c478bd9Sstevel@tonic-gate 	 * Construct the principal name for the slave host.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	memset((char *)&creds, 0, sizeof(creds));
2977c478bd9Sstevel@tonic-gate 	retval = krb5_sname_to_principal(context,
2987c478bd9Sstevel@tonic-gate 					 slave_host, KPROP_SERVICE_NAME,
2997c478bd9Sstevel@tonic-gate 					 KRB5_NT_SRV_HST, &creds.server);
3007c478bd9Sstevel@tonic-gate 	if (retval) {
3017c478bd9Sstevel@tonic-gate 	    com_err(progname, errno, gettext("while setting server principal name"));
3027c478bd9Sstevel@tonic-gate 	    (void) krb5_cc_destroy(context, ccache);
3037c478bd9Sstevel@tonic-gate 	    exit(1);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	if (realm) {
30656a424ccSmp 	    retval = krb5_set_principal_realm(context, creds.server, realm);
30756a424ccSmp 	    if (retval) {
30856a424ccSmp 	        com_err(progname, errno,
30956a424ccSmp 			gettext("while setting server principal realm"));
31056a424ccSmp 		exit(1);
31156a424ccSmp 	    }
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Now fill in the client....
3167c478bd9Sstevel@tonic-gate 	 */
31756a424ccSmp 	retval = krb5_copy_principal(context, my_principal, &creds.client);
31856a424ccSmp 	if (retval) {
3197c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("While copying client principal"));
3207c478bd9Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
3217c478bd9Sstevel@tonic-gate 		exit(1);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	if (srvtab) {
32456a424ccSmp 	        retval = krb5_kt_resolve(context, srvtab, &keytab);
32556a424ccSmp 		if (retval) {
3267c478bd9Sstevel@tonic-gate 			com_err(progname, retval, gettext("while resolving keytab"));
3277c478bd9Sstevel@tonic-gate 			(void) krb5_cc_destroy(context, ccache);
3287c478bd9Sstevel@tonic-gate 			exit(1);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	(void) memset(&opt, 0, sizeof (opt));
3327c478bd9Sstevel@tonic-gate 	krb5_get_init_creds_opt_init(&opt);
3337c478bd9Sstevel@tonic-gate 	retval = krb5_unparse_name(context,  creds.server, &svcname);
3347c478bd9Sstevel@tonic-gate 	if (retval) {
3357c478bd9Sstevel@tonic-gate 		com_err(progname, errno, gettext("while parsing svc principal name"));
3367c478bd9Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
3377c478bd9Sstevel@tonic-gate 		exit (1);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	retval = krb5_get_init_creds_keytab(context, &creds, creds.client,
3407c478bd9Sstevel@tonic-gate 				keytab,  0, svcname, &opt);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (svcname)
3437c478bd9Sstevel@tonic-gate 		free(svcname);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (retval) {
3467c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("while getting initial ticket\n"));
3477c478bd9Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
3487c478bd9Sstevel@tonic-gate 		exit(1);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (keytab)
3527c478bd9Sstevel@tonic-gate 	    (void) krb5_kt_close(context, keytab);
353ff67a31bSToomas Soome 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Now destroy the cache right away --- the credentials we
3567c478bd9Sstevel@tonic-gate 	 * need will be in my_creds.
3577c478bd9Sstevel@tonic-gate 	 */
35856a424ccSmp 	retval = krb5_cc_destroy(context, ccache);
35956a424ccSmp 	if (retval) {
3607c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("while destroying ticket cache"));
3617c478bd9Sstevel@tonic-gate 		exit(1);
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
365505d05c7Sgtb /* SUNW14resync - SOCKET is defed in 1.4 in port-sockets.h */
366505d05c7Sgtb #ifdef SOCKET
367505d05c7Sgtb #undef SOCKET
368505d05c7Sgtb #endif
369505d05c7Sgtb 
3707c478bd9Sstevel@tonic-gate krb5_error_code
open_connection(host,fd,Errmsg,ErrmsgSz)3717c478bd9Sstevel@tonic-gate open_connection(host, fd, Errmsg, ErrmsgSz)
37256a424ccSmp 	char		*host;
37356a424ccSmp 	int		*fd;
37456a424ccSmp 	char		*Errmsg;
37556a424ccSmp 	unsigned int	 ErrmsgSz;
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	int	s;
378ff67a31bSToomas Soome 	krb5_error_code	retval = 0;
379ff67a31bSToomas Soome 
3807c478bd9Sstevel@tonic-gate 	int	socket_length;
3817c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
3827c478bd9Sstevel@tonic-gate 	struct sockaddr_storage	  ss;
3837c478bd9Sstevel@tonic-gate 	char serv_or_port[NI_MAXSERV];
3847c478bd9Sstevel@tonic-gate 	enum err_types {SOCKET, CONNECT};
385ff67a31bSToomas Soome 	int which_err = -1;
386ff67a31bSToomas Soome 
3877c478bd9Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
3887c478bd9Sstevel@tonic-gate 	hints.ai_family = AF_UNSPEC;    /* go for either IPv4 or v6 */
3897c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (port != 0)
392ff67a31bSToomas Soome 		(void) snprintf(serv_or_port, sizeof(serv_or_port), ("%hu"),
3937c478bd9Sstevel@tonic-gate 				port);
394ff67a31bSToomas Soome 	else
3957c478bd9Sstevel@tonic-gate 		strncpy(serv_or_port, KPROP_SERVICE, sizeof(serv_or_port));
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (getaddrinfo(host, serv_or_port, &hints, &aitop) != 0) {
3987c478bd9Sstevel@tonic-gate 		(void) snprintf(Errmsg, ERRMSGSIZ, gettext("%s: unknown host"),
3997c478bd9Sstevel@tonic-gate 				host);
4007c478bd9Sstevel@tonic-gate 		*fd = -1;
4017c478bd9Sstevel@tonic-gate 		return(0);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
4057c478bd9Sstevel@tonic-gate 		if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
4067c478bd9Sstevel@tonic-gate 			continue;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		s = socket(ai->ai_family, SOCK_STREAM, 0);
4097c478bd9Sstevel@tonic-gate 		if (s < 0) {
4107c478bd9Sstevel@tonic-gate 			which_err = SOCKET;
4117c478bd9Sstevel@tonic-gate 			retval = errno;
4127c478bd9Sstevel@tonic-gate 			continue;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
4167c478bd9Sstevel@tonic-gate 		    errno != EINPROGRESS) {
4177c478bd9Sstevel@tonic-gate 			which_err = CONNECT;
4187c478bd9Sstevel@tonic-gate 			retval = errno;
4197c478bd9Sstevel@tonic-gate 			close(s);
4207c478bd9Sstevel@tonic-gate 			continue;	/* fail -- try next */
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		break; /* success */
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (ai == NULL) {
4277c478bd9Sstevel@tonic-gate 		switch (which_err) {
428ff67a31bSToomas Soome 			case SOCKET:
429ff67a31bSToomas Soome 				(void) snprintf(Errmsg, ERRMSGSIZ,
4307c478bd9Sstevel@tonic-gate 						gettext("in call to socket"));
4317c478bd9Sstevel@tonic-gate 				break;
432ff67a31bSToomas Soome 			case CONNECT:
433ff67a31bSToomas Soome 				(void) snprintf(Errmsg, ERRMSGSIZ,
4347c478bd9Sstevel@tonic-gate 						gettext("in call to connect"));
4357c478bd9Sstevel@tonic-gate 				break;
4367c478bd9Sstevel@tonic-gate 			default :
4377c478bd9Sstevel@tonic-gate 				retval = -1; /* generic error */
438ff67a31bSToomas Soome 				(void) snprintf(Errmsg, ERRMSGSIZ,
4397c478bd9Sstevel@tonic-gate 					gettext("could not setup network"));
4407c478bd9Sstevel@tonic-gate 				break;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		if (aitop != NULL)
4437c478bd9Sstevel@tonic-gate 			freeaddrinfo(aitop);
4447c478bd9Sstevel@tonic-gate 		return(retval);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 	*fd = s;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Set receiver_addr and sender_addr.
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	if (cvtkaddr((struct sockaddr_storage *)ai->ai_addr, &receiver_addr)
4527c478bd9Sstevel@tonic-gate 			== NULL) {
4537c478bd9Sstevel@tonic-gate 		retval = errno;
4547c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
4557c478bd9Sstevel@tonic-gate 			gettext("while converting socket address"));
4567c478bd9Sstevel@tonic-gate 		if (aitop != NULL)
4577c478bd9Sstevel@tonic-gate 			freeaddrinfo(aitop);
4587c478bd9Sstevel@tonic-gate 		return(retval);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	if (aitop != NULL)
4617c478bd9Sstevel@tonic-gate 		freeaddrinfo(aitop);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	socket_length = sizeof(ss);
4647c478bd9Sstevel@tonic-gate 	if (getsockname(s, (struct sockaddr *)&ss, &socket_length) < 0) {
4657c478bd9Sstevel@tonic-gate 		retval = errno;
4667c478bd9Sstevel@tonic-gate 		close(s);
467ff67a31bSToomas Soome 		(void) snprintf(Errmsg, ERRMSGSIZ,
4687c478bd9Sstevel@tonic-gate 				gettext("in call to getsockname"));
4697c478bd9Sstevel@tonic-gate 		return(retval);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (cvtkaddr(&ss, &sender_addr) == NULL) {
4737c478bd9Sstevel@tonic-gate 		retval = errno;
4747c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
4757c478bd9Sstevel@tonic-gate 			gettext("while converting socket address"));
4767c478bd9Sstevel@tonic-gate 		return(retval);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	return(0);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 
kerberos_authenticate(context,auth_context,fd,me,new_creds)4837c478bd9Sstevel@tonic-gate void kerberos_authenticate(context, auth_context, fd, me, new_creds)
4847c478bd9Sstevel@tonic-gate     krb5_context context;
4857c478bd9Sstevel@tonic-gate     krb5_auth_context *auth_context;
4867c478bd9Sstevel@tonic-gate     int	fd;
4877c478bd9Sstevel@tonic-gate     krb5_principal me;
4887c478bd9Sstevel@tonic-gate     krb5_creds ** new_creds;
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	krb5_error_code	retval;
4917c478bd9Sstevel@tonic-gate 	krb5_error	*error = NULL;
4927c478bd9Sstevel@tonic-gate 	krb5_ap_rep_enc_part	*rep_result;
4937c478bd9Sstevel@tonic-gate 
49456a424ccSmp     retval = krb5_auth_con_init(context, auth_context);
495ff67a31bSToomas Soome     if (retval)
4967c478bd9Sstevel@tonic-gate 	exit(1);
4977c478bd9Sstevel@tonic-gate 
498ff67a31bSToomas Soome     krb5_auth_con_setflags(context, *auth_context,
4997c478bd9Sstevel@tonic-gate 			   KRB5_AUTH_CONTEXT_DO_SEQUENCE);
5007c478bd9Sstevel@tonic-gate 
50156a424ccSmp     retval = krb5_auth_con_setaddrs(context, *auth_context, &sender_addr,
50256a424ccSmp 				    &receiver_addr);
50356a424ccSmp     if (retval) {
5047c478bd9Sstevel@tonic-gate 	com_err(progname, retval, gettext("in krb5_auth_con_setaddrs"));
5057c478bd9Sstevel@tonic-gate 	exit(1);
5067c478bd9Sstevel@tonic-gate     }
5077c478bd9Sstevel@tonic-gate 
508ff67a31bSToomas Soome     retval = krb5_sendauth(context, auth_context, (void *)&fd,
50956a424ccSmp 			   kprop_version, me, creds.server,
51056a424ccSmp 			   AP_OPTS_MUTUAL_REQUIRED, NULL, &creds, NULL,
51156a424ccSmp 			   &error, &rep_result, new_creds);
51256a424ccSmp     if (retval) {
51356a424ccSmp         com_err(progname, retval, gettext("while authenticating to server"));
51456a424ccSmp 	if (error) {
51556a424ccSmp 	    if (error->error == KRB_ERR_GENERIC) {
51656a424ccSmp 	        if (error->text.data)
51756a424ccSmp 		    fprintf(stderr,
51856a424ccSmp 			    gettext("Generic remote error: %s\n"),
51956a424ccSmp 			    error->text.data);
52056a424ccSmp 	    } else if (error->error) {
52156a424ccSmp 	        com_err(progname,
52256a424ccSmp 			(krb5_error_code) error->error + ERROR_TABLE_BASE_krb5,
52356a424ccSmp 		gettext("signalled from server"));
52456a424ccSmp 		if (error->text.data)
52556a424ccSmp 		    fprintf(stderr,
52656a424ccSmp 			    gettext("Error text from server: %s\n"),
52756a424ccSmp 			    error->text.data);
52856a424ccSmp 	    }
52956a424ccSmp 	    krb5_free_error(context, error);
5307c478bd9Sstevel@tonic-gate 	}
53156a424ccSmp 	exit(1);
53256a424ccSmp     }
53356a424ccSmp     krb5_free_ap_rep_enc_part(context, rep_result);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate char * dbpathname;
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate  * Open the Kerberos database dump file.  Takes care of locking it
5397c478bd9Sstevel@tonic-gate  * and making sure that the .ok file is more recent that the database
5407c478bd9Sstevel@tonic-gate  * dump file itself.
5417c478bd9Sstevel@tonic-gate  *
5427c478bd9Sstevel@tonic-gate  * Returns the file descriptor of the database dump file.  Also fills
5437c478bd9Sstevel@tonic-gate  * in the size of the database file.
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate int
open_database(context,data_fn,size)5467c478bd9Sstevel@tonic-gate open_database(context, data_fn, size)
5477c478bd9Sstevel@tonic-gate     krb5_context context;
5487c478bd9Sstevel@tonic-gate     char *data_fn;
5497c478bd9Sstevel@tonic-gate     int	*size;
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	int		fd;
5527c478bd9Sstevel@tonic-gate 	int		err;
553ff67a31bSToomas Soome 	struct stat	stbuf, stbuf_ok;
5547c478bd9Sstevel@tonic-gate 	char		*data_ok_fn;
5557c478bd9Sstevel@tonic-gate 	static char ok[] = ".dump_ok";
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	dbpathname = strdup(data_fn);
5587c478bd9Sstevel@tonic-gate 	if (!dbpathname) {
559ff67a31bSToomas Soome 		com_err(progname, ENOMEM,
560ff67a31bSToomas Soome 		    gettext("allocating database file name '%s'"), data_fn);
561ff67a31bSToomas Soome 		exit(1);
562ff67a31bSToomas Soome 	}
5637c478bd9Sstevel@tonic-gate 	if ((fd = open(dbpathname, O_RDONLY)) < 0) {
5647c478bd9Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to open %s"),
5657c478bd9Sstevel@tonic-gate 			dbpathname);
5667c478bd9Sstevel@tonic-gate 		exit(1);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	err = krb5_lock_file(context, fd,
5707c478bd9Sstevel@tonic-gate 			     KRB5_LOCKMODE_SHARED|KRB5_LOCKMODE_DONTBLOCK);
5717c478bd9Sstevel@tonic-gate 	if (err == EAGAIN || err == EWOULDBLOCK || errno == EACCES) {
5727c478bd9Sstevel@tonic-gate 	    com_err(progname, 0, gettext("database locked"));
5737c478bd9Sstevel@tonic-gate 	    exit(1);
5747c478bd9Sstevel@tonic-gate 	} else if (err) {
5757c478bd9Sstevel@tonic-gate 	    com_err(progname, err, gettext("while trying to lock '%s'"), dbpathname);
5767c478bd9Sstevel@tonic-gate 	    exit(1);
577ff67a31bSToomas Soome 	}
5787c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stbuf)) {
5797c478bd9Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to stat %s"),
5807c478bd9Sstevel@tonic-gate 			data_fn);
5817c478bd9Sstevel@tonic-gate 		exit(1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	if ((data_ok_fn = (char *) malloc(strlen(data_fn)+strlen(ok)+1))
5847c478bd9Sstevel@tonic-gate 	    == NULL) {
5857c478bd9Sstevel@tonic-gate 		com_err(progname, ENOMEM, gettext("while trying to malloc data_ok_fn"));
5867c478bd9Sstevel@tonic-gate 		exit(1);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	strcpy(data_ok_fn, data_fn);
5897c478bd9Sstevel@tonic-gate 	strcat(data_ok_fn, ok);
5907c478bd9Sstevel@tonic-gate 	if (stat(data_ok_fn, &stbuf_ok)) {
5917c478bd9Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to stat %s"),
5927c478bd9Sstevel@tonic-gate 			data_ok_fn);
5937c478bd9Sstevel@tonic-gate 		free(data_ok_fn);
5947c478bd9Sstevel@tonic-gate 		exit(1);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	free(data_ok_fn);
5977c478bd9Sstevel@tonic-gate 	if (stbuf.st_mtime > stbuf_ok.st_mtime) {
5987c478bd9Sstevel@tonic-gate 		com_err(progname, 0, gettext("'%s' more recent than '%s'."),
5997c478bd9Sstevel@tonic-gate 			data_fn, data_ok_fn);
6007c478bd9Sstevel@tonic-gate 		exit(1);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	*size = stbuf.st_size;
6037c478bd9Sstevel@tonic-gate 	return(fd);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate void
close_database(context,fd)6077c478bd9Sstevel@tonic-gate close_database(context, fd)
6087c478bd9Sstevel@tonic-gate     krb5_context context;
6097c478bd9Sstevel@tonic-gate     int fd;
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate     int err;
61256a424ccSmp     err = krb5_lock_file(context, fd, KRB5_LOCKMODE_UNLOCK);
61356a424ccSmp     if (err)
6147c478bd9Sstevel@tonic-gate 	com_err(progname, err, gettext("while unlocking database '%s'"), dbpathname);
6157c478bd9Sstevel@tonic-gate     free(dbpathname);
6167c478bd9Sstevel@tonic-gate     (void)close(fd);
6177c478bd9Sstevel@tonic-gate     return;
6187c478bd9Sstevel@tonic-gate }
619ff67a31bSToomas Soome 
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate  * Now we send over the database.  We use the following protocol:
6227c478bd9Sstevel@tonic-gate  * Send over a KRB_SAFE message with the size.  Then we send over the
6237c478bd9Sstevel@tonic-gate  * database in blocks of KPROP_BLKSIZE, encrypted using KRB_PRIV.
6247c478bd9Sstevel@tonic-gate  * Then we expect to see a KRB_SAFE message with the size sent back.
625ff67a31bSToomas Soome  *
6267c478bd9Sstevel@tonic-gate  * At any point in the protocol, we may send a KRB_ERROR message; this
6277c478bd9Sstevel@tonic-gate  * will abort the entire operation.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate void
xmit_database(context,auth_context,my_creds,fd,database_fd,in_database_size)630ff67a31bSToomas Soome xmit_database(context, auth_context, my_creds, fd, database_fd,
63156a424ccSmp 	      in_database_size)
6327c478bd9Sstevel@tonic-gate     krb5_context context;
6337c478bd9Sstevel@tonic-gate     krb5_auth_context auth_context;
6347c478bd9Sstevel@tonic-gate     krb5_creds *my_creds;
6357c478bd9Sstevel@tonic-gate     int	fd;
6367c478bd9Sstevel@tonic-gate     int	database_fd;
63756a424ccSmp     int	in_database_size;
6387c478bd9Sstevel@tonic-gate {
63956a424ccSmp 	krb5_int32	sent_size, n;
6407c478bd9Sstevel@tonic-gate 	krb5_data	inbuf, outbuf;
6417c478bd9Sstevel@tonic-gate 	char		buf[KPROP_BUFSIZ];
6427c478bd9Sstevel@tonic-gate 	krb5_error_code	retval;
6437c478bd9Sstevel@tonic-gate 	krb5_error	*error;
64456a424ccSmp 	/* These must be 4 bytes */
645ff67a31bSToomas Soome 	krb5_ui_4	database_size = in_database_size;
64656a424ccSmp 	krb5_ui_4	send_size;
64756a424ccSmp 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Send over the size
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	send_size = htonl(database_size);
6527c478bd9Sstevel@tonic-gate 	inbuf.data = (char *) &send_size;
6537c478bd9Sstevel@tonic-gate 	inbuf.length = sizeof(send_size); /* must be 4, really */
6547c478bd9Sstevel@tonic-gate 	/* KPROP_CKSUMTYPE */
655ff67a31bSToomas Soome 	retval = krb5_mk_safe(context, auth_context, &inbuf,
65656a424ccSmp 			      &outbuf, NULL);
65756a424ccSmp 	if (retval) {
6587c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("while encoding database size"));
6597c478bd9Sstevel@tonic-gate 		send_error(context, my_creds, fd, gettext("while encoding database size"), retval);
6607c478bd9Sstevel@tonic-gate 		exit(1);
6617c478bd9Sstevel@tonic-gate 	}
66256a424ccSmp 
66356a424ccSmp 	retval = krb5_write_message(context, (void *) &fd, &outbuf);
66456a424ccSmp 	if (retval) {
6657c478bd9Sstevel@tonic-gate 		krb5_free_data_contents(context, &outbuf);
6667c478bd9Sstevel@tonic-gate 		com_err(progname, retval, gettext("while sending database size"));
6677c478bd9Sstevel@tonic-gate 		exit(1);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	krb5_free_data_contents(context, &outbuf);
67056a424ccSmp 	/*
67156a424ccSmp 	 * Initialize the initial vector.
67256a424ccSmp 	 */
67356a424ccSmp 	retval = krb5_auth_con_initivector(context, auth_context);
67456a424ccSmp 	if (retval) {
675ff67a31bSToomas Soome 	    send_error(context, my_creds, fd,
6767c478bd9Sstevel@tonic-gate 		   gettext("failed while initializing i_vector"), retval);
67756a424ccSmp 	    com_err(progname, retval, gettext("while allocating i_vector"));
67856a424ccSmp 	    exit(1);
67956a424ccSmp 	}
680ff67a31bSToomas Soome 
6817c478bd9Sstevel@tonic-gate 	/*
6827c478bd9Sstevel@tonic-gate 	 * Send over the file, block by block....
6837c478bd9Sstevel@tonic-gate 	 */
6847c478bd9Sstevel@tonic-gate 	inbuf.data = buf;
6857c478bd9Sstevel@tonic-gate 	sent_size = 0;
68656a424ccSmp 	while ((n = read(database_fd, buf, sizeof(buf)))) {
6877c478bd9Sstevel@tonic-gate 		inbuf.length = n;
68856a424ccSmp 		retval = krb5_mk_priv(context, auth_context, &inbuf,
68956a424ccSmp 				      &outbuf, NULL);
69056a424ccSmp 		if (retval) {
6917c478bd9Sstevel@tonic-gate 			snprintf(buf, sizeof (buf),
6927c478bd9Sstevel@tonic-gate 				gettext("while encoding database block starting at %d"),
6937c478bd9Sstevel@tonic-gate 				sent_size);
6947c478bd9Sstevel@tonic-gate 			com_err(progname, retval, buf);
6957c478bd9Sstevel@tonic-gate 			send_error(context, my_creds, fd, buf, retval);
6967c478bd9Sstevel@tonic-gate 			exit(1);
6977c478bd9Sstevel@tonic-gate 		}
69856a424ccSmp 
69956a424ccSmp 		retval = krb5_write_message(context, (void *)&fd,&outbuf);
70056a424ccSmp 		if (retval) {
7017c478bd9Sstevel@tonic-gate 			krb5_free_data_contents(context, &outbuf);
7027c478bd9Sstevel@tonic-gate 			com_err(progname, retval,
7037c478bd9Sstevel@tonic-gate 				gettext("while sending database block starting at %d"),
7047c478bd9Sstevel@tonic-gate 				sent_size);
7057c478bd9Sstevel@tonic-gate 			exit(1);
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		krb5_free_data_contents(context, &outbuf);
7087c478bd9Sstevel@tonic-gate 		sent_size += n;
7097c478bd9Sstevel@tonic-gate 		if (debug)
7107c478bd9Sstevel@tonic-gate 			printf(gettext("%d bytes sent.\n"), sent_size);
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	if (sent_size != database_size) {
7137c478bd9Sstevel@tonic-gate 		com_err(progname, 0, gettext("Premature EOF found for database file!"));
7147c478bd9Sstevel@tonic-gate 		send_error(context, my_creds, fd,gettext("Premature EOF found for database file!"),
7157c478bd9Sstevel@tonic-gate 			   KRB5KRB_ERR_GENERIC);
7167c478bd9Sstevel@tonic-gate 		exit(1);
7177c478bd9Sstevel@tonic-gate 	}
71856a424ccSmp 
7197c478bd9Sstevel@tonic-gate 	/*
7207c478bd9Sstevel@tonic-gate 	 * OK, we've sent the database; now let's wait for a success
7217c478bd9Sstevel@tonic-gate 	 * indication from the remote end.
7227c478bd9Sstevel@tonic-gate 	 */
72356a424ccSmp 	retval = krb5_read_message(context, (void *) &fd, &inbuf);
72456a424ccSmp 	if (retval) {
7257c478bd9Sstevel@tonic-gate 		com_err(progname, retval,
7267c478bd9Sstevel@tonic-gate 			gettext("while reading response from server"));
7277c478bd9Sstevel@tonic-gate 		exit(1);
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	/*
7307c478bd9Sstevel@tonic-gate 	 * If we got an error response back from the server, display
7317c478bd9Sstevel@tonic-gate 	 * the error message
7327c478bd9Sstevel@tonic-gate 	 */
7337c478bd9Sstevel@tonic-gate 	if (krb5_is_krb_error(&inbuf)) {
734ff67a31bSToomas Soome 		retval = krb5_rd_error(context, &inbuf, &error);
73556a424ccSmp 		if (retval) {
7367c478bd9Sstevel@tonic-gate 			com_err(progname, retval,
7377c478bd9Sstevel@tonic-gate 				gettext("while decoding error response from server"));
7387c478bd9Sstevel@tonic-gate 			exit(1);
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 		if (error->error == KRB_ERR_GENERIC) {
7417c478bd9Sstevel@tonic-gate 			if (error->text.data)
7427c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7437c478bd9Sstevel@tonic-gate 				gettext("Generic remote error: %s\n"),
7447c478bd9Sstevel@tonic-gate 					error->text.data);
7457c478bd9Sstevel@tonic-gate 		} else if (error->error) {
746ff67a31bSToomas Soome 			com_err(progname,
747ff67a31bSToomas Soome 				(krb5_error_code) error->error +
74856a424ccSmp 				  ERROR_TABLE_BASE_krb5,
7497c478bd9Sstevel@tonic-gate 				gettext("signalled from server"));
7507c478bd9Sstevel@tonic-gate 			if (error->text.data)
7517c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7527c478bd9Sstevel@tonic-gate 				gettext("Error text from server: %s\n"),
7537c478bd9Sstevel@tonic-gate 					error->text.data);
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		krb5_free_error(context, error);
7567c478bd9Sstevel@tonic-gate 		exit(1);
7577c478bd9Sstevel@tonic-gate 	}
75856a424ccSmp 
75956a424ccSmp 	retval = krb5_rd_safe(context,auth_context,&inbuf,&outbuf,NULL);
76056a424ccSmp 	if (retval) {
7617c478bd9Sstevel@tonic-gate 		com_err(progname, retval,
7627c478bd9Sstevel@tonic-gate 			gettext("while decoding final size packet from server"));
7637c478bd9Sstevel@tonic-gate 		exit(1);
7647c478bd9Sstevel@tonic-gate 	}
76556a424ccSmp 
7667c478bd9Sstevel@tonic-gate 	memcpy((char *)&send_size, outbuf.data, sizeof(send_size));
7677c478bd9Sstevel@tonic-gate 	send_size = ntohl(send_size);
7687c478bd9Sstevel@tonic-gate 	if (send_size != database_size) {
7697c478bd9Sstevel@tonic-gate 		com_err(progname, 0,
7707c478bd9Sstevel@tonic-gate 			gettext("Kpropd sent database size %d, expecting %d"),
7717c478bd9Sstevel@tonic-gate 			send_size, database_size);
7727c478bd9Sstevel@tonic-gate 		exit(1);
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	free(outbuf.data);
7757c478bd9Sstevel@tonic-gate 	free(inbuf.data);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate void
send_error(context,my_creds,fd,err_text,err_code)7797c478bd9Sstevel@tonic-gate send_error(context, my_creds, fd, err_text, err_code)
7807c478bd9Sstevel@tonic-gate     krb5_context context;
7817c478bd9Sstevel@tonic-gate     krb5_creds *my_creds;
7827c478bd9Sstevel@tonic-gate     int	fd;
7837c478bd9Sstevel@tonic-gate     char	*err_text;
7847c478bd9Sstevel@tonic-gate     krb5_error_code	err_code;
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	krb5_error	error;
7877c478bd9Sstevel@tonic-gate 	const char	*text;
7887c478bd9Sstevel@tonic-gate 	krb5_data	outbuf;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	memset((char *)&error, 0, sizeof(error));
7917c478bd9Sstevel@tonic-gate 	krb5_us_timeofday(context, &error.ctime, &error.cusec);
7927c478bd9Sstevel@tonic-gate 	error.server = my_creds->server;
7937c478bd9Sstevel@tonic-gate 	error.client = my_principal;
7947c478bd9Sstevel@tonic-gate 	error.error = err_code - ERROR_TABLE_BASE_krb5;
7957c478bd9Sstevel@tonic-gate 	if (error.error > 127)
7967c478bd9Sstevel@tonic-gate 		error.error = KRB_ERR_GENERIC;
7977c478bd9Sstevel@tonic-gate 	if (err_text)
7987c478bd9Sstevel@tonic-gate 		text = err_text;
7997c478bd9Sstevel@tonic-gate 	else
8007c478bd9Sstevel@tonic-gate 		text = error_message(err_code);
8017c478bd9Sstevel@tonic-gate 	error.text.length = strlen(text) + 1;
80256a424ccSmp 	error.text.data = malloc((unsigned int) error.text.length);
80356a424ccSmp 	if (error.text.data) {
8047c478bd9Sstevel@tonic-gate 		strcpy(error.text.data, text);
8057c478bd9Sstevel@tonic-gate 		if (!krb5_mk_error(context, &error, &outbuf)) {
8067c478bd9Sstevel@tonic-gate 			(void) krb5_write_message(context, (void *)&fd,&outbuf);
8077c478bd9Sstevel@tonic-gate 			krb5_free_data_contents(context, &outbuf);
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 		free(error.text.data);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
update_last_prop_file(hostname,file_name)8137c478bd9Sstevel@tonic-gate void update_last_prop_file(hostname, file_name)
8147c478bd9Sstevel@tonic-gate 	char *hostname;
8157c478bd9Sstevel@tonic-gate 	char *file_name;
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	/* handle slave locking/failure stuff */
8187c478bd9Sstevel@tonic-gate 	char *file_last_prop;
8197c478bd9Sstevel@tonic-gate 	int fd;
8207c478bd9Sstevel@tonic-gate 	static char last_prop[]=".last_prop";
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	if ((file_last_prop = (char *)malloc(strlen(file_name) +
8237c478bd9Sstevel@tonic-gate 					     strlen(hostname) + 1 +
8247c478bd9Sstevel@tonic-gate 					     strlen(last_prop) + 1)) == NULL) {
8257c478bd9Sstevel@tonic-gate 		com_err(progname, ENOMEM,
8267c478bd9Sstevel@tonic-gate 			gettext("while allocating filename for update_last_prop_file"));
8277c478bd9Sstevel@tonic-gate 		return;
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	strcpy(file_last_prop, file_name);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/*
8327c478bd9Sstevel@tonic-gate 	 * If a nondefault file name was specified then we should not add an
8337c478bd9Sstevel@tonic-gate 	 * extraneous host name to the file name given that a file name could
8347c478bd9Sstevel@tonic-gate 	 * have already specified a host name and therefore would be redundant.
8357c478bd9Sstevel@tonic-gate 	 */
8367c478bd9Sstevel@tonic-gate 	if (strcmp(file_name, KPROP_DEFAULT_FILE) == 0) {
83756a424ccSmp 	strcat(file_last_prop, ".");
83856a424ccSmp 	strcat(file_last_prop, hostname);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 	strcat(file_last_prop, last_prop);
8417c478bd9Sstevel@tonic-gate 	if ((fd = THREEPARAMOPEN(file_last_prop, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
8427c478bd9Sstevel@tonic-gate 		com_err(progname, errno,
8437c478bd9Sstevel@tonic-gate 			gettext("while creating 'last_prop' file, '%s'"),
8447c478bd9Sstevel@tonic-gate 			file_last_prop);
8457c478bd9Sstevel@tonic-gate 		free(file_last_prop);
8467c478bd9Sstevel@tonic-gate 		return;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 	write(fd, "", 1);
8497c478bd9Sstevel@tonic-gate 	free(file_last_prop);
8507c478bd9Sstevel@tonic-gate 	close(fd);
8517c478bd9Sstevel@tonic-gate 	return;
8527c478bd9Sstevel@tonic-gate }
853