xref: /onnv-gate/usr/src/cmd/krb5/slave/kprop.c (revision 12624:a1fa1ee4d57c)
10Sstevel@tonic-gate /*
2*12624SPeter.Shoults@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate  */
40Sstevel@tonic-gate 
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate  * slave/kprop.c
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
90Sstevel@tonic-gate  * All Rights Reserved.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * Export of this software from the United States of America may
120Sstevel@tonic-gate  *   require a specific license from the United States Government.
130Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
140Sstevel@tonic-gate  *   export to obtain such a license before exporting.
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
170Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
180Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
190Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
200Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
210Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
220Sstevel@tonic-gate  * to distribution of the software without specific, written prior
230Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
240Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
250Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
260Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
270Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
280Sstevel@tonic-gate  * or implied warranty.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <sys/file.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/time.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/socket.h>
440Sstevel@tonic-gate #include <netinet/in.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <netdb.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <libintl.h>
490Sstevel@tonic-gate #include <locale.h>
500Sstevel@tonic-gate #include <k5-int.h>
510Sstevel@tonic-gate #include "com_err.h"
520Sstevel@tonic-gate #include "kprop.h"
530Sstevel@tonic-gate static char *kprop_version = KPROP_PROT_VERSION;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate char	*progname = 0;
560Sstevel@tonic-gate int     debug = 0;
570Sstevel@tonic-gate char	*srvtab = 0;
580Sstevel@tonic-gate char	*slave_host;
590Sstevel@tonic-gate char	*realm = 0;
600Sstevel@tonic-gate char	*file = KPROP_DEFAULT_FILE;
610Sstevel@tonic-gate short	port = 0;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate krb5_principal	my_principal;		/* The Kerberos principal we'll be */
640Sstevel@tonic-gate 				/* running under, initialized in */
650Sstevel@tonic-gate 				/* get_tickets() */
660Sstevel@tonic-gate krb5_ccache	ccache;		/* Credentials cache which we'll be using */
670Sstevel@tonic-gate krb5_creds	creds;
680Sstevel@tonic-gate krb5_address	sender_addr;
690Sstevel@tonic-gate krb5_address	receiver_addr;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate void	PRS
722881Smp153739 	(int, char **);
730Sstevel@tonic-gate void	get_tickets
742881Smp153739 	(krb5_context);
750Sstevel@tonic-gate static void usage
762881Smp153739 	(void);
770Sstevel@tonic-gate krb5_error_code open_connection
782881Smp153739 	(char *, int *, char *, unsigned int);
790Sstevel@tonic-gate void	kerberos_authenticate
802881Smp153739 	(krb5_context, krb5_auth_context *,
810Sstevel@tonic-gate 		   int, krb5_principal, krb5_creds **);
820Sstevel@tonic-gate int	open_database
832881Smp153739 	(krb5_context, char *, int *);
840Sstevel@tonic-gate void	close_database
852881Smp153739 	(krb5_context, int);
860Sstevel@tonic-gate void	xmit_database
872881Smp153739 	(krb5_context, krb5_auth_context, krb5_creds *,
880Sstevel@tonic-gate 		   int, int, int);
890Sstevel@tonic-gate void	send_error
902881Smp153739 	(krb5_context, krb5_creds *, int, char *, krb5_error_code);
910Sstevel@tonic-gate void	update_last_prop_file
922881Smp153739 	(char *, char *);
930Sstevel@tonic-gate 
usage()940Sstevel@tonic-gate static void usage()
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	fprintf(stderr,
970Sstevel@tonic-gate 		gettext
980Sstevel@tonic-gate 		("\nUsage: %s [-r realm] [-f file] [-d] [-P port] [-s srvtab] slave_host\n\n"),
990Sstevel@tonic-gate 		progname);
1000Sstevel@tonic-gate 	exit(1);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int
main(argc,argv)1040Sstevel@tonic-gate main(argc, argv)
1050Sstevel@tonic-gate 	int	argc;
1060Sstevel@tonic-gate 	char	**argv;
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	int	fd, database_fd, database_size;
1090Sstevel@tonic-gate 	krb5_error_code	retval;
1100Sstevel@tonic-gate 	krb5_context context;
1110Sstevel@tonic-gate 	krb5_creds *my_creds;
1120Sstevel@tonic-gate 	krb5_auth_context auth_context;
1130Sstevel@tonic-gate #define	ERRMSGSIZ	256
1140Sstevel@tonic-gate 	char	Errmsg[ERRMSGSIZ];
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
1190Sstevel@tonic-gate #define	TEXT_DOMAIN	"KPROP_TEST"	/* Use this only if it weren't */
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	retval = krb5_init_context(&context);
1250Sstevel@tonic-gate 	if (retval) {
1260Sstevel@tonic-gate 		com_err(argv[0], retval, gettext("while initializing krb5"));
1270Sstevel@tonic-gate 		exit(1);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	PRS(argc, argv);
1300Sstevel@tonic-gate 	get_tickets(context);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	database_fd = open_database(context, file, &database_size);
1332881Smp153739 	retval = open_connection(slave_host, &fd, Errmsg, sizeof(Errmsg));
1342881Smp153739 	if (retval) {
1350Sstevel@tonic-gate 		com_err(progname, retval, gettext("%s while opening connection to %s"),
1360Sstevel@tonic-gate 			Errmsg, slave_host);
1370Sstevel@tonic-gate 		exit(1);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	if (fd < 0) {
1400Sstevel@tonic-gate 		fprintf(stderr,
1410Sstevel@tonic-gate 			gettext("%s: %s while opening connection to %s\n"),
1420Sstevel@tonic-gate 			progname, Errmsg, slave_host);
1430Sstevel@tonic-gate 		exit(1);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	kerberos_authenticate(context, &auth_context, fd, my_principal,
1460Sstevel@tonic-gate 			      &my_creds);
1470Sstevel@tonic-gate 	xmit_database(context, auth_context, my_creds, fd, database_fd,
1480Sstevel@tonic-gate 		      database_size);
1490Sstevel@tonic-gate 	update_last_prop_file(slave_host, file);
1500Sstevel@tonic-gate 	printf(gettext("Database propagation to %s: SUCCEEDED\n"), slave_host);
1510Sstevel@tonic-gate 	krb5_free_cred_contents(context, my_creds);
1520Sstevel@tonic-gate 	close_database(context, database_fd);
1530Sstevel@tonic-gate 	exit(0);
1540Sstevel@tonic-gate }
1552881Smp153739 
PRS(argc,argv)1560Sstevel@tonic-gate void PRS(argc, argv)
1570Sstevel@tonic-gate 	int	argc;
1580Sstevel@tonic-gate 	char	**argv;
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int c;
1610Sstevel@tonic-gate 	register char	*word, ch;
1620Sstevel@tonic-gate 	extern int optind;
1630Sstevel@tonic-gate 	extern char *optarg;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	progname = argv[0];
1660Sstevel@tonic-gate 	while ((c= getopt(argc, argv, "r:f:dP:s:h:")) != EOF) {
1670Sstevel@tonic-gate 		switch (c) {
1680Sstevel@tonic-gate 				case 'r':
1690Sstevel@tonic-gate 			realm = optarg;
1700Sstevel@tonic-gate 					if (!realm)
1710Sstevel@tonic-gate 						usage();
1720Sstevel@tonic-gate 					break;
1730Sstevel@tonic-gate 				case 'f':
1740Sstevel@tonic-gate 			file = optarg;
1750Sstevel@tonic-gate 					if (!file)
1760Sstevel@tonic-gate 						usage();
1770Sstevel@tonic-gate 					break;
1780Sstevel@tonic-gate 				case 'd':
1790Sstevel@tonic-gate 					debug++;
1800Sstevel@tonic-gate 					break;
1810Sstevel@tonic-gate 				case 'P':
18296Ssemery 			port = atoi(optarg);
1830Sstevel@tonic-gate 					if (!port)
1840Sstevel@tonic-gate 						usage();
1850Sstevel@tonic-gate 					break;
1860Sstevel@tonic-gate 				case 's':
1870Sstevel@tonic-gate 			srvtab = optarg;
1880Sstevel@tonic-gate 					if (!srvtab)
1890Sstevel@tonic-gate 						usage();
1900Sstevel@tonic-gate 					break;
1910Sstevel@tonic-gate 		case '?':
1920Sstevel@tonic-gate 				default:
1930Sstevel@tonic-gate 			printf (gettext("Error \n"));
1940Sstevel@tonic-gate 					usage();
1950Sstevel@tonic-gate 				}
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 	argc -= optind;
1980Sstevel@tonic-gate 	argv = &argv[optind];
1990Sstevel@tonic-gate 	if (*argv)
2000Sstevel@tonic-gate 		slave_host = *argv;
2010Sstevel@tonic-gate 			else
2020Sstevel@tonic-gate 		usage();
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
get_tickets(context)2050Sstevel@tonic-gate void get_tickets(context)
2060Sstevel@tonic-gate     krb5_context context;
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	char   buf[BUFSIZ];
2090Sstevel@tonic-gate 	krb5_error_code retval;
2100Sstevel@tonic-gate 	static char tkstring[] = "/tmp/kproptktXXXXXX";
2110Sstevel@tonic-gate 	krb5_keytab keytab = NULL;
2120Sstevel@tonic-gate 	krb5_get_init_creds_opt opt;
2130Sstevel@tonic-gate 	char *svcname = NULL;
2140Sstevel@tonic-gate 	char *def_realm = NULL;
2150Sstevel@tonic-gate 	char *master_host = NULL;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Figure out what tickets we'll be using to send stuff
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	if (realm) {
2220Sstevel@tonic-gate 	    if ((def_realm = strdup(realm)) == NULL) {
2230Sstevel@tonic-gate 	      com_err(progname, ENOMEM,
2240Sstevel@tonic-gate 		      gettext("while allocating default realm name '%s'"),
2250Sstevel@tonic-gate 		      realm);
2260Sstevel@tonic-gate 	      exit(1);
2270Sstevel@tonic-gate 	    }
2280Sstevel@tonic-gate 	} else {
2290Sstevel@tonic-gate 	    retval = krb5_get_default_realm(context, &def_realm);
2300Sstevel@tonic-gate 	    if (retval) {
2310Sstevel@tonic-gate 	        com_err(progname, retval,
2320Sstevel@tonic-gate 			gettext("while getting default realm"));
2330Sstevel@tonic-gate 	        exit(1);
2340Sstevel@tonic-gate 	    }
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * Always pick up the master hostname from krb5.conf, as
2390Sstevel@tonic-gate 	 * opposed to picking up the localhost, so we do not get bit
2400Sstevel@tonic-gate 	 * if the master KDC is HA and hence points to a logicalhost.
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	retval = kadm5_get_master(context, def_realm, &master_host);
2430Sstevel@tonic-gate 	if (retval) {
2440Sstevel@tonic-gate 	    free(def_realm);
2450Sstevel@tonic-gate 	    com_err(progname, retval,
2460Sstevel@tonic-gate 		gettext("while getting admin server fqdn"));
2470Sstevel@tonic-gate 	    exit(1);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	retval = krb5_sname_to_principal(context, master_host, NULL,
2510Sstevel@tonic-gate 					 KRB5_NT_SRV_HST, &my_principal);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	free(def_realm);
2540Sstevel@tonic-gate 	free(master_host);
2550Sstevel@tonic-gate 	if (retval) {
2560Sstevel@tonic-gate 	    com_err(progname, errno, gettext("while setting client principal name"));
2570Sstevel@tonic-gate 	    exit(1);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	if (realm) {
2602881Smp153739 	    retval = krb5_set_principal_realm(context, my_principal, realm);
2612881Smp153739 	    if (retval) {
2622881Smp153739 	        com_err(progname, errno,
2632881Smp153739 			 gettext("while setting client principal realm"));
2642881Smp153739 		exit(1);
2652881Smp153739 	    }
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate #if 0
2680Sstevel@tonic-gate 	krb5_princ_type(context, my_principal) = KRB5_NT_PRINCIPAL;
2690Sstevel@tonic-gate #endif
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/*
2720Sstevel@tonic-gate 	 * Initialize cache file which we're going to be using
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	(void) mktemp(tkstring);
275*12624SPeter.Shoults@Sun.COM 	snprintf(buf, sizeof (buf), "FILE:%s", tkstring);
2762881Smp153739 
2772881Smp153739 	retval = krb5_cc_resolve(context, buf, &ccache);
2782881Smp153739 	if (retval) {
2790Sstevel@tonic-gate 		com_err(progname, retval, gettext("while opening credential cache %s"),
2800Sstevel@tonic-gate 			buf);
2810Sstevel@tonic-gate 		exit(1);
2820Sstevel@tonic-gate 	}
2832881Smp153739 
2842881Smp153739 	retval = krb5_cc_initialize(context, ccache, my_principal);
2852881Smp153739 	if (retval) {
2860Sstevel@tonic-gate 		com_err (progname, retval, gettext("when initializing cache %s"),
2870Sstevel@tonic-gate 			 buf);
2880Sstevel@tonic-gate 		exit(1);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/*
2920Sstevel@tonic-gate 	 * Get the tickets we'll need.
2930Sstevel@tonic-gate 	 *
2940Sstevel@tonic-gate 	 * Construct the principal name for the slave host.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	memset((char *)&creds, 0, sizeof(creds));
2970Sstevel@tonic-gate 	retval = krb5_sname_to_principal(context,
2980Sstevel@tonic-gate 					 slave_host, KPROP_SERVICE_NAME,
2990Sstevel@tonic-gate 					 KRB5_NT_SRV_HST, &creds.server);
3000Sstevel@tonic-gate 	if (retval) {
3010Sstevel@tonic-gate 	    com_err(progname, errno, gettext("while setting server principal name"));
3020Sstevel@tonic-gate 	    (void) krb5_cc_destroy(context, ccache);
3030Sstevel@tonic-gate 	    exit(1);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	if (realm) {
3062881Smp153739 	    retval = krb5_set_principal_realm(context, creds.server, realm);
3072881Smp153739 	    if (retval) {
3082881Smp153739 	        com_err(progname, errno,
3092881Smp153739 			gettext("while setting server principal realm"));
3102881Smp153739 		exit(1);
3112881Smp153739 	    }
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * Now fill in the client....
3160Sstevel@tonic-gate 	 */
3172881Smp153739 	retval = krb5_copy_principal(context, my_principal, &creds.client);
3182881Smp153739 	if (retval) {
3190Sstevel@tonic-gate 		com_err(progname, retval, gettext("While copying client principal"));
3200Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
3210Sstevel@tonic-gate 		exit(1);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	if (srvtab) {
3242881Smp153739 	        retval = krb5_kt_resolve(context, srvtab, &keytab);
3252881Smp153739 		if (retval) {
3260Sstevel@tonic-gate 			com_err(progname, retval, gettext("while resolving keytab"));
3270Sstevel@tonic-gate 			(void) krb5_cc_destroy(context, ccache);
3280Sstevel@tonic-gate 			exit(1);
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 	(void) memset(&opt, 0, sizeof (opt));
3320Sstevel@tonic-gate 	krb5_get_init_creds_opt_init(&opt);
3330Sstevel@tonic-gate 	retval = krb5_unparse_name(context,  creds.server, &svcname);
3340Sstevel@tonic-gate 	if (retval) {
3350Sstevel@tonic-gate 		com_err(progname, errno, gettext("while parsing svc principal name"));
3360Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
3370Sstevel@tonic-gate 		exit (1);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	retval = krb5_get_init_creds_keytab(context, &creds, creds.client,
3400Sstevel@tonic-gate 				keytab,  0, svcname, &opt);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (svcname)
3430Sstevel@tonic-gate 		free(svcname);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (retval) {
3460Sstevel@tonic-gate 		com_err(progname, retval, gettext("while getting initial ticket\n"));
3470Sstevel@tonic-gate 		(void) krb5_cc_destroy(context, ccache);
3480Sstevel@tonic-gate 		exit(1);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (keytab)
3520Sstevel@tonic-gate 	    (void) krb5_kt_close(context, keytab);
3532881Smp153739 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Now destroy the cache right away --- the credentials we
3560Sstevel@tonic-gate 	 * need will be in my_creds.
3570Sstevel@tonic-gate 	 */
3582881Smp153739 	retval = krb5_cc_destroy(context, ccache);
3592881Smp153739 	if (retval) {
3600Sstevel@tonic-gate 		com_err(progname, retval, gettext("while destroying ticket cache"));
3610Sstevel@tonic-gate 		exit(1);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
365781Sgtb /* SUNW14resync - SOCKET is defed in 1.4 in port-sockets.h */
366781Sgtb #ifdef SOCKET
367781Sgtb #undef SOCKET
368781Sgtb #endif
369781Sgtb 
3700Sstevel@tonic-gate krb5_error_code
open_connection(host,fd,Errmsg,ErrmsgSz)3710Sstevel@tonic-gate open_connection(host, fd, Errmsg, ErrmsgSz)
3722881Smp153739 	char		*host;
3732881Smp153739 	int		*fd;
3742881Smp153739 	char		*Errmsg;
3752881Smp153739 	unsigned int	 ErrmsgSz;
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	int	s;
3780Sstevel@tonic-gate 	krb5_error_code	retval;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	int	socket_length;
3810Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
3820Sstevel@tonic-gate 	struct sockaddr_storage	  ss;
3830Sstevel@tonic-gate 	char serv_or_port[NI_MAXSERV];
3840Sstevel@tonic-gate 	enum err_types {SOCKET, CONNECT};
3850Sstevel@tonic-gate 	int which_err;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
3880Sstevel@tonic-gate 	hints.ai_family = AF_UNSPEC;    /* go for either IPv4 or v6 */
3890Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (port != 0)
39296Ssemery 		(void) snprintf(serv_or_port, sizeof(serv_or_port), ("%hu"),
3930Sstevel@tonic-gate 				port);
3940Sstevel@tonic-gate 	else
3950Sstevel@tonic-gate 		strncpy(serv_or_port, KPROP_SERVICE, sizeof(serv_or_port));
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (getaddrinfo(host, serv_or_port, &hints, &aitop) != 0) {
3980Sstevel@tonic-gate 		(void) snprintf(Errmsg, ERRMSGSIZ, gettext("%s: unknown host"),
3990Sstevel@tonic-gate 				host);
4000Sstevel@tonic-gate 		*fd = -1;
4010Sstevel@tonic-gate 		return(0);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
4050Sstevel@tonic-gate 		if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
4060Sstevel@tonic-gate 			continue;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		s = socket(ai->ai_family, SOCK_STREAM, 0);
4090Sstevel@tonic-gate 		if (s < 0) {
4100Sstevel@tonic-gate 			which_err = SOCKET;
4110Sstevel@tonic-gate 			retval = errno;
4120Sstevel@tonic-gate 			continue;
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
4160Sstevel@tonic-gate 		    errno != EINPROGRESS) {
4170Sstevel@tonic-gate 			which_err = CONNECT;
4180Sstevel@tonic-gate 			retval = errno;
4190Sstevel@tonic-gate 			close(s);
4200Sstevel@tonic-gate 			continue;	/* fail -- try next */
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		break; /* success */
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (ai == NULL) {
4270Sstevel@tonic-gate 		switch (which_err) {
4280Sstevel@tonic-gate 			case SOCKET:
4290Sstevel@tonic-gate 				(void) snprintf(Errmsg, ERRMSGSIZ,
4300Sstevel@tonic-gate 						gettext("in call to socket"));
4310Sstevel@tonic-gate 				break;
4320Sstevel@tonic-gate 			case CONNECT:
4330Sstevel@tonic-gate 				(void) snprintf(Errmsg, ERRMSGSIZ,
4340Sstevel@tonic-gate 						gettext("in call to connect"));
4350Sstevel@tonic-gate 				break;
4360Sstevel@tonic-gate 			default :
4370Sstevel@tonic-gate 				retval = -1; /* generic error */
4380Sstevel@tonic-gate 				(void) snprintf(Errmsg, ERRMSGSIZ,
4390Sstevel@tonic-gate 					gettext("could not setup network"));
4400Sstevel@tonic-gate 				break;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 		if (aitop != NULL)
4430Sstevel@tonic-gate 			freeaddrinfo(aitop);
4440Sstevel@tonic-gate 		return(retval);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	*fd = s;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * Set receiver_addr and sender_addr.
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	if (cvtkaddr((struct sockaddr_storage *)ai->ai_addr, &receiver_addr)
4520Sstevel@tonic-gate 			== NULL) {
4530Sstevel@tonic-gate 		retval = errno;
4540Sstevel@tonic-gate 		com_err(progname, errno,
4550Sstevel@tonic-gate 			gettext("while converting socket address"));
4560Sstevel@tonic-gate 		if (aitop != NULL)
4570Sstevel@tonic-gate 			freeaddrinfo(aitop);
4580Sstevel@tonic-gate 		return(retval);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 	if (aitop != NULL)
4610Sstevel@tonic-gate 		freeaddrinfo(aitop);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	socket_length = sizeof(ss);
4640Sstevel@tonic-gate 	if (getsockname(s, (struct sockaddr *)&ss, &socket_length) < 0) {
4650Sstevel@tonic-gate 		retval = errno;
4660Sstevel@tonic-gate 		close(s);
4670Sstevel@tonic-gate 		(void) snprintf(Errmsg, ERRMSGSIZ,
4680Sstevel@tonic-gate 				gettext("in call to getsockname"));
4690Sstevel@tonic-gate 		return(retval);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (cvtkaddr(&ss, &sender_addr) == NULL) {
4730Sstevel@tonic-gate 		retval = errno;
4740Sstevel@tonic-gate 		com_err(progname, errno,
4750Sstevel@tonic-gate 			gettext("while converting socket address"));
4760Sstevel@tonic-gate 		return(retval);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	return(0);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 
kerberos_authenticate(context,auth_context,fd,me,new_creds)4830Sstevel@tonic-gate void kerberos_authenticate(context, auth_context, fd, me, new_creds)
4840Sstevel@tonic-gate     krb5_context context;
4850Sstevel@tonic-gate     krb5_auth_context *auth_context;
4860Sstevel@tonic-gate     int	fd;
4870Sstevel@tonic-gate     krb5_principal me;
4880Sstevel@tonic-gate     krb5_creds ** new_creds;
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	krb5_error_code	retval;
4910Sstevel@tonic-gate 	krb5_error	*error = NULL;
4920Sstevel@tonic-gate 	krb5_ap_rep_enc_part	*rep_result;
4930Sstevel@tonic-gate 
4942881Smp153739     retval = krb5_auth_con_init(context, auth_context);
4952881Smp153739     if (retval)
4960Sstevel@tonic-gate 	exit(1);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate     krb5_auth_con_setflags(context, *auth_context,
4990Sstevel@tonic-gate 			   KRB5_AUTH_CONTEXT_DO_SEQUENCE);
5000Sstevel@tonic-gate 
5012881Smp153739     retval = krb5_auth_con_setaddrs(context, *auth_context, &sender_addr,
5022881Smp153739 				    &receiver_addr);
5032881Smp153739     if (retval) {
5040Sstevel@tonic-gate 	com_err(progname, retval, gettext("in krb5_auth_con_setaddrs"));
5050Sstevel@tonic-gate 	exit(1);
5060Sstevel@tonic-gate     }
5070Sstevel@tonic-gate 
5082881Smp153739     retval = krb5_sendauth(context, auth_context, (void *)&fd,
5092881Smp153739 			   kprop_version, me, creds.server,
5102881Smp153739 			   AP_OPTS_MUTUAL_REQUIRED, NULL, &creds, NULL,
5112881Smp153739 			   &error, &rep_result, new_creds);
5122881Smp153739     if (retval) {
5132881Smp153739         com_err(progname, retval, gettext("while authenticating to server"));
5142881Smp153739 	if (error) {
5152881Smp153739 	    if (error->error == KRB_ERR_GENERIC) {
5162881Smp153739 	        if (error->text.data)
5172881Smp153739 		    fprintf(stderr,
5182881Smp153739 			    gettext("Generic remote error: %s\n"),
5192881Smp153739 			    error->text.data);
5202881Smp153739 	    } else if (error->error) {
5212881Smp153739 	        com_err(progname,
5222881Smp153739 			(krb5_error_code) error->error + ERROR_TABLE_BASE_krb5,
5232881Smp153739 		gettext("signalled from server"));
5242881Smp153739 		if (error->text.data)
5252881Smp153739 		    fprintf(stderr,
5262881Smp153739 			    gettext("Error text from server: %s\n"),
5272881Smp153739 			    error->text.data);
5282881Smp153739 	    }
5292881Smp153739 	    krb5_free_error(context, error);
5300Sstevel@tonic-gate 	}
5312881Smp153739 	exit(1);
5322881Smp153739     }
5332881Smp153739     krb5_free_ap_rep_enc_part(context, rep_result);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate char * dbpathname;
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * Open the Kerberos database dump file.  Takes care of locking it
5390Sstevel@tonic-gate  * and making sure that the .ok file is more recent that the database
5400Sstevel@tonic-gate  * dump file itself.
5410Sstevel@tonic-gate  *
5420Sstevel@tonic-gate  * Returns the file descriptor of the database dump file.  Also fills
5430Sstevel@tonic-gate  * in the size of the database file.
5440Sstevel@tonic-gate  */
5450Sstevel@tonic-gate int
open_database(context,data_fn,size)5460Sstevel@tonic-gate open_database(context, data_fn, size)
5470Sstevel@tonic-gate     krb5_context context;
5480Sstevel@tonic-gate     char *data_fn;
5490Sstevel@tonic-gate     int	*size;
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	int		fd;
5520Sstevel@tonic-gate 	int		err;
5530Sstevel@tonic-gate 	struct stat 	stbuf, stbuf_ok;
5540Sstevel@tonic-gate 	char		*data_ok_fn;
5550Sstevel@tonic-gate 	static char ok[] = ".dump_ok";
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	dbpathname = strdup(data_fn);
5580Sstevel@tonic-gate 	if (!dbpathname) {
5590Sstevel@tonic-gate  	    com_err(progname, ENOMEM, gettext("allocating database file name '%s'"),
5600Sstevel@tonic-gate  		    data_fn);
5610Sstevel@tonic-gate  	    exit(1);
5620Sstevel@tonic-gate  	}
5630Sstevel@tonic-gate 	if ((fd = open(dbpathname, O_RDONLY)) < 0) {
5640Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to open %s"),
5650Sstevel@tonic-gate 			dbpathname);
5660Sstevel@tonic-gate 		exit(1);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	err = krb5_lock_file(context, fd,
5700Sstevel@tonic-gate 			     KRB5_LOCKMODE_SHARED|KRB5_LOCKMODE_DONTBLOCK);
5710Sstevel@tonic-gate 	if (err == EAGAIN || err == EWOULDBLOCK || errno == EACCES) {
5720Sstevel@tonic-gate 	    com_err(progname, 0, gettext("database locked"));
5730Sstevel@tonic-gate 	    exit(1);
5740Sstevel@tonic-gate 	} else if (err) {
5750Sstevel@tonic-gate 	    com_err(progname, err, gettext("while trying to lock '%s'"), dbpathname);
5760Sstevel@tonic-gate 	    exit(1);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 	if (fstat(fd, &stbuf)) {
5790Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to stat %s"),
5800Sstevel@tonic-gate 			data_fn);
5810Sstevel@tonic-gate 		exit(1);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	if ((data_ok_fn = (char *) malloc(strlen(data_fn)+strlen(ok)+1))
5840Sstevel@tonic-gate 	    == NULL) {
5850Sstevel@tonic-gate 		com_err(progname, ENOMEM, gettext("while trying to malloc data_ok_fn"));
5860Sstevel@tonic-gate 		exit(1);
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	strcpy(data_ok_fn, data_fn);
5890Sstevel@tonic-gate 	strcat(data_ok_fn, ok);
5900Sstevel@tonic-gate 	if (stat(data_ok_fn, &stbuf_ok)) {
5910Sstevel@tonic-gate 		com_err(progname, errno, gettext("while trying to stat %s"),
5920Sstevel@tonic-gate 			data_ok_fn);
5930Sstevel@tonic-gate 		free(data_ok_fn);
5940Sstevel@tonic-gate 		exit(1);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	free(data_ok_fn);
5970Sstevel@tonic-gate 	if (stbuf.st_mtime > stbuf_ok.st_mtime) {
5980Sstevel@tonic-gate 		com_err(progname, 0, gettext("'%s' more recent than '%s'."),
5990Sstevel@tonic-gate 			data_fn, data_ok_fn);
6000Sstevel@tonic-gate 		exit(1);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 	*size = stbuf.st_size;
6030Sstevel@tonic-gate 	return(fd);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate void
close_database(context,fd)6070Sstevel@tonic-gate close_database(context, fd)
6080Sstevel@tonic-gate     krb5_context context;
6090Sstevel@tonic-gate     int fd;
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate     int err;
6122881Smp153739     err = krb5_lock_file(context, fd, KRB5_LOCKMODE_UNLOCK);
6132881Smp153739     if (err)
6140Sstevel@tonic-gate 	com_err(progname, err, gettext("while unlocking database '%s'"), dbpathname);
6150Sstevel@tonic-gate     free(dbpathname);
6160Sstevel@tonic-gate     (void)close(fd);
6170Sstevel@tonic-gate     return;
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate  * Now we send over the database.  We use the following protocol:
6220Sstevel@tonic-gate  * Send over a KRB_SAFE message with the size.  Then we send over the
6230Sstevel@tonic-gate  * database in blocks of KPROP_BLKSIZE, encrypted using KRB_PRIV.
6240Sstevel@tonic-gate  * Then we expect to see a KRB_SAFE message with the size sent back.
6250Sstevel@tonic-gate  *
6260Sstevel@tonic-gate  * At any point in the protocol, we may send a KRB_ERROR message; this
6270Sstevel@tonic-gate  * will abort the entire operation.
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate void
xmit_database(context,auth_context,my_creds,fd,database_fd,in_database_size)6302881Smp153739 xmit_database(context, auth_context, my_creds, fd, database_fd,
6312881Smp153739 	      in_database_size)
6320Sstevel@tonic-gate     krb5_context context;
6330Sstevel@tonic-gate     krb5_auth_context auth_context;
6340Sstevel@tonic-gate     krb5_creds *my_creds;
6350Sstevel@tonic-gate     int	fd;
6360Sstevel@tonic-gate     int	database_fd;
6372881Smp153739     int	in_database_size;
6380Sstevel@tonic-gate {
6392881Smp153739 	krb5_int32	sent_size, n;
6400Sstevel@tonic-gate 	krb5_data	inbuf, outbuf;
6410Sstevel@tonic-gate 	char		buf[KPROP_BUFSIZ];
6420Sstevel@tonic-gate 	krb5_error_code	retval;
6430Sstevel@tonic-gate 	krb5_error	*error;
6442881Smp153739 	/* These must be 4 bytes */
6452881Smp153739 	krb5_ui_4	database_size = in_database_size;
6462881Smp153739 	krb5_ui_4	send_size;
6472881Smp153739 
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * Send over the size
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	send_size = htonl(database_size);
6520Sstevel@tonic-gate 	inbuf.data = (char *) &send_size;
6530Sstevel@tonic-gate 	inbuf.length = sizeof(send_size); /* must be 4, really */
6540Sstevel@tonic-gate 	/* KPROP_CKSUMTYPE */
6552881Smp153739 	retval = krb5_mk_safe(context, auth_context, &inbuf,
6562881Smp153739 			      &outbuf, NULL);
6572881Smp153739 	if (retval) {
6580Sstevel@tonic-gate 		com_err(progname, retval, gettext("while encoding database size"));
6590Sstevel@tonic-gate 		send_error(context, my_creds, fd, gettext("while encoding database size"), retval);
6600Sstevel@tonic-gate 		exit(1);
6610Sstevel@tonic-gate 	}
6622881Smp153739 
6632881Smp153739 	retval = krb5_write_message(context, (void *) &fd, &outbuf);
6642881Smp153739 	if (retval) {
6650Sstevel@tonic-gate 		krb5_free_data_contents(context, &outbuf);
6660Sstevel@tonic-gate 		com_err(progname, retval, gettext("while sending database size"));
6670Sstevel@tonic-gate 		exit(1);
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 	krb5_free_data_contents(context, &outbuf);
6702881Smp153739 	/*
6712881Smp153739 	 * Initialize the initial vector.
6722881Smp153739 	 */
6732881Smp153739 	retval = krb5_auth_con_initivector(context, auth_context);
6742881Smp153739 	if (retval) {
6752881Smp153739 	    send_error(context, my_creds, fd,
6760Sstevel@tonic-gate 		   gettext("failed while initializing i_vector"), retval);
6772881Smp153739 	    com_err(progname, retval, gettext("while allocating i_vector"));
6782881Smp153739 	    exit(1);
6792881Smp153739 	}
6802881Smp153739 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * Send over the file, block by block....
6830Sstevel@tonic-gate 	 */
6840Sstevel@tonic-gate 	inbuf.data = buf;
6850Sstevel@tonic-gate 	sent_size = 0;
6862881Smp153739 	while ((n = read(database_fd, buf, sizeof(buf)))) {
6870Sstevel@tonic-gate 		inbuf.length = n;
6882881Smp153739 		retval = krb5_mk_priv(context, auth_context, &inbuf,
6892881Smp153739 				      &outbuf, NULL);
6902881Smp153739 		if (retval) {
6910Sstevel@tonic-gate 			snprintf(buf, sizeof (buf),
6920Sstevel@tonic-gate 				gettext("while encoding database block starting at %d"),
6930Sstevel@tonic-gate 				sent_size);
6940Sstevel@tonic-gate 			com_err(progname, retval, buf);
6950Sstevel@tonic-gate 			send_error(context, my_creds, fd, buf, retval);
6960Sstevel@tonic-gate 			exit(1);
6970Sstevel@tonic-gate 		}
6982881Smp153739 
6992881Smp153739 		retval = krb5_write_message(context, (void *)&fd,&outbuf);
7002881Smp153739 		if (retval) {
7010Sstevel@tonic-gate 			krb5_free_data_contents(context, &outbuf);
7020Sstevel@tonic-gate 			com_err(progname, retval,
7030Sstevel@tonic-gate 				gettext("while sending database block starting at %d"),
7040Sstevel@tonic-gate 				sent_size);
7050Sstevel@tonic-gate 			exit(1);
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 		krb5_free_data_contents(context, &outbuf);
7080Sstevel@tonic-gate 		sent_size += n;
7090Sstevel@tonic-gate 		if (debug)
7100Sstevel@tonic-gate 			printf(gettext("%d bytes sent.\n"), sent_size);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	if (sent_size != database_size) {
7130Sstevel@tonic-gate 		com_err(progname, 0, gettext("Premature EOF found for database file!"));
7140Sstevel@tonic-gate 		send_error(context, my_creds, fd,gettext("Premature EOF found for database file!"),
7150Sstevel@tonic-gate 			   KRB5KRB_ERR_GENERIC);
7160Sstevel@tonic-gate 		exit(1);
7170Sstevel@tonic-gate 	}
7182881Smp153739 
7190Sstevel@tonic-gate 	/*
7200Sstevel@tonic-gate 	 * OK, we've sent the database; now let's wait for a success
7210Sstevel@tonic-gate 	 * indication from the remote end.
7220Sstevel@tonic-gate 	 */
7232881Smp153739 	retval = krb5_read_message(context, (void *) &fd, &inbuf);
7242881Smp153739 	if (retval) {
7250Sstevel@tonic-gate 		com_err(progname, retval,
7260Sstevel@tonic-gate 			gettext("while reading response from server"));
7270Sstevel@tonic-gate 		exit(1);
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	/*
7300Sstevel@tonic-gate 	 * If we got an error response back from the server, display
7310Sstevel@tonic-gate 	 * the error message
7320Sstevel@tonic-gate 	 */
7330Sstevel@tonic-gate 	if (krb5_is_krb_error(&inbuf)) {
7342881Smp153739  	        retval = krb5_rd_error(context, &inbuf, &error);
7352881Smp153739 		if (retval) {
7360Sstevel@tonic-gate 			com_err(progname, retval,
7370Sstevel@tonic-gate 				gettext("while decoding error response from server"));
7380Sstevel@tonic-gate 			exit(1);
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 		if (error->error == KRB_ERR_GENERIC) {
7410Sstevel@tonic-gate 			if (error->text.data)
7420Sstevel@tonic-gate 				fprintf(stderr,
7430Sstevel@tonic-gate 				gettext("Generic remote error: %s\n"),
7440Sstevel@tonic-gate 					error->text.data);
7450Sstevel@tonic-gate 		} else if (error->error) {
7462881Smp153739 			com_err(progname,
7472881Smp153739 				(krb5_error_code) error->error +
7482881Smp153739 				  ERROR_TABLE_BASE_krb5,
7490Sstevel@tonic-gate 				gettext("signalled from server"));
7500Sstevel@tonic-gate 			if (error->text.data)
7510Sstevel@tonic-gate 				fprintf(stderr,
7520Sstevel@tonic-gate 				gettext("Error text from server: %s\n"),
7530Sstevel@tonic-gate 					error->text.data);
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 		krb5_free_error(context, error);
7560Sstevel@tonic-gate 		exit(1);
7570Sstevel@tonic-gate 	}
7582881Smp153739 
7592881Smp153739 	retval = krb5_rd_safe(context,auth_context,&inbuf,&outbuf,NULL);
7602881Smp153739 	if (retval) {
7610Sstevel@tonic-gate 		com_err(progname, retval,
7620Sstevel@tonic-gate 			gettext("while decoding final size packet from server"));
7630Sstevel@tonic-gate 		exit(1);
7640Sstevel@tonic-gate 	}
7652881Smp153739 
7660Sstevel@tonic-gate 	memcpy((char *)&send_size, outbuf.data, sizeof(send_size));
7670Sstevel@tonic-gate 	send_size = ntohl(send_size);
7680Sstevel@tonic-gate 	if (send_size != database_size) {
7690Sstevel@tonic-gate 		com_err(progname, 0,
7700Sstevel@tonic-gate 			gettext("Kpropd sent database size %d, expecting %d"),
7710Sstevel@tonic-gate 			send_size, database_size);
7720Sstevel@tonic-gate 		exit(1);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	free(outbuf.data);
7750Sstevel@tonic-gate 	free(inbuf.data);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate void
send_error(context,my_creds,fd,err_text,err_code)7790Sstevel@tonic-gate send_error(context, my_creds, fd, err_text, err_code)
7800Sstevel@tonic-gate     krb5_context context;
7810Sstevel@tonic-gate     krb5_creds *my_creds;
7820Sstevel@tonic-gate     int	fd;
7830Sstevel@tonic-gate     char	*err_text;
7840Sstevel@tonic-gate     krb5_error_code	err_code;
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	krb5_error	error;
7870Sstevel@tonic-gate 	const char	*text;
7880Sstevel@tonic-gate 	krb5_data	outbuf;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	memset((char *)&error, 0, sizeof(error));
7910Sstevel@tonic-gate 	krb5_us_timeofday(context, &error.ctime, &error.cusec);
7920Sstevel@tonic-gate 	error.server = my_creds->server;
7930Sstevel@tonic-gate 	error.client = my_principal;
7940Sstevel@tonic-gate 	error.error = err_code - ERROR_TABLE_BASE_krb5;
7950Sstevel@tonic-gate 	if (error.error > 127)
7960Sstevel@tonic-gate 		error.error = KRB_ERR_GENERIC;
7970Sstevel@tonic-gate 	if (err_text)
7980Sstevel@tonic-gate 		text = err_text;
7990Sstevel@tonic-gate 	else
8000Sstevel@tonic-gate 		text = error_message(err_code);
8010Sstevel@tonic-gate 	error.text.length = strlen(text) + 1;
8022881Smp153739 	error.text.data = malloc((unsigned int) error.text.length);
8032881Smp153739 	if (error.text.data) {
8040Sstevel@tonic-gate 		strcpy(error.text.data, text);
8050Sstevel@tonic-gate 		if (!krb5_mk_error(context, &error, &outbuf)) {
8060Sstevel@tonic-gate 			(void) krb5_write_message(context, (void *)&fd,&outbuf);
8070Sstevel@tonic-gate 			krb5_free_data_contents(context, &outbuf);
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 		free(error.text.data);
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate 
update_last_prop_file(hostname,file_name)8130Sstevel@tonic-gate void update_last_prop_file(hostname, file_name)
8140Sstevel@tonic-gate 	char *hostname;
8150Sstevel@tonic-gate 	char *file_name;
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	/* handle slave locking/failure stuff */
8180Sstevel@tonic-gate 	char *file_last_prop;
8190Sstevel@tonic-gate 	int fd;
8200Sstevel@tonic-gate 	static char last_prop[]=".last_prop";
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if ((file_last_prop = (char *)malloc(strlen(file_name) +
8230Sstevel@tonic-gate 					     strlen(hostname) + 1 +
8240Sstevel@tonic-gate 					     strlen(last_prop) + 1)) == NULL) {
8250Sstevel@tonic-gate 		com_err(progname, ENOMEM,
8260Sstevel@tonic-gate 			gettext("while allocating filename for update_last_prop_file"));
8270Sstevel@tonic-gate 		return;
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	strcpy(file_last_prop, file_name);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * If a nondefault file name was specified then we should not add an
8330Sstevel@tonic-gate 	 * extraneous host name to the file name given that a file name could
8340Sstevel@tonic-gate 	 * have already specified a host name and therefore would be redundant.
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	if (strcmp(file_name, KPROP_DEFAULT_FILE) == 0) {
8372881Smp153739 	strcat(file_last_prop, ".");
8382881Smp153739 	strcat(file_last_prop, hostname);
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 	strcat(file_last_prop, last_prop);
8410Sstevel@tonic-gate 	if ((fd = THREEPARAMOPEN(file_last_prop, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
8420Sstevel@tonic-gate 		com_err(progname, errno,
8430Sstevel@tonic-gate 			gettext("while creating 'last_prop' file, '%s'"),
8440Sstevel@tonic-gate 			file_last_prop);
8450Sstevel@tonic-gate 		free(file_last_prop);
8460Sstevel@tonic-gate 		return;
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 	write(fd, "", 1);
8490Sstevel@tonic-gate 	free(file_last_prop);
8500Sstevel@tonic-gate 	close(fd);
8510Sstevel@tonic-gate 	return;
8520Sstevel@tonic-gate }
853